src/video/wayland/SDL_waylandevents.c
changeset 8062 4fc5f66d63cc
child 8104 2e4f1bd21196
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/wayland/SDL_waylandevents.c	Sat Dec 14 20:18:43 2013 -0300
     1.3 @@ -0,0 +1,381 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include "SDL_stdinc.h"
    1.28 +#include "SDL_assert.h"
    1.29 +
    1.30 +#include "../../events/SDL_sysevents.h"
    1.31 +#include "../../events/SDL_events_c.h"
    1.32 +#include "../../events/scancodes_xfree86.h"
    1.33 +
    1.34 +#include "SDL_waylandvideo.h"
    1.35 +#include "SDL_waylandevents_c.h"
    1.36 +#include "SDL_waylandwindow.h"
    1.37 +
    1.38 +#include <linux/input.h>
    1.39 +#include <sys/select.h>
    1.40 +#include <sys/mman.h>
    1.41 +#include <poll.h>
    1.42 +#include <errno.h>
    1.43 +#include <unistd.h>
    1.44 +#include <xkbcommon/xkbcommon.h>
    1.45 +
    1.46 +struct SDL_WaylandInput {
    1.47 +    SDL_VideoData *display;
    1.48 +    struct wl_seat *seat;
    1.49 +    struct wl_pointer *pointer;
    1.50 +    struct wl_keyboard *keyboard;
    1.51 +    SDL_WindowData *pointer_focus;
    1.52 +    SDL_WindowData *keyboard_focus;
    1.53 +
    1.54 +    struct {
    1.55 +        struct xkb_keymap *keymap;
    1.56 +        struct xkb_state *state;
    1.57 +    } xkb;
    1.58 +};
    1.59 +
    1.60 +void
    1.61 +Wayland_PumpEvents(_THIS)
    1.62 +{
    1.63 +    SDL_VideoData *d = _this->driverdata;
    1.64 +    struct pollfd pfd[1];
    1.65 +
    1.66 +    pfd[0].fd = wl_display_get_fd(d->display);
    1.67 +    pfd[0].events = POLLIN;
    1.68 +    poll(pfd, 1, 0);
    1.69 +
    1.70 +    if (pfd[0].revents & POLLIN)
    1.71 +        wl_display_dispatch(d->display);
    1.72 +    else
    1.73 +        wl_display_dispatch_pending(d->display);
    1.74 +}
    1.75 +
    1.76 +static void
    1.77 +pointer_handle_enter(void *data, struct wl_pointer *pointer,
    1.78 +                     uint32_t serial, struct wl_surface *surface,
    1.79 +                     wl_fixed_t sx_w, wl_fixed_t sy_w)
    1.80 +{
    1.81 +    struct SDL_WaylandInput *input = data;
    1.82 +    SDL_WindowData *window;
    1.83 +
    1.84 +    if (!surface) {
    1.85 +        /* enter event for a window we've just destroyed */
    1.86 +        return;
    1.87 +    }
    1.88 +
    1.89 +    input->pointer_focus = wl_surface_get_user_data(surface);
    1.90 +    window = input->pointer_focus;
    1.91 +    SDL_SetMouseFocus(window->sdlwindow);
    1.92 +}
    1.93 +
    1.94 +static void
    1.95 +pointer_handle_leave(void *data, struct wl_pointer *pointer,
    1.96 +                     uint32_t serial, struct wl_surface *surface)
    1.97 +{
    1.98 +    struct SDL_WaylandInput *input = data;
    1.99 +
   1.100 +    SDL_SetMouseFocus(NULL);
   1.101 +    input->pointer_focus = NULL;
   1.102 +}
   1.103 +
   1.104 +static void
   1.105 +pointer_handle_motion(void *data, struct wl_pointer *pointer,
   1.106 +                      uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
   1.107 +{
   1.108 +    struct SDL_WaylandInput *input = data;
   1.109 +    SDL_WindowData *window = input->pointer_focus;
   1.110 +    int sx = wl_fixed_to_int(sx_w);
   1.111 +    int sy = wl_fixed_to_int(sy_w);
   1.112 +
   1.113 +    SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
   1.114 +}
   1.115 +
   1.116 +static void
   1.117 +pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
   1.118 +                      uint32_t time, uint32_t button, uint32_t state_w)
   1.119 +{
   1.120 +    struct SDL_WaylandInput *input = data;
   1.121 +    SDL_WindowData *window = input->pointer_focus;
   1.122 +    enum wl_pointer_button_state state = state_w;
   1.123 +    uint32_t sdl_button;
   1.124 +
   1.125 +    switch (button) {
   1.126 +    case BTN_LEFT:
   1.127 +        sdl_button = SDL_BUTTON_LEFT;
   1.128 +        break;
   1.129 +    case BTN_MIDDLE:
   1.130 +        sdl_button = SDL_BUTTON_MIDDLE;
   1.131 +        break;
   1.132 +    case BTN_RIGHT:
   1.133 +        sdl_button = SDL_BUTTON_RIGHT;
   1.134 +        break;
   1.135 +    case BTN_SIDE:
   1.136 +        sdl_button = SDL_BUTTON_X1;
   1.137 +        break;
   1.138 +    case BTN_EXTRA:
   1.139 +        sdl_button = SDL_BUTTON_X2;
   1.140 +        break;
   1.141 +    default:
   1.142 +        return;
   1.143 +    }
   1.144 +
   1.145 +    SDL_SendMouseButton(window->sdlwindow, 0,
   1.146 +                        state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
   1.147 +}
   1.148 +
   1.149 +static void
   1.150 +pointer_handle_axis(void *data, struct wl_pointer *pointer,
   1.151 +                    uint32_t time, uint32_t axis, wl_fixed_t value)
   1.152 +{
   1.153 +    struct SDL_WaylandInput *input = data;
   1.154 +    SDL_WindowData *window = input->pointer_focus;
   1.155 +    enum wl_pointer_axis a = axis;
   1.156 +    int x, y;
   1.157 +
   1.158 +    switch (a) {
   1.159 +    case WL_POINTER_AXIS_VERTICAL_SCROLL:
   1.160 +        x = 0;
   1.161 +        y = wl_fixed_to_int(value);
   1.162 +        break;
   1.163 +    case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
   1.164 +        x = wl_fixed_to_int(value);
   1.165 +        y = 0;
   1.166 +        break;
   1.167 +    default:
   1.168 +        return;
   1.169 +    }
   1.170 +
   1.171 +    SDL_SendMouseWheel(window->sdlwindow, 0, x, y);
   1.172 +}
   1.173 +
   1.174 +static const struct wl_pointer_listener pointer_listener = {
   1.175 +    pointer_handle_enter,
   1.176 +    pointer_handle_leave,
   1.177 +    pointer_handle_motion,
   1.178 +    pointer_handle_button,
   1.179 +    pointer_handle_axis,
   1.180 +};
   1.181 +
   1.182 +static void
   1.183 +keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
   1.184 +                       uint32_t format, int fd, uint32_t size)
   1.185 +{
   1.186 +    struct SDL_WaylandInput *input = data;
   1.187 +    char *map_str;
   1.188 +
   1.189 +    if (!data) {
   1.190 +        close(fd);
   1.191 +        return;
   1.192 +    }
   1.193 +
   1.194 +    if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
   1.195 +        close(fd);
   1.196 +        return;
   1.197 +    }
   1.198 +
   1.199 +    map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
   1.200 +    if (map_str == MAP_FAILED) {
   1.201 +        close(fd);
   1.202 +        return;
   1.203 +    }
   1.204 +
   1.205 +    input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
   1.206 +                                                map_str,
   1.207 +                                                XKB_KEYMAP_FORMAT_TEXT_V1,
   1.208 +                                                0);
   1.209 +    munmap(map_str, size);
   1.210 +    close(fd);
   1.211 +
   1.212 +    if (!input->xkb.keymap) {
   1.213 +        fprintf(stderr, "failed to compile keymap\n");
   1.214 +        return;
   1.215 +    }
   1.216 +
   1.217 +    input->xkb.state = xkb_state_new(input->xkb.keymap);
   1.218 +    if (!input->xkb.state) {
   1.219 +        fprintf(stderr, "failed to create XKB state\n");
   1.220 +        xkb_map_unref(input->xkb.keymap);
   1.221 +        input->xkb.keymap = NULL;
   1.222 +        return;
   1.223 +    }
   1.224 +}
   1.225 +
   1.226 +static void
   1.227 +keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
   1.228 +                      uint32_t serial, struct wl_surface *surface,
   1.229 +                      struct wl_array *keys)
   1.230 +{
   1.231 +    struct SDL_WaylandInput *input = data;
   1.232 +    SDL_WindowData *window = wl_surface_get_user_data(surface);
   1.233 +
   1.234 +    input->keyboard_focus = window;
   1.235 +    window->keyboard_device = input;
   1.236 +    SDL_SetKeyboardFocus(window->sdlwindow);
   1.237 +}
   1.238 +
   1.239 +static void
   1.240 +keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
   1.241 +                      uint32_t serial, struct wl_surface *surface)
   1.242 +{
   1.243 +    SDL_SetKeyboardFocus(NULL);
   1.244 +}
   1.245 +
   1.246 +static void
   1.247 +keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
   1.248 +                    uint32_t serial, uint32_t time, uint32_t key,
   1.249 +                    uint32_t state_w)
   1.250 +{
   1.251 +    struct SDL_WaylandInput *input = data;
   1.252 +    SDL_WindowData *window = input->keyboard_focus;
   1.253 +    enum wl_keyboard_key_state state = state_w;
   1.254 +    const xkb_keysym_t *syms;
   1.255 +    uint32_t scancode;
   1.256 +    char text[8];
   1.257 +    int size;
   1.258 +
   1.259 +    if (key < SDL_arraysize(xfree86_scancode_table2)) {
   1.260 +        scancode = xfree86_scancode_table2[key];
   1.261 +
   1.262 +        // TODO when do we get WL_KEYBOARD_KEY_STATE_REPEAT?
   1.263 +        if (scancode != SDL_SCANCODE_UNKNOWN)
   1.264 +            SDL_SendKeyboardKey(state == WL_KEYBOARD_KEY_STATE_PRESSED ?
   1.265 +                                SDL_PRESSED : SDL_RELEASED, scancode);
   1.266 +    }
   1.267 +
   1.268 +    if (!window || window->keyboard_device != input || !input->xkb.state)
   1.269 +        return;
   1.270 +
   1.271 +    // TODO can this happen?
   1.272 +    if (xkb_key_get_syms(input->xkb.state, key + 8, &syms) != 1)
   1.273 +        return;
   1.274 +
   1.275 +    if (state) {
   1.276 +        size = xkb_keysym_to_utf8(syms[0], text, sizeof text);
   1.277 +
   1.278 +        if (size > 0) {
   1.279 +            text[size] = 0;
   1.280 +            SDL_SendKeyboardText(text);
   1.281 +        }
   1.282 +    }
   1.283 +}
   1.284 +
   1.285 +static void
   1.286 +keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
   1.287 +                          uint32_t serial, uint32_t mods_depressed,
   1.288 +                          uint32_t mods_latched, uint32_t mods_locked,
   1.289 +                          uint32_t group)
   1.290 +{
   1.291 +    struct SDL_WaylandInput *input = data;
   1.292 +
   1.293 +    xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
   1.294 +                          mods_locked, 0, 0, group);
   1.295 +}
   1.296 +
   1.297 +static const struct wl_keyboard_listener keyboard_listener = {
   1.298 +    keyboard_handle_keymap,
   1.299 +    keyboard_handle_enter,
   1.300 +    keyboard_handle_leave,
   1.301 +    keyboard_handle_key,
   1.302 +    keyboard_handle_modifiers,
   1.303 +};
   1.304 +
   1.305 +static void
   1.306 +seat_handle_capabilities(void *data, struct wl_seat *seat,
   1.307 +                         enum wl_seat_capability caps)
   1.308 +{
   1.309 +    struct SDL_WaylandInput *input = data;
   1.310 +
   1.311 +    if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
   1.312 +        input->pointer = wl_seat_get_pointer(seat);
   1.313 +        input->display->pointer = input->pointer;
   1.314 +        wl_pointer_set_user_data(input->pointer, input);
   1.315 +        wl_pointer_add_listener(input->pointer, &pointer_listener,
   1.316 +                                input);
   1.317 +    } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
   1.318 +        wl_pointer_destroy(input->pointer);
   1.319 +        input->pointer = NULL;
   1.320 +    }
   1.321 +
   1.322 +    if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
   1.323 +        input->keyboard = wl_seat_get_keyboard(seat);
   1.324 +        wl_keyboard_set_user_data(input->keyboard, input);
   1.325 +        wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
   1.326 +                                 input);
   1.327 +    } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
   1.328 +        wl_keyboard_destroy(input->keyboard);
   1.329 +        input->keyboard = NULL;
   1.330 +    }
   1.331 +}
   1.332 +
   1.333 +static const struct wl_seat_listener seat_listener = {
   1.334 +    seat_handle_capabilities,
   1.335 +};
   1.336 +
   1.337 +void
   1.338 +Wayland_display_add_input(SDL_VideoData *d, uint32_t id)
   1.339 +{
   1.340 +    struct SDL_WaylandInput *input;
   1.341 +
   1.342 +    input = malloc(sizeof *input);
   1.343 +    if (input == NULL)
   1.344 +        return;
   1.345 +
   1.346 +    memset(input, 0, sizeof *input);
   1.347 +    input->display = d;
   1.348 +    input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
   1.349 +
   1.350 +    d->input = input;
   1.351 +
   1.352 +    wl_seat_add_listener(input->seat, &seat_listener, input);
   1.353 +    wl_seat_set_user_data(input->seat, input);
   1.354 +
   1.355 +    wayland_schedule_write(d);
   1.356 +}
   1.357 +
   1.358 +void Wayland_display_destroy_input(SDL_VideoData *d)
   1.359 +{
   1.360 +    struct SDL_WaylandInput *input = d->input;
   1.361 +
   1.362 +    if (!input)
   1.363 +        return;
   1.364 +
   1.365 +    if (input->keyboard)
   1.366 +        wl_keyboard_destroy(input->keyboard);
   1.367 +
   1.368 +    if (input->pointer)
   1.369 +        wl_pointer_destroy(input->pointer);
   1.370 +
   1.371 +    if (input->seat)
   1.372 +        wl_seat_destroy(input->seat);
   1.373 +
   1.374 +    if (input->xkb.state)
   1.375 +        xkb_state_unref(input->xkb.state);
   1.376 +
   1.377 +    if (input->xkb.keymap)
   1.378 +        xkb_map_unref(input->xkb.keymap);
   1.379 +
   1.380 +    free(input);
   1.381 +    d->input = NULL;
   1.382 +}
   1.383 +
   1.384 +/* vi: set ts=4 sw=4 expandtab: */