-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwayland.c
2390 lines (1981 loc) · 72.5 KB
/
wayland.c
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
#include "wayland.h"
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <cursor-shape-v1.h>
#include <wayland-client.h>
#include <wayland-cursor.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <tllist.h>
#define LOG_MODULE "wayland"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "config.h"
#include "terminal.h"
#include "ime.h"
#include "input.h"
#include "render.h"
#include "selection.h"
#include "shm.h"
#include "shm-formats.h"
#include "util.h"
#include "xmalloc.h"
static void
csd_reload_font(struct wl_window *win, float old_scale)
{
struct terminal *term = win->term;
const struct config *conf = term->conf;
const float scale = term->scale;
bool enable_csd = win->csd_mode == CSD_YES && !win->is_fullscreen;
if (!enable_csd)
return;
if (win->csd.font != NULL && scale == old_scale)
return;
fcft_destroy(win->csd.font);
const char *patterns[conf->csd.font.count];
for (size_t i = 0; i < conf->csd.font.count; i++)
patterns[i] = conf->csd.font.arr[i].pattern;
char pixelsize[32];
snprintf(pixelsize, sizeof(pixelsize), "pixelsize=%u",
(int)roundf(conf->csd.title_height * scale * 1 / 2));
LOG_DBG("loading CSD font \"%s:%s\" (old-scale=%.2f, scale=%.2f)",
patterns[0], pixelsize, old_scale, scale);
win->csd.font = fcft_from_name(conf->csd.font.count, patterns, pixelsize);
}
static void
csd_instantiate(struct wl_window *win)
{
struct wayland *wayl = win->term->wl;
xassert(wayl != NULL);
for (size_t i = 0; i < CSD_SURF_MINIMIZE; i++) {
bool ret = wayl_win_subsurface_new(win, &win->csd.surface[i], true);
xassert(ret);
}
for (size_t i = CSD_SURF_MINIMIZE; i < CSD_SURF_COUNT; i++) {
bool ret = wayl_win_subsurface_new_with_custom_parent(
win, win->csd.surface[CSD_SURF_TITLE].surface.surf, &win->csd.surface[i],
true);
xassert(ret);
}
csd_reload_font(win, -1.);
}
static void
csd_destroy(struct wl_window *win)
{
struct terminal *term = win->term;
fcft_destroy(term->window->csd.font);
term->window->csd.font = NULL;
for (size_t i = 0; i < ALEN(win->csd.surface); i++)
wayl_win_subsurface_destroy(&win->csd.surface[i]);
shm_purge(term->render.chains.csd);
}
static void
seat_add_data_device(struct seat *seat)
{
if (seat->wayl->data_device_manager == NULL)
return;
if (seat->data_device != NULL) {
/* TODO: destroy old device + clipboard data? */
return;
}
struct wl_data_device *data_device = wl_data_device_manager_get_data_device(
seat->wayl->data_device_manager, seat->wl_seat);
if (data_device == NULL)
return;
seat->data_device = data_device;
wl_data_device_add_listener(data_device, &data_device_listener, seat);
}
static void
seat_add_primary_selection(struct seat *seat)
{
if (seat->wayl->primary_selection_device_manager == NULL)
return;
if (seat->primary_selection_device != NULL)
return;
struct zwp_primary_selection_device_v1 *primary_selection_device
= zwp_primary_selection_device_manager_v1_get_device(
seat->wayl->primary_selection_device_manager, seat->wl_seat);
if (primary_selection_device == NULL)
return;
seat->primary_selection_device = primary_selection_device;
zwp_primary_selection_device_v1_add_listener(
primary_selection_device, &primary_selection_device_listener, seat);
}
static void
seat_add_text_input(struct seat *seat)
{
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
if (seat->wayl->text_input_manager == NULL)
return;
struct zwp_text_input_v3 *text_input
= zwp_text_input_manager_v3_get_text_input(
seat->wayl->text_input_manager, seat->wl_seat);
if (text_input == NULL)
return;
seat->wl_text_input = text_input;
zwp_text_input_v3_add_listener(text_input, &text_input_listener, seat);
#endif
}
static void
seat_add_key_bindings(struct seat *seat)
{
key_binding_new_for_seat(seat->wayl->key_binding_manager, seat);
}
static void
seat_destroy(struct seat *seat)
{
if (seat == NULL)
return;
tll_free(seat->mouse.buttons);
key_binding_remove_seat(seat->wayl->key_binding_manager, seat);
if (seat->kbd.xkb_compose_state != NULL)
xkb_compose_state_unref(seat->kbd.xkb_compose_state);
if (seat->kbd.xkb_compose_table != NULL)
xkb_compose_table_unref(seat->kbd.xkb_compose_table);
if (seat->kbd.xkb_keymap != NULL)
xkb_keymap_unref(seat->kbd.xkb_keymap);
if (seat->kbd.xkb_state != NULL)
xkb_state_unref(seat->kbd.xkb_state);
if (seat->kbd.xkb != NULL)
xkb_context_unref(seat->kbd.xkb);
if (seat->kbd.repeat.fd >= 0)
fdm_del(seat->wayl->fdm, seat->kbd.repeat.fd);
if (seat->pointer.theme != NULL)
wl_cursor_theme_destroy(seat->pointer.theme);
if (seat->pointer.surface.surf != NULL)
wl_surface_destroy(seat->pointer.surface.surf);
if (seat->pointer.surface.viewport != NULL)
wp_viewport_destroy(seat->pointer.surface.viewport);
if (seat->pointer.xcursor_callback != NULL)
wl_callback_destroy(seat->pointer.xcursor_callback);
if (seat->clipboard.data_source != NULL)
wl_data_source_destroy(seat->clipboard.data_source);
if (seat->clipboard.data_offer != NULL)
wl_data_offer_destroy(seat->clipboard.data_offer);
if (seat->primary.data_source != NULL)
zwp_primary_selection_source_v1_destroy(seat->primary.data_source);
if (seat->primary.data_offer != NULL)
zwp_primary_selection_offer_v1_destroy(seat->primary.data_offer);
if (seat->primary_selection_device != NULL)
zwp_primary_selection_device_v1_destroy(seat->primary_selection_device);
if (seat->data_device != NULL)
wl_data_device_release(seat->data_device);
if (seat->pointer.shape_device != NULL)
wp_cursor_shape_device_v1_destroy(seat->pointer.shape_device);
if (seat->wl_keyboard != NULL)
wl_keyboard_release(seat->wl_keyboard);
if (seat->wl_pointer != NULL)
wl_pointer_release(seat->wl_pointer);
if (seat->wl_touch != NULL)
wl_touch_release(seat->wl_touch);
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
if (seat->wl_text_input != NULL)
zwp_text_input_v3_destroy(seat->wl_text_input);
#endif
if (seat->wl_seat != NULL)
wl_seat_release(seat->wl_seat);
ime_reset_pending(seat);
free(seat->clipboard.text);
free(seat->primary.text);
free(seat->pointer.last_custom_xcursor);
free(seat->name);
}
static void
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{
#if defined(_DEBUG)
bool have_description = false;
for (size_t i = 0; i < ALEN(shm_formats); i++) {
if (shm_formats[i].format == format) {
LOG_DBG("shm: 0x%08x: %s", format, shm_formats[i].description);
have_description = true;
break;
}
}
if (!have_description)
LOG_DBG("shm: 0x%08x: unknown", format);
#endif
}
static const struct wl_shm_listener shm_listener = {
.format = &shm_format,
};
static void
xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
{
LOG_DBG("wm base ping");
xdg_wm_base_pong(shell, serial);
}
static const struct xdg_wm_base_listener xdg_wm_base_listener = {
.ping = &xdg_wm_base_ping,
};
static void
seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
enum wl_seat_capability caps)
{
struct seat *seat = data;
xassert(seat->wl_seat == wl_seat);
LOG_DBG("%s: keyboard=%s, pointer=%s, touch=%s", seat->name,
(caps & WL_SEAT_CAPABILITY_KEYBOARD) ? "yes" : "no",
(caps & WL_SEAT_CAPABILITY_POINTER) ? "yes" : "no",
(caps & WL_SEAT_CAPABILITY_TOUCH) ? "yes" : "no");
if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
if (seat->wl_keyboard == NULL) {
seat->wl_keyboard = wl_seat_get_keyboard(wl_seat);
wl_keyboard_add_listener(seat->wl_keyboard, &keyboard_listener, seat);
}
} else {
if (seat->wl_keyboard != NULL) {
wl_keyboard_release(seat->wl_keyboard);
seat->wl_keyboard = NULL;
}
}
if (caps & WL_SEAT_CAPABILITY_POINTER) {
if (seat->wl_pointer == NULL) {
xassert(seat->pointer.surface.surf == NULL);
seat->pointer.surface.surf =
wl_compositor_create_surface(seat->wayl->compositor);
if (seat->pointer.surface.surf == NULL) {
LOG_ERR("%s: failed to create pointer surface", seat->name);
return;
}
if (seat->wayl->viewporter != NULL) {
xassert(seat->pointer.surface.viewport == NULL);
seat->pointer.surface.viewport = wp_viewporter_get_viewport(
seat->wayl->viewporter, seat->pointer.surface.surf);
if (seat->pointer.surface.viewport == NULL) {
LOG_ERR("%s: failed to create pointer viewport", seat->name);
wl_surface_destroy(seat->pointer.surface.surf);
seat->pointer.surface.surf = NULL;
return;
}
}
seat->wl_pointer = wl_seat_get_pointer(wl_seat);
wl_pointer_add_listener(seat->wl_pointer, &pointer_listener, seat);
if (seat->wayl->cursor_shape_manager != NULL) {
xassert(seat->pointer.shape_device == NULL);
seat->pointer.shape_device = wp_cursor_shape_manager_v1_get_pointer(
seat->wayl->cursor_shape_manager, seat->wl_pointer);
}
}
} else {
if (seat->wl_pointer != NULL) {
if (seat->pointer.shape_device != NULL) {
wp_cursor_shape_device_v1_destroy(seat->pointer.shape_device);
seat->pointer.shape_device = NULL;
}
wl_pointer_release(seat->wl_pointer);
wl_surface_destroy(seat->pointer.surface.surf);
if (seat->pointer.surface.viewport != NULL) {
wp_viewport_destroy(seat->pointer.surface.viewport);
seat->pointer.surface.viewport = NULL;
}
if (seat->pointer.theme != NULL)
wl_cursor_theme_destroy(seat->pointer.theme);
if (seat->wl_touch != NULL &&
seat->touch.state == TOUCH_STATE_INHIBITED)
{
seat->touch.state = TOUCH_STATE_IDLE;
}
seat->wl_pointer = NULL;
seat->pointer.surface.surf = NULL;
seat->pointer.theme = NULL;
seat->pointer.cursor = NULL;
}
}
if (caps & WL_SEAT_CAPABILITY_TOUCH) {
if (seat->wl_touch == NULL) {
seat->wl_touch = wl_seat_get_touch(wl_seat);
wl_touch_add_listener(seat->wl_touch, &touch_listener, seat);
seat->touch.state = TOUCH_STATE_IDLE;
}
} else {
if (seat->wl_touch != NULL) {
wl_touch_release(seat->wl_touch);
seat->wl_touch = NULL;
}
seat->touch.state = TOUCH_STATE_INHIBITED;
}
}
static void
seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
{
struct seat *seat = data;
free(seat->name);
seat->name = xstrdup(name);
}
static const struct wl_seat_listener seat_listener = {
.capabilities = seat_handle_capabilities,
.name = seat_handle_name,
};
static void
update_term_for_output_change(struct terminal *term)
{
const float old_scale = term->scale;
const float logical_width = term->width / old_scale;
const float logical_height = term->height / old_scale;
/* Note: order matters! term_update_scale() must come first */
bool scale_updated = term_update_scale(term);
bool fonts_updated = term_font_dpi_changed(term, old_scale);
term_font_subpixel_changed(term);
csd_reload_font(term->window, old_scale);
enum resize_options resize_opts = RESIZE_KEEP_GRID;
if (fonts_updated) {
/*
* If the fonts have been updated, the cell dimensions have
* changed. This requires a "forced" resize, since the surface
* buffer dimensions may not have been updated (in which case
* render_resize() normally shortcuts and returns early).
*/
resize_opts |= RESIZE_FORCE;
} else if (!scale_updated) {
/* No need to resize if neither scale nor fonts have changed */
return;
} else if (term->conf->dpi_aware) {
/*
* If fonts are sized according to DPI, it is possible for the cell
* size to remain the same when display scale changes. This will not
* change the surface buffer dimensions, but will change the logical
* size of the window. To ensure that the compositor is made aware of
* the proper logical size, force a resize rather than allowing
* render_resize() to shortcut the notification if the buffer
* dimensions remain the same.
*/
resize_opts |= RESIZE_FORCE;
}
render_resize(
term,
(int)roundf(logical_width),
(int)roundf(logical_height),
resize_opts);
}
static void
update_terms_on_monitor(struct monitor *mon)
{
struct wayland *wayl = mon->wayl;
tll_foreach(wayl->terms, it) {
struct terminal *term = it->item;
tll_foreach(term->window->on_outputs, it2) {
if (it2->item == mon) {
update_term_for_output_change(term);
break;
}
}
}
}
static void
output_update_ppi(struct monitor *mon)
{
if (mon->dim.mm.width <= 0 || mon->dim.mm.height <= 0)
return;
double x_inches = mon->dim.mm.width * 0.03937008;
double y_inches = mon->dim.mm.height * 0.03937008;
const int width = mon->dim.px_real.width;
const int height = mon->dim.px_real.height;
mon->ppi.real.x = mon->dim.px_real.width / x_inches;
mon->ppi.real.y = mon->dim.px_real.height / y_inches;
/* The *logical* size is affected by the transform */
switch (mon->transform) {
case WL_OUTPUT_TRANSFORM_90:
case WL_OUTPUT_TRANSFORM_270:
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
case WL_OUTPUT_TRANSFORM_FLIPPED_270: {
int swap = x_inches;
x_inches = y_inches;
y_inches = swap;
break;
}
case WL_OUTPUT_TRANSFORM_NORMAL:
case WL_OUTPUT_TRANSFORM_180:
case WL_OUTPUT_TRANSFORM_FLIPPED:
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
break;
}
const int scaled_width = mon->dim.px_scaled.width;
const int scaled_height = mon->dim.px_scaled.height;
mon->ppi.scaled.x = scaled_width / x_inches;
mon->ppi.scaled.y = scaled_height / y_inches;
const double px_diag_physical = sqrt(pow(width, 2) + pow(height, 2));
mon->dpi.physical = width == 0 && height == 0
? 96.
: px_diag_physical / mon->inch;
const double px_diag_scaled = sqrt(pow(scaled_width, 2) + pow(scaled_height, 2));
mon->dpi.scaled = scaled_width == 0 && scaled_height == 0
? 96.
: px_diag_scaled / mon->inch * mon->scale;
if (mon->dpi.physical > 1000) {
if (mon->name != NULL) {
LOG_WARN("%s: DPI=%f (physical) is unreasonable, using 96 instead",
mon->name, mon->dpi.physical);
}
mon->dpi.physical = 96;
}
if (mon->dpi.scaled > 1000) {
if (mon->name != NULL) {
LOG_WARN("%s: DPI=%f (logical) is unreasonable, using 96 instead",
mon->name, mon->dpi.scaled);
}
mon->dpi.scaled = 96;
}
}
static void
output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y,
int32_t physical_width, int32_t physical_height,
int32_t subpixel, const char *make, const char *model,
int32_t transform)
{
struct monitor *mon = data;
free(mon->make);
free(mon->model);
mon->dim.mm.width = physical_width;
mon->dim.mm.height = physical_height;
mon->inch = sqrt(pow(mon->dim.mm.width, 2) + pow(mon->dim.mm.height, 2)) * 0.03937008;
mon->make = make != NULL ? xstrdup(make) : NULL;
mon->model = model != NULL ? xstrdup(model) : NULL;
mon->subpixel = subpixel;
mon->transform = transform;
output_update_ppi(mon);
}
static void
output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
int32_t width, int32_t height, int32_t refresh)
{
if ((flags & WL_OUTPUT_MODE_CURRENT) == 0)
return;
struct monitor *mon = data;
mon->refresh = (float)refresh / 1000;
mon->dim.px_real.width = width;
mon->dim.px_real.height = height;
output_update_ppi(mon);
}
static void
output_done(void *data, struct wl_output *wl_output)
{
struct monitor *mon = data;
update_terms_on_monitor(mon);
}
static void
output_scale(void *data, struct wl_output *wl_output, int32_t factor)
{
struct monitor *mon = data;
mon->scale = factor;
output_update_ppi(mon);
}
#if defined(WL_OUTPUT_NAME_SINCE_VERSION)
static void
output_name(void *data, struct wl_output *wl_output, const char *name)
{
struct monitor *mon = data;
free(mon->name);
mon->name = name != NULL ? xstrdup(name) : NULL;
}
#endif
#if defined(WL_OUTPUT_DESCRIPTION_SINCE_VERSION)
static void
output_description(void *data, struct wl_output *wl_output,
const char *description)
{
struct monitor *mon = data;
free(mon->description);
mon->description = description != NULL ? xstrdup(description) : NULL;
}
#endif
static const struct wl_output_listener output_listener = {
.geometry = &output_geometry,
.mode = &output_mode,
.done = &output_done,
.scale = &output_scale,
#if defined(WL_OUTPUT_NAME_SINCE_VERSION)
.name = &output_name,
#endif
#if defined(WL_OUTPUT_DESCRIPTION_SINCE_VERSION)
.description = &output_description,
#endif
};
static void
xdg_output_handle_logical_position(
void *data, struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y)
{
struct monitor *mon = data;
mon->x = x;
mon->y = y;
}
static void
xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output,
int32_t width, int32_t height)
{
struct monitor *mon = data;
mon->dim.px_scaled.width = width;
mon->dim.px_scaled.height = height;
output_update_ppi(mon);
}
static void
xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)
{
struct monitor *mon = data;
update_terms_on_monitor(mon);
}
static void
xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output,
const char *name)
{
struct monitor *mon = data;
free(mon->name);
mon->name = name != NULL ? xstrdup(name) : NULL;
}
static void
xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,
const char *description)
{
struct monitor *mon = data;
free(mon->description);
mon->description = description != NULL ? xstrdup(description) : NULL;
}
static const struct zxdg_output_v1_listener xdg_output_listener = {
.logical_position = xdg_output_handle_logical_position,
.logical_size = xdg_output_handle_logical_size,
.done = xdg_output_handle_done,
.name = xdg_output_handle_name,
.description = xdg_output_handle_description,
};
static void
clock_id(void *data, struct wp_presentation *wp_presentation, uint32_t clk_id)
{
struct wayland *wayl = data;
wayl->presentation_clock_id = clk_id;
LOG_DBG("presentation clock ID: %u", clk_id);
}
static const struct wp_presentation_listener presentation_listener = {
.clock_id = &clock_id,
};
static bool
verify_iface_version(const char *iface, uint32_t version, uint32_t wanted)
{
if (version >= wanted)
return true;
LOG_ERR("%s: need interface version %u, but compositor only implements %u",
iface, wanted, version);
return false;
}
static void
surface_enter(void *data, struct wl_surface *wl_surface,
struct wl_output *wl_output)
{
struct wl_window *win = data;
struct terminal *term = win->term;
tll_foreach(term->wl->monitors, it) {
if (it->item.output == wl_output) {
LOG_DBG("mapped on %s", it->item.name);
tll_push_back(term->window->on_outputs, &it->item);
update_term_for_output_change(term);
return;
}
}
LOG_ERR("mapped on unknown output");
}
static void
surface_leave(void *data, struct wl_surface *wl_surface,
struct wl_output *wl_output)
{
struct wl_window *win = data;
struct terminal *term = win->term;
tll_foreach(term->window->on_outputs, it) {
if (it->item->output != wl_output)
continue;
LOG_DBG("unmapped from %s", it->item->name);
tll_remove(term->window->on_outputs, it);
update_term_for_output_change(term);
return;
}
LOG_WARN("unmapped from unknown output");
}
#if defined(WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION)
static void
surface_preferred_buffer_scale(void *data, struct wl_surface *surface,
int32_t scale)
{
struct wl_window *win = data;
if (win->preferred_buffer_scale == scale)
return;
LOG_DBG("wl_surface preferred scale: %d -> %d", win->preferred_buffer_scale, scale);
win->preferred_buffer_scale = scale;
update_term_for_output_change(win->term);
}
static void
surface_preferred_buffer_transform(void *data, struct wl_surface *surface,
uint32_t transform)
{
}
#endif
static const struct wl_surface_listener surface_listener = {
.enter = &surface_enter,
.leave = &surface_leave,
#if defined(WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION)
.preferred_buffer_scale = &surface_preferred_buffer_scale,
.preferred_buffer_transform = &surface_preferred_buffer_transform,
#endif
};
static void
xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
int32_t width, int32_t height, struct wl_array *states)
{
bool is_activated = false;
bool is_fullscreen = false;
bool is_maximized = false;
bool is_resizing = false;
bool is_tiled_top = false;
bool is_tiled_bottom = false;
bool is_tiled_left = false;
bool is_tiled_right = false;
bool is_suspended UNUSED = false;
#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
char state_str[2048];
int state_chars = 0;
static const char *const strings[] = {
[XDG_TOPLEVEL_STATE_MAXIMIZED] = "maximized",
[XDG_TOPLEVEL_STATE_FULLSCREEN] = "fullscreen",
[XDG_TOPLEVEL_STATE_RESIZING] = "resizing",
[XDG_TOPLEVEL_STATE_ACTIVATED] = "activated",
[XDG_TOPLEVEL_STATE_TILED_LEFT] = "tiled:left",
[XDG_TOPLEVEL_STATE_TILED_RIGHT] = "tiled:right",
[XDG_TOPLEVEL_STATE_TILED_TOP] = "tiled:top",
[XDG_TOPLEVEL_STATE_TILED_BOTTOM] = "tiled:bottom",
#if defined(XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION) /* wayland-protocols >= 1.32 */
[XDG_TOPLEVEL_STATE_SUSPENDED] = "suspended",
#endif
};
#endif
enum xdg_toplevel_state *state;
wl_array_for_each(state, states) {
switch (*state) {
case XDG_TOPLEVEL_STATE_MAXIMIZED: is_maximized = true; break;
case XDG_TOPLEVEL_STATE_FULLSCREEN: is_fullscreen = true; break;
case XDG_TOPLEVEL_STATE_RESIZING: is_resizing = true; break;
case XDG_TOPLEVEL_STATE_ACTIVATED: is_activated = true; break;
case XDG_TOPLEVEL_STATE_TILED_LEFT: is_tiled_left = true; break;
case XDG_TOPLEVEL_STATE_TILED_RIGHT: is_tiled_right = true; break;
case XDG_TOPLEVEL_STATE_TILED_TOP: is_tiled_top = true; break;
case XDG_TOPLEVEL_STATE_TILED_BOTTOM: is_tiled_bottom = true; break;
#if defined(XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION)
case XDG_TOPLEVEL_STATE_SUSPENDED: is_suspended = true; break;
#endif
}
#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
if (*state >= 0 && *state < ALEN(strings)) {
state_chars += snprintf(
&state_str[state_chars], sizeof(state_str) - state_chars,
"%s, ",
strings[*state] != NULL ? strings[*state] : "<unknown>");
}
#endif
}
#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
if (state_chars > 2)
state_str[state_chars - 2] = '\0';
else
state_str[0] = '\0';
LOG_DBG("xdg-toplevel: configure: size=%dx%d, states=[%s]",
width, height, state_str);
#endif
/*
* Changes done here are ignored until the configure event has
* been ack:ed in xdg_surface_configure().
*
* So, just store the config data and apply it later, in
* xdg_surface_configure() after we've ack:ed the event.
*/
struct wl_window *win = data;
win->configure.is_activated = is_activated;
win->configure.is_fullscreen = is_fullscreen;
win->configure.is_maximized = is_maximized;
win->configure.is_resizing = is_resizing;
win->configure.is_tiled_top = is_tiled_top;
win->configure.is_tiled_bottom = is_tiled_bottom;
win->configure.is_tiled_left = is_tiled_left;
win->configure.is_tiled_right = is_tiled_right;
win->configure.width = width;
win->configure.height = height;
}
static void
xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
{
struct wl_window *win = data;
struct terminal *term = win->term;
LOG_DBG("xdg-toplevel: close");
term_shutdown(term);
}
#if defined(XDG_TOPLEVEL_CONFIGURE_BOUNDS_SINCE_VERSION)
static void
xdg_toplevel_configure_bounds(void *data,
struct xdg_toplevel *xdg_toplevel,
int32_t width, int32_t height)
{
/* TODO: ensure we don't pick a bigger size */
}
#endif
#if defined(XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION)
static void
xdg_toplevel_wm_capabilities(void *data,
struct xdg_toplevel *xdg_toplevel,
struct wl_array *caps)
{
struct wl_window *win = data;
win->wm_capabilities.maximize = false;
win->wm_capabilities.minimize = false;
#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
char cap_str[2048];
int cap_chars = 0;
static const char *const strings[] = {
[XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU] = "window-menu",
[XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE] = "maximize",
[XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN] = "fullscreen",
[XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE] = "minimize",
};
#endif
enum xdg_toplevel_wm_capabilities *cap;
wl_array_for_each(cap, caps) {
switch (*cap) {
case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE:
win->wm_capabilities.maximize = true;
break;
case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE:
win->wm_capabilities.minimize = true;
break;
case XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU:
case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN:
break;
}
#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
if (*cap >= 0 && *cap < ALEN(strings)) {
cap_chars += snprintf(
&cap_str[cap_chars], sizeof(cap_str) - cap_chars,
"%s, ",
strings[*cap] != NULL ? strings[*cap] : "<unknown>");
}
#endif
}
#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
if (cap_chars > 2)
cap_str[cap_chars - 2] = '\0';
else
cap_str[0] = '\0';
LOG_DBG("xdg-toplevel: wm-capabilities=[%s]", cap_str);
#endif
}
#endif
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = &xdg_toplevel_configure,
/*.close = */&xdg_toplevel_close, /* epoll-shim defines a macro 'close'... */
#if defined(XDG_TOPLEVEL_CONFIGURE_BOUNDS_SINCE_VERSION)
.configure_bounds = &xdg_toplevel_configure_bounds,
#endif
#if defined(XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION)
.wm_capabilities = xdg_toplevel_wm_capabilities,
#endif
};
static void
xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
uint32_t serial)
{
LOG_DBG("xdg-surface: configure");
struct wl_window *win = data;
struct terminal *term = win->term;
if (win->unmapped) {
/*
* https://codeberg.org/dnkl/foot/issues/1249
* https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/3487
* https://gitlab.freedesktop.org/wlroots/wlroots/-/merge_requests/3719
* https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/108
*/
return;
}
bool wasnt_configured = !win->is_configured;
bool was_resizing = win->is_resizing;
bool csd_was_enabled = win->csd_mode == CSD_YES && !win->is_fullscreen;
int new_width = win->configure.width;
int new_height = win->configure.height;
win->is_configured = true;
win->is_maximized = win->configure.is_maximized;
win->is_fullscreen = win->configure.is_fullscreen;
win->is_resizing = win->configure.is_resizing;
win->is_tiled_top = win->configure.is_tiled_top;
win->is_tiled_bottom = win->configure.is_tiled_bottom;
win->is_tiled_left = win->configure.is_tiled_left;
win->is_tiled_right = win->configure.is_tiled_right;
win->is_tiled = (win->is_tiled_top ||
win->is_tiled_bottom ||
win->is_tiled_left ||
win->is_tiled_right);
win->csd_mode = win->configure.csd_mode;
bool enable_csd = win->csd_mode == CSD_YES && !win->is_fullscreen;
if (!csd_was_enabled && enable_csd)
csd_instantiate(win);
else if (csd_was_enabled && !enable_csd)
csd_destroy(win);
if (enable_csd && new_width > 0 && new_height > 0) {
if (wayl_win_csd_titlebar_visible(win))
new_height -= win->term->conf->csd.title_height;
if (wayl_win_csd_borders_visible(win)) {
new_height -= 2 * win->term->conf->csd.border_width_visible;
new_width -= 2 * win->term->conf->csd.border_width_visible;
}
}
xdg_surface_ack_configure(xdg_surface, serial);
enum resize_options opts = RESIZE_BY_CELLS;
#if 1
/*
* TODO: decide if we should do the last "forced" call when ending
* an interactive resize.
*
* Without it, the last TIOCSWINSZ sent to the client will be a
* scheduled one. I.e. there will be a small delay after the user
* has *stopped* resizing, and the client application receives the
* final size.