src/events/SDL_mouse.c
changeset 7107 2fcf7bf1d2b2
parent 7104 4e4ca313000c
child 7191 75360622e65f
     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);