Bläddra i källkod

Unified X11 cursor creation.

Camilla Berglund 11 år sedan
förälder
incheckning
9b6c14b7ae
3 ändrade filer med 39 tillägg och 52 borttagningar
  1. 36 29
      src/x11_init.c
  2. 2 0
      src/x11_platform.h
  3. 1 23
      src/x11_window.c

+ 36 - 29
src/x11_init.c

@@ -576,40 +576,16 @@ static GLboolean initExtensions(void)
     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)
 {
-    Pixmap cursormask;
-    XGCValues xgc;
-    GC gc;
-    XColor col;
-    Cursor cursor;
-
-    _glfwGrabXErrorHandler();
-
-    cursormask = XCreatePixmap(_glfw.x11.display, _glfw.x11.root, 1, 1, 1);
-    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();
+    unsigned char pixels[16 * 16 * 4];
+    GLFWimage image = { 16, 16, pixels };
 
-    if (cursor == None)
-    {
-        _glfwInputXError(GLFW_PLATFORM_ERROR,
-                         "X11: Failed to create null cursor");
-    }
+    memset(pixels, 0, sizeof(pixels));
 
-    return cursor;
+    return _glfwCreateCursor(&image, 0, 0);
 }
 
 // Terminate X11 display
@@ -664,6 +640,37 @@ void _glfwInputXError(int error, const char* message)
     _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                      //////

+ 2 - 0
src/x11_platform.h

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

+ 1 - 23
src/x11_window.c

@@ -1454,29 +1454,7 @@ int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
                               const GLFWimage* image,
                               int xhot, int yhot)
 {
-    int i;
-
-    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);
-
+    cursor->x11.handle = _glfwCreateCursor(image, xhot, yhot);
     if (cursor->x11.handle == None)
         return GL_FALSE;