Cursor mode cleanup.

This commit is contained in:
Camilla Berglund 2014-01-15 13:21:13 +01:00
parent 54b8d0d3c8
commit e0a26aa62f
6 changed files with 56 additions and 97 deletions

View File

@ -41,9 +41,9 @@ static void centerCursor(_GLFWwindow *window)
// Update the cursor to match the specified cursor mode // 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]; [[NSCursor arrowCursor] set];
else else
[(NSCursor*) _glfw.ns.cursor set]; [(NSCursor*) _glfw.ns.cursor set];
@ -472,7 +472,7 @@ static int translateKey(unsigned int key)
- (void)cursorUpdate:(NSEvent *)event - (void)cursorUpdate:(NSEvent *)event
{ {
setModeCursor(window, window->cursorMode); setModeCursor(window);
} }
- (void)mouseDown:(NSEvent *)event - (void)mouseDown:(NSEvent *)event
@ -1084,7 +1084,7 @@ void _glfwPlatformWaitEvents(void)
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
{ {
setModeCursor(window, window->cursorMode); setModeCursor(window);
if (window->monitor) 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); CGAssociateMouseAndMouseCursorPosition(false);
centerCursor(window); centerCursor(window);

View File

@ -70,7 +70,7 @@ static void setCursorMode(_GLFWwindow* window, int newMode)
_glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0); _glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0);
} }
_glfwPlatformSetCursorMode(window, newMode); _glfwPlatformApplyCursorMode(window);
} }
} }

View File

@ -364,12 +364,11 @@ const char* _glfwPlatformGetVersionString(void);
*/ */
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos); void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos);
/*! @brief Sets up the specified cursor mode for the specified window. /*! @brief Applies the cursor mode of the specified window to the system.
* @param[in] window The window whose cursor mode to change. * @param[in] window The window whose cursor mode to apply.
* @param[in] mode The desired cursor mode.
* @ingroup platform * @ingroup platform
*/ */
void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode); void _glfwPlatformApplyCursorMode(_GLFWwindow* window);
/*! @copydoc glfwGetMonitors /*! @copydoc glfwGetMonitors
* @ingroup platform * @ingroup platform

View File

@ -45,7 +45,7 @@ static void updateClipRect(_GLFWwindow* window)
ClipCursor(&clipRect); ClipCursor(&clipRect);
} }
// Hide mouse cursor // Hide the mouse cursor
// //
static void hideCursor(_GLFWwindow* window) 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) if (!window->win32.cursorHidden)
{ {
@ -81,9 +81,9 @@ static void captureCursor(_GLFWwindow* window)
SetCapture(window->win32.handle); SetCapture(window->win32.handle);
} }
// Show mouse cursor // Restores the mouse cursor
// //
static void showCursor(_GLFWwindow* window) static void restoreCursor(_GLFWwindow* window)
{ {
POINT pos; POINT pos;
@ -410,7 +410,7 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
// The window was defocused (or iconified, see above) // The window was defocused (or iconified, see above)
if (window->cursorMode != GLFW_CURSOR_NORMAL) if (window->cursorMode != GLFW_CURSOR_NORMAL)
showCursor(window); restoreCursor(window);
if (window->monitor) if (window->monitor)
{ {
@ -428,10 +428,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
{ {
// The window was focused // The window was focused
if (window->cursorMode == GLFW_CURSOR_DISABLED) if (window->cursorMode != GLFW_CURSOR_NORMAL)
captureCursor(window); _glfwPlatformApplyCursorMode(window);
else if (window->cursorMode == GLFW_CURSOR_HIDDEN)
hideCursor(window);
if (window->monitor) if (window->monitor)
_glfwSetVideoMode(window->monitor, &window->videoMode); _glfwSetVideoMode(window->monitor, &window->videoMode);
@ -678,9 +676,9 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
case WM_SIZE: case WM_SIZE:
{ {
if (window->cursorMode == GLFW_CURSOR_DISABLED && if (_glfw.focusedWindow == window)
_glfw.focusedWindow == window)
{ {
if (window->cursorMode == GLFW_CURSOR_DISABLED)
updateClipRect(window); updateClipRect(window);
} }
@ -691,9 +689,9 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
case WM_MOVE: case WM_MOVE:
{ {
if (window->cursorMode == GLFW_CURSOR_DISABLED && if (_glfw.focusedWindow == window)
_glfw.focusedWindow == window)
{ {
if (window->cursorMode == GLFW_CURSOR_DISABLED)
updateClipRect(window); updateClipRect(window);
} }
@ -713,13 +711,15 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
case WM_SETCURSOR: case WM_SETCURSOR:
{ {
if (window->cursorMode != GLFW_CURSOR_NORMAL && if (_glfw.focusedWindow == window && LOWORD(lParam) == HTCLIENT)
_glfw.focusedWindow == window && {
LOWORD(lParam) == HTCLIENT) if (window->cursorMode == GLFW_CURSOR_HIDDEN ||
window->cursorMode == GLFW_CURSOR_DISABLED)
{ {
SetCursor(NULL); SetCursor(NULL);
return TRUE; return TRUE;
} }
}
break; break;
} }
@ -1113,7 +1113,7 @@ void _glfwPlatformPollEvents(void)
_glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, 0, GLFW_RELEASE, mods); _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 && if (window->cursorMode == GLFW_CURSOR_DISABLED &&
!window->win32.cursorCentered) !window->win32.cursorCentered)
{ {
@ -1142,18 +1142,18 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos)
window->win32.oldCursorY = (int) 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: case GLFW_CURSOR_NORMAL:
showCursor(window); restoreCursor(window);
break; break;
case GLFW_CURSOR_HIDDEN: case GLFW_CURSOR_HIDDEN:
hideCursor(window); hideCursor(window);
break; break;
case GLFW_CURSOR_DISABLED: case GLFW_CURSOR_DISABLED:
captureCursor(window); disableCursor(window);
break; break;
} }
} }

View File

@ -80,8 +80,6 @@ typedef struct _GLFWwindowX11
// Various platform specific internal variables // Various platform specific internal variables
GLboolean overrideRedirect; // True if window is OverrideRedirect 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 // Cached position and size used to filter out duplicate events
int width, height; int width, height;

View File

@ -317,62 +317,30 @@ static GLboolean createWindow(_GLFWwindow* window,
return GL_TRUE; return GL_TRUE;
} }
// Hide cursor // Hide the mouse cursor
// //
static void hideCursor(_GLFWwindow* window) 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); XUngrabPointer(_glfw.x11.display, CurrentTime);
window->x11.cursorGrabbed = GL_FALSE;
}
if (!window->x11.cursorHidden)
{
XDefineCursor(_glfw.x11.display, window->x11.handle, _glfw.x11.cursor); XDefineCursor(_glfw.x11.display, window->x11.handle, _glfw.x11.cursor);
window->x11.cursorHidden = GL_TRUE;
}
} }
// Capture cursor // Disable the mouse cursor
// //
static void captureCursor(_GLFWwindow* window) static void disableCursor(_GLFWwindow* window)
{ {
if (!window->x11.cursorGrabbed) XGrabPointer(_glfw.x11.display, window->x11.handle, True,
{ ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
if (XGrabPointer(_glfw.x11.display, window->x11.handle, True, GrabModeAsync, GrabModeAsync,
ButtonPressMask | ButtonReleaseMask | window->x11.handle, _glfw.x11.cursor, CurrentTime);
PointerMotionMask, GrabModeAsync, GrabModeAsync,
window->x11.handle, _glfw.x11.cursor, CurrentTime) ==
GrabSuccess)
{
window->x11.cursorGrabbed = GL_TRUE;
}
}
} }
// 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); XUngrabPointer(_glfw.x11.display, CurrentTime);
window->x11.cursorGrabbed = GL_FALSE;
}
// Show cursor
if (window->x11.cursorHidden)
{
XUndefineCursor(_glfw.x11.display, window->x11.handle); XUndefineCursor(_glfw.x11.display, window->x11.handle);
window->x11.cursorHidden = GL_FALSE;
}
} }
// Enter fullscreen mode // Enter fullscreen mode
@ -640,18 +608,12 @@ static void processEvent(XEvent *event)
case EnterNotify: case EnterNotify:
{ {
if (window->cursorMode == GLFW_CURSOR_HIDDEN)
hideCursor(window);
_glfwInputCursorEnter(window, GL_TRUE); _glfwInputCursorEnter(window, GL_TRUE);
break; break;
} }
case LeaveNotify: case LeaveNotify:
{ {
if (window->cursorMode == GLFW_CURSOR_HIDDEN)
showCursor(window);
_glfwInputCursorEnter(window, GL_FALSE); _glfwInputCursorEnter(window, GL_FALSE);
break; break;
} }
@ -763,7 +725,7 @@ static void processEvent(XEvent *event)
_glfwInputWindowFocus(window, GL_TRUE); _glfwInputWindowFocus(window, GL_TRUE);
if (window->cursorMode == GLFW_CURSOR_DISABLED) if (window->cursorMode == GLFW_CURSOR_DISABLED)
captureCursor(window); disableCursor(window);
break; break;
} }
@ -773,7 +735,7 @@ static void processEvent(XEvent *event)
_glfwInputWindowFocus(window, GL_FALSE); _glfwInputWindowFocus(window, GL_FALSE);
if (window->cursorMode == GLFW_CURSOR_DISABLED) if (window->cursorMode == GLFW_CURSOR_DISABLED)
showCursor(window); restoreCursor(window);
break; break;
} }
@ -1202,18 +1164,18 @@ void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
0,0,0,0, (int) x, (int) 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: case GLFW_CURSOR_NORMAL:
showCursor(window); restoreCursor(window);
break; break;
case GLFW_CURSOR_HIDDEN: case GLFW_CURSOR_HIDDEN:
hideCursor(window); hideCursor(window);
break; break;
case GLFW_CURSOR_DISABLED: case GLFW_CURSOR_DISABLED:
captureCursor(window); disableCursor(window);
break; break;
} }
} }