-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathSDL_waylandevents.c
More file actions
3975 lines (3427 loc) · 156 KB
/
Copy pathSDL_waylandevents.c
File metadata and controls
3975 lines (3427 loc) · 156 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_WAYLAND
#include "../../core/unix/SDL_poll.h"
#include "../../events/SDL_events_c.h"
#include "../../events/SDL_scancode_tables_c.h"
#include "../../events/SDL_keysym_to_keycode_c.h"
#include "../../core/linux/SDL_system_theme.h"
#include "../SDL_sysvideo.h"
#include "SDL_waylandvideo.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylandwindow.h"
#include "SDL_waylandmouse.h"
#include "SDL_waylandclipboard.h"
#include "SDL_waylandkeyboard.h"
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
#include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h"
#include "text-input-unstable-v3-client-protocol.h"
#include "tablet-v2-client-protocol.h"
#include "primary-selection-unstable-v1-client-protocol.h"
#include "input-timestamps-unstable-v1-client-protocol.h"
#include "pointer-gestures-unstable-v1-client-protocol.h"
#include "cursor-shape-v1-client-protocol.h"
#include "viewporter-client-protocol.h"
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
#endif
// Per the spec, Wayland mouse and stylus buttons are defined as Linux event codes.
#ifdef SDL_INPUT_LINUXEV
#include <linux/input.h>
#else
#define BTN_LEFT (0x110)
#define BTN_RIGHT (0x111)
#define BTN_MIDDLE (0x112)
#define BTN_SIDE (0x113)
#define BTN_EXTRA (0x114)
#define BTN_STYLUS (0x14b)
#define BTN_STYLUS2 (0x14c)
#define BTN_STYLUS3 (0x149)
#endif
#include "../../events/SDL_keysym_to_scancode_c.h"
#include "../../events/imKStoUCS.h"
#include <errno.h>
#include <sys/mman.h>
#include <unistd.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <xkbcommon/xkbcommon.h>
// Weston uses a ratio of 10 units per scroll tick
#define WAYLAND_WHEEL_AXIS_UNIT 10
#ifndef XKB_MOD_NAME_MOD3
#define XKB_MOD_NAME_MOD3 "Mod3"
#endif
#ifndef XKB_MOD_NAME_MOD5
#define XKB_MOD_NAME_MOD5 "Mod5"
#endif
// Keyboard and mouse names to match XWayland
#define WAYLAND_DEFAULT_KEYBOARD_NAME "Virtual core keyboard"
#define WAYLAND_DEFAULT_POINTER_NAME "Virtual core pointer"
#define WAYLAND_DEFAULT_TOUCH_NAME "Virtual core touch"
// Focus clickthrough timeout
#define WAYLAND_FOCUS_CLICK_TIMEOUT_NS SDL_MS_TO_NS(10)
// Timer rollover detection thresholds
#define WAYLAND_TIMER_ROLLOVER_INTERVAL_LOW (SDL_MAX_UINT32 / 16U)
#define WAYLAND_TIMER_ROLLOVER_INTERVAL_HIGH (WAYLAND_TIMER_ROLLOVER_INTERVAL_LOW * 15U)
// Scoped function declarations
static void Wayland_SeatUpdateKeyboardGrab(SDL_WaylandSeat *seat);
typedef struct
{
SDL_TouchID id;
wl_fixed_t fx;
wl_fixed_t fy;
struct wl_surface *surface;
struct wl_list link;
} SDL_WaylandTouchPoint;
static void Wayland_SeatAddTouch(SDL_WaylandSeat *seat, SDL_TouchID id, wl_fixed_t fx, wl_fixed_t fy, struct wl_surface *surface)
{
SDL_WaylandTouchPoint *tp = SDL_malloc(sizeof(SDL_WaylandTouchPoint));
SDL_zerop(tp);
tp->id = id;
tp->fx = fx;
tp->fy = fy;
tp->surface = surface;
WAYLAND_wl_list_insert(&seat->touch.points, &tp->link);
}
static void Wayland_SeatCancelTouch(SDL_WaylandSeat *seat, SDL_WaylandTouchPoint *tp)
{
if (tp->surface) {
SDL_WindowData *window_data = Wayland_GetWindowDataForOwnedSurface(tp->surface);
if (window_data) {
const float x = (float)(wl_fixed_to_double(tp->fx) / window_data->current.logical_width);
const float y = (float)(wl_fixed_to_double(tp->fy) / window_data->current.logical_height);
SDL_SendTouch(0, (SDL_TouchID)(uintptr_t)seat->touch.wl_touch,
(SDL_FingerID)(tp->id + 1), window_data->sdlwindow, SDL_EVENT_FINGER_CANCELED, x, y, 0.0f);
--window_data->active_touch_count;
/* If the window currently has mouse focus and has no currently active keyboards, pointers,
* or touch events, then consider mouse focus to be lost.
*/
if (SDL_GetMouseFocus() == window_data->sdlwindow && !window_data->keyboard_focus_count &&
!window_data->pointer_focus_count && !window_data->active_touch_count) {
SDL_SetMouseFocus(NULL);
}
}
}
WAYLAND_wl_list_remove(&tp->link);
SDL_free(tp);
}
static void Wayland_SeatUpdateTouch(SDL_WaylandSeat *seat, SDL_TouchID id, wl_fixed_t fx, wl_fixed_t fy, struct wl_surface **surface)
{
SDL_WaylandTouchPoint *tp;
wl_list_for_each (tp, &seat->touch.points, link) {
if (tp->id == id) {
tp->fx = fx;
tp->fy = fy;
if (surface) {
*surface = tp->surface;
}
break;
}
}
}
static void Wayland_SeatRemoveTouch(SDL_WaylandSeat *seat, SDL_TouchID id, wl_fixed_t *fx, wl_fixed_t *fy, struct wl_surface **surface)
{
SDL_WaylandTouchPoint *tp;
wl_list_for_each (tp, &seat->touch.points, link) {
if (tp->id == id) {
if (fx) {
*fx = tp->fx;
}
if (fy) {
*fy = tp->fy;
}
if (surface) {
*surface = tp->surface;
}
WAYLAND_wl_list_remove(&tp->link);
SDL_free(tp);
break;
}
}
}
static void Wayland_GetScaledMouseRect(SDL_Window *window, SDL_Rect *scaled_rect)
{
SDL_WindowData *window_data = window->internal;
scaled_rect->x = (int)SDL_floor(window->mouse_rect.x / window_data->pointer_scale.x);
scaled_rect->y = (int)SDL_floor(window->mouse_rect.y / window_data->pointer_scale.y);
scaled_rect->w = (int)SDL_ceil(window->mouse_rect.w / window_data->pointer_scale.x);
scaled_rect->h = (int)SDL_ceil(window->mouse_rect.h / window_data->pointer_scale.y);
}
static Uint64 Wayland_AdjustEventTimestampBase(Uint64 nsTimestamp)
{
static Uint64 timestamp_offset = 0;
const Uint64 now = SDL_GetTicksNS();
if (!timestamp_offset) {
timestamp_offset = (now - nsTimestamp);
}
nsTimestamp += timestamp_offset;
if (nsTimestamp > now) {
timestamp_offset -= (nsTimestamp - now);
nsTimestamp = now;
}
return nsTimestamp;
}
/* This should only be called with 32-bit millisecond timestamps received in Wayland events!
* No synthetic or high-res timestamps, as they can corrupt the rollover offset!
*/
static Uint64 Wayland_EventTimestampMSToNS(Uint32 wl_timestamp_ms)
{
static Uint64 timestamp_offset = 0;
static Uint32 last = 0;
Uint64 timestamp = SDL_MS_TO_NS(wl_timestamp_ms) + timestamp_offset;
if (wl_timestamp_ms >= last) {
if (timestamp_offset && last < WAYLAND_TIMER_ROLLOVER_INTERVAL_LOW && wl_timestamp_ms > WAYLAND_TIMER_ROLLOVER_INTERVAL_HIGH) {
// A time that crossed backwards across zero was received. Subtract the increased time base offset.
timestamp -= SDL_MS_TO_NS(SDL_UINT64_C(0x100000000));
} else {
last = wl_timestamp_ms;
}
} else {
/* Only increment the base time offset if the timer actually crossed forward across 0,
* and not if this is just a timestamp from a slightly older event.
*/
if (wl_timestamp_ms < WAYLAND_TIMER_ROLLOVER_INTERVAL_LOW && last > WAYLAND_TIMER_ROLLOVER_INTERVAL_HIGH) {
timestamp_offset += SDL_MS_TO_NS(SDL_UINT64_C(0x100000000));
timestamp += SDL_MS_TO_NS(SDL_UINT64_C(0x100000000));
last = wl_timestamp_ms;
}
}
return timestamp;
}
/* Even if high-res timestamps are available, the millisecond timestamps are still processed
* to accumulate the rollover offset if needed later.
*/
static Uint64 Wayland_GetKeyboardTimestamp(SDL_WaylandSeat *seat, Uint32 wl_timestamp_ms)
{
const Uint64 adjustedTimestampNS = Wayland_EventTimestampMSToNS(wl_timestamp_ms);
return Wayland_AdjustEventTimestampBase(seat->keyboard.timestamps ? seat->keyboard.highres_timestamp_ns : adjustedTimestampNS);
}
static Uint64 Wayland_GetPointerTimestamp(SDL_WaylandSeat *seat, Uint32 wl_timestamp_ms)
{
const Uint64 adjustedTimestampNS = Wayland_EventTimestampMSToNS(wl_timestamp_ms);
return Wayland_AdjustEventTimestampBase(seat->pointer.timestamps ? seat->pointer.highres_timestamp_ns : adjustedTimestampNS);
}
Uint64 Wayland_GetTouchTimestamp(SDL_WaylandSeat *seat, Uint32 wl_timestamp_ms)
{
const Uint64 adjustedTimestampNS = Wayland_EventTimestampMSToNS(wl_timestamp_ms);
return Wayland_AdjustEventTimestampBase(seat->touch.timestamps ? seat->touch.highres_timestamp_ns : adjustedTimestampNS);
}
static void input_timestamp_listener(void *data, struct zwp_input_timestamps_v1 *zwp_input_timestamps_v1,
uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec)
{
*((Uint64 *)data) = ((((Uint64)tv_sec_hi << 32) | (Uint64)tv_sec_lo) * SDL_NS_PER_SECOND) + tv_nsec;
}
static const struct zwp_input_timestamps_v1_listener timestamp_listener = {
input_timestamp_listener
};
static void Wayland_SeatRegisterInputTimestampListeners(SDL_WaylandSeat *seat)
{
if (seat->display->input_timestamps_manager) {
if (seat->keyboard.wl_keyboard && !seat->keyboard.timestamps) {
seat->keyboard.timestamps = zwp_input_timestamps_manager_v1_get_keyboard_timestamps(seat->display->input_timestamps_manager, seat->keyboard.wl_keyboard);
zwp_input_timestamps_v1_add_listener(seat->keyboard.timestamps, ×tamp_listener, &seat->keyboard.highres_timestamp_ns);
}
if (seat->pointer.wl_pointer && !seat->pointer.timestamps) {
seat->pointer.timestamps = zwp_input_timestamps_manager_v1_get_pointer_timestamps(seat->display->input_timestamps_manager, seat->pointer.wl_pointer);
zwp_input_timestamps_v1_add_listener(seat->pointer.timestamps, ×tamp_listener, &seat->pointer.highres_timestamp_ns);
}
if (seat->touch.wl_touch && !seat->touch.timestamps) {
seat->touch.timestamps = zwp_input_timestamps_manager_v1_get_touch_timestamps(seat->display->input_timestamps_manager, seat->touch.wl_touch);
zwp_input_timestamps_v1_add_listener(seat->touch.timestamps, ×tamp_listener, &seat->touch.highres_timestamp_ns);
}
}
}
void Wayland_DisplayInitInputTimestampManager(SDL_VideoData *display)
{
if (display->input_timestamps_manager) {
SDL_WaylandSeat *seat;
wl_list_for_each (seat, &display->seat_list, link) {
Wayland_SeatRegisterInputTimestampListeners(seat);
}
}
}
static void handle_pinch_begin(void *data, struct zwp_pointer_gesture_pinch_v1 *zwp_pointer_gesture_pinch_v1, uint32_t serial, uint32_t time, struct wl_surface *surface, uint32_t fingers)
{
if (!surface) {
return;
}
SDL_WindowData *wind = Wayland_GetWindowDataForOwnedSurface(surface);
if (wind) {
SDL_WaylandSeat *seat = (SDL_WaylandSeat *)data;
seat->pointer.gesture_focus = wind;
const Uint64 timestamp = Wayland_GetPointerTimestamp(seat, time);
SDL_SendPinch(SDL_EVENT_PINCH_BEGIN, timestamp, wind->sdlwindow, 0.0f, -1.0f, -1.0f, -1.0f, -1.0f);
}
}
static void handle_pinch_update(void *data, struct zwp_pointer_gesture_pinch_v1 *zwp_pointer_gesture_pinch_v1, uint32_t time,
wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t scale, wl_fixed_t rotation)
{
SDL_WaylandSeat *seat = (SDL_WaylandSeat *)data;
if (seat->pointer.gesture_focus) {
const Uint64 timestamp = Wayland_GetPointerTimestamp(seat, time);
const float s = (float)wl_fixed_to_double(scale);
SDL_SendPinch(SDL_EVENT_PINCH_UPDATE, timestamp, seat->pointer.gesture_focus->sdlwindow, s, -1.0f, -1.0f, -1.0f, -1.0f);
}
}
static void handle_pinch_end(void *data, struct zwp_pointer_gesture_pinch_v1 *zwp_pointer_gesture_pinch_v1, uint32_t serial, uint32_t time, int32_t cancelled)
{
SDL_WaylandSeat *seat = (SDL_WaylandSeat *)data;
if (seat->pointer.gesture_focus) {
const Uint64 timestamp = Wayland_GetPointerTimestamp(seat, time);
SDL_SendPinch(SDL_EVENT_PINCH_END, timestamp, seat->pointer.gesture_focus->sdlwindow, 0.0f, -1.0f, -1.0f, -1.0f, -1.0f);
seat->pointer.gesture_focus = NULL;
}
}
static const struct zwp_pointer_gesture_pinch_v1_listener gesture_pinch_listener = {
handle_pinch_begin,
handle_pinch_update,
handle_pinch_end
};
static void Wayland_SeatCreatePointerGestures(SDL_WaylandSeat *seat)
{
if (seat->display->zwp_pointer_gestures) {
if (seat->pointer.wl_pointer && !seat->pointer.gesture_pinch) {
seat->pointer.gesture_pinch = zwp_pointer_gestures_v1_get_pinch_gesture(seat->display->zwp_pointer_gestures, seat->pointer.wl_pointer);
zwp_pointer_gesture_pinch_v1_set_user_data(seat->pointer.gesture_pinch, seat);
zwp_pointer_gesture_pinch_v1_add_listener(seat->pointer.gesture_pinch, &gesture_pinch_listener, seat);
}
}
}
void Wayland_DisplayInitPointerGestureManager(SDL_VideoData *display)
{
SDL_WaylandSeat *seat;
wl_list_for_each (seat, &display->seat_list, link) {
Wayland_SeatCreatePointerGestures(seat);
}
}
static void Wayland_SeatCreateCursorShape(SDL_WaylandSeat *seat)
{
if (seat->display->cursor_shape_manager) {
if (seat->pointer.wl_pointer && !seat->pointer.cursor_state.cursor_shape) {
seat->pointer.cursor_state.cursor_shape = wp_cursor_shape_manager_v1_get_pointer(seat->display->cursor_shape_manager, seat->pointer.wl_pointer);
}
SDL_WaylandPenTool *tool;
wl_list_for_each(tool, &seat->tablet.tool_list, link) {
if (!tool->cursor_state.cursor_shape) {
tool->cursor_state.cursor_shape = wp_cursor_shape_manager_v1_get_tablet_tool_v2(seat->display->cursor_shape_manager, tool->wltool);
}
}
}
}
void Wayland_DisplayInitCursorShapeManager(SDL_VideoData *display)
{
SDL_WaylandSeat *seat;
wl_list_for_each (seat, &display->seat_list, link) {
Wayland_SeatCreateCursorShape(seat);
}
}
static void Wayland_SeatSetKeymap(SDL_WaylandSeat *seat)
{
const bool send_event = !seat->display->initializing;
if (seat->keyboard.sdl_keymap &&
seat->keyboard.xkb.current_layout < seat->keyboard.xkb.num_layouts &&
seat->keyboard.sdl_keymap[seat->keyboard.xkb.current_layout] != SDL_GetCurrentKeymap(true)) {
SDL_SetKeymap(seat->keyboard.sdl_keymap[seat->keyboard.xkb.current_layout], send_event);
SDL_SetModState(seat->keyboard.pressed_modifiers | seat->keyboard.locked_modifiers);
}
}
// Returns true if a key repeat event was due
static bool keyboard_repeat_handle(SDL_WaylandKeyboardRepeat *repeat_info, Uint64 elapsed)
{
bool ret = false;
while (elapsed >= repeat_info->next_repeat_ns) {
if (repeat_info->scancode != SDL_SCANCODE_UNKNOWN) {
const Uint64 timestamp = repeat_info->base_time_ns + repeat_info->next_repeat_ns;
SDL_SendKeyboardKeyIgnoreModifiers(Wayland_AdjustEventTimestampBase(timestamp), repeat_info->keyboard_id, repeat_info->key, repeat_info->scancode, true);
}
if (repeat_info->text[0] && !(SDL_GetModState() & (SDL_KMOD_CTRL | SDL_KMOD_ALT))) {
SDL_SendKeyboardText(repeat_info->text);
}
repeat_info->next_repeat_ns += SDL_NS_PER_SECOND / (Uint64)repeat_info->repeat_rate;
ret = true;
}
return ret;
}
static void keyboard_repeat_clear(SDL_WaylandKeyboardRepeat *repeat_info)
{
if (!repeat_info->is_initialized) {
return;
}
repeat_info->is_key_down = false;
}
static void keyboard_repeat_set(SDL_WaylandKeyboardRepeat *repeat_info, Uint32 keyboard_id, uint32_t key, Uint32 wl_press_time_ms,
Uint64 base_time_ns, uint32_t scancode, bool has_text, char text[8])
{
if (!repeat_info->is_initialized || !repeat_info->repeat_rate) {
return;
}
repeat_info->is_key_down = true;
repeat_info->keyboard_id = keyboard_id;
repeat_info->key = key;
repeat_info->wl_press_time_ms = wl_press_time_ms;
repeat_info->base_time_ns = base_time_ns;
repeat_info->sdl_press_time_ns = SDL_GetTicksNS();
repeat_info->next_repeat_ns = SDL_MS_TO_NS(repeat_info->repeat_delay_ms);
repeat_info->scancode = scancode;
if (has_text) {
SDL_memcpy(repeat_info->text, text, sizeof(repeat_info->text));
} else {
repeat_info->text[0] = '\0';
}
}
static uint32_t keyboard_repeat_get_key(SDL_WaylandKeyboardRepeat *repeat_info)
{
if (repeat_info->is_initialized && repeat_info->is_key_down) {
return repeat_info->key;
}
return 0;
}
static void keyboard_repeat_set_text(SDL_WaylandKeyboardRepeat *repeat_info, const char text[8])
{
if (repeat_info->is_initialized) {
SDL_copyp(repeat_info->text, text);
}
}
static bool keyboard_repeat_is_set(SDL_WaylandKeyboardRepeat *repeat_info)
{
return repeat_info->is_initialized && repeat_info->is_key_down;
}
static bool keyboard_repeat_key_is_set(SDL_WaylandKeyboardRepeat *repeat_info, uint32_t key)
{
return repeat_info->is_initialized && repeat_info->is_key_down && key == repeat_info->key;
}
static void sync_done_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
{
// Nothing to do, just destroy the callback
wl_callback_destroy(callback);
}
static struct wl_callback_listener sync_listener = {
sync_done_handler
};
void Wayland_SendWakeupEvent(SDL_VideoDevice *_this, SDL_Window *window)
{
SDL_VideoData *d = _this->internal;
/* Queue a sync event to unblock the event queue fd if it's empty and being waited on.
* TODO: Maybe use a pipe to avoid the compositor roundtrip?
*/
struct wl_callback *cb = wl_display_sync(d->display);
wl_callback_add_listener(cb, &sync_listener, NULL);
WAYLAND_wl_display_flush(d->display);
}
int Wayland_WaitEventTimeout(SDL_VideoDevice *_this, Sint64 timeoutNS)
{
SDL_VideoData *d = _this->internal;
SDL_WaylandSeat *seat;
Uint64 start = SDL_GetTicksNS();
const int display_fd = WAYLAND_wl_display_get_fd(d->display);
int ret;
bool poll_alarm_set = false;
#ifdef SDL_USE_IME
SDL_Window *keyboard_focus = SDL_GetKeyboardFocus();
if (!d->text_input_manager && keyboard_focus && SDL_TextInputActive(keyboard_focus)) {
// If a DBus IME is active with no text input protocol, periodically wake to poll it.
if (timeoutNS < 0 || SDL_MS_TO_NS(200) <= timeoutNS) {
timeoutNS = SDL_MS_TO_NS(200);
poll_alarm_set = true;
}
}
#endif
// If key repeat is active, we'll need to cap our maximum wait time to handle repeats
wl_list_for_each (seat, &d->seat_list, link) {
if (keyboard_repeat_is_set(&seat->keyboard.repeat)) {
const Uint64 elapsed = start - seat->keyboard.repeat.sdl_press_time_ns;
const Uint64 next_repeat_wait_time = (seat->keyboard.repeat.next_repeat_ns - elapsed) + 1;
if (timeoutNS < 0 || next_repeat_wait_time <= timeoutNS) {
timeoutNS = next_repeat_wait_time;
poll_alarm_set = true;
}
}
}
if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
if (timeoutNS > 0) {
const Uint64 now = SDL_GetTicksNS();
const Uint64 elapsed = now - start;
start = now;
timeoutNS = elapsed <= timeoutNS ? timeoutNS - elapsed : 0;
}
ret = WAYLAND_wl_display_flush(d->display);
if (ret == -1 && errno == EAGAIN) {
// Unable to write to the socket; poll until the socket can be written to, it times out, or is interrupted.
ret = SDL_IOReady(display_fd, SDL_IOR_WRITE | SDL_IOR_NO_RETRY, timeoutNS);
if (ret <= 0) {
// The poll operation timed out or experienced an error, so see if there are any events to read without waiting.
timeoutNS = 0;
}
}
if (ret < 0) {
// Pump events on an interrupt or broken pipe to handle the error.
WAYLAND_wl_display_cancel_read(d->display);
return errno == EINTR || errno == EPIPE ? 1 : ret;
}
if (timeoutNS > 0) {
const Uint64 now = SDL_GetTicksNS();
const Uint64 elapsed = now - start;
start = now;
timeoutNS = elapsed <= timeoutNS ? timeoutNS - elapsed : 0;
}
// Use SDL_IOR_NO_RETRY to catch EINTR.
ret = SDL_IOReady(display_fd, SDL_IOR_READ | SDL_IOR_NO_RETRY, timeoutNS);
if (ret <= 0) {
// Timeout or error, cancel the read.
WAYLAND_wl_display_cancel_read(d->display);
// The poll timed out with no data to read, but signal the caller to pump events if polling is required.
if (ret == 0) {
return poll_alarm_set ? 1 : 0;
} else {
// Pump events on an interrupt or broken pipe to handle the error.
return errno == EINTR || errno == EPIPE ? 1 : ret;
}
}
ret = WAYLAND_wl_display_read_events(d->display);
if (ret == -1) {
return ret;
}
}
// Signal to the caller that there might be an event available.
return 1;
}
void Wayland_PumpEvents(SDL_VideoDevice *_this)
{
SDL_VideoData *d = _this->internal;
SDL_WaylandSeat *seat;
const int display_fd = WAYLAND_wl_display_get_fd(d->display);
int ret = 0;
#ifdef SDL_USE_IME
SDL_Window *keyboard_focus = SDL_GetKeyboardFocus();
if (!d->text_input_manager && keyboard_focus && SDL_TextInputActive(keyboard_focus)) {
SDL_IME_PumpEvents();
}
#endif
#ifdef HAVE_LIBDECOR_H
if (d->shell.libdecor) {
libdecor_dispatch(d->shell.libdecor, 0);
}
#endif
/* If the queue isn't empty, dispatch any old events, and try to prepare for reading again.
* If preparing to read returns -1 on the second try, wl_display_read_events() enqueued new
* events at some point between dispatching the old events and preparing for the read,
* probably from another thread, which means that the events in the queue are current.
*/
ret = WAYLAND_wl_display_prepare_read(d->display);
if (ret == -1) {
ret = WAYLAND_wl_display_dispatch_pending(d->display);
if (ret < 0) {
goto connection_error;
}
ret = WAYLAND_wl_display_prepare_read(d->display);
}
if (ret == 0) {
ret = WAYLAND_wl_display_flush(d->display);
if (ret == -1 && errno == EAGAIN) {
// Unable to write to the socket; wait a brief time to see if it becomes writable.
ret = SDL_IOReady(display_fd, SDL_IOR_WRITE, SDL_MS_TO_NS(4));
if (ret > 0) {
ret = WAYLAND_wl_display_flush(d->display);
}
}
// If the compositor closed the socket, just jump to the error handler.
if (ret < 0 && errno == EPIPE) {
WAYLAND_wl_display_cancel_read(d->display);
goto connection_error;
}
ret = SDL_IOReady(display_fd, SDL_IOR_READ, 0);
if (ret > 0) {
ret = WAYLAND_wl_display_read_events(d->display);
if (ret == 0) {
ret = WAYLAND_wl_display_dispatch_pending(d->display);
}
} else {
WAYLAND_wl_display_cancel_read(d->display);
}
} else {
ret = WAYLAND_wl_display_dispatch_pending(d->display);
}
if (ret >= 0) {
// Synthesize key repeat events.
wl_list_for_each (seat, &d->seat_list, link) {
if (keyboard_repeat_is_set(&seat->keyboard.repeat)) {
Wayland_SeatSetKeymap(seat);
const Uint64 elapsed = SDL_GetTicksNS() - seat->keyboard.repeat.sdl_press_time_ns;
keyboard_repeat_handle(&seat->keyboard.repeat, elapsed);
}
}
}
connection_error:
if (ret < 0) {
Wayland_HandleDisplayDisconnected(_this);
}
}
static void pointer_dispatch_absolute_motion(SDL_WaylandSeat *seat)
{
SDL_WindowData *window_data = seat->pointer.focus;
SDL_Window *window = window_data ? window_data->sdlwindow : NULL;
if (window_data) {
const float sx = (float)(wl_fixed_to_double(seat->pointer.pending_frame.absolute.sx) * window_data->pointer_scale.x);
const float sy = (float)(wl_fixed_to_double(seat->pointer.pending_frame.absolute.sy) * window_data->pointer_scale.y);
SDL_SendMouseMotion(seat->pointer.pending_frame.timestamp_ns, window_data->sdlwindow, seat->pointer.sdl_id, false, sx, sy);
seat->pointer.last_motion.x = (int)SDL_floorf(sx);
seat->pointer.last_motion.y = (int)SDL_floorf(sy);
// If the pointer should be confined, but wasn't for some reason, keep trying until it is.
if (!SDL_RectEmpty(&window->mouse_rect) && !seat->pointer.is_confined) {
Wayland_SeatUpdatePointerGrab(seat);
}
if (window->hit_test) {
SDL_HitTestResult rc = window->hit_test(window, &seat->pointer.last_motion, window->hit_test_data);
// Apply the toplevel constraints if the window isn't resizable from those directions.
switch (rc) {
case SDL_HITTEST_RESIZE_TOPLEFT:
if ((window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_TOP) &&
(window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_LEFT)) {
rc = SDL_HITTEST_NORMAL;
} else if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_TOP) {
rc = SDL_HITTEST_RESIZE_LEFT;
} else if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_LEFT) {
rc = SDL_HITTEST_RESIZE_TOP;
}
break;
case SDL_HITTEST_RESIZE_TOP:
if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_TOP) {
rc = SDL_HITTEST_NORMAL;
}
break;
case SDL_HITTEST_RESIZE_TOPRIGHT:
if ((window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_TOP) &&
(window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_RIGHT)) {
rc = SDL_HITTEST_NORMAL;
} else if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_TOP) {
rc = SDL_HITTEST_RESIZE_RIGHT;
} else if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_RIGHT) {
rc = SDL_HITTEST_RESIZE_TOP;
}
break;
case SDL_HITTEST_RESIZE_RIGHT:
if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_RIGHT) {
rc = SDL_HITTEST_NORMAL;
}
break;
case SDL_HITTEST_RESIZE_BOTTOMRIGHT:
if ((window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_BOTTOM) &&
(window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_RIGHT)) {
rc = SDL_HITTEST_NORMAL;
} else if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_BOTTOM) {
rc = SDL_HITTEST_RESIZE_RIGHT;
} else if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_RIGHT) {
rc = SDL_HITTEST_RESIZE_BOTTOM;
}
break;
case SDL_HITTEST_RESIZE_BOTTOM:
if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_BOTTOM) {
rc = SDL_HITTEST_NORMAL;
}
break;
case SDL_HITTEST_RESIZE_BOTTOMLEFT:
if ((window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_BOTTOM) &&
(window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_LEFT)) {
rc = SDL_HITTEST_NORMAL;
} else if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_BOTTOM) {
rc = SDL_HITTEST_RESIZE_LEFT;
} else if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_LEFT) {
rc = SDL_HITTEST_RESIZE_BOTTOM;
}
break;
case SDL_HITTEST_RESIZE_LEFT:
if (window_data->toplevel_constraints & WAYLAND_TOPLEVEL_CONSTRAINED_LEFT) {
rc = SDL_HITTEST_NORMAL;
}
break;
default:
break;
}
if (rc != window_data->hit_test_result) {
window_data->hit_test_result = rc;
Wayland_SeatUpdatePointerCursor(seat);
}
}
}
}
static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
{
SDL_WaylandSeat *seat = (SDL_WaylandSeat *)data;
const Uint64 timestamp = Wayland_GetPointerTimestamp(seat, time);
seat->pointer.pending_frame.absolute.sx = sx;
seat->pointer.pending_frame.absolute.sy = sy;
if (wl_pointer_get_version(seat->pointer.wl_pointer) >= WL_POINTER_FRAME_SINCE_VERSION) {
seat->pointer.pending_frame.have_absolute = true;
/* The relative pointer timestamp is higher resolution than the default millisecond timestamp,
* but lower than the highres timestamp. Use the best timer available for this frame,
*/
if (!seat->pointer.pending_frame.have_relative || seat->pointer.timestamps) {
seat->pointer.pending_frame.timestamp_ns = timestamp;
}
} else {
seat->pointer.pending_frame.timestamp_ns = timestamp;
pointer_dispatch_absolute_motion(seat);
}
}
static void pointer_dispatch_enter(SDL_WaylandSeat *seat)
{
SDL_WindowData *window = seat->pointer.pending_frame.enter_window;
seat->pointer.focus = window;
++window->pointer_focus_count;
SDL_SetMouseFocus(window->sdlwindow);
// Send the initial position.
pointer_dispatch_absolute_motion(seat);
// Update the pointer grab state.
Wayland_SeatUpdatePointerGrab(seat);
/* If the cursor was changed while our window didn't have pointer
* focus, we might need to trigger another call to
* wl_pointer_set_cursor() for the new cursor to be displayed.
*
* This will also update the cursor if a second pointer entered a
* window that already has focus, as the focus change sequence
* won't be run.
*/
Wayland_SeatUpdatePointerCursor(seat);
}
static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t sx_w, wl_fixed_t sy_w)
{
if (!surface) {
// Enter event for a destroyed surface.
return;
}
SDL_WindowData *window = Wayland_GetWindowDataForOwnedSurface(surface);
if (!window) {
// Not a surface owned by SDL.
return;
}
SDL_WaylandSeat *seat = (SDL_WaylandSeat *)data;
seat->pointer.pending_frame.enter_window = window;
seat->pointer.enter_serial = serial;
/* In the case of e.g. a pointer confine warp, we may receive an enter
* event with no following motion event, but with the new coordinates
* as part of the enter event.
*/
seat->pointer.pending_frame.absolute.sx = sx_w;
seat->pointer.pending_frame.absolute.sy = sy_w;
if (wl_pointer_get_version(seat->pointer.wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
// Dispatching an enter event generates an absolute motion event, for which there is no timestamp.
seat->pointer.pending_frame.timestamp_ns = 0;
pointer_dispatch_enter(seat);
}
}
static void pointer_dispatch_leave(SDL_WaylandSeat *seat, bool update_pointer)
{
SDL_WindowData *window = seat->pointer.pending_frame.leave_window;
if (window) {
// Clear the capture flag and raise all buttons
window->sdlwindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
seat->pointer.focus = NULL;
for (Uint8 i = 1; seat->pointer.buttons_pressed; ++i) {
if (seat->pointer.buttons_pressed & SDL_BUTTON_MASK(i)) {
SDL_SendMouseButton(0, window->sdlwindow, seat->pointer.sdl_id, i, false);
seat->pointer.buttons_pressed &= ~SDL_BUTTON_MASK(i);
}
}
/* A pointer leave event may be emitted if the compositor hides the pointer in response to receiving a touch event.
* Don't relinquish focus if the surface has active touches, as the compositor is just transitioning from mouse to touch mode.
*/
SDL_Window *mouse_focus = SDL_GetMouseFocus();
const bool had_focus = mouse_focus && window->sdlwindow == mouse_focus;
if (!--window->pointer_focus_count && had_focus && !window->active_touch_count) {
SDL_SetMouseFocus(NULL);
}
if (update_pointer) {
Wayland_SeatUpdatePointerGrab(seat);
Wayland_SeatUpdatePointerCursor(seat);
}
}
}
static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
if (!surface) {
// Leave event for a destroyed surface.
return;
}
SDL_WindowData *window = Wayland_GetWindowDataForOwnedSurface(surface);
if (!window) {
// Not a surface owned by SDL.
return;
}
SDL_WaylandSeat *seat = (SDL_WaylandSeat *)data;
seat->pointer.pending_frame.leave_window = window;
if (wl_pointer_get_version(seat->pointer.wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION && window == seat->pointer.focus) {
pointer_dispatch_leave(seat, true);
}
}
static bool Wayland_ProcessHitTest(SDL_WaylandSeat *seat, Uint32 serial)
{
// Pointer is immobilized, do nothing.
if (seat->pointer.locked_pointer) {
return false;
}
SDL_WindowData *window_data = seat->pointer.focus;
SDL_Window *window = window_data->sdlwindow;
if (window->hit_test) {
static const uint32_t directions[] = {
XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT, XDG_TOPLEVEL_RESIZE_EDGE_TOP,
XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT, XDG_TOPLEVEL_RESIZE_EDGE_RIGHT,
XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT, XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM,
XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT, XDG_TOPLEVEL_RESIZE_EDGE_LEFT
};
#ifdef HAVE_LIBDECOR_H
static const uint32_t directions_libdecor[] = {
LIBDECOR_RESIZE_EDGE_TOP_LEFT, LIBDECOR_RESIZE_EDGE_TOP,
LIBDECOR_RESIZE_EDGE_TOP_RIGHT, LIBDECOR_RESIZE_EDGE_RIGHT,
LIBDECOR_RESIZE_EDGE_BOTTOM_RIGHT, LIBDECOR_RESIZE_EDGE_BOTTOM,
LIBDECOR_RESIZE_EDGE_BOTTOM_LEFT, LIBDECOR_RESIZE_EDGE_LEFT
};
#endif
switch (window_data->hit_test_result) {
case SDL_HITTEST_DRAGGABLE:
#ifdef HAVE_LIBDECOR_H
if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
if (window_data->shell_surface.libdecor.frame) {
libdecor_frame_move(window_data->shell_surface.libdecor.frame,
seat->wl_seat,
serial);
}
} else
#endif
if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL) {
if (window_data->shell_surface.xdg.toplevel.xdg_toplevel) {
xdg_toplevel_move(window_data->shell_surface.xdg.toplevel.xdg_toplevel,
seat->wl_seat,
serial);
}
}
return true;
case SDL_HITTEST_RESIZE_TOPLEFT:
case SDL_HITTEST_RESIZE_TOP:
case SDL_HITTEST_RESIZE_TOPRIGHT:
case SDL_HITTEST_RESIZE_RIGHT:
case SDL_HITTEST_RESIZE_BOTTOMRIGHT:
case SDL_HITTEST_RESIZE_BOTTOM:
case SDL_HITTEST_RESIZE_BOTTOMLEFT:
case SDL_HITTEST_RESIZE_LEFT:
#ifdef HAVE_LIBDECOR_H
if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
if (window_data->shell_surface.libdecor.frame) {
libdecor_frame_resize(window_data->shell_surface.libdecor.frame,
seat->wl_seat,
serial,
directions_libdecor[window_data->hit_test_result - SDL_HITTEST_RESIZE_TOPLEFT]);
}
} else
#endif
if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL) {
if (window_data->shell_surface.xdg.toplevel.xdg_toplevel) {
xdg_toplevel_resize(window_data->shell_surface.xdg.toplevel.xdg_toplevel,
seat->wl_seat,
serial,
directions[window_data->hit_test_result - SDL_HITTEST_RESIZE_TOPLEFT]);
}
}
return true;
default:
return false;
}
}
return false;
}
static void pointer_dispatch_button(SDL_WaylandSeat *seat, Uint8 sdl_button, bool down)
{
SDL_WindowData *window = seat->pointer.focus;