1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-23 02:38:52 -05:00

Unified X11 cursor creation.

This commit is contained in:
Camilla Berglund 2014-06-22 13:13:53 +02:00
parent 215924f797
commit 9b6c14b7ae
3 changed files with 39 additions and 52 deletions

View File

@ -576,40 +576,16 @@ static GLboolean initExtensions(void)
return GL_TRUE; return GL_TRUE;
} }
// Create a blank cursor (for locked mouse mode) // Create a blank cursor for hidden and disabled cursor modes
// //
static Cursor createNULLCursor(void) static Cursor createNULLCursor(void)
{ {
Pixmap cursormask; unsigned char pixels[16 * 16 * 4];
XGCValues xgc; GLFWimage image = { 16, 16, pixels };
GC gc;
XColor col;
Cursor cursor;
_glfwGrabXErrorHandler(); memset(pixels, 0, sizeof(pixels));
cursormask = XCreatePixmap(_glfw.x11.display, _glfw.x11.root, 1, 1, 1); return _glfwCreateCursor(&image, 0, 0);
xgc.function = GXclear;
gc = XCreateGC(_glfw.x11.display, cursormask, GCFunction, &xgc);
XFillRectangle(_glfw.x11.display, cursormask, gc, 0, 0, 1, 1);
col.pixel = 0;
col.red = 0;
col.flags = 4;
cursor = XCreatePixmapCursor(_glfw.x11.display,
cursormask, cursormask,
&col, &col, 0, 0);
XFreePixmap(_glfw.x11.display, cursormask);
XFreeGC(_glfw.x11.display, gc);
_glfwReleaseXErrorHandler();
if (cursor == None)
{
_glfwInputXError(GLFW_PLATFORM_ERROR,
"X11: Failed to create null cursor");
}
return cursor;
} }
// Terminate X11 display // Terminate X11 display
@ -664,6 +640,37 @@ void _glfwInputXError(int error, const char* message)
_glfwInputError(error, "%s: %s", message, buffer); _glfwInputError(error, "%s: %s", message, buffer);
} }
// Create a cursor object
//
Cursor _glfwCreateCursor(const GLFWimage* image, int xhot, int yhot)
{
int i;
Cursor cursor;
XcursorImage* native = XcursorImageCreate(image->width, image->height);
if (native == NULL)
return None;
native->xhot = xhot;
native->yhot = yhot;
unsigned char* source = (unsigned char*) image->pixels;
XcursorPixel* target = native->pixels;
for (i = 0; i < image->width * image->height; i++, target++, source += 4)
{
*target = (source[3] << 24) |
(source[0] << 16) |
(source[1] << 8) |
source[2];
}
cursor = XcursorImageLoadCursor(_glfw.x11.display, native);
XcursorImageDestroy(native);
return cursor;
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////// GLFW platform API ////// ////// GLFW platform API //////

View File

@ -256,6 +256,8 @@ void _glfwHandleSelectionClear(XEvent* event);
void _glfwHandleSelectionRequest(XEvent* event); void _glfwHandleSelectionRequest(XEvent* event);
void _glfwPushSelectionToManager(_GLFWwindow* window); void _glfwPushSelectionToManager(_GLFWwindow* window);
Cursor _glfwCreateCursor(const GLFWimage* image, int xhot, int yhot);
// Window support // Window support
_GLFWwindow* _glfwFindWindowByHandle(Window handle); _GLFWwindow* _glfwFindWindowByHandle(Window handle);
unsigned long _glfwGetWindowProperty(Window window, unsigned long _glfwGetWindowProperty(Window window,

View File

@ -1454,29 +1454,7 @@ int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
const GLFWimage* image, const GLFWimage* image,
int xhot, int yhot) int xhot, int yhot)
{ {
int i; cursor->x11.handle = _glfwCreateCursor(image, xhot, yhot);
XcursorImage* native = XcursorImageCreate(image->width, image->height);
if (native == NULL)
return GL_FALSE;
native->xhot = xhot;
native->yhot = yhot;
unsigned char* source = (unsigned char*) image->pixels;
XcursorPixel* target = native->pixels;
for (i = 0; i < image->width * image->height; i++, target++, source += 4)
{
*target = (source[3] << 24) |
(source[0] << 16) |
(source[1] << 8) |
source[2];
}
cursor->x11.handle = XcursorImageLoadCursor(_glfw.x11.display, native);
XcursorImageDestroy(native);
if (cursor->x11.handle == None) if (cursor->x11.handle == None)
return GL_FALSE; return GL_FALSE;