src/video/wayland/SDL_waylandmouse.c
author Ryan C. Gordon
Mon, 23 Jan 2017 12:06:10 -0500
changeset 10837 c2f241c2f6ad
parent 10737 3406a0f8b041
child 11019 97839b6bbd46
permissions -rw-r--r--
audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128.
gabomdq@8062
     1
/*
gabomdq@8062
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
icculus@8116
    22
#include "../../SDL_internal.h"
icculus@8116
    23
icculus@8116
    24
#if SDL_VIDEO_DRIVER_WAYLAND
icculus@8116
    25
gabomdq@8062
    26
#include <sys/types.h>
gabomdq@8062
    27
#include <sys/mman.h>
gabomdq@8062
    28
#include <fcntl.h>
gabomdq@8062
    29
#include <unistd.h>
gabomdq@8062
    30
#include <stdlib.h>
gabomdq@8062
    31
#include <limits.h>
gabomdq@8062
    32
gabomdq@8062
    33
#include "../SDL_sysvideo.h"
gabomdq@8062
    34
gabomdq@8062
    35
#include "SDL_mouse.h"
gabomdq@8062
    36
#include "../../events/SDL_mouse_c.h"
gabomdq@8062
    37
#include "SDL_waylandvideo.h"
gabomdq@8062
    38
#include "SDL_waylandevents_c.h"
gabomdq@8062
    39
gabomdq@8104
    40
#include "SDL_waylanddyn.h"
gabomdq@8104
    41
#include "wayland-cursor.h"
gabomdq@8104
    42
gabomdq@8062
    43
#include "SDL_assert.h"
gabomdq@8062
    44
gabomdq@8062
    45
gabomdq@8062
    46
typedef struct {
gabomdq@8062
    47
    struct wl_buffer   *buffer;
gabomdq@8062
    48
    struct wl_surface  *surface;
gabomdq@8062
    49
gabomdq@8062
    50
    int                hot_x, hot_y;
slouken@8711
    51
    int                w, h;
gabomdq@8062
    52
gabomdq@8062
    53
    /* Either a preloaded cursor, or one we created ourselves */
gabomdq@8062
    54
    struct wl_cursor   *cursor;
gabomdq@8062
    55
    void               *shm_data;
gabomdq@8062
    56
} Wayland_CursorData;
gabomdq@8062
    57
gabomdq@8062
    58
static int
gabomdq@8062
    59
wayland_create_tmp_file(off_t size)
gabomdq@8062
    60
{
gabomdq@8062
    61
    static const char template[] = "/sdl-shared-XXXXXX";
gabomdq@8062
    62
    char *xdg_path;
gabomdq@8062
    63
    char tmp_path[PATH_MAX];
gabomdq@8062
    64
    int fd;
gabomdq@8062
    65
gabomdq@8062
    66
    xdg_path = SDL_getenv("XDG_RUNTIME_DIR");
gabomdq@8062
    67
    if (!xdg_path) {
gabomdq@8062
    68
        return -1;
gabomdq@8062
    69
    }
gabomdq@8062
    70
gabomdq@8062
    71
    SDL_strlcpy(tmp_path, xdg_path, PATH_MAX);
gabomdq@8062
    72
    SDL_strlcat(tmp_path, template, PATH_MAX);
gabomdq@8062
    73
gabomdq@8062
    74
    fd = mkostemp(tmp_path, O_CLOEXEC);
gabomdq@8062
    75
    if (fd < 0)
gabomdq@8062
    76
        return -1;
gabomdq@8062
    77
gabomdq@8062
    78
    if (ftruncate(fd, size) < 0) {
gabomdq@8062
    79
        close(fd);
gabomdq@8062
    80
        return -1;
gabomdq@8062
    81
    }
gabomdq@8062
    82
gabomdq@8062
    83
    return fd;
gabomdq@8062
    84
}
gabomdq@8062
    85
gabomdq@8062
    86
static void
gabomdq@8062
    87
mouse_buffer_release(void *data, struct wl_buffer *buffer)
gabomdq@8062
    88
{
gabomdq@8062
    89
}
gabomdq@8062
    90
gabomdq@8062
    91
static const struct wl_buffer_listener mouse_buffer_listener = {
gabomdq@8062
    92
    mouse_buffer_release
gabomdq@8062
    93
};
gabomdq@8062
    94
gabomdq@8062
    95
static int
gabomdq@8062
    96
create_buffer_from_shm(Wayland_CursorData *d,
gabomdq@8062
    97
                       int width,
gabomdq@8062
    98
                       int height,
gabomdq@8062
    99
                       uint32_t format)
gabomdq@8062
   100
{
gabomdq@8062
   101
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
gabomdq@8062
   102
    SDL_VideoData *data = (SDL_VideoData *) vd->driverdata;
icculus@8721
   103
    struct wl_shm_pool *shm_pool;
gabomdq@8062
   104
gabomdq@8062
   105
    int stride = width * 4;
gabomdq@8062
   106
    int size = stride * height;
gabomdq@8062
   107
gabomdq@8062
   108
    int shm_fd;
gabomdq@8062
   109
gabomdq@8062
   110
    shm_fd = wayland_create_tmp_file(size);
gabomdq@8062
   111
    if (shm_fd < 0)
gabomdq@8062
   112
    {
philipp@10130
   113
        return SDL_SetError("Creating mouse cursor buffer failed.");
gabomdq@8062
   114
    }
gabomdq@8062
   115
gabomdq@8062
   116
    d->shm_data = mmap(NULL,
gabomdq@8062
   117
                       size,
gabomdq@8062
   118
                       PROT_READ | PROT_WRITE,
gabomdq@8062
   119
                       MAP_SHARED,
gabomdq@8062
   120
                       shm_fd,
gabomdq@8062
   121
                       0);
ryomnktml@9136
   122
    if (d->shm_data == MAP_FAILED) {
gabomdq@8062
   123
        d->shm_data = NULL;
gabomdq@8062
   124
        close (shm_fd);
philipp@10130
   125
        return SDL_SetError("mmap() failed.");
gabomdq@8062
   126
    }
gabomdq@8062
   127
icculus@8721
   128
    shm_pool = wl_shm_create_pool(data->shm, shm_fd, size);
gabomdq@8062
   129
    d->buffer = wl_shm_pool_create_buffer(shm_pool,
gabomdq@8062
   130
                                          0,
gabomdq@8062
   131
                                          width,
gabomdq@8062
   132
                                          height,
gabomdq@8062
   133
                                          stride,
gabomdq@8062
   134
                                          format);
gabomdq@8062
   135
    wl_buffer_add_listener(d->buffer,
gabomdq@8062
   136
                           &mouse_buffer_listener,
gabomdq@8062
   137
                           d);
gabomdq@8062
   138
gabomdq@8062
   139
    wl_shm_pool_destroy (shm_pool);
gabomdq@8062
   140
    close (shm_fd);
gabomdq@8062
   141
gabomdq@8062
   142
    return 0;
gabomdq@8062
   143
}
gabomdq@8062
   144
gabomdq@8062
   145
static SDL_Cursor *
gabomdq@8062
   146
Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
gabomdq@8062
   147
{
gabomdq@8062
   148
    SDL_Cursor *cursor;
gabomdq@8062
   149
gabomdq@8062
   150
    cursor = calloc(1, sizeof (*cursor));
gabomdq@8062
   151
    if (cursor) {
gabomdq@8062
   152
        SDL_VideoDevice *vd = SDL_GetVideoDevice ();
gabomdq@8062
   153
        SDL_VideoData *wd = (SDL_VideoData *) vd->driverdata;
gabomdq@8062
   154
        Wayland_CursorData *data = calloc (1, sizeof (Wayland_CursorData));
philipp@10098
   155
        if (!data) {
philipp@10098
   156
            SDL_OutOfMemory();
philipp@10098
   157
            free(cursor);
philipp@10098
   158
            return NULL;
philipp@10098
   159
        }
gabomdq@8062
   160
        cursor->driverdata = (void *) data;
gabomdq@8062
   161
gabomdq@8062
   162
        /* Assume ARGB8888 */
gabomdq@8062
   163
        SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
gabomdq@8062
   164
        SDL_assert(surface->pitch == surface->w * 4);
gabomdq@8062
   165
gabomdq@8062
   166
        /* Allocate shared memory buffer for this cursor */
gabomdq@8062
   167
        if (create_buffer_from_shm (data,
gabomdq@8062
   168
                                    surface->w,
gabomdq@8062
   169
                                    surface->h,
philipp@10114
   170
                                    WL_SHM_FORMAT_ARGB8888) < 0)
gabomdq@8062
   171
        {
gabomdq@8062
   172
            free (cursor->driverdata);
gabomdq@8062
   173
            free (cursor);
gabomdq@8062
   174
            return NULL;
gabomdq@8062
   175
        }
gabomdq@8062
   176
gabomdq@8062
   177
        SDL_memcpy(data->shm_data,
gabomdq@8062
   178
                   surface->pixels,
gabomdq@8062
   179
                   surface->h * surface->pitch);
gabomdq@8062
   180
gabomdq@8062
   181
        data->surface = wl_compositor_create_surface(wd->compositor);
gabomdq@8135
   182
        wl_surface_set_user_data(data->surface, NULL);
gabomdq@8062
   183
gabomdq@8062
   184
        data->hot_x = hot_x;
gabomdq@8062
   185
        data->hot_y = hot_y;
slouken@8711
   186
        data->w = surface->w;
slouken@8711
   187
        data->h = surface->h;
philipp@10098
   188
    } else {
philipp@10098
   189
        SDL_OutOfMemory();
gabomdq@8062
   190
    }
gabomdq@8062
   191
gabomdq@8062
   192
    return cursor;
gabomdq@8062
   193
}
gabomdq@8062
   194
gabomdq@8062
   195
static SDL_Cursor *
gabomdq@8062
   196
CreateCursorFromWlCursor(SDL_VideoData *d, struct wl_cursor *wlcursor)
gabomdq@8062
   197
{
gabomdq@8062
   198
    SDL_Cursor *cursor;
gabomdq@8062
   199
gabomdq@8062
   200
    cursor = calloc(1, sizeof (*cursor));
gabomdq@8062
   201
    if (cursor) {
gabomdq@8062
   202
        Wayland_CursorData *data = calloc (1, sizeof (Wayland_CursorData));
philipp@10098
   203
        if (!data) {
philipp@10098
   204
            SDL_OutOfMemory();
philipp@10098
   205
            free(cursor);
philipp@10098
   206
            return NULL;
philipp@10098
   207
        }
gabomdq@8062
   208
        cursor->driverdata = (void *) data;
gabomdq@8062
   209
slouken@8711
   210
        data->buffer = WAYLAND_wl_cursor_image_get_buffer(wlcursor->images[0]);
gabomdq@8062
   211
        data->surface = wl_compositor_create_surface(d->compositor);
gabomdq@8135
   212
        wl_surface_set_user_data(data->surface, NULL);
gabomdq@8062
   213
        data->hot_x = wlcursor->images[0]->hotspot_x;
gabomdq@8062
   214
        data->hot_y = wlcursor->images[0]->hotspot_y;
slouken@8711
   215
        data->w = wlcursor->images[0]->width;
slouken@8711
   216
        data->h = wlcursor->images[0]->height;
gabomdq@8062
   217
        data->cursor= wlcursor;
gabomdq@8062
   218
    } else {
gabomdq@8062
   219
        SDL_OutOfMemory ();
gabomdq@8062
   220
    }
gabomdq@8062
   221
gabomdq@8062
   222
    return cursor;
gabomdq@8062
   223
}
gabomdq@8062
   224
gabomdq@8062
   225
static SDL_Cursor *
gabomdq@8062
   226
Wayland_CreateDefaultCursor()
gabomdq@8062
   227
{
gabomdq@8062
   228
    SDL_VideoDevice *device = SDL_GetVideoDevice();
gabomdq@8062
   229
    SDL_VideoData *data = device->driverdata;
gabomdq@8062
   230
gabomdq@8062
   231
    return CreateCursorFromWlCursor (data,
gabomdq@8104
   232
                                     WAYLAND_wl_cursor_theme_get_cursor(data->cursor_theme,
gabomdq@8062
   233
                                                                "left_ptr"));
gabomdq@8062
   234
}
gabomdq@8062
   235
gabomdq@8062
   236
static SDL_Cursor *
gabomdq@8062
   237
Wayland_CreateSystemCursor(SDL_SystemCursor id)
gabomdq@8062
   238
{
gabomdq@8062
   239
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
gabomdq@8062
   240
    SDL_VideoData *d = vd->driverdata;
gabomdq@8062
   241
gabomdq@8062
   242
    struct wl_cursor *cursor = NULL;
gabomdq@8062
   243
gabomdq@8062
   244
    switch(id)
gabomdq@8062
   245
    {
gabomdq@8062
   246
    default:
gabomdq@8062
   247
        SDL_assert(0);
gabomdq@8062
   248
        return NULL;
gabomdq@8062
   249
    case SDL_SYSTEM_CURSOR_ARROW:
gabomdq@8104
   250
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
gabomdq@8062
   251
        break;
gabomdq@8062
   252
    case SDL_SYSTEM_CURSOR_IBEAM:
gabomdq@8104
   253
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "xterm");
gabomdq@8062
   254
        break;
gabomdq@8062
   255
    case SDL_SYSTEM_CURSOR_WAIT:
slouken@8710
   256
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "watch");
gabomdq@8062
   257
        break;
gabomdq@8062
   258
    case SDL_SYSTEM_CURSOR_CROSSHAIR:
gabomdq@8104
   259
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
gabomdq@8062
   260
        break;
gabomdq@8062
   261
    case SDL_SYSTEM_CURSOR_WAITARROW:
slouken@8710
   262
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "watch");
gabomdq@8062
   263
        break;
gabomdq@8062
   264
    case SDL_SYSTEM_CURSOR_SIZENWSE:
gabomdq@8104
   265
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
gabomdq@8062
   266
        break;
gabomdq@8062
   267
    case SDL_SYSTEM_CURSOR_SIZENESW:
gabomdq@8104
   268
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
gabomdq@8062
   269
        break;
gabomdq@8062
   270
    case SDL_SYSTEM_CURSOR_SIZEWE:
gabomdq@8104
   271
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
gabomdq@8062
   272
        break;
gabomdq@8062
   273
    case SDL_SYSTEM_CURSOR_SIZENS:
gabomdq@8104
   274
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
gabomdq@8062
   275
        break;
gabomdq@8062
   276
    case SDL_SYSTEM_CURSOR_SIZEALL:
gabomdq@8104
   277
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
gabomdq@8062
   278
        break;
gabomdq@8062
   279
    case SDL_SYSTEM_CURSOR_NO:
gabomdq@8104
   280
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "xterm");
gabomdq@8062
   281
        break;
gabomdq@8062
   282
    case SDL_SYSTEM_CURSOR_HAND:
gabomdq@8104
   283
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
gabomdq@8062
   284
        break;
gabomdq@8062
   285
    }
gabomdq@8062
   286
icculus@8721
   287
    return CreateCursorFromWlCursor(d, cursor);
gabomdq@8062
   288
}
gabomdq@8062
   289
gabomdq@8062
   290
static void
gabomdq@8062
   291
Wayland_FreeCursor(SDL_Cursor *cursor)
gabomdq@8062
   292
{
icculus@8721
   293
    Wayland_CursorData *d;
icculus@8721
   294
gabomdq@8062
   295
    if (!cursor)
gabomdq@8062
   296
        return;
gabomdq@8062
   297
icculus@8721
   298
    d = cursor->driverdata;
gabomdq@8062
   299
gabomdq@8062
   300
    /* Probably not a cursor we own */
gabomdq@8062
   301
    if (!d)
gabomdq@8062
   302
        return;
gabomdq@8062
   303
slouken@8711
   304
    if (d->buffer && !d->cursor)
gabomdq@8062
   305
        wl_buffer_destroy(d->buffer);
gabomdq@8062
   306
gabomdq@8062
   307
    if (d->surface)
gabomdq@8062
   308
        wl_surface_destroy(d->surface);
gabomdq@8062
   309
gabomdq@8062
   310
    /* Not sure what's meant to happen to shm_data */
gabomdq@8062
   311
    free (cursor->driverdata);
gabomdq@8062
   312
    SDL_free(cursor);
gabomdq@8062
   313
}
gabomdq@8062
   314
gabomdq@8062
   315
static int
gabomdq@8062
   316
Wayland_ShowCursor(SDL_Cursor *cursor)
gabomdq@8062
   317
{
gabomdq@8062
   318
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
gabomdq@8062
   319
    SDL_VideoData *d = vd->driverdata;
gabomdq@8062
   320
gabomdq@8062
   321
    struct wl_pointer *pointer = d->pointer;
gabomdq@8062
   322
gabomdq@8062
   323
    if (!pointer)
gabomdq@8062
   324
        return -1;
gabomdq@8062
   325
gabomdq@8062
   326
    if (cursor)
gabomdq@8062
   327
    {
gabomdq@8062
   328
        Wayland_CursorData *data = cursor->driverdata;
gabomdq@8062
   329
gabomdq@8062
   330
        wl_pointer_set_cursor (pointer, 0,
gabomdq@8062
   331
                               data->surface,
gabomdq@8062
   332
                               data->hot_x,
gabomdq@8062
   333
                               data->hot_y);
philipp@10113
   334
        wl_surface_attach(data->surface, data->buffer, 0, 0);
philipp@10113
   335
        wl_surface_damage(data->surface, 0, 0, data->w, data->h);
philipp@10113
   336
        wl_surface_commit(data->surface);
gabomdq@8062
   337
    }
gabomdq@8062
   338
    else
gabomdq@8062
   339
    {
gabomdq@8062
   340
        wl_pointer_set_cursor (pointer, 0,
gabomdq@8062
   341
                               NULL,
gabomdq@8062
   342
                               0,
gabomdq@8062
   343
                               0);
gabomdq@8062
   344
    }
gabomdq@8062
   345
    
gabomdq@8062
   346
    return 0;
gabomdq@8062
   347
}
gabomdq@8062
   348
gabomdq@8062
   349
static void
gabomdq@8062
   350
Wayland_WarpMouse(SDL_Window *window, int x, int y)
gabomdq@8062
   351
{
gabomdq@8062
   352
    SDL_Unsupported();
slouken@8815
   353
}
slouken@8815
   354
icculus@9807
   355
static int
slouken@8815
   356
Wayland_WarpMouseGlobal(int x, int y)
slouken@8815
   357
{
icculus@9807
   358
    return SDL_Unsupported();
gabomdq@8062
   359
}
gabomdq@8062
   360
gabomdq@8062
   361
static int
gabomdq@8062
   362
Wayland_SetRelativeMouseMode(SDL_bool enabled)
gabomdq@8062
   363
{
slouken@10304
   364
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
slouken@10304
   365
    SDL_VideoData *data = (SDL_VideoData *) vd->driverdata;
slouken@10304
   366
slouken@10304
   367
    if (enabled)
slouken@10304
   368
        return Wayland_input_lock_pointer(data->input);
slouken@10304
   369
    else
slouken@10304
   370
        return Wayland_input_unlock_pointer(data->input);
gabomdq@8062
   371
}
gabomdq@8062
   372
gabomdq@8062
   373
void
gabomdq@8062
   374
Wayland_InitMouse(void)
gabomdq@8062
   375
{
gabomdq@8062
   376
    SDL_Mouse *mouse = SDL_GetMouse();
gabomdq@8062
   377
gabomdq@8062
   378
    mouse->CreateCursor = Wayland_CreateCursor;
gabomdq@8062
   379
    mouse->CreateSystemCursor = Wayland_CreateSystemCursor;
gabomdq@8062
   380
    mouse->ShowCursor = Wayland_ShowCursor;
gabomdq@8062
   381
    mouse->FreeCursor = Wayland_FreeCursor;
gabomdq@8062
   382
    mouse->WarpMouse = Wayland_WarpMouse;
slouken@8815
   383
    mouse->WarpMouseGlobal = Wayland_WarpMouseGlobal;
gabomdq@8062
   384
    mouse->SetRelativeMouseMode = Wayland_SetRelativeMouseMode;
gabomdq@8062
   385
gabomdq@8062
   386
    SDL_SetDefaultCursor(Wayland_CreateDefaultCursor());
gabomdq@8062
   387
}
gabomdq@8062
   388
gabomdq@8062
   389
void
gabomdq@8062
   390
Wayland_FiniMouse(void)
gabomdq@8062
   391
{
gabomdq@8062
   392
    /* This effectively assumes that nobody else
gabomdq@8062
   393
     * touches SDL_Mouse which is effectively
gabomdq@8062
   394
     * a singleton */
gabomdq@8062
   395
gabomdq@8062
   396
    SDL_Mouse *mouse = SDL_GetMouse();
gabomdq@8062
   397
gabomdq@8062
   398
    /* Free the current cursor if not the same pointer as
gabomdq@8062
   399
     * the default cursor */
gabomdq@8062
   400
    if (mouse->def_cursor != mouse->cur_cursor)
gabomdq@8062
   401
        Wayland_FreeCursor (mouse->cur_cursor);
gabomdq@8062
   402
gabomdq@8062
   403
    Wayland_FreeCursor (mouse->def_cursor);
gabomdq@8062
   404
    mouse->def_cursor = NULL;
gabomdq@8062
   405
    mouse->cur_cursor = NULL;
gabomdq@8062
   406
gabomdq@8062
   407
    mouse->CreateCursor =  NULL;
gabomdq@8062
   408
    mouse->CreateSystemCursor = NULL;
gabomdq@8062
   409
    mouse->ShowCursor = NULL;
gabomdq@8062
   410
    mouse->FreeCursor = NULL;
gabomdq@8062
   411
    mouse->WarpMouse = NULL;
gabomdq@8062
   412
    mouse->SetRelativeMouseMode = NULL;
gabomdq@8062
   413
}
icculus@8116
   414
#endif  /* SDL_VIDEO_DRIVER_WAYLAND */