From e0a26aa62f12545d7999f3c8486d9b832f76f0a0 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Wed, 15 Jan 2014 13:21:13 +0100 Subject: [PATCH] Cursor mode cleanup. --- src/cocoa_window.m | 14 ++++----- src/input.c | 2 +- src/internal.h | 7 ++--- src/win32_window.c | 52 +++++++++++++++---------------- src/x11_platform.h | 2 -- src/x11_window.c | 76 ++++++++++++---------------------------------- 6 files changed, 56 insertions(+), 97 deletions(-) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 44db9eb8..ee4ad71e 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -41,9 +41,9 @@ static void centerCursor(_GLFWwindow *window) // Update the cursor to match the specified cursor mode // -static void setModeCursor(_GLFWwindow* window, int mode) +static void setModeCursor(_GLFWwindow* window) { - if (mode == GLFW_CURSOR_NORMAL) + if (window->cursorMode == GLFW_CURSOR_NORMAL) [[NSCursor arrowCursor] set]; else [(NSCursor*) _glfw.ns.cursor set]; @@ -472,7 +472,7 @@ static int translateKey(unsigned int key) - (void)cursorUpdate:(NSEvent *)event { - setModeCursor(window, window->cursorMode); + setModeCursor(window); } - (void)mouseDown:(NSEvent *)event @@ -1084,7 +1084,7 @@ void _glfwPlatformWaitEvents(void) void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) { - setModeCursor(window, window->cursorMode); + setModeCursor(window); if (window->monitor) { @@ -1102,11 +1102,11 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) } } -void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) +void _glfwPlatformApplyCursorMode(_GLFWwindow* window) { - setModeCursor(window, mode); + setModeCursor(window); - if (mode == GLFW_CURSOR_DISABLED) + if (window->cursorMode == GLFW_CURSOR_DISABLED) { CGAssociateMouseAndMouseCursorPosition(false); centerCursor(window); diff --git a/src/input.c b/src/input.c index 866a4891..9e628f06 100644 --- a/src/input.c +++ b/src/input.c @@ -70,7 +70,7 @@ static void setCursorMode(_GLFWwindow* window, int newMode) _glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0); } - _glfwPlatformSetCursorMode(window, newMode); + _glfwPlatformApplyCursorMode(window); } } diff --git a/src/internal.h b/src/internal.h index 7dfd4b59..748d27b1 100644 --- a/src/internal.h +++ b/src/internal.h @@ -364,12 +364,11 @@ const char* _glfwPlatformGetVersionString(void); */ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos); -/*! @brief Sets up the specified cursor mode for the specified window. - * @param[in] window The window whose cursor mode to change. - * @param[in] mode The desired cursor mode. +/*! @brief Applies the cursor mode of the specified window to the system. + * @param[in] window The window whose cursor mode to apply. * @ingroup platform */ -void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode); +void _glfwPlatformApplyCursorMode(_GLFWwindow* window); /*! @copydoc glfwGetMonitors * @ingroup platform diff --git a/src/win32_window.c b/src/win32_window.c index 6803f70f..aa2aa4bb 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -45,7 +45,7 @@ static void updateClipRect(_GLFWwindow* window) ClipCursor(&clipRect); } -// Hide mouse cursor +// Hide the mouse cursor // static void hideCursor(_GLFWwindow* window) { @@ -67,9 +67,9 @@ static void hideCursor(_GLFWwindow* window) } } -// Capture mouse cursor +// Disable the mouse cursor // -static void captureCursor(_GLFWwindow* window) +static void disableCursor(_GLFWwindow* window) { if (!window->win32.cursorHidden) { @@ -81,9 +81,9 @@ static void captureCursor(_GLFWwindow* window) SetCapture(window->win32.handle); } -// Show mouse cursor +// Restores the mouse cursor // -static void showCursor(_GLFWwindow* window) +static void restoreCursor(_GLFWwindow* window) { POINT pos; @@ -410,7 +410,7 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, // The window was defocused (or iconified, see above) if (window->cursorMode != GLFW_CURSOR_NORMAL) - showCursor(window); + restoreCursor(window); if (window->monitor) { @@ -428,10 +428,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, { // The window was focused - if (window->cursorMode == GLFW_CURSOR_DISABLED) - captureCursor(window); - else if (window->cursorMode == GLFW_CURSOR_HIDDEN) - hideCursor(window); + if (window->cursorMode != GLFW_CURSOR_NORMAL) + _glfwPlatformApplyCursorMode(window); if (window->monitor) _glfwSetVideoMode(window->monitor, &window->videoMode); @@ -678,10 +676,10 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, case WM_SIZE: { - if (window->cursorMode == GLFW_CURSOR_DISABLED && - _glfw.focusedWindow == window) + if (_glfw.focusedWindow == window) { - updateClipRect(window); + if (window->cursorMode == GLFW_CURSOR_DISABLED) + updateClipRect(window); } _glfwInputFramebufferSize(window, LOWORD(lParam), HIWORD(lParam)); @@ -691,10 +689,10 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, case WM_MOVE: { - if (window->cursorMode == GLFW_CURSOR_DISABLED && - _glfw.focusedWindow == window) + if (_glfw.focusedWindow == window) { - updateClipRect(window); + if (window->cursorMode == GLFW_CURSOR_DISABLED) + updateClipRect(window); } // NOTE: This cannot use LOWORD/HIWORD recommended by MSDN, as @@ -713,12 +711,14 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, case WM_SETCURSOR: { - if (window->cursorMode != GLFW_CURSOR_NORMAL && - _glfw.focusedWindow == window && - LOWORD(lParam) == HTCLIENT) + if (_glfw.focusedWindow == window && LOWORD(lParam) == HTCLIENT) { - SetCursor(NULL); - return TRUE; + if (window->cursorMode == GLFW_CURSOR_HIDDEN || + window->cursorMode == GLFW_CURSOR_DISABLED) + { + SetCursor(NULL); + return TRUE; + } } break; @@ -1113,7 +1113,7 @@ void _glfwPlatformPollEvents(void) _glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, 0, GLFW_RELEASE, mods); } - // Did the cursor move in an focused window that has captured the cursor + // Did the cursor move in an focused window that has disabled the cursor if (window->cursorMode == GLFW_CURSOR_DISABLED && !window->win32.cursorCentered) { @@ -1142,18 +1142,18 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos) window->win32.oldCursorY = (int) ypos; } -void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) +void _glfwPlatformApplyCursorMode(_GLFWwindow* window) { - switch (mode) + switch (window->cursorMode) { case GLFW_CURSOR_NORMAL: - showCursor(window); + restoreCursor(window); break; case GLFW_CURSOR_HIDDEN: hideCursor(window); break; case GLFW_CURSOR_DISABLED: - captureCursor(window); + disableCursor(window); break; } } diff --git a/src/x11_platform.h b/src/x11_platform.h index f8129385..e1e1e7cc 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -80,8 +80,6 @@ typedef struct _GLFWwindowX11 // Various platform specific internal variables GLboolean overrideRedirect; // True if window is OverrideRedirect - GLboolean cursorGrabbed; // True if cursor is currently grabbed - GLboolean cursorHidden; // True if cursor is currently hidden // Cached position and size used to filter out duplicate events int width, height; diff --git a/src/x11_window.c b/src/x11_window.c index 29658d4e..0dd5f30e 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -317,62 +317,30 @@ static GLboolean createWindow(_GLFWwindow* window, return GL_TRUE; } -// Hide cursor +// Hide the mouse cursor // static void hideCursor(_GLFWwindow* window) { - // Un-grab cursor (in windowed mode only; in fullscreen mode we still - // want the cursor grabbed in order to confine the cursor to the window - // area) - if (window->x11.cursorGrabbed && window->monitor == NULL) - { - XUngrabPointer(_glfw.x11.display, CurrentTime); - window->x11.cursorGrabbed = GL_FALSE; - } - - if (!window->x11.cursorHidden) - { - XDefineCursor(_glfw.x11.display, window->x11.handle, _glfw.x11.cursor); - window->x11.cursorHidden = GL_TRUE; - } + XUngrabPointer(_glfw.x11.display, CurrentTime); + XDefineCursor(_glfw.x11.display, window->x11.handle, _glfw.x11.cursor); } -// Capture cursor +// Disable the mouse cursor // -static void captureCursor(_GLFWwindow* window) +static void disableCursor(_GLFWwindow* window) { - if (!window->x11.cursorGrabbed) - { - if (XGrabPointer(_glfw.x11.display, window->x11.handle, True, - ButtonPressMask | ButtonReleaseMask | - PointerMotionMask, GrabModeAsync, GrabModeAsync, - window->x11.handle, _glfw.x11.cursor, CurrentTime) == - GrabSuccess) - { - window->x11.cursorGrabbed = GL_TRUE; - } - } + XGrabPointer(_glfw.x11.display, window->x11.handle, True, + ButtonPressMask | ButtonReleaseMask | PointerMotionMask, + GrabModeAsync, GrabModeAsync, + window->x11.handle, _glfw.x11.cursor, CurrentTime); } -// Show cursor +// Restores the mouse cursor // -static void showCursor(_GLFWwindow* window) +static void restoreCursor(_GLFWwindow* window) { - // Un-grab cursor (in windowed mode only; in fullscreen mode we still - // want the cursor grabbed in order to confine the cursor to the window - // area) - if (window->x11.cursorGrabbed && window->monitor == NULL) - { - XUngrabPointer(_glfw.x11.display, CurrentTime); - window->x11.cursorGrabbed = GL_FALSE; - } - - // Show cursor - if (window->x11.cursorHidden) - { - XUndefineCursor(_glfw.x11.display, window->x11.handle); - window->x11.cursorHidden = GL_FALSE; - } + XUngrabPointer(_glfw.x11.display, CurrentTime); + XUndefineCursor(_glfw.x11.display, window->x11.handle); } // Enter fullscreen mode @@ -640,18 +608,12 @@ static void processEvent(XEvent *event) case EnterNotify: { - if (window->cursorMode == GLFW_CURSOR_HIDDEN) - hideCursor(window); - _glfwInputCursorEnter(window, GL_TRUE); break; } case LeaveNotify: { - if (window->cursorMode == GLFW_CURSOR_HIDDEN) - showCursor(window); - _glfwInputCursorEnter(window, GL_FALSE); break; } @@ -763,7 +725,7 @@ static void processEvent(XEvent *event) _glfwInputWindowFocus(window, GL_TRUE); if (window->cursorMode == GLFW_CURSOR_DISABLED) - captureCursor(window); + disableCursor(window); break; } @@ -773,7 +735,7 @@ static void processEvent(XEvent *event) _glfwInputWindowFocus(window, GL_FALSE); if (window->cursorMode == GLFW_CURSOR_DISABLED) - showCursor(window); + restoreCursor(window); break; } @@ -1202,18 +1164,18 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) 0,0,0,0, (int) x, (int) y); } -void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) +void _glfwPlatformApplyCursorMode(_GLFWwindow* window) { - switch (mode) + switch (window->cursorMode) { case GLFW_CURSOR_NORMAL: - showCursor(window); + restoreCursor(window); break; case GLFW_CURSOR_HIDDEN: hideCursor(window); break; case GLFW_CURSOR_DISABLED: - captureCursor(window); + disableCursor(window); break; } }