22 #include "../../SDL_internal.h" 24 #if SDL_VIDEO_DRIVER_WAYLAND 29 #include "../../events/SDL_sysevents.h" 30 #include "../../events/SDL_events_c.h" 31 #include "../../events/scancodes_xfree86.h" 39 #include <linux/input.h> 40 #include <sys/select.h> 45 #include <xkbcommon/xkbcommon.h> 47 struct SDL_WaylandInput {
51 struct wl_keyboard *keyboard;
60 struct xkb_keymap *keymap;
61 struct xkb_state *
state;
71 pfd[0].fd = WAYLAND_wl_display_get_fd(d->
display);
72 pfd[0].events = POLLIN;
75 if (pfd[0].revents & POLLIN)
76 WAYLAND_wl_display_dispatch(d->
display);
78 WAYLAND_wl_display_dispatch_pending(d->
display);
82 pointer_handle_enter(
void *
data,
struct wl_pointer *
pointer,
83 uint32_t serial,
struct wl_surface *surface,
84 wl_fixed_t sx_w, wl_fixed_t sy_w)
103 input->pointer_focus =
window;
109 pointer_handle_leave(
void *data,
struct wl_pointer *pointer,
110 uint32_t serial,
struct wl_surface *surface)
112 struct SDL_WaylandInput *input =
data;
114 if (input->pointer_focus) {
116 input->pointer_focus =
NULL;
121 pointer_handle_motion(
void *data,
struct wl_pointer *pointer,
122 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
124 struct SDL_WaylandInput *input =
data;
128 if (input->pointer_focus) {
129 const int sx = wl_fixed_to_int(sx_w);
130 const int sy = wl_fixed_to_int(sy_w);
136 ProcessHitTest(
struct SDL_WaylandInput *input,
uint32_t serial)
142 const SDL_Point point = { wl_fixed_to_int(input->sx_w), wl_fixed_to_int(input->sy_w) };
144 static const uint32_t directions[] = {
145 WL_SHELL_SURFACE_RESIZE_TOP_LEFT, WL_SHELL_SURFACE_RESIZE_TOP,
146 WL_SHELL_SURFACE_RESIZE_TOP_RIGHT, WL_SHELL_SURFACE_RESIZE_RIGHT,
147 WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT, WL_SHELL_SURFACE_RESIZE_BOTTOM,
148 WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT, WL_SHELL_SURFACE_RESIZE_LEFT
152 wl_shell_surface_move(window_data->
shell_surface, input->seat, serial);
174 pointer_handle_button(
void *data,
struct wl_pointer *pointer,
uint32_t serial,
177 struct SDL_WaylandInput *input =
data;
179 enum wl_pointer_button_state
state = state_w;
182 if (input->pointer_focus) {
186 if (ProcessHitTest(data, serial)) {
212 pointer_handle_axis(
void *data,
struct wl_pointer *pointer,
215 struct SDL_WaylandInput *input =
data;
217 enum wl_pointer_axis
a =
axis;
220 if (input->pointer_focus) {
222 case WL_POINTER_AXIS_VERTICAL_SCROLL:
224 y = wl_fixed_to_int(value);
226 case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
227 x = wl_fixed_to_int(value);
238 static const struct wl_pointer_listener pointer_listener = {
239 pointer_handle_enter,
240 pointer_handle_leave,
241 pointer_handle_motion,
242 pointer_handle_button,
247 keyboard_handle_keymap(
void *data,
struct wl_keyboard *keyboard,
250 struct SDL_WaylandInput *input =
data;
258 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
263 map_str = mmap(
NULL, size, PROT_READ, MAP_SHARED, fd, 0);
264 if (map_str == MAP_FAILED) {
269 input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
271 XKB_KEYMAP_FORMAT_TEXT_V1,
273 munmap(map_str, size);
276 if (!input->xkb.keymap) {
277 fprintf(stderr,
"failed to compile keymap\n");
281 input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
282 if (!input->xkb.state) {
283 fprintf(stderr,
"failed to create XKB state\n");
284 WAYLAND_xkb_keymap_unref(input->xkb.keymap);
285 input->xkb.keymap =
NULL;
291 keyboard_handle_enter(
void *data,
struct wl_keyboard *keyboard,
292 uint32_t serial,
struct wl_surface *surface,
293 struct wl_array *keys)
295 struct SDL_WaylandInput *input =
data;
303 window = wl_surface_get_user_data(surface);
305 input->keyboard_focus =
window;
313 keyboard_handle_leave(
void *data,
struct wl_keyboard *keyboard,
314 uint32_t serial,
struct wl_surface *surface)
320 keyboard_handle_key(
void *data,
struct wl_keyboard *keyboard,
324 struct SDL_WaylandInput *input =
data;
326 enum wl_keyboard_key_state state = state_w;
327 const xkb_keysym_t *syms;
345 if (WAYLAND_xkb_state_key_get_syms(input->xkb.state, key + 8, &syms) != 1)
349 size = WAYLAND_xkb_keysym_to_utf8(syms[0], text,
sizeof text);
359 keyboard_handle_modifiers(
void *data,
struct wl_keyboard *keyboard,
364 struct SDL_WaylandInput *input =
data;
366 WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
367 mods_locked, 0, 0, group);
370 static const struct wl_keyboard_listener keyboard_listener = {
371 keyboard_handle_keymap,
372 keyboard_handle_enter,
373 keyboard_handle_leave,
375 keyboard_handle_modifiers,
379 seat_handle_capabilities(
void *data,
struct wl_seat *seat,
380 enum wl_seat_capability caps)
382 struct SDL_WaylandInput *input =
data;
384 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
385 input->pointer = wl_seat_get_pointer(seat);
386 input->display->pointer = input->pointer;
387 wl_pointer_set_user_data(input->pointer, input);
388 wl_pointer_add_listener(input->pointer, &pointer_listener,
390 }
else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
391 wl_pointer_destroy(input->pointer);
392 input->pointer =
NULL;
395 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
396 input->keyboard = wl_seat_get_keyboard(seat);
397 wl_keyboard_set_user_data(input->keyboard, input);
398 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
400 }
else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
401 wl_keyboard_destroy(input->keyboard);
402 input->keyboard =
NULL;
406 static const struct wl_seat_listener seat_listener = {
407 seat_handle_capabilities,
413 struct SDL_WaylandInput *
input;
420 input->seat = wl_registry_bind(d->
registry,
id, &wl_seat_interface, 1);
421 input->sx_w = wl_fixed_from_int(0);
422 input->sy_w = wl_fixed_from_int(0);
425 wl_seat_add_listener(input->seat, &seat_listener, input);
426 wl_seat_set_user_data(input->seat, input);
428 WAYLAND_wl_display_flush(d->
display);
433 struct SDL_WaylandInput *input = d->
input;
439 wl_keyboard_destroy(input->keyboard);
442 wl_pointer_destroy(input->pointer);
445 wl_seat_destroy(input->seat);
447 if (input->xkb.state)
448 WAYLAND_xkb_state_unref(input->xkb.state);
450 if (input->xkb.keymap)
451 WAYLAND_xkb_keymap_unref(input->xkb.keymap);
void SDL_SetKeyboardFocus(SDL_Window *window)
struct wl_display * display
The structure that defines a point.
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
static const SDL_Scancode xfree86_scancode_table2[]
void SDL_SetMouseFocus(SDL_Window *window)
struct SDL_WaylandInput * input
GLint GLint GLint GLint GLint x
GLsizei const void * pointer
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
static SDL_VideoDevice * _this
SDL_HitTestResult
Possible return values from the SDL_HitTest callback.
struct SDL_WaylandInput * keyboard_device
void * SDL_calloc(size_t nmemb, size_t size)
GLenum GLenum GLenum input
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 ** d
GLsizei const GLfloat * value
int SDL_SendKeyboardText(const char *text)
struct wl_shell_surface * shell_surface
#define SDL_BUTTON_MIDDLE
void Wayland_display_add_input(SDL_VideoData *d, uint32_t id)
static char text[MAX_TEXT_LENGTH]
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
GLint GLint GLint GLint GLint GLint y
void Wayland_display_destroy_input(SDL_VideoData *d)
void Wayland_PumpEvents(_THIS)
The type used to identify a window.
struct wl_registry * registry
#define SDL_arraysize(array)
int SDL_SendMouseWheel(SDL_Window *window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction)
GLboolean GLboolean GLboolean GLboolean a
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)