SDL_waylandmouse.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_WAYLAND
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include "../SDL_sysvideo.h"
#include "SDL_mouse.h"
#include "../../events/SDL_mouse_c.h"
#include "SDL_waylandvideo.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylanddyn.h"
#include "wayland-cursor.h"
#include "SDL_assert.h"
typedef struct {
struct wl_buffer *buffer;
struct wl_surface *surface;
int hot_x, hot_y;
int w, h;
/* Either a preloaded cursor, or one we created ourselves */
struct wl_cursor *cursor;
void *shm_data;
} Wayland_CursorData;
static int
wayland_create_tmp_file(off_t size)
{
static const char template[] = "/sdl-shared-XXXXXX";
char *xdg_path;
char tmp_path[PATH_MAX];
int fd;
xdg_path = SDL_getenv("XDG_RUNTIME_DIR");
if (!xdg_path) {
return -1;
}
SDL_strlcpy(tmp_path, xdg_path, PATH_MAX);
SDL_strlcat(tmp_path, template, PATH_MAX);
fd = mkostemp(tmp_path, O_CLOEXEC);
if (fd < 0)
return -1;
if (ftruncate(fd, size) < 0) {
close(fd);
return -1;
}
return fd;
}
static void
mouse_buffer_release(void *data, struct wl_buffer *buffer)
{
}
static const struct wl_buffer_listener mouse_buffer_listener = {
mouse_buffer_release
};
static int
create_buffer_from_shm(Wayland_CursorData *d,
int width,
int height,
uint32_t format)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *data = (SDL_VideoData *) vd->driverdata;
struct wl_shm_pool *shm_pool;
int stride = width * 4;
int size = stride * height;
int shm_fd;
shm_fd = wayland_create_tmp_file(size);
if (shm_fd < 0)
{
return SDL_SetError("Creating mouse cursor buffer failed.");
}
d->shm_data = mmap(NULL,
size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
shm_fd,
0);
if (d->shm_data == MAP_FAILED) {
d->shm_data = NULL;
close (shm_fd);
return SDL_SetError("mmap() failed.");
}
shm_pool = wl_shm_create_pool(data->shm, shm_fd, size);
d->buffer = wl_shm_pool_create_buffer(shm_pool,
0,
width,
height,
stride,
format);
wl_buffer_add_listener(d->buffer,
&mouse_buffer_listener,
d);
wl_shm_pool_destroy (shm_pool);
close (shm_fd);
return 0;
}
static SDL_Cursor *
Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
{
SDL_Cursor *cursor;
cursor = calloc(1, sizeof (*cursor));
if (cursor) {
SDL_VideoDevice *vd = SDL_GetVideoDevice ();
SDL_VideoData *wd = (SDL_VideoData *) vd->driverdata;
Wayland_CursorData *data = calloc (1, sizeof (Wayland_CursorData));
if (!data) {
SDL_OutOfMemory();
free(cursor);
return NULL;
}
cursor->driverdata = (void *) data;
/* Assume ARGB8888 */
SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
SDL_assert(surface->pitch == surface->w * 4);
/* Allocate shared memory buffer for this cursor */
if (create_buffer_from_shm (data,
surface->w,
surface->h,
WL_SHM_FORMAT_ARGB8888) < 0)
{
free (cursor->driverdata);
free (cursor);
return NULL;
}
SDL_memcpy(data->shm_data,
surface->pixels,
surface->h * surface->pitch);
data->surface = wl_compositor_create_surface(wd->compositor);
wl_surface_set_user_data(data->surface, NULL);
data->hot_x = hot_x;
data->hot_y = hot_y;
data->w = surface->w;
data->h = surface->h;
} else {
SDL_OutOfMemory();
}
return cursor;
}
static SDL_Cursor *
CreateCursorFromWlCursor(SDL_VideoData *d, struct wl_cursor *wlcursor)
{
SDL_Cursor *cursor;
cursor = calloc(1, sizeof (*cursor));
if (cursor) {
Wayland_CursorData *data = calloc (1, sizeof (Wayland_CursorData));
if (!data) {
SDL_OutOfMemory();
free(cursor);
return NULL;
}
cursor->driverdata = (void *) data;
data->buffer = WAYLAND_wl_cursor_image_get_buffer(wlcursor->images[0]);
data->surface = wl_compositor_create_surface(d->compositor);
wl_surface_set_user_data(data->surface, NULL);
data->hot_x = wlcursor->images[0]->hotspot_x;
data->hot_y = wlcursor->images[0]->hotspot_y;
data->w = wlcursor->images[0]->width;
data->h = wlcursor->images[0]->height;
data->cursor= wlcursor;
} else {
SDL_OutOfMemory ();
}
return cursor;
}
static SDL_Cursor *
Wayland_CreateDefaultCursor()
{
SDL_VideoDevice *device = SDL_GetVideoDevice();
SDL_VideoData *data = device->driverdata;
return CreateCursorFromWlCursor (data,
WAYLAND_wl_cursor_theme_get_cursor(data->cursor_theme,
"left_ptr"));
}
static SDL_Cursor *
Wayland_CreateSystemCursor(SDL_SystemCursor id)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *d = vd->driverdata;
struct wl_cursor *cursor = NULL;
switch(id)
{
default:
SDL_assert(0);
return NULL;
case SDL_SYSTEM_CURSOR_ARROW:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
break;
case SDL_SYSTEM_CURSOR_IBEAM:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "xterm");
break;
case SDL_SYSTEM_CURSOR_WAIT:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "watch");
break;
case SDL_SYSTEM_CURSOR_CROSSHAIR:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
break;
case SDL_SYSTEM_CURSOR_WAITARROW:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "watch");
break;
case SDL_SYSTEM_CURSOR_SIZENWSE:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
break;
case SDL_SYSTEM_CURSOR_SIZENESW:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
break;
case SDL_SYSTEM_CURSOR_SIZEWE:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
break;
case SDL_SYSTEM_CURSOR_SIZENS:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
break;
case SDL_SYSTEM_CURSOR_SIZEALL:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
break;
case SDL_SYSTEM_CURSOR_NO:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "xterm");
break;
case SDL_SYSTEM_CURSOR_HAND:
cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "hand1");
break;
}
return CreateCursorFromWlCursor(d, cursor);
}
static void
Wayland_FreeCursor(SDL_Cursor *cursor)
{
Wayland_CursorData *d;
if (!cursor)
return;
d = cursor->driverdata;
/* Probably not a cursor we own */
if (!d)
return;
if (d->buffer && !d->cursor)
wl_buffer_destroy(d->buffer);
if (d->surface)
wl_surface_destroy(d->surface);
/* Not sure what's meant to happen to shm_data */
free (cursor->driverdata);
SDL_free(cursor);
}
static int
Wayland_ShowCursor(SDL_Cursor *cursor)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *d = vd->driverdata;
struct wl_pointer *pointer = d->pointer;
if (!pointer)
return -1;
if (cursor)
{
Wayland_CursorData *data = cursor->driverdata;
wl_pointer_set_cursor (pointer, 0,
data->surface,
data->hot_x,
data->hot_y);
wl_surface_attach(data->surface, data->buffer, 0, 0);
wl_surface_damage(data->surface, 0, 0, data->w, data->h);
wl_surface_commit(data->surface);
}
else
{
wl_pointer_set_cursor (pointer, 0,
NULL,
0,
0);
}
return 0;
}
static void
Wayland_WarpMouse(SDL_Window *window, int x, int y)
{
SDL_Unsupported();
}
static int
Wayland_WarpMouseGlobal(int x, int y)
{
return SDL_Unsupported();
}
static int
Wayland_SetRelativeMouseMode(SDL_bool enabled)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *data = (SDL_VideoData *) vd->driverdata;
if (enabled)
return Wayland_input_lock_pointer(data->input);
else
return Wayland_input_unlock_pointer(data->input);
}
void
Wayland_InitMouse(void)
{
SDL_Mouse *mouse = SDL_GetMouse();
mouse->CreateCursor = Wayland_CreateCursor;
mouse->CreateSystemCursor = Wayland_CreateSystemCursor;
mouse->ShowCursor = Wayland_ShowCursor;
mouse->FreeCursor = Wayland_FreeCursor;
mouse->WarpMouse = Wayland_WarpMouse;
mouse->WarpMouseGlobal = Wayland_WarpMouseGlobal;
mouse->SetRelativeMouseMode = Wayland_SetRelativeMouseMode;
SDL_SetDefaultCursor(Wayland_CreateDefaultCursor());
}
void
Wayland_FiniMouse(void)
{
/* This effectively assumes that nobody else
* touches SDL_Mouse which is effectively
* a singleton */
}
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
Wayland_input_unlock_pointer
int Wayland_input_unlock_pointer(struct SDL_WaylandInput *input)
SDL_strlcpy
#define SDL_strlcpy
Definition: SDL_dynapi_overrides.h:394
SDL_PIXELFORMAT_ARGB8888
Definition: SDL_pixels.h:248
stride
GLsizei stride
Definition: SDL_opengl_glext.h:378
SDL_GetMouse
SDL_Mouse * SDL_GetMouse(void)
Definition: SDL_mouse.c:112
wl_surface_set_user_data
static void wl_surface_set_user_data(struct wl_surface *wl_surface, void *user_data)
Definition: wayland-client-protocol.h:3368
SDL_Mouse::ShowCursor
int(* ShowCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:52
SDL_SYSTEM_CURSOR_ARROW
Definition: SDL_mouse.h:48
SDL_strlcat
#define SDL_strlcat
Definition: SDL_dynapi_overrides.h:396
wl_surface_commit
static void wl_surface_commit(struct wl_surface *wl_surface)
Definition: wayland-client-protocol.h:3619
SDL_Surface::h
int h
Definition: SDL_surface.h:73
SDL_SYSTEM_CURSOR_HAND
Definition: SDL_mouse.h:59
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_Mouse::SetRelativeMouseMode
int(* SetRelativeMouseMode)(SDL_bool enabled)
Definition: SDL_mouse_c.h:67
wl_surface_attach
static void wl_surface_attach(struct wl_surface *wl_surface, struct wl_buffer *buffer, int32_t x, int32_t y)
Definition: wayland-client-protocol.h:3444
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
free
SDL_EventEntry * free
Definition: SDL_events.c:84
surface
EGLSurface surface
Definition: eglext.h:248
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
SDL_VideoDevice::driverdata
void * driverdata
Definition: SDL_sysvideo.h:377
SDL_Cursor
Definition: SDL_mouse_c.h:30
WL_SHM_FORMAT_ARGB8888
Definition: wayland-client-protocol.h:1358
wl_buffer_add_listener
static int wl_buffer_add_listener(struct wl_buffer *wl_buffer, const struct wl_buffer_listener *listener, void *data)
Definition: wayland-client-protocol.h:1706
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_VideoData::cursor_theme
struct wl_cursor_theme * cursor_theme
Definition: SDL_waylandvideo.h:44
Wayland_input_lock_pointer
int Wayland_input_lock_pointer(struct SDL_WaylandInput *input)
SDL_Surface::w
int w
Definition: SDL_surface.h:73
wl_surface_destroy
static void wl_surface_destroy(struct wl_surface *wl_surface)
Definition: wayland-client-protocol.h:3392
wl_surface_damage
static void wl_surface_damage(struct wl_surface *wl_surface, int32_t x, int32_t y, int32_t width, int32_t height)
Definition: wayland-client-protocol.h:3476
SDL_VideoData::input
struct SDL_WaylandInput * input
Definition: SDL_waylandvideo.h:60
SDL_SYSTEM_CURSOR_SIZEALL
Definition: SDL_mouse.h:57
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
SDL_SYSTEM_CURSOR_SIZEWE
Definition: SDL_mouse.h:55
format
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
device
static SDL_AudioDeviceID device
Definition: loopwave.c:37
SDL_Mouse::CreateCursor
SDL_Cursor *(* CreateCursor)(SDL_Surface *surface, int hot_x, int hot_y)
Definition: SDL_mouse_c.h:46
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
wl_buffer_listener
Definition: wayland-client-protocol.h:1681
d
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 ** d
Definition: SDL_dynapi_procs.h:117
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:75
SDL_SYSTEM_CURSOR_SIZENWSE
Definition: SDL_mouse.h:53
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
wl_shm_pool_destroy
static void wl_shm_pool_destroy(struct wl_shm_pool *wl_shm_pool)
Definition: wayland-client-protocol.h:1291
SDL_mouse.h
SDL_VideoData::h
int h
Definition: SDL_naclvideo.h:39
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731
SDL_SystemCursor
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor().
Definition: SDL_mouse.h:46
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_waylanddyn.h
pointer
GLsizei const void * pointer
Definition: SDL_opengl_glext.h:378
SDL_SetDefaultCursor
void SDL_SetDefaultCursor(SDL_Cursor *cursor)
Definition: SDL_mouse.c:101
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537
SDL_Mouse::WarpMouseGlobal
int(* WarpMouseGlobal)(int x, int y)
Definition: SDL_mouse_c.h:64
wl_shm_pool_create_buffer
static struct wl_buffer * wl_shm_pool_create_buffer(struct wl_shm_pool *wl_shm_pool, int32_t offset, int32_t width, int32_t height, int32_t stride, uint32_t format)
Definition: wayland-client-protocol.h:1271
enabled
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: SDL_opengl_glext.h:2479
cursor
SDL_Cursor * cursor
Definition: testcustomcursor.c:119
SDL_getenv
#define SDL_getenv
Definition: SDL_dynapi_overrides.h:378
SDL_Mouse::FreeCursor
void(* FreeCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:58
SDL_VideoData::pointer
struct wl_pointer * pointer
Definition: SDL_waylandvideo.h:45
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
SDL_SYSTEM_CURSOR_CROSSHAIR
Definition: SDL_mouse.h:51
SDL_waylandvideo.h
NULL
#define NULL
Definition: begin_code.h:164
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:139
buffer
GLuint buffer
Definition: SDL_opengl_glext.h:533
wl_buffer_destroy
static void wl_buffer_destroy(struct wl_buffer *wl_buffer)
Definition: wayland-client-protocol.h:1754
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:63
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_VideoData::compositor
struct wl_compositor * compositor
Definition: SDL_waylandvideo.h:42
height
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
SDL_SYSTEM_CURSOR_WAIT
Definition: SDL_mouse.h:50
wl_compositor_create_surface
static struct wl_surface * wl_compositor_create_surface(struct wl_compositor *wl_compositor)
Definition: wayland-client-protocol.h:1191
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_SYSTEM_CURSOR_IBEAM
Definition: SDL_mouse.h:49
wl_pointer_set_cursor
static void wl_pointer_set_cursor(struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface, int32_t hotspot_x, int32_t hotspot_y)
Definition: wayland-client-protocol.h:4441
SDL_Mouse::WarpMouse
void(* WarpMouse)(SDL_Window *window, int x, int y)
Definition: SDL_mouse_c.h:61
SDL_Surface::pitch
int pitch
Definition: SDL_surface.h:74
SDL_VideoDevice
Definition: SDL_sysvideo.h:147
SDL_SYSTEM_CURSOR_SIZENESW
Definition: SDL_mouse.h:54
SDL_VideoData
Definition: SDL_androidvideo.h:35
SDL_waylandevents_c.h
SDL_GetVideoDevice
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:586
wl_shm_create_pool
static struct wl_shm_pool * wl_shm_create_pool(struct wl_shm *wl_shm, int32_t fd, int32_t size)
Definition: wayland-client-protocol.h:1667
SDL_Mouse
Definition: SDL_mouse_c.h:43
SDL_SYSTEM_CURSOR_NO
Definition: SDL_mouse.h:58
SDL_PixelFormat::format
Uint32 format
Definition: SDL_pixels.h:317
SDL_SYSTEM_CURSOR_SIZENS
Definition: SDL_mouse.h:56
SDL_SYSTEM_CURSOR_WAITARROW
Definition: SDL_mouse.h:52
SDL_Mouse::CreateSystemCursor
SDL_Cursor *(* CreateSystemCursor)(SDL_SystemCursor id)
Definition: SDL_mouse_c.h:49
SDL_Cursor::driverdata
void * driverdata
Definition: SDL_mouse_c.h:33
SDL_VideoData::shm
struct wl_shm * shm
Definition: SDL_waylandvideo.h:43
SDL_Unsupported
#define SDL_Unsupported()
Definition: SDL_error.h:53
SDL_VideoData::w
int w
Definition: SDL_naclvideo.h:39
fd
GLuint64 GLenum GLint fd
Definition: gl2ext.h:1508
SDL_assert.h
hot_x
int uint32_t uint32_t uint32_t uint32_t uint32_t int drmModeModeInfoPtr mode int uint32_t uint32_t uint32_t uint32_t int32_t hot_x
Definition: SDL_kmsdrmsym.h:55