src/video/wayland/SDL_waylandmouse.c
changeset 8062 4fc5f66d63cc
child 8104 2e4f1bd21196
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/wayland/SDL_waylandmouse.c	Sat Dec 14 20:18:43 2013 -0300
     1.3 @@ -0,0 +1,407 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#ifndef _GNU_SOURCE
    1.26 +#define _GNU_SOURCE
    1.27 +#endif
    1.28 +
    1.29 +#include <errno.h>
    1.30 +#include <sys/types.h>
    1.31 +#include <sys/mman.h>
    1.32 +#include <fcntl.h>
    1.33 +#include <unistd.h>
    1.34 +#include <stdlib.h>
    1.35 +#include <limits.h>
    1.36 +
    1.37 +#include "../SDL_sysvideo.h"
    1.38 +
    1.39 +#include "SDL_config.h"
    1.40 +#include "SDL_mouse.h"
    1.41 +#include "../../events/SDL_mouse_c.h"
    1.42 +#include "SDL_waylandvideo.h"
    1.43 +#include "SDL_waylandevents_c.h"
    1.44 +
    1.45 +#include "SDL_assert.h"
    1.46 +
    1.47 +#if SDL_VIDEO_DRIVER_WAYLAND
    1.48 +
    1.49 +typedef struct {
    1.50 +    struct wl_buffer   *buffer;
    1.51 +    struct wl_surface  *surface;
    1.52 +
    1.53 +    int                hot_x, hot_y;
    1.54 +
    1.55 +    /* Either a preloaded cursor, or one we created ourselves */
    1.56 +    struct wl_cursor   *cursor;
    1.57 +    void               *shm_data;
    1.58 +} Wayland_CursorData;
    1.59 +
    1.60 +static int
    1.61 +wayland_create_tmp_file(off_t size)
    1.62 +{
    1.63 +    static const char template[] = "/sdl-shared-XXXXXX";
    1.64 +    char *xdg_path;
    1.65 +    char tmp_path[PATH_MAX];
    1.66 +    int fd;
    1.67 +
    1.68 +    xdg_path = SDL_getenv("XDG_RUNTIME_DIR");
    1.69 +    if (!xdg_path) {
    1.70 +        errno = ENOENT;
    1.71 +        return -1;
    1.72 +    }
    1.73 +
    1.74 +    SDL_strlcpy(tmp_path, xdg_path, PATH_MAX);
    1.75 +    SDL_strlcat(tmp_path, template, PATH_MAX);
    1.76 +
    1.77 +    fd = mkostemp(tmp_path, O_CLOEXEC);
    1.78 +    if (fd < 0)
    1.79 +        return -1;
    1.80 +
    1.81 +    if (ftruncate(fd, size) < 0) {
    1.82 +        close(fd);
    1.83 +        return -1;
    1.84 +    }
    1.85 +
    1.86 +    return fd;
    1.87 +}
    1.88 +
    1.89 +static void
    1.90 +mouse_buffer_release(void *data, struct wl_buffer *buffer)
    1.91 +{
    1.92 +}
    1.93 +
    1.94 +static const struct wl_buffer_listener mouse_buffer_listener = {
    1.95 +    mouse_buffer_release
    1.96 +};
    1.97 +
    1.98 +static int
    1.99 +create_buffer_from_shm(Wayland_CursorData *d,
   1.100 +                       int width,
   1.101 +                       int height,
   1.102 +                       uint32_t format)
   1.103 +{
   1.104 +    SDL_VideoDevice *vd = SDL_GetVideoDevice();
   1.105 +    SDL_VideoData *data = (SDL_VideoData *) vd->driverdata;
   1.106 +
   1.107 +    int stride = width * 4;
   1.108 +    int size = stride * height;
   1.109 +
   1.110 +    int shm_fd;
   1.111 +
   1.112 +    shm_fd = wayland_create_tmp_file(size);
   1.113 +    if (shm_fd < 0)
   1.114 +    {
   1.115 +        fprintf(stderr, "creating mouse cursor buffer failed!\n");
   1.116 +        return -1;
   1.117 +    }
   1.118 +
   1.119 +    d->shm_data = mmap(NULL,
   1.120 +                       size,
   1.121 +                       PROT_READ | PROT_WRITE,
   1.122 +                       MAP_SHARED,
   1.123 +                       shm_fd,
   1.124 +                       0);
   1.125 +    if (data == MAP_FAILED) {
   1.126 +        d->shm_data = NULL;
   1.127 +        fprintf (stderr, "mmap () failed\n");
   1.128 +        close (shm_fd);
   1.129 +    }
   1.130 +
   1.131 +    struct wl_shm_pool *shm_pool = wl_shm_create_pool(data->shm,
   1.132 +                                                      shm_fd,
   1.133 +                                                      size);
   1.134 +    d->buffer = wl_shm_pool_create_buffer(shm_pool,
   1.135 +                                          0,
   1.136 +                                          width,
   1.137 +                                          height,
   1.138 +                                          stride,
   1.139 +                                          format);
   1.140 +    wl_buffer_add_listener(d->buffer,
   1.141 +                           &mouse_buffer_listener,
   1.142 +                           d);
   1.143 +
   1.144 +    wl_shm_pool_destroy (shm_pool);
   1.145 +    close (shm_fd);
   1.146 +
   1.147 +    return 0;
   1.148 +}
   1.149 +
   1.150 +static SDL_Cursor *
   1.151 +Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
   1.152 +{
   1.153 +    SDL_Cursor *cursor;
   1.154 +
   1.155 +    cursor = calloc(1, sizeof (*cursor));
   1.156 +    if (cursor) {
   1.157 +        SDL_VideoDevice *vd = SDL_GetVideoDevice ();
   1.158 +        SDL_VideoData *wd = (SDL_VideoData *) vd->driverdata;
   1.159 +        Wayland_CursorData *data = calloc (1, sizeof (Wayland_CursorData));
   1.160 +        cursor->driverdata = (void *) data;
   1.161 +
   1.162 +        /* Assume ARGB8888 */
   1.163 +        SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
   1.164 +        SDL_assert(surface->pitch == surface->w * 4);
   1.165 +
   1.166 +        /* Allocate shared memory buffer for this cursor */
   1.167 +        if (create_buffer_from_shm (data,
   1.168 +                                    surface->w,
   1.169 +                                    surface->h,
   1.170 +                                    WL_SHM_FORMAT_XRGB8888) < 0)
   1.171 +        {
   1.172 +            free (cursor->driverdata);
   1.173 +            free (cursor);
   1.174 +            return NULL;
   1.175 +        }
   1.176 +
   1.177 +        SDL_memcpy(data->shm_data,
   1.178 +                   surface->pixels,
   1.179 +                   surface->h * surface->pitch);
   1.180 +
   1.181 +        data->surface = wl_compositor_create_surface(wd->compositor);
   1.182 +        wl_surface_attach(data->surface,
   1.183 +                          data->buffer,
   1.184 +                          0,
   1.185 +                          0);
   1.186 +        wl_surface_damage(data->surface,
   1.187 +                          0,
   1.188 +                          0,
   1.189 +                          surface->w,
   1.190 +                          surface->h);
   1.191 +        wl_surface_commit(data->surface);
   1.192 +
   1.193 +        data->hot_x = hot_x;
   1.194 +        data->hot_y = hot_y;
   1.195 +    }
   1.196 +
   1.197 +    return cursor;
   1.198 +}
   1.199 +
   1.200 +static SDL_Cursor *
   1.201 +CreateCursorFromWlCursor(SDL_VideoData *d, struct wl_cursor *wlcursor)
   1.202 +{
   1.203 +    SDL_Cursor *cursor;
   1.204 +
   1.205 +    cursor = calloc(1, sizeof (*cursor));
   1.206 +    if (cursor) {
   1.207 +        Wayland_CursorData *data = calloc (1, sizeof (Wayland_CursorData));
   1.208 +        cursor->driverdata = (void *) data;
   1.209 +
   1.210 +        /* The wl_buffer here will be destroyed from wl_cursor_theme_destroy
   1.211 +         * if we are fetching this from a wl_cursor_theme, so don't store a
   1.212 +         * reference to it here */
   1.213 +        data->buffer = NULL;
   1.214 +        data->surface = wl_compositor_create_surface(d->compositor);
   1.215 +        wl_surface_attach(data->surface,
   1.216 +                          wl_cursor_image_get_buffer(wlcursor->images[0]),
   1.217 +                          0,
   1.218 +                          0);
   1.219 +        wl_surface_damage(data->surface,
   1.220 +                          0,
   1.221 +                          0,
   1.222 +                          wlcursor->images[0]->width,
   1.223 +                          wlcursor->images[0]->height);
   1.224 +        wl_surface_commit(data->surface);
   1.225 +        data->hot_x = wlcursor->images[0]->hotspot_x;
   1.226 +        data->hot_y = wlcursor->images[0]->hotspot_y;
   1.227 +        data->cursor= wlcursor;
   1.228 +    } else {
   1.229 +        SDL_OutOfMemory ();
   1.230 +    }
   1.231 +
   1.232 +    return cursor;
   1.233 +}
   1.234 +
   1.235 +static SDL_Cursor *
   1.236 +Wayland_CreateDefaultCursor()
   1.237 +{
   1.238 +    SDL_VideoDevice *device = SDL_GetVideoDevice();
   1.239 +    SDL_VideoData *data = device->driverdata;
   1.240 +
   1.241 +    return CreateCursorFromWlCursor (data,
   1.242 +                                     wl_cursor_theme_get_cursor(data->cursor_theme,
   1.243 +                                                                "left_ptr"));
   1.244 +}
   1.245 +
   1.246 +static SDL_Cursor *
   1.247 +Wayland_CreateSystemCursor(SDL_SystemCursor id)
   1.248 +{
   1.249 +    SDL_VideoDevice *vd = SDL_GetVideoDevice();
   1.250 +    SDL_VideoData *d = vd->driverdata;
   1.251 +
   1.252 +    struct wl_cursor *cursor = NULL;
   1.253 +
   1.254 +    switch(id)
   1.255 +    {
   1.256 +    default:
   1.257 +        SDL_assert(0);
   1.258 +        return NULL;
   1.259 +    case SDL_SYSTEM_CURSOR_ARROW:
   1.260 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
   1.261 +        break;
   1.262 +    case SDL_SYSTEM_CURSOR_IBEAM:
   1.263 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "xterm");
   1.264 +        break;
   1.265 +    case SDL_SYSTEM_CURSOR_WAIT:
   1.266 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "wait");
   1.267 +        break;
   1.268 +    case SDL_SYSTEM_CURSOR_CROSSHAIR:
   1.269 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
   1.270 +        break;
   1.271 +    case SDL_SYSTEM_CURSOR_WAITARROW:
   1.272 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "wait");
   1.273 +        break;
   1.274 +    case SDL_SYSTEM_CURSOR_SIZENWSE:
   1.275 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
   1.276 +        break;
   1.277 +    case SDL_SYSTEM_CURSOR_SIZENESW:
   1.278 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
   1.279 +        break;
   1.280 +    case SDL_SYSTEM_CURSOR_SIZEWE:
   1.281 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
   1.282 +        break;
   1.283 +    case SDL_SYSTEM_CURSOR_SIZENS:
   1.284 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
   1.285 +        break;
   1.286 +    case SDL_SYSTEM_CURSOR_SIZEALL:
   1.287 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
   1.288 +        break;
   1.289 +    case SDL_SYSTEM_CURSOR_NO:
   1.290 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "xterm");
   1.291 +        break;
   1.292 +    case SDL_SYSTEM_CURSOR_HAND:
   1.293 +        cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
   1.294 +        break;
   1.295 +    }
   1.296 +
   1.297 +    SDL_Cursor *sdl_cursor = CreateCursorFromWlCursor (d, cursor);
   1.298 +
   1.299 +    return sdl_cursor;
   1.300 +}
   1.301 +
   1.302 +static void
   1.303 +Wayland_FreeCursor(SDL_Cursor *cursor)
   1.304 +{
   1.305 +    if (!cursor)
   1.306 +        return;
   1.307 +
   1.308 +    Wayland_CursorData *d = cursor->driverdata;
   1.309 +
   1.310 +    /* Probably not a cursor we own */
   1.311 +    if (!d)
   1.312 +        return;
   1.313 +
   1.314 +    if (d->buffer)
   1.315 +        wl_buffer_destroy(d->buffer);
   1.316 +
   1.317 +    if (d->surface)
   1.318 +        wl_surface_destroy(d->surface);
   1.319 +
   1.320 +    /* Not sure what's meant to happen to shm_data */
   1.321 +    free (cursor->driverdata);
   1.322 +    SDL_free(cursor);
   1.323 +}
   1.324 +
   1.325 +static int
   1.326 +Wayland_ShowCursor(SDL_Cursor *cursor)
   1.327 +{
   1.328 +    SDL_VideoDevice *vd = SDL_GetVideoDevice();
   1.329 +    SDL_VideoData *d = vd->driverdata;
   1.330 +
   1.331 +    struct wl_pointer *pointer = d->pointer;
   1.332 +
   1.333 +    if (!pointer)
   1.334 +        return -1;
   1.335 +
   1.336 +    if (cursor)
   1.337 +    {
   1.338 +        Wayland_CursorData *data = cursor->driverdata;
   1.339 +
   1.340 +        wl_pointer_set_cursor (pointer, 0,
   1.341 +                               data->surface,
   1.342 +                               data->hot_x,
   1.343 +                               data->hot_y);
   1.344 +    }
   1.345 +    else
   1.346 +    {
   1.347 +        wl_pointer_set_cursor (pointer, 0,
   1.348 +                               NULL,
   1.349 +                               0,
   1.350 +                               0);
   1.351 +    }
   1.352 +    
   1.353 +    return 0;
   1.354 +}
   1.355 +
   1.356 +static void
   1.357 +Wayland_WarpMouse(SDL_Window *window, int x, int y)
   1.358 +{
   1.359 +    SDL_Unsupported();
   1.360 +    return;
   1.361 +}
   1.362 +
   1.363 +static int
   1.364 +Wayland_SetRelativeMouseMode(SDL_bool enabled)
   1.365 +{
   1.366 +    SDL_Unsupported();
   1.367 +    return -1;
   1.368 +}
   1.369 +
   1.370 +void
   1.371 +Wayland_InitMouse(void)
   1.372 +{
   1.373 +    SDL_Mouse *mouse = SDL_GetMouse();
   1.374 +
   1.375 +    mouse->CreateCursor = Wayland_CreateCursor;
   1.376 +    mouse->CreateSystemCursor = Wayland_CreateSystemCursor;
   1.377 +    mouse->ShowCursor = Wayland_ShowCursor;
   1.378 +    mouse->FreeCursor = Wayland_FreeCursor;
   1.379 +    mouse->WarpMouse = Wayland_WarpMouse;
   1.380 +    mouse->SetRelativeMouseMode = Wayland_SetRelativeMouseMode;
   1.381 +
   1.382 +    SDL_SetDefaultCursor(Wayland_CreateDefaultCursor());
   1.383 +}
   1.384 +
   1.385 +void
   1.386 +Wayland_FiniMouse(void)
   1.387 +{
   1.388 +    /* This effectively assumes that nobody else
   1.389 +     * touches SDL_Mouse which is effectively
   1.390 +     * a singleton */
   1.391 +
   1.392 +    SDL_Mouse *mouse = SDL_GetMouse();
   1.393 +
   1.394 +    /* Free the current cursor if not the same pointer as
   1.395 +     * the default cursor */
   1.396 +    if (mouse->def_cursor != mouse->cur_cursor)
   1.397 +        Wayland_FreeCursor (mouse->cur_cursor);
   1.398 +
   1.399 +    Wayland_FreeCursor (mouse->def_cursor);
   1.400 +    mouse->def_cursor = NULL;
   1.401 +    mouse->cur_cursor = NULL;
   1.402 +
   1.403 +    mouse->CreateCursor =  NULL;
   1.404 +    mouse->CreateSystemCursor = NULL;
   1.405 +    mouse->ShowCursor = NULL;
   1.406 +    mouse->FreeCursor = NULL;
   1.407 +    mouse->WarpMouse = NULL;
   1.408 +    mouse->SetRelativeMouseMode = NULL;
   1.409 +}
   1.410 +#endif