-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwwp-switcher.cpp
630 lines (516 loc) · 18.1 KB
/
wwp-switcher.cpp
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#include <cstring>
#include <iostream>
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <giomm/desktopappinfo.h>
#include <giomm/file.h>
#include <giomm/init.h>
#include <pango/pangocairo.h>
#include <wayfire/plugin.hpp>
#include <wayfire/render-manager.hpp>
#include <wayfire/plugins/common/input-grab.hpp>
#include "wayfire/plugins/common/util.hpp"
#include <wayfire/per-output-plugin.hpp>
#include <wayfire/signal-definitions.hpp>
#include <wayfire/view.hpp>
#include <wayfire/workspace-stream.hpp>
#include <wayfire/workspace-set.hpp>
#include <wayfire/plugins/common/cairo-util.hpp>
#include <wayfire/scene.hpp>
#include <wayfire/scene-render.hpp>
#include <wayfire/scene-operations.hpp>
#include <wayfire/seat.hpp>
#include <wayfire/window-manager.hpp>
#include <wayfire/workarea.hpp>
/*
* This plugin provides abilities to switch between views.
* It works similarly to the alt-tab binding in Windows or GNOME
*/
class switcher_texture
{
public:
wf::geometry_t rect;
std::unique_ptr<wf::simple_texture_t> texture;
cairo_t *cr = nullptr;
cairo_surface_t *cairo_surface;
PangoLayout *layout;
PangoFontDescription *desc;
};
/* Icon loading functions */
namespace IconProvider
{
using Icon = Glib::RefPtr<Gio::Icon>;
namespace
{
std::string tolower(std::string str)
{
for (auto& c : str)
c = std::tolower(c);
return str;
}
}
/* Gio::DesktopAppInfo
*
* Usually knowing the app_id, we can get a desktop app info from Gio
* The filename is either the app_id + ".desktop" or lower_app_id + ".desktop" */
Icon get_from_desktop_app_info(std::string app_id)
{
Glib::RefPtr<Gio::DesktopAppInfo> app_info;
std::vector<std::string> prefixes = {
"",
"/usr/share/applications/",
"/usr/share/applications/kde/",
"/usr/share/applications/org.kde.",
"/usr/local/share/applications/",
"/usr/local/share/applications/org.kde.",
};
std::vector<std::string> app_id_variations = {
app_id,
tolower(app_id),
};
std::vector<std::string> suffixes = {
"",
".desktop"
};
for (auto& prefix : prefixes)
{
for (auto& id : app_id_variations)
{
for (auto& suffix : suffixes)
{
if (!app_info)
{
app_info = Gio::DesktopAppInfo
::create_from_filename(prefix + id + suffix);
}
}
}
}
if (app_info) // success
return app_info->get_icon();
return Icon{};
}
std::string get_image_path(std::string icon_theme_path, std::string icon_string)
{
auto path = icon_theme_path + "/24x24/" + icon_string + ".svg";
if (Gio::File::create_for_path(path)->query_exists())
return path;
path = icon_theme_path + "/24x24/" + icon_string + ".png";
if (Gio::File::create_for_path(path)->query_exists())
return path;
path = icon_theme_path + "/scalable/" + icon_string + ".svg";
if (Gio::File::create_for_path(path)->query_exists())
return path;
return "";
}
/* Second method: Just look up the built-in icon theme,
* perhaps some icon can be found there */
std::string get_image_path_from_icon(std::string icon_theme, std::string app_id)
{
// TODO: Implement entire algorithm from https://specifications.freedesktop.org/icon-theme-spec/latest/ar01s05.html
auto icon_theme_path = "/usr/share/icons/" + icon_theme;
if (!Gio::File::create_for_path(icon_theme_path + "/index.theme")->query_exists()) {
icon_theme_path = "/usr/local/share/icons/" + icon_theme;
if (!Gio::File::create_for_path(icon_theme_path + "/index.theme")->query_exists()) {
// TODO: Icon theme does not exist
}
}
auto icon = get_from_desktop_app_info(app_id);
if (icon) {
auto icon_string = icon->to_string();
if (icon_string.c_str()[0] == '/') {
if (Gio::File::create_for_path(icon_string)->query_exists()) {
return icon_string;
}
} else {
auto path = get_image_path(icon_theme_path, "apps/" + icon_string);
if (path != "")
return path;
}
}
auto path = get_image_path(icon_theme_path, "apps/" + app_id);
if (path != "")
return path;
path = get_image_path(icon_theme_path, "mimetypes/application-x-executable");
if (icon_theme != "hicolor") {
auto hicolor_path = get_image_path_from_icon("hicolor", app_id);
if (path == "" || (hicolor_path.find("mimetypes/application-x-executable") == std::string::npos && hicolor_path != ""))
return hicolor_path;
}
return path;
}
};
namespace wf
{
namespace scene
{
class simple_node_render_instance_t : public render_instance_t
{
wf::signal::connection_t<node_damage_signal> on_node_damaged =
[=] (node_damage_signal *ev)
{
push_to_parent(ev->region);
};
node_t *self;
damage_callback push_to_parent;
std::shared_ptr<switcher_texture> st;
int *x, *y, *w, *h;
public:
simple_node_render_instance_t(node_t *self, damage_callback push_dmg,
int *x, int *y, int *w, int *h, std::shared_ptr<switcher_texture> st)
{
this->x = x;
this->y = y;
this->w = w;
this->h = h;
this->self = self;
this->st = st;
this->push_to_parent = push_dmg;
self->connect(&on_node_damaged);
}
void schedule_instructions(
std::vector<render_instruction_t>& instructions,
const wf::render_target_t& target, wf::region_t& damage)
{
// We want to render ourselves only, the node does not have children
instructions.push_back(render_instruction_t{
.instance = this,
.target = target,
.damage = damage & self->get_bounding_box(),
});
}
void render(const wf::render_target_t& target,
const wf::region_t& region)
{
auto ol = this->st;
wlr_box og = {*x, *y, *w, *h};
auto rect = st->rect;
if (!st->texture)
return;
OpenGL::render_begin(target);
for (auto& box : region)
{
target.logic_scissor(wlr_box_from_pixman_box(box));
rect.x += og.x;
rect.y += og.y;
OpenGL::render_texture(wf::texture_t{st->texture->tex},
target, rect, glm::vec4(1, 1, 1, 1),
OpenGL::TEXTURE_TRANSFORM_INVERT_Y);
}
OpenGL::render_end();
}
};
class simple_node_t : public node_t
{
int x, y, w, h;
public:
std::shared_ptr<switcher_texture> st;
simple_node_t(int x, int y, int w, int h) : node_t(false)
{
this->x = x;
this->y = y;
this->w = w;
this->h = h;
st = std::make_shared<switcher_texture>();
}
void gen_render_instances(std::vector<render_instance_uptr>& instances,
damage_callback push_damage, wf::output_t *shown_on) override
{
// push_damage accepts damage in the parent's coordinate system
// If the node is a transformer, it may transform the damage. However,
// this simple nodes does not need any transformations, so the push_damage
// callback is just passed along.
instances.push_back(std::make_unique<simple_node_render_instance_t>(
this, push_damage, &x, &y, &w, &h, st));
}
void do_push_damage(wf::region_t updated_region)
{
node_damage_signal ev;
ev.region = updated_region;
this->emit(&ev);
}
wf::geometry_t get_bounding_box() override
{
// Specify whatever geometry your node has
return {x, y, w, h};
}
void set_position(int x, int y)
{
this->x = x;
this->y = y;
}
void set_size(int w, int h)
{
this->w = w;
this->h = h;
}
};
std::shared_ptr<simple_node_t> add_simple_node(wf::output_t *output, int x, int y,
int w, int h)
{
auto subnode = std::make_shared<simple_node_t>(x, y, w, h);
wf::scene::add_front(output->node_for_layer(wf::scene::layer::OVERLAY), subnode);
return subnode;
}
class wayfire_simple_switcher : public wf::per_output_plugin_instance_t, public wf::keyboard_interaction_t
{
wf::option_wrapper_t<wf::keybinding_t> activate_key{"wwp-switcher/activate"};
wf::option_wrapper_t<wf::color_t> background_color{"wwp-switcher/background_color"};
wf::option_wrapper_t<wf::color_t> text_color{"wwp-switcher/text_color"};
wf::option_wrapper_t<std::string> font{"wwp-switcher/font"};
wf::option_wrapper_t<std::string> icon_theme{"wwp-switcher/icon_theme"};
std::vector<std::vector<std::shared_ptr<simple_node_t>>> switcher_textures;
std::vector<wayfire_toplevel_view> views; // all views on current viewport
size_t current_view_index = 0;
bool active = false;
std::unique_ptr<wf::input_grab_t> input_grab;
wf::plugin_activation_data_t grab_interface = {
.name = "wwp-switcher",
.capabilities = wf::CAPABILITY_MANAGE_COMPOSITOR
};
public:
void init() override
{
auto wsize = output->wset()->get_workspace_grid_size();
switcher_textures.resize(wsize.width);
for (int x = 0; x < wsize.width; x++)
{
switcher_textures[x].resize(wsize.height);
}
output->add_key(activate_key, &simple_switch);
input_grab = std::make_unique<wf::input_grab_t>("wwp-switcher", output, this, nullptr, nullptr);
grab_interface.cancel = [=] () { switch_terminate(); };
Gio::init();
}
void handle_keyboard_key(wf::seat_t*, wlr_keyboard_key_event event) override
{
auto mod = wf::get_core().seat->modifier_from_keycode(event.keycode);
if ((event.state == WLR_KEY_RELEASED) && (mod & ((wf::keybinding_t)activate_key).get_modifiers()))
switch_terminate();
}
std::shared_ptr<switcher_texture> get_current_st()
{
auto wsize = output->wset()->get_workspace_grid_size();
auto ws = output->wset()->get_current_workspace();
auto og = output->get_relative_geometry();
for (int x = 0; x < wsize.width; x++)
{
for (int y = 0; y < wsize.height; y++)
{
if (!switcher_textures[x][y])
{
switcher_textures[x][y] = add_simple_node(output, x * og.width, y * og.height,
og.width, og.height);
}
}
}
return switcher_textures[ws.x][ws.y]->st;
}
void update_views()
{
views = output->wset()->get_views(
wf::WSET_CURRENT_WORKSPACE | wf::WSET_MAPPED_ONLY | wf::WSET_EXCLUDE_MINIMIZED);
std::sort(views.begin(), views.end(), [] (wayfire_toplevel_view& a, wayfire_toplevel_view& b)
{
return wf::get_focus_timestamp(a) > wf::get_focus_timestamp(b);
});
}
wf::signal::connection_t<wf::view_disappeared_signal> cleanup_view = [=] (wf::view_disappeared_signal *ev)
{
auto view = ev->view;
if (!view)
{
return;
}
size_t i = 0;
for (; i < views.size() && views[i] != view; i++)
{}
if (i == views.size())
{
return;
}
views.erase(views.begin() + i);
if (views.empty())
{
switch_terminate();
return;
}
if (i <= current_view_index)
{
current_view_index =
(current_view_index + views.size() - 1) % views.size();
}
};
void cairo_clear(cairo_t *cr)
{
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);
}
void recreate_switcher_texture()
{
auto st = get_current_st();
cairo_t *cr = st->cr;
cairo_surface_t *cairo_surface = st->cairo_surface;
if (!cr)
{
/* Setup dummy context to get initial font size */
cairo_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
cr = cairo_create(cairo_surface);
st->texture = std::make_unique<wf::simple_texture_t>();
st->layout = pango_cairo_create_layout(cr);
st->desc = pango_font_description_from_string(std::string(font).c_str());
}
st->rect.width = 480;
st->rect.height = views.size() * 48 + 24;
/* Recreate surface based on font size */
cairo_destroy(cr);
cairo_surface_destroy(cairo_surface);
cairo_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
st->rect.width, st->rect.height);
cr = cairo_create(cairo_surface);
pango_cairo_update_layout(cr, st->layout);
st->cr = cr;
st->cairo_surface = cairo_surface;
auto workarea = output->workarea->get_workarea();
st->rect.x = workarea.x + (workarea.width / 2 - st->rect.width / 2);
st->rect.y = workarea.y + (workarea.height / 2 - st->rect.height / 2);
}
void render_switcher_texture()
{
recreate_switcher_texture();
auto st = get_current_st();
double xc = st->rect.width / 2;
double yc = st->rect.height / 2;
int x2, y2;
cairo_t *cr = st->cr;
cairo_clear(cr);
x2 = st->rect.width;
y2 = st->rect.height;
cairo_set_source_rgba(cr,
wf::color_t(background_color).r,
wf::color_t(background_color).g,
wf::color_t(background_color).b,
wf::color_t(background_color).a);
cairo_new_path(cr);
cairo_move_to(cr, 0, y2);
cairo_line_to(cr, 0, 0);
cairo_line_to(cr, x2, 0);
cairo_line_to(cr, x2, y2);
cairo_close_path(cr);
cairo_fill(cr);
int width, height;
pango_layout_get_size(st->layout, &width, &height);
cairo_move_to(cr,
xc - width / PANGO_SCALE / 2,
yc - height / PANGO_SCALE / 2);
wayfire_view view;
for (long unsigned int i = 0; i < views.size(); i++)
{
view = views[i];
std::string icon_path = IconProvider::get_image_path_from_icon(std::string(icon_theme).c_str(), view->get_app_id());
if (icon_path != "") {
auto pixbuf = gdk_pixbuf_new_from_file_at_size(icon_path.c_str(), 24, 24, nullptr);
gdk_cairo_set_source_pixbuf(cr, pixbuf, 24, i * 48 + 24);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_rectangle(cr, 24, i * 48 + 24, 24, 24);
cairo_fill(cr);
}
cairo_move_to(cr, 72, i * 48 + 24);
cairo_set_source_rgba(cr,
wf::color_t(text_color).r,
wf::color_t(text_color).g,
wf::color_t(text_color).b,
wf::color_t(text_color).a);
pango_font_description_set_weight(st->desc, i == current_view_index ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
pango_layout_set_font_description(st->layout, st->desc);
pango_layout_set_ellipsize(st->layout, PANGO_ELLIPSIZE_END);
pango_layout_set_width(st->layout, 384 * PANGO_SCALE);
pango_layout_set_text(st->layout, view->get_title().c_str(), -1);
pango_cairo_show_layout(cr, st->layout);
}
cairo_stroke(cr);
OpenGL::render_begin();
cairo_surface_upload_to_texture(st->cairo_surface, *st->texture);
OpenGL::render_end();
output->render->damage_whole();
}
wf::key_callback simple_switch = [=] (auto)
{
if (active)
{
switch_next();
return true;
}
if (!output->activate_plugin(&grab_interface))
{
return false;
}
update_views();
if (views.size() < 1)
{
output->deactivate_plugin(&grab_interface);
return false;
}
current_view_index = 0;
active = true;
input_grab->grab_input(wf::scene::layer::OVERLAY);
input_grab->set_wants_raw_input(true);
switch_next();
output->connect(&cleanup_view);
return true;
};
void switcher_texture_destroy(std::shared_ptr<switcher_texture> st)
{
if (!st->cr)
{
return;
}
st->texture.reset();
cairo_surface_destroy(st->cairo_surface);
cairo_destroy(st->cr);
st->cr = nullptr;
g_object_unref(st->layout);
pango_font_description_free(st->desc);
}
void switch_terminate()
{
output->render->damage_whole();
input_grab->ungrab_input();
output->deactivate_plugin(&grab_interface);
wf::get_core().default_wm->focus_raise_view(views[current_view_index]);
active = false;
output->disconnect(&cleanup_view);
auto wsize = output->wset()->get_workspace_grid_size();
for (int x = 0; x < wsize.width; x++)
{
for (int y = 0; y < wsize.height; y++)
{
auto& st = switcher_textures[x][y]->st;
switcher_texture_destroy(st);
st.reset();
wf::scene::remove_child(switcher_textures[x][y]);
switcher_textures[x][y].reset();
}
}
output->render->damage_whole();
}
void switch_next()
{
#define index current_view_index
index = (index + 1) % views.size();
#undef index
render_switcher_texture();
}
void fini() override
{
if (active)
{
switch_terminate();
}
output->rem_binding(&simple_switch);
}
};
DECLARE_WAYFIRE_PLUGIN(wf::per_output_plugin_t<wayfire_simple_switcher>);
}
}