Implement glfwCreateStandardCursor for Wayland

Closes #620.
This commit is contained in:
Ricardo Vieira 2015-10-23 00:28:16 +01:00 committed by Camilla Berglund
parent d95b77ebec
commit e8f3de0f2e
3 changed files with 58 additions and 13 deletions

View File

@ -586,14 +586,6 @@ int _glfwPlatformInit(void)
"Wayland: Unable to load default cursor theme\n"); "Wayland: Unable to load default cursor theme\n");
return GLFW_FALSE; return GLFW_FALSE;
} }
_glfw.wl.defaultCursor =
wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, "left_ptr");
if (!_glfw.wl.defaultCursor)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Wayland: Unable to load default left pointer\n");
return GLFW_FALSE;
}
_glfw.wl.cursorSurface = _glfw.wl.cursorSurface =
wl_compositor_create_surface(_glfw.wl.compositor); wl_compositor_create_surface(_glfw.wl.compositor);
} }

View File

@ -84,7 +84,6 @@ typedef struct _GLFWlibraryWayland
struct wl_keyboard* keyboard; struct wl_keyboard* keyboard;
struct wl_cursor_theme* cursorTheme; struct wl_cursor_theme* cursorTheme;
struct wl_cursor* defaultCursor;
struct wl_surface* cursorSurface; struct wl_surface* cursorSurface;
uint32_t pointerSerial; uint32_t pointerSerial;
@ -130,6 +129,7 @@ typedef struct _GLFWmonitorWayland
// //
typedef struct _GLFWcursorWayland typedef struct _GLFWcursorWayland
{ {
struct wl_cursor_image* image;
struct wl_buffer* buffer; struct wl_buffer* buffer;
int width, height; int width, height;
int xhot, yhot; int xhot, yhot;

View File

@ -207,6 +207,28 @@ createAnonymousFile(off_t size)
return fd; return fd;
} }
// Translates a GLFW standard cursor to a theme cursor name
//
static char *translateCursorShape(int shape)
{
switch (shape)
{
case GLFW_ARROW_CURSOR:
return "left_ptr";
case GLFW_IBEAM_CURSOR:
return "xterm";
case GLFW_CROSSHAIR_CURSOR:
return "crosshair";
case GLFW_HAND_CURSOR:
return "grabbing";
case GLFW_HRESIZE_CURSOR:
return "sb_h_double_arrow";
case GLFW_VRESIZE_CURSOR:
return "sb_v_double_arrow";
}
return NULL;
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////// GLFW platform API ////// ////// GLFW platform API //////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -469,19 +491,34 @@ int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape) int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape)
{ {
// TODO struct wl_cursor* standard_cursor;
fprintf(stderr, "_glfwPlatformCreateStandardCursor not implemented yet\n");
return GLFW_FALSE; standard_cursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme,
translateCursorShape(shape));
if (!standard_cursor) {
_glfwInputError(GLFW_PLATFORM_ERROR,
"Wayland: Standard cursor \"%s\" not found",
translateCursorShape(shape));
return GLFW_FALSE;
}
cursor->wl.image = standard_cursor->images[0];
return GLFW_TRUE;
} }
void _glfwPlatformDestroyCursor(_GLFWcursor* cursor) void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
{ {
// If it's a standard cursor we don't need to do anything here
if (cursor->wl.image)
return;
wl_buffer_destroy(cursor->wl.buffer); wl_buffer_destroy(cursor->wl.buffer);
} }
void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
{ {
struct wl_buffer* buffer; struct wl_buffer* buffer;
struct wl_cursor* defaultCursor;
struct wl_cursor_image* image; struct wl_cursor_image* image;
struct wl_surface* surface = _glfw.wl.cursorSurface; struct wl_surface* surface = _glfw.wl.cursorSurface;
@ -499,7 +536,23 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
{ {
if (cursor == NULL) if (cursor == NULL)
{ {
image = _glfw.wl.defaultCursor->images[0]; defaultCursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme,
"left_ptr");
if (!defaultCursor)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Wayland: Standard cursor not found");
return;
}
image = defaultCursor->images[0];
}
else
{
image = cursor->wl.image;
}
if (image)
{
buffer = wl_cursor_image_get_buffer(image); buffer = wl_cursor_image_get_buffer(image);
if (!buffer) if (!buffer)
return; return;