浏览代码

Clean up cursor mode setting

Camilla Berglund 10 年之前
父节点
当前提交
13fbb4748a
共有 7 个文件被更改,包括 74 次插入139 次删除
  1. 14 21
      src/cocoa_window.m
  2. 1 1
      src/input.c
  3. 3 3
      src/internal.h
  4. 1 1
      src/mir_window.c
  5. 25 65
      src/win32_window.c
  6. 1 1
      src/wl_window.c
  7. 29 47
      src/x11_window.c

+ 14 - 21
src/cocoa_window.m

@@ -64,21 +64,6 @@ static void centerCursor(_GLFWwindow *window)
     _glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0);
 }
 
-// Update the cursor to match the specified cursor mode
-//
-static void updateModeCursor(_GLFWwindow* window)
-{
-    if (window->cursorMode == GLFW_CURSOR_NORMAL)
-    {
-        if (window->cursor)
-            [(NSCursor*) window->cursor->ns.object set];
-        else
-            [[NSCursor arrowCursor] set];
-    }
-    else
-        [(NSCursor*) _glfw.ns.cursor set];
-}
-
 // Enter full screen mode
 //
 static GLboolean enterFullscreenMode(_GLFWwindow* window)
@@ -242,7 +227,7 @@ static int translateKey(unsigned int key)
     }
 
     _glfwInputWindowFocus(window, GL_TRUE);
-    _glfwPlatformApplyCursorMode(window);
+    _glfwPlatformSetCursorMode(window, window->cursorMode);
 }
 
 - (void)windowDidResignKey:(NSNotification *)notification
@@ -367,7 +352,7 @@ static int translateKey(unsigned int key)
 
 - (void)cursorUpdate:(NSEvent *)event
 {
-    updateModeCursor(window);
+    _glfwPlatformSetCursorMode(window, window->cursorMode);
 }
 
 - (void)mouseDown:(NSEvent *)event
@@ -1156,7 +1141,7 @@ void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
 
 void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
 {
-    updateModeCursor(window);
+    _glfwPlatformSetCursorMode(window, window->cursorMode);
 
     const NSRect contentRect = [window->ns.view frame];
     const NSPoint pos = [window->ns.object mouseLocationOutsideOfEventStream];
@@ -1185,11 +1170,19 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
     }
 }
 
-void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
+void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
 {
-    updateModeCursor(window);
+    if (mode == GLFW_CURSOR_NORMAL)
+    {
+        if (window->cursor)
+            [(NSCursor*) window->cursor->ns.object set];
+        else
+            [[NSCursor arrowCursor] set];
+    }
+    else
+        [(NSCursor*) _glfw.ns.cursor set];
 
-    if (window->cursorMode == GLFW_CURSOR_DISABLED)
+    if (mode == GLFW_CURSOR_DISABLED)
         CGAssociateMouseAndMouseCursorPosition(false);
     else
         CGAssociateMouseAndMouseCursorPosition(true);

+ 1 - 1
src/input.c

@@ -78,7 +78,7 @@ static void setCursorMode(_GLFWwindow* window, int newMode)
             _glfwPlatformSetCursorPos(window, width / 2, height / 2);
         }
 
-        _glfwPlatformApplyCursorMode(window);
+        _glfwPlatformSetCursorMode(window, window->cursorMode);
     }
 }
 

+ 3 - 3
src/internal.h

@@ -412,11 +412,11 @@ void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos);
  */
 void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos);
 
-/*! @brief Applies the cursor mode of the specified window to the system.
- *  @param[in] window The window whose cursor mode to apply.
+/*! @brief Sets the specified cursor mode of the specified window.
+ *  @param[in] window The window whose cursor mode to set.
  *  @ingroup platform
  */
-void _glfwPlatformApplyCursorMode(_GLFWwindow* window);
+void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode);
 
 /*! @copydoc glfwGetMonitors
  *  @ingroup platform

+ 1 - 1
src/mir_window.c

@@ -779,7 +779,7 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos)
                     "Mir: Unsupported function %s", __PRETTY_FUNCTION__);
 }
 
-void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
+void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
 {
     _glfwInputError(GLFW_PLATFORM_ERROR,
                     "Mir: Unsupported function %s", __PRETTY_FUNCTION__);

+ 25 - 65
src/win32_window.c

@@ -80,56 +80,6 @@ static void updateClipRect(_GLFWwindow* window)
     ClipCursor(&clipRect);
 }
 
-// Hide the mouse cursor
-//
-static void hideCursor(_GLFWwindow* window)
-{
-    POINT pos;
-
-    ClipCursor(NULL);
-
-    if (GetCursorPos(&pos))
-    {
-        if (WindowFromPoint(pos) == window->win32.handle)
-            SetCursor(NULL);
-    }
-}
-
-// Disable the mouse cursor
-//
-static void disableCursor(_GLFWwindow* window)
-{
-    POINT pos;
-
-    updateClipRect(window);
-
-    if (GetCursorPos(&pos))
-    {
-        if (WindowFromPoint(pos) == window->win32.handle)
-            SetCursor(NULL);
-    }
-}
-
-// Restores the mouse cursor
-//
-static void restoreCursor(_GLFWwindow* window)
-{
-    POINT pos;
-
-    ClipCursor(NULL);
-
-    if (GetCursorPos(&pos))
-    {
-        if (WindowFromPoint(pos) == window->win32.handle)
-        {
-            if (window->cursor)
-                SetCursor(window->cursor->win32.handle);
-            else
-                SetCursor(LoadCursorW(NULL, IDC_ARROW));
-        }
-    }
-}
-
 // Translates a GLFW standard cursor to a resource ID
 //
 static LPWSTR translateCursorShape(int shape)
@@ -277,8 +227,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
 
         case WM_SETFOCUS:
         {
-            if (window->cursorMode != GLFW_CURSOR_NORMAL)
-                _glfwPlatformApplyCursorMode(window);
+            if (window->cursorMode == GLFW_CURSOR_DISABLED)
+                _glfwPlatformSetCursorMode(window, GLFW_CURSOR_DISABLED);
 
             _glfwInputWindowFocus(window, GL_TRUE);
             return 0;
@@ -286,8 +236,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
 
         case WM_KILLFOCUS:
         {
-            if (window->cursorMode != GLFW_CURSOR_NORMAL)
-                restoreCursor(window);
+            if (window->cursorMode == GLFW_CURSOR_DISABLED)
+                _glfwPlatformSetCursorMode(window, GLFW_CURSOR_NORMAL);
 
             if (window->monitor && window->autoIconify)
                 _glfwPlatformIconifyWindow(window);
@@ -1093,20 +1043,30 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos)
     SetCursorPos(pos.x, pos.y);
 }
 
-void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
+void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
 {
-    switch (window->cursorMode)
+    POINT pos;
+
+    if (mode == GLFW_CURSOR_DISABLED)
+        updateClipRect(window);
+    else
+        ClipCursor(NULL);
+
+    if (!GetCursorPos(&pos))
+        return;
+
+    if (WindowFromPoint(pos) != window->win32.handle)
+        return;
+
+    if (mode == GLFW_CURSOR_NORMAL)
     {
-        case GLFW_CURSOR_NORMAL:
-            restoreCursor(window);
-            break;
-        case GLFW_CURSOR_HIDDEN:
-            hideCursor(window);
-            break;
-        case GLFW_CURSOR_DISABLED:
-            disableCursor(window);
-            break;
+        if (window->cursor)
+            SetCursor(window->cursor->win32.handle);
+        else
+            SetCursor(LoadCursorW(NULL, IDC_ARROW));
     }
+    else
+        SetCursor(NULL);
 }
 
 int _glfwPlatformCreateCursor(_GLFWcursor* cursor,

+ 1 - 1
src/wl_window.c

@@ -392,7 +392,7 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
                     "Wayland: Cursor position setting not supported");
 }
 
-void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
+void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
 {
     _glfwPlatformSetCursor(window, window->wl.currentCursor);
 }

+ 29 - 47
src/x11_window.c

@@ -502,39 +502,6 @@ static GLboolean createWindow(_GLFWwindow* window,
     return GL_TRUE;
 }
 
-// Hide the mouse cursor
-//
-static void hideCursor(_GLFWwindow* window)
-{
-    XUngrabPointer(_glfw.x11.display, CurrentTime);
-    XDefineCursor(_glfw.x11.display, window->x11.handle, _glfw.x11.cursor);
-}
-
-// Disable the mouse cursor
-//
-static void disableCursor(_GLFWwindow* window)
-{
-    XGrabPointer(_glfw.x11.display, window->x11.handle, True,
-                 ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
-                 GrabModeAsync, GrabModeAsync,
-                 window->x11.handle, _glfw.x11.cursor, CurrentTime);
-}
-
-// Restores the mouse cursor
-//
-static void restoreCursor(_GLFWwindow* window)
-{
-    XUngrabPointer(_glfw.x11.display, CurrentTime);
-
-    if (window->cursor)
-    {
-        XDefineCursor(_glfw.x11.display, window->x11.handle,
-                      window->cursor->x11.handle);
-    }
-    else
-        XUndefineCursor(_glfw.x11.display, window->x11.handle);
-}
-
 // Returns whether the event is a selection event
 //
 static Bool isSelectionEvent(Display* display, XEvent* event, XPointer pointer)
@@ -1109,7 +1076,7 @@ static void processEvent(XEvent *event)
             // HACK: This is a workaround for WMs (KWM, Fluxbox) that otherwise
             //       ignore the defined cursor for hidden cursor mode
             if (window->cursorMode == GLFW_CURSOR_HIDDEN)
-                hideCursor(window);
+                _glfwPlatformSetCursorMode(window, GLFW_CURSOR_HIDDEN);
 
             _glfwInputCursorEnter(window, GL_TRUE);
             return;
@@ -1323,7 +1290,7 @@ static void processEvent(XEvent *event)
                 XSetICFocus(window->x11.ic);
 
             if (window->cursorMode == GLFW_CURSOR_DISABLED)
-                disableCursor(window);
+                _glfwPlatformSetCursorMode(window, GLFW_CURSOR_DISABLED);
 
             _glfwInputWindowFocus(window, GL_TRUE);
             return;
@@ -1343,7 +1310,7 @@ static void processEvent(XEvent *event)
                 XUnsetICFocus(window->x11.ic);
 
             if (window->cursorMode == GLFW_CURSOR_DISABLED)
-                restoreCursor(window);
+                _glfwPlatformSetCursorMode(window, GLFW_CURSOR_NORMAL);
 
             if (window->monitor && window->autoIconify)
                 _glfwPlatformIconifyWindow(window);
@@ -1876,19 +1843,34 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
                  0,0,0,0, (int) x, (int) y);
 }
 
-void _glfwPlatformApplyCursorMode(_GLFWwindow* window)
+void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
 {
-    switch (window->cursorMode)
+    if (mode == GLFW_CURSOR_DISABLED)
     {
-        case GLFW_CURSOR_NORMAL:
-            restoreCursor(window);
-            break;
-        case GLFW_CURSOR_HIDDEN:
-            hideCursor(window);
-            break;
-        case GLFW_CURSOR_DISABLED:
-            disableCursor(window);
-            break;
+        XGrabPointer(_glfw.x11.display, window->x11.handle, True,
+                     ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
+                     GrabModeAsync, GrabModeAsync,
+                     window->x11.handle, _glfw.x11.cursor, CurrentTime);
+    }
+    else
+    {
+        XUngrabPointer(_glfw.x11.display, CurrentTime);
+
+        if (mode == GLFW_CURSOR_NORMAL)
+        {
+            if (window->cursor)
+            {
+                XDefineCursor(_glfw.x11.display, window->x11.handle,
+                              window->cursor->x11.handle);
+            }
+            else
+                XUndefineCursor(_glfw.x11.display, window->x11.handle);
+        }
+        else
+        {
+            XDefineCursor(_glfw.x11.display, window->x11.handle,
+                          _glfw.x11.cursor);
+        }
     }
 }