src/video/wayland/SDL_waylandevents.c
author Sam Lantinga
Mon, 03 Feb 2020 00:57:12 -0800
changeset 13474 b76ac1a7f87c
parent 13422 fd6a12de91c7
child 13696 ea20a7434b98
permissions -rw-r--r--
Fixed bug 4917 - Wayland: handle discrete pointer axis events

Luis Caceres

The current handling of Wayland mouse pointer events only handles wl_pointer.axis events, which, according to the Wayland documentation, deal with mouse wheel scroll events on a continuous scale. While this is reasonable for some input sources (e.g. touchpad two-finger scrolling), it is not for mouse wheel clicks which generate wl_pointer.axis events with large deltas.

This patch adds handling for wl_pointer.axis_discrete and wl_pointer.frame events and prefers to report SDL_MouseWheelEvent in discrete units if they are available. This means that for mouse wheel scrolling we count in clicks, but for touchpad two-finger scrolling we still use whatever units Wayland uses. This behaviour is closer to that of the X11 backend.

Since these events are only available since version 5 of the wl_seat interface, this patch also checks for this and falls back to the previous behaviour if its not available. I also had to add definitions for some of the pointer and keyboard events specified in versions 2-5 but these are just stubs and do nothing.
gabomdq@8062
     1
/*
gabomdq@8062
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 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"
slouken@10304
    28
#include "SDL_log.h"
gabomdq@8062
    29
slouken@11296
    30
#include "../../core/unix/SDL_poll.h"
gabomdq@8062
    31
#include "../../events/SDL_sysevents.h"
gabomdq@8062
    32
#include "../../events/SDL_events_c.h"
gabomdq@8062
    33
#include "../../events/scancodes_xfree86.h"
gabomdq@8062
    34
gabomdq@8062
    35
#include "SDL_waylandvideo.h"
gabomdq@8062
    36
#include "SDL_waylandevents_c.h"
gabomdq@8062
    37
#include "SDL_waylandwindow.h"
gabomdq@8062
    38
gabomdq@8104
    39
#include "SDL_waylanddyn.h"
gabomdq@8104
    40
slouken@10304
    41
#include "pointer-constraints-unstable-v1-client-protocol.h"
slouken@10304
    42
#include "relative-pointer-unstable-v1-client-protocol.h"
icculus@12033
    43
#include "xdg-shell-client-protocol.h"
icculus@11848
    44
#include "xdg-shell-unstable-v6-client-protocol.h"
slouken@10304
    45
gabomdq@8062
    46
#include <linux/input.h>
gabomdq@8062
    47
#include <sys/select.h>
gabomdq@8062
    48
#include <sys/mman.h>
gabomdq@8062
    49
#include <poll.h>
gabomdq@8062
    50
#include <unistd.h>
gabomdq@8062
    51
#include <xkbcommon/xkbcommon.h>
gabomdq@8062
    52
gabomdq@8062
    53
struct SDL_WaylandInput {
gabomdq@8062
    54
    SDL_VideoData *display;
gabomdq@8062
    55
    struct wl_seat *seat;
gabomdq@8062
    56
    struct wl_pointer *pointer;
slouken@11336
    57
    struct wl_touch *touch;
gabomdq@8062
    58
    struct wl_keyboard *keyboard;
slouken@10583
    59
    SDL_WaylandDataDevice *data_device;
slouken@10304
    60
    struct zwp_relative_pointer_v1 *relative_pointer;
gabomdq@8062
    61
    SDL_WindowData *pointer_focus;
gabomdq@8062
    62
    SDL_WindowData *keyboard_focus;
gabomdq@8062
    63
icculus@9554
    64
    /* Last motion location */
icculus@9554
    65
    wl_fixed_t sx_w;
icculus@9554
    66
    wl_fixed_t sy_w;
slouken@10304
    67
slouken@10304
    68
    double dx_frac;
slouken@10304
    69
    double dy_frac;
slouken@10304
    70
gabomdq@8062
    71
    struct {
gabomdq@8062
    72
        struct xkb_keymap *keymap;
gabomdq@8062
    73
        struct xkb_state *state;
gabomdq@8062
    74
    } xkb;
slouken@13474
    75
slouken@13474
    76
    /* information about axis events on current frame */
slouken@13474
    77
    struct {
slouken@13474
    78
        SDL_bool is_x_discrete;
slouken@13474
    79
        float x;
slouken@13474
    80
slouken@13474
    81
        SDL_bool is_y_discrete;
slouken@13474
    82
        float y;
slouken@13474
    83
    } pointer_curr_axis_info;
gabomdq@8062
    84
};
gabomdq@8062
    85
slouken@11336
    86
struct SDL_WaylandTouchPoint {
slouken@11336
    87
    SDL_TouchID id;
slouken@11336
    88
    float x;
slouken@11336
    89
    float y;
slouken@11336
    90
    struct wl_surface* surface;
slouken@11336
    91
slouken@11336
    92
    struct SDL_WaylandTouchPoint* prev;
slouken@11336
    93
    struct SDL_WaylandTouchPoint* next;
slouken@11336
    94
};
slouken@11336
    95
slouken@11336
    96
struct SDL_WaylandTouchPointList {
slouken@11336
    97
    struct SDL_WaylandTouchPoint* head;
slouken@11336
    98
    struct SDL_WaylandTouchPoint* tail;
slouken@11336
    99
};
slouken@11336
   100
slouken@11336
   101
static struct SDL_WaylandTouchPointList touch_points = {NULL, NULL};
slouken@11336
   102
slouken@11336
   103
static void
slouken@11336
   104
touch_add(SDL_TouchID id, float x, float y, struct wl_surface *surface)
slouken@11336
   105
{
slouken@11336
   106
    struct SDL_WaylandTouchPoint* tp = SDL_malloc(sizeof(struct SDL_WaylandTouchPoint));
slouken@11336
   107
slouken@11336
   108
    tp->id = id;
slouken@11336
   109
    tp->x = x;
slouken@11336
   110
    tp->y = y;
slouken@11336
   111
    tp->surface = surface;
slouken@11336
   112
slouken@11336
   113
    if (touch_points.tail) {
slouken@11336
   114
        touch_points.tail->next = tp;
slouken@11336
   115
        tp->prev = touch_points.tail;
slouken@11336
   116
    } else {
slouken@11336
   117
        touch_points.head = tp;
slouken@11336
   118
        tp->prev = NULL;
slouken@11336
   119
    }
slouken@11336
   120
slouken@11336
   121
    touch_points.tail = tp;
slouken@11336
   122
    tp->next = NULL;
slouken@11336
   123
}
slouken@11336
   124
slouken@11336
   125
static void
slouken@11336
   126
touch_update(SDL_TouchID id, float x, float y)
slouken@11336
   127
{
slouken@11336
   128
    struct SDL_WaylandTouchPoint* tp = touch_points.head;
slouken@11336
   129
slouken@11336
   130
    while (tp) {
slouken@11336
   131
        if (tp->id == id) {
slouken@11336
   132
            tp->x = x;
slouken@11336
   133
            tp->y = y;
slouken@11336
   134
        }
slouken@11336
   135
slouken@11336
   136
        tp = tp->next;
slouken@11336
   137
    }
slouken@11336
   138
}
slouken@11336
   139
slouken@11336
   140
static void
slime73@12979
   141
touch_del(SDL_TouchID id, float* x, float* y, struct wl_surface **surface)
slouken@11336
   142
{
slouken@11336
   143
    struct SDL_WaylandTouchPoint* tp = touch_points.head;
slouken@11336
   144
slouken@11336
   145
    while (tp) {
slouken@11336
   146
        if (tp->id == id) {
slouken@11336
   147
            *x = tp->x;
slouken@11336
   148
            *y = tp->y;
slime73@12979
   149
            *surface = tp->surface;
slouken@11336
   150
slouken@11336
   151
            if (tp->prev) {
slouken@11336
   152
                tp->prev->next = tp->next;
slouken@11336
   153
            } else {
slouken@11336
   154
                touch_points.head = tp->next;
slouken@11336
   155
            }
slouken@11336
   156
slouken@11336
   157
            if (tp->next) {
slouken@11336
   158
                tp->next->prev = tp->prev;
slouken@11336
   159
            } else {
slouken@11336
   160
                touch_points.tail = tp->prev;
slouken@11336
   161
            }
slouken@11336
   162
sylvain@13211
   163
            {
sylvain@13211
   164
                struct SDL_WaylandTouchPoint *next = tp->next;
sylvain@13211
   165
                SDL_free(tp);
sylvain@13211
   166
                tp = next;
sylvain@13211
   167
            }
sylvain@13211
   168
        } else {
sylvain@13211
   169
            tp = tp->next;
slouken@11336
   170
        }
slouken@11336
   171
    }
slouken@11336
   172
}
slouken@11336
   173
slouken@11336
   174
static struct wl_surface*
slouken@11336
   175
touch_surface(SDL_TouchID id)
slouken@11336
   176
{
slouken@11336
   177
    struct SDL_WaylandTouchPoint* tp = touch_points.head;
slouken@11336
   178
slouken@11336
   179
    while (tp) {
slouken@11336
   180
        if (tp->id == id) {
slouken@11336
   181
            return tp->surface;
slouken@11336
   182
        }
slouken@11336
   183
slouken@11336
   184
        tp = tp->next;
slouken@11336
   185
    }
slouken@11336
   186
slouken@11336
   187
    return NULL;
slouken@11336
   188
}
slouken@11336
   189
gabomdq@8062
   190
void
gabomdq@8062
   191
Wayland_PumpEvents(_THIS)
gabomdq@8062
   192
{
gabomdq@8062
   193
    SDL_VideoData *d = _this->driverdata;
slouken@13000
   194
    int err;
gabomdq@8062
   195
m@12342
   196
    WAYLAND_wl_display_flush(d->display);
m@12342
   197
slouken@11296
   198
    if (SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_FALSE, 0)) {
slouken@13000
   199
        err = WAYLAND_wl_display_dispatch(d->display);
slouken@13000
   200
    } else {
slouken@13000
   201
        err = WAYLAND_wl_display_dispatch_pending(d->display);
slouken@11296
   202
    }
slouken@13000
   203
    if (err == -1 && !d->display_disconnected) {
slouken@13000
   204
        /* Something has failed with the Wayland connection -- for example,
slouken@13000
   205
         * the compositor may have shut down and closed its end of the socket,
slouken@13000
   206
         * or there is a library-specific error. No recovery is possible. */
slouken@13000
   207
        d->display_disconnected = 1;
slouken@13000
   208
        /* Only send a single quit message, as application shutdown might call
slouken@13000
   209
         * SDL_PumpEvents */
slouken@13000
   210
        SDL_SendQuit();
slouken@11296
   211
    }
gabomdq@8062
   212
}
gabomdq@8062
   213
gabomdq@8062
   214
static void
gabomdq@8062
   215
pointer_handle_enter(void *data, struct wl_pointer *pointer,
gabomdq@8062
   216
                     uint32_t serial, struct wl_surface *surface,
gabomdq@8062
   217
                     wl_fixed_t sx_w, wl_fixed_t sy_w)
gabomdq@8062
   218
{
gabomdq@8062
   219
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   220
    SDL_WindowData *window;
gabomdq@8062
   221
gabomdq@8062
   222
    if (!surface) {
gabomdq@8062
   223
        /* enter event for a window we've just destroyed */
gabomdq@8062
   224
        return;
gabomdq@8062
   225
    }
slouken@11300
   226
gabomdq@8135
   227
    /* This handler will be called twice in Wayland 1.4
gabomdq@8135
   228
     * Once for the window surface which has valid user data
gabomdq@8135
   229
     * and again for the mouse cursor surface which does not have valid user data
gabomdq@8135
   230
     * We ignore the later
gabomdq@8135
   231
     */
gabomdq@8062
   232
gabomdq@8135
   233
    window = (SDL_WindowData *)wl_surface_get_user_data(surface);
slouken@11300
   234
gabomdq@8135
   235
    if (window) {
gabomdq@8135
   236
        input->pointer_focus = window;
gabomdq@8135
   237
        SDL_SetMouseFocus(window->sdlwindow);
gabomdq@8135
   238
    }
gabomdq@8062
   239
}
gabomdq@8062
   240
gabomdq@8062
   241
static void
gabomdq@8062
   242
pointer_handle_leave(void *data, struct wl_pointer *pointer,
gabomdq@8062
   243
                     uint32_t serial, struct wl_surface *surface)
gabomdq@8062
   244
{
gabomdq@8062
   245
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   246
gabomdq@8135
   247
    if (input->pointer_focus) {
gabomdq@8135
   248
        SDL_SetMouseFocus(NULL);
gabomdq@8135
   249
        input->pointer_focus = NULL;
gabomdq@8135
   250
    }
gabomdq@8062
   251
}
gabomdq@8062
   252
gabomdq@8062
   253
static void
gabomdq@8062
   254
pointer_handle_motion(void *data, struct wl_pointer *pointer,
gabomdq@8062
   255
                      uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
gabomdq@8062
   256
{
gabomdq@8062
   257
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   258
    SDL_WindowData *window = input->pointer_focus;
icculus@9554
   259
    input->sx_w = sx_w;
icculus@9554
   260
    input->sy_w = sy_w;
gabomdq@8135
   261
    if (input->pointer_focus) {
icculus@9556
   262
        const int sx = wl_fixed_to_int(sx_w);
icculus@9556
   263
        const int sy = wl_fixed_to_int(sy_w);
gabomdq@8135
   264
        SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
gabomdq@8135
   265
    }
gabomdq@8062
   266
}
gabomdq@8062
   267
icculus@9554
   268
static SDL_bool
icculus@9554
   269
ProcessHitTest(struct SDL_WaylandInput *input, uint32_t serial)
icculus@9554
   270
{
icculus@9554
   271
    SDL_WindowData *window_data = input->pointer_focus;
icculus@9554
   272
    SDL_Window *window = window_data->sdlwindow;
icculus@9554
   273
icculus@9554
   274
    if (window->hit_test) {
icculus@9554
   275
        const SDL_Point point = { wl_fixed_to_int(input->sx_w), wl_fixed_to_int(input->sy_w) };
icculus@9554
   276
        const SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
icculus@11848
   277
icculus@11848
   278
        static const uint32_t directions_wl[] = {
icculus@9555
   279
            WL_SHELL_SURFACE_RESIZE_TOP_LEFT, WL_SHELL_SURFACE_RESIZE_TOP,
icculus@9555
   280
            WL_SHELL_SURFACE_RESIZE_TOP_RIGHT, WL_SHELL_SURFACE_RESIZE_RIGHT,
icculus@9555
   281
            WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT, WL_SHELL_SURFACE_RESIZE_BOTTOM,
icculus@9555
   282
            WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT, WL_SHELL_SURFACE_RESIZE_LEFT
icculus@9555
   283
        };
icculus@11848
   284
icculus@11848
   285
        /* the names are different (ZXDG_TOPLEVEL_V6_RESIZE_EDGE_* vs
icculus@11848
   286
           WL_SHELL_SURFACE_RESIZE_*), but the values are the same. */
icculus@11848
   287
        const uint32_t *directions_zxdg = directions_wl;
icculus@11848
   288
icculus@9554
   289
        switch (rc) {
icculus@9555
   290
            case SDL_HITTEST_DRAGGABLE:
icculus@12033
   291
                if (input->display->shell.xdg) {
icculus@12033
   292
                    xdg_toplevel_move(window_data->shell_surface.xdg.roleobj.toplevel, input->seat, serial);
icculus@12033
   293
                } else if (input->display->shell.zxdg) {
icculus@11848
   294
                    zxdg_toplevel_v6_move(window_data->shell_surface.zxdg.roleobj.toplevel, input->seat, serial);
icculus@11848
   295
                } else {
icculus@11848
   296
                    wl_shell_surface_move(window_data->shell_surface.wl, input->seat, serial);
icculus@11848
   297
                }
icculus@9555
   298
                return SDL_TRUE;
icculus@9555
   299
icculus@9555
   300
            case SDL_HITTEST_RESIZE_TOPLEFT:
icculus@9555
   301
            case SDL_HITTEST_RESIZE_TOP:
icculus@9555
   302
            case SDL_HITTEST_RESIZE_TOPRIGHT:
icculus@9555
   303
            case SDL_HITTEST_RESIZE_RIGHT:
icculus@9555
   304
            case SDL_HITTEST_RESIZE_BOTTOMRIGHT:
icculus@9555
   305
            case SDL_HITTEST_RESIZE_BOTTOM:
icculus@9555
   306
            case SDL_HITTEST_RESIZE_BOTTOMLEFT:
icculus@9555
   307
            case SDL_HITTEST_RESIZE_LEFT:
icculus@12033
   308
                if (input->display->shell.xdg) {
icculus@12033
   309
                    xdg_toplevel_resize(window_data->shell_surface.xdg.roleobj.toplevel, input->seat, serial, directions_zxdg[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
icculus@12033
   310
                } else if (input->display->shell.zxdg) {
icculus@11848
   311
                    zxdg_toplevel_v6_resize(window_data->shell_surface.zxdg.roleobj.toplevel, input->seat, serial, directions_zxdg[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
icculus@11848
   312
                } else {
icculus@11848
   313
                    wl_shell_surface_resize(window_data->shell_surface.wl, input->seat, serial, directions_wl[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
icculus@11848
   314
                }
icculus@9555
   315
                return SDL_TRUE;
icculus@9555
   316
icculus@9555
   317
            default: return SDL_FALSE;
icculus@9554
   318
        }
icculus@9554
   319
    }
icculus@9554
   320
icculus@9555
   321
    return SDL_FALSE;
icculus@9554
   322
}
icculus@9554
   323
gabomdq@8062
   324
static void
slouken@10304
   325
pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
slouken@10304
   326
                             uint32_t time, uint32_t button, uint32_t state_w)
gabomdq@8062
   327
{
gabomdq@8062
   328
    SDL_WindowData *window = input->pointer_focus;
gabomdq@8062
   329
    enum wl_pointer_button_state state = state_w;
gabomdq@8062
   330
    uint32_t sdl_button;
slouken@11300
   331
gabomdq@8135
   332
    if  (input->pointer_focus) {
gabomdq@8135
   333
        switch (button) {
gabomdq@8135
   334
            case BTN_LEFT:
gabomdq@8135
   335
                sdl_button = SDL_BUTTON_LEFT;
slouken@10304
   336
                if (ProcessHitTest(input, serial)) {
icculus@9554
   337
                    return;  /* don't pass this event on to app. */
icculus@9554
   338
                }
gabomdq@8135
   339
                break;
gabomdq@8135
   340
            case BTN_MIDDLE:
gabomdq@8135
   341
                sdl_button = SDL_BUTTON_MIDDLE;
gabomdq@8135
   342
                break;
gabomdq@8135
   343
            case BTN_RIGHT:
gabomdq@8135
   344
                sdl_button = SDL_BUTTON_RIGHT;
gabomdq@8135
   345
                break;
gabomdq@8135
   346
            case BTN_SIDE:
gabomdq@8135
   347
                sdl_button = SDL_BUTTON_X1;
gabomdq@8135
   348
                break;
gabomdq@8135
   349
            case BTN_EXTRA:
gabomdq@8135
   350
                sdl_button = SDL_BUTTON_X2;
gabomdq@8135
   351
                break;
gabomdq@8135
   352
            default:
gabomdq@8135
   353
                return;
gabomdq@8135
   354
        }
slouken@10583
   355
            
slouken@10583
   356
        Wayland_data_device_set_serial(input->data_device, serial); 
gabomdq@8062
   357
gabomdq@8135
   358
        SDL_SendMouseButton(window->sdlwindow, 0,
gabomdq@8135
   359
                            state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
gabomdq@8062
   360
    }
gabomdq@8062
   361
}
gabomdq@8062
   362
gabomdq@8062
   363
static void
slouken@10304
   364
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
slouken@10304
   365
                      uint32_t time, uint32_t button, uint32_t state_w)
gabomdq@8062
   366
{
gabomdq@8062
   367
    struct SDL_WaylandInput *input = data;
slouken@10304
   368
slouken@10304
   369
    pointer_handle_button_common(input, serial, time, button, state_w);
slouken@10304
   370
}
slouken@10304
   371
slouken@10304
   372
static void
slouken@13474
   373
pointer_handle_axis_common_v1(struct SDL_WaylandInput *input,
slouken@13474
   374
                              uint32_t time, uint32_t axis, wl_fixed_t value)
slouken@10304
   375
{
gabomdq@8062
   376
    SDL_WindowData *window = input->pointer_focus;
gabomdq@8062
   377
    enum wl_pointer_axis a = axis;
slouken@11300
   378
    float x, y;
gabomdq@8062
   379
gabomdq@8135
   380
    if (input->pointer_focus) {
gabomdq@8135
   381
        switch (a) {
gabomdq@8135
   382
            case WL_POINTER_AXIS_VERTICAL_SCROLL:
gabomdq@8135
   383
                x = 0;
slouken@11813
   384
                y = 0 - (float)wl_fixed_to_double(value);
gabomdq@8135
   385
                break;
gabomdq@8135
   386
            case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
slouken@11813
   387
                x = 0 - (float)wl_fixed_to_double(value);
gabomdq@8135
   388
                y = 0;
gabomdq@8135
   389
                break;
gabomdq@8135
   390
            default:
gabomdq@8135
   391
                return;
gabomdq@8135
   392
        }
gabomdq@8135
   393
urkle@9257
   394
        SDL_SendMouseWheel(window->sdlwindow, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
gabomdq@8062
   395
    }
gabomdq@8062
   396
}
gabomdq@8062
   397
slouken@10304
   398
static void
slouken@13474
   399
pointer_handle_axis_common(struct SDL_WaylandInput *input, SDL_bool discrete,
slouken@13474
   400
                           uint32_t axis, wl_fixed_t value)
slouken@13474
   401
{
slouken@13474
   402
    enum wl_pointer_axis a = axis;
slouken@13474
   403
slouken@13474
   404
    if (input->pointer_focus) {
slouken@13474
   405
        switch (a) {
slouken@13474
   406
            case WL_POINTER_AXIS_VERTICAL_SCROLL:
slouken@13474
   407
                if (discrete) {
slouken@13474
   408
                    /* this is a discrete axis event so we process it and flag
slouken@13474
   409
                     * to ignore future continuous axis events in this frame */
slouken@13474
   410
                    input->pointer_curr_axis_info.is_y_discrete = SDL_TRUE;
slouken@13474
   411
                } else if(input->pointer_curr_axis_info.is_y_discrete) {
slouken@13474
   412
                    /* this is a continuous axis event and we have already
slouken@13474
   413
                     * processed a discrete axis event before so we ignore it */
slouken@13474
   414
                    break;
slouken@13474
   415
                }
slouken@13474
   416
                input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
slouken@13474
   417
                break;
slouken@13474
   418
            case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
slouken@13474
   419
                if (discrete) {
slouken@13474
   420
                    /* this is a discrete axis event so we process it and flag
slouken@13474
   421
                     * to ignore future continuous axis events in this frame */
slouken@13474
   422
                    input->pointer_curr_axis_info.is_x_discrete = SDL_TRUE;
slouken@13474
   423
                } else if(input->pointer_curr_axis_info.is_x_discrete) {
slouken@13474
   424
                    /* this is a continuous axis event and we have already
slouken@13474
   425
                     * processed a discrete axis event before so we ignore it */
slouken@13474
   426
                    break;
slouken@13474
   427
                }
slouken@13474
   428
                input->pointer_curr_axis_info.x = 0 - (float)wl_fixed_to_double(value);
slouken@13474
   429
                break;
slouken@13474
   430
        }
slouken@13474
   431
    }
slouken@13474
   432
}
slouken@13474
   433
slouken@13474
   434
static void
slouken@10304
   435
pointer_handle_axis(void *data, struct wl_pointer *pointer,
slouken@10304
   436
                    uint32_t time, uint32_t axis, wl_fixed_t value)
slouken@10304
   437
{
slouken@10304
   438
    struct SDL_WaylandInput *input = data;
slouken@10304
   439
slouken@13474
   440
    if(wl_seat_interface.version >= 5)
slouken@13474
   441
        pointer_handle_axis_common(input, SDL_FALSE, axis, value);
slouken@13474
   442
    else
slouken@13474
   443
        pointer_handle_axis_common_v1(input, time, axis, value);
slouken@13474
   444
}
slouken@13474
   445
slouken@13474
   446
static void
slouken@13474
   447
pointer_handle_frame(void *data, struct wl_pointer *pointer)
slouken@13474
   448
{
slouken@13474
   449
    struct SDL_WaylandInput *input = data;
slouken@13474
   450
    SDL_WindowData *window = input->pointer_focus;
slouken@13474
   451
    float x = input->pointer_curr_axis_info.x, y = input->pointer_curr_axis_info.y;
slouken@13474
   452
slouken@13474
   453
    /* clear pointer_curr_axis_info for next frame */
slouken@13474
   454
    memset(&input->pointer_curr_axis_info, 0, sizeof input->pointer_curr_axis_info);
slouken@13474
   455
slouken@13474
   456
    if(x == 0.0f && y == 0.0f)
slouken@13474
   457
        return;
slouken@13474
   458
    else
slouken@13474
   459
        SDL_SendMouseWheel(window->sdlwindow, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
slouken@10304
   460
}
slouken@10304
   461
slouken@13474
   462
static void
slouken@13474
   463
pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
slouken@13474
   464
                           uint32_t axis_source)
slouken@13474
   465
{
slouken@13474
   466
    /* unimplemented */
slouken@13474
   467
}
slouken@13474
   468
slouken@13474
   469
static void
slouken@13474
   470
pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
slouken@13474
   471
                         uint32_t time, uint32_t axis)
slouken@13474
   472
{
slouken@13474
   473
    /* unimplemented */
slouken@13474
   474
}
slouken@13474
   475
slouken@13474
   476
static void
slouken@13474
   477
pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
slouken@13474
   478
                             uint32_t axis, int32_t discrete)
slouken@13474
   479
{
slouken@13474
   480
    struct SDL_WaylandInput *input = data;
slouken@13474
   481
slouken@13474
   482
    pointer_handle_axis_common(input, SDL_TRUE, axis, wl_fixed_from_int(discrete));
slouken@13474
   483
}
slouken@13474
   484
slouken@13474
   485
gabomdq@8062
   486
static const struct wl_pointer_listener pointer_listener = {
gabomdq@8062
   487
    pointer_handle_enter,
gabomdq@8062
   488
    pointer_handle_leave,
gabomdq@8062
   489
    pointer_handle_motion,
gabomdq@8062
   490
    pointer_handle_button,
gabomdq@8062
   491
    pointer_handle_axis,
slouken@13474
   492
    pointer_handle_frame,           // Version 5
slouken@13474
   493
    pointer_handle_axis_source,     // Version 5
slouken@13474
   494
    pointer_handle_axis_stop,       // Version 5
slouken@13474
   495
    pointer_handle_axis_discrete,   // Version 5
gabomdq@8062
   496
};
gabomdq@8062
   497
gabomdq@8062
   498
static void
slouken@11336
   499
touch_handler_down(void *data, struct wl_touch *touch, unsigned int serial,
slouken@11336
   500
                   unsigned int timestamp, struct wl_surface *surface,
slouken@11336
   501
                   int id, wl_fixed_t fx, wl_fixed_t fy)
slouken@11336
   502
{
icculus@12455
   503
    SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
icculus@12455
   504
    const double dblx = wl_fixed_to_double(fx);
icculus@12455
   505
    const double dbly = wl_fixed_to_double(fy);
icculus@12455
   506
    const float x = dblx / window_data->sdlwindow->w;
icculus@12455
   507
    const float y = dbly / window_data->sdlwindow->h;
slouken@11336
   508
slouken@11336
   509
    touch_add(id, x, y, surface);
icculus@12455
   510
slime73@12979
   511
    SDL_SendTouch(1, (SDL_FingerID)id, window_data->sdlwindow, SDL_TRUE, x, y, 1.0f);
slouken@11336
   512
}
slouken@11336
   513
slouken@11336
   514
static void
slouken@11336
   515
touch_handler_up(void *data, struct wl_touch *touch, unsigned int serial,
slouken@11336
   516
                 unsigned int timestamp, int id)
slouken@11336
   517
{
slouken@11336
   518
    float x = 0, y = 0;
slime73@12979
   519
    struct wl_surface *surface = NULL;
slime73@12979
   520
    SDL_Window *window = NULL;
slouken@11336
   521
slime73@12979
   522
    touch_del(id, &x, &y, &surface);
slime73@12979
   523
slime73@12979
   524
    if (surface) {
slime73@12979
   525
        SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
slime73@12979
   526
        window = window_data->sdlwindow;
slime73@12979
   527
    }
slime73@12979
   528
slime73@12979
   529
    SDL_SendTouch(1, (SDL_FingerID)id, window, SDL_FALSE, x, y, 0.0f);
slouken@11336
   530
}
slouken@11336
   531
slouken@11336
   532
static void
slouken@11336
   533
touch_handler_motion(void *data, struct wl_touch *touch, unsigned int timestamp,
slouken@11336
   534
                     int id, wl_fixed_t fx, wl_fixed_t fy)
slouken@11336
   535
{
icculus@12455
   536
    SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(touch_surface(id));
icculus@12455
   537
    const double dblx = wl_fixed_to_double(fx);
icculus@12455
   538
    const double dbly = wl_fixed_to_double(fy);
icculus@12455
   539
    const float x = dblx / window_data->sdlwindow->w;
icculus@12455
   540
    const float y = dbly / window_data->sdlwindow->h;
slouken@11336
   541
slouken@11336
   542
    touch_update(id, x, y);
slime73@12979
   543
    SDL_SendTouchMotion(1, (SDL_FingerID)id, window_data->sdlwindow, x, y, 1.0f);
slouken@11336
   544
}
slouken@11336
   545
slouken@11336
   546
static void
slouken@11336
   547
touch_handler_frame(void *data, struct wl_touch *touch)
slouken@11336
   548
{
slouken@11336
   549
slouken@11336
   550
}
slouken@11336
   551
slouken@11336
   552
static void
slouken@11336
   553
touch_handler_cancel(void *data, struct wl_touch *touch)
slouken@11336
   554
{
slouken@11336
   555
slouken@11336
   556
}
slouken@11336
   557
slouken@11336
   558
static const struct wl_touch_listener touch_listener = {
slouken@11336
   559
    touch_handler_down,
slouken@11336
   560
    touch_handler_up,
slouken@11336
   561
    touch_handler_motion,
slouken@11336
   562
    touch_handler_frame,
brandon@11552
   563
    touch_handler_cancel,
brandon@11552
   564
    NULL, /* shape */
brandon@11552
   565
    NULL, /* orientation */
slouken@11336
   566
};
slouken@11336
   567
slouken@11336
   568
static void
gabomdq@8062
   569
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   570
                       uint32_t format, int fd, uint32_t size)
gabomdq@8062
   571
{
gabomdq@8062
   572
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   573
    char *map_str;
gabomdq@8062
   574
gabomdq@8062
   575
    if (!data) {
gabomdq@8062
   576
        close(fd);
gabomdq@8062
   577
        return;
gabomdq@8062
   578
    }
gabomdq@8062
   579
gabomdq@8062
   580
    if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
gabomdq@8062
   581
        close(fd);
gabomdq@8062
   582
        return;
gabomdq@8062
   583
    }
gabomdq@8062
   584
gabomdq@8062
   585
    map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
gabomdq@8062
   586
    if (map_str == MAP_FAILED) {
gabomdq@8062
   587
        close(fd);
gabomdq@8062
   588
        return;
gabomdq@8062
   589
    }
gabomdq@8062
   590
gabomdq@8104
   591
    input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
gabomdq@8062
   592
                                                map_str,
gabomdq@8062
   593
                                                XKB_KEYMAP_FORMAT_TEXT_V1,
gabomdq@8062
   594
                                                0);
gabomdq@8062
   595
    munmap(map_str, size);
gabomdq@8062
   596
    close(fd);
gabomdq@8062
   597
gabomdq@8062
   598
    if (!input->xkb.keymap) {
gabomdq@8062
   599
        fprintf(stderr, "failed to compile keymap\n");
gabomdq@8062
   600
        return;
gabomdq@8062
   601
    }
gabomdq@8062
   602
gabomdq@8104
   603
    input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
gabomdq@8062
   604
    if (!input->xkb.state) {
gabomdq@8062
   605
        fprintf(stderr, "failed to create XKB state\n");
gabomdq@8104
   606
        WAYLAND_xkb_keymap_unref(input->xkb.keymap);
gabomdq@8062
   607
        input->xkb.keymap = NULL;
gabomdq@8062
   608
        return;
gabomdq@8062
   609
    }
gabomdq@8062
   610
}
gabomdq@8062
   611
gabomdq@8062
   612
static void
gabomdq@8062
   613
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   614
                      uint32_t serial, struct wl_surface *surface,
gabomdq@8062
   615
                      struct wl_array *keys)
gabomdq@8062
   616
{
gabomdq@8062
   617
    struct SDL_WaylandInput *input = data;
icculus@9656
   618
    SDL_WindowData *window;
icculus@9655
   619
icculus@9655
   620
    if (!surface) {
icculus@9655
   621
        /* enter event for a window we've just destroyed */
icculus@9655
   622
        return;
icculus@9655
   623
    }
slouken@11300
   624
icculus@9656
   625
    window = wl_surface_get_user_data(surface);
gabomdq@8062
   626
gabomdq@8135
   627
    if (window) {
philipp@10099
   628
        input->keyboard_focus = window;
philipp@10099
   629
        window->keyboard_device = input;
gabomdq@8135
   630
        SDL_SetKeyboardFocus(window->sdlwindow);
gabomdq@8135
   631
    }
gabomdq@8062
   632
}
gabomdq@8062
   633
gabomdq@8062
   634
static void
gabomdq@8062
   635
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   636
                      uint32_t serial, struct wl_surface *surface)
gabomdq@8062
   637
{
gabomdq@8062
   638
    SDL_SetKeyboardFocus(NULL);
gabomdq@8062
   639
}
gabomdq@8062
   640
gabomdq@8062
   641
static void
gabomdq@8062
   642
keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   643
                    uint32_t serial, uint32_t time, uint32_t key,
gabomdq@8062
   644
                    uint32_t state_w)
gabomdq@8062
   645
{
gabomdq@8062
   646
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   647
    SDL_WindowData *window = input->keyboard_focus;
gabomdq@8062
   648
    enum wl_keyboard_key_state state = state_w;
gabomdq@8062
   649
    const xkb_keysym_t *syms;
gabomdq@8062
   650
    uint32_t scancode;
gabomdq@8062
   651
    char text[8];
gabomdq@8062
   652
    int size;
gabomdq@8062
   653
gabomdq@8062
   654
    if (key < SDL_arraysize(xfree86_scancode_table2)) {
gabomdq@8062
   655
        scancode = xfree86_scancode_table2[key];
gabomdq@8062
   656
gabomdq@8062
   657
        // TODO when do we get WL_KEYBOARD_KEY_STATE_REPEAT?
gabomdq@8062
   658
        if (scancode != SDL_SCANCODE_UNKNOWN)
gabomdq@8062
   659
            SDL_SendKeyboardKey(state == WL_KEYBOARD_KEY_STATE_PRESSED ?
gabomdq@8062
   660
                                SDL_PRESSED : SDL_RELEASED, scancode);
gabomdq@8062
   661
    }
gabomdq@8062
   662
gabomdq@8062
   663
    if (!window || window->keyboard_device != input || !input->xkb.state)
gabomdq@8062
   664
        return;
gabomdq@8062
   665
gabomdq@8062
   666
    // TODO can this happen?
gabomdq@8104
   667
    if (WAYLAND_xkb_state_key_get_syms(input->xkb.state, key + 8, &syms) != 1)
gabomdq@8062
   668
        return;
gabomdq@8062
   669
gabomdq@8062
   670
    if (state) {
gabomdq@8104
   671
        size = WAYLAND_xkb_keysym_to_utf8(syms[0], text, sizeof text);
gabomdq@8062
   672
gabomdq@8062
   673
        if (size > 0) {
gabomdq@8062
   674
            text[size] = 0;
slouken@10583
   675
slouken@10583
   676
            Wayland_data_device_set_serial(input->data_device, serial);
slouken@10583
   677
gabomdq@8062
   678
            SDL_SendKeyboardText(text);
gabomdq@8062
   679
        }
gabomdq@8062
   680
    }
gabomdq@8062
   681
}
gabomdq@8062
   682
gabomdq@8062
   683
static void
gabomdq@8062
   684
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
gabomdq@8062
   685
                          uint32_t serial, uint32_t mods_depressed,
gabomdq@8062
   686
                          uint32_t mods_latched, uint32_t mods_locked,
gabomdq@8062
   687
                          uint32_t group)
gabomdq@8062
   688
{
gabomdq@8062
   689
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   690
gabomdq@8104
   691
    WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
gabomdq@8062
   692
                          mods_locked, 0, 0, group);
gabomdq@8062
   693
}
gabomdq@8062
   694
slouken@13474
   695
static void
slouken@13474
   696
keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
slouken@13474
   697
                            int32_t rate, int32_t delay)
slouken@13474
   698
{
slouken@13474
   699
    /* unimplemented */
slouken@13474
   700
}
slouken@13474
   701
gabomdq@8062
   702
static const struct wl_keyboard_listener keyboard_listener = {
gabomdq@8062
   703
    keyboard_handle_keymap,
gabomdq@8062
   704
    keyboard_handle_enter,
gabomdq@8062
   705
    keyboard_handle_leave,
gabomdq@8062
   706
    keyboard_handle_key,
gabomdq@8062
   707
    keyboard_handle_modifiers,
slouken@13474
   708
    keyboard_handle_repeat_info,    // Version 4
gabomdq@8062
   709
};
gabomdq@8062
   710
gabomdq@8062
   711
static void
gabomdq@8062
   712
seat_handle_capabilities(void *data, struct wl_seat *seat,
gabomdq@8062
   713
                         enum wl_seat_capability caps)
gabomdq@8062
   714
{
gabomdq@8062
   715
    struct SDL_WaylandInput *input = data;
gabomdq@8062
   716
gabomdq@8062
   717
    if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
gabomdq@8062
   718
        input->pointer = wl_seat_get_pointer(seat);
slouken@13474
   719
        memset(&input->pointer_curr_axis_info, 0, sizeof input->pointer_curr_axis_info);
gabomdq@8062
   720
        input->display->pointer = input->pointer;
gabomdq@8062
   721
        wl_pointer_set_user_data(input->pointer, input);
gabomdq@8062
   722
        wl_pointer_add_listener(input->pointer, &pointer_listener,
gabomdq@8062
   723
                                input);
gabomdq@8062
   724
    } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
gabomdq@8062
   725
        wl_pointer_destroy(input->pointer);
gabomdq@8062
   726
        input->pointer = NULL;
slouken@11812
   727
        input->display->pointer = NULL;
gabomdq@8062
   728
    }
gabomdq@8062
   729
slouken@11336
   730
    if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
slime73@12404
   731
        SDL_AddTouch(1, SDL_TOUCH_DEVICE_DIRECT, "wayland_touch");
slouken@11336
   732
        input->touch = wl_seat_get_touch(seat);
slouken@11336
   733
        wl_touch_set_user_data(input->touch, input);
slouken@11336
   734
        wl_touch_add_listener(input->touch, &touch_listener,
slouken@11336
   735
                                 input);
slouken@11336
   736
    } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
slouken@11336
   737
        SDL_DelTouch(1);
slouken@11336
   738
        wl_touch_destroy(input->touch);
slouken@11336
   739
        input->touch = NULL;
slouken@11336
   740
    }
slouken@11336
   741
gabomdq@8062
   742
    if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
gabomdq@8062
   743
        input->keyboard = wl_seat_get_keyboard(seat);
gabomdq@8062
   744
        wl_keyboard_set_user_data(input->keyboard, input);
gabomdq@8062
   745
        wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
gabomdq@8062
   746
                                 input);
gabomdq@8062
   747
    } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
gabomdq@8062
   748
        wl_keyboard_destroy(input->keyboard);
gabomdq@8062
   749
        input->keyboard = NULL;
gabomdq@8062
   750
    }
gabomdq@8062
   751
}
gabomdq@8062
   752
slouken@13474
   753
static void
slouken@13474
   754
seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
slouken@13474
   755
{
slouken@13474
   756
    /* unimplemented */
slouken@13474
   757
}
slouken@13474
   758
gabomdq@8062
   759
static const struct wl_seat_listener seat_listener = {
gabomdq@8062
   760
    seat_handle_capabilities,
slouken@13474
   761
    seat_handle_name,           // Version 2
gabomdq@8062
   762
};
gabomdq@8062
   763
slouken@10583
   764
static void
slouken@10583
   765
data_source_handle_target(void *data, struct wl_data_source *wl_data_source,
slouken@10583
   766
                          const char *mime_type)
slouken@10583
   767
{
slouken@10583
   768
}
slouken@10583
   769
slouken@10583
   770
static void
slouken@10583
   771
data_source_handle_send(void *data, struct wl_data_source *wl_data_source,
slouken@10583
   772
                        const char *mime_type, int32_t fd)
slouken@10583
   773
{
slouken@10583
   774
    Wayland_data_source_send((SDL_WaylandDataSource *)data, mime_type, fd);
slouken@10583
   775
}
slouken@10583
   776
                       
slouken@10583
   777
static void
slouken@10583
   778
data_source_handle_cancelled(void *data, struct wl_data_source *wl_data_source)
slouken@10583
   779
{
slouken@10583
   780
    Wayland_data_source_destroy(data);
slouken@10583
   781
}
slouken@10583
   782
                       
slouken@10583
   783
static void
slouken@10583
   784
data_source_handle_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source)
slouken@10583
   785
{
slouken@10583
   786
}
slouken@10583
   787
slouken@10583
   788
static void
slouken@10583
   789
data_source_handle_dnd_finished(void *data, struct wl_data_source *wl_data_source)
slouken@10583
   790
{
slouken@10583
   791
}
slouken@10583
   792
slouken@10583
   793
static void
slouken@10583
   794
data_source_handle_action(void *data, struct wl_data_source *wl_data_source,
slouken@10583
   795
                          uint32_t dnd_action)
slouken@10583
   796
{
slouken@10583
   797
}
slouken@10583
   798
slouken@10583
   799
static const struct wl_data_source_listener data_source_listener = {
slouken@10583
   800
    data_source_handle_target,
slouken@10583
   801
    data_source_handle_send,
slouken@10583
   802
    data_source_handle_cancelled,
slouken@10583
   803
    data_source_handle_dnd_drop_performed, // Version 3
slouken@10583
   804
    data_source_handle_dnd_finished,       // Version 3
slouken@10583
   805
    data_source_handle_action,             // Version 3
slouken@10583
   806
};
slouken@10583
   807
slouken@10583
   808
SDL_WaylandDataSource*
slouken@10583
   809
Wayland_data_source_create(_THIS)
slouken@10583
   810
{
slouken@10583
   811
    SDL_WaylandDataSource *data_source = NULL;
slouken@10583
   812
    SDL_VideoData *driver_data = NULL;
slouken@10583
   813
    struct wl_data_source *id = NULL;
slouken@10583
   814
slouken@10583
   815
    if (_this == NULL || _this->driverdata == NULL) {
slouken@10583
   816
        SDL_SetError("Video driver uninitialized");
slouken@10583
   817
    } else {
slouken@10583
   818
        driver_data = _this->driverdata;
slouken@10583
   819
slouken@10583
   820
        if (driver_data->data_device_manager != NULL) {
slouken@10583
   821
            id = wl_data_device_manager_create_data_source(
slouken@10583
   822
                     driver_data->data_device_manager);
slouken@10583
   823
        }
slouken@10583
   824
slouken@10583
   825
        if (id == NULL) { 
slouken@10583
   826
            SDL_SetError("Wayland unable to create data source");
slouken@10583
   827
        } else {
slouken@10583
   828
            data_source = SDL_calloc(1, sizeof *data_source);
slouken@10583
   829
            if (data_source == NULL) {
slouken@10583
   830
                SDL_OutOfMemory();
slouken@10583
   831
                wl_data_source_destroy(id);
slouken@10583
   832
            } else {
slouken@10583
   833
                WAYLAND_wl_list_init(&(data_source->mimes));
slouken@10583
   834
                data_source->source = id;
slouken@10583
   835
                wl_data_source_set_user_data(id, data_source);
slouken@10583
   836
                wl_data_source_add_listener(id, &data_source_listener,
slouken@10583
   837
                                            data_source);
slouken@10583
   838
            }
slouken@10583
   839
        }
slouken@10583
   840
    }
slouken@10583
   841
    return data_source;
slouken@10583
   842
}
slouken@10583
   843
slouken@10583
   844
static void
slouken@10583
   845
data_offer_handle_offer(void *data, struct wl_data_offer *wl_data_offer,
slouken@10583
   846
                        const char *mime_type)
slouken@10583
   847
{
slouken@10583
   848
    SDL_WaylandDataOffer *offer = data;
slouken@10583
   849
    Wayland_data_offer_add_mime(offer, mime_type);
slouken@10583
   850
}
slouken@10583
   851
slouken@10583
   852
static void
slouken@10583
   853
data_offer_handle_source_actions(void *data, struct wl_data_offer *wl_data_offer,
slouken@10583
   854
                                 uint32_t source_actions)
slouken@10583
   855
{
slouken@10583
   856
}
slouken@10583
   857
slouken@10583
   858
static void
slouken@10583
   859
data_offer_handle_actions(void *data, struct wl_data_offer *wl_data_offer,
slouken@10583
   860
                          uint32_t dnd_action)
slouken@10583
   861
{
slouken@10583
   862
}
slouken@10583
   863
slouken@10583
   864
static const struct wl_data_offer_listener data_offer_listener = {
slouken@10583
   865
    data_offer_handle_offer,
slouken@10583
   866
    data_offer_handle_source_actions, // Version 3
slouken@10583
   867
    data_offer_handle_actions,        // Version 3
slouken@10583
   868
};
slouken@10583
   869
slouken@10583
   870
static void
slouken@10583
   871
data_device_handle_data_offer(void *data, struct wl_data_device *wl_data_device,
slouken@12201
   872
                              struct wl_data_offer *id)
slouken@10583
   873
{
slouken@10583
   874
    SDL_WaylandDataOffer *data_offer = NULL;
slouken@10583
   875
slouken@10583
   876
    data_offer = SDL_calloc(1, sizeof *data_offer);
slouken@10583
   877
    if (data_offer == NULL) {
slouken@10583
   878
        SDL_OutOfMemory();
slouken@10583
   879
    } else {
slouken@10583
   880
        data_offer->offer = id;
slouken@10583
   881
        data_offer->data_device = data;
slouken@10583
   882
        WAYLAND_wl_list_init(&(data_offer->mimes));
slouken@10583
   883
        wl_data_offer_set_user_data(id, data_offer);
slouken@10583
   884
        wl_data_offer_add_listener(id, &data_offer_listener, data_offer);
slouken@10583
   885
    }
slouken@10583
   886
}
slouken@10583
   887
slouken@10583
   888
static void
slouken@10583
   889
data_device_handle_enter(void *data, struct wl_data_device *wl_data_device,
slouken@12201
   890
                         uint32_t serial, struct wl_surface *surface,
slouken@10583
   891
                         wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *id)
slouken@10583
   892
{
slouken@10583
   893
    SDL_WaylandDataDevice *data_device = data;
slouken@10583
   894
    SDL_bool has_mime = SDL_FALSE;
slouken@10583
   895
    uint32_t dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; 
slouken@10583
   896
        
slouken@10583
   897
    data_device->drag_serial = serial;
slouken@10583
   898
slouken@10583
   899
    if (id != NULL) {
slouken@10583
   900
        data_device->drag_offer = wl_data_offer_get_user_data(id);
slouken@10583
   901
slouken@10583
   902
        /* TODO: SDL Support more mime types */
slouken@10583
   903
        has_mime = Wayland_data_offer_has_mime(
slouken@10583
   904
            data_device->drag_offer, FILE_MIME);
slouken@10583
   905
slouken@10583
   906
        /* If drag_mime is NULL this will decline the offer */
slouken@10583
   907
        wl_data_offer_accept(id, serial,
slouken@10583
   908
                             (has_mime == SDL_TRUE) ? FILE_MIME : NULL);
slouken@10583
   909
slouken@10583
   910
        /* SDL only supports "copy" style drag and drop */
slouken@10583
   911
        if (has_mime == SDL_TRUE) {
slouken@10583
   912
            dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
slouken@10583
   913
        }
mforney@13391
   914
        if (wl_data_offer_get_version(data_device->drag_offer->offer) >= 3) {
mforney@13391
   915
            wl_data_offer_set_actions(data_device->drag_offer->offer,
mforney@13391
   916
                                      dnd_action, dnd_action);
mforney@13391
   917
        }
slouken@10583
   918
    }
slouken@10583
   919
}
slouken@10583
   920
slouken@10583
   921
static void
slouken@10583
   922
data_device_handle_leave(void *data, struct wl_data_device *wl_data_device)
slouken@10583
   923
{
slouken@10583
   924
    SDL_WaylandDataDevice *data_device = data;
slouken@10583
   925
    SDL_WaylandDataOffer *offer = NULL;
slouken@10583
   926
slouken@10583
   927
    if (data_device->selection_offer != NULL) {
slouken@10583
   928
        data_device->selection_offer = NULL;
slouken@10583
   929
        Wayland_data_offer_destroy(offer);
slouken@10583
   930
    }
slouken@10583
   931
}
slouken@10583
   932
slouken@10583
   933
static void
slouken@10583
   934
data_device_handle_motion(void *data, struct wl_data_device *wl_data_device,
slouken@12201
   935
                          uint32_t time, wl_fixed_t x, wl_fixed_t y)
slouken@10583
   936
{
slouken@10583
   937
}
slouken@10583
   938
slouken@10583
   939
static void
slouken@10583
   940
data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
slouken@10583
   941
{
slouken@10583
   942
    SDL_WaylandDataDevice *data_device = data;
slouken@10583
   943
    void *buffer = NULL;
slouken@10583
   944
    size_t length = 0;
slouken@10583
   945
slouken@10583
   946
    const char *current_uri = NULL;
slouken@10583
   947
    const char *last_char = NULL;
slouken@10583
   948
    char *current_char = NULL;
slouken@10583
   949
    
slouken@10583
   950
    if (data_device->drag_offer != NULL) {
slouken@10583
   951
        /* TODO: SDL Support more mime types */
slouken@10583
   952
        buffer = Wayland_data_offer_receive(data_device->drag_offer,
slouken@10583
   953
                                            &length, FILE_MIME, SDL_FALSE);
slouken@10583
   954
slouken@10583
   955
        /* uri-list */
slouken@10583
   956
        current_uri = (const char *)buffer;
slouken@10583
   957
        last_char = (const char *)buffer + length;
slouken@10583
   958
        for (current_char = buffer; current_char < last_char; ++current_char) {
slouken@10583
   959
            if (*current_char == '\n' || *current_char == 0) {
slouken@10583
   960
                if (*current_uri != 0 && *current_uri != '#') {
slouken@10583
   961
                    *current_char = 0;
slouken@10583
   962
                    SDL_SendDropFile(NULL, current_uri);
slouken@10583
   963
                }
slouken@10583
   964
                current_uri = (const char *)current_char + 1;
slouken@10583
   965
            }
slouken@10583
   966
        }
slouken@10583
   967
slouken@10583
   968
        SDL_free(buffer);
slouken@10583
   969
    }
slouken@10583
   970
}
slouken@10583
   971
slouken@10583
   972
static void
slouken@10583
   973
data_device_handle_selection(void *data, struct wl_data_device *wl_data_device,
slouken@12201
   974
                             struct wl_data_offer *id)
slouken@10583
   975
{    
slouken@10583
   976
    SDL_WaylandDataDevice *data_device = data;
slouken@10583
   977
    SDL_WaylandDataOffer *offer = NULL;
slouken@10583
   978
slouken@10583
   979
    if (id != NULL) {
slouken@10583
   980
        offer = wl_data_offer_get_user_data(id);
slouken@10583
   981
    }
slouken@10583
   982
slouken@10583
   983
    if (data_device->selection_offer != offer) {
slouken@10583
   984
        Wayland_data_offer_destroy(data_device->selection_offer);
slouken@10583
   985
        data_device->selection_offer = offer;
slouken@10583
   986
    }
slouken@10583
   987
slouken@10583
   988
    SDL_SendClipboardUpdate();
slouken@10583
   989
}
slouken@10583
   990
slouken@10583
   991
static const struct wl_data_device_listener data_device_listener = {
slouken@10583
   992
    data_device_handle_data_offer,
slouken@10583
   993
    data_device_handle_enter,
slouken@10583
   994
    data_device_handle_leave,
slouken@10583
   995
    data_device_handle_motion,
slouken@10583
   996
    data_device_handle_drop,
slouken@10583
   997
    data_device_handle_selection
slouken@10583
   998
};
slouken@10583
   999
gabomdq@8062
  1000
void
gabomdq@8062
  1001
Wayland_display_add_input(SDL_VideoData *d, uint32_t id)
gabomdq@8062
  1002
{
gabomdq@8062
  1003
    struct SDL_WaylandInput *input;
slouken@10583
  1004
    SDL_WaylandDataDevice *data_device = NULL;
gabomdq@8062
  1005
gabomdq@8135
  1006
    input = SDL_calloc(1, sizeof *input);
gabomdq@8062
  1007
    if (input == NULL)
gabomdq@8062
  1008
        return;
gabomdq@8062
  1009
gabomdq@8062
  1010
    input->display = d;
slouken@13474
  1011
    if (wl_seat_interface.version >= 5)
slouken@13474
  1012
        input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 5);
slouken@13474
  1013
    else
slouken@13474
  1014
        input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
icculus@9554
  1015
    input->sx_w = wl_fixed_from_int(0);
icculus@9554
  1016
    input->sy_w = wl_fixed_from_int(0);
gabomdq@8062
  1017
    d->input = input;
slouken@10583
  1018
    
slouken@10583
  1019
    if (d->data_device_manager != NULL) {
slouken@10583
  1020
        data_device = SDL_calloc(1, sizeof *data_device);
slouken@10583
  1021
        if (data_device == NULL) {
slouken@10583
  1022
            return;
slouken@10583
  1023
        }
slouken@10583
  1024
slouken@10583
  1025
        data_device->data_device = wl_data_device_manager_get_data_device(
slouken@10583
  1026
            d->data_device_manager, input->seat
slouken@10583
  1027
        );
slouken@10583
  1028
        data_device->video_data = d;
slouken@10583
  1029
slouken@10583
  1030
        if (data_device->data_device == NULL) {
slouken@10583
  1031
            SDL_free(data_device);
slouken@10583
  1032
        } else {
slouken@10583
  1033
            wl_data_device_set_user_data(data_device->data_device, data_device);
slouken@10583
  1034
            wl_data_device_add_listener(data_device->data_device,
slouken@10583
  1035
                                        &data_device_listener, data_device);
slouken@10583
  1036
            input->data_device = data_device;
slouken@10583
  1037
        }
slouken@10583
  1038
    }
gabomdq@8062
  1039
gabomdq@8062
  1040
    wl_seat_add_listener(input->seat, &seat_listener, input);
gabomdq@8062
  1041
    wl_seat_set_user_data(input->seat, input);
gabomdq@8062
  1042
gabomdq@8104
  1043
    WAYLAND_wl_display_flush(d->display);
gabomdq@8062
  1044
}
gabomdq@8062
  1045
gabomdq@8062
  1046
void Wayland_display_destroy_input(SDL_VideoData *d)
gabomdq@8062
  1047
{
gabomdq@8062
  1048
    struct SDL_WaylandInput *input = d->input;
gabomdq@8062
  1049
gabomdq@8062
  1050
    if (!input)
gabomdq@8062
  1051
        return;
gabomdq@8062
  1052
slouken@10583
  1053
    if (input->data_device != NULL) {
slouken@10583
  1054
        Wayland_data_device_clear_selection(input->data_device);
slouken@10583
  1055
        if (input->data_device->selection_offer != NULL) {
slouken@10583
  1056
            Wayland_data_offer_destroy(input->data_device->selection_offer);
slouken@10583
  1057
        }
slouken@10583
  1058
        if (input->data_device->drag_offer != NULL) {
slouken@10583
  1059
            Wayland_data_offer_destroy(input->data_device->drag_offer);
slouken@10583
  1060
        }
slouken@10583
  1061
        if (input->data_device->data_device != NULL) {
slouken@10583
  1062
            wl_data_device_release(input->data_device->data_device);
slouken@10583
  1063
        }
slouken@10583
  1064
        SDL_free(input->data_device);
slouken@10583
  1065
    }
slouken@10583
  1066
gabomdq@8062
  1067
    if (input->keyboard)
gabomdq@8062
  1068
        wl_keyboard_destroy(input->keyboard);
gabomdq@8062
  1069
gabomdq@8062
  1070
    if (input->pointer)
gabomdq@8062
  1071
        wl_pointer_destroy(input->pointer);
gabomdq@8062
  1072
slouken@11336
  1073
    if (input->touch) {
slouken@11336
  1074
        SDL_DelTouch(1);
slouken@11336
  1075
        wl_touch_destroy(input->touch);
slouken@11336
  1076
    }
slouken@11336
  1077
gabomdq@8062
  1078
    if (input->seat)
gabomdq@8062
  1079
        wl_seat_destroy(input->seat);
gabomdq@8062
  1080
gabomdq@8062
  1081
    if (input->xkb.state)
gabomdq@8104
  1082
        WAYLAND_xkb_state_unref(input->xkb.state);
gabomdq@8062
  1083
gabomdq@8062
  1084
    if (input->xkb.keymap)
gabomdq@8104
  1085
        WAYLAND_xkb_keymap_unref(input->xkb.keymap);
gabomdq@8062
  1086
gabomdq@8135
  1087
    SDL_free(input);
gabomdq@8062
  1088
    d->input = NULL;
gabomdq@8062
  1089
}
gabomdq@8062
  1090
slouken@10583
  1091
SDL_WaylandDataDevice* Wayland_get_data_device(struct SDL_WaylandInput *input)
slouken@10583
  1092
{
slouken@10583
  1093
    if (input == NULL) {
slouken@10583
  1094
        return NULL;
slouken@10583
  1095
    }
slouken@10583
  1096
slouken@10583
  1097
    return input->data_device;
slouken@10583
  1098
}
slouken@10583
  1099
icculus@11848
  1100
/* !!! FIXME: just merge these into display_handle_global(). */
slouken@10304
  1101
void Wayland_display_add_relative_pointer_manager(SDL_VideoData *d, uint32_t id)
slouken@10304
  1102
{
slouken@10304
  1103
    d->relative_pointer_manager =
slouken@10304
  1104
        wl_registry_bind(d->registry, id,
slouken@10304
  1105
                         &zwp_relative_pointer_manager_v1_interface, 1);
slouken@10304
  1106
}
slouken@10304
  1107
slouken@10304
  1108
void Wayland_display_destroy_relative_pointer_manager(SDL_VideoData *d)
slouken@10304
  1109
{
slouken@10304
  1110
    if (d->relative_pointer_manager)
slouken@10304
  1111
        zwp_relative_pointer_manager_v1_destroy(d->relative_pointer_manager);
slouken@10304
  1112
}
slouken@10304
  1113
slouken@10304
  1114
void Wayland_display_add_pointer_constraints(SDL_VideoData *d, uint32_t id)
slouken@10304
  1115
{
slouken@10304
  1116
    d->pointer_constraints =
slouken@10304
  1117
        wl_registry_bind(d->registry, id,
slouken@10304
  1118
                         &zwp_pointer_constraints_v1_interface, 1);
slouken@10304
  1119
}
slouken@10304
  1120
slouken@10304
  1121
void Wayland_display_destroy_pointer_constraints(SDL_VideoData *d)
slouken@10304
  1122
{
slouken@10304
  1123
    if (d->pointer_constraints)
slouken@10304
  1124
        zwp_pointer_constraints_v1_destroy(d->pointer_constraints);
slouken@10304
  1125
}
slouken@10304
  1126
slouken@10304
  1127
static void
slouken@10304
  1128
relative_pointer_handle_relative_motion(void *data,
slouken@10304
  1129
                                        struct zwp_relative_pointer_v1 *pointer,
slouken@10304
  1130
                                        uint32_t time_hi,
slouken@10304
  1131
                                        uint32_t time_lo,
slouken@10304
  1132
                                        wl_fixed_t dx_w,
slouken@10304
  1133
                                        wl_fixed_t dy_w,
slouken@10304
  1134
                                        wl_fixed_t dx_unaccel_w,
slouken@10304
  1135
                                        wl_fixed_t dy_unaccel_w)
slouken@10304
  1136
{
slouken@10304
  1137
    struct SDL_WaylandInput *input = data;
slouken@10304
  1138
    SDL_VideoData *d = input->display;
slouken@10304
  1139
    SDL_WindowData *window = input->pointer_focus;
slouken@10304
  1140
    double dx_unaccel;
slouken@10304
  1141
    double dy_unaccel;
slouken@10304
  1142
    double dx;
slouken@10304
  1143
    double dy;
slouken@10304
  1144
slouken@10304
  1145
    dx_unaccel = wl_fixed_to_double(dx_unaccel_w);
slouken@10304
  1146
    dy_unaccel = wl_fixed_to_double(dy_unaccel_w);
slouken@10304
  1147
slouken@10304
  1148
    /* Add left over fraction from last event. */
slouken@10304
  1149
    dx_unaccel += input->dx_frac;
slouken@10304
  1150
    dy_unaccel += input->dy_frac;
slouken@10304
  1151
slouken@10304
  1152
    input->dx_frac = modf(dx_unaccel, &dx);
slouken@10304
  1153
    input->dy_frac = modf(dy_unaccel, &dy);
slouken@10304
  1154
slouken@10304
  1155
    if (input->pointer_focus && d->relative_mouse_mode) {
slouken@10304
  1156
        SDL_SendMouseMotion(window->sdlwindow, 0, 1, (int)dx, (int)dy);
slouken@10304
  1157
    }
slouken@10304
  1158
}
slouken@10304
  1159
slouken@10304
  1160
static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
slouken@10304
  1161
    relative_pointer_handle_relative_motion,
slouken@10304
  1162
};
slouken@10304
  1163
slouken@10304
  1164
static void
slouken@10304
  1165
locked_pointer_locked(void *data,
slouken@10304
  1166
                      struct zwp_locked_pointer_v1 *locked_pointer)
slouken@10304
  1167
{
slouken@10304
  1168
}
slouken@10304
  1169
slouken@10304
  1170
static void
slouken@10304
  1171
locked_pointer_unlocked(void *data,
slouken@10304
  1172
                        struct zwp_locked_pointer_v1 *locked_pointer)
slouken@10304
  1173
{
slouken@10304
  1174
}
slouken@10304
  1175
slouken@10304
  1176
static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
slouken@10304
  1177
    locked_pointer_locked,
slouken@10304
  1178
    locked_pointer_unlocked,
slouken@10304
  1179
};
slouken@10304
  1180
slouken@10304
  1181
static void
slouken@10304
  1182
lock_pointer_to_window(SDL_Window *window,
slouken@10304
  1183
                       struct SDL_WaylandInput *input)
slouken@10304
  1184
{
slouken@10304
  1185
    SDL_WindowData *w = window->driverdata;
slouken@10304
  1186
    SDL_VideoData *d = input->display;
slouken@10304
  1187
    struct zwp_locked_pointer_v1 *locked_pointer;
slouken@10304
  1188
slouken@10304
  1189
    if (w->locked_pointer)
slouken@10304
  1190
        return;
slouken@10304
  1191
slouken@10304
  1192
    locked_pointer =
slouken@10304
  1193
        zwp_pointer_constraints_v1_lock_pointer(d->pointer_constraints,
slouken@10304
  1194
                                                w->surface,
slouken@10304
  1195
                                                input->pointer,
slouken@10304
  1196
                                                NULL,
slouken@10304
  1197
                                                ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
slouken@10304
  1198
    zwp_locked_pointer_v1_add_listener(locked_pointer,
slouken@10304
  1199
                                       &locked_pointer_listener,
slouken@10304
  1200
                                       window);
slouken@10304
  1201
slouken@10304
  1202
    w->locked_pointer = locked_pointer;
slouken@10304
  1203
}
slouken@10304
  1204
slouken@10304
  1205
int Wayland_input_lock_pointer(struct SDL_WaylandInput *input)
slouken@10304
  1206
{
slouken@10304
  1207
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
slouken@10304
  1208
    SDL_VideoData *d = input->display;
slouken@10304
  1209
    SDL_Window *window;
slouken@10304
  1210
    struct zwp_relative_pointer_v1 *relative_pointer;
slouken@10304
  1211
slouken@10304
  1212
    if (!d->relative_pointer_manager)
slouken@10304
  1213
        return -1;
slouken@10304
  1214
slouken@10304
  1215
    if (!d->pointer_constraints)
slouken@10304
  1216
        return -1;
slouken@10304
  1217
brandontschaefer@12465
  1218
    if (!input->pointer)
brandontschaefer@12465
  1219
        return -1;
brandontschaefer@12465
  1220
slouken@10304
  1221
    if (!input->relative_pointer) {
slouken@10304
  1222
        relative_pointer =
slouken@10304
  1223
            zwp_relative_pointer_manager_v1_get_relative_pointer(
slouken@10304
  1224
                d->relative_pointer_manager,
slouken@10304
  1225
                input->pointer);
slouken@10304
  1226
        zwp_relative_pointer_v1_add_listener(relative_pointer,
slouken@10304
  1227
                                             &relative_pointer_listener,
slouken@10304
  1228
                                             input);
slouken@10304
  1229
        input->relative_pointer = relative_pointer;
slouken@10304
  1230
    }
slouken@10304
  1231
slouken@10304
  1232
    for (window = vd->windows; window; window = window->next)
slouken@10304
  1233
        lock_pointer_to_window(window, input);
slouken@10304
  1234
slouken@10304
  1235
    d->relative_mouse_mode = 1;
slouken@10304
  1236
slouken@10304
  1237
    return 0;
slouken@10304
  1238
}
slouken@10304
  1239
slouken@10304
  1240
int Wayland_input_unlock_pointer(struct SDL_WaylandInput *input)
slouken@10304
  1241
{
slouken@10304
  1242
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
slouken@10304
  1243
    SDL_VideoData *d = input->display;
slouken@10304
  1244
    SDL_Window *window;
slouken@10304
  1245
    SDL_WindowData *w;
slouken@10304
  1246
slouken@10304
  1247
    for (window = vd->windows; window; window = window->next) {
slouken@10304
  1248
        w = window->driverdata;
slouken@10304
  1249
        if (w->locked_pointer)
slouken@10304
  1250
            zwp_locked_pointer_v1_destroy(w->locked_pointer);
slouken@10304
  1251
        w->locked_pointer = NULL;
slouken@10304
  1252
    }
slouken@10304
  1253
slouken@10304
  1254
    zwp_relative_pointer_v1_destroy(input->relative_pointer);
slouken@10304
  1255
    input->relative_pointer = NULL;
slouken@10304
  1256
slouken@10304
  1257
    d->relative_mouse_mode = 0;
slouken@10304
  1258
slouken@10304
  1259
    return 0;
slouken@10304
  1260
}
slouken@10304
  1261
icculus@8116
  1262
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
icculus@8116
  1263
gabomdq@8062
  1264
/* vi: set ts=4 sw=4 expandtab: */