src/video/wayland/SDL_waylandmouse.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Fri, 11 Mar 2016 22:09:50 +0100
changeset 10113 991977532d7f
parent 10098 91b34fbbb88e
child 10114 6740b15172f0
permissions -rw-r--r--
Wayland: Fixed showing created cursors incorrectly before the first redraw.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     1
/*
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9807
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     4
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     7
  arising from the use of this software.
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     8
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    12
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    16
     appreciated but is not required.
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    18
     misrepresented as being the original software.
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    20
*/
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    21
8116
f7c2f71251e5 Patched to compile if Wayland is disabled via SDL_config.h (thanks, Martin!).
Ryan C. Gordon <icculus@icculus.org>
parents: 8104
diff changeset
    22
#include "../../SDL_internal.h"
f7c2f71251e5 Patched to compile if Wayland is disabled via SDL_config.h (thanks, Martin!).
Ryan C. Gordon <icculus@icculus.org>
parents: 8104
diff changeset
    23
f7c2f71251e5 Patched to compile if Wayland is disabled via SDL_config.h (thanks, Martin!).
Ryan C. Gordon <icculus@icculus.org>
parents: 8104
diff changeset
    24
#if SDL_VIDEO_DRIVER_WAYLAND
f7c2f71251e5 Patched to compile if Wayland is disabled via SDL_config.h (thanks, Martin!).
Ryan C. Gordon <icculus@icculus.org>
parents: 8104
diff changeset
    25
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    26
#ifndef _GNU_SOURCE
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    27
#define _GNU_SOURCE
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    28
#endif
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    29
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    30
#include <errno.h>
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    31
#include <sys/types.h>
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    32
#include <sys/mman.h>
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    33
#include <fcntl.h>
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    34
#include <unistd.h>
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    35
#include <stdlib.h>
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    36
#include <limits.h>
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    37
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    38
#include "../SDL_sysvideo.h"
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    39
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    40
#include "SDL_mouse.h"
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    41
#include "../../events/SDL_mouse_c.h"
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    42
#include "SDL_waylandvideo.h"
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    43
#include "SDL_waylandevents_c.h"
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    44
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
    45
#include "SDL_waylanddyn.h"
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
    46
#include "wayland-cursor.h"
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
    47
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    48
#include "SDL_assert.h"
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    49
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    50
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    51
typedef struct {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    52
    struct wl_buffer   *buffer;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    53
    struct wl_surface  *surface;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    54
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    55
    int                hot_x, hot_y;
8711
46f85bfe9303 Fixed bug 2485 - [PATCH] Wayland: cursor disappears permanently after window loses mouse focus
Sam Lantinga <slouken@libsdl.org>
parents: 8710
diff changeset
    56
    int                w, h;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    57
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    58
    /* Either a preloaded cursor, or one we created ourselves */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    59
    struct wl_cursor   *cursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    60
    void               *shm_data;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    61
} Wayland_CursorData;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    62
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    63
static int
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    64
wayland_create_tmp_file(off_t size)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    65
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    66
    static const char template[] = "/sdl-shared-XXXXXX";
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    67
    char *xdg_path;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    68
    char tmp_path[PATH_MAX];
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    69
    int fd;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    70
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    71
    xdg_path = SDL_getenv("XDG_RUNTIME_DIR");
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    72
    if (!xdg_path) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    73
        errno = ENOENT;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    74
        return -1;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    75
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    76
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    77
    SDL_strlcpy(tmp_path, xdg_path, PATH_MAX);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    78
    SDL_strlcat(tmp_path, template, PATH_MAX);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    79
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    80
    fd = mkostemp(tmp_path, O_CLOEXEC);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    81
    if (fd < 0)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    82
        return -1;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    83
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    84
    if (ftruncate(fd, size) < 0) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    85
        close(fd);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    86
        return -1;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    87
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    88
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    89
    return fd;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    90
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    91
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    92
static void
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    93
mouse_buffer_release(void *data, struct wl_buffer *buffer)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    94
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    95
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    96
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    97
static const struct wl_buffer_listener mouse_buffer_listener = {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    98
    mouse_buffer_release
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    99
};
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   100
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   101
static int
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   102
create_buffer_from_shm(Wayland_CursorData *d,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   103
                       int width,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   104
                       int height,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   105
                       uint32_t format)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   106
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   107
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   108
    SDL_VideoData *data = (SDL_VideoData *) vd->driverdata;
8721
d73e451f1dc5 Fixed some issues reported on new Ubuntu 14.04 buildbots.
Ryan C. Gordon <icculus@icculus.org>
parents: 8711
diff changeset
   109
    struct wl_shm_pool *shm_pool;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   110
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   111
    int stride = width * 4;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   112
    int size = stride * height;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   113
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   114
    int shm_fd;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   115
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   116
    shm_fd = wayland_create_tmp_file(size);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   117
    if (shm_fd < 0)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   118
    {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   119
        fprintf(stderr, "creating mouse cursor buffer failed!\n");
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   120
        return -1;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   121
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   122
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   123
    d->shm_data = mmap(NULL,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   124
                       size,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   125
                       PROT_READ | PROT_WRITE,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   126
                       MAP_SHARED,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   127
                       shm_fd,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   128
                       0);
9136
64fcdfcd5bca video: waylandmouse: handle mmap error properly
Ryo Munakata <ryomnktml@gmail.com>
parents: 8815
diff changeset
   129
    if (d->shm_data == MAP_FAILED) {
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   130
        d->shm_data = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   131
        fprintf (stderr, "mmap () failed\n");
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   132
        close (shm_fd);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   133
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   134
8721
d73e451f1dc5 Fixed some issues reported on new Ubuntu 14.04 buildbots.
Ryan C. Gordon <icculus@icculus.org>
parents: 8711
diff changeset
   135
    shm_pool = wl_shm_create_pool(data->shm, shm_fd, size);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   136
    d->buffer = wl_shm_pool_create_buffer(shm_pool,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   137
                                          0,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   138
                                          width,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   139
                                          height,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   140
                                          stride,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   141
                                          format);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   142
    wl_buffer_add_listener(d->buffer,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   143
                           &mouse_buffer_listener,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   144
                           d);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   145
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   146
    wl_shm_pool_destroy (shm_pool);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   147
    close (shm_fd);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   148
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   149
    return 0;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   150
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   151
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   152
static SDL_Cursor *
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   153
Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   154
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   155
    SDL_Cursor *cursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   156
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   157
    cursor = calloc(1, sizeof (*cursor));
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   158
    if (cursor) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   159
        SDL_VideoDevice *vd = SDL_GetVideoDevice ();
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   160
        SDL_VideoData *wd = (SDL_VideoData *) vd->driverdata;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   161
        Wayland_CursorData *data = calloc (1, sizeof (Wayland_CursorData));
10098
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   162
        if (!data) {
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   163
            SDL_OutOfMemory();
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   164
            free(cursor);
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   165
            return NULL;
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   166
        }
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   167
        cursor->driverdata = (void *) data;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   168
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   169
        /* Assume ARGB8888 */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   170
        SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   171
        SDL_assert(surface->pitch == surface->w * 4);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   172
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   173
        /* Allocate shared memory buffer for this cursor */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   174
        if (create_buffer_from_shm (data,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   175
                                    surface->w,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   176
                                    surface->h,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   177
                                    WL_SHM_FORMAT_XRGB8888) < 0)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   178
        {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   179
            free (cursor->driverdata);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   180
            free (cursor);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   181
            return NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   182
        }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   183
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   184
        SDL_memcpy(data->shm_data,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   185
                   surface->pixels,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   186
                   surface->h * surface->pitch);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   187
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   188
        data->surface = wl_compositor_create_surface(wd->compositor);
8135
dd4729596096 [Wayland] Fixes segfault when mouse enters window
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8116
diff changeset
   189
        wl_surface_set_user_data(data->surface, NULL);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   190
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   191
        data->hot_x = hot_x;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   192
        data->hot_y = hot_y;
8711
46f85bfe9303 Fixed bug 2485 - [PATCH] Wayland: cursor disappears permanently after window loses mouse focus
Sam Lantinga <slouken@libsdl.org>
parents: 8710
diff changeset
   193
        data->w = surface->w;
46f85bfe9303 Fixed bug 2485 - [PATCH] Wayland: cursor disappears permanently after window loses mouse focus
Sam Lantinga <slouken@libsdl.org>
parents: 8710
diff changeset
   194
        data->h = surface->h;
10098
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   195
    } else {
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   196
        SDL_OutOfMemory();
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   197
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   198
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   199
    return cursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   200
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   201
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   202
static SDL_Cursor *
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   203
CreateCursorFromWlCursor(SDL_VideoData *d, struct wl_cursor *wlcursor)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   204
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   205
    SDL_Cursor *cursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   206
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   207
    cursor = calloc(1, sizeof (*cursor));
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   208
    if (cursor) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   209
        Wayland_CursorData *data = calloc (1, sizeof (Wayland_CursorData));
10098
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   210
        if (!data) {
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   211
            SDL_OutOfMemory();
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   212
            free(cursor);
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   213
            return NULL;
91b34fbbb88e Wayland: Fixed crash if allocating memory for cursor failed.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9998
diff changeset
   214
        }
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   215
        cursor->driverdata = (void *) data;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   216
8711
46f85bfe9303 Fixed bug 2485 - [PATCH] Wayland: cursor disappears permanently after window loses mouse focus
Sam Lantinga <slouken@libsdl.org>
parents: 8710
diff changeset
   217
        data->buffer = WAYLAND_wl_cursor_image_get_buffer(wlcursor->images[0]);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   218
        data->surface = wl_compositor_create_surface(d->compositor);
8135
dd4729596096 [Wayland] Fixes segfault when mouse enters window
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8116
diff changeset
   219
        wl_surface_set_user_data(data->surface, NULL);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   220
        data->hot_x = wlcursor->images[0]->hotspot_x;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   221
        data->hot_y = wlcursor->images[0]->hotspot_y;
8711
46f85bfe9303 Fixed bug 2485 - [PATCH] Wayland: cursor disappears permanently after window loses mouse focus
Sam Lantinga <slouken@libsdl.org>
parents: 8710
diff changeset
   222
        data->w = wlcursor->images[0]->width;
46f85bfe9303 Fixed bug 2485 - [PATCH] Wayland: cursor disappears permanently after window loses mouse focus
Sam Lantinga <slouken@libsdl.org>
parents: 8710
diff changeset
   223
        data->h = wlcursor->images[0]->height;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   224
        data->cursor= wlcursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   225
    } else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   226
        SDL_OutOfMemory ();
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   227
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   228
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   229
    return cursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   230
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   231
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   232
static SDL_Cursor *
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   233
Wayland_CreateDefaultCursor()
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   234
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   235
    SDL_VideoDevice *device = SDL_GetVideoDevice();
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   236
    SDL_VideoData *data = device->driverdata;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   237
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   238
    return CreateCursorFromWlCursor (data,
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   239
                                     WAYLAND_wl_cursor_theme_get_cursor(data->cursor_theme,
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   240
                                                                "left_ptr"));
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   241
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   242
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   243
static SDL_Cursor *
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   244
Wayland_CreateSystemCursor(SDL_SystemCursor id)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   245
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   246
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   247
    SDL_VideoData *d = vd->driverdata;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   248
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   249
    struct wl_cursor *cursor = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   250
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   251
    switch(id)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   252
    {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   253
    default:
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   254
        SDL_assert(0);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   255
        return NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   256
    case SDL_SYSTEM_CURSOR_ARROW:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   257
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   258
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   259
    case SDL_SYSTEM_CURSOR_IBEAM:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   260
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "xterm");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   261
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   262
    case SDL_SYSTEM_CURSOR_WAIT:
8710
403c95143f8a Fixed bug 2482 - Wayland_CreateSystemCursor trying to load nonexistent "wait" cursor
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   263
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "watch");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   264
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   265
    case SDL_SYSTEM_CURSOR_CROSSHAIR:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   266
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   267
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   268
    case SDL_SYSTEM_CURSOR_WAITARROW:
8710
403c95143f8a Fixed bug 2482 - Wayland_CreateSystemCursor trying to load nonexistent "wait" cursor
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   269
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "watch");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   270
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   271
    case SDL_SYSTEM_CURSOR_SIZENWSE:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   272
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   273
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   274
    case SDL_SYSTEM_CURSOR_SIZENESW:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   275
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   276
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   277
    case SDL_SYSTEM_CURSOR_SIZEWE:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   278
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   279
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   280
    case SDL_SYSTEM_CURSOR_SIZENS:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   281
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   282
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   283
    case SDL_SYSTEM_CURSOR_SIZEALL:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   284
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   285
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   286
    case SDL_SYSTEM_CURSOR_NO:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   287
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "xterm");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   288
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   289
    case SDL_SYSTEM_CURSOR_HAND:
8104
2e4f1bd21196 Dynamic loading support for Wayland
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8062
diff changeset
   290
        cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   291
        break;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   292
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   293
8721
d73e451f1dc5 Fixed some issues reported on new Ubuntu 14.04 buildbots.
Ryan C. Gordon <icculus@icculus.org>
parents: 8711
diff changeset
   294
    return CreateCursorFromWlCursor(d, cursor);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   295
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   296
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   297
static void
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   298
Wayland_FreeCursor(SDL_Cursor *cursor)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   299
{
8721
d73e451f1dc5 Fixed some issues reported on new Ubuntu 14.04 buildbots.
Ryan C. Gordon <icculus@icculus.org>
parents: 8711
diff changeset
   300
    Wayland_CursorData *d;
d73e451f1dc5 Fixed some issues reported on new Ubuntu 14.04 buildbots.
Ryan C. Gordon <icculus@icculus.org>
parents: 8711
diff changeset
   301
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   302
    if (!cursor)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   303
        return;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   304
8721
d73e451f1dc5 Fixed some issues reported on new Ubuntu 14.04 buildbots.
Ryan C. Gordon <icculus@icculus.org>
parents: 8711
diff changeset
   305
    d = cursor->driverdata;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   306
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   307
    /* Probably not a cursor we own */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   308
    if (!d)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   309
        return;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   310
8711
46f85bfe9303 Fixed bug 2485 - [PATCH] Wayland: cursor disappears permanently after window loses mouse focus
Sam Lantinga <slouken@libsdl.org>
parents: 8710
diff changeset
   311
    if (d->buffer && !d->cursor)
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   312
        wl_buffer_destroy(d->buffer);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   313
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   314
    if (d->surface)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   315
        wl_surface_destroy(d->surface);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   316
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   317
    /* Not sure what's meant to happen to shm_data */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   318
    free (cursor->driverdata);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   319
    SDL_free(cursor);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   320
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   321
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   322
static int
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   323
Wayland_ShowCursor(SDL_Cursor *cursor)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   324
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   325
    SDL_VideoDevice *vd = SDL_GetVideoDevice();
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   326
    SDL_VideoData *d = vd->driverdata;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   327
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   328
    struct wl_pointer *pointer = d->pointer;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   329
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   330
    if (!pointer)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   331
        return -1;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   332
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   333
    if (cursor)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   334
    {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   335
        Wayland_CursorData *data = cursor->driverdata;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   336
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   337
        wl_pointer_set_cursor (pointer, 0,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   338
                               data->surface,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   339
                               data->hot_x,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   340
                               data->hot_y);
10113
991977532d7f Wayland: Fixed showing created cursors incorrectly before the first redraw.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10098
diff changeset
   341
        wl_surface_attach(data->surface, data->buffer, 0, 0);
991977532d7f Wayland: Fixed showing created cursors incorrectly before the first redraw.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10098
diff changeset
   342
        wl_surface_damage(data->surface, 0, 0, data->w, data->h);
991977532d7f Wayland: Fixed showing created cursors incorrectly before the first redraw.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10098
diff changeset
   343
        wl_surface_commit(data->surface);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   344
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   345
    else
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   346
    {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   347
        wl_pointer_set_cursor (pointer, 0,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   348
                               NULL,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   349
                               0,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   350
                               0);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   351
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   352
    
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   353
    return 0;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   354
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   355
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   356
static void
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   357
Wayland_WarpMouse(SDL_Window *window, int x, int y)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   358
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   359
    SDL_Unsupported();
8815
c6d0a457f3b2 Added an API function to warp the mouse cursor in global screen space: SDL_WarpMouseGlobal()
Sam Lantinga <slouken@libsdl.org>
parents: 8721
diff changeset
   360
}
c6d0a457f3b2 Added an API function to warp the mouse cursor in global screen space: SDL_WarpMouseGlobal()
Sam Lantinga <slouken@libsdl.org>
parents: 8721
diff changeset
   361
9807
57b448735f48 SDL_WarpMouseGlobal() should return non-void.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   362
static int
8815
c6d0a457f3b2 Added an API function to warp the mouse cursor in global screen space: SDL_WarpMouseGlobal()
Sam Lantinga <slouken@libsdl.org>
parents: 8721
diff changeset
   363
Wayland_WarpMouseGlobal(int x, int y)
c6d0a457f3b2 Added an API function to warp the mouse cursor in global screen space: SDL_WarpMouseGlobal()
Sam Lantinga <slouken@libsdl.org>
parents: 8721
diff changeset
   364
{
9807
57b448735f48 SDL_WarpMouseGlobal() should return non-void.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   365
    return SDL_Unsupported();
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   366
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   367
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   368
static int
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   369
Wayland_SetRelativeMouseMode(SDL_bool enabled)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   370
{
9469
3f8d36ffd19d Wayland: changed a few "SetError(); return -1;" to "return SetError()".
Ryan C. Gordon <icculus@icculus.org>
parents: 9136
diff changeset
   371
    return SDL_Unsupported();
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   372
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   373
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   374
void
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   375
Wayland_InitMouse(void)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   376
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   377
    SDL_Mouse *mouse = SDL_GetMouse();
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   378
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   379
    mouse->CreateCursor = Wayland_CreateCursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   380
    mouse->CreateSystemCursor = Wayland_CreateSystemCursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   381
    mouse->ShowCursor = Wayland_ShowCursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   382
    mouse->FreeCursor = Wayland_FreeCursor;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   383
    mouse->WarpMouse = Wayland_WarpMouse;
8815
c6d0a457f3b2 Added an API function to warp the mouse cursor in global screen space: SDL_WarpMouseGlobal()
Sam Lantinga <slouken@libsdl.org>
parents: 8721
diff changeset
   384
    mouse->WarpMouseGlobal = Wayland_WarpMouseGlobal;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   385
    mouse->SetRelativeMouseMode = Wayland_SetRelativeMouseMode;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   386
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   387
    SDL_SetDefaultCursor(Wayland_CreateDefaultCursor());
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   388
}
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   389
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   390
void
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   391
Wayland_FiniMouse(void)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   392
{
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   393
    /* This effectively assumes that nobody else
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   394
     * touches SDL_Mouse which is effectively
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   395
     * a singleton */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   396
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   397
    SDL_Mouse *mouse = SDL_GetMouse();
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   398
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   399
    /* Free the current cursor if not the same pointer as
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   400
     * the default cursor */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   401
    if (mouse->def_cursor != mouse->cur_cursor)
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   402
        Wayland_FreeCursor (mouse->cur_cursor);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   403
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   404
    Wayland_FreeCursor (mouse->def_cursor);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   405
    mouse->def_cursor = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   406
    mouse->cur_cursor = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   407
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   408
    mouse->CreateCursor =  NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   409
    mouse->CreateSystemCursor = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   410
    mouse->ShowCursor = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   411
    mouse->FreeCursor = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   412
    mouse->WarpMouse = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   413
    mouse->SetRelativeMouseMode = NULL;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   414
}
8116
f7c2f71251e5 Patched to compile if Wayland is disabled via SDL_config.h (thanks, Martin!).
Ryan C. Gordon <icculus@icculus.org>
parents: 8104
diff changeset
   415
#endif  /* SDL_VIDEO_DRIVER_WAYLAND */