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