-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathwm-actions.cpp
505 lines (440 loc) · 15.8 KB
/
wm-actions.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
#include <wayfire/view.hpp>
#include <wayfire/per-output-plugin.hpp>
#include <wayfire/output.hpp>
#include <wayfire/workspace-set.hpp>
#include <wayfire/util/log.hpp>
#include "plugins/common/wayfire/plugins/common/shared-core-data.hpp"
#include "plugins/ipc/ipc-method-repository.hpp"
#include "plugins/ipc/ipc-helpers.hpp"
#include "plugins/ipc/ipc-activator.hpp"
#include "wayfire/core.hpp"
#include "wayfire/plugin.hpp"
#include "wayfire/scene-operations.hpp"
#include "wayfire/scene.hpp"
#include "wayfire/signal-definitions.hpp"
#include "wayfire/signal-provider.hpp"
#include "wayfire/toplevel-view.hpp"
#include "wayfire/window-manager.hpp"
#include "wayfire/seat.hpp"
#include "wayfire/nonstd/reverse.hpp"
#include "wm-actions-signals.hpp"
#include "wayfire/nonstd/reverse.hpp"
class always_on_top_root_node_t : public wf::scene::output_node_t
{
public:
using output_node_t::output_node_t;
std::string stringify() const override
{
return "always-on-top for output " + get_output()->to_string() + " " + stringify_flags();
}
};
class wayfire_wm_actions_output_t : public wf::per_output_plugin_instance_t
{
wf::scene::floating_inner_ptr always_above;
bool showdesktop_active = false;
wf::option_wrapper_t<wf::activatorbinding_t> minimize{
"wm-actions/minimize"};
wf::option_wrapper_t<wf::activatorbinding_t> toggle_maximize{
"wm-actions/toggle_maximize"};
wf::option_wrapper_t<wf::activatorbinding_t> toggle_above{
"wm-actions/toggle_always_on_top"};
wf::option_wrapper_t<wf::activatorbinding_t> toggle_fullscreen{
"wm-actions/toggle_fullscreen"};
wf::option_wrapper_t<wf::activatorbinding_t> toggle_sticky{
"wm-actions/toggle_sticky"};
wf::option_wrapper_t<wf::activatorbinding_t> send_to_back{
"wm-actions/send_to_back"};
wf::plugin_activation_data_t grab_interface = {
.name = "wm-actions",
.capabilities = 0,
};
public:
bool set_keep_above_state(wayfire_view view, bool above)
{
if (!view || !output->can_activate_plugin(&grab_interface))
{
return false;
}
if (above)
{
wf::scene::readd_front(always_above, view->get_root_node());
view->store_data(std::make_unique<wf::custom_data_t>(),
"wm-actions-above");
} else
{
wf::scene::readd_front(output->wset()->get_node(), view->get_root_node());
if (view->has_data("wm-actions-above"))
{
view->erase_data("wm-actions-above");
}
}
wf::wm_actions_above_changed_signal data;
data.view = view;
output->emit(&data);
return true;
}
/**
* Find the selected toplevel view, or nullptr if the selected view is not
* toplevel.
*/
wayfire_toplevel_view choose_view(wf::activator_source_t source)
{
wayfire_view view;
if (source == wf::activator_source_t::BUTTONBINDING)
{
view = wf::get_core().get_cursor_focus_view();
} else
{
view = wf::get_core().seat->get_active_view();
}
return wf::toplevel_cast(view);
}
/**
* Calling a specific view / specific keep_above action via signal
*/
wf::signal::connection_t<wf::wm_actions_set_above_state_signal> on_set_above_state_signal =
[=] (wf::wm_actions_set_above_state_signal *signal)
{
if (!set_keep_above_state(signal->view, signal->above))
{
LOG(wf::log::LOG_LEVEL_DEBUG, "view above action failed via signal.");
}
};
/**
* Ensures views marked as above are still above if their output changes.
*/
wf::signal::connection_t<wf::view_moved_to_wset_signal> on_view_output_changed =
[=] (wf::view_moved_to_wset_signal *signal)
{
if (!signal->new_wset || (signal->new_wset->get_attached_output() != output))
{
return;
}
auto view = signal->view;
if (!view)
{
return;
}
if (view->has_data("wm-actions-above"))
{
wf::scene::readd_front(always_above, view->get_root_node());
}
};
/**
* Ensures views marked as above are still above if they are minimized and
* unminimized.
*/
wf::signal::connection_t<wf::view_minimized_signal> on_view_minimized =
[=] (wf::view_minimized_signal *ev)
{
if (ev->view->get_output() != output)
{
return;
}
if (ev->view->has_data("wm-actions-above") && !ev->view->minimized)
{
wf::scene::readd_front(always_above, ev->view->get_root_node());
}
};
void check_disable_showdesktop(wayfire_view view)
{
if ((view->role != wf::VIEW_ROLE_TOPLEVEL) || !view->is_mapped())
{
return;
}
disable_showdesktop();
}
/**
* Disables show desktop if the workspace is changed or any view is attached,
* mapped or unminimized.
*/
wf::signal::connection_t<wf::view_set_output_signal> view_set_output =
[=] (wf::view_set_output_signal *ev)
{
check_disable_showdesktop(ev->view);
};
wf::signal::connection_t<wf::view_mapped_signal> on_view_mapped = [=] (wf::view_mapped_signal *ev)
{
check_disable_showdesktop(ev->view);
};
wf::signal::connection_t<wf::workspace_changed_signal> workspace_changed =
[=] (wf::workspace_changed_signal *ev)
{
disable_showdesktop();
};
wf::signal::connection_t<wf::view_minimized_signal> view_minimized = [=] (wf::view_minimized_signal *ev)
{
if ((ev->view->role != wf::VIEW_ROLE_TOPLEVEL) || !ev->view->is_mapped())
{
return;
}
if (!ev->view->minimized)
{
disable_showdesktop();
}
};
/**
* Execute for_view on the selected view, if available.
*/
bool execute_for_selected_view(wf::activator_source_t source,
std::function<bool(wayfire_toplevel_view)> for_view)
{
auto view = choose_view(source);
if (!view || !output->can_activate_plugin(&grab_interface))
{
return false;
}
return for_view(view);
}
/**
* The default activator bindings.
*/
wf::activator_callback on_toggle_above = [=] (auto ev) -> bool
{
auto view = choose_view(ev.source);
if (view)
{
return set_keep_above_state(view, !view->has_data("wm-actions-above"));
} else
{
return false;
}
};
wf::activator_callback on_minimize = [=] (auto ev) -> bool
{
return execute_for_selected_view(ev.source, [] (wayfire_toplevel_view view)
{
wf::get_core().default_wm->minimize_request(view, !view->minimized);
return true;
});
};
wf::activator_callback on_toggle_maximize = [=] (auto ev) -> bool
{
return execute_for_selected_view(ev.source, [] (wayfire_toplevel_view view)
{
wf::get_core().default_wm->tile_request(view,
view->pending_tiled_edges() == wf::TILED_EDGES_ALL ? 0 : wf::TILED_EDGES_ALL);
return true;
});
};
wf::activator_callback on_toggle_fullscreen = [=] (auto ev) -> bool
{
return execute_for_selected_view(ev.source, [] (wayfire_toplevel_view view)
{
wf::get_core().default_wm->fullscreen_request(view, view->get_output(),
!view->pending_fullscreen());
return true;
});
};
wf::activator_callback on_toggle_sticky = [=] (auto ev) -> bool
{
return execute_for_selected_view(ev.source, [] (wayfire_toplevel_view view)
{
view->set_sticky(view->sticky ^ 1);
return true;
});
};
bool on_toggle_showdesktop()
{
showdesktop_active = !showdesktop_active;
if (showdesktop_active)
{
for (auto& view : output->wset()->get_views())
{
if (!view->minimized)
{
wf::get_core().default_wm->minimize_request(view, true);
view->store_data(std::make_unique<wf::custom_data_t>(), "wm-actions-showdesktop");
}
}
output->connect(&view_set_output);
output->connect(&workspace_changed);
output->connect(&view_minimized);
output->connect(&on_view_mapped);
return true;
}
disable_showdesktop();
return true;
}
void do_send_to_back(wayfire_view view)
{
auto view_root = view->get_root_node();
if (auto parent =
dynamic_cast<wf::scene::floating_inner_node_t*>(view_root->parent()))
{
auto parent_children = parent->get_children();
parent_children.erase(
std::remove(parent_children.begin(), parent_children.end(),
view_root),
parent_children.end());
parent_children.push_back(view_root);
parent->set_children_list(parent_children);
wf::scene::update(parent->shared_from_this(),
wf::scene::update_flag::CHILDREN_LIST);
}
}
wf::activator_callback on_send_to_back = [=] (auto ev) -> bool
{
return execute_for_selected_view(ev.source, [this] (wayfire_view view)
{
auto views = view->get_output()->wset()->get_views(
wf::WSET_CURRENT_WORKSPACE | wf::WSET_MAPPED_ONLY |
wf::WSET_EXCLUDE_MINIMIZED | wf::WSET_SORT_STACKING);
wayfire_view bottom_view = views[views.size() - 1];
if (view != bottom_view)
{
do_send_to_back(view);
// Change focus to the last focused view on this workspace
// Update the list after restacking.
views = view->get_output()->wset()->get_views(
wf::WSET_CURRENT_WORKSPACE | wf::WSET_MAPPED_ONLY |
wf::WSET_EXCLUDE_MINIMIZED | wf::WSET_SORT_STACKING);
wf::get_core().seat->focus_view(views[0]);
}
return true;
});
};
void disable_showdesktop()
{
view_set_output.disconnect();
workspace_changed.disconnect();
view_minimized.disconnect();
auto views = output->wset()->get_views(wf::WSET_SORT_STACKING);
for (auto& view : wf::reverse(views))
{
if (view->has_data("wm-actions-showdesktop"))
{
view->erase_data("wm-actions-showdesktop");
wf::get_core().default_wm->minimize_request(view, false);
}
}
showdesktop_active = false;
}
public:
void init() override
{
always_above = std::make_shared<always_on_top_root_node_t>(output);
wf::scene::add_front(wf::get_core().scene()->layers[(int)wf::scene::layer::WORKSPACE], always_above);
output->add_activator(minimize, &on_minimize);
output->add_activator(toggle_maximize, &on_toggle_maximize);
output->add_activator(toggle_above, &on_toggle_above);
output->add_activator(toggle_fullscreen, &on_toggle_fullscreen);
output->add_activator(toggle_sticky, &on_toggle_sticky);
output->add_activator(send_to_back, &on_send_to_back);
output->connect(&on_set_above_state_signal);
output->connect(&on_view_minimized);
wf::get_core().connect(&on_view_output_changed);
}
void fini() override
{
for (auto view : output->wset()->get_views())
{
if (view->has_data("wm-actions-above"))
{
set_keep_above_state(view, false);
}
}
wf::scene::remove_child(always_above);
output->rem_binding(&on_minimize);
output->rem_binding(&on_toggle_maximize);
output->rem_binding(&on_toggle_above);
output->rem_binding(&on_toggle_fullscreen);
output->rem_binding(&on_toggle_sticky);
output->rem_binding(&on_send_to_back);
}
};
class wayfire_wm_actions_t : public wf::plugin_interface_t,
public wf::per_output_tracker_mixin_t<wayfire_wm_actions_output_t>
{
wf::shared_data::ref_ptr_t<wf::ipc::method_repository_t> ipc_repo;
wf::ipc_activator_t toggle_showdesktop{"wm-actions/toggle_showdesktop"};
public:
void init() override
{
init_output_tracking();
ipc_repo->register_method("wm-actions/set-minimized", ipc_minimize);
ipc_repo->register_method("wm-actions/set-always-on-top", ipc_set_always_on_top);
ipc_repo->register_method("wm-actions/set-fullscreen", ipc_set_fullscreen);
ipc_repo->register_method("wm-actions/set-sticky", ipc_set_sticky);
ipc_repo->register_method("wm-actions/send-to-back", ipc_send_to_back);
toggle_showdesktop.set_handler(on_toggle_showdesktop);
}
void fini() override
{
fini_output_tracking();
ipc_repo->unregister_method("wm-actions/set-minimized");
ipc_repo->unregister_method("wm-actions/set-always-on-top");
ipc_repo->unregister_method("wm-actions/set-fullscreen");
ipc_repo->unregister_method("wm-actions/set-sticky");
ipc_repo->unregister_method("wm-actions/send-to-back");
}
wf::json_t execute_for_view(const wf::json_t& params,
std::function<void(wayfire_toplevel_view, bool)> view_op)
{
uint64_t view_id = wf::ipc::json_get_uint64(params, "view_id");
bool state = wf::ipc::json_get_bool(params, "state");
wayfire_toplevel_view view = toplevel_cast(wf::ipc::find_view_by_id(view_id));
if (!view)
{
return wf::ipc::json_error("toplevel view id not found!");
}
view_op(view, state);
return wf::ipc::json_ok();
}
wf::ipc::method_callback ipc_minimize = [=] (const wf::json_t& js)
{
return execute_for_view(js, [=] (wayfire_toplevel_view view, bool state)
{
wf::get_core().default_wm->minimize_request(view, state);
});
};
wf::ipc::method_callback ipc_maximize = [=] (const wf::json_t& js)
{
return execute_for_view(js, [=] (wayfire_toplevel_view view, bool state)
{
wf::get_core().default_wm->tile_request(view, state ? wf::TILED_EDGES_ALL : 0);
});
};
wf::ipc::method_callback ipc_set_always_on_top = [=] (const wf::json_t& js)
{
return execute_for_view(js, [=] (wayfire_toplevel_view view, bool state)
{
if (!view->get_output())
{
view->store_data(std::make_unique<wf::custom_data_t>(), "wm-actions-above");
return;
}
output_instance[view->get_output()]->set_keep_above_state(view, state);
});
};
wf::ipc::method_callback ipc_set_fullscreen = [=] (const wf::json_t& js)
{
return execute_for_view(js, [=] (wayfire_toplevel_view view, bool state)
{
wf::get_core().default_wm->fullscreen_request(view, nullptr, state);
});
};
wf::ipc::method_callback ipc_set_sticky = [=] (const wf::json_t& js)
{
return execute_for_view(js, [=] (wayfire_toplevel_view view, bool state)
{
view->set_sticky(state);
});
};
wf::ipc::method_callback ipc_send_to_back = [=] (const wf::json_t& js)
{
return execute_for_view(js, [=] (wayfire_toplevel_view view, bool state)
{
if (!view->get_output())
{
return;
}
output_instance[view->get_output()]->do_send_to_back(view);
});
};
wf::ipc_activator_t::handler_t on_toggle_showdesktop = [=] (wf::output_t *output, wayfire_view)
{
return this->output_instance[output]->on_toggle_showdesktop();
};
};
DECLARE_WAYFIRE_PLUGIN(wayfire_wm_actions_t);