Move cursor into window when enabling relative mode or gaining focus in relative mode.
authorJørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 24 Apr 2013 12:20:51 -0700
changeset 71072fcf7bf1d2b2
parent 7106 31f8acac196b
child 7108 732d2cac80c0
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.
src/events/SDL_mouse.c
src/video/SDL_video.c
     1.1 --- a/src/events/SDL_mouse.c	Wed Apr 24 12:20:48 2013 -0700
     1.2 +++ b/src/events/SDL_mouse.c	Wed Apr 24 12:20:51 2013 -0700
     1.3 @@ -415,6 +415,8 @@
     1.4  SDL_SetRelativeMouseMode(SDL_bool enabled)
     1.5  {
     1.6      SDL_Mouse *mouse = SDL_GetMouse();
     1.7 +    SDL_Window *focusWindow = SDL_GetKeyboardFocus();
     1.8 +    int original_x = mouse->x, original_y = mouse->y;
     1.9  
    1.10      if (enabled == mouse->relative_mode) {
    1.11          return 0;
    1.12 @@ -424,6 +426,14 @@
    1.13          return SDL_Unsupported();
    1.14      }
    1.15  
    1.16 +    if (enabled && focusWindow) {
    1.17 +        /* Center it in the focused window to prevent clicks from going through
    1.18 +         * to background windows.
    1.19 +         */
    1.20 +        SDL_SetMouseFocus(focusWindow);
    1.21 +        SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
    1.22 +    }
    1.23 +
    1.24      if (mouse->SetRelativeMouseMode(enabled) < 0) {
    1.25          return -1;
    1.26      }
    1.27 @@ -433,8 +443,8 @@
    1.28  
    1.29      if (enabled) {
    1.30          /* Save the expected mouse position */
    1.31 -        mouse->original_x = mouse->x;
    1.32 -        mouse->original_y = mouse->y;
    1.33 +        mouse->original_x = original_x;
    1.34 +        mouse->original_y = original_y;
    1.35      } else if (mouse->focus) {
    1.36          /* Restore the expected mouse position */
    1.37          SDL_WarpMouseInWindow(mouse->focus, mouse->original_x, mouse->original_y);
     2.1 --- a/src/video/SDL_video.c	Wed Apr 24 12:20:48 2013 -0700
     2.2 +++ b/src/video/SDL_video.c	Wed Apr 24 12:20:51 2013 -0700
     2.3 @@ -2044,10 +2044,17 @@
     2.4  void
     2.5  SDL_OnWindowFocusGained(SDL_Window * window)
     2.6  {
     2.7 +    SDL_Mouse *mouse = SDL_GetMouse();
     2.8 +
     2.9      if (window->gamma && _this->SetWindowGammaRamp) {
    2.10          _this->SetWindowGammaRamp(_this, window, window->gamma);
    2.11      }
    2.12  
    2.13 +    if (mouse && mouse->relative_mode) {
    2.14 +        SDL_SetMouseFocus(window);
    2.15 +        SDL_WarpMouseInWindow(window, window->w/2, window->h/2);
    2.16 +    }
    2.17 +
    2.18      SDL_UpdateWindowGrab(window);
    2.19  }
    2.20  
    2.21 @@ -2067,10 +2074,17 @@
    2.22  void
    2.23  SDL_OnWindowFocusLost(SDL_Window * window)
    2.24  {
    2.25 +    SDL_Mouse *mouse = SDL_GetMouse();
    2.26 +
    2.27      if (window->gamma && _this->SetWindowGammaRamp) {
    2.28          _this->SetWindowGammaRamp(_this, window, window->saved_gamma);
    2.29      }
    2.30  
    2.31 +    if (mouse && mouse->relative_mode) {
    2.32 +        /* Restore the expected mouse position */
    2.33 +        SDL_WarpMouseInWindow(window, mouse->original_x, mouse->original_y);
    2.34 +    }
    2.35 +
    2.36      SDL_UpdateWindowGrab(window);
    2.37  
    2.38      /* If we're fullscreen on a single-head system and lose focus, minimize */