-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SDL_waylandevents.c
2911 lines (2483 loc) · 96.1 KB
/
SDL_waylandevents.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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 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 "SDL_stdinc.h"
#include "SDL_timer.h"
#include "SDL_hints.h"
#include "../../core/unix/SDL_poll.h"
#include "../../events/SDL_events_c.h"
#include "../../events/SDL_scancode_tables_c.h"
#include "../SDL_sysvideo.h"
#include "SDL_waylandvideo.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylandwindow.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-unstable-v2-client-protocol.h"
#include "primary-selection-unstable-v1-client-protocol.h"
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
#endif
#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)
#endif
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include "../../events/imKStoUCS.h"
#include "../../events/SDL_keysym_to_scancode_c.h"
#include "cursor-shape-v1-client-protocol.h"
/* Clamp the wl_seat version on older versions of libwayland. */
#if SDL_WAYLAND_CHECK_VERSION(1, 21, 0)
#define SDL_WL_SEAT_VERSION 8
#else
#define SDL_WL_SEAT_VERSION 5
#endif
/* Weston uses a ratio of 10 units per scroll tick */
#define WAYLAND_WHEEL_AXIS_UNIT 10
struct SDL_WaylandTouchPoint
{
SDL_TouchID id;
wl_fixed_t x;
wl_fixed_t y;
struct wl_surface *surface;
struct SDL_WaylandTouchPoint *prev;
struct SDL_WaylandTouchPoint *next;
};
struct SDL_WaylandTouchPointList
{
struct SDL_WaylandTouchPoint *head;
struct SDL_WaylandTouchPoint *tail;
};
static struct SDL_WaylandTouchPointList touch_points = { NULL, NULL };
static void touch_add(SDL_TouchID id, wl_fixed_t x, wl_fixed_t y, struct wl_surface *surface)
{
struct SDL_WaylandTouchPoint *tp = SDL_malloc(sizeof(struct SDL_WaylandTouchPoint));
tp->id = id;
tp->x = x;
tp->y = y;
tp->surface = surface;
if (touch_points.tail) {
touch_points.tail->next = tp;
tp->prev = touch_points.tail;
} else {
touch_points.head = tp;
tp->prev = NULL;
}
touch_points.tail = tp;
tp->next = NULL;
}
static void touch_update(SDL_TouchID id, wl_fixed_t x, wl_fixed_t y, struct wl_surface **surface)
{
struct SDL_WaylandTouchPoint *tp = touch_points.head;
while (tp) {
if (tp->id == id) {
tp->x = x;
tp->y = y;
*surface = tp->surface;
}
tp = tp->next;
}
}
static void touch_del(SDL_TouchID id, wl_fixed_t *x, wl_fixed_t *y, struct wl_surface **surface)
{
struct SDL_WaylandTouchPoint *tp = touch_points.head;
while (tp) {
if (tp->id == id) {
*x = tp->x;
*y = tp->y;
*surface = tp->surface;
if (tp->prev) {
tp->prev->next = tp->next;
} else {
touch_points.head = tp->next;
}
if (tp->next) {
tp->next->prev = tp->prev;
} else {
touch_points.tail = tp->prev;
}
{
struct SDL_WaylandTouchPoint *next = tp->next;
SDL_free(tp);
tp = next;
}
} else {
tp = tp->next;
}
}
}
static SDL_bool Wayland_SurfaceHasActiveTouches(struct wl_surface *surface)
{
struct SDL_WaylandTouchPoint *tp = touch_points.head;
while (tp) {
if (tp->surface == surface) {
return SDL_TRUE;
}
tp = tp->next;
}
return SDL_FALSE;
}
void Wayland_CreateCursorShapeDevice(struct SDL_WaylandInput *input)
{
SDL_VideoData *viddata = input->display;
if (viddata->cursor_shape_manager) {
if (input->pointer && !input->cursor_shape) {
input->cursor_shape = wp_cursor_shape_manager_v1_get_pointer(viddata->cursor_shape_manager, input->pointer);
}
}
}
/* Returns SDL_TRUE if a key repeat event was due */
static SDL_bool keyboard_repeat_handle(SDL_WaylandKeyboardRepeat *repeat_info, uint32_t elapsed)
{
SDL_bool ret = SDL_FALSE;
while ((elapsed - repeat_info->next_repeat_ms) < 0x80000000U) {
if (repeat_info->scancode != SDL_SCANCODE_UNKNOWN) {
SDL_SendKeyboardKey(SDL_PRESSED, repeat_info->scancode);
}
if (repeat_info->text[0]) {
SDL_SendKeyboardText(repeat_info->text);
}
repeat_info->next_repeat_ms += 1000 / repeat_info->repeat_rate;
ret = SDL_TRUE;
}
return ret;
}
static void keyboard_repeat_clear(SDL_WaylandKeyboardRepeat *repeat_info)
{
if (!repeat_info->is_initialized) {
return;
}
repeat_info->is_key_down = SDL_FALSE;
}
static void keyboard_repeat_set(SDL_WaylandKeyboardRepeat *repeat_info, uint32_t key, uint32_t wl_press_time,
uint32_t scancode, SDL_bool has_text, char text[8])
{
if (!repeat_info->is_initialized || !repeat_info->repeat_rate) {
return;
}
repeat_info->is_key_down = SDL_TRUE;
repeat_info->key = key;
repeat_info->wl_press_time = wl_press_time;
repeat_info->sdl_press_time = SDL_GetTicks();
repeat_info->next_repeat_ms = repeat_info->repeat_delay;
repeat_info->scancode = scancode;
if (has_text) {
SDL_memcpy(repeat_info->text, text, 8);
} 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_memcpy(repeat_info->text, text, 8);
}
}
static SDL_bool keyboard_repeat_is_set(SDL_WaylandKeyboardRepeat *repeat_info)
{
return repeat_info->is_initialized && repeat_info->is_key_down;
}
static SDL_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;
}
void Wayland_SendWakeupEvent(_THIS, SDL_Window *window)
{
SDL_VideoData *d = _this->driverdata;
/* TODO: Maybe use a pipe to avoid the compositor roundtrip? */
wl_display_sync(d->display);
WAYLAND_wl_display_flush(d->display);
}
static int dispatch_queued_events(SDL_VideoData *viddata)
{
int ret;
/*
* NOTE: When reconnection is implemented, check if libdecor needs to be
* involved in the reconnection process.
*/
#ifdef HAVE_LIBDECOR_H
if (viddata->shell.libdecor) {
libdecor_dispatch(viddata->shell.libdecor, 0);
}
#endif
ret = WAYLAND_wl_display_dispatch_pending(viddata->display);
return ret >= 0 ? 1 : ret;
}
int Wayland_WaitEventTimeout(_THIS, int timeout)
{
SDL_VideoData *d = _this->driverdata;
struct SDL_WaylandInput *input = d->input;
SDL_bool key_repeat_active = SDL_FALSE;
WAYLAND_wl_display_flush(d->display);
#ifdef SDL_USE_IME
if (!d->text_input_manager && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
SDL_IME_PumpEvents();
}
#endif
/* If key repeat is active, we'll need to cap our maximum wait time to handle repeats */
if (input && keyboard_repeat_is_set(&input->keyboard_repeat)) {
uint32_t elapsed = SDL_GetTicks() - input->keyboard_repeat.sdl_press_time;
if (keyboard_repeat_handle(&input->keyboard_repeat, elapsed)) {
/* A repeat key event was already due */
return 1;
} else {
uint32_t next_repeat_wait_time = (input->keyboard_repeat.next_repeat_ms - elapsed) + 1;
if (timeout >= 0) {
timeout = SDL_min(timeout, next_repeat_wait_time);
} else {
timeout = next_repeat_wait_time;
}
key_repeat_active = SDL_TRUE;
}
}
/* wl_display_prepare_read() will return -1 if the default queue is not empty.
* If the default queue is empty, it will prepare us for our SDL_IOReady() call. */
if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
/* Use SDL_IOR_NO_RETRY to ensure SIGINT will break us out of our wait */
int err = SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_IOR_READ | SDL_IOR_NO_RETRY, timeout);
if (err > 0) {
/* There are new events available to read */
WAYLAND_wl_display_read_events(d->display);
return dispatch_queued_events(d);
} else if (err == 0) {
/* No events available within the timeout */
WAYLAND_wl_display_cancel_read(d->display);
/* If key repeat is active, we might have woken up to generate a key event */
if (key_repeat_active) {
uint32_t elapsed = SDL_GetTicks() - input->keyboard_repeat.sdl_press_time;
if (keyboard_repeat_handle(&input->keyboard_repeat, elapsed)) {
return 1;
}
}
return 0;
} else {
/* Error returned from poll()/select() */
WAYLAND_wl_display_cancel_read(d->display);
if (errno == EINTR) {
/* If the wait was interrupted by a signal, we may have generated a
* SDL_QUIT event. Let the caller know to call SDL_PumpEvents(). */
return 1;
} else {
return err;
}
}
} else {
/* We already had pending events */
return dispatch_queued_events(d);
}
}
void Wayland_PumpEvents(_THIS)
{
SDL_VideoData *d = _this->driverdata;
struct SDL_WaylandInput *input = d->input;
int err;
#ifdef SDL_USE_IME
if (!d->text_input_manager && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
SDL_IME_PumpEvents();
}
#endif
#ifdef HAVE_LIBDECOR_H
if (d->shell.libdecor) {
libdecor_dispatch(d->shell.libdecor, 0);
}
#endif
WAYLAND_wl_display_flush(d->display);
/* wl_display_prepare_read() will return -1 if the default queue is not empty.
* If the default queue is empty, it will prepare us for our SDL_IOReady() call. */
if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
if (SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_IOR_READ, 0) > 0) {
WAYLAND_wl_display_read_events(d->display);
} else {
WAYLAND_wl_display_cancel_read(d->display);
}
}
/* Dispatch any pre-existing pending events or new events we may have read */
err = WAYLAND_wl_display_dispatch_pending(d->display);
if (input && keyboard_repeat_is_set(&input->keyboard_repeat)) {
uint32_t elapsed = SDL_GetTicks() - input->keyboard_repeat.sdl_press_time;
keyboard_repeat_handle(&input->keyboard_repeat, elapsed);
}
if (err < 0 && !d->display_disconnected) {
/* Something has failed with the Wayland connection -- for example,
* the compositor may have shut down and closed its end of the socket,
* or there is a library-specific error.
*
* Try to recover once, then quit.
*/
if (!Wayland_VideoReconnect(_this)) {
d->display_disconnected = 1;
/* Only send a single quit message, as application shutdown might call
* SDL_PumpEvents
*/
SDL_SendQuit();
}
}
}
static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window = input->pointer_focus;
input->sx_w = sx_w;
input->sy_w = sy_w;
if (input->pointer_focus) {
const float sx_f = (float)wl_fixed_to_double(sx_w);
const float sy_f = (float)wl_fixed_to_double(sy_w);
const int sx = (int)SDL_floorf(sx_f * window->pointer_scale_x);
const int sy = (int)SDL_floorf(sy_f * window->pointer_scale_y);
SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
}
}
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)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window;
if (!surface) {
/* enter event for a window we've just destroyed */
return;
}
/* check that this surface belongs to one of the SDL windows */
if (!SDL_WAYLAND_own_surface(surface)) {
return;
}
/* This handler will be called twice in Wayland 1.4
* Once for the window surface which has valid user data
* and again for the mouse cursor surface which does not have valid user data
* We ignore the later
*/
window = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window) {
input->pointer_focus = window;
input->pointer_enter_serial = serial;
SDL_SetMouseFocus(window->sdlwindow);
/* 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. */
pointer_handle_motion(data, pointer, serial, sx_w, sy_w);
/* 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. */
SDL_SetCursor(NULL);
}
}
static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
struct SDL_WaylandInput *input = data;
if (!surface || !SDL_WAYLAND_own_surface(surface)) {
return;
}
if (input->pointer_focus) {
/* 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.
*/
if (!Wayland_SurfaceHasActiveTouches(surface)) {
SDL_SetMouseFocus(NULL);
}
input->pointer_focus = NULL;
}
}
static SDL_bool ProcessHitTest(struct SDL_WaylandInput *input, uint32_t serial)
{
SDL_WindowData *window_data = input->pointer_focus;
SDL_Window *window = window_data->sdlwindow;
if (window->hit_test) {
const SDL_Point point = { wl_fixed_to_int(input->sx_w), wl_fixed_to_int(input->sy_w) };
const SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
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 (rc) {
case SDL_HITTEST_DRAGGABLE:
#ifdef HAVE_LIBDECOR_H
if (window_data->shell_surface_type == WAYLAND_SURFACE_LIBDECOR) {
if (window_data->shell_surface.libdecor.frame) {
libdecor_frame_move(window_data->shell_surface.libdecor.frame, input->seat, serial);
}
} else
#endif
if (window_data->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
if (window_data->shell_surface.xdg.roleobj.toplevel) {
xdg_toplevel_move(window_data->shell_surface.xdg.roleobj.toplevel,
input->seat,
serial);
}
}
return SDL_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_SURFACE_LIBDECOR) {
if (window_data->shell_surface.libdecor.frame) {
libdecor_frame_resize(window_data->shell_surface.libdecor.frame, input->seat, serial, directions_libdecor[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
}
} else
#endif
if (window_data->shell_surface_type == WAYLAND_SURFACE_XDG_TOPLEVEL) {
if (window_data->shell_surface.xdg.roleobj.toplevel) {
xdg_toplevel_resize(window_data->shell_surface.xdg.roleobj.toplevel,
input->seat,
serial,
directions[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
}
}
return SDL_TRUE;
default:
return SDL_FALSE;
}
}
return SDL_FALSE;
}
static void pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
{
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_button_state state = state_w;
uint32_t sdl_button;
if (window) {
SDL_VideoData *viddata = window->waylandData;
switch (button) {
case BTN_LEFT:
sdl_button = SDL_BUTTON_LEFT;
if (ProcessHitTest(input, serial)) {
return; /* don't pass this event on to app. */
}
break;
case BTN_MIDDLE:
sdl_button = SDL_BUTTON_MIDDLE;
break;
case BTN_RIGHT:
sdl_button = SDL_BUTTON_RIGHT;
break;
case BTN_SIDE:
sdl_button = SDL_BUTTON_X1;
break;
case BTN_EXTRA:
sdl_button = SDL_BUTTON_X2;
break;
default:
return;
}
/* Wayland won't let you "capture" the mouse, but it will
automatically track the mouse outside the window if you
drag outside of it, until you let go of all buttons (even
if you add or remove presses outside the window, as long
as any button is still down, the capture remains) */
if (state) { /* update our mask of currently-pressed buttons */
input->buttons_pressed |= SDL_BUTTON(sdl_button);
} else {
input->buttons_pressed &= ~(SDL_BUTTON(sdl_button));
}
/* Don't modify the capture flag in relative mode. */
if (!viddata->relative_mouse_mode) {
if (input->buttons_pressed != 0) {
window->sdlwindow->flags |= SDL_WINDOW_MOUSE_CAPTURE;
} else {
window->sdlwindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
}
}
Wayland_data_device_set_serial(input->data_device, serial);
Wayland_primary_selection_device_set_serial(input->primary_selection_device, serial);
SDL_SendMouseButton(window->sdlwindow, 0,
state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
}
}
static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
{
struct SDL_WaylandInput *input = data;
pointer_handle_button_common(input, serial, time, button, state_w);
}
static void pointer_handle_axis_common_v1(struct SDL_WaylandInput *input,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_axis a = axis;
float x, y;
if (input->pointer_focus) {
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
x = 0;
y = 0 - (float)wl_fixed_to_double(value);
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
x = (float)wl_fixed_to_double(value);
y = 0;
break;
default:
return;
}
x /= WAYLAND_WHEEL_AXIS_UNIT;
y /= WAYLAND_WHEEL_AXIS_UNIT;
SDL_SendMouseWheel(window->sdlwindow, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
}
}
static void pointer_handle_axis_common(struct SDL_WaylandInput *input, enum SDL_WaylandAxisEvent type,
uint32_t axis, wl_fixed_t value)
{
enum wl_pointer_axis a = axis;
if (input->pointer_focus) {
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
switch (type) {
case AXIS_EVENT_VALUE120:
/*
* High resolution scroll event. The spec doesn't state that axis_value120
* events are limited to one per frame, so the values are accumulated.
*/
if (input->pointer_curr_axis_info.y_axis_type != AXIS_EVENT_VALUE120) {
input->pointer_curr_axis_info.y_axis_type = AXIS_EVENT_VALUE120;
input->pointer_curr_axis_info.y = 0.0f;
}
input->pointer_curr_axis_info.y += 0 - (float)wl_fixed_to_double(value);
break;
case AXIS_EVENT_DISCRETE:
/*
* This is a discrete axis event, so we process it and set the
* flag to ignore future continuous axis events in this frame.
*/
if (input->pointer_curr_axis_info.y_axis_type != AXIS_EVENT_DISCRETE) {
input->pointer_curr_axis_info.y_axis_type = AXIS_EVENT_DISCRETE;
input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
}
break;
case AXIS_EVENT_CONTINUOUS:
/* Only process continuous events if no discrete events have been received. */
if (input->pointer_curr_axis_info.y_axis_type == AXIS_EVENT_CONTINUOUS) {
input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
}
break;
}
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
switch (type) {
case AXIS_EVENT_VALUE120:
/*
* High resolution scroll event. The spec doesn't state that axis_value120
* events are limited to one per frame, so the values are accumulated.
*/
if (input->pointer_curr_axis_info.x_axis_type != AXIS_EVENT_VALUE120) {
input->pointer_curr_axis_info.x_axis_type = AXIS_EVENT_VALUE120;
input->pointer_curr_axis_info.x = 0.0f;
}
input->pointer_curr_axis_info.x += (float)wl_fixed_to_double(value);
break;
case AXIS_EVENT_DISCRETE:
/*
* This is a discrete axis event, so we process it and set the
* flag to ignore future continuous axis events in this frame.
*/
if (input->pointer_curr_axis_info.x_axis_type != AXIS_EVENT_DISCRETE) {
input->pointer_curr_axis_info.x_axis_type = AXIS_EVENT_DISCRETE;
input->pointer_curr_axis_info.x = (float)wl_fixed_to_double(value);
}
break;
case AXIS_EVENT_CONTINUOUS:
/* Only process continuous events if no discrete events have been received. */
if (input->pointer_curr_axis_info.x_axis_type == AXIS_EVENT_CONTINUOUS) {
input->pointer_curr_axis_info.x = (float)wl_fixed_to_double(value);
}
break;
}
break;
}
}
}
static void pointer_handle_axis(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
struct SDL_WaylandInput *input = data;
if (wl_seat_get_version(input->seat) >= 5) {
pointer_handle_axis_common(input, AXIS_EVENT_CONTINUOUS, axis, value);
} else {
pointer_handle_axis_common_v1(input, time, axis, value);
}
}
static void pointer_handle_frame(void *data, struct wl_pointer *pointer)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window = input->pointer_focus;
float x, y;
switch (input->pointer_curr_axis_info.x_axis_type) {
case AXIS_EVENT_CONTINUOUS:
x = input->pointer_curr_axis_info.x / WAYLAND_WHEEL_AXIS_UNIT;
break;
case AXIS_EVENT_DISCRETE:
x = input->pointer_curr_axis_info.x;
break;
case AXIS_EVENT_VALUE120:
x = input->pointer_curr_axis_info.x / 120.0f;
break;
default:
x = 0.0f;
break;
}
switch (input->pointer_curr_axis_info.y_axis_type) {
case AXIS_EVENT_CONTINUOUS:
y = input->pointer_curr_axis_info.y / WAYLAND_WHEEL_AXIS_UNIT;
break;
case AXIS_EVENT_DISCRETE:
y = input->pointer_curr_axis_info.y;
break;
case AXIS_EVENT_VALUE120:
y = input->pointer_curr_axis_info.y / 120.0f;
break;
default:
y = 0.0f;
break;
}
/* clear pointer_curr_axis_info for next frame */
SDL_memset(&input->pointer_curr_axis_info, 0, sizeof(input->pointer_curr_axis_info));
if (x != 0.0f || y != 0.0f) {
SDL_SendMouseWheel(window->sdlwindow, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
}
}
static void pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
uint32_t axis_source)
{
/* unimplemented */
}
static void pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis)
{
/* unimplemented */
}
static void pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
uint32_t axis, int32_t discrete)
{
struct SDL_WaylandInput *input = data;
pointer_handle_axis_common(input, AXIS_EVENT_DISCRETE, axis, wl_fixed_from_int(discrete));
}
static void pointer_handle_axis_value120(void *data, struct wl_pointer *pointer,
uint32_t axis, int32_t value120)
{
struct SDL_WaylandInput *input = data;
pointer_handle_axis_common(input, AXIS_EVENT_VALUE120, axis, wl_fixed_from_int(value120));
}
static const struct wl_pointer_listener pointer_listener = {
pointer_handle_enter,
pointer_handle_leave,
pointer_handle_motion,
pointer_handle_button,
pointer_handle_axis,
pointer_handle_frame, /* Version 5 */
pointer_handle_axis_source, /* Version 5 */
pointer_handle_axis_stop, /* Version 5 */
pointer_handle_axis_discrete, /* Version 5 */
pointer_handle_axis_value120 /* Version 8 */
};
static void touch_handler_down(void *data, struct wl_touch *touch, uint32_t serial,
uint32_t timestamp, struct wl_surface *surface,
int id, wl_fixed_t fx, wl_fixed_t fy)
{
SDL_WindowData *window_data;
/* Check that this surface belongs to one of the SDL windows */
if (!SDL_WAYLAND_own_surface(surface)) {
return;
}
touch_add(id, fx, fy, surface);
window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window_data) {
const double dblx = wl_fixed_to_double(fx) * window_data->pointer_scale_x;
const double dbly = wl_fixed_to_double(fy) * window_data->pointer_scale_y;
const float x = dblx / window_data->sdlwindow->w;
const float y = dbly / window_data->sdlwindow->h;
SDL_SetMouseFocus(window_data->sdlwindow);
SDL_SendTouch((SDL_TouchID)(intptr_t)touch, (SDL_FingerID)id,
window_data->sdlwindow, SDL_TRUE, x, y, 1.0f);
}
}
static void touch_handler_up(void *data, struct wl_touch *touch, uint32_t serial,
uint32_t timestamp, int id)
{
wl_fixed_t fx = 0, fy = 0;
struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
struct wl_surface *surface = NULL;
touch_del(id, &fx, &fy, &surface);
if (surface) {
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window_data) {
const double dblx = wl_fixed_to_double(fx) * window_data->pointer_scale_x;
const double dbly = wl_fixed_to_double(fy) * window_data->pointer_scale_y;
const float x = dblx / window_data->sdlwindow->w;
const float y = dbly / window_data->sdlwindow->h;
SDL_SendTouch((SDL_TouchID)(intptr_t)touch, (SDL_FingerID)id,
window_data->sdlwindow, SDL_FALSE, x, y, 1.0f);
/* If the seat lacks pointer focus, the seat's keyboard focus is another window or NULL, this window curently
* has mouse focus, and the surface has no active touch events, consider mouse focus to be lost.
*/
if (!input->pointer_focus && input->keyboard_focus != window_data &&
SDL_GetMouseFocus() == window_data->sdlwindow && !Wayland_SurfaceHasActiveTouches(surface)) {
SDL_SetMouseFocus(NULL);
}
}
}
}
static void touch_handler_motion(void *data, struct wl_touch *touch, uint32_t timestamp,
int id, wl_fixed_t fx, wl_fixed_t fy)
{
struct wl_surface *surface = NULL;
touch_update(id, fx, fy, &surface);
if (surface) {
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window_data) {
const double dblx = wl_fixed_to_double(fx) * window_data->pointer_scale_x;
const double dbly = wl_fixed_to_double(fy) * window_data->pointer_scale_y;
const float x = dblx / window_data->sdlwindow->w;
const float y = dbly / window_data->sdlwindow->h;
SDL_SendTouchMotion((SDL_TouchID)(intptr_t)touch, (SDL_FingerID)id,
window_data->sdlwindow, x, y, 1.0f);
}
}
}
static void touch_handler_frame(void *data, struct wl_touch *touch)
{
}
static void touch_handler_cancel(void *data, struct wl_touch *touch)
{
}
static const struct wl_touch_listener touch_listener = {
touch_handler_down,
touch_handler_up,
touch_handler_motion,
touch_handler_frame,
touch_handler_cancel,
NULL, /* shape */
NULL, /* orientation */
};
typedef struct Wayland_Keymap
{
xkb_layout_index_t layout;
SDL_Keycode keymap[SDL_NUM_SCANCODES];
} Wayland_Keymap;
static void Wayland_keymap_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
{
const xkb_keysym_t *syms;
Wayland_Keymap *sdlKeymap = (Wayland_Keymap *)data;
SDL_Scancode scancode;
scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, (key - 8));
if (scancode == SDL_SCANCODE_UNKNOWN) {
return;
}
if (WAYLAND_xkb_keymap_key_get_syms_by_level(keymap, key, sdlKeymap->layout, 0, &syms) > 0) {
uint32_t keycode = SDL_KeySymToUcs4(syms[0]);
if (!keycode) {
const SDL_Scancode sc = SDL_GetScancodeFromKeySym(syms[0], key);
keycode = SDL_GetDefaultKeyFromScancode(sc);
}
if (keycode) {
sdlKeymap->keymap[scancode] = keycode;
} else {
switch (scancode) {
case SDL_SCANCODE_RETURN:
sdlKeymap->keymap[scancode] = SDLK_RETURN;
break;
case SDL_SCANCODE_ESCAPE:
sdlKeymap->keymap[scancode] = SDLK_ESCAPE;
break;
case SDL_SCANCODE_BACKSPACE:
sdlKeymap->keymap[scancode] = SDLK_BACKSPACE;
break;
case SDL_SCANCODE_TAB:
sdlKeymap->keymap[scancode] = SDLK_TAB;
break;
case SDL_SCANCODE_DELETE:
sdlKeymap->keymap[scancode] = SDLK_DELETE;
break;
default:
sdlKeymap->keymap[scancode] = SDL_SCANCODE_TO_KEYCODE(scancode);
break;
}
}
}
}
static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
uint32_t format, int fd, uint32_t size)
{
struct SDL_WaylandInput *input = data;
char *map_str;
const char *locale;
if (!data) {
close(fd);
return;
}
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;