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
|
//- input helpers
internal void wayland_button_update(PlatformButton *button, b32 down)
{
if(button->ended_down != down) {
button->ended_down = down;
++button->half_transition_count;
}
}
internal u32 wayland_current_mods(WaylandState *state)
{
u32 mods = 0;
if(!state->xkb_state) {
return 0;
}
if(xkb_state_mod_name_is_active(state->xkb_state, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_EFFECTIVE) > 0) mods |= PLATFORM_MOD_SHIFT;
if(xkb_state_mod_name_is_active(state->xkb_state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE) > 0) mods |= PLATFORM_MOD_CTRL;
if(xkb_state_mod_name_is_active(state->xkb_state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_EFFECTIVE) > 0) mods |= PLATFORM_MOD_ALT;
return mods;
}
// NOTE: a key press (or synthesized repeat): one key event plus whatever text it types
internal void wayland_emit_key(WaylandState *state, u32 evdev_key, b32 repeat)
{
if(!state->xkb_state) {
return;
}
PlatformInput *input = &state->input;
u32 keycode = evdev_key + 8; // xkb keycodes are evdev + 8, historical x11 offset
u32 mods = wayland_current_mods(state);
if(input->key_count < PLATFORM_MAX_KEY_EVENTS) {
PlatformKeyEvent *event = &input->keys[input->key_count++];
event->keysym = xkb_state_key_get_one_sym(state->xkb_state, keycode);
event->mods = mods;
event->repeat = repeat;
}
if(!(mods & (PLATFORM_MOD_CTRL | PLATFORM_MOD_ALT))) {
char text[16];
int length = xkb_state_key_get_utf8(state->xkb_state, keycode, text, sizeof(text));
if(length > 0 && (u8)text[0] >= 0x20 && text[0] != 0x7f && input->text_size + (u32)length <= sizeof(input->text)) {
MemCpy(input->text + input->text_size, text, (u64)length);
input->text_size += (u32)length;
}
}
}
//- wl_keyboard
internal void wayland_keyboard_keymap(void *data, struct wl_keyboard *keyboard, u32 format, s32 fd, u32 size)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
if(format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
char *text = (char *)mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if(text == MAP_FAILED) {
return;
}
struct xkb_keymap *keymap = xkb_keymap_new_from_string(state->xkb_context, text, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(text, size);
if(!keymap) {
Log("failed to compile the xkb keymap");
return;
}
if(state->xkb_state) xkb_state_unref(state->xkb_state);
if(state->xkb_keymap) xkb_keymap_unref(state->xkb_keymap);
state->xkb_keymap = keymap;
state->xkb_state = xkb_state_new(keymap);
}
internal void wayland_keyboard_enter(void *data, struct wl_keyboard *keyboard, u32 serial, struct wl_surface *surface, struct wl_array *keys)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->input.focused = true;
}
// NOTE: after a leave we get no releases for keys still held, so stop repeating ourselves
internal void wayland_keyboard_leave(void *data, struct wl_keyboard *keyboard, u32 serial, struct wl_surface *surface)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->input.focused = false;
state->repeat_key = 0;
}
internal void wayland_keyboard_key(void *data, struct wl_keyboard *keyboard, u32 serial, u32 time, u32 key, u32 key_state)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->input_serial = serial;
b32 pressed = (key_state == WL_KEYBOARD_KEY_STATE_PRESSED);
if(pressed) {
wayland_emit_key(state, key, false);
if(state->repeat_rate > 0 && state->xkb_keymap && xkb_keymap_key_repeats(state->xkb_keymap, key + 8)) {
state->repeat_key = key;
state->repeat_next_ns = time_now_ns() + (u64)state->repeat_delay_ms * 1000000ull;
}
} else if(state->repeat_key == key) {
state->repeat_key = 0;
}
}
internal void wayland_keyboard_modifiers(void *data, struct wl_keyboard *keyboard, u32 serial, u32 depressed, u32 latched, u32 locked, u32 group)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
if(state->xkb_state) {
xkb_state_update_mask(state->xkb_state, depressed, latched, locked, 0, 0, group);
}
state->input.mods = wayland_current_mods(state);
}
internal void wayland_keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, s32 rate, s32 delay)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->repeat_rate = rate;
state->repeat_delay_ms = delay;
}
global_variable const struct wl_keyboard_listener wayland_keyboard_listener = {
wayland_keyboard_keymap,
wayland_keyboard_enter,
wayland_keyboard_leave,
wayland_keyboard_key,
wayland_keyboard_modifiers,
wayland_keyboard_repeat_info,
};
//- wl_pointer
internal void wayland_pointer_enter(void *data, struct wl_pointer *pointer, u32 serial, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->input.mouse_in_window = true;
state->input.mouse_x = (f32)wl_fixed_to_double(x);
state->input.mouse_y = (f32)wl_fixed_to_double(y);
state->pointer_serial = serial;
state->cursor = UINT32_MAX; // force the next set_cursor to apply
if(state->cursor_shape_device) {
wp_cursor_shape_device_v1_set_shape(state->cursor_shape_device, serial, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
state->cursor = PLATFORM_CURSOR_DEFAULT;
}
}
internal void wayland_pointer_leave(void *data, struct wl_pointer *pointer, u32 serial, struct wl_surface *surface)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->input.mouse_in_window = false;
for(u32 index = 0; index < ArrayCount(state->input.mouse); ++index) {
wayland_button_update(&state->input.mouse[index], false);
}
}
internal void wayland_pointer_motion(void *data, struct wl_pointer *pointer, u32 time, wl_fixed_t x, wl_fixed_t y)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->input.mouse_x = (f32)wl_fixed_to_double(x);
state->input.mouse_y = (f32)wl_fixed_to_double(y);
}
internal void wayland_pointer_button(void *data, struct wl_pointer *pointer, u32 serial, u32 time, u32 button, u32 button_state)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->last_button_serial = serial;
state->input_serial = serial;
// BTN_LEFT, BTN_RIGHT, BTN_MIDDLE are consecutive
if(button >= BTN_LEFT && button <= BTN_MIDDLE) {
wayland_button_update(&state->input.mouse[button - BTN_LEFT], button_state == WL_POINTER_BUTTON_STATE_PRESSED);
}
}
internal void wayland_pointer_axis(void *data, struct wl_pointer *pointer, u32 time, u32 axis, wl_fixed_t value)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
if(axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
// NOTE: a wheel notch is 10 units on wlroots/libinput, touchpads send smaller deltas
state->input.scroll_y += (f32)wl_fixed_to_double(value) / 10.0f;
} else if(axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
state->input.scroll_x += (f32)wl_fixed_to_double(value) / 10.0f;
}
}
internal void wayland_pointer_frame(void *data, struct wl_pointer *pointer) {}
internal void wayland_pointer_axis_source(void *data, struct wl_pointer *pointer, u32 source) {}
internal void wayland_pointer_axis_stop(void *data, struct wl_pointer *pointer, u32 time, u32 axis) {}
internal void wayland_pointer_axis_discrete(void *data, struct wl_pointer *pointer, u32 axis, s32 discrete) {}
global_variable const struct wl_pointer_listener wayland_pointer_listener = {
wayland_pointer_enter,
wayland_pointer_leave,
wayland_pointer_motion,
wayland_pointer_button,
wayland_pointer_axis,
wayland_pointer_frame,
wayland_pointer_axis_source,
wayland_pointer_axis_stop,
wayland_pointer_axis_discrete,
};
//- wl_seat
internal void wayland_seat_capabilities(void *data, struct wl_seat *seat, u32 capabilities)
{
WaylandState *state = (WaylandState *)data;
if((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && !state->keyboard) {
state->keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(state->keyboard, &wayland_keyboard_listener, state);
}
if((capabilities & WL_SEAT_CAPABILITY_POINTER) && !state->pointer) {
state->pointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(state->pointer, &wayland_pointer_listener, state);
if(state->cursor_shape_manager) {
state->cursor_shape_device = wp_cursor_shape_manager_v1_get_pointer(state->cursor_shape_manager, state->pointer);
}
}
}
internal void wayland_seat_name(void *data, struct wl_seat *seat, const char *name) {}
global_variable const struct wl_seat_listener wayland_seat_listener = {
wayland_seat_capabilities,
wayland_seat_name,
};
//- wl_surface
// NOTE: compositor v6 tells us the scale it wants directly, no wl_output bookkeeping
internal void wayland_surface_enter(void *data, struct wl_surface *surface, struct wl_output *output) {}
internal void wayland_surface_leave(void *data, struct wl_surface *surface, struct wl_output *output) {}
internal void wayland_surface_preferred_buffer_scale(void *data, struct wl_surface *surface, s32 factor)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
if(factor > 0 && (u32)factor != state->scale) {
state->scale = (u32)factor;
state->resized = true;
wl_surface_set_buffer_scale(state->surface, factor);
}
}
internal void wayland_surface_preferred_buffer_transform(void *data, struct wl_surface *surface, u32 transform) {}
global_variable const struct wl_surface_listener wayland_surface_listener = {
wayland_surface_enter,
wayland_surface_leave,
wayland_surface_preferred_buffer_scale,
wayland_surface_preferred_buffer_transform,
};
//- xdg
internal void wayland_xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, u32 serial)
{
xdg_wm_base_pong(xdg_wm_base, serial);
}
global_variable const struct xdg_wm_base_listener wayland_xdg_wm_base_listener = {
wayland_xdg_wm_base_ping,
};
internal void wayland_xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, u32 serial)
{
WaylandState *state = (WaylandState *)data;
xdg_surface_ack_configure(xdg_surface, serial);
state->configured = true;
}
global_variable const struct xdg_surface_listener wayland_xdg_surface_listener = {
wayland_xdg_surface_configure,
};
internal void wayland_xdg_toplevel_configure(void *data, struct xdg_toplevel *toplevel, s32 width, s32 height, struct wl_array *states)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
state->maximized = false;
state->suspended = false;
for(u32 *it = (u32 *)states->data; (u8 *)it < (u8 *)states->data + states->size; ++it) {
if(*it == XDG_TOPLEVEL_STATE_MAXIMIZED) state->maximized = true;
if(*it == XDG_TOPLEVEL_STATE_SUSPENDED) state->suspended = true;
}
if(width > 0 && height > 0 && ((u32)width != state->width || (u32)height != state->height)) {
state->width = (u32)width;
state->height = (u32)height;
state->resized = true;
}
}
internal void wayland_xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel)
{
WaylandState *state = (WaylandState *)data;
state->should_close = true;
}
internal void wayland_xdg_toplevel_configure_bounds(void *data, struct xdg_toplevel *toplevel, s32 width, s32 height) {}
internal void wayland_xdg_toplevel_wm_capabilities(void *data, struct xdg_toplevel *toplevel, struct wl_array *capabilities) {}
global_variable const struct xdg_toplevel_listener wayland_xdg_toplevel_listener = {
wayland_xdg_toplevel_configure,
wayland_xdg_toplevel_close,
wayland_xdg_toplevel_configure_bounds,
wayland_xdg_toplevel_wm_capabilities,
};
//- layer shell
internal void wayland_layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *layer_surface, u32 serial, u32 width, u32 height)
{
WaylandState *state = (WaylandState *)data;
state->input.changed = true;
zwlr_layer_surface_v1_ack_configure(layer_surface, serial);
if(width > 0 && height > 0 && (width != state->width || height != state->height)) {
state->width = width;
state->height = height;
state->resized = true;
}
state->configured = true;
}
internal void wayland_layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *layer_surface)
{
WaylandState *state = (WaylandState *)data;
state->should_close = true;
}
global_variable const struct zwlr_layer_surface_v1_listener wayland_layer_surface_listener = {
wayland_layer_surface_configure,
wayland_layer_surface_closed,
};
//- drag and drop
// NOTE: file drops: we accept text/uri-list, read it through a pipe on drop and turn the file://
// uris into paths for PlatformInput.drop_paths. the clipboard is the same machinery: the
// compositor announces the selection's offer, we read text from it on paste and serve our own
// wl_data_source on copy.
#define WAYLAND_URI_LIST_MIME "text/uri-list"
internal void wayland_data_offer_offer(void *data, struct wl_data_offer *offer, const char *mime_type)
{
WaylandState *state = (WaylandState *)data;
if(offer == state->pending_offer && CstringIsMatch(mime_type, WAYLAND_URI_LIST_MIME)) {
state->pending_offer_uri = true;
}
if(offer == state->pending_offer && (CstringIsMatch(mime_type, "text/plain;charset=utf-8") || CstringIsMatch(mime_type, "UTF8_STRING") ||
CstringIsMatch(mime_type, "text/plain") || CstringIsMatch(mime_type, "TEXT"))) {
// NOTE: the first text type wins, the utf-8 one when it comes first (it usually does)
if(!state->pending_offer_text || CstringIsMatch(mime_type, "text/plain;charset=utf-8")) {
snprintf(state->selection_mime, sizeof(state->selection_mime), "%s", mime_type);
}
state->pending_offer_text = true;
}
}
internal void wayland_data_offer_source_actions(void *data, struct wl_data_offer *offer, u32 source_actions) {}
internal void wayland_data_offer_action(void *data, struct wl_data_offer *offer, u32 dnd_action) {}
global_variable const struct wl_data_offer_listener wayland_data_offer_listener = {
wayland_data_offer_offer,
wayland_data_offer_source_actions,
wayland_data_offer_action,
};
internal void wayland_data_device_data_offer(void *data, struct wl_data_device *device, struct wl_data_offer *offer)
{
WaylandState *state = (WaylandState *)data;
state->pending_offer = offer;
state->pending_offer_uri = false;
state->pending_offer_text = false;
wl_data_offer_add_listener(offer, &wayland_data_offer_listener, state);
}
internal void wayland_drag_offer_release(WaylandState *state)
{
if(state->drag_offer) wl_data_offer_destroy(state->drag_offer);
if(state->pending_offer == state->drag_offer) state->pending_offer = NULL;
state->drag_offer = NULL;
state->drag_offer_uri = false;
}
internal void wayland_data_device_enter(void *data, struct wl_data_device *device, u32 serial, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *offer)
{
WaylandState *state = (WaylandState *)data;
wayland_drag_offer_release(state);
state->drag_offer = offer;
state->drag_offer_uri = offer && offer == state->pending_offer && state->pending_offer_uri;
state->drag_x = (f32)wl_fixed_to_double(x);
state->drag_y = (f32)wl_fixed_to_double(y);
if(!offer) return;
wl_data_offer_accept(offer, serial, state->drag_offer_uri ? WAYLAND_URI_LIST_MIME : NULL);
if(wl_data_offer_get_version(offer) >= 3) {
u32 action = state->drag_offer_uri ? WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY : WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
wl_data_offer_set_actions(offer, action, action);
}
}
internal void wayland_data_device_leave(void *data, struct wl_data_device *device)
{
wayland_drag_offer_release((WaylandState *)data);
}
internal void wayland_data_device_motion(void *data, struct wl_data_device *device, u32 time, wl_fixed_t x, wl_fixed_t y)
{
WaylandState *state = (WaylandState *)data;
state->drag_x = (f32)wl_fixed_to_double(x);
state->drag_y = (f32)wl_fixed_to_double(y);
}
internal void wayland_data_device_drop(void *data, struct wl_data_device *device)
{
WaylandState *state = (WaylandState *)data;
if(!state->drag_offer || !state->drag_offer_uri) {
wayland_drag_offer_release(state);
return;
}
int fds[2];
if(pipe2(fds, O_CLOEXEC) == 0) {
wl_data_offer_receive(state->drag_offer, WAYLAND_URI_LIST_MIME, fds[1]);
close(fds[1]);
wl_display_flush(state->display);
// NOTE: blocking read, the dragging client writes the list and closes its end
char text[PLATFORM_MAX_DROPS * PLATFORM_DROP_PATH_SIZE];
u64 size = 0;
for(;;) {
ssize_t got = read(fds[0], text + size, sizeof(text) - 1 - size);
if(got > 0) { size += (u64)got; if(size >= sizeof(text) - 1) break; continue; }
if(got < 0 && errno == EINTR) continue;
break;
}
close(fds[0]);
text[size] = 0;
linux_parse_uri_list(&state->input, text, size);
state->input.drop_x = state->drag_x;
state->input.drop_y = state->drag_y;
state->input.changed = true;
}
if(wl_data_offer_get_version(state->drag_offer) >= 3) wl_data_offer_finish(state->drag_offer);
wayland_drag_offer_release(state);
}
internal void wayland_data_device_selection(void *data, struct wl_data_device *device, struct wl_data_offer *offer)
{
WaylandState *state = (WaylandState *)data;
// NOTE: the old selection offer is dead once a new one (or none) is announced
if(state->selection_offer && state->selection_offer != offer && state->selection_offer != state->drag_offer) {
wl_data_offer_destroy(state->selection_offer);
}
state->selection_offer = offer;
state->selection_offer_text = offer && offer == state->pending_offer && state->pending_offer_text;
if(offer && state->pending_offer == offer) state->pending_offer = NULL;
}
//- clipboard: our data source
internal void wayland_source_target(void *data, struct wl_data_source *source, const char *mime_type) {}
internal void wayland_source_send(void *data, struct wl_data_source *source, const char *mime_type, s32 fd)
{
WaylandState *state = (WaylandState *)data;
if(source == state->clip_source && state->clip_data) {
u64 sent = 0;
while(sent < state->clip_size) {
ssize_t n = write(fd, state->clip_data + sent, state->clip_size - sent);
if(n > 0) { sent += (u64)n; continue; }
if(n < 0 && errno == EINTR) continue;
break;
}
}
close(fd);
}
internal void wayland_source_cancelled(void *data, struct wl_data_source *source)
{
WaylandState *state = (WaylandState *)data;
if(source == state->clip_source) state->clip_source = NULL;
wl_data_source_destroy(source);
}
internal void wayland_source_dnd_drop_performed(void *data, struct wl_data_source *source) {}
internal void wayland_source_dnd_finished(void *data, struct wl_data_source *source) {}
internal void wayland_source_action(void *data, struct wl_data_source *source, u32 action) {}
global_variable const struct wl_data_source_listener wayland_source_listener = {
wayland_source_target,
wayland_source_send,
wayland_source_cancelled,
wayland_source_dnd_drop_performed,
wayland_source_dnd_finished,
wayland_source_action,
};
// NOTE: runs inside the pump (host code), so the listener above never lives in a hot reloaded .so
internal void wayland_apply_clipboard(WaylandState *state)
{
if(!state->clip_pending) return;
state->clip_pending = false;
if(!state->data_device_manager || !state->data_device) return;
struct wl_data_source *source = wl_data_device_manager_create_data_source(state->data_device_manager);
if(!source) return;
wl_data_source_add_listener(source, &wayland_source_listener, state);
wl_data_source_offer(source, "text/plain;charset=utf-8");
wl_data_source_offer(source, "text/plain");
wl_data_source_offer(source, "UTF8_STRING");
wl_data_source_offer(source, "TEXT");
wl_data_source_offer(source, "STRING");
wl_data_device_set_selection(state->data_device, source, state->input_serial);
// NOTE: the old source gets a cancelled event, which destroys it
state->clip_source = source;
}
global_variable const struct wl_data_device_listener wayland_data_device_listener = {
wayland_data_device_data_offer,
wayland_data_device_enter,
wayland_data_device_leave,
wayland_data_device_motion,
wayland_data_device_drop,
wayland_data_device_selection,
};
//- registry
internal void wayland_registry_global(void *data, struct wl_registry *registry, u32 name, const char *interface, u32 version)
{
WaylandState *state = (WaylandState *)data;
if(CstringIsMatch(interface, wl_compositor_interface.name)) {
state->compositor_version = Min(version, 6u);
state->compositor = (struct wl_compositor *)wl_registry_bind(registry, name, &wl_compositor_interface, state->compositor_version);
} else if(CstringIsMatch(interface, xdg_wm_base_interface.name)) {
// NOTE: v6 for the `suspended` toplevel state, our toplevel listener covers every event up to v6
state->xdg_wm_base = (struct xdg_wm_base *)wl_registry_bind(registry, name, &xdg_wm_base_interface, Min(version, 6u));
xdg_wm_base_add_listener(state->xdg_wm_base, &wayland_xdg_wm_base_listener, state);
} else if(CstringIsMatch(interface, zwlr_layer_shell_v1_interface.name)) {
state->layer_shell_version = Min(version, 4u);
state->layer_shell = (struct zwlr_layer_shell_v1 *)wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, state->layer_shell_version);
} else if(CstringIsMatch(interface, wl_seat_interface.name) && !state->seat) {
state->seat = (struct wl_seat *)wl_registry_bind(registry, name, &wl_seat_interface, Min(version, 5u));
wl_seat_add_listener(state->seat, &wayland_seat_listener, state);
} else if(CstringIsMatch(interface, zxdg_decoration_manager_v1_interface.name)) {
state->decoration_manager = (struct zxdg_decoration_manager_v1 *)wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1);
} else if(CstringIsMatch(interface, wl_data_device_manager_interface.name)) {
state->data_device_manager = (struct wl_data_device_manager *)wl_registry_bind(registry, name, &wl_data_device_manager_interface, Min(version, 3u));
} else if(CstringIsMatch(interface, wp_cursor_shape_manager_v1_interface.name)) {
state->cursor_shape_manager = (struct wp_cursor_shape_manager_v1 *)wl_registry_bind(registry, name, &wp_cursor_shape_manager_v1_interface, 1);
}
}
internal void wayland_registry_global_remove(void *data, struct wl_registry *registry, u32 name) {}
global_variable const struct wl_registry_listener wayland_registry_listener = {
wayland_registry_global,
wayland_registry_global_remove,
};
//- public api
internal void wayland_update_opaque_region(WaylandState *state);
internal b32 wayland_window_open(PlatformWindow *window, WindowParams params)
{
WaylandState *state = &window->wayland;
state->width = params.width ? params.width : 960;
state->height = params.height ? params.height : 600;
state->scale = 1;
state->repeat_rate = 25;
state->repeat_delay_ms = 600;
state->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
state->display = wl_display_connect(NULL);
if(!state->display) {
Log("no wayland display");
if(state->xkb_context) xkb_context_unref(state->xkb_context);
state->xkb_context = NULL;
return false;
}
// NOTE: first roundtrip binds the globals, the second delivers the seat capabilities
state->registry = wl_display_get_registry(state->display);
wl_registry_add_listener(state->registry, &wayland_registry_listener, state);
wl_display_roundtrip(state->display);
wl_display_roundtrip(state->display);
if(!state->compositor) {
Log("compositor has no wl_compositor");
return false;
}
if(state->data_device_manager && state->seat) {
state->data_device = wl_data_device_manager_get_data_device(state->data_device_manager, state->seat);
wl_data_device_add_listener(state->data_device, &wayland_data_device_listener, state);
}
state->surface = wl_compositor_create_surface(state->compositor);
if(state->compositor_version >= 6) {
wl_surface_add_listener(state->surface, &wayland_surface_listener, state);
}
state->is_layer = params.layer && state->layer_shell;
if(params.layer && !state->layer_shell) {
Log("compositor has no wlr layer shell, falling back to a normal window");
}
if(state->is_layer) {
state->layer_surface = zwlr_layer_shell_v1_get_layer_surface(state->layer_shell, state->surface, NULL,
ZWLR_LAYER_SHELL_V1_LAYER_TOP, params.name);
zwlr_layer_surface_v1_add_listener(state->layer_surface, &wayland_layer_surface_listener, state);
zwlr_layer_surface_v1_set_size(state->layer_surface, state->width, state->height);
zwlr_layer_surface_v1_set_anchor(state->layer_surface, params.anchor); // PlatformAnchor matches the protocol bits
zwlr_layer_surface_v1_set_margin(state->layer_surface, params.margin, params.margin, params.margin, params.margin);
zwlr_layer_surface_v1_set_exclusive_zone(state->layer_surface, 0);
// NOTE: on_demand (v4) = focus when clicked, like a normal window. v3 only has exclusive.
zwlr_layer_surface_v1_set_keyboard_interactivity(state->layer_surface, state->layer_shell_version >= 4 ? 2u : 1u);
} else {
if(!state->xdg_wm_base) {
Log("compositor has no xdg_wm_base");
return false;
}
state->xdg_surface = xdg_wm_base_get_xdg_surface(state->xdg_wm_base, state->surface);
xdg_surface_add_listener(state->xdg_surface, &wayland_xdg_surface_listener, state);
state->xdg_toplevel = xdg_surface_get_toplevel(state->xdg_surface);
xdg_toplevel_add_listener(state->xdg_toplevel, &wayland_xdg_toplevel_listener, state);
xdg_toplevel_set_title(state->xdg_toplevel, params.title ? params.title : params.name);
xdg_toplevel_set_app_id(state->xdg_toplevel, params.name);
if(params.min_width && params.min_height) {
xdg_toplevel_set_min_size(state->xdg_toplevel, (s32)params.min_width, (s32)params.min_height);
}
// NOTE: we draw no title bar. asking for server side decorations tells tiling compositors
// (niri, sway) this is a regular window they should frame, focus-ring and round themselves.
if(state->decoration_manager) {
state->decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(state->decoration_manager, state->xdg_toplevel);
zxdg_toplevel_decoration_v1_set_mode(state->decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
}
}
// NOTE: commit without a buffer and wait for the first configure, attaching before is a protocol error
wl_surface_commit(state->surface);
while(!state->configured && !state->should_close) {
if(wl_display_dispatch(state->display) == -1) {
Log("wayland connection lost before the first configure");
return false;
}
}
state->resized = false;
wayland_update_opaque_region(state);
return true;
}
internal b32 wayland_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask)
{
if(ready_mask) *ready_mask = 0;
WaylandState *state = &window->wayland;
// NOTE: a held key needs us awake for its next repeat
if(state->repeat_key && state->repeat_rate > 0) {
u64 now = time_now_ns();
s64 repeat_ns = (state->repeat_next_ns > now) ? (s64)(state->repeat_next_ns - now) : 0;
timeout_ns = (timeout_ns < 0) ? repeat_ns : Min(timeout_ns, repeat_ns);
}
wayland_apply_clipboard(state);
while(wl_display_prepare_read(state->display) != 0) {
if(wl_display_dispatch_pending(state->display) == -1) {
return false;
}
}
wl_display_flush(state->display);
struct pollfd fds[8] = {{wl_display_get_fd(state->display), POLLIN, 0}};
u32 which[8] = {}; // fds slot -> extra_fds index, for ready_mask
u32 count = 1;
for(u32 index = 0; index < extra_count && count < ArrayCount(fds); ++index) {
if(extra_fds[index] >= 0) {
which[count] = index;
fds[count++] = {extra_fds[index], POLLIN, 0};
}
}
// NOTE: ppoll for nanosecond timeouts, a 240hz frame is 4.17ms and poll() only does whole ms
struct timespec timeout = {};
struct timespec *timeout_ptr = NULL;
if(timeout_ns >= 0) {
timeout.tv_sec = timeout_ns / 1000000000ll;
timeout.tv_nsec = timeout_ns % 1000000000ll;
timeout_ptr = &timeout;
}
int ready = ppoll(fds, count, timeout_ptr, NULL);
if(ready > 0 && ready_mask) {
for(u32 slot = 1; slot < count; ++slot) {
if(fds[slot].revents) *ready_mask |= 1u << which[slot];
}
}
if(ready > 0 && (fds[0].revents & POLLIN)) {
if(wl_display_read_events(state->display) == -1) {
return false;
}
} else {
wl_display_cancel_read(state->display);
}
if(wl_display_dispatch_pending(state->display) == -1) {
Log("wayland connection lost");
return false;
}
//- synthesize key repeats
if(state->repeat_key && state->repeat_rate > 0) {
u64 now = time_now_ns();
u64 interval = 1000000000ull / (u64)state->repeat_rate;
while(now >= state->repeat_next_ns) {
wayland_emit_key(state, state->repeat_key, true);
state->input.changed = true;
state->repeat_next_ns += interval;
}
}
*input = state->input;
// NOTE: transitions, key events, text and scroll are per frame, held state carries over
for(u32 index = 0; index < ArrayCount(state->input.mouse); ++index) {
state->input.mouse[index].half_transition_count = 0;
}
state->input.key_count = 0;
state->input.text_size = 0;
state->input.scroll_x = 0;
state->input.scroll_y = 0;
state->input.drop_count = 0;
state->input.changed = false;
return !state->should_close;
}
internal void wayland_window_size(PlatformWindow *window, u32 *width, u32 *height, u32 *scale)
{
*width = window->wayland.width;
*height = window->wayland.height;
*scale = window->wayland.scale;
}
// NOTE: a normal window is fully opaque, telling the compositor lets it skip blending whatever
// is behind us. layer surfaces keep their rounded, transparent corners.
internal void wayland_update_opaque_region(WaylandState *state)
{
if(state->is_layer) {
return;
}
struct wl_region *region = wl_compositor_create_region(state->compositor);
wl_region_add(region, 0, 0, (s32)state->width, (s32)state->height);
wl_surface_set_opaque_region(state->surface, region);
wl_region_destroy(region);
}
internal b32 wayland_window_take_resized(PlatformWindow *window)
{
b32 resized = window->wayland.resized;
window->wayland.resized = false;
if(resized) {
wayland_update_opaque_region(&window->wayland);
}
return resized;
}
//- window management
internal void wayland_window_begin_move(PlatformWindow *window)
{
// NOTE: hands the drag to the compositor, it moves the window (or in niri, the tile) until release
WaylandState *state = &window->wayland;
if(state->xdg_toplevel && state->seat) {
xdg_toplevel_move(state->xdg_toplevel, state->seat, state->last_button_serial);
}
}
internal void wayland_window_toggle_maximized(PlatformWindow *window)
{
WaylandState *state = &window->wayland;
if(!state->xdg_toplevel) return;
if(state->maximized) xdg_toplevel_unset_maximized(state->xdg_toplevel);
else xdg_toplevel_set_maximized(state->xdg_toplevel);
}
//- cursor
internal void wayland_window_set_cursor(PlatformWindow *window, PlatformCursor cursor)
{
WaylandState *state = &window->wayland;
if(!state->cursor_shape_device || !state->input.mouse_in_window || state->cursor == (u32)cursor) {
return;
}
u32 shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT;
switch(cursor) {
case PLATFORM_CURSOR_DEFAULT: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT; break;
case PLATFORM_CURSOR_POINTER: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER; break;
case PLATFORM_CURSOR_GRABBING: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING; break;
case PLATFORM_CURSOR_RESIZE_X: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE; break;
case PLATFORM_CURSOR_RESIZE_Y: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE; break;
case PLATFORM_CURSOR_CROSSHAIR: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR; break;
case PLATFORM_CURSOR_MOVE: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE; break;
case PLATFORM_CURSOR_RESIZE_NWSE: shape = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE; break;
}
wp_cursor_shape_device_v1_set_shape(state->cursor_shape_device, state->pointer_serial, shape);
state->cursor = (u32)cursor;
}
//- frame pacing
internal void wayland_frame_done(void *data, struct wl_callback *callback, u32 time)
{
WaylandState *state = (WaylandState *)data;
wl_callback_destroy(callback);
if(state->frame_callback == callback) state->frame_callback = NULL;
state->last_frame_done_ns = time_now_ns();
}
global_variable const struct wl_callback_listener wayland_frame_listener = {
wayland_frame_done,
};
internal void wayland_window_request_frame(PlatformWindow *window)
{
// NOTE: frame requests are double buffered surface state. the vulkan driver's commit inside
// vkQueuePresentKHR picks it up, so this has to be called right before presenting.
WaylandState *state = &window->wayland;
if(state->frame_callback) {
wl_callback_destroy(state->frame_callback);
}
state->frame_callback = wl_surface_frame(state->surface);
state->frame_requested_ns = time_now_ns();
wl_callback_add_listener(state->frame_callback, &wayland_frame_listener, state);
}
internal void wayland_window_cancel_frame(PlatformWindow *window)
{
WaylandState *state = &window->wayland;
if(state->frame_callback) {
wl_callback_destroy(state->frame_callback);
state->frame_callback = NULL;
}
}
internal b32 wayland_window_frame_ready(PlatformWindow *window)
{
return window->wayland.frame_callback == NULL;
}
internal void wayland_window_close(PlatformWindow *window)
{
WaylandState *state = &window->wayland;
wayland_window_cancel_frame(window);
wayland_drag_offer_release(state);
if(state->data_device) {
if(wl_data_device_get_version(state->data_device) >= 2) wl_data_device_release(state->data_device);
else wl_data_device_destroy(state->data_device);
}
if(state->decoration) zxdg_toplevel_decoration_v1_destroy(state->decoration);
if(state->layer_surface) zwlr_layer_surface_v1_destroy(state->layer_surface);
if(state->xdg_toplevel) xdg_toplevel_destroy(state->xdg_toplevel);
if(state->xdg_surface) xdg_surface_destroy(state->xdg_surface);
if(state->surface) wl_surface_destroy(state->surface);
if(state->xkb_state) xkb_state_unref(state->xkb_state);
if(state->xkb_keymap) xkb_keymap_unref(state->xkb_keymap);
if(state->xkb_context) xkb_context_unref(state->xkb_context);
wl_display_disconnect(state->display);
}
//- clipboard api (called from the ui, which may be a hot reloaded .so: no listeners created here)
internal void wayland_clipboard_set(PlatformWindow *window, const char *data, u64 size)
{
WaylandState *state = &window->wayland;
free(state->clip_data);
state->clip_data = (char *)malloc(size + 1);
if(!state->clip_data) { state->clip_size = 0; return; }
MemCpy(state->clip_data, data, size);
state->clip_data[size] = 0;
state->clip_size = size;
state->clip_pending = true;
}
internal String8 wayland_clipboard_get(PlatformWindow *window, Arena *arena)
{
WaylandState *state = &window->wayland;
// NOTE: our own selection: reading it through the compositor would wait on ourselves
if((state->clip_source || state->clip_pending) && state->clip_data) {
u8 *copy = PushArray(arena, u8, state->clip_size + 1);
MemCpy(copy, state->clip_data, state->clip_size);
copy[state->clip_size] = 0;
return ToString8Size(copy, state->clip_size);
}
if(!state->selection_offer || !state->selection_offer_text) return str8_zero();
int fds[2];
if(pipe2(fds, O_CLOEXEC) != 0) return str8_zero();
wl_data_offer_receive(state->selection_offer, state->selection_mime[0] ? state->selection_mime : "text/plain;charset=utf-8", fds[1]);
close(fds[1]);
wl_display_flush(state->display);
u64 cap = MiB(1);
u8 *out = PushArray(arena, u8, cap + 1);
u64 size = 0;
u64 deadline = time_now_ns() + 500000000ull; // a client that never writes doesn't hang us
for(;;) {
s64 left = (s64)(deadline - time_now_ns());
if(left <= 0) break;
struct pollfd pfd = {fds[0], POLLIN, 0};
if(poll(&pfd, 1, (int)(left / 1000000ll) + 1) <= 0) break;
ssize_t got = read(fds[0], out + size, cap - size);
if(got > 0) { size += (u64)got; if(size >= cap) break; continue; }
if(got < 0 && errno == EINTR) continue;
break;
}
close(fds[0]);
out[size] = 0;
return ToString8Size(out, size);
}
|