forked from trip-zip/somewm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.c
More file actions
858 lines (745 loc) · 28 KB
/
Copy pathmonitor.c
File metadata and controls
858 lines (745 loc) · 28 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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/* monitor.c - Monitor/output management for somewm compositor */
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <wayland-server-core.h>
#include <wlr/backend.h>
#include <wlr/render/allocator.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_fractional_scale_v1.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_output_management_v1.h>
#include <wlr/types/wlr_output_power_management_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_xcursor_manager.h>
#include <wlr/util/log.h>
#include "somewm.h"
#include "somewm_api.h"
#include "monitor.h"
#include "nested_inhibitor.h"
#include "protocols.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/output.h"
#include "objects/layer_surface.h"
#include "objects/signal.h"
#include "banning.h"
#include "animation.h"
/* macros */
#include "focus.h"
#include "window.h"
#include "somewm_internal.h"
#include "event_queue.h"
#include "bench.h"
/* Module-private state */
static int in_updatemons;
static int updatemons_pending;
/* forward declarations */
void cleanupmon(struct wl_listener *listener, void *data);
void closemon(Monitor *m);
void createmon(struct wl_listener *listener, void *data);
void gpureset(struct wl_listener *listener, void *data);
void outputmgrapply(struct wl_listener *listener, void *data);
void outputmgrapplyortest(struct wlr_output_configuration_v1 *config, int test);
void outputmgrtest(struct wl_listener *listener, void *data);
void powermgrsetmode(struct wl_listener *listener, void *data);
void rendermon(struct wl_listener *listener, void *data);
void requestmonstate(struct wl_listener *listener, void *data);
void updatemons(struct wl_listener *listener, void *data);
/* listener structs */
struct wl_listener gpu_reset = {.notify = gpureset};
struct wl_listener layout_change = {.notify = updatemons};
struct wl_listener new_output = {.notify = createmon};
struct wl_listener output_mgr_apply = {.notify = outputmgrapply};
struct wl_listener output_mgr_test = {.notify = outputmgrtest};
struct wl_listener output_power_mgr_set_mode = {.notify = powermgrsetmode};
void
cleanupmon(struct wl_listener *listener, void *data)
{
Monitor *m = wl_container_of(listener, m, destroy);
LayerSurface *l, *tmp;
size_t i;
wlr_log(WLR_ERROR, "[HOTPLUG] cleanupmon: %s remaining_mons=%d",
m->wlr_output->name, wl_list_length(&mons) - 1);
/* Find and remove screen BEFORE destroying monitor data (AwesomeWM pattern)
* This emits instance-level "removed" signal and relocates clients.
* Also emit viewports and primary_changed signals as needed. */
if (globalconf_L) {
screen_t *screen = luaA_screen_get_by_monitor(globalconf_L, m);
if (screen) {
/* Check if this screen was the primary before removing it */
screen_t *old_primary = luaA_screen_get_primary_screen(globalconf_L);
bool was_primary = (old_primary == screen);
screen_removed(globalconf_L, screen);
luaA_screen_emit_viewports(globalconf_L);
/* If removed screen was primary, emit primary_changed on new primary */
if (was_primary) {
screen_t *new_primary = luaA_screen_get_primary_screen(globalconf_L);
if (new_primary && new_primary != screen) {
luaA_screen_emit_primary_changed(globalconf_L, new_primary);
}
}
/* Emit property::screen on output (screen→nil) */
if (m->output) {
luaA_object_push(globalconf_L, m->output);
luaA_object_emit_signal(globalconf_L, -1, "property::screen", 0);
lua_pop(globalconf_L, 1);
}
}
}
/* Invalidate output Lua object before destroying monitor data */
if (globalconf_L && m->output) {
luaA_object_push(globalconf_L, m->output);
luaA_class_emit_signal(globalconf_L, &output_class, "removed", 1);
luaA_output_invalidate(globalconf_L, m->output);
m->output = NULL;
}
/* m->layers[i] are intentionally not unlinked */
for (i = 0; i < LENGTH(m->layers); i++) {
wl_list_for_each_safe(l, tmp, &m->layers[i], link)
wlr_layer_surface_v1_destroy(l->layer_surface);
}
wl_list_remove(&m->destroy.link);
wl_list_remove(&m->frame.link);
wl_list_remove(&m->link);
wl_list_remove(&m->request_state.link);
if (m->lock_surface)
destroylocksurface(&m->destroy_lock_surface, NULL);
m->wlr_output->data = NULL;
/* Block updatemons() during output cleanup. wlr_output_layout_remove
* emits layout::change which triggers updatemons(). If updatemons runs,
* it does arrange/focus work that can indirectly add commit listeners
* (e.g. presentation_time, gamma) to the output being destroyed, causing
* wlr_output_finish() assertion failure. */
in_updatemons = 1;
wlr_scene_output_destroy(m->scene_output);
wlr_output_layout_remove(output_layout, m->wlr_output);
in_updatemons = 0;
closemon(m);
wlr_scene_node_destroy(&m->fullscreen_bg->node);
free(m);
if (updatemons_pending) {
updatemons_pending = 0;
updatemons(NULL, NULL);
}
}
void
closemon(Monitor *m)
{
/* update selmon if needed and
* move closed monitor's clients to the focused one */
Client *c;
int nclients = 0;
if (wl_list_empty(&mons)) {
selmon = NULL;
} else if (m == selmon) {
/* Find next enabled monitor (properly iterate the list) */
Monitor *iter;
selmon = NULL;
wl_list_for_each(iter, &mons, link) {
if (iter != m && iter->wlr_output->enabled) {
selmon = iter;
break;
}
}
}
foreach(client, globalconf.clients) {
c = *client;
if (some_client_get_floating(c) && c->geometry.x > m->m.width)
resize(c, (struct wlr_box){.x = c->geometry.x - m->w.width, .y = c->geometry.y,
.width = c->geometry.width, .height = c->geometry.height}, 0);
if (c->mon == m) {
nclients++;
setmon(c, selmon, 0);
}
}
wlr_log(WLR_ERROR, "[HOTPLUG] closemon: %s selmon=%s nclients=%d",
m->wlr_output->name,
selmon ? selmon->wlr_output->name : "NULL",
nclients);
focus_restore(selmon);
printstatus();
}
void
createmon(struct wl_listener *listener, void *data)
{
/* This event is raised by the backend when a new output (aka a display or
* monitor) becomes available. */
struct wlr_output *wlr_output = data;
size_t i;
struct wlr_output_state state;
Monitor *m;
if (wlr_output->data) {
wlr_log(WLR_ERROR, "[HOTPLUG] createmon duplicate ignored: %s already has monitor data",
wlr_output->name);
return;
}
if (wlr_output_layout_get(output_layout, wlr_output)) {
wlr_log(WLR_ERROR, "[HOTPLUG] createmon duplicate ignored: %s already in layout",
wlr_output->name);
return;
}
if (!wlr_output_init_render(wlr_output, alloc, drw))
return;
/* Inhibit host shortcuts for this output when nested. */
nested_inhibitor_attach_output(wlr_output);
wlr_log(WLR_ERROR, "[HOTPLUG] createmon: %s enabled=%d mons=%d",
wlr_output->name, wlr_output->enabled, wl_list_length(&mons));
m = wlr_output->data = ecalloc(1, sizeof(*m));
m->wlr_output = wlr_output;
for (i = 0; i < LENGTH(m->layers); i++)
wl_list_init(&m->layers[i]);
wlr_output_state_init(&state);
/* Apply safe defaults for monitor configuration (AwesomeWM pattern).
* Scale, transform, and position can be overridden from Lua via screen properties.
* Position is auto-configured by wlr_output_layout_add_auto() below. */
m->m.x = -1; /* Auto-position */
m->m.y = -1; /* Auto-position */
/* mfact/nmaster are per-tag properties, set in Lua */
/* Layouts are set from Lua, not C */
wlr_output_state_set_scale(&state, 1.0); /* Default 1:1 scale */
wlr_output_state_set_transform(&state, WL_OUTPUT_TRANSFORM_NORMAL); /* No rotation */
/* The mode is a tuple of (width, height, refresh rate), and each
* monitor supports only a specific set of modes. We just pick the
* monitor's preferred mode; a more sophisticated compositor would let
* the user configure it. */
wlr_output_state_set_mode(&state, wlr_output_preferred_mode(wlr_output));
/* Set up event listeners */
LISTEN(&wlr_output->events.frame, &m->frame, rendermon);
LISTEN(&wlr_output->events.destroy, &m->destroy, cleanupmon);
LISTEN(&wlr_output->events.request_state, &m->request_state, requestmonstate);
wlr_output_state_set_enabled(&state, 1);
if (!wlr_output_commit_state(wlr_output, &state)) {
wlr_log(WLR_ERROR, "[HOTPLUG] createmon: %s commit FAILED, aborting",
wlr_output->name);
wlr_output_state_finish(&state);
wl_list_remove(&m->frame.link);
wl_list_remove(&m->destroy.link);
wl_list_remove(&m->request_state.link);
wlr_output->data = NULL;
free(m);
return;
}
wlr_output_state_finish(&state);
wl_list_insert(&mons, &m->link);
printstatus();
/* Create output Lua object (persists from connect to disconnect).
* Signal emission is deferred to updatemons() so that o.screen is
* available in "added" handlers — eliminates a timing footgun. */
if (globalconf_L) {
m->output = luaA_output_new(globalconf_L, m);
if (m->output) {
lua_pop(globalconf_L, 1); /* Pop userdata (tracked in output.c) */
m->needs_output_added = 1;
}
}
/* The xdg-protocol specifies:
*
* If the fullscreened surface is not opaque, the compositor must make
* sure that other screen content not part of the same surface tree (made
* up of subsurfaces, popups or similarly coupled surfaces) are not
* visible below the fullscreened surface.
*
*/
/* updatemons() will resize and set correct position */
m->fullscreen_bg = wlr_scene_rect_create(layers[LyrFS], 0, 0, globalconf.appearance.fullscreen_bg);
wlr_scene_node_set_enabled(&m->fullscreen_bg->node, 0);
/* Adds this to the output layout in the order it was configured.
*
* The output layout utility automatically adds a wl_output global to the
* display, which Wayland clients can see to find out information about the
* output (such as DPI, scale factor, manufacturer, etc).
*/
m->scene_output = wlr_scene_output_create(scene, wlr_output);
/* Create screen object BEFORE adding to layout.
* wlr_output_layout_add_auto() triggers updatemons() SYNCHRONOUSLY
* via layout::change signal. needs_screen_added must be set before
* that, so updatemons() can emit screen_added with correct geometry. */
if (globalconf_L) {
Monitor *tmp;
int screen_index;
screen_t *screen;
screen_index = 1;
/* Calculate screen index by counting existing monitors */
wl_list_for_each(tmp, &mons, link) {
if (tmp != m)
screen_index++;
}
/* Create screen object (leaves it on Lua stack) */
screen = luaA_screen_new(globalconf_L, m, screen_index);
if (screen) {
/* Pop the screen userdata from stack (it's tracked in screen.c globals) */
lua_pop(globalconf_L, 1);
/* If startup is complete, this is a hotplugged monitor.
* Set flag so updatemons() emits screen_added AFTER geometry
* is set but BEFORE orphaned clients are assigned.
* This ensures Lua tags/wibar exist when clients arrive. */
if (luaA_screen_scanned_done()) {
m->needs_screen_added = 1;
}
}
}
/* Add to output layout — triggers updatemons() synchronously.
* updatemons() will: set geometry → emit screen_added → assign orphans */
if (m->m.x == -1 && m->m.y == -1)
wlr_output_layout_add_auto(output_layout, wlr_output);
else
wlr_output_layout_add(output_layout, wlr_output, m->m.x, m->m.y);
}
Monitor *
dirtomon(enum wlr_direction dir)
{
struct wlr_output *next;
if (!wlr_output_layout_get(output_layout, selmon->wlr_output))
return selmon;
if ((next = wlr_output_layout_adjacent_output(output_layout,
dir, selmon->wlr_output, selmon->m.x, selmon->m.y)))
return next->data;
if ((next = wlr_output_layout_farthest_output(output_layout,
dir ^ (WLR_DIRECTION_LEFT|WLR_DIRECTION_RIGHT),
selmon->wlr_output, selmon->m.x, selmon->m.y)))
return next->data;
return selmon;
}
void
focusmon(const Arg *arg)
{
int i = 0, nmons = wl_list_length(&mons);
if (nmons) {
do /* don't switch to disabled mons */
selmon = dirtomon(arg->i);
while (!selmon->wlr_output->enabled && i++ < nmons);
}
focus_restore(selmon);
}
void
gpureset(struct wl_listener *listener, void *data)
{
struct wlr_renderer *old_drw = drw;
struct wlr_allocator *old_alloc = alloc;
struct Monitor *m;
if (!(drw = wlr_renderer_autocreate(backend)))
die("couldn't recreate renderer");
if (!(alloc = wlr_allocator_autocreate(backend, drw)))
die("couldn't recreate allocator");
wl_list_remove(&gpu_reset.link);
wl_signal_add(&drw->events.lost, &gpu_reset);
wlr_compositor_set_renderer(compositor, drw);
wl_list_for_each(m, &mons, link) {
wlr_output_init_render(m->wlr_output, alloc, drw);
}
wlr_allocator_destroy(old_alloc);
wlr_renderer_destroy(old_drw);
}
void
outputmgrapply(struct wl_listener *listener, void *data)
{
struct wlr_output_configuration_v1 *config = data;
outputmgrapplyortest(config, 0);
}
void
outputmgrapplyortest(struct wlr_output_configuration_v1 *config, int test)
{
/*
* Called when a client such as wlr-randr requests a change in output
* configuration. This is only one way that the layout can be changed,
* so any Monitor information should be updated by updatemons() after an
* output_layout.change event, not here.
*/
struct wlr_output_configuration_head_v1 *config_head;
int ok = 1;
wl_list_for_each(config_head, &config->heads, link) {
struct wlr_output *wlr_output = config_head->state.output;
Monitor *m = wlr_output->data;
struct wlr_output_state state;
/* Orphan output: createmon bailed (commit or render init failed).
* Skip; there is no Monitor to reconfigure. */
if (!m)
continue;
/* Ensure displays previously disabled by wlr-output-power-management-v1
* are properly handled*/
m->asleep = 0;
wlr_output_state_init(&state);
wlr_output_state_set_enabled(&state, config_head->state.enabled);
if (!config_head->state.enabled)
goto apply_or_test;
if (config_head->state.mode)
wlr_output_state_set_mode(&state, config_head->state.mode);
else
wlr_output_state_set_custom_mode(&state,
config_head->state.custom_mode.width,
config_head->state.custom_mode.height,
config_head->state.custom_mode.refresh);
wlr_output_state_set_transform(&state, config_head->state.transform);
wlr_output_state_set_scale(&state, config_head->state.scale);
wlr_output_state_set_adaptive_sync_enabled(&state,
config_head->state.adaptive_sync_enabled);
apply_or_test:
ok &= test ? wlr_output_test_state(wlr_output, &state)
: wlr_output_commit_state(wlr_output, &state);
/* Don't move monitors if position wouldn't change. This avoids
* wlroots marking the output as manually configured.
* wlr_output_layout_add does not like disabled outputs */
if (!test && wlr_output->enabled && (m->m.x != config_head->state.x || m->m.y != config_head->state.y))
wlr_output_layout_add(output_layout, wlr_output,
config_head->state.x, config_head->state.y);
wlr_output_state_finish(&state);
}
if (ok)
wlr_output_configuration_v1_send_succeeded(config);
else
wlr_output_configuration_v1_send_failed(config);
wlr_output_configuration_v1_destroy(config);
/* Force monitor refresh after output config change */
updatemons(NULL, NULL);
}
void
outputmgrtest(struct wl_listener *listener, void *data)
{
struct wlr_output_configuration_v1 *config = data;
outputmgrapplyortest(config, 1);
}
void
powermgrsetmode(struct wl_listener *listener, void *data)
{
struct wlr_output_power_v1_set_mode_event *event = data;
struct wlr_output_state state;
Monitor *m = event->output->data;
if (!m)
return;
wlr_output_state_init(&state);
m->gamma_lut_changed = 1; /* Reapply gamma LUT when re-enabling the ouput */
wlr_output_state_set_enabled(&state, event->mode);
wlr_output_commit_state(m->wlr_output, &state);
wlr_output_state_finish(&state);
m->asleep = !event->mode;
wlr_log(WLR_ERROR, "[HOTPLUG] powermgrsetmode: %s mode=%d asleep=%d",
m->wlr_output->name, event->mode, m->asleep);
updatemons(NULL, NULL);
}
void
rendermon(struct wl_listener *listener, void *data)
{
/* This function is called every time an output is ready to display a frame,
* generally at the output's refresh rate (e.g. 60Hz). */
Monitor *m = wl_container_of(listener, m, frame);
Client *c;
struct timespec now;
bool presented = false;
/* Safety: scene_output may not exist yet if frame fires before createmon
* finishes (possible on NVIDIA), or output may be disabled */
if (!m->scene_output)
return;
if (!m->wlr_output->enabled)
goto skip;
/* Render if no XDG clients have an outstanding resize and are visible on
* this monitor. */
foreach(client, globalconf.clients) {
c = *client;
if (c->resize && !some_client_get_floating(c) && c->mon == m && !client_is_stopped(c))
goto skip;
}
#ifdef SOMEWM_BENCH
struct timespec bench_render_start, bench_render_end;
clock_gettime(CLOCK_MONOTONIC, &bench_render_start);
#endif
/* needs_frame is true only when there is something to present;
* wlr_scene_output_commit() returns true without presenting otherwise, so
* sample it first to count only real presents. */
presented = wlr_scene_output_needs_frame(m->scene_output);
if (!wlr_scene_output_commit(m->scene_output, NULL)) {
wlr_log(WLR_DEBUG, "[HOTPLUG] rendermon commit failed: %s",
m->wlr_output->name);
} else {
if (presented)
globalconf.frame_commit_count++;
#ifdef SOMEWM_BENCH
clock_gettime(CLOCK_MONOTONIC, &bench_render_end);
bench_render_record(timespec_diff_ns(&bench_render_start, &bench_render_end));
bench_input_commit_flush();
#endif
}
skip:
clock_gettime(CLOCK_MONOTONIC, &now);
wlr_scene_output_send_frame_done(m->scene_output, &now);
}
void
requestmonstate(struct wl_listener *listener, void *data)
{
struct wlr_output_event_request_state *event = data;
uint32_t committed = event->state->committed;
wlr_log(WLR_ERROR, "[HOTPLUG] requestmonstate: %s", event->output->name);
if (!wlr_output_commit_state(event->output, event->state)) {
wlr_log(WLR_ERROR, "[HOTPLUG] requestmonstate commit FAILED: %s",
event->output->name);
updatemons(NULL, NULL);
return;
}
/* Queue property::* signals on the output object so Lua stays in sync
* when external tools (wlr-randr, kanshi) change output state. */
if (globalconf_L) {
Monitor *m = event->output->data;
if (m && m->output) {
luaA_object_push(globalconf_L, m->output);
if (committed & WLR_OUTPUT_STATE_ENABLED)
some_event_queue_signal0(globalconf_L, -1, SIG_PROPERTY_ENABLED);
if (committed & WLR_OUTPUT_STATE_SCALE) {
some_event_queue_signal0(globalconf_L, -1, SIG_PROPERTY_SCALE);
/* Also emit on screen for backward compat */
screen_t *screen = luaA_screen_get_by_monitor(globalconf_L, m);
if (screen) {
luaA_object_push(globalconf_L, screen);
some_event_queue_signal0(globalconf_L, -1, SIG_PROPERTY_SCALE);
lua_pop(globalconf_L, 1);
}
}
if (committed & WLR_OUTPUT_STATE_TRANSFORM)
some_event_queue_signal0(globalconf_L, -1, SIG_PROPERTY_TRANSFORM);
if (committed & WLR_OUTPUT_STATE_MODE)
some_event_queue_signal0(globalconf_L, -1, SIG_PROPERTY_MODE);
if (committed & WLR_OUTPUT_STATE_ADAPTIVE_SYNC_ENABLED)
some_event_queue_signal0(globalconf_L, -1, SIG_PROPERTY_ADAPTIVE_SYNC);
lua_pop(globalconf_L, 1);
}
}
updatemons(NULL, NULL);
}
void
updatemons(struct wl_listener *listener, void *data)
{
/*
* Called whenever the output layout changes: adding or removing a
* monitor, changing an output's mode or position, etc. This is where
* the change officially happens and we update geometry, window
* positions, focus, and the stored configuration in wlroots'
* output-manager implementation.
*/
/* Guard against re-entrancy: wlr_output_layout_remove/add_auto below
* emit layout::change which would call us again, causing
* use-after-free in wlroots output_layout_add().
* Also used by cleanupmon() to prevent updatemons during output destruction. */
if (in_updatemons) {
updatemons_pending = 1;
return;
}
in_updatemons = 1;
struct wlr_output_configuration_v1 *config
= wlr_output_configuration_v1_create();
Client *c;
struct wlr_output_configuration_head_v1 *config_head;
Monitor *m;
/* Add disabled monitors to the config. For those still in the layout
* (transitioning to disabled), properly remove the screen and close.
* Already-disabled monitors just get a config_head so output manager
* clients (wlr-randr, wlopm, kanshi) can still see and re-enable
* them. Fixes #269. */
wl_list_for_each(m, &mons, link) {
if (m->wlr_output->enabled || m->asleep)
continue;
config_head = wlr_output_configuration_head_v1_create(config, m->wlr_output);
config_head->state.enabled = 0;
if (wlr_output_layout_get(output_layout, m->wlr_output)) {
wlr_log(WLR_ERROR, "[HOTPLUG] updatemons disable: %s",
m->wlr_output->name);
/* Properly remove the screen object so Lua tears down
* tags/wibars. On re-enable, a fresh screen is created
* and screen_added triggers request::desktop_decoration. */
if (globalconf_L) {
screen_t *screen = luaA_screen_get_by_monitor(globalconf_L, m);
if (screen) {
screen_t *old_primary = luaA_screen_get_primary_screen(globalconf_L);
bool was_primary = (old_primary == screen);
screen_removed(globalconf_L, screen);
luaA_screen_emit_viewports(globalconf_L);
if (was_primary) {
screen_t *new_primary = luaA_screen_get_primary_screen(globalconf_L);
if (new_primary && new_primary != screen)
luaA_screen_emit_primary_changed(globalconf_L, new_primary);
}
}
/* Emit property::screen on output (screen→nil) */
if (m->output) {
luaA_object_push(globalconf_L, m->output);
luaA_object_emit_signal(globalconf_L, -1, "property::screen", 0);
lua_pop(globalconf_L, 1);
}
}
/* Remove this output from the layout to avoid cursor enter inside it */
wlr_output_layout_remove(output_layout, m->wlr_output);
closemon(m);
m->m = m->w = (struct wlr_box){0};
}
}
/* Insert outputs that need to */
wl_list_for_each(m, &mons, link) {
if (m->wlr_output->enabled
&& !wlr_output_layout_get(output_layout, m->wlr_output))
wlr_output_layout_add_auto(output_layout, m->wlr_output);
}
/* Now that we update the output layout we can get its box */
wlr_output_layout_get_box(output_layout, NULL, &sgeom);
wlr_scene_node_set_position(&root_bg->node, sgeom.x, sgeom.y);
wlr_scene_rect_set_size(root_bg, sgeom.width, sgeom.height);
/* Make sure the clients are hidden when somewm is locked */
wlr_scene_node_set_position(&locked_bg->node, sgeom.x, sgeom.y);
wlr_scene_rect_set_size(locked_bg, sgeom.width, sgeom.height);
wl_list_for_each(m, &mons, link) {
if (!m->wlr_output->enabled)
continue;
config_head = wlr_output_configuration_head_v1_create(config, m->wlr_output);
/* Get the effective monitor geometry to use for surfaces */
wlr_output_layout_get_box(output_layout, m->wlr_output, &m->m);
m->w = m->m;
wlr_scene_output_set_position(m->scene_output, m->m.x, m->m.y);
wlr_scene_node_set_position(&m->fullscreen_bg->node, m->m.x, m->m.y);
wlr_scene_rect_set_size(m->fullscreen_bg, m->m.width, m->m.height);
if (m->lock_surface) {
struct wlr_scene_tree *scene_tree = client_surface_get_scene_tree(m->lock_surface->surface);
wlr_scene_node_set_position(&scene_tree->node, m->m.x, m->m.y);
wlr_session_lock_surface_v1_configure(m->lock_surface, m->m.width, m->m.height);
}
/* Calculate the effective monitor geometry to use for clients */
arrangelayers(m);
/* Update screen object geometry and emit property:: signals if changed */
{
screen_t *screen = luaA_screen_get_by_monitor(globalconf_L, m);
if (!screen && globalconf_L) {
/* Re-enabled monitor has no screen (removed during disable).
* Create a fresh one — treated like a hotplug. */
Monitor *tmp;
int screen_index = 1;
wl_list_for_each(tmp, &mons, link) {
if (tmp != m && luaA_screen_get_by_monitor(globalconf_L, tmp))
screen_index++;
}
screen = luaA_screen_new(globalconf_L, m, screen_index);
if (screen) {
lua_pop(globalconf_L, 1);
m->needs_screen_added = 1;
}
}
if (screen) {
if (m->needs_screen_added) {
/* New screen: cache geometry silently.
* Don't emit property::geometry before _added —
* Lua handlers (naughty) expect init_screen() to
* have run first. screen_added() fires in the
* add loop below and sets workarea = geometry. */
some_monitor_get_geometry(m, &screen->geometry);
screen->workarea = screen->geometry;
} else {
luaA_screen_update_geometry(globalconf_L, screen);
}
}
}
/* Don't move clients to the left output when plugging monitors */
arrange(m);
/* make sure fullscreen clients have the right size */
if ((c = focustop(m)) && c->fullscreen)
resize(c, m->m, 0);
/* Try to re-set the gamma LUT when updating monitors,
* it's only really needed when enabling a disabled output, but meh. */
m->gamma_lut_changed = 1;
config_head->state.x = m->m.x;
config_head->state.y = m->m.y;
wlr_log(WLR_ERROR, "[HOTPLUG] updatemons geom: %s %d,%d %dx%d",
m->wlr_output->name, m->m.x, m->m.y, m->m.width, m->m.height);
if (!selmon) {
selmon = m;
}
}
/* Emit screen_added for newly hotplugged monitors.
* Done AFTER geometry loop (m->m is set), BEFORE orphaned clients
* are assigned, so Lua tags and wibar exist when clients arrive.
* Separate loop from geometry to avoid reentrancy issues —
* screen_added triggers Lua callbacks that may cause layout changes.
* in_updatemons=1 prevents recursive updatemons calls. */
wl_list_for_each(m, &mons, link) {
if (!m->needs_screen_added || !m->wlr_output->enabled)
continue;
m->needs_screen_added = 0;
screen_t *screen = luaA_screen_get_by_monitor(globalconf_L, m);
if (screen && screen->valid) {
wlr_log(WLR_ERROR, "[HOTPLUG] updatemons screen_added: %s",
m->wlr_output->name);
screen_t *old_primary = luaA_screen_get_primary_screen(globalconf_L);
screen_added(globalconf_L, screen);
luaA_screen_emit_list(globalconf_L);
luaA_screen_emit_viewports(globalconf_L);
screen_t *new_primary = luaA_screen_get_primary_screen(globalconf_L);
if (new_primary == screen && old_primary != screen)
luaA_screen_emit_primary_changed(globalconf_L, screen);
/* Queue property::screen on the output (nil→screen) */
if (m->output) {
luaA_object_push(globalconf_L, m->output);
some_event_queue_signal0(globalconf_L, -1, SIG_PROPERTY_SCREEN);
lua_pop(globalconf_L, 1);
}
banning_refresh();
some_refresh();
}
}
/* Queue deferred output "added" signals. Done AFTER screen_added so that
* o.screen is available in "added" handlers. */
wl_list_for_each(m, &mons, link) {
if (!m->needs_output_added)
continue;
m->needs_output_added = 0;
if (m->output && globalconf_L) {
luaA_object_push(globalconf_L, m->output);
some_event_queue_class_args(globalconf_L, &output_class,
SIG_OUTPUT_ADDED, 1);
}
}
if (selmon && selmon->wlr_output->enabled) {
struct wlr_surface *surf;
foreach(client, globalconf.clients) {
c = *client;
if (!c->mon && (surf = client_surface(c)) && surf->mapped) {
wlr_log(WLR_ERROR, "[HOTPLUG] updatemons orphan: %s → %s",
client_get_title(c) ? client_get_title(c) : "?",
selmon->wlr_output->name);
setmon(c, selmon, 0);
}
}
focus_restore(selmon);
if (selmon->lock_surface) {
client_notify_enter(selmon->lock_surface->surface,
wlr_seat_get_keyboard(seat));
client_activate_surface(selmon->lock_surface->surface, 1);
}
}
wlr_log(WLR_ERROR, "[HOTPLUG] updatemons exit selmon=%s",
selmon ? selmon->wlr_output->name : "NULL");
/* FIXME: figure out why the cursor image is at 0,0 after turning all
* the monitors on.
* Move the cursor image where it used to be. It does not generate a
* wl_pointer.motion event for the clients, it's only the image what it's
* at the wrong position after all. */
wlr_cursor_move(cursor, NULL, 0, 0);
in_updatemons = 0;
wlr_output_manager_v1_set_configuration(output_mgr, config);
in_updatemons = 0;
if (updatemons_pending) {
updatemons_pending = 0;
updatemons(NULL, NULL);
}
}
Monitor *
xytomon(double x, double y)
{
struct wlr_output *o = wlr_output_layout_output_at(output_layout, x, y);
return o ? o->data : NULL;
}