From 13fbb4748ad24a1b9d02c5eabb648b743232f137 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 2 Jul 2015 13:04:56 +0200 Subject: [PATCH] Clean up cursor mode setting --- src/cocoa_window.m | 35 ++++++++---------- src/input.c | 2 +- src/internal.h | 6 ++-- src/mir_window.c | 2 +- src/win32_window.c | 90 +++++++++++++--------------------------------- src/wl_window.c | 2 +- src/x11_window.c | 76 +++++++++++++++------------------------ 7 files changed, 74 insertions(+), 139 deletions(-) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 62b7b66c..9ef4e036 100644 --- a/src/cocoa_window.m +++ b/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); diff --git a/src/input.c b/src/input.c index 932f4ded..721c7a28 100644 --- a/src/input.c +++ b/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); } } diff --git a/src/internal.h b/src/internal.h index ca583452..6ff5efe9 100644 --- a/src/internal.h +++ b/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 diff --git a/src/mir_window.c b/src/mir_window.c index acfc714a..f97744b2 100644 --- a/src/mir_window.c +++ b/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__); diff --git a/src/win32_window.c b/src/win32_window.c index 194d5d67..4c11d9ea 100644 --- a/src/win32_window.c +++ b/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, diff --git a/src/wl_window.c b/src/wl_window.c index 5910cfb7..c49e83f7 100644 --- a/src/wl_window.c +++ b/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); } diff --git a/src/x11_window.c b/src/x11_window.c index f8a15cb4..3be8c821 100644 --- a/src/x11_window.c +++ b/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); + } } }