Skip to content

Latest commit

 

History

History
1327 lines (1105 loc) · 39.8 KB

SDL_waylandevents.c

File metadata and controls

1327 lines (1105 loc) · 39.8 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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
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"
#if SDL_VIDEO_DRIVER_WAYLAND
#include "SDL_stdinc.h"
#include "SDL_assert.h"
Aug 15, 2017
Aug 15, 2017
29
#include "../../core/unix/SDL_poll.h"
30
31
32
33
34
35
36
37
38
39
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"
#include "../../events/scancodes_xfree86.h"
#include "SDL_waylandvideo.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylandwindow.h"
#include "SDL_waylanddyn.h"
Sep 1, 2016
Sep 1, 2016
40
41
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
Jun 25, 2018
Jun 25, 2018
42
#include "xdg-shell-client-protocol.h"
Feb 7, 2018
Feb 7, 2018
43
#include "xdg-shell-unstable-v6-client-protocol.h"
Sep 1, 2016
Sep 1, 2016
44
Apr 7, 2020
Apr 7, 2020
45
#ifdef SDL_INPUT_LINUXEV
46
#include <linux/input.h>
Apr 7, 2020
Apr 7, 2020
47
48
49
50
51
52
53
#else
#define BTN_LEFT (0x110)
#define BTN_RIGHT (0x111)
#define BTN_MIDDLE (0x112)
#define BTN_SIDE (0x113)
#define BTN_EXTRA (0x114)
#endif
54
55
56
57
58
59
60
61
62
63
#include <sys/select.h>
#include <sys/mman.h>
#include <poll.h>
#include <unistd.h>
#include <xkbcommon/xkbcommon.h>
struct SDL_WaylandInput {
SDL_VideoData *display;
struct wl_seat *seat;
struct wl_pointer *pointer;
Aug 21, 2017
Aug 21, 2017
64
struct wl_touch *touch;
65
struct wl_keyboard *keyboard;
Nov 6, 2016
Nov 6, 2016
66
SDL_WaylandDataDevice *data_device;
Sep 1, 2016
Sep 1, 2016
67
struct zwp_relative_pointer_v1 *relative_pointer;
Apr 17, 2020
Apr 17, 2020
68
struct zwp_confined_pointer_v1 *confined_pointer;
69
70
71
72
73
74
SDL_WindowData *pointer_focus;
SDL_WindowData *keyboard_focus;
/* Last motion location */
wl_fixed_t sx_w;
wl_fixed_t sy_w;
Sep 1, 2016
Sep 1, 2016
75
76
77
78
double dx_frac;
double dy_frac;
79
80
81
82
struct {
struct xkb_keymap *keymap;
struct xkb_state *state;
} xkb;
Feb 3, 2020
Feb 3, 2020
83
84
85
86
87
88
89
90
91
/* information about axis events on current frame */
struct {
SDL_bool is_x_discrete;
float x;
SDL_bool is_y_discrete;
float y;
} pointer_curr_axis_info;
Aug 21, 2017
Aug 21, 2017
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
struct SDL_WaylandTouchPoint {
SDL_TouchID id;
float x;
float 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, float x, float 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, float x, float y)
{
struct SDL_WaylandTouchPoint* tp = touch_points.head;
while (tp) {
if (tp->id == id) {
tp->x = x;
tp->y = y;
}
tp = tp->next;
}
}
static void
Aug 1, 2019
Aug 1, 2019
149
touch_del(SDL_TouchID id, float* x, float* y, struct wl_surface **surface)
Aug 21, 2017
Aug 21, 2017
150
151
152
153
154
155
156
{
struct SDL_WaylandTouchPoint* tp = touch_points.head;
while (tp) {
if (tp->id == id) {
*x = tp->x;
*y = tp->y;
Aug 1, 2019
Aug 1, 2019
157
*surface = tp->surface;
Aug 21, 2017
Aug 21, 2017
158
159
160
161
162
163
164
165
166
167
168
169
170
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;
}
Oct 30, 2019
Oct 30, 2019
171
172
173
174
175
176
177
{
struct SDL_WaylandTouchPoint *next = tp->next;
SDL_free(tp);
tp = next;
}
} else {
tp = tp->next;
Aug 21, 2017
Aug 21, 2017
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
}
}
}
static struct wl_surface*
touch_surface(SDL_TouchID id)
{
struct SDL_WaylandTouchPoint* tp = touch_points.head;
while (tp) {
if (tp->id == id) {
return tp->surface;
}
tp = tp->next;
}
return NULL;
}
198
199
200
201
void
Wayland_PumpEvents(_THIS)
{
SDL_VideoData *d = _this->driverdata;
Aug 6, 2019
Aug 6, 2019
202
int err;
Aug 23, 2018
Aug 23, 2018
204
205
WAYLAND_wl_display_flush(d->display);
Aug 15, 2017
Aug 15, 2017
206
if (SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_FALSE, 0)) {
Aug 6, 2019
Aug 6, 2019
207
208
209
err = WAYLAND_wl_display_dispatch(d->display);
} else {
err = WAYLAND_wl_display_dispatch_pending(d->display);
Aug 15, 2017
Aug 15, 2017
210
}
Aug 6, 2019
Aug 6, 2019
211
212
213
214
215
216
217
218
if (err == -1 && !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. No recovery is possible. */
d->display_disconnected = 1;
/* Only send a single quit message, as application shutdown might call
* SDL_PumpEvents */
SDL_SendQuit();
Aug 15, 2017
Aug 15, 2017
219
}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
}
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;
}
Aug 15, 2017
Aug 15, 2017
234
235
236
237
238
239
240
241
/* 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);
Aug 15, 2017
Aug 15, 2017
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
if (window) {
input->pointer_focus = window;
SDL_SetMouseFocus(window->sdlwindow);
}
}
static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
struct SDL_WaylandInput *input = data;
if (input->pointer_focus) {
SDL_SetMouseFocus(NULL);
input->pointer_focus = NULL;
}
}
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 int sx = wl_fixed_to_int(sx_w);
const int sy = wl_fixed_to_int(sy_w);
SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
}
}
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);
Feb 7, 2018
Feb 7, 2018
285
286
static const uint32_t directions_wl[] = {
287
288
289
290
291
WL_SHELL_SURFACE_RESIZE_TOP_LEFT, WL_SHELL_SURFACE_RESIZE_TOP,
WL_SHELL_SURFACE_RESIZE_TOP_RIGHT, WL_SHELL_SURFACE_RESIZE_RIGHT,
WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT, WL_SHELL_SURFACE_RESIZE_BOTTOM,
WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT, WL_SHELL_SURFACE_RESIZE_LEFT
};
Feb 7, 2018
Feb 7, 2018
292
293
294
295
296
/* the names are different (ZXDG_TOPLEVEL_V6_RESIZE_EDGE_* vs
WL_SHELL_SURFACE_RESIZE_*), but the values are the same. */
const uint32_t *directions_zxdg = directions_wl;
297
298
switch (rc) {
case SDL_HITTEST_DRAGGABLE:
Jun 25, 2018
Jun 25, 2018
299
300
301
if (input->display->shell.xdg) {
xdg_toplevel_move(window_data->shell_surface.xdg.roleobj.toplevel, input->seat, serial);
} else if (input->display->shell.zxdg) {
Feb 7, 2018
Feb 7, 2018
302
303
304
305
zxdg_toplevel_v6_move(window_data->shell_surface.zxdg.roleobj.toplevel, input->seat, serial);
} else {
wl_shell_surface_move(window_data->shell_surface.wl, input->seat, serial);
}
306
307
308
309
310
311
312
313
314
315
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:
Jun 25, 2018
Jun 25, 2018
316
317
318
if (input->display->shell.xdg) {
xdg_toplevel_resize(window_data->shell_surface.xdg.roleobj.toplevel, input->seat, serial, directions_zxdg[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
} else if (input->display->shell.zxdg) {
Feb 7, 2018
Feb 7, 2018
319
320
321
322
zxdg_toplevel_v6_resize(window_data->shell_surface.zxdg.roleobj.toplevel, input->seat, serial, directions_zxdg[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
} else {
wl_shell_surface_resize(window_data->shell_surface.wl, input->seat, serial, directions_wl[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
}
323
324
325
326
327
328
329
330
331
332
return SDL_TRUE;
default: return SDL_FALSE;
}
}
return SDL_FALSE;
}
static void
Sep 1, 2016
Sep 1, 2016
333
334
pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
335
336
337
338
{
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_button_state state = state_w;
uint32_t sdl_button;
Aug 15, 2017
Aug 15, 2017
339
340
341
342
343
if (input->pointer_focus) {
switch (button) {
case BTN_LEFT:
sdl_button = SDL_BUTTON_LEFT;
Sep 1, 2016
Sep 1, 2016
344
if (ProcessHitTest(input, serial)) {
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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;
}
Nov 6, 2016
Nov 6, 2016
363
364
Wayland_data_device_set_serial(input->data_device, serial);
365
366
367
368
369
370
371
SDL_SendMouseButton(window->sdlwindow, 0,
state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
}
}
static void
Sep 1, 2016
Sep 1, 2016
372
373
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
374
375
{
struct SDL_WaylandInput *input = data;
Sep 1, 2016
Sep 1, 2016
376
377
378
379
380
pointer_handle_button_common(input, serial, time, button, state_w);
}
static void
Feb 3, 2020
Feb 3, 2020
381
382
pointer_handle_axis_common_v1(struct SDL_WaylandInput *input,
uint32_t time, uint32_t axis, wl_fixed_t value)
Sep 1, 2016
Sep 1, 2016
383
{
384
385
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_axis a = axis;
Aug 15, 2017
Aug 15, 2017
386
float x, y;
387
388
389
390
391
if (input->pointer_focus) {
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
x = 0;
Jan 3, 2018
Jan 3, 2018
392
y = 0 - (float)wl_fixed_to_double(value);
393
394
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
Jan 3, 2018
Jan 3, 2018
395
x = 0 - (float)wl_fixed_to_double(value);
396
397
398
399
400
401
402
403
404
405
y = 0;
break;
default:
return;
}
SDL_SendMouseWheel(window->sdlwindow, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
}
}
Feb 3, 2020
Feb 3, 2020
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
static void
pointer_handle_axis_common(struct SDL_WaylandInput *input, SDL_bool discrete,
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:
if (discrete) {
/* this is a discrete axis event so we process it and flag
* to ignore future continuous axis events in this frame */
input->pointer_curr_axis_info.is_y_discrete = SDL_TRUE;
} else if(input->pointer_curr_axis_info.is_y_discrete) {
/* this is a continuous axis event and we have already
* processed a discrete axis event before so we ignore it */
break;
}
input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
if (discrete) {
/* this is a discrete axis event so we process it and flag
* to ignore future continuous axis events in this frame */
input->pointer_curr_axis_info.is_x_discrete = SDL_TRUE;
} else if(input->pointer_curr_axis_info.is_x_discrete) {
/* this is a continuous axis event and we have already
* processed a discrete axis event before so we ignore it */
break;
}
input->pointer_curr_axis_info.x = 0 - (float)wl_fixed_to_double(value);
break;
}
}
}
Sep 1, 2016
Sep 1, 2016
442
443
444
445
446
447
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;
Apr 7, 2020
Apr 7, 2020
448
if(wl_seat_get_version(input->seat) >= 5)
Feb 3, 2020
Feb 3, 2020
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
pointer_handle_axis_common(input, SDL_FALSE, 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 = input->pointer_curr_axis_info.x, y = input->pointer_curr_axis_info.y;
/* clear pointer_curr_axis_info for next frame */
memset(&input->pointer_curr_axis_info, 0, sizeof input->pointer_curr_axis_info);
if(x == 0.0f && y == 0.0f)
return;
else
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, SDL_TRUE, axis, wl_fixed_from_int(discrete));
Sep 1, 2016
Sep 1, 2016
491
492
}
Feb 3, 2020
Feb 3, 2020
493
494
495
496
497
498
499
static const struct wl_pointer_listener pointer_listener = {
pointer_handle_enter,
pointer_handle_leave,
pointer_handle_motion,
pointer_handle_button,
pointer_handle_axis,
Feb 3, 2020
Feb 3, 2020
500
501
502
503
pointer_handle_frame, // Version 5
pointer_handle_axis_source, // Version 5
pointer_handle_axis_stop, // Version 5
pointer_handle_axis_discrete, // Version 5
Aug 21, 2017
Aug 21, 2017
506
507
508
509
510
static void
touch_handler_down(void *data, struct wl_touch *touch, unsigned int serial,
unsigned int timestamp, struct wl_surface *surface,
int id, wl_fixed_t fx, wl_fixed_t fy)
{
Dec 5, 2018
Dec 5, 2018
511
512
513
514
515
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
const double dblx = wl_fixed_to_double(fx);
const double dbly = wl_fixed_to_double(fy);
const float x = dblx / window_data->sdlwindow->w;
const float y = dbly / window_data->sdlwindow->h;
Aug 21, 2017
Aug 21, 2017
516
Dec 5, 2018
Dec 5, 2018
517
touch_add(id, x, y, surface);
Aug 21, 2017
Aug 21, 2017
518
Aug 1, 2019
Aug 1, 2019
519
SDL_SendTouch(1, (SDL_FingerID)id, window_data->sdlwindow, SDL_TRUE, x, y, 1.0f);
Aug 21, 2017
Aug 21, 2017
520
521
522
523
524
525
526
}
static void
touch_handler_up(void *data, struct wl_touch *touch, unsigned int serial,
unsigned int timestamp, int id)
{
float x = 0, y = 0;
Aug 1, 2019
Aug 1, 2019
527
528
struct wl_surface *surface = NULL;
SDL_Window *window = NULL;
Aug 21, 2017
Aug 21, 2017
529
Aug 1, 2019
Aug 1, 2019
530
531
532
533
534
535
536
537
touch_del(id, &x, &y, &surface);
if (surface) {
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
window = window_data->sdlwindow;
}
SDL_SendTouch(1, (SDL_FingerID)id, window, SDL_FALSE, x, y, 0.0f);
Aug 21, 2017
Aug 21, 2017
538
539
540
541
542
543
}
static void
touch_handler_motion(void *data, struct wl_touch *touch, unsigned int timestamp,
int id, wl_fixed_t fx, wl_fixed_t fy)
{
Dec 5, 2018
Dec 5, 2018
544
545
546
547
548
549
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(touch_surface(id));
const double dblx = wl_fixed_to_double(fx);
const double dbly = wl_fixed_to_double(fy);
const float x = dblx / window_data->sdlwindow->w;
const float y = dbly / window_data->sdlwindow->h;
Aug 21, 2017
Aug 21, 2017
550
touch_update(id, x, y);
Aug 1, 2019
Aug 1, 2019
551
SDL_SendTouchMotion(1, (SDL_FingerID)id, window_data->sdlwindow, x, y, 1.0f);
Aug 21, 2017
Aug 21, 2017
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
}
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,
Sep 29, 2017
Sep 29, 2017
571
572
573
touch_handler_cancel,
NULL, /* shape */
NULL, /* orientation */
Aug 21, 2017
Aug 21, 2017
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
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;
if (!data) {
close(fd);
return;
}
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (map_str == MAP_FAILED) {
close(fd);
return;
}
input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
map_str,
XKB_KEYMAP_FORMAT_TEXT_V1,
0);
munmap(map_str, size);
close(fd);
if (!input->xkb.keymap) {
fprintf(stderr, "failed to compile keymap\n");
return;
}
input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
if (!input->xkb.state) {
fprintf(stderr, "failed to create XKB state\n");
WAYLAND_xkb_keymap_unref(input->xkb.keymap);
input->xkb.keymap = NULL;
return;
}
}
static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface,
struct wl_array *keys)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window;
if (!surface) {
/* enter event for a window we've just destroyed */
return;
}
Aug 15, 2017
Aug 15, 2017
632
633
634
635
window = wl_surface_get_user_data(surface);
if (window) {
Mar 2, 2016
Mar 2, 2016
636
637
input->keyboard_focus = window;
window->keyboard_device = input;
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
SDL_SetKeyboardFocus(window->sdlwindow);
}
}
static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface)
{
SDL_SetKeyboardFocus(NULL);
}
static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key,
uint32_t state_w)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window = input->keyboard_focus;
enum wl_keyboard_key_state state = state_w;
const xkb_keysym_t *syms;
uint32_t scancode;
char text[8];
int size;
if (key < SDL_arraysize(xfree86_scancode_table2)) {
scancode = xfree86_scancode_table2[key];
// TODO when do we get WL_KEYBOARD_KEY_STATE_REPEAT?
if (scancode != SDL_SCANCODE_UNKNOWN)
SDL_SendKeyboardKey(state == WL_KEYBOARD_KEY_STATE_PRESSED ?
SDL_PRESSED : SDL_RELEASED, scancode);
}
if (!window || window->keyboard_device != input || !input->xkb.state)
return;
// TODO can this happen?
if (WAYLAND_xkb_state_key_get_syms(input->xkb.state, key + 8, &syms) != 1)
return;
if (state) {
size = WAYLAND_xkb_keysym_to_utf8(syms[0], text, sizeof text);
if (size > 0) {
text[size] = 0;
Nov 6, 2016
Nov 6, 2016
683
684
685
Wayland_data_device_set_serial(input->data_device, serial);
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
SDL_SendKeyboardText(text);
}
}
}
static void
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t mods_depressed,
uint32_t mods_latched, uint32_t mods_locked,
uint32_t group)
{
struct SDL_WaylandInput *input = data;
WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
mods_locked, 0, 0, group);
}
Feb 3, 2020
Feb 3, 2020
703
704
705
706
707
708
709
static void
keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
int32_t rate, int32_t delay)
{
/* unimplemented */
}
710
711
712
713
714
715
static const struct wl_keyboard_listener keyboard_listener = {
keyboard_handle_keymap,
keyboard_handle_enter,
keyboard_handle_leave,
keyboard_handle_key,
keyboard_handle_modifiers,
Feb 3, 2020
Feb 3, 2020
716
keyboard_handle_repeat_info, // Version 4
717
718
719
720
721
722
723
724
725
726
};
static void
seat_handle_capabilities(void *data, struct wl_seat *seat,
enum wl_seat_capability caps)
{
struct SDL_WaylandInput *input = data;
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
input->pointer = wl_seat_get_pointer(seat);
Feb 3, 2020
Feb 3, 2020
727
memset(&input->pointer_curr_axis_info, 0, sizeof input->pointer_curr_axis_info);
728
729
730
731
732
733
734
input->display->pointer = input->pointer;
wl_pointer_set_user_data(input->pointer, input);
wl_pointer_add_listener(input->pointer, &pointer_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
wl_pointer_destroy(input->pointer);
input->pointer = NULL;
Jan 3, 2018
Jan 3, 2018
735
input->display->pointer = NULL;
Aug 21, 2017
Aug 21, 2017
738
if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
Nov 10, 2018
Nov 10, 2018
739
SDL_AddTouch(1, SDL_TOUCH_DEVICE_DIRECT, "wayland_touch");
Aug 21, 2017
Aug 21, 2017
740
741
742
743
744
745
746
747
748
749
input->touch = wl_seat_get_touch(seat);
wl_touch_set_user_data(input->touch, input);
wl_touch_add_listener(input->touch, &touch_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
SDL_DelTouch(1);
wl_touch_destroy(input->touch);
input->touch = NULL;
}
750
751
752
753
754
755
756
757
758
759
760
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
input->keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_set_user_data(input->keyboard, input);
wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
wl_keyboard_destroy(input->keyboard);
input->keyboard = NULL;
}
}
Feb 3, 2020
Feb 3, 2020
761
762
763
764
765
766
static void
seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
{
/* unimplemented */
}
767
768
static const struct wl_seat_listener seat_listener = {
seat_handle_capabilities,
Feb 3, 2020
Feb 3, 2020
769
seat_handle_name, // Version 2
Nov 6, 2016
Nov 6, 2016
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
static void
data_source_handle_target(void *data, struct wl_data_source *wl_data_source,
const char *mime_type)
{
}
static void
data_source_handle_send(void *data, struct wl_data_source *wl_data_source,
const char *mime_type, int32_t fd)
{
Wayland_data_source_send((SDL_WaylandDataSource *)data, mime_type, fd);
}
static void
data_source_handle_cancelled(void *data, struct wl_data_source *wl_data_source)
{
Wayland_data_source_destroy(data);
}
static void
data_source_handle_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source)
{
}
static void
data_source_handle_dnd_finished(void *data, struct wl_data_source *wl_data_source)
{
}
static void
data_source_handle_action(void *data, struct wl_data_source *wl_data_source,
uint32_t dnd_action)
{
}
static const struct wl_data_source_listener data_source_listener = {
data_source_handle_target,
data_source_handle_send,
data_source_handle_cancelled,
data_source_handle_dnd_drop_performed, // Version 3
data_source_handle_dnd_finished, // Version 3
data_source_handle_action, // Version 3
};
SDL_WaylandDataSource*
Wayland_data_source_create(_THIS)
{
SDL_WaylandDataSource *data_source = NULL;
SDL_VideoData *driver_data = NULL;
struct wl_data_source *id = NULL;
if (_this == NULL || _this->driverdata == NULL) {
SDL_SetError("Video driver uninitialized");
} else {
driver_data = _this->driverdata;
if (driver_data->data_device_manager != NULL) {
id = wl_data_device_manager_create_data_source(
driver_data->data_device_manager);
}
if (id == NULL) {
SDL_SetError("Wayland unable to create data source");
} else {
data_source = SDL_calloc(1, sizeof *data_source);
if (data_source == NULL) {
SDL_OutOfMemory();
wl_data_source_destroy(id);
} else {
WAYLAND_wl_list_init(&(data_source->mimes));
data_source->source = id;
wl_data_source_set_user_data(id, data_source);
wl_data_source_add_listener(id, &data_source_listener,
data_source);
}
}
}
return data_source;
}
static void
data_offer_handle_offer(void *data, struct wl_data_offer *wl_data_offer,
const char *mime_type)
{
SDL_WaylandDataOffer *offer = data;
Wayland_data_offer_add_mime(offer, mime_type);
}
static void
data_offer_handle_source_actions(void *data, struct wl_data_offer *wl_data_offer,
uint32_t source_actions)
{
}
static void
data_offer_handle_actions(void *data, struct wl_data_offer *wl_data_offer,
uint32_t dnd_action)
{
}
static const struct wl_data_offer_listener data_offer_listener = {
data_offer_handle_offer,
data_offer_handle_source_actions, // Version 3
data_offer_handle_actions, // Version 3
};
static void
data_device_handle_data_offer(void *data, struct wl_data_device *wl_data_device,
Sep 24, 2018
Sep 24, 2018
880
struct wl_data_offer *id)
Nov 6, 2016
Nov 6, 2016
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
{
SDL_WaylandDataOffer *data_offer = NULL;
data_offer = SDL_calloc(1, sizeof *data_offer);
if (data_offer == NULL) {
SDL_OutOfMemory();
} else {
data_offer->offer = id;
data_offer->data_device = data;
WAYLAND_wl_list_init(&(data_offer->mimes));
wl_data_offer_set_user_data(id, data_offer);
wl_data_offer_add_listener(id, &data_offer_listener, data_offer);
}
}
static void
data_device_handle_enter(void *data, struct wl_data_device *wl_data_device,
Sep 24, 2018
Sep 24, 2018
898
uint32_t serial, struct wl_surface *surface,
Nov 6, 2016
Nov 6, 2016
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *id)
{
SDL_WaylandDataDevice *data_device = data;
SDL_bool has_mime = SDL_FALSE;
uint32_t dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
data_device->drag_serial = serial;
if (id != NULL) {
data_device->drag_offer = wl_data_offer_get_user_data(id);
/* TODO: SDL Support more mime types */
has_mime = Wayland_data_offer_has_mime(
data_device->drag_offer, FILE_MIME);
/* If drag_mime is NULL this will decline the offer */
wl_data_offer_accept(id, serial,
(has_mime == SDL_TRUE) ? FILE_MIME : NULL);
/* SDL only supports "copy" style drag and drop */
if (has_mime == SDL_TRUE) {
dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
}
Dec 30, 2019
Dec 30, 2019
922
923
924
925
if (wl_data_offer_get_version(data_device->drag_offer->offer) >= 3) {
wl_data_offer_set_actions(data_device->drag_offer->offer,
dnd_action, dnd_action);
}
Nov 6, 2016
Nov 6, 2016
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
}
}
static void
data_device_handle_leave(void *data, struct wl_data_device *wl_data_device)
{
SDL_WaylandDataDevice *data_device = data;
SDL_WaylandDataOffer *offer = NULL;
if (data_device->selection_offer != NULL) {
data_device->selection_offer = NULL;
Wayland_data_offer_destroy(offer);
}
}
static void
data_device_handle_motion(void *data, struct wl_data_device *wl_data_device,
Sep 24, 2018
Sep 24, 2018
943
uint32_t time, wl_fixed_t x, wl_fixed_t y)
Nov 6, 2016
Nov 6, 2016
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
{
}
static void
data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
{
SDL_WaylandDataDevice *data_device = data;
void *buffer = NULL;
size_t length = 0;
const char *current_uri = NULL;
const char *last_char = NULL;
char *current_char = NULL;
if (data_device->drag_offer != NULL) {
/* TODO: SDL Support more mime types */
buffer = Wayland_data_offer_receive(data_device->drag_offer,
&length, FILE_MIME, SDL_FALSE);
/* uri-list */
current_uri = (const char *)buffer;
last_char = (const char *)buffer + length;
for (current_char = buffer; current_char < last_char; ++current_char) {
if (*current_char == '\n' || *current_char == 0) {
if (*current_uri != 0 && *current_uri != '#') {
*current_char = 0;
SDL_SendDropFile(NULL, current_uri);
}
current_uri = (const char *)current_char + 1;
}
}
SDL_free(buffer);
}
}
static void
data_device_handle_selection(void *data, struct wl_data_device *wl_data_device,
Sep 24, 2018
Sep 24, 2018
982
struct wl_data_offer *id)
Nov 6, 2016
Nov 6, 2016
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
SDL_WaylandDataDevice *data_device = data;
SDL_WaylandDataOffer *offer = NULL;
if (id != NULL) {
offer = wl_data_offer_get_user_data(id);
}
if (data_device->selection_offer != offer) {
Wayland_data_offer_destroy(data_device->selection_offer);
data_device->selection_offer = offer;
}
SDL_SendClipboardUpdate();
}
static const struct wl_data_device_listener data_device_listener = {
data_device_handle_data_offer,