src/video/wayland/SDL_waylandevents.c
author Arne Janbu <arnej@arnej.de>
Sat, 10 Jan 2015 13:47:37 +0100
changeset 9362 dfd8202eb9a7
parent 9257 6f41196c2d6b
child 9554 879f71e1478b
permissions -rw-r--r--
Fix build on Linux when wayland is enabled

Bug: https://bugzilla.libsdl.org/show_bug.cgi?id=2838
gabomdq@8062
     1
/*
gabomdq@8062
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
gabomdq@8062
     4
gabomdq@8062
     5
  This software is provided 'as-is', without any express or implied
gabomdq@8062
     6
  warranty.  In no event will the authors be held liable for any damages
gabomdq@8062
     7
  arising from the use of this software.
gabomdq@8062
     8
gabomdq@8062
     9
  Permission is granted to anyone to use this software for any purpose,
gabomdq@8062
    10
  including commercial applications, and to alter it and redistribute it
gabomdq@8062
    11
  freely, subject to the following restrictions:
gabomdq@8062
    12
gabomdq@8062
    13
  1. The origin of this software must not be misrepresented; you must not
gabomdq@8062
    14
     claim that you wrote the original software. If you use this software
gabomdq@8062
    15
     in a product, an acknowledgment in the product documentation would be
gabomdq@8062
    16
     appreciated but is not required.
gabomdq@8062
    17
  2. Altered source versions must be plainly marked as such, and must not be
gabomdq@8062
    18
     misrepresented as being the original software.
gabomdq@8062
    19
  3. This notice may not be removed or altered from any source distribution.
gabomdq@8062
    20
*/
gabomdq@8062
    21
gabomdq@8104
    22
#include "../../SDL_internal.h"
gabomdq@8062
    23
icculus@8116
    24
#if SDL_VIDEO_DRIVER_WAYLAND
icculus@8116
    25
gabomdq@8062
    26
#include "SDL_stdinc.h"
gabomdq@8062
    27
#include "SDL_assert.h"
gabomdq@8062
    28
gabomdq@8062
    29
#include "../../events/SDL_sysevents.h"
gabomdq@8062
    30
#include "../../events/SDL_events_c.h"
gabomdq@8062
    31
#include "../../events/scancodes_xfree86.h"
gabomdq@8062
    32
gabomdq@8062
    33
#include "SDL_waylandvideo.h"
gabomdq@8062
    34
#include "SDL_waylandevents_c.h"
gabomdq@8062
    35
#include "SDL_waylandwindow.h"
gabomdq@8062
    36
gabomdq@8104
    37
#include "SDL_waylanddyn.h"
gabomdq@8104
    38
gabomdq@8062
    39
#include <linux/input.h>
gabomdq@8062
    40
#include <sys/select.h>
gabomdq@8062
    41
#include <sys/mman.h>
gabomdq@8062
    42
#include <poll.h>
gabomdq@8062
    43
#include <errno.h>
gabomdq@8062
    44
#include <unistd.h>
gabomdq@8062
    45
#include <xkbcommon/xkbcommon.h>
gabomdq@8062
    46
gabomdq@8062
    47
struct SDL_WaylandInput {
gabomdq@8062
    48
    SDL_VideoData *display;
gabomdq@8062
    49
    struct wl_seat *seat;
gabomdq@8062
    50
    struct wl_pointer *pointer;
gabomdq@8062
    51
    struct wl_keyboard *keyboard;
gabomdq@8062
    52
    SDL_WindowData *pointer_focus;
gabomdq@8062
    53
    SDL_WindowData *keyboard_focus;
gabomdq@8062
    54
gabomdq@8062
    55
    struct {
gabomdq@8062
    56
        struct xkb_keymap *keymap;
gabomdq@8062
    57
        struct xkb_state *state;
gabomdq@8062
    58
    } xkb;
gabomdq@8062
    59
};
gabomdq@8062
    60
gabomdq@8062
    61
void
gabomdq@8062
    62
Wayland_PumpEvents(_THIS)
gabomdq@8062
    63
{
gabomdq@8062
    64
    SDL_VideoData *d = _this->driverdata;
gabomdq@8062
    65
    struct pollfd pfd[1];
gabomdq@8062
    66
gabomdq@8104
    67
    pfd[0].fd = WAYLAND_wl_display_get_fd(d->display);
gabomdq@8062
    68
    pfd[0].events = POLLIN;
gabomdq@8062
    69
    poll(pfd, 1, 0);
gabomdq@8062
    70
gabomdq@8062
    71
    if (pfd[0].revents & POLLIN)
gabomdq@8104
    72
        WAYLAND_wl_display_dispatch(d->display);
gabomdq@8062
    73
    else
gabomdq@8104
    74
        WAYLAND_wl_display_dispatch_pending(d->display);
gabomdq@8062
    75
}
gabomdq@8062
    76
gabomdq@8062
    77
static void
gabomdq@8062
    78
pointer_handle_enter(void *data, struct wl_pointer *pointer,
gabomdq@8062
    79
                     uint32_t serial, struct wl_surface *surface,
gabomdq@8062
    80
                     wl_fixed_t sx_w, wl_fixed_t sy_w)
gabomdq@8062
    81
{
gabomdq@8062
    82
    struct SDL_WaylandInput *input = data;
gabomdq@8062
    83
    SDL_WindowData *window;
gabomdq@8062
    84
gabomdq@8062
    85
    if (!surface) {
gabomdq@8062
    86
        /* enter event for a window we've just destroyed */
gabomdq@8062
    87
        return;
gabomdq@8062
    88
    }
gabomdq@8135
    89
    
gabomdq@8135
    90
    /* This handler will be called twice in Wayland 1.4
gabomdq@8135
    91
     * Once for the window surface which has valid user data
gabomdq@8135
    92
     * and again for the mouse cursor surface which does not have valid user data
gabomdq@8135
    93
     * We ignore the later
gabomdq@8135
    94
     */
gabomdq@8062
    95
gabomdq@8135
    96
    window = (SDL_WindowData *)wl_surface_get_user_data(surface);
gabomdq@8135
    97
    
gabomdq@8135
    98
    if (window) {
gabomdq@8135
    99
        input->pointer_focus = window;
gabomdq@8135
   100
        SDL_SetMouseFocus(window->sdlwindow);
gabomdq@8135
   101
    }
gabomdq@8062
   102
}
gabomdq@8062
   103
gabomdq@8062
   104
static void
gabomdq@8062
   105
pointer_handle_leave(void *data, struct wl_pointer *pointer,
gabomdq@8062
   106
                     uint32_t serial, struct wl_surface *surface)
gabomdq@8062
   107
{
gabomdq@8062
   108
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   109
gabomdq@8135
   110
    if (input->pointer_focus) {
gabomdq@8135
   111
        SDL_SetMouseFocus(NULL);
gabomdq@8135
   112
        input->pointer_focus = NULL;
gabomdq@8135
   113
    }
gabomdq@8062
   114
}
gabomdq@8062
   115
gabomdq@8062
   116
static void
gabomdq@8062
   117
pointer_handle_motion(void *data, struct wl_pointer *pointer,
gabomdq@8062
   118
                      uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
gabomdq@8062
   119
{
gabomdq@8062
   120
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   121
    SDL_WindowData *window = input->pointer_focus;
gabomdq@8062
   122
    int sx = wl_fixed_to_int(sx_w);
gabomdq@8062
   123
    int sy = wl_fixed_to_int(sy_w);
gabomdq@8135
   124
    if (input->pointer_focus) {
gabomdq@8135
   125
        SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
gabomdq@8135
   126
    }
gabomdq@8062
   127
}
gabomdq@8062
   128
gabomdq@8062
   129
static void
gabomdq@8062
   130
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
gabomdq@8062
   131
                      uint32_t time, uint32_t button, uint32_t state_w)
gabomdq@8062
   132
{
gabomdq@8062
   133
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   134
    SDL_WindowData *window = input->pointer_focus;
gabomdq@8062
   135
    enum wl_pointer_button_state state = state_w;
gabomdq@8062
   136
    uint32_t sdl_button;
gabomdq@8135
   137
    
gabomdq@8135
   138
    if  (input->pointer_focus) {
gabomdq@8135
   139
        switch (button) {
gabomdq@8135
   140
            case BTN_LEFT:
gabomdq@8135
   141
                sdl_button = SDL_BUTTON_LEFT;
gabomdq@8135
   142
                break;
gabomdq@8135
   143
            case BTN_MIDDLE:
gabomdq@8135
   144
                sdl_button = SDL_BUTTON_MIDDLE;
gabomdq@8135
   145
                break;
gabomdq@8135
   146
            case BTN_RIGHT:
gabomdq@8135
   147
                sdl_button = SDL_BUTTON_RIGHT;
gabomdq@8135
   148
                break;
gabomdq@8135
   149
            case BTN_SIDE:
gabomdq@8135
   150
                sdl_button = SDL_BUTTON_X1;
gabomdq@8135
   151
                break;
gabomdq@8135
   152
            case BTN_EXTRA:
gabomdq@8135
   153
                sdl_button = SDL_BUTTON_X2;
gabomdq@8135
   154
                break;
gabomdq@8135
   155
            default:
gabomdq@8135
   156
                return;
gabomdq@8135
   157
        }
gabomdq@8062
   158
gabomdq@8135
   159
        SDL_SendMouseButton(window->sdlwindow, 0,
gabomdq@8135
   160
                            state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
gabomdq@8062
   161
    }
gabomdq@8062
   162
}
gabomdq@8062
   163
gabomdq@8062
   164
static void
gabomdq@8062
   165
pointer_handle_axis(void *data, struct wl_pointer *pointer,
gabomdq@8062
   166
                    uint32_t time, uint32_t axis, wl_fixed_t value)
gabomdq@8062
   167
{
gabomdq@8062
   168
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   169
    SDL_WindowData *window = input->pointer_focus;
gabomdq@8062
   170
    enum wl_pointer_axis a = axis;
gabomdq@8062
   171
    int x, y;
gabomdq@8062
   172
gabomdq@8135
   173
    if (input->pointer_focus) {
gabomdq@8135
   174
        switch (a) {
gabomdq@8135
   175
            case WL_POINTER_AXIS_VERTICAL_SCROLL:
gabomdq@8135
   176
                x = 0;
gabomdq@8135
   177
                y = wl_fixed_to_int(value);
gabomdq@8135
   178
                break;
gabomdq@8135
   179
            case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
gabomdq@8135
   180
                x = wl_fixed_to_int(value);
gabomdq@8135
   181
                y = 0;
gabomdq@8135
   182
                break;
gabomdq@8135
   183
            default:
gabomdq@8135
   184
                return;
gabomdq@8135
   185
        }
gabomdq@8135
   186
urkle@9257
   187
        SDL_SendMouseWheel(window->sdlwindow, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
gabomdq@8062
   188
    }
gabomdq@8062
   189
}
gabomdq@8062
   190
gabomdq@8062
   191
static const struct wl_pointer_listener pointer_listener = {
gabomdq@8062
   192
    pointer_handle_enter,
gabomdq@8062
   193
    pointer_handle_leave,
gabomdq@8062
   194
    pointer_handle_motion,
gabomdq@8062
   195
    pointer_handle_button,
gabomdq@8062
   196
    pointer_handle_axis,
gabomdq@8062
   197
};
gabomdq@8062
   198
gabomdq@8062
   199
static void
gabomdq@8062
   200
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   201
                       uint32_t format, int fd, uint32_t size)
gabomdq@8062
   202
{
gabomdq@8062
   203
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   204
    char *map_str;
gabomdq@8062
   205
gabomdq@8062
   206
    if (!data) {
gabomdq@8062
   207
        close(fd);
gabomdq@8062
   208
        return;
gabomdq@8062
   209
    }
gabomdq@8062
   210
gabomdq@8062
   211
    if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
gabomdq@8062
   212
        close(fd);
gabomdq@8062
   213
        return;
gabomdq@8062
   214
    }
gabomdq@8062
   215
gabomdq@8062
   216
    map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
gabomdq@8062
   217
    if (map_str == MAP_FAILED) {
gabomdq@8062
   218
        close(fd);
gabomdq@8062
   219
        return;
gabomdq@8062
   220
    }
gabomdq@8062
   221
gabomdq@8104
   222
    input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
gabomdq@8062
   223
                                                map_str,
gabomdq@8062
   224
                                                XKB_KEYMAP_FORMAT_TEXT_V1,
gabomdq@8062
   225
                                                0);
gabomdq@8062
   226
    munmap(map_str, size);
gabomdq@8062
   227
    close(fd);
gabomdq@8062
   228
gabomdq@8062
   229
    if (!input->xkb.keymap) {
gabomdq@8062
   230
        fprintf(stderr, "failed to compile keymap\n");
gabomdq@8062
   231
        return;
gabomdq@8062
   232
    }
gabomdq@8062
   233
gabomdq@8104
   234
    input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
gabomdq@8062
   235
    if (!input->xkb.state) {
gabomdq@8062
   236
        fprintf(stderr, "failed to create XKB state\n");
gabomdq@8104
   237
        WAYLAND_xkb_keymap_unref(input->xkb.keymap);
gabomdq@8062
   238
        input->xkb.keymap = NULL;
gabomdq@8062
   239
        return;
gabomdq@8062
   240
    }
gabomdq@8062
   241
}
gabomdq@8062
   242
gabomdq@8062
   243
static void
gabomdq@8062
   244
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   245
                      uint32_t serial, struct wl_surface *surface,
gabomdq@8062
   246
                      struct wl_array *keys)
gabomdq@8062
   247
{
gabomdq@8062
   248
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   249
    SDL_WindowData *window = wl_surface_get_user_data(surface);
gabomdq@8062
   250
gabomdq@8062
   251
    input->keyboard_focus = window;
gabomdq@8062
   252
    window->keyboard_device = input;
gabomdq@8135
   253
    if (window) {
gabomdq@8135
   254
        SDL_SetKeyboardFocus(window->sdlwindow);
gabomdq@8135
   255
    }
gabomdq@8062
   256
}
gabomdq@8062
   257
gabomdq@8062
   258
static void
gabomdq@8062
   259
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   260
                      uint32_t serial, struct wl_surface *surface)
gabomdq@8062
   261
{
gabomdq@8062
   262
    SDL_SetKeyboardFocus(NULL);
gabomdq@8062
   263
}
gabomdq@8062
   264
gabomdq@8062
   265
static void
gabomdq@8062
   266
keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   267
                    uint32_t serial, uint32_t time, uint32_t key,
gabomdq@8062
   268
                    uint32_t state_w)
gabomdq@8062
   269
{
gabomdq@8062
   270
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   271
    SDL_WindowData *window = input->keyboard_focus;
gabomdq@8062
   272
    enum wl_keyboard_key_state state = state_w;
gabomdq@8062
   273
    const xkb_keysym_t *syms;
gabomdq@8062
   274
    uint32_t scancode;
gabomdq@8062
   275
    char text[8];
gabomdq@8062
   276
    int size;
gabomdq@8062
   277
gabomdq@8062
   278
    if (key < SDL_arraysize(xfree86_scancode_table2)) {
gabomdq@8062
   279
        scancode = xfree86_scancode_table2[key];
gabomdq@8062
   280
gabomdq@8062
   281
        // TODO when do we get WL_KEYBOARD_KEY_STATE_REPEAT?
gabomdq@8062
   282
        if (scancode != SDL_SCANCODE_UNKNOWN)
gabomdq@8062
   283
            SDL_SendKeyboardKey(state == WL_KEYBOARD_KEY_STATE_PRESSED ?
gabomdq@8062
   284
                                SDL_PRESSED : SDL_RELEASED, scancode);
gabomdq@8062
   285
    }
gabomdq@8062
   286
gabomdq@8062
   287
    if (!window || window->keyboard_device != input || !input->xkb.state)
gabomdq@8062
   288
        return;
gabomdq@8062
   289
gabomdq@8062
   290
    // TODO can this happen?
gabomdq@8104
   291
    if (WAYLAND_xkb_state_key_get_syms(input->xkb.state, key + 8, &syms) != 1)
gabomdq@8062
   292
        return;
gabomdq@8062
   293
gabomdq@8062
   294
    if (state) {
gabomdq@8104
   295
        size = WAYLAND_xkb_keysym_to_utf8(syms[0], text, sizeof text);
gabomdq@8062
   296
gabomdq@8062
   297
        if (size > 0) {
gabomdq@8062
   298
            text[size] = 0;
gabomdq@8062
   299
            SDL_SendKeyboardText(text);
gabomdq@8062
   300
        }
gabomdq@8062
   301
    }
gabomdq@8062
   302
}
gabomdq@8062
   303
gabomdq@8062
   304
static void
gabomdq@8062
   305
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   306
                          uint32_t serial, uint32_t mods_depressed,
gabomdq@8062
   307
                          uint32_t mods_latched, uint32_t mods_locked,
gabomdq@8062
   308
                          uint32_t group)
gabomdq@8062
   309
{
gabomdq@8062
   310
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   311
gabomdq@8104
   312
    WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
gabomdq@8062
   313
                          mods_locked, 0, 0, group);
gabomdq@8062
   314
}
gabomdq@8062
   315
gabomdq@8062
   316
static const struct wl_keyboard_listener keyboard_listener = {
gabomdq@8062
   317
    keyboard_handle_keymap,
gabomdq@8062
   318
    keyboard_handle_enter,
gabomdq@8062
   319
    keyboard_handle_leave,
gabomdq@8062
   320
    keyboard_handle_key,
gabomdq@8062
   321
    keyboard_handle_modifiers,
gabomdq@8062
   322
};
gabomdq@8062
   323
gabomdq@8062
   324
static void
gabomdq@8062
   325
seat_handle_capabilities(void *data, struct wl_seat *seat,
gabomdq@8062
   326
                         enum wl_seat_capability caps)
gabomdq@8062
   327
{
gabomdq@8062
   328
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   329
gabomdq@8062
   330
    if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
gabomdq@8062
   331
        input->pointer = wl_seat_get_pointer(seat);
gabomdq@8062
   332
        input->display->pointer = input->pointer;
gabomdq@8062
   333
        wl_pointer_set_user_data(input->pointer, input);
gabomdq@8062
   334
        wl_pointer_add_listener(input->pointer, &pointer_listener,
gabomdq@8062
   335
                                input);
gabomdq@8062
   336
    } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
gabomdq@8062
   337
        wl_pointer_destroy(input->pointer);
gabomdq@8062
   338
        input->pointer = NULL;
gabomdq@8062
   339
    }
gabomdq@8062
   340
gabomdq@8062
   341
    if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
gabomdq@8062
   342
        input->keyboard = wl_seat_get_keyboard(seat);
gabomdq@8062
   343
        wl_keyboard_set_user_data(input->keyboard, input);
gabomdq@8062
   344
        wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
gabomdq@8062
   345
                                 input);
gabomdq@8062
   346
    } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
gabomdq@8062
   347
        wl_keyboard_destroy(input->keyboard);
gabomdq@8062
   348
        input->keyboard = NULL;
gabomdq@8062
   349
    }
gabomdq@8062
   350
}
gabomdq@8062
   351
gabomdq@8062
   352
static const struct wl_seat_listener seat_listener = {
gabomdq@8062
   353
    seat_handle_capabilities,
gabomdq@8062
   354
};
gabomdq@8062
   355
gabomdq@8062
   356
void
gabomdq@8062
   357
Wayland_display_add_input(SDL_VideoData *d, uint32_t id)
gabomdq@8062
   358
{
gabomdq@8062
   359
    struct SDL_WaylandInput *input;
gabomdq@8062
   360
gabomdq@8135
   361
    input = SDL_calloc(1, sizeof *input);
gabomdq@8062
   362
    if (input == NULL)
gabomdq@8062
   363
        return;
gabomdq@8062
   364
gabomdq@8062
   365
    input->display = d;
gabomdq@8062
   366
    input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
gabomdq@8062
   367
gabomdq@8062
   368
    d->input = input;
gabomdq@8062
   369
gabomdq@8062
   370
    wl_seat_add_listener(input->seat, &seat_listener, input);
gabomdq@8062
   371
    wl_seat_set_user_data(input->seat, input);
gabomdq@8062
   372
gabomdq@8104
   373
    WAYLAND_wl_display_flush(d->display);
gabomdq@8062
   374
}
gabomdq@8062
   375
gabomdq@8062
   376
void Wayland_display_destroy_input(SDL_VideoData *d)
gabomdq@8062
   377
{
gabomdq@8062
   378
    struct SDL_WaylandInput *input = d->input;
gabomdq@8062
   379
gabomdq@8062
   380
    if (!input)
gabomdq@8062
   381
        return;
gabomdq@8062
   382
gabomdq@8062
   383
    if (input->keyboard)
gabomdq@8062
   384
        wl_keyboard_destroy(input->keyboard);
gabomdq@8062
   385
gabomdq@8062
   386
    if (input->pointer)
gabomdq@8062
   387
        wl_pointer_destroy(input->pointer);
gabomdq@8062
   388
gabomdq@8062
   389
    if (input->seat)
gabomdq@8062
   390
        wl_seat_destroy(input->seat);
gabomdq@8062
   391
gabomdq@8062
   392
    if (input->xkb.state)
gabomdq@8104
   393
        WAYLAND_xkb_state_unref(input->xkb.state);
gabomdq@8062
   394
gabomdq@8062
   395
    if (input->xkb.keymap)
gabomdq@8104
   396
        WAYLAND_xkb_keymap_unref(input->xkb.keymap);
gabomdq@8062
   397
gabomdq@8135
   398
    SDL_free(input);
gabomdq@8062
   399
    d->input = NULL;
gabomdq@8062
   400
}
gabomdq@8062
   401
icculus@8116
   402
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
icculus@8116
   403
gabomdq@8062
   404
/* vi: set ts=4 sw=4 expandtab: */