Skip to content

Latest commit

 

History

History
404 lines (334 loc) · 11.3 KB

SDL_waylandevents.c

File metadata and controls

404 lines (334 loc) · 11.3 KB
 
Dec 14, 2013
Dec 14, 2013
1
2
/*
Simple DirectMedia Layer
Feb 2, 2014
Feb 2, 2014
3
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
Dec 14, 2013
Dec 14, 2013
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.
*/
Jan 9, 2014
Jan 9, 2014
22
#include "../../SDL_internal.h"
Dec 14, 2013
Dec 14, 2013
23
Jan 20, 2014
Jan 20, 2014
24
25
#if SDL_VIDEO_DRIVER_WAYLAND
Dec 14, 2013
Dec 14, 2013
26
27
28
29
30
31
32
33
34
35
36
#include "SDL_stdinc.h"
#include "SDL_assert.h"
#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"
Jan 9, 2014
Jan 9, 2014
37
38
#include "SDL_waylanddyn.h"
Dec 14, 2013
Dec 14, 2013
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
#include <linux/input.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <poll.h>
#include <errno.h>
#include <unistd.h>
#include <xkbcommon/xkbcommon.h>
struct SDL_WaylandInput {
SDL_VideoData *display;
struct wl_seat *seat;
struct wl_pointer *pointer;
struct wl_keyboard *keyboard;
SDL_WindowData *pointer_focus;
SDL_WindowData *keyboard_focus;
struct {
struct xkb_keymap *keymap;
struct xkb_state *state;
} xkb;
};
void
Wayland_PumpEvents(_THIS)
{
SDL_VideoData *d = _this->driverdata;
struct pollfd pfd[1];
Jan 9, 2014
Jan 9, 2014
67
pfd[0].fd = WAYLAND_wl_display_get_fd(d->display);
Dec 14, 2013
Dec 14, 2013
68
69
70
71
pfd[0].events = POLLIN;
poll(pfd, 1, 0);
if (pfd[0].revents & POLLIN)
Jan 9, 2014
Jan 9, 2014
72
WAYLAND_wl_display_dispatch(d->display);
Dec 14, 2013
Dec 14, 2013
73
else
Jan 9, 2014
Jan 9, 2014
74
WAYLAND_wl_display_dispatch_pending(d->display);
Dec 14, 2013
Dec 14, 2013
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}
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;
}
Jan 28, 2014
Jan 28, 2014
89
90
91
92
93
94
95
96
97
98
99
100
101
/* 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;
SDL_SetMouseFocus(window->sdlwindow);
}
Dec 14, 2013
Dec 14, 2013
102
103
104
105
106
107
108
109
}
static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
struct SDL_WaylandInput *input = data;
Jan 28, 2014
Jan 28, 2014
110
111
112
113
if (input->pointer_focus) {
SDL_SetMouseFocus(NULL);
input->pointer_focus = NULL;
}
Dec 14, 2013
Dec 14, 2013
114
115
116
117
118
119
120
121
122
123
}
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;
int sx = wl_fixed_to_int(sx_w);
int sy = wl_fixed_to_int(sy_w);
Jan 28, 2014
Jan 28, 2014
124
125
126
if (input->pointer_focus) {
SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
}
Dec 14, 2013
Dec 14, 2013
127
128
129
130
131
132
133
134
135
136
}
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;
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_button_state state = state_w;
uint32_t sdl_button;
Jan 28, 2014
Jan 28, 2014
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
if (input->pointer_focus) {
switch (button) {
case BTN_LEFT:
sdl_button = SDL_BUTTON_LEFT;
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;
}
Dec 14, 2013
Dec 14, 2013
158
Jan 28, 2014
Jan 28, 2014
159
160
SDL_SendMouseButton(window->sdlwindow, 0,
state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
Dec 14, 2013
Dec 14, 2013
161
162
163
164
165
166
167
168
169
170
171
172
}
}
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;
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_axis a = axis;
int x, y;
Jan 28, 2014
Jan 28, 2014
173
174
175
176
177
178
179
180
181
182
183
184
185
if (input->pointer_focus) {
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
x = 0;
y = wl_fixed_to_int(value);
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
x = wl_fixed_to_int(value);
y = 0;
break;
default:
return;
}
Dec 14, 2013
Dec 14, 2013
186
Jan 28, 2014
Jan 28, 2014
187
188
SDL_SendMouseWheel(window->sdlwindow, 0, x, y);
}
Dec 14, 2013
Dec 14, 2013
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
}
static const struct wl_pointer_listener pointer_listener = {
pointer_handle_enter,
pointer_handle_leave,
pointer_handle_motion,
pointer_handle_button,
pointer_handle_axis,
};
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;
}
Jan 9, 2014
Jan 9, 2014
222
input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
Dec 14, 2013
Dec 14, 2013
223
224
225
226
227
228
229
230
231
232
233
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;
}
Jan 9, 2014
Jan 9, 2014
234
input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
Dec 14, 2013
Dec 14, 2013
235
236
if (!input->xkb.state) {
fprintf(stderr, "failed to create XKB state\n");
Jan 9, 2014
Jan 9, 2014
237
WAYLAND_xkb_keymap_unref(input->xkb.keymap);
Dec 14, 2013
Dec 14, 2013
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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 = wl_surface_get_user_data(surface);
input->keyboard_focus = window;
window->keyboard_device = input;
Jan 28, 2014
Jan 28, 2014
253
254
255
if (window) {
SDL_SetKeyboardFocus(window->sdlwindow);
}
Dec 14, 2013
Dec 14, 2013
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
}
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?
Jan 9, 2014
Jan 9, 2014
291
if (WAYLAND_xkb_state_key_get_syms(input->xkb.state, key + 8, &syms) != 1)
Dec 14, 2013
Dec 14, 2013
292
293
294
return;
if (state) {
Jan 9, 2014
Jan 9, 2014
295
size = WAYLAND_xkb_keysym_to_utf8(syms[0], text, sizeof text);
Dec 14, 2013
Dec 14, 2013
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
if (size > 0) {
text[size] = 0;
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;
Jan 9, 2014
Jan 9, 2014
312
WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
Dec 14, 2013
Dec 14, 2013
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
mods_locked, 0, 0, group);
}
static const struct wl_keyboard_listener keyboard_listener = {
keyboard_handle_keymap,
keyboard_handle_enter,
keyboard_handle_leave,
keyboard_handle_key,
keyboard_handle_modifiers,
};
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);
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;
}
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;
}
}
static const struct wl_seat_listener seat_listener = {
seat_handle_capabilities,
};
void
Wayland_display_add_input(SDL_VideoData *d, uint32_t id)
{
struct SDL_WaylandInput *input;
Jan 28, 2014
Jan 28, 2014
361
input = SDL_calloc(1, sizeof *input);
Dec 14, 2013
Dec 14, 2013
362
363
364
365
366
367
368
369
370
371
372
if (input == NULL)
return;
input->display = d;
input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
d->input = input;
wl_seat_add_listener(input->seat, &seat_listener, input);
wl_seat_set_user_data(input->seat, input);
Jan 9, 2014
Jan 9, 2014
373
WAYLAND_wl_display_flush(d->display);
Dec 14, 2013
Dec 14, 2013
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
}
void Wayland_display_destroy_input(SDL_VideoData *d)
{
struct SDL_WaylandInput *input = d->input;
if (!input)
return;
if (input->keyboard)
wl_keyboard_destroy(input->keyboard);
if (input->pointer)
wl_pointer_destroy(input->pointer);
if (input->seat)
wl_seat_destroy(input->seat);
if (input->xkb.state)
Jan 9, 2014
Jan 9, 2014
393
WAYLAND_xkb_state_unref(input->xkb.state);
Dec 14, 2013
Dec 14, 2013
394
395
if (input->xkb.keymap)
Jan 9, 2014
Jan 9, 2014
396
WAYLAND_xkb_keymap_unref(input->xkb.keymap);
Dec 14, 2013
Dec 14, 2013
397
Jan 28, 2014
Jan 28, 2014
398
SDL_free(input);
Dec 14, 2013
Dec 14, 2013
399
400
401
d->input = NULL;
}
Jan 20, 2014
Jan 20, 2014
402
403
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
Dec 14, 2013
Dec 14, 2013
404
/* vi: set ts=4 sw=4 expandtab: */