forked from trip-zip/somewm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocols.c
More file actions
800 lines (667 loc) · 24.6 KB
/
Copy pathprotocols.c
File metadata and controls
800 lines (667 loc) · 24.6 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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* protocols.c - Protocol handlers for somewm compositor
*
* Layer shell, idle inhibit, session lock, foreign toplevel management,
* and XDG activation token handling.
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <glib.h>
#include <wayland-server-core.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_foreign_toplevel_management_v1.h>
#include <wlr/types/wlr_idle_inhibit_v1.h>
#include <wlr/types/wlr_idle_notify_v1.h>
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_fractional_scale_v1.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_session_lock_v1.h>
#include <wlr/types/wlr_xdg_activation_v1.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/log.h>
#include "somewm.h"
#include "somewm_api.h"
#include "event_queue.h"
#include "globalconf.h"
#include "client.h"
#include "common/luaobject.h"
#include "common/util.h"
#include "objects/client.h"
#include "objects/screen.h"
#include "objects/drawin.h"
#include "objects/layer_surface.h"
#include "objects/signal.h"
#include "objects/mousegrabber.h"
#include "stack.h"
#include "banning.h"
#include "animation.h"
#include "protocols.h"
/* Forward declaration for wlr_input_device (used by motionnotify extern) */
struct wlr_input_device;
#include "window.h"
#include "focus.h"
#include "input.h"
#include "somewm_internal.h"
/* Forward declarations */
static void destroyidleinhibitor(struct wl_listener *listener, void *data);
void destroylayersurfacenotify(struct wl_listener *listener, void *data);
void unmaplayersurfacenotify(struct wl_listener *listener, void *data);
static void destroylock(SessionLock *lock, int unlock);
void destroylocksurface(struct wl_listener *listener, void *data);
static void destroysessionlock(struct wl_listener *listener, void *data);
void unlocksession(struct wl_listener *listener, void *data);
void foreign_toplevel_request_activate(struct wl_listener *listener, void *data);
void foreign_toplevel_request_close(struct wl_listener *listener, void *data);
void foreign_toplevel_request_fullscreen(struct wl_listener *listener, void *data);
void foreign_toplevel_request_maximize(struct wl_listener *listener, void *data);
void foreign_toplevel_request_minimize(struct wl_listener *listener, void *data);
static gboolean activation_token_timeout(gpointer user_data);
/* XDG Activation token tracking (Wayland startup notification) */
typedef struct {
char *token; /* XDG activation token string */
char *app_id; /* Application ID from spawn */
uint32_t timeout_id; /* GLib timeout source ID for cleanup */
} activation_token_t;
/* Pending activation tokens (matches AwesomeWM's sn_waits pattern) */
static activation_token_t *pending_tokens = NULL;
static size_t pending_tokens_len = 0;
static size_t pending_tokens_cap = 0;
/* ==========================================================================
* Layer Shell
* ========================================================================== */
void
arrangelayer(Monitor *m, struct wl_list *list, struct wlr_box *usable_area, int exclusive)
{
LayerSurface *l;
struct wlr_box full_area = m->m;
wl_list_for_each(l, list, link) {
struct wlr_layer_surface_v1 *layer_surface = l->layer_surface;
if (!layer_surface->initialized)
continue;
if (exclusive != (layer_surface->current.exclusive_zone > 0))
continue;
wlr_scene_layer_surface_v1_configure(l->scene_layer, &full_area, usable_area);
wlr_scene_node_set_position(&l->popups->node, l->scene->node.x, l->scene->node.y);
}
}
void
arrangelayers(Monitor *m)
{
int i;
struct wlr_box usable_area = m->m;
LayerSurface *l;
uint32_t layers_above_shell[] = {
ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY,
ZWLR_LAYER_SHELL_V1_LAYER_TOP,
};
if (!m->wlr_output->enabled)
return;
/* Arrange exclusive surfaces from top->bottom */
for (i = 3; i >= 0; i--)
arrangelayer(m, &m->layers[i], &usable_area, 1);
/* Apply drawin struts (from Lua wibars) to the usable area
* This must happen AFTER layer shell exclusive zones but BEFORE setting m->w */
some_monitor_apply_drawin_struts(m, &usable_area);
if (!wlr_box_equal(&usable_area, &m->w)) {
lua_State *L;
screen_t *screen;
m->w = usable_area;
/* Update Lua screen.workarea property to match the new usable area
* This emits property::workarea signal so layouts get the correct workarea */
L = globalconf_get_lua_State();
if (L && globalconf.screens.tab) {
screen = luaA_screen_get_by_monitor(L, m);
if (screen) {
screen_set_workarea(L, screen, &usable_area);
}
}
arrange(m);
}
/* Arrange non-exlusive surfaces from top->bottom */
for (i = 3; i >= 0; i--)
arrangelayer(m, &m->layers[i], &usable_area, 0);
/* Find topmost keyboard interactive layer and emit request::keyboard signal
* If the layer surface has a Lua object, let Lua decide whether to grant focus.
* Otherwise, use legacy auto-grant behavior. */
for (i = 0; i < (int)LENGTH(layers_above_shell); i++) {
wl_list_for_each_reverse(l, &m->layers[layers_above_shell[i]], link) {
if (locked || !l->layer_surface->current.keyboard_interactive || !l->mapped)
continue;
if (l->lua_object && globalconf_L) {
/* Emit request::keyboard signal - Lua decides whether to grant focus */
const char *context = (l->layer_surface->current.keyboard_interactive ==
ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE)
? "exclusive" : "on_demand";
layer_surface_emit_request_keyboard(l->lua_object, context);
/* If Lua granted focus, we're done. Otherwise, continue searching. */
if (l->lua_object->has_keyboard_focus)
return;
} else {
/* Legacy behavior for surfaces without Lua objects */
focusclient(NULL, 0);
exclusive_focus = l;
client_notify_enter(l->layer_surface->surface, wlr_seat_get_keyboard(seat));
return;
}
}
}
}
void
commitlayersurfacenotify(struct wl_listener *listener, void *data)
{
LayerSurface *l = wl_container_of(listener, l, surface_commit);
struct wlr_layer_surface_v1 *layer_surface = l->layer_surface;
struct wlr_scene_tree *scene_layer = layers[layermap[layer_surface->current.layer]];
struct wlr_layer_surface_v1_state old_state;
int was_mapped;
if (l->layer_surface->initial_commit) {
client_set_scale(layer_surface->surface, l->mon->wlr_output->scale);
/* Temporarily set the layer's current state to pending
* so that we can easily arrange it */
old_state = l->layer_surface->current;
l->layer_surface->current = l->layer_surface->pending;
arrangelayers(l->mon);
l->layer_surface->current = old_state;
return;
}
if (layer_surface->current.committed == 0 && l->mapped == layer_surface->surface->mapped)
return;
was_mapped = l->mapped;
l->mapped = layer_surface->surface->mapped;
/* If surface just mapped, create Lua object and emit request::manage */
if (!was_mapped && l->mapped && !l->lua_object && globalconf_L) {
lua_State *L = globalconf_get_lua_State();
layer_surface_t *ls = layer_surface_manage(L, l);
/* Note: luaA_object_ref inside layer_surface_manage already pops the object */
layer_surface_emit_manage(ls, "new");
}
if (scene_layer != l->scene->node.parent) {
wlr_scene_node_reparent(&l->scene->node, scene_layer);
wl_list_remove(&l->link);
wl_list_insert(&l->mon->layers[layer_surface->current.layer], &l->link);
wlr_scene_node_reparent(&l->popups->node, (layer_surface->current.layer
< ZWLR_LAYER_SHELL_V1_LAYER_TOP ? layers[LyrTop] : scene_layer));
}
arrangelayers(l->mon);
/* When a layer surface maps, re-evaluate pointer focus so that
* wl_pointer.enter is delivered if the cursor is already over it.
* Without this, hover doesn't work until the user moves the mouse.
* Similar to sway's cursor_rebase_all() in handle_map(), but uses
* motionnotify(0,...) which re-runs xytonode() + pointerfocus()
* without emitting Lua mouse signals (those are gated on time != 0).
* Skip when exclusive_focus is active to avoid disrupting grabs. */
if (!was_mapped && l->mapped && !exclusive_focus)
motionnotify(0, NULL, 0, 0, 0, 0);
/* Queue property change signals for Lua (matches AwesomeWM pattern) */
if (l->lua_object && globalconf_L && layer_surface->current.committed) {
lua_State *L = globalconf_get_lua_State();
uint32_t committed = layer_surface->current.committed;
luaA_object_push(L, l->lua_object);
if (committed & (1 << 5)) /* WLR_LAYER_SURFACE_V1_STATE_LAYER */
some_event_queue_signal0(L, -1, SIG_PROPERTY_LAYER);
if (committed & (1 << 1)) /* WLR_LAYER_SURFACE_V1_STATE_ANCHOR */
some_event_queue_signal0(L, -1, SIG_PROPERTY_ANCHOR);
if (committed & (1 << 2)) /* WLR_LAYER_SURFACE_V1_STATE_EXCLUSIVE_ZONE */
some_event_queue_signal0(L, -1, SIG_PROPERTY_EXCLUSIVE_ZONE);
if (committed & (1 << 4)) /* WLR_LAYER_SURFACE_V1_STATE_KEYBOARD_INTERACTIVITY */
some_event_queue_signal0(L, -1, SIG_PROPERTY_KEYBOARD_INTERACTIVE);
if (committed & (1 << 3)) /* WLR_LAYER_SURFACE_V1_STATE_MARGIN */
some_event_queue_signal0(L, -1, SIG_PROPERTY_MARGIN);
lua_pop(L, 1);
}
}
void
createlayersurface(struct wl_listener *listener, void *data)
{
struct wlr_layer_surface_v1 *layer_surface = data;
LayerSurface *l;
struct wlr_surface *surface = layer_surface->surface;
struct wlr_scene_tree *scene_layer = layers[layermap[layer_surface->pending.layer]];
if (!layer_surface->output
&& !(layer_surface->output = selmon ? selmon->wlr_output : NULL)) {
wlr_layer_surface_v1_destroy(layer_surface);
return;
}
l = layer_surface->data = ecalloc(1, sizeof(*l));
l->type = LayerShell;
LISTEN(&surface->events.unmap, &l->unmap, unmaplayersurfacenotify);
LISTEN(&layer_surface->events.destroy, &l->destroy, destroylayersurfacenotify);
l->layer_surface = layer_surface;
l->mon = layer_surface->output->data;
l->scene_layer = wlr_scene_layer_surface_v1_create(scene_layer, layer_surface);
/* Register commit listener AFTER wlr_scene_layer_surface_v1_create() so our
* listener fires AFTER wlroots' internal scene commit handler. This lets
* the scene graph reflect the new buffer before the commit handler calls
* motionnotify(0, ...) to re-evaluate pointer focus on map. */
LISTEN(&surface->events.commit, &l->surface_commit, commitlayersurfacenotify);
l->scene = l->scene_layer->tree;
l->popups = surface->data = wlr_scene_tree_create(layer_surface->current.layer
< ZWLR_LAYER_SHELL_V1_LAYER_TOP ? layers[LyrTop] : scene_layer);
l->scene->node.data = l->popups->node.data = l;
wl_list_insert(&l->mon->layers[layer_surface->pending.layer],&l->link);
wlr_surface_send_enter(surface, layer_surface->output);
}
void
destroylayersurfacenotify(struct wl_listener *listener, void *data)
{
LayerSurface *l = wl_container_of(listener, l, destroy);
/* Defensive: clear exclusive_focus if the destroyed surface held it
* (shouldn't happen per Wayland spec - unmap fires before destroy) */
if (l == exclusive_focus)
exclusive_focus = NULL;
wl_list_remove(&l->link);
wl_list_remove(&l->destroy.link);
wl_list_remove(&l->unmap.link);
wl_list_remove(&l->surface_commit.link);
wlr_scene_node_destroy(&l->scene->node);
wlr_scene_node_destroy(&l->popups->node);
free(l);
}
void
unmaplayersurfacenotify(struct wl_listener *listener, void *data)
{
LayerSurface *l = wl_container_of(listener, l, unmap);
l->mapped = 0;
wlr_scene_node_set_enabled(&l->scene->node, 0);
if (l == exclusive_focus)
exclusive_focus = NULL;
if (l->layer_surface && l->layer_surface->output) {
/* Pair send_enter() with send_leave(). */
if (l->layer_surface->surface)
wlr_surface_send_leave(l->layer_surface->surface, l->layer_surface->output);
l->mon = l->layer_surface->output->data;
if (l->mon)
arrangelayers(l->mon);
}
/* Emit request::unmanage signal if we have a Lua object */
if (l->lua_object && globalconf_L) {
layer_surface_emit_unmanage(l->lua_object);
}
/* Restore focus via Lua history on the layer surface's monitor */
focus_restore(l->mon ? l->mon : selmon);
motionnotify(0, NULL, 0, 0, 0, 0);
}
/* ==========================================================================
* Idle Inhibit
* ========================================================================== */
/** Check if idle is effectively inhibited (for Lua API and idle timers). */
bool
some_is_idle_inhibited(void)
{
int unused_lx, unused_ly;
struct wlr_idle_inhibitor_v1 *inhibitor;
if (!idle_inhibit_mgr)
return false;
wl_list_for_each(inhibitor, &idle_inhibit_mgr->inhibitors, link) {
/* An unmapped surface must not inhibit, and its scene tree may
* already be gone (the !tree case below counts as inhibited). */
if (!inhibitor->surface->mapped)
continue;
struct wlr_surface *surface = wlr_surface_get_root_surface(inhibitor->surface);
struct wlr_scene_tree *tree = surface->data;
if (globalconf.appearance.bypass_surface_visibility || (!tree
|| wlr_scene_node_coords(&tree->node, &unused_lx, &unused_ly))) {
return true;
}
}
return false;
}
int
some_idle_inhibitor_count(void)
{
int count = 0;
struct wlr_idle_inhibitor_v1 *inhibitor;
if (!idle_inhibit_mgr)
return 0;
wl_list_for_each(inhibitor, &idle_inhibit_mgr->inhibitors, link) {
count++;
}
return count;
}
int
some_push_idle_inhibitors(lua_State *L)
{
int idx = 1;
int lx, ly;
struct wlr_idle_inhibitor_v1 *inhibitor;
if (!idle_inhibit_mgr) {
lua_newtable(L);
return 1;
}
lua_newtable(L);
wl_list_for_each(inhibitor, &idle_inhibit_mgr->inhibitors, link) {
struct wlr_surface *surface = wlr_surface_get_root_surface(inhibitor->surface);
struct wlr_scene_tree *tree = surface->data;
bool visible = globalconf.appearance.bypass_surface_visibility
|| (!tree || wlr_scene_node_coords(&tree->node, &lx, &ly));
Client *c = some_client_from_surface(surface);
lua_createtable(L, 0, 2);
if (c)
luaA_object_push(L, c);
else
lua_pushnil(L);
lua_setfield(L, -2, "client");
lua_pushboolean(L, visible);
lua_setfield(L, -2, "visible");
lua_rawseti(L, -2, idx++);
}
return 1;
}
void
createidleinhibitor(struct wl_listener *listener, void *data)
{
struct wlr_idle_inhibitor_v1 *idle_inhibitor = data;
LISTEN_STATIC(&idle_inhibitor->events.destroy, destroyidleinhibitor);
some_recompute_idle_inhibit();
}
/* wlroots emits the inhibitor destroy signal before unlinking it from
* idle_inhibit_mgr->inhibitors, so a recompute here would still count the
* dying inhibitor and, if its surface is still mapped, latch idle timers
* off with nothing left to ever recompute. Defer to the end of the current
* dispatch (same pattern as pending_flush_source, see #530). */
static struct wl_event_source *pending_idle_recompute;
static void
recompute_idle_inhibit_idle(void *data)
{
pending_idle_recompute = NULL;
some_recompute_idle_inhibit();
}
static void
destroyidleinhibitor(struct wl_listener *listener, void *data)
{
if (!pending_idle_recompute)
pending_idle_recompute = wl_event_loop_add_idle(some_get_event_loop(),
recompute_idle_inhibit_idle, NULL);
wl_list_remove(&listener->link);
free(listener);
}
/* ==========================================================================
* Session Lock
* ========================================================================== */
static void
destroylock(SessionLock *lock, int unlock)
{
wlr_seat_keyboard_notify_clear_focus(seat);
if ((locked = !unlock))
goto destroy;
wlr_scene_node_set_enabled(&locked_bg->node, 0);
focus_restore(selmon);
motionnotify(0, NULL, 0, 0, 0, 0);
destroy:
wl_list_remove(&lock->new_surface.link);
wl_list_remove(&lock->unlock.link);
wl_list_remove(&lock->destroy.link);
wlr_scene_node_destroy(&lock->scene->node);
cur_lock = NULL;
free(lock);
}
void
destroylocksurface(struct wl_listener *listener, void *data)
{
Monitor *m = wl_container_of(listener, m, destroy_lock_surface);
struct wlr_session_lock_surface_v1 *surface, *lock_surface = m->lock_surface;
m->lock_surface = NULL;
wl_list_remove(&m->destroy_lock_surface.link);
if (lock_surface->surface != seat->keyboard_state.focused_surface)
return;
if (locked && cur_lock && !wl_list_empty(&cur_lock->surfaces)) {
surface = wl_container_of(cur_lock->surfaces.next, surface, link);
client_notify_enter(surface->surface, wlr_seat_get_keyboard(seat));
} else if (!locked) {
focus_restore(selmon);
} else {
wlr_seat_keyboard_clear_focus(seat);
}
}
static void
destroysessionlock(struct wl_listener *listener, void *data)
{
SessionLock *lock = wl_container_of(listener, lock, destroy);
destroylock(lock, 0);
}
void
createlocksurface(struct wl_listener *listener, void *data)
{
SessionLock *lock = wl_container_of(listener, lock, new_surface);
struct wlr_session_lock_surface_v1 *lock_surface = data;
Monitor *m = lock_surface->output->data;
struct wlr_scene_tree *scene_tree = lock_surface->surface->data
= wlr_scene_subsurface_tree_create(lock->scene, lock_surface->surface);
m->lock_surface = lock_surface;
wlr_scene_node_set_position(&scene_tree->node, m->m.x, m->m.y);
wlr_session_lock_surface_v1_configure(lock_surface, m->m.width, m->m.height);
LISTEN(&lock_surface->events.destroy, &m->destroy_lock_surface, destroylocksurface);
if (m == selmon)
client_notify_enter(lock_surface->surface, wlr_seat_get_keyboard(seat));
}
void
locksession(struct wl_listener *listener, void *data)
{
struct wlr_session_lock_v1 *session_lock = data;
SessionLock *lock;
if (cur_lock) {
wlr_session_lock_v1_destroy(session_lock);
return;
}
/* EDGE-3: Reject ext-session-lock when Lua lock is active */
if (some_is_lua_locked()) {
fprintf(stderr, "somewm: ext-session-lock rejected while Lua lock is active\n");
wlr_session_lock_v1_destroy(session_lock);
return;
}
wlr_scene_node_set_enabled(&locked_bg->node, 1);
lock = session_lock->data = ecalloc(1, sizeof(*lock));
focusclient(NULL, 0);
lock->scene = wlr_scene_tree_create(layers[LyrBlock]);
cur_lock = lock->lock = session_lock;
locked = 1;
LISTEN(&session_lock->events.new_surface, &lock->new_surface, createlocksurface);
LISTEN(&session_lock->events.destroy, &lock->destroy, destroysessionlock);
LISTEN(&session_lock->events.unlock, &lock->unlock, unlocksession);
wlr_session_lock_v1_send_locked(session_lock);
}
void
unlocksession(struct wl_listener *listener, void *data)
{
SessionLock *lock = wl_container_of(listener, lock, unlock);
destroylock(lock, 1);
}
/* ==========================================================================
* Foreign Toplevel Management
* ========================================================================== */
void
foreign_toplevel_request_activate(struct wl_listener *listener, void *data)
{
Client *c = wl_container_of(listener, c, foreign_request_activate);
/* Emit request::activate signal to Lua with switch_to_tag hint.
* This lets awful.permissions.activate handle tag switching when
* activating a window on a different tag (e.g., via rofi -show window). */
lua_State *L = globalconf_get_lua_State();
luaA_object_push(L, c);
lua_pushstring(L, "foreign_toplevel"); /* context */
lua_newtable(L); /* hints table */
lua_pushboolean(L, true);
lua_setfield(L, -2, "switch_to_tag");
lua_pushboolean(L, true);
lua_setfield(L, -2, "raise");
some_event_queue_signal(L, -3, SIG_REQUEST_ACTIVATE, 2);
lua_pop(L, 1);
}
void
foreign_toplevel_request_close(struct wl_listener *listener, void *data)
{
Client *c = wl_container_of(listener, c, foreign_request_close);
client_send_close(c);
}
void
foreign_toplevel_request_fullscreen(struct wl_listener *listener, void *data)
{
struct wlr_foreign_toplevel_handle_v1_fullscreen_event *event = data;
Client *c = wl_container_of(listener, c, foreign_request_fullscreen);
setfullscreen(c, event->fullscreen);
}
void
foreign_toplevel_request_maximize(struct wl_listener *listener, void *data)
{
struct wlr_foreign_toplevel_handle_v1_maximized_event *event = data;
Client *c = wl_container_of(listener, c, foreign_request_maximize);
lua_State *L = globalconf_get_lua_State();
luaA_object_push(L, c);
client_set_maximized(L, -1, event->maximized);
lua_pop(L, 1);
}
void
foreign_toplevel_request_minimize(struct wl_listener *listener, void *data)
{
struct wlr_foreign_toplevel_handle_v1_minimized_event *event = data;
Client *c = wl_container_of(listener, c, foreign_request_minimize);
lua_State *L = globalconf_get_lua_State();
luaA_object_push(L, c);
client_set_minimized(L, -1, event->minimized);
lua_pop(L, 1);
}
/* ==========================================================================
* XDG Activation Tokens
* ========================================================================== */
/* Cancel all pending activation token timeouts.
* Called before hot-reload GLib source sweep to prevent compositor-owned
* timeout sources from being destroyed by the ID-based scan. */
void
activation_tokens_cancel_all(void)
{
for (size_t i = 0; i < pending_tokens_len; i++) {
g_source_remove(pending_tokens[i].timeout_id);
free(pending_tokens[i].token);
free(pending_tokens[i].app_id);
}
pending_tokens_len = 0;
}
static gboolean
activation_token_timeout(gpointer user_data)
{
char *token;
size_t i;
token = (char *)user_data;
/* Find and remove the token */
for (i = 0; i < pending_tokens_len; i++) {
if (strcmp(pending_tokens[i].token, token) == 0) {
/* Queue spawn::timeout signal (matches AwesomeWM spawn.c:115) */
some_event_queue_global_with_table(SIG_SPAWN_TIMEOUT, 2,
"id", token);
free(pending_tokens[i].token);
free(pending_tokens[i].app_id);
/* Shift remaining tokens */
memmove(&pending_tokens[i], &pending_tokens[i + 1],
(pending_tokens_len - i - 1) * sizeof(activation_token_t));
pending_tokens_len--;
break;
}
}
free(token);
return G_SOURCE_REMOVE; /* One-shot timer */
}
/* Create activation token and store it */
char *activation_token_create(const char *app_id)
{
struct wlr_xdg_activation_token_v1 *token;
const char *token_name;
size_t new_cap;
activation_token_t *new_tokens;
activation_token_t *slot;
if (!activation)
return NULL;
token = wlr_xdg_activation_token_v1_create(activation);
if (!token)
return NULL;
/* Commit the token to get the token string */
token_name = wlr_xdg_activation_token_v1_get_name(token);
if (!token_name)
return NULL;
/* Grow array if needed */
if (pending_tokens_len >= pending_tokens_cap) {
new_cap = pending_tokens_cap == 0 ? 8 : pending_tokens_cap * 2;
new_tokens = realloc(pending_tokens, new_cap * sizeof(activation_token_t));
if (!new_tokens)
return NULL;
pending_tokens = new_tokens;
pending_tokens_cap = new_cap;
}
/* Store token with metadata */
slot = &pending_tokens[pending_tokens_len++];
slot->token = strdup(token_name);
slot->app_id = app_id ? strdup(app_id) : NULL;
/* Setup 20-second timeout (matches AwesomeWM) */
slot->timeout_id = g_timeout_add_seconds(20, activation_token_timeout,
strdup(token_name));
return slot->token;
}
/* Cleanup token (called from urgent handler on match) */
void activation_token_cleanup(const char *token)
{
size_t i;
if (!token)
return;
for (i = 0; i < pending_tokens_len; i++) {
if (strcmp(pending_tokens[i].token, token) == 0) {
/* Cancel timeout */
g_source_remove(pending_tokens[i].timeout_id);
free(pending_tokens[i].token);
free(pending_tokens[i].app_id);
/* Shift remaining tokens */
memmove(&pending_tokens[i], &pending_tokens[i + 1],
(pending_tokens_len - i - 1) * sizeof(activation_token_t));
pending_tokens_len--;
return;
}
}
}
void urgent(struct wl_listener *listener, void *data)
{
struct wlr_xdg_activation_v1_request_activate_event *event;
Client *c;
const char *token_name;
bool token_matched;
lua_State *L;
event = data;
c = NULL;
token_matched = false;
toplevel_from_wlr_surface(event->surface, &c, NULL);
if (!c)
return;
/* Extract token from activation event */
token_name = event->token ? wlr_xdg_activation_token_v1_get_name(event->token) : NULL;
/* Validate token against pending tokens */
if (token_name) {
size_t i;
for (i = 0; i < pending_tokens_len; i++) {
if (strcmp(pending_tokens[i].token, token_name) == 0) {
token_matched = true;
/* Set startup_id on client (matches AwesomeWM pattern) */
if (c->startup_id)
free(c->startup_id);
c->startup_id = strdup(token_name);
/* Clean up matched token */
activation_token_cleanup(token_name);
/* Queue spawn::completed signal (matches AwesomeWM spawn.c:167) */
some_event_queue_global_with_table(SIG_SPAWN_COMPLETED, 2,
"id", token_name);
break;
}
}
}
/* Emit request::activate signal to Lua (matches AwesomeWM) */
L = globalconf_get_lua_State();
luaA_object_push(L, c);
lua_pushstring(L, token_matched ? "startup" : "client"); /* context */
some_event_queue_signal(L, -2, SIG_REQUEST_ACTIVATE, 1);
lua_pop(L, 1);
/* Emit request::urgent and let Lua decide (matches AwesomeWM) */
luaA_object_push(L, c);
lua_pushboolean(L, true);
some_event_queue_signal(L, -2, SIG_REQUEST_URGENT, 1);
lua_pop(L, 1);
}