-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathpanel.cpp
More file actions
612 lines (513 loc) · 16 KB
/
panel.cpp
File metadata and controls
612 lines (513 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#include <glibmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/headerbar.h>
#include <gtkmm/box.h>
#include <gtkmm/application.h>
#include <gdk/wayland/gdkwayland.h>
#include <gtk4-layer-shell.h>
#include <iostream>
#include <memory>
#include <sstream>
#include <map>
#include <css-config.hpp>
#include "panel.hpp"
#include "widgets/battery.hpp"
#include "widgets/command-output.hpp"
#include "widgets/language.hpp"
#include "widgets/menu.hpp"
#include "widgets/clock.hpp"
#ifdef HAVE_WEATHER
#include "widgets/weather.hpp"
#endif
#include "widgets/launchers.hpp"
#include "widgets/network.hpp"
#include "widgets/spacing.hpp"
#include "widgets/separator.hpp"
#include "widgets/workspace-switcher.hpp"
#ifdef HAVE_PULSE
#include "widgets/volume.hpp"
#endif
#ifdef HAVE_WIREPLUMBER
#include "widgets/wp-mixer/wp-mixer.hpp"
#endif
#include "widgets/window-list/window-list.hpp"
#include "widgets/notifications/notification-center.hpp"
#include "widgets/tray/tray.hpp"
#include "wf-autohide-window.hpp"
class WayfirePanel::impl
{
std::unique_ptr<WayfireAutohidingWindow> window;
Gtk::CenterBox content_box;
Gtk::Box left_box, center_box, right_box;
using Widget = std::unique_ptr<WayfireWidget>;
using WidgetContainer = std::vector<Widget>;
WidgetContainer left_widgets, center_widgets, right_widgets;
WayfireOutput *output;
WfOption<std::string> panel_layer{"panel/layer"};
std::function<void()> set_panel_layer = [=] ()
{
if ((std::string)panel_layer == "overlay")
{
gtk_layer_set_layer(window->gobj(), GTK_LAYER_SHELL_LAYER_OVERLAY);
}
if ((std::string)panel_layer == "top")
{
gtk_layer_set_layer(window->gobj(), GTK_LAYER_SHELL_LAYER_TOP);
}
if ((std::string)panel_layer == "bottom")
{
gtk_layer_set_layer(window->gobj(), GTK_LAYER_SHELL_LAYER_BOTTOM);
}
if ((std::string)panel_layer == "background")
{
gtk_layer_set_layer(window->gobj(), GTK_LAYER_SHELL_LAYER_BACKGROUND);
}
};
WfOption<int> minimal_panel_height{"panel/minimal_height"};
void create_window()
{
window = std::make_unique<WayfireAutohidingWindow>(output, "panel");
window->set_default_size(0, minimal_panel_height);
window->add_css_class("wf-panel");
panel_layer.set_callback(set_panel_layer);
set_panel_layer(); // initial setting
gtk_layer_set_anchor(window->gobj(), GTK_LAYER_SHELL_EDGE_LEFT, true);
gtk_layer_set_anchor(window->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, true);
gtk_layer_set_margin(window->gobj(), GTK_LAYER_SHELL_EDGE_LEFT, 0);
gtk_layer_set_margin(window->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, 0);
window->present();
init_layout();
}
void init_layout()
{
left_box.add_css_class("left");
right_box.add_css_class("right");
center_box.add_css_class("center");
content_box.set_start_widget(left_box);
if (!center_box.get_children().empty())
{
content_box.set_center_widget(center_box);
}
content_box.set_end_widget(right_box);
content_box.set_hexpand(true);
left_box.set_halign(Gtk::Align::START);
center_box.set_halign(Gtk::Align::CENTER);
right_box.set_halign(Gtk::Align::END);
window->set_child(content_box);
}
std::optional<int> widget_with_value(std::string value, std::string prefix)
{
if (value.find(prefix) == 0)
{
auto output_str = value.substr(prefix.size());
int output = std::atoi(output_str.c_str());
if (output > 0)
{
return output;
}
std::cerr << "Invalid widget value: " << value << std::endl;
}
return {};
}
Widget widget_from_name(std::string name)
{
if (name == "menu")
{
return Widget(new WayfireMenu(output));
}
if (name == "launchers")
{
return Widget(new WayfireLaunchers());
}
if (name == "clock")
{
return Widget(new WayfireClock());
}
#ifdef HAVE_WEATHER
if (name == "weather")
{
return Widget(new WayfireWeather());
}
#endif
if (name == "network")
{
return Widget(new WayfireNetworkInfo());
}
if (name == "battery")
{
return Widget(new WayfireBatteryInfo());
}
if (name == "volume")
{
#ifdef HAVE_PULSE
return Widget(new WayfireVolume());
#else
std::cerr << "Built without pulse support, volume widget "
" is not available." << std::endl;
#endif
}
if (name == "wp-mixer")
{
#ifdef HAVE_WIREPLUMBER
return Widget(new WayfireWpMixer());
#else
std::cerr << "Built without wireplumber support, mixer widget "
" is not available." << std::endl;
#endif
}
if (name == "window-list")
{
return Widget(new WayfireWindowList(output));
}
if (name == "notifications")
{
return Widget(new WayfireNotificationCenter());
}
if (name == "tray")
{
return Widget(new WayfireStatusNotifier());
}
if (name == "command-output")
{
return Widget(new WfCommandOutputButtons());
}
if (name == "language")
{
if (get_ipc_server_instance()->connected)
{
return Widget(new WayfireLanguage());
} else
{
std::cerr << "Wayfire IPC not connected, which is required to load language widget." <<
std::endl;
return nullptr;
}
}
if (name == "workspace-switcher")
{
if (get_ipc_server_instance()->connected)
{
return Widget(new WayfireWorkspaceSwitcher(output));
} else
{
std::cerr <<
"Wayfire IPC not connected, which is required to load workspace-switcher widget." <<
std::endl;
return nullptr;
}
}
if (auto pixel = widget_with_value(name, "spacing"))
{
return Widget(new WayfireSpacing(*pixel));
}
if (auto pixel = widget_with_value(name, "separator"))
{
return Widget(new WayfireSeparator(*pixel));
}
if (name != "none")
{
std::cerr << "Invalid widget: " << name << std::endl;
}
return nullptr;
}
static std::vector<std::string> tokenize(std::string list)
{
std::string token;
std::istringstream stream(list);
std::vector<std::string> result;
while (stream >> token)
{
if (token.size())
{
result.push_back(token);
}
}
return result;
}
void reload_widgets(std::string list, WidgetContainer& container,
Gtk::Box& box)
{
const auto lock_sn_watcher = Watcher::Instance();
const auto lock_notification_daemon = Daemon::Instance();
for (auto child : box.get_children())
{
box.remove(*child);
}
container.clear();
auto widgets = tokenize(list);
for (auto widget_name : widgets)
{
if (widget_name == "window-list")
{
box.set_hexpand(true);
}
auto widget = widget_from_name(widget_name);
if (!widget)
{
continue;
}
widget->widget_name = widget_name;
widget->init(&box);
container.push_back(std::move(widget));
}
}
WfOption<std::string> left_widgets_opt{"panel/widgets_left"};
WfOption<std::string> right_widgets_opt{"panel/widgets_right"};
WfOption<std::string> center_widgets_opt{"panel/widgets_center"};
public:
impl(WayfireOutput *output) : output(output)
{
create_window();
}
wl_surface *get_wl_surface()
{
return window->get_wl_surface();
}
Gtk::Window& get_window()
{
return *window;
}
void handle_config_reload()
{
for (auto& w : left_widgets)
{
w->handle_config_reload();
}
for (auto& w : right_widgets)
{
w->handle_config_reload();
}
for (auto& w : center_widgets)
{
w->handle_config_reload();
}
}
WayfirePanelApp *panel_app;
void set_panel_app(WayfirePanelApp *panel_app)
{
this->panel_app = panel_app;
}
void init_widgets()
{
left_widgets_opt.set_callback([=] ()
{
reload_widgets((std::string)left_widgets_opt, left_widgets, left_box);
});
right_widgets_opt.set_callback([=] ()
{
reload_widgets((std::string)right_widgets_opt, right_widgets, right_box);
});
center_widgets_opt.set_callback([=] ()
{
reload_widgets((std::string)center_widgets_opt, center_widgets, center_box);
if (center_box.get_children().empty())
{
content_box.unset_center_widget();
} else
{
content_box.set_center_widget(center_box);
}
});
reload_widgets((std::string)left_widgets_opt, left_widgets, left_box);
reload_widgets((std::string)right_widgets_opt, right_widgets, right_box);
reload_widgets((std::string)center_widgets_opt, center_widgets, center_box);
}
std::shared_ptr<WayfireIPC> get_ipc_server_instance()
{
return panel_app->get_ipc_server_instance();
}
};
WayfirePanel::WayfirePanel(WayfireOutput *output) : pimpl(new impl(output))
{}
wl_surface*WayfirePanel::get_wl_surface()
{
return pimpl->get_wl_surface();
}
Gtk::Window& WayfirePanel::get_window()
{
return pimpl->get_window();
}
void WayfirePanel::handle_config_reload()
{
pimpl->handle_config_reload();
}
void WayfirePanel::init_widgets()
{
pimpl->init_widgets();
}
void WayfirePanel::set_panel_app(WayfirePanelApp *panel_app)
{
pimpl->set_panel_app(panel_app);
}
class WayfirePanelApp::impl
{
public:
std::map<WayfireOutput*, std::unique_ptr<WayfirePanel>> panels;
WfOption<std::string> *panel_outputs = NULL;
};
void WayfirePanelApp::on_config_reload()
{
if (!priv->panel_outputs)
{
priv->panel_outputs = new WfOption<std::string>("panel/outputs");
}
for (auto& p : priv->panels)
{
p.second->handle_config_reload();
}
}
bool WayfirePanelApp::panel_allowed_by_config(bool allowed, std::string output_name)
{
if (allowed)
{
return std::string(*priv->panel_outputs).find("*") != std::string::npos ||
std::string(*priv->panel_outputs).find(output_name) != std::string::npos;
} else
{
return std::string(*priv->panel_outputs).find("*") == std::string::npos &&
std::string(*priv->panel_outputs).find(output_name) == std::string::npos;
}
}
void WayfirePanelApp::update_panels()
{
for (auto& o : *get_wayfire_outputs())
{
auto output = o.get();
auto output_name = o->monitor->get_connector();
if (panel_allowed_by_config(false, output_name))
{
std::cout << "Removing panel from output: " << output_name << std::endl;
priv->panels.erase(output);
}
const auto it = std::find_if(priv->panels.begin(), priv->panels.end(),
[&output_name] (const auto& panel)
{
return panel.first->monitor->get_connector() == output_name;
});
if ((it == priv->panels.end()) && panel_allowed_by_config(true, output_name))
{
std::cout << "Adding panel for output: " << output_name << std::endl;
priv->panels[output] = std::unique_ptr<WayfirePanel>(
new WayfirePanel(output));
if (ipc_server)
{
priv->panels[output]->handle_config_reload();
priv->panels[output]->set_panel_app(this);
priv->panels[output]->init_widgets();
}
}
}
}
void WayfirePanelApp::on_activate()
{
WayfireShellApp::on_activate();
priv->panel_outputs->set_callback([=] ()
{
update_panels();
});
if (!ipc_server)
{
ipc_server = WayfireIPC::get_instance();
}
for (auto& p : priv->panels)
{
p.second->handle_config_reload();
p.second->set_panel_app(this);
p.second->init_widgets();
}
if (priv->panels.empty())
{
std::cout << std::endl <<
"WARNING: wf-panel outputs option did not match any outputs, " \
"so none were created. Set the [panel] outputs option to * " \
"wildcard character in wf-shell configuariton file to match " \
"all outputs, or set one or more of the following detected outputs:" <<
std::endl << std::endl;
for (auto& o : *get_wayfire_outputs())
{
std::cout << o->monitor->get_connector() << std::endl;
}
std::cout << std::endl << "Currently the [panel] outputs option is set to: " <<
std::string(*priv->panel_outputs) << std::endl;
}
const static std::vector<std::pair<std::string, std::string>> icon_sizes_args =
{
{"panel/minimal_height", ""},
{"panel/menu_icon_size", ".menu-icon"},
{"panel/launchers_size", ".launcher"},
{"panel/battery_icon_size", ".battery image"},
{"panel/network_icon_size", ".network"},
{"panel/volume_icon_size", ".volume"},
{"panel/wp_icon_size", ".wireplumber"},
{"panel/notifications_icon_size", ".notification-center "},
{"panel/tray_icon_size", ".tray-button"}
};
for (auto pair : icon_sizes_args)
{
new CssFromConfigIconSize(pair.first, pair.second);
}
new CssFromConfigInt("panel/launchers_spacing", ".launcher{padding: 0px ", "px;}");
new CssFromConfigString("panel/background_color", ".wf-panel{background-color:", ";}");
new CssFromConfigBool("panel/battery_icon_invert", ".battery image{filter:invert(100%);}", "");
new CssFromConfigBool("panel/network_icon_invert_color", ".network-icon{filter:invert(100%);}", "");
new CssFromConfigFont("panel/battery_font", ".battery {", "}");
new CssFromConfigFont("panel/clock_font", ".clock {", "}");
new CssFromConfigFont("panel/weather_font", ".weather {", "}");
}
std::shared_ptr<WayfireIPC> WayfirePanelApp::get_ipc_server_instance()
{
return ipc_server;
}
void WayfirePanelApp::handle_new_output(WayfireOutput *output)
{
update_panels();
}
WayfirePanel*WayfirePanelApp::panel_for_wl_output(wl_output *output)
{
for (auto& p : priv->panels)
{
if (p.first->wo == output)
{
return p.second.get();
}
}
return nullptr;
}
void WayfirePanelApp::handle_output_removed(WayfireOutput *output)
{
priv->panels.erase(output);
}
WayfirePanelApp& WayfirePanelApp::get()
{
if (!instance)
{
throw std::logic_error("Calling WayfirePanelApp::get() before starting app!");
}
return dynamic_cast<WayfirePanelApp&>(*instance.get());
}
void WayfirePanelApp::create(int argc, char **argv)
{
if (instance)
{
throw std::logic_error("Running WayfirePanelApp twice!");
}
instance = std::unique_ptr<WayfireShellApp>(new WayfirePanelApp{});
instance->init_app();
instance->run(argc, argv);
}
std::string WayfirePanelApp::get_application_name()
{
return "org.wayfire.panel";
}
Gio::Application::Flags WayfirePanelApp::get_extra_application_flags()
{
return Gio::Application::Flags::NON_UNIQUE;
}
WayfirePanelApp::~WayfirePanelApp() = default;
WayfirePanelApp::WayfirePanelApp() : WayfireShellApp(), priv(new impl())
{}
int main(int argc, char **argv)
{
WayfirePanelApp::create(argc, argv);
return 0;
}