Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Move cursor into window when enabling relative mode or gaining focus …
Browse files Browse the repository at this point in the history
…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 1a2db6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/events/SDL_mouse.c
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -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);
}

Expand All @@ -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 */
Expand Down

0 comments on commit 1a2db6c

Please sign in to comment.