Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Move cursor into window when enabling relative mode or gaining focus …

…in relative mode.

This prevents wonky behavior where the clicks won't go to the window
because the cursor was outside it when we enabled relative mode.
  • Loading branch information
jorgenpt committed Apr 24, 2013
1 parent 06f0751 commit 1a2db6c030038302bd9b2fc8a3e78c9bc3b16c82
Showing with 26 additions and 2 deletions.
  1. +12 −2 src/events/SDL_mouse.c
  2. +14 −0 src/video/SDL_video.c
@@ -415,6 +415,8 @@ int
SDL_SetRelativeMouseMode(SDL_bool enabled)
{
SDL_Mouse *mouse = SDL_GetMouse();
SDL_Window *focusWindow = SDL_GetKeyboardFocus();
int original_x = mouse->x, original_y = mouse->y;

if (enabled == mouse->relative_mode) {
return 0;
@@ -424,6 +426,14 @@ SDL_SetRelativeMouseMode(SDL_bool enabled)
return SDL_Unsupported();
}

if (enabled && focusWindow) {
/* Center it in the focused window to prevent clicks from going through
* to background windows.
*/
SDL_SetMouseFocus(focusWindow);
SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
}

if (mouse->SetRelativeMouseMode(enabled) < 0) {
return -1;
}
@@ -433,8 +443,8 @@ SDL_SetRelativeMouseMode(SDL_bool enabled)

if (enabled) {
/* Save the expected mouse position */
mouse->original_x = mouse->x;
mouse->original_y = mouse->y;
mouse->original_x = original_x;
mouse->original_y = original_y;
} else if (mouse->focus) {
/* Restore the expected mouse position */
SDL_WarpMouseInWindow(mouse->focus, mouse->original_x, mouse->original_y);
@@ -2044,10 +2044,17 @@ SDL_OnWindowLeave(SDL_Window * window)
void
SDL_OnWindowFocusGained(SDL_Window * window)
{
SDL_Mouse *mouse = SDL_GetMouse();

if (window->gamma && _this->SetWindowGammaRamp) {
_this->SetWindowGammaRamp(_this, window, window->gamma);
}

if (mouse && mouse->relative_mode) {
SDL_SetMouseFocus(window);
SDL_WarpMouseInWindow(window, window->w/2, window->h/2);
}

SDL_UpdateWindowGrab(window);
}

@@ -2067,10 +2074,17 @@ static SDL_bool ShouldMinimizeOnFocusLoss()
void
SDL_OnWindowFocusLost(SDL_Window * window)
{
SDL_Mouse *mouse = SDL_GetMouse();

if (window->gamma && _this->SetWindowGammaRamp) {
_this->SetWindowGammaRamp(_this, window, window->saved_gamma);
}

if (mouse && mouse->relative_mode) {
/* Restore the expected mouse position */
SDL_WarpMouseInWindow(window, mouse->original_x, mouse->original_y);
}

SDL_UpdateWindowGrab(window);

/* If we're fullscreen on a single-head system and lose focus, minimize */

0 comments on commit 1a2db6c

Please sign in to comment.