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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
// linux_wl_window -- Wayland windows (indexed records): globals, xdg toplevels, configure,
// scaling, window commands and the event loop [src/plat/linux/wl_window.s]
//
// win_create(title, w_dip, h_dip, flags, owner, x, y) -> index / -1
// owner -1: the main window (registry, globals; its first configure is awaited).
// Else a toplevel owned by `owner`: xdg_toplevel.set_parent(owner), client-side frame
// unless PWF_SYSFRAME gets a server-side one (xdg-decoration), xdg_dialog_v1.set_modal
// for PWF_MODAL when the compositor has xdg_wm_dialog_v1. Created unmapped; x, y are
// ignored (Wayland clients can neither place their windows nor learn where they are).
// Tool windows (PWF_TOOL without PWF_DIALOG) are child windows instead (WLWF_CHILD,
// WST_CHILD): a wl_subsurface of the owner's surface (desynchronized), so they can be
// placed -- at a position relative to the owner (px, set_position in logical units,
// applied with a commit of the owner) -- and move, minimize and stack with it. Toplevel
// tool windows were centred on the owner by every compositor (GNOME, KDE, WSLg). They
// stay inside the owner (clamped; re-clamped when it shrinks), the library moves and
// resizes them itself (title bar / edge grips), the keyboard stays with the owner's
// toplevel and goes to the child while it is activated (a click on a text box,
// win_activate), as on Windows (linux_wl_child.rs). jfgui_WL_TOOLWIN=toplevel keeps
// separate toplevels.
// win_destroy(w) (the windows it owns are never shown again, as on Windows)
// win_show(w, mode) (show = objects + the initial commit; the first configure is awaited
// asynchronously, presents wait for it; hide = the window's objects are destroyed:
// WSLg keeps the RAIL window of an unmapped toplevel, only a destroyed wl_surface
// removes it) win_activate: which window gets the keys inside a toplevel (child
// windows); a toplevel is raised (and restored) through xdg-activation
// (linux_wl_act.rs) win_enable: nothing
// win_set_rect(w, x, y, cw, ch) size only (the position can not be set); a child
// window's position relative to its owner and its size on the pixel grid
// win_get_rect -> None (unknown on Wayland), a child window's rectangle in its owner
// win_work_area -> None
// win_set_title win_state -> WST_* win_cmd win_begin_move win_begin_resize
// (L7) win_cmd WCMD_FULLSCREEN / WCMD_WINDOWED: xdg_toplevel set_fullscreen (the
// compositor picks the output) / unset_fullscreen; the configure's states tell
// WST_FULLSCREEN (a change of it comes with an E_RESIZE); child windows: nothing
// win_size -> (w, h) win_dpi win_set_min_size (also before the window exists)
// wait(deadline_us) -> queued events (0 = poll without blocking, u64::MAX = forever,
// otherwise an absolute time_us() value)
//
// Logical size = DIP (the compositor's surface coordinates). Physical size = logical *
// scale; the scale comes from wp_fractional_scale (with a viewport),
// wl_surface.preferred_buffer_scale or the largest wl_output.scale the surface is on.
// jfgui_WLDEBUG=1 traces every protocol message to stderr; jfgui_SCALE=N (1..4) pretends
// every output has integer scale N (HiDPI tests).
// WSLg (Weston RAIL, measured 1.0.79): a toplevel with set_parent gets its own RAIL window
// (own taskbar button, not owned in Windows terms), is centred on its parent when mapped, is
// not kept above it and stays visible while the parent is minimized.
use alloc::vec::Vec;
use super::proto::*;
use super::wire::{fatal, poll, Args, PollFd, A, EINTR, POLLERR, POLLHUP, POLLIN, POLLNVAL, POLLOUT};
use super::Wl;
use crate::{
E_CLOSE, E_DPI, E_RESIZE, PWF_DIALOG, PWF_MASK, PWF_MODAL, PWF_RESIZABLE, PWF_SYSFRAME, PWF_TOOL,
PWS_SHOWNA, PW_KEEP, PW_MAX, PW_NOOWNER, WCMD_CLOSE, WCMD_MAXIMIZE, WCMD_MINIMIZE, WCMD_RESTORE, WST_ACTIVE,
WST_CHILD, WST_CSD, WST_FULLSCREEN, WST_MAXIMIZED, WST_MINIMIZED, WST_TILED,
};
use crate::{WCMD_FULLSCREEN, WCMD_WINDOWED};
pub(crate) const OUT_MAX: usize = 8;
const POLL_MAX: usize = 20; // wayland fd + clipboard transfers (16 writers, the drop) + the wake eventfd
const STATE_BIG: u32 = WST_MAXIMIZED | WST_FULLSCREEN | WST_TILED;
const TITLE_MAX: usize = 256; // bytes per window incl. the NUL
pub(crate) const WLWF_USED: u32 = 0x100;
pub(crate) const WLWF_TOP: u32 = 0x200; // top-level window (no owner)
pub(crate) const WLWF_CHILD: u32 = 0x400; // a subsurface of its owner (tool window)
pub(crate) const WLWF_DEAD: u32 = 0x800; // its owner was destroyed: never shown again
/// one wl_output
#[derive(Clone, Copy, Default)]
pub(crate) struct Out {
pub id: u32, // wl_output object (0 = free slot)
pub name: u32, // registry name
pub scale: i32,
pub mw: i32, // current mode width / height
pub mh: i32,
pub ver: u32,
// (screen.rs monitors) geometry position and transform, the mode's refresh (mHz), the
// output's name, its zxdg_output_v1 and logical position / size (lw 0: none)
pub x: i32,
pub y: i32,
pub transform: i32,
pub refresh: i32,
pub label: [u8; 32],
pub label_len: u8,
pub xo: u32,
pub lx: i32,
pub ly: i32,
pub lw: i32,
pub lh: i32,
}
/// the bound globals and process-wide window state
#[derive(Default)]
pub(crate) struct Globals {
pub reg: u32,
pub comp: u32,
pub comp_ver: u32,
pub shm: u32,
pub seat: u32,
pub seat_ver: u32,
pub xdg_wm: u32,
pub ddm: u32,
pub ddm_ver: u32,
pub vpr: u32,
pub fsm: u32,
pub csm: u32,
pub decom: u32,
pub dialogm: u32,
pub tabm: u32,
pub subc: u32, // wl_subcompositor (child windows)
pub rpm: u32, // zwp_relative_pointer_manager_v1 (child windows' drags)
pub actm: u32, // xdg_activation_v1 (linux_wl_act.rs)
pub toolwin_top: bool, // jfgui_WL_TOOLWIN=toplevel: tool windows are toplevels
pub zc: u32, // stacking counter of the child windows
pub xom: u32, // zxdg_output_manager_v1 (screen.rs monitors)
pub scale_ovr: i32,
pub refresh: Option<fn()>, // stored only: Wayland has no modal size loop
pub titles: [Vec<u8>; PW_MAX],
}
/// a window record (index 0 = the main window); its protocol objects carry the index as
/// udata, so the event handlers know their window
#[derive(Clone, Copy, Default)]
pub(crate) struct WinRec {
pub surf: u32, // wl_surface (0 = not shown)
pub xsurf: u32,
pub top: u32,
pub vp: u32, // wp_viewport
pub fs: u32, // wp_fractional_scale_v1
pub deco: u32, // zxdg_toplevel_decoration_v1
pub deco_mode: u32,
pub lw: i32, // logical (DIP) size
pub lh: i32,
pub pw: i32, // physical (buffer) size
pub ph: i32,
pub normal_w: i32, // logical size when not maximized
pub normal_h: i32,
pub minw: i32, // minimum size (DIP)
pub minh: i32,
pub flags: u32, // PWF_* | WLWF_*
pub state: u32, // WST_*
pub s120: i32, // scale in 120ths
pub frac: bool, // fractional scale (viewport)
pub dpi: i32,
pub pbs: i32, // preferred_buffer_scale
pub frac_pref: u32, // wp_fractional_scale preferred scale
pub pend_w: i32, // xdg_toplevel.configure until xdg_surface.configure
pub pend_h: i32,
pub pend_state: u32,
pub ack_serial: u32,
pub ack_pending: bool,
pub mapped: bool, // a buffer was attached since it was shown
pub configured: bool, // the first configure since it was shown arrived
pub resize_pending: bool,
pub owner: i32, // owner index (-1 = top-level)
pub shown: bool, // its initial state was committed (shown)
pub outs: u32, // bit per output slot the surface is on
pub cursor: u32, // CUR_*
pub hidmin: bool, // hidden because its owner was minimized
pub dialog: u32, // xdg_dialog_v1
// child windows (WLWF_CHILD)
pub sub: u32, // wl_subsurface
pub cx: i32, // position in the owner's surface (px of the owner)
pub cy: i32,
pub z: u32, // stacking order among the owner's children (higher = above)
pub wm_caps: u32, // xdg_toplevel.wm_capabilities: bit 1 << n for capability n, bit 0 = sent
pub hidpar: bool, // shown, but its owner has no surface now (made when the owner is shown)
}
/// the globals we bind: name, interface, highest version we speak, lowest we accept
static GLOBALS: [(&str, &Iface, u32, u32); 14] = [
("wl_compositor", &IFACE_WL_COMPOSITOR, 6, 4),
("wl_shm", &IFACE_WL_SHM, 1, 1),
("wl_seat", &IFACE_WL_SEAT, 8, 1),
("xdg_wm_base", &IFACE_XDG_WM_BASE, 6, 1),
("wl_data_device_manager", &IFACE_WL_DDM, 3, 1),
("wp_viewporter", &IFACE_WP_VIEWPORTER, 1, 1),
("wp_fractional_scale_manager_v1", &IFACE_WP_FSM, 1, 1),
("wp_cursor_shape_manager_v1", &IFACE_WP_CSM, 1, 1),
("zxdg_decoration_manager_v1", &IFACE_ZXDG_DM, 1, 1),
("xdg_wm_dialog_v1", &IFACE_XDG_WM_DIALOG, 1, 1),
("zwp_tablet_manager_v2", &IFACE_TABLET_MGR, 1, 1), // (PN1) linux_wl_tablet.rs
("wl_subcompositor", &IFACE_WL_SUBCOMPOSITOR, 1, 1), // child windows
("zwp_relative_pointer_manager_v1", &IFACE_ZWP_RPM, 1, 1), // child windows' drags
("xdg_activation_v1", &super::act::IFACE_XDG_ACTIVATION, 1, 1), // raising windows
];
/// leading decimal integer (parse_int: blanks, sign)
fn parse_int(s: &str) -> i32 {
let b = s.as_bytes();
let mut i = 0;
while i < b.len() && b[i] == b' ' {
i += 1;
}
let mut neg = false;
if i < b.len() && b[i] == b'-' {
neg = true;
i += 1;
} else if i < b.len() && b[i] == b'+' {
i += 1;
}
let mut v = 0i32;
while i < b.len() && b[i].is_ascii_digit() {
v = v.wrapping_mul(10).wrapping_add((b[i] - b'0') as i32);
i += 1;
}
if neg { v.wrapping_neg() } else { v }
}
/// window index -> its slot (w & 7, whether it exists or not) [wlw_slot]
pub(crate) fn slot(w: i32) -> usize {
(w as u32 as usize) & (PW_MAX - 1)
}
impl Wl {
/// the environment, then the socket [wl_plat_win_create's start + wl_connect]
pub(crate) fn connect(&mut self) -> bool {
if let Some(v) = crate::env("jfgui_WLDEBUG") {
let c = v.as_bytes().first().copied().unwrap_or(0);
if c != 0 && c != b'0' {
self.w.debug = true;
}
}
if let Some(v) = crate::env("jfgui_SCALE") {
let s = parse_int(&v);
if (1..=4).contains(&s) {
self.g.scale_ovr = s; // test aid: pretend outputs have this scale
}
}
self.g.toolwin_top = crate::env("jfgui_WL_TOOLWIN").is_some_and(|v| v.starts_with('t'));
self.act_start();
// disp.s probing: quiet unless jfgui_BACKEND forces Wayland
self.w.quiet = crate::disp_wl_quiet();
self.w.connect()
}
/// window w if it exists (a hidden window has a record but no surface) [wlw_rec]
pub(crate) fn rec(&self, w: i32) -> Option<usize> {
if (w as u32 as usize) < PW_MAX && self.win[w as usize].flags & WLWF_USED != 0 { Some(w as usize) } else { None }
}
/// index of the window with that wl_surface, -1 (surface 0 finds the main window while
/// there is no connection: synthetic test events) [wlw_find]
pub(crate) fn find(&self, surface: u32) -> i32 {
self.win.iter().position(|r| r.surf == surface).map_or(-1, |i| i as i32)
}
// ------------------------------------------------------------------ creation
/// -> index / -1 [wl_plat_win_create]
pub(crate) fn win_create(&mut self, title: &str, w_dip: i32, h_dip: i32, flags: u32, owner: i32, _x: i32,
_y: i32) -> i32 {
let idx;
if owner == PW_NOOWNER {
if self.win[0].surf != 0 {
return -1;
}
if self.g.reg == 0 {
if self.w.fd < 0 && !self.connect() {
return -1; // no compositor: linux_disp tries X11 / reports
}
self.g.reg = self.w.send(1, &DISPLAY_GET_REGISTRY, &[A::N(&IFACE_WL_REGISTRY)]);
if self.roundtrip() != 0 {
fatal("the compositor did not answer within 5 s");
}
for (name, id) in [("wl_compositor", self.g.comp), ("wl_shm", self.g.shm), ("xdg_wm_base", self.g.xdg_wm)] {
if id == 0 {
crate::write_stderr(alloc::format!("wayland: the compositor does not offer {name}\n").as_bytes());
crate::exit(1);
}
}
self.clip_init();
self.tablet_init(); // (PN1) pen tablets
if self.roundtrip() != 0 {
// outputs, seat capabilities
fatal("the compositor did not answer within 5 s");
}
}
idx = 0;
} else {
if self.win[0].surf == 0 || self.rec(owner).is_none() {
return -1;
}
match (1..PW_MAX).find(|&i| self.win[i].flags & WLWF_USED == 0) {
Some(i) => idx = i,
None => return -1,
}
}
let r = &mut self.win[idx];
let (minw, minh) = (r.minw, r.minh); // a minimum size set before creation stays
*r = WinRec::default();
r.minw = minw;
r.minh = minh;
r.flags = (flags & PWF_MASK) | WLWF_USED | if owner == PW_NOOWNER { WLWF_TOP } else { 0 };
if owner != PW_NOOWNER && flags & (PWF_TOOL | PWF_DIALOG) == PWF_TOOL && self.g.subc != 0 && !self.g.toolwin_top {
r.flags |= WLWF_CHILD;
}
r.owner = owner;
r.lw = w_dip;
r.lh = h_dip;
r.normal_w = w_dip;
r.normal_h = h_dip;
r.s120 = 120;
r.dpi = 96;
// the title is kept: the objects are made again each time the window is shown
self.win_set_title(idx as i32, title.as_bytes());
if owner != PW_NOOWNER {
// until it is on an output: the owner's scale
let o = self.win[slot(owner)];
let r = &mut self.win[idx];
r.s120 = o.s120;
r.frac = o.frac;
r.dpi = o.dpi;
r.frac_pref = o.frac_pref;
let (lw, lh) = (r.lw, r.lh);
let pw = self.phys(lw, idx);
let ph = self.phys(lh, idx);
self.win[idx].pw = pw;
self.win[idx].ph = ph;
return idx as i32; // owned windows: objects when shown
}
self.objects_create(0);
self.update_scale(0);
self.win[0].shown = true;
let s = self.win[0].surf;
self.w.send(s, &SURFACE_COMMIT, &[]);
// wait for the first configure
let deadline = crate::time_us() + 5_000_000;
loop {
self.w.flush_wait();
self.dispatch();
if self.win[0].configured {
break;
}
let now = crate::time_us();
if now >= deadline {
fatal("the compositor did not answer within 5 s");
}
if self.w.wait_readable(((deadline - now) / 1000) as i32 + 1) {
self.w.recv();
}
}
self.cursor_init();
self.w.flush_wait();
0
}
/// the protocol objects of a shown window: wl_surface (+ viewport, fractional scale),
/// xdg_surface, xdg_toplevel (set_parent for owned windows, title, app id, size limits),
/// decoration, xdg_dialog (not committed) [wl_objects_create]
pub(crate) fn objects_create(&mut self, w: usize) {
let flags = self.win[w].flags;
let wu = w as u64;
let surf = self.w.send(self.g.comp, &COMPOSITOR_CREATE_SURFACE, &[A::N(&IFACE_WL_SURFACE)]);
self.win[w].surf = surf;
self.w.set_udata(surf, wu);
if self.g.fsm != 0 && self.g.vpr != 0 {
let vp = self.w.send(self.g.vpr, &VIEWPORTER_GET_VIEWPORT, &[A::N(&IFACE_WP_VIEWPORT), A::O(surf)]);
self.win[w].vp = vp;
let fs = self.w.send(self.g.fsm, &FSM_GET_FRACTIONAL_SCALE, &[A::N(&IFACE_WP_FS), A::O(surf)]);
self.win[w].fs = fs;
self.w.set_udata(fs, wu);
}
if flags & WLWF_CHILD != 0 {
self.child_objects_create(w); // a subsurface of the owner, no xdg roles
return;
}
let xs = self.w.send(self.g.xdg_wm, &WM_BASE_GET_XDG_SURFACE, &[A::N(&IFACE_XDG_SURFACE), A::O(surf)]);
self.win[w].xsurf = xs;
self.w.set_udata(xs, wu);
let top = self.w.send(xs, &XDG_SURFACE_GET_TOPLEVEL, &[A::N(&IFACE_XDG_TOPLEVEL)]);
self.win[w].top = top;
self.w.set_udata(top, wu);
if flags & WLWF_TOP == 0 {
// owned: its parent (0 while the owner is hidden)
let parent = self.win[slot(self.win[w].owner)].top;
self.w.send(top, &TOPLEVEL_SET_PARENT, &[A::O(parent)]);
}
let title = core::mem::take(&mut self.g.titles[w]);
self.w.send(top, &TOPLEVEL_SET_TITLE, &[A::S(Some(&title))]);
self.g.titles[w] = title;
// app id = basename of argv[0]
let id = super::act::app_id();
if !id.is_empty() {
self.w.send(top, &TOPLEVEL_SET_APP_ID, &[A::S(Some(id.as_bytes()))]);
}
self.size_limits(w as i32);
if self.g.decom != 0 {
let d = self.w.send(self.g.decom, &DM_GET_TOPLEVEL_DECORATION, &[A::N(&IFACE_ZXDG_TD), A::O(top)]);
self.win[w].deco = d;
self.w.set_udata(d, wu);
// server side, unless we draw our own
let mode = if flags & PWF_SYSFRAME != 0 { 2 } else { 1 };
self.w.send(d, &TD_SET_MODE, &[A::U(mode)]);
}
if flags & PWF_MODAL != 0 && self.g.dialogm != 0 {
// xdg_wm_dialog_v1 (not on WSLg)
let d = self.w.send(self.g.dialogm, &WM_DIALOG_GET_XDG_DIALOG, &[A::N(&IFACE_XDG_DIALOG), A::O(top)]);
self.win[w].dialog = d;
self.w.send(d, &DIALOG_SET_MODAL, &[]);
}
}
/// the window disappears: its protocol objects are destroyed (on WSLg only a destroyed
/// wl_surface removes the RAIL window: attach NULL + commit, or destroying the
/// xdg_toplevel, leave it on the screen -- measured, WSLg 1.0.79) [wl_objects_destroy]
pub(crate) fn objects_destroy(&mut self, w: usize) {
let r = self.win[w];
if r.surf == 0 {
return;
}
// its child windows go first (shown again with it: hidpar)
for c in 0..PW_MAX {
let cr = self.win[c];
if c != w && cr.flags & (WLWF_USED | WLWF_CHILD) == WLWF_USED | WLWF_CHILD && cr.owner == w as i32 && cr.surf != 0 {
self.objects_destroy(c);
self.win[c].hidpar = true;
}
}
if r.dialog != 0 {
self.w.send(r.dialog, &DIALOG_DESTROY, &[]);
}
if r.deco != 0 {
self.w.send(r.deco, &TD_DESTROY, &[]);
}
if r.top != 0 {
self.w.send(r.top, &TOPLEVEL_DESTROY, &[]);
}
if r.xsurf != 0 {
self.w.send(r.xsurf, &XDG_SURFACE_DESTROY, &[]);
}
if r.sub != 0 {
self.w.send(r.sub, &SUBSURFACE_DESTROY, &[]);
self.child_unmapped(w);
}
if r.fs != 0 {
self.w.send(r.fs, &FS_DESTROY, &[]);
}
if r.vp != 0 {
self.w.send(r.vp, &VIEWPORT_DESTROY, &[]);
}
self.w.send(r.surf, &SURFACE_DESTROY, &[]);
let r = &mut self.win[w];
r.surf = 0;
r.xsurf = 0;
r.top = 0;
r.vp = 0;
r.fs = 0;
r.deco = 0;
r.dialog = 0;
r.sub = 0;
r.shown = false;
r.configured = false;
r.mapped = false;
r.ack_pending = false;
r.resize_pending = false;
r.outs = 0;
self.present_unmapped(w);
if self.inp.ptr_win == w as i32 {
self.inp.ptr_win = -1;
self.inp.ptr_in = false;
}
self.w.flush();
}
/// xdg_toplevel min / max size: fixed windows min = max = the size, resizable ones the
/// minimum (applied with the next commit) [wl_size_limits]
fn size_limits(&mut self, w: i32) {
let Some(i) = self.rec(w) else { return };
let r = self.win[i];
if r.top == 0 {
return;
}
if r.flags & PWF_RESIZABLE == 0 {
self.w.send(r.top, &TOPLEVEL_SET_MIN_SIZE, &[A::I(r.lw), A::I(r.lh)]);
self.w.send(r.top, &TOPLEVEL_SET_MAX_SIZE, &[A::I(r.lw), A::I(r.lh)]);
} else if r.minw != 0 {
self.w.send(r.top, &TOPLEVEL_SET_MIN_SIZE, &[A::I(r.minw), A::I(r.minh)]);
}
}
/// minimum size (also before the window exists) [wl_plat_win_set_min_size]
pub(crate) fn win_set_min_size(&mut self, w: i32, w_dip: i32, h_dip: i32) {
if w as u32 as usize >= PW_MAX {
return;
}
let r = &mut self.win[w as usize];
r.minw = w_dip;
r.minh = h_dip;
if r.surf == 0 || r.flags & PWF_RESIZABLE == 0 {
return;
}
let s = r.surf;
self.size_limits(w);
self.w.send(s, &SURFACE_COMMIT, &[]);
self.w.flush();
}
// ------------------------------------------------------------------ scale + size
/// logical -> physical pixels at window w's scale (fractional: rounded up -- the buffer
/// holds the whole logical size, the viewport's source takes exactly that part) [win_phys]
pub(crate) fn phys(&self, logical: i32, w: usize) -> i32 {
let r = &self.win[w & (PW_MAX - 1)];
let v = logical.wrapping_mul(r.s120);
if r.frac { v.wrapping_add(119) / 120 } else { v / 120 }
}
/// recompute the physical size; E_RESIZE when it changed [win_set_phys]
fn set_phys(&mut self, w: usize) {
let pw = self.phys(self.win[w].lw, w);
let ph = self.phys(self.win[w].lh, w);
let r = &mut self.win[w];
if pw == r.pw && ph == r.ph {
return;
}
r.pw = pw;
r.ph = ph;
if r.configured {
r.resize_pending = true;
self.inp.evwin = w as i32;
self.push(E_RESIZE, 0, pw, ph, 0);
}
self.children_clamp(w);
}
/// the outputs changed: every window picks its scale again [wl_update_scale_all]
fn update_scale_all(&mut self) {
for w in 0..PW_MAX {
self.update_scale(w as i32);
}
}
/// pick window w's scale; on change E_DPI, then E_RESIZE [wl_update_scale]
pub(crate) fn update_scale(&mut self, w: i32) {
let i = slot(w);
let r = self.win[i];
let old = r.s120;
let (s, frac) = if r.frac_pref != 0 && r.vp != 0 {
(r.frac_pref as i32, true)
} else {
let mut v = self.g.scale_ovr;
if v == 0 {
v = r.pbs;
}
if v != 0 {
(v.wrapping_mul(120), false)
} else {
// entered outputs, or every output while the surface is on none
let mut best = 1;
for (k, o) in self.outs.iter().enumerate() {
if o.id != 0 && (r.outs == 0 || r.outs & 1 << k != 0) && o.scale > best {
best = o.scale;
}
}
(best * 120, false)
}
};
if s == r.s120 && frac == r.frac {
return;
}
let dpi = ((s as u32).wrapping_mul(96).wrapping_add(60) / 120) as i32;
let r = &mut self.win[i];
r.s120 = s;
r.frac = frac;
r.dpi = dpi;
if !r.configured {
return;
}
self.inp.evwin = w;
self.push(E_DPI, 0, 0, 0, dpi as u32);
self.children_rescale(i, old); // (before the new size clamps them)
self.set_phys(i);
if w == self.inp.ptr_win {
self.cursor_apply();
}
}
/// largest output mode width * height * 4 [wl_max_mode_bytes]
pub(crate) fn max_mode_bytes(&self) -> usize {
self.outs.iter().filter(|o| o.id != 0).map(|o| o.mw as u32 as usize * o.mh as u32 as usize * 4).max().unwrap_or(0)
}
// ------------------------------------------------------------------ window operations
/// destroy the window's objects and buffers, free its record [wl_plat_win_destroy]
pub(crate) fn win_destroy(&mut self, w: i32) {
let Some(i) = self.rec(w) else { return };
self.owned_orphan(i); // (as on Windows, owned windows go with their owner)
self.objects_destroy(i);
self.present_free(i);
self.w.flush();
if self.inp.kb_win == w {
self.inp.kb_win = 0;
}
self.win[i] = WinRec::default();
}
/// show: its objects and the initial commit (the compositor answers with a configure;
/// then the next present, or the last surface, attaches a buffer); hide: its objects
/// are destroyed [wl_plat_win_show]
pub(crate) fn win_show(&mut self, w: i32, mode: u32) {
let Some(i) = self.rec(w) else { return };
self.win[i].hidmin = false;
if mode == 0 {
self.win[i].hidpar = false;
self.objects_destroy(i);
return;
}
if self.win[i].shown || self.win[i].flags & WLWF_DEAD != 0 {
return;
}
if self.win[i].flags & WLWF_CHILD != 0 {
self.child_show(i);
return;
}
if self.win[i].surf == 0 {
self.objects_create(i);
}
self.win[i].shown = true;
self.update_scale(w);
let s = self.win[i].surf;
self.w.send(s, &SURFACE_COMMIT, &[]);
self.children_show(i);
self.w.flush();
}
/// hide = true: hide the shown windows owned by w (they come back with hide = false,
/// when w is activated again) [wl_hide_owned]
fn hide_owned(&mut self, w: usize, hide: bool) {
for i in 0..PW_MAX {
if i == w || self.rec(i as i32).is_none() || self.win[i].owner != w as i32 {
continue;
}
if self.win[i].flags & WLWF_CHILD != 0 {
continue; // a subsurface disappears and comes back with its owner
}
if hide {
if self.win[i].shown {
self.objects_destroy(i);
self.win[i].hidmin = true;
}
} else if self.win[i].hidmin {
// its last surface comes back with the configure (present_service)
self.win_show(i as i32, PWS_SHOWNA);
}
}
}
/// the size (the position can not be set on Wayland); applied with the next present, not
/// reported back [wl_plat_win_set_rect]
pub(crate) fn win_set_rect(&mut self, w: i32, x: i32, y: i32, cw: i32, ch: i32) {
let Some(i) = self.rec(w) else { return };
if self.win[i].flags & WLWF_CHILD != 0 {
self.child_set_rect(i, x, y, cw, ch);
return;
}
if self.win[i].state & STATE_BIG != 0 {
return; // maximized: the compositor's size
}
// (an integer buffer scale: the buffer is the logical size times it -- an odd size at
// scale 2 would never be presented; the UI takes the size it got with win_size)
let frac = self.win[i].frac;
let mut changed = false;
if cw != PW_KEEP {
let l = self.phys_logical(cw, i);
self.win[i].pw = if frac { cw } else { self.phys(l, i) };
if l != self.win[i].lw {
self.win[i].lw = l;
self.win[i].normal_w = l;
changed = true;
}
}
if ch != PW_KEEP {
let l = self.phys_logical(ch, i);
self.win[i].ph = if frac { ch } else { self.phys(l, i) };
if l != self.win[i].lh {
self.win[i].lh = l;
self.win[i].normal_h = l;
changed = true;
}
}
if changed {
self.size_limits(w); // fixed-size windows: min = max = the size
}
}
/// logical size of px physical pixels at window w's scale (fractional: rounded down, so
/// the buffer covers it and the viewport shows it pixel for pixel) [phys_logical]
fn phys_logical(&self, px: i32, w: usize) -> i32 {
let r = &self.win[w & (PW_MAX - 1)];
let mut s = r.s120 as u32;
if s == 0 {
s = 120;
}
let round = if r.frac { 0 } else { s >> 1 };
((px as u32).wrapping_mul(120).wrapping_add(round) / s) as i32
}
/// the position is unknown on Wayland; a child window's relative to its owner (WST_CHILD)
/// [wl_plat_win_get_rect]
pub(crate) fn win_get_rect(&mut self, w: i32) -> Option<[i32; 4]> {
let i = self.rec(w)?;
let r = &self.win[i];
if r.flags & WLWF_CHILD == 0 {
return None;
}
Some([r.cx, r.cy, r.pw, r.ph])
}
/// unknown on Wayland (the assembly fills in the largest output mode and returns 0)
/// [wl_plat_win_work_area]
pub(crate) fn win_work_area(&mut self, _w: i32) -> Option<[i32; 4]> {
None
}
/// (physical pixels) [wl_plat_win_size]
pub(crate) fn win_size(&self, w: i32) -> (i32, i32) {
match self.rec(w) {
Some(i) => (self.win[i].pw, self.win[i].ph),
None => (0, 0),
}
}
/// [wl_plat_win_dpi]
pub(crate) fn win_dpi(&self, w: i32) -> i32 {
let d = self.win[slot(w)].dpi;
if d == 0 { 96 } else { d }
}
/// (kept per window: sent again when it is shown) [wl_plat_win_set_title]
pub(crate) fn win_set_title(&mut self, w: i32, title: &[u8]) {
if w as u32 as usize >= PW_MAX {
return;
}
let t = super::wire::cstr(title);
let t = &t[..t.len().min(TITLE_MAX - 1)];
self.g.titles[w as usize] = t.to_vec();
let top = self.win[w as usize].top;
if top != 0 {
self.w.send(top, &TOPLEVEL_SET_TITLE, &[A::S(Some(t))]);
self.w.flush();
}
}
/// WCMD_MINIMIZE / MAXIMIZE / RESTORE / CLOSE; minimizing a window hides the windows it
/// owns until it is activated again [wl_plat_win_cmd]
pub(crate) fn win_cmd(&mut self, w: i32, cmd: u32) {
let Some(i) = self.rec(w) else { return };
if cmd == WCMD_CLOSE {
self.inp.evwin = w;
self.push(E_CLOSE, 0, 0, 0, 0);
return;
}
let top = self.win[i].top;
if top == 0 {
return; // hidden
}
match cmd {
WCMD_MINIMIZE => {
if self.win[i].wm_caps & (1 | 1 << 4) == 1 {
return; // the compositor cannot minimize (sway): nothing happens, nothing is hidden
}
self.w.send(top, &TOPLEVEL_SET_MINIMIZED, &[]);
self.win[i].state |= WST_MINIMIZED;
self.hide_owned(i, true);
}
WCMD_MAXIMIZE => {
self.w.send(top, &TOPLEVEL_SET_MAXIMIZED, &[]);
}
WCMD_RESTORE => {
if self.win[i].state & WST_FULLSCREEN != 0 {
self.w.send(top, &TOPLEVEL_UNSET_FULLSCREEN, &[]);
}
if self.win[i].state & WST_MAXIMIZED != 0 {
self.w.send(top, &TOPLEVEL_UNSET_MAXIMIZED, &[]);
}
}
WCMD_FULLSCREEN => {
self.w.send(top, &TOPLEVEL_SET_FULLSCREEN, &[A::O(0)]);
}
WCMD_WINDOWED => {
self.w.send(top, &TOPLEVEL_UNSET_FULLSCREEN, &[]);
}
_ => return,
}
self.w.flush();
}
/// WST_* bits of window w, WST_ACTIVE when one of the app's windows is activated, plus
/// WST_CSD when the compositor draws no frame for it (no xdg-decoration, or it chose
/// client-side): the UI draws its own title bar and resize band (WSLg, GNOME)
/// [wl_plat_win_state]
pub(crate) fn win_state(&self, w: i32) -> u32 {
let r = &self.win[slot(w)];
if r.flags & (WLWF_USED | WLWF_CHILD) == WLWF_USED | WLWF_CHILD {
// a child window: its owner's (root's) minimized state, the app's activation
let mut s = WST_CHILD | WST_CSD | (self.win[self.root_of(slot(w))].state & WST_MINIMIZED);
if self.win.iter().any(|x| x.state & WST_ACTIVE != 0 && x.surf != 0) {
s |= WST_ACTIVE;
}
return s;
}
let mut s = r.state & !WST_ACTIVE;
if self.win.iter().any(|x| x.state & WST_ACTIVE != 0 && x.surf != 0) {
s |= WST_ACTIVE;
}
if self.g.decom == 0 || r.deco_mode != 2 {
s |= WST_CSD;
}
s
}
/// let the compositor move the window (call on a button press) [wl_plat_win_begin_move]
pub(crate) fn win_begin_move(&mut self, w: i32) {
let Some(i) = self.rec(w) else { return };
if self.win[i].flags & WLWF_CHILD != 0 {
self.child_drag_begin(i, 0);
return;
}
let top = self.win[i].top;
if top == 0 || self.g.seat == 0 {
return;
}
self.w.send(top, &TOPLEVEL_MOVE, &[A::O(self.g.seat), A::U(self.inp.btn_serial)]);
self.release_buttons();
self.w.flush();
}
/// let the compositor resize from an edge (EDGE_* bits equal xdg_toplevel.resize_edge
/// values) [wl_plat_win_begin_resize]
pub(crate) fn win_begin_resize(&mut self, w: i32, edges: u32) {
let e = edges & 15;
if e == 0 {
return;
}
let Some(i) = self.rec(w) else { return };
if self.win[i].flags & WLWF_CHILD != 0 {
self.child_drag_begin(i, e);
return;
}
let top = self.win[i].top;
if top == 0 || self.g.seat == 0 {
return;
}
self.w.send(top, &TOPLEVEL_RESIZE, &[A::O(self.g.seat), A::U(self.inp.btn_serial), A::U(e)]);
self.release_buttons();
self.w.flush();
}
/// the compositor's window menu at x, y (px of the window; on a button press: its serial)
pub(crate) fn win_show_menu(&mut self, w: i32, x: i32, y: i32) {
let Some(i) = self.rec(w) else { return };
let top = self.win[i].top;
if top == 0 || self.g.seat == 0 {
return; // a child window has none
}
let s = self.win[i].s120.max(1);
let (lx, ly) = (x * 120 / s, y * 120 / s);
self.w.send(top, &TOPLEVEL_SHOW_WINDOW_MENU, &[A::O(self.g.seat), A::U(self.inp.btn_serial), A::I(lx), A::I(ly)]);
self.release_buttons(); // the menu has the pointer now
self.w.flush();
}
// ------------------------------------------------------------------ event loop
/// configures that need no new frame (the window is mapped at the size it has):
/// acknowledged right away [wl_ack_idle]
fn ack_idle(&mut self) {
for i in 0..PW_MAX {
let r = self.win[i];
if r.surf == 0 || !r.ack_pending || !r.mapped || r.resize_pending {
continue;
}
self.win[i].ack_pending = false;
self.w.send(r.xsurf, &XDG_SURFACE_ACK_CONFIGURE, &[A::U(r.ack_serial)]);
self.w.send(r.surf, &SURFACE_COMMIT, &[]);
}
}
/// pump the compositor until an event is queued or the deadline passes -> queued
/// events [wl_plat_wait]
pub(crate) fn wait(&mut self, deadline: u64) -> u32 {
// (not connected yet; a hidden main window still waits: its owned windows, the
// clipboard and wake-ups are served)
if self.w.fd < 0 {
return crate::evq_count();
}
let mut polled = false; // the socket was polled once
loop {
self.dispatch();
let now = crate::time_us();
let mut next = self.input_service(now); // key repeat -> next deadline
self.ack_idle();
let p = self.present_service(now); // deferred frames -> next deadline
if p < next {
next = p;
}
let c = self.clip_tick(now); // a stalled drop transfer gives up
if c < next {
next = c;
}
self.w.flush();
let n = crate::evq_count();
if n != 0 {
return n;
}
// (linux_wl_gpu.rs) a frame that waited for the compositor may go on
#[cfg(jfgui_glibc)]
if core::mem::take(&mut self.dm.wake) {
return 0;
}
let ms: i32;
if deadline == 0 || (deadline != u64::MAX && now >= deadline) {
// no waiting, but always read what the compositor sent once per call (frame
// callbacks, pings), even when the caller is late
if polled {
return 0;
}
ms = 0;
} else {
if deadline != u64::MAX && deadline < next {
next = deadline;
}
ms = if next == u64::MAX {
-1
} else if next <= now {
0
} else {
((next - now + 999) / 1000).min(0x7FFF_FFFF) as i32
};
}
polled = true;
let mut fds = [PollFd::default(); POLL_MAX];
fds[0] = PollFd { fd: self.w.fd, events: POLLIN | if self.w.pending_out() != 0 { POLLOUT } else { 0 },
revents: 0 };
let nclip = self.clip_pollfds(&mut fds[1..POLL_MAX - 1]);
let wk = nclip + 1; // the wake eventfd after them (poll ignores fd -1)
fds[wk] = PollFd { fd: crate::linux::thread::wake_fd(), events: POLLIN, revents: 0 };
let r = poll(&mut fds[..wk + 1], ms);
if r == -EINTR {
continue;
}
if r < 0 {
fatal("poll() failed");
}
let rev = fds[0].revents;
if rev & POLLIN != 0 {
self.w.recv();
} else if rev & (POLLERR | POLLHUP | POLLNVAL) != 0 {
fatal("connection to the compositor lost");
}
if fds[wk].revents & POLLIN != 0 {
crate::evq_push(crate::linux::thread::wake_consume()); // wake() from another thread
}
let ready = fds;
self.clip_service(&ready[1..1 + nclip]);
}
}
}
// ------------------------------------------------------------------ registry
/// wl_registry.global(name, interface, version): bind what we use [wlh_global]
pub(crate) fn wlh_global(wl: &mut Wl, _id: u32, a: &Args, _u: u64) {
let name = a.u(0);
let Some(iface) = a.s(1) else { return };
let ver = a.u(2);
if iface == b"zwp_text_input_manager_v3" && wl.ime.manager==0 {
wl.ime.manager=wl.w.send(wl.g.reg,®ISTRY_BIND,&[A::U(name),A::B(&super::ime::MANAGER,1)]);
wl.ime_setup(); return;
}
// (the GPU's buffers; below version 3 there are no modifiers: the CPU path)
#[cfg(jfgui_glibc)]
if iface == b"zwp_linux_dmabuf_v1" {
if wl.dm.mgr == 0 && ver >= 3 {
wl.dm.ver = ver.min(4);
wl.dm.mgr = wl.w.send(wl.g.reg, ®ISTRY_BIND, &[A::U(name), A::B(&super::gpu::IFACE_DMABUF, wl.dm.ver)]);
}
return;
}
#[cfg(jfgui_glibc)]
if iface == b"wp_presentation" {
wl.gpu_feedback_setup(name, ver); // (a bench says when a frame reached the screen)
return;
}
if iface == b"wl_output" {
let Some(k) = wl.outs.iter().position(|o| o.id == 0) else { return };
let v = ver.min(4);
let id = wl.w.send(wl.g.reg, ®ISTRY_BIND, &[A::U(name), A::B(&IFACE_WL_OUTPUT, v)]);
wl.outs[k] = Out { id, name, scale: 1, ver: v, ..Out::default() };
wl.w.set_udata(id, k as u64 + 1);
wl.xdg_output(k);
return;
}
if iface == b"zxdg_output_manager_v1" && wl.g.xom == 0 {
wl.g.xom = wl.w.send(wl.g.reg, ®ISTRY_BIND, &[A::U(name), A::B(&IFACE_ZXDG_OUTPUT_MANAGER, ver.min(3))]);
for k in 0..OUT_MAX {
wl.xdg_output(k);
}
return;
}
let Some(gi) = GLOBALS.iter().position(|g| g.0.as_bytes() == iface) else { return };
let (gname, gif, maxv, minv) = GLOBALS[gi];
if wl.global_id(gi) != 0 {
return; // first one wins (e.g. seats)
}
if ver < minv {
crate::write_stderr(alloc::format!("wayland: {gname} version {ver} is too old, not used\n").as_bytes());
return;
}
let v = ver.min(maxv);
let id = wl.w.send(wl.g.reg, ®ISTRY_BIND, &[A::U(name), A::B(gif, v)]);
wl.set_global(gi, id, v);
wl.ime_setup();
}
impl Wl {
/// the zxdg_output_v1 of output slot k (once both the output and the manager are bound)
fn xdg_output(&mut self, k: usize) {
let o = self.outs[k];
if o.id == 0 || o.xo != 0 || self.g.xom == 0 {
return;
}
let xo = self.w.send(self.g.xom, &XOM_GET_XDG_OUTPUT, &[A::N(&IFACE_ZXDG_OUTPUT), A::O(o.id)]);
self.w.set_udata(xo, k as u64 + 1);
self.outs[k].xo = xo;
}
/// (screen.rs) every output as a monitor: the logical rectangle (xdg-output, else the
/// position and the mode / scale), dpi from physical / logical width, the refresh, the
/// name; no work area and no primary on Wayland (the first output counts)
pub(crate) fn monitors(&self) -> alloc::vec::Vec<crate::screen::Monitor> {
let mut v = alloc::vec::Vec::new();
for o in self.outs.iter().filter(|o| o.id != 0) {
let (mw, mh) = if o.transform & 1 != 0 { (o.mh, o.mw) } else { (o.mw, o.mh) };
let sc = o.scale.max(1);
let rect = if o.lw > 0 && o.lh > 0 { [o.lx, o.ly, o.lw, o.lh] } else { [o.x, o.y, mw / sc, mh / sc] };
let dpi = if mw > 0 && rect[2] > 0 { (mw as u32 * 96 + rect[2] as u32 / 2) / rect[2] as u32 } else { 96 * sc as u32 };
v.push(crate::screen::Monitor {
id: o.name as u64,
name: alloc::string::String::from_utf8_lossy(&o.label[..o.label_len as usize]).into_owned(),
rect,
work: rect,
dpi,
refresh_mhz: o.refresh.max(0) as u32,
primary: v.is_empty(),
});
}
v
}
/// an output changed after start-up: E_SETTINGS with SET_MONITORS
fn monitors_changed(&mut self) {
if self.win[0].configured {
self.inp.evwin = 0;
self.push(crate::E_SETTINGS, 0, 0, 0, crate::SET_MONITORS);
}
}
fn global_id(&self, gi: usize) -> u32 {
let g = &self.g;
[g.comp, g.shm, g.seat, g.xdg_wm, g.ddm, g.vpr, g.fsm, g.csm, g.decom, g.dialogm, g.tabm, g.subc, g.rpm, g.actm][gi]
}
fn set_global(&mut self, gi: usize, id: u32, v: u32) {
let g = &mut self.g;
match gi {
0 => {
g.comp = id;
g.comp_ver = v;
}
1 => g.shm = id,
2 => {
g.seat = id;
g.seat_ver = v;
}
3 => g.xdg_wm = id,
4 => {
g.ddm = id;
g.ddm_ver = v;
}
5 => g.vpr = id,
6 => g.fsm = id,
7 => g.csm = id,
8 => g.decom = id,
9 => g.dialogm = id,
10 => g.tabm = id,
11 => g.subc = id,
12 => g.rpm = id,
_ => g.actm = id,
}
}
}
/// an output went away [wlh_global_remove]
pub(crate) fn wlh_global_remove(wl: &mut Wl, _id: u32, a: &Args, _u: u64) {
let name = a.u(0);
for k in 0..OUT_MAX {
let o = wl.outs[k];
if o.id == 0 || o.name != name {
continue;
}
wl.w.set_udata(o.id, 0);
if o.xo != 0 {
wl.w.set_udata(o.xo, 0);
wl.w.send(o.xo, &XDG_OUTPUT_DESTROY, &[]);
}
if o.ver >= 3 {
wl.w.send(o.id, &OUTPUT_RELEASE, &[]);
}
wl.outs[k] = Out::default();
for r in wl.win.iter_mut() {
r.outs &= !(1 << k); // no window is on it any more
}
wl.update_scale_all();
wl.monitors_changed();
}
}
/// remember the current mode size [wlh_output_mode]
pub(crate) fn wlh_output_mode(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
if u == 0 || a.u(0) & 1 == 0 {
return; // WL_OUTPUT_MODE_CURRENT
}
let o = &mut wl.outs[(u - 1) as usize & (OUT_MAX - 1)];
o.mw = a.i(1);
o.mh = a.i(2);
o.refresh = a.i(3);
}
/// position in the compositor's space and transform (screen.rs monitors)
pub(crate) fn wlh_output_geometry(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
if u == 0 {
return;
}
let o = &mut wl.outs[(u - 1) as usize & (OUT_MAX - 1)];
o.x = a.i(0);
o.y = a.i(1);
o.transform = a.i(7);
}
// an output's name into its slot (cut to 31 bytes)
fn out_label(wl: &mut Wl, u: u64, s: Option<&[u8]>) {
let Some(s) = s else { return };
if u == 0 {
return;
}
let o = &mut wl.outs[(u - 1) as usize & (OUT_MAX - 1)];
let n = s.len().min(31);
o.label[..n].copy_from_slice(&s[..n]);
o.label_len = n as u8;
}
/// wl_output.name (v4): "HDMI-A-1"
pub(crate) fn wlh_output_name(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
out_label(wl, u, a.s(0));
}
/// zxdg_output_v1.logical_position
pub(crate) fn wlh_xo_pos(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
if u != 0 {
let o = &mut wl.outs[(u - 1) as usize & (OUT_MAX - 1)];
o.lx = a.i(0);
o.ly = a.i(1);
}
}
/// zxdg_output_v1.logical_size
pub(crate) fn wlh_xo_size(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
if u != 0 {
let o = &mut wl.outs[(u - 1) as usize & (OUT_MAX - 1)];
o.lw = a.i(0);
o.lh = a.i(1);
}
}
/// zxdg_output_v1.name (when wl_output has none: below version 4)
pub(crate) fn wlh_xo_name(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
if u != 0 && wl.outs[(u - 1) as usize & (OUT_MAX - 1)].label_len == 0 {
out_label(wl, u, a.s(0));
}
}
/// [wlh_output_scale]
pub(crate) fn wlh_output_scale(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
if u == 0 {
return;
}
wl.outs[(u - 1) as usize & (OUT_MAX - 1)].scale = a.i(0).clamp(1, 8);
}
/// output properties are complete [wlh_output_done]
pub(crate) fn wlh_output_done(wl: &mut Wl, _id: u32, _a: &Args, _u: u64) {
wl.update_scale_all();
wl.monitors_changed();
}
/// surface.enter / leave bookkeeping [win_out_mark]
fn win_out_mark(wl: &mut Wl, output: u32, entered: bool, w: u64) {
let i = w as usize & (PW_MAX - 1);
for k in 0..OUT_MAX {
if output == 0 || wl.outs[k].id != output {
continue;
}
wl.win[i].outs &= !(1 << k);
if entered {
wl.win[i].outs |= 1 << k;
}
}
wl.update_scale(w as i32);
}
/// the surface entered an output (scale bookkeeping) [wlh_surface_enter]
pub(crate) fn wlh_surface_enter(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
win_out_mark(wl, a.u(0), true, u);
}
/// the surface left an output [wlh_surface_leave]
pub(crate) fn wlh_surface_leave(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
win_out_mark(wl, a.u(0), false, u);
}
/// wl_surface.preferred_buffer_scale (compositor v6) [wlh_surface_pbs]
pub(crate) fn wlh_surface_pbs(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
wl.win[u as usize & (PW_MAX - 1)].pbs = a.i(0).max(1);
wl.update_scale(u as i32);
}
/// wp_fractional_scale_v1.preferred_scale (120ths) [wlh_frac_scale]
pub(crate) fn wlh_frac_scale(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
let v = a.u(0);
wl.win[u as usize & (PW_MAX - 1)].frac_pref = if v < 60 { 120 } else { v };
wl.update_scale(u as i32);
}
// ------------------------------------------------------------------ xdg shell events
/// xdg_wm_base.ping: answer at once [wlh_ping]
pub(crate) fn wlh_ping(wl: &mut Wl, id: u32, a: &Args, _u: u64) {
wl.w.send(id, &WM_BASE_PONG, &[A::U(a.u(0))]);
}
/// xdg_toplevel.configure(width, height, states) [wlh_top_configure]
pub(crate) fn wlh_top_configure(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
let r = &mut wl.win[u as usize & (PW_MAX - 1)];
r.pend_w = a.i(0);
r.pend_h = a.i(1);
let mut st = 0;
for s in a.arr(2).chunks_exact(4) {
match u32::from_le_bytes([s[0], s[1], s[2], s[3]]) {
1 => st |= WST_MAXIMIZED,
2 => st |= WST_FULLSCREEN,
4 => st |= WST_ACTIVE,
// tiled_left, tiled_right, tiled_top, tiled_bottom
5 => st |= WST_TILED | crate::WST_TILED_LEFT,
6 => st |= WST_TILED | crate::WST_TILED_RIGHT,
7 => st |= WST_TILED | crate::WST_TILED_TOP,
8 => st |= WST_TILED | crate::WST_TILED_BOTTOM,
_ => {}
}
}
r.pend_state = st;
}
/// xdg_toplevel.wm_capabilities(array of u32): what the compositor offers (window_menu 1,
/// maximize 2, fullscreen 3, minimize 4)
pub(crate) fn wlh_top_caps(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
let mut caps = 1;
for s in a.arr(0).chunks_exact(4) {
let c = u32::from_le_bytes([s[0], s[1], s[2], s[3]]);
if (1..32).contains(&c) {
caps |= 1 << c;
}
}
wl.win[u as usize & (PW_MAX - 1)].wm_caps = caps;
}
/// xdg_surface.configure(serial): latch the state [wlh_xdg_configure]
pub(crate) fn wlh_xdg_configure(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
let i = u as usize & (PW_MAX - 1);
wl.win[i].ack_serial = a.u(0);
wl.win[i].ack_pending = true;
let old = wl.win[i].state;
let mut new = wl.win[i].pend_state;
if new & WST_ACTIVE != 0 {
if old & WST_MINIMIZED != 0 {
// activated again after a minimize: the windows it hid come back
wl.hide_owned(i, false);
}
} else {
new |= old & WST_MINIMIZED; // stays minimized until activated again
}
let r = &mut wl.win[i];
r.state = new;
let (pw0, ph0) = (r.pw, r.ph);
let (mut w, mut h) = (r.pend_w, r.pend_h);
if w <= 0 || h <= 0 {
// no size suggested: back to the normal size when leaving a maximized (or tiled /
// fullscreen) state, else keep ours
w = r.lw;
h = r.lh;
if old & STATE_BIG != 0 && new & STATE_BIG == 0 {
w = r.normal_w;
h = r.normal_h;
}
}
if new & STATE_BIG == 0 {
r.normal_w = w;
r.normal_h = h;
}
r.lw = w;
r.lh = h;
// configured first: a size the first configure after a show brings (a maximized window
// shown again, a dialog the compositor made smaller) is reported like any other (E_RESIZE)
r.configured = true;
wl.set_phys(i);
// (L7) full screen came or went at the same size: the UI looks at the state after a resize
if (old ^ new) & WST_FULLSCREEN != 0 && (wl.win[i].pw, wl.win[i].ph) == (pw0, ph0) {
let (pw, ph) = (wl.win[i].pw, wl.win[i].ph);
wl.inp.evwin = i as i32;
wl.push(E_RESIZE, 0, pw, ph, 0);
}
}
/// the user asked to close the window [wlh_top_close]
pub(crate) fn wlh_top_close(wl: &mut Wl, _id: u32, _a: &Args, u: u64) {
wl.inp.evwin = (u as usize & (PW_MAX - 1)) as i32;
wl.push(E_CLOSE, 0, 0, 0, 0);
}
/// decoration mode chosen by the compositor [wlh_deco_configure]
pub(crate) fn wlh_deco_configure(wl: &mut Wl, _id: u32, a: &Args, u: u64) {
wl.win[u as usize & (PW_MAX - 1)].deco_mode = a.u(0);
}