Added a relative mouse mode that uses mouse warping instead of raw input.
authorSam Lantinga <slouken@libsdl.org>
Mon, 23 Dec 2013 17:37:22 -0800
changeset 80711ac2db4abe11
parent 8070 c0cedfd0c45e
child 8072 9d8865351afd
Added a relative mouse mode that uses mouse warping instead of raw input.
To enable this, set the environment variable SDL_MOUSE_RELATIVE_MODE_WARP to "1"

When mouse relative mode is disabled, put the cursor back where the application expects it to be, instead of where it was when relative mode was enabled.
include/SDL_hints.h
src/events/SDL_mouse.c
src/events/SDL_mouse_c.h
src/video/SDL_video.c
src/video/windows/SDL_windowsevents.c
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11xinput2.c
     1.1 --- a/include/SDL_hints.h	Mon Dec 23 17:15:32 2013 -0800
     1.2 +++ b/include/SDL_hints.h	Mon Dec 23 17:37:22 2013 -0800
     1.3 @@ -173,7 +173,18 @@
     1.4  #define SDL_HINT_GRAB_KEYBOARD              "SDL_GRAB_KEYBOARD"
     1.5  
     1.6  /**
     1.7 - *  \brief Minimize your SDL_Window if it loses key focus when in Fullscreen mode. Defaults to true.
     1.8 +*  \brief  A variable controlling whether relative mouse mode is implemented using mouse warping
     1.9 +*
    1.10 +*  This variable can be set to the following values:
    1.11 +*    "0"       - Relative mouse mode uses raw input
    1.12 +*    "1"       - Relative mouse mode uses mouse warping
    1.13 +*
    1.14 +*  By default SDL will use raw input for relative mouse mode
    1.15 +*/
    1.16 +#define SDL_HINT_MOUSE_RELATIVE_MODE_WARP    "SDL_MOUSE_RELATIVE_MODE_WARP"
    1.17 +
    1.18 +/**
    1.19 + *  \brief Minimize your SDL_Window if it loses key focus when in fullscreen mode. Defaults to true.
    1.20   *
    1.21   */
    1.22  #define SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS   "SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS"
     2.1 --- a/src/events/SDL_mouse.c	Mon Dec 23 17:15:32 2013 -0800
     2.2 +++ b/src/events/SDL_mouse.c	Mon Dec 23 17:37:22 2013 -0800
     2.3 @@ -23,6 +23,7 @@
     2.4  /* General mouse handling code for SDL */
     2.5  
     2.6  #include "SDL_assert.h"
     2.7 +#include "SDL_hints.h"
     2.8  #include "SDL_timer.h"
     2.9  #include "SDL_events.h"
    2.10  #include "SDL_events_c.h"
    2.11 @@ -205,12 +206,24 @@
    2.12      int yrel;
    2.13      int x_max = 0, y_max = 0;
    2.14  
    2.15 -    /* relative motion is calculated regarding the system cursor last position */
    2.16 +    if (mouse->relative_mode_warp) {
    2.17 +        int center_x = 0, center_y = 0;
    2.18 +        SDL_GetWindowSize(window, &center_x, &center_y);
    2.19 +        center_x /= 2;
    2.20 +        center_y /= 2;
    2.21 +        if (x == center_x && y == center_y) {
    2.22 +            mouse->last_x = center_x;
    2.23 +            mouse->last_y = center_y;
    2.24 +            return 0;
    2.25 +        }
    2.26 +        SDL_WarpMouseInWindow(window, center_x, center_y);
    2.27 +    }
    2.28 +
    2.29      if (relative) {
    2.30          xrel = x;
    2.31          yrel = y;
    2.32 -        x = (mouse->last_x + x);
    2.33 -        y = (mouse->last_y + y);
    2.34 +        x = (mouse->last_x + xrel);
    2.35 +        y = (mouse->last_y + yrel);
    2.36      } else {
    2.37          xrel = x - mouse->last_x;
    2.38          yrel = y - mouse->last_y;
    2.39 @@ -225,7 +238,7 @@
    2.40      }
    2.41  
    2.42      /* Update internal mouse coordinates */
    2.43 -    if (mouse->relative_mode == SDL_FALSE) {
    2.44 +    if (!mouse->relative_mode) {
    2.45          mouse->x = x;
    2.46          mouse->y = y;
    2.47      } else {
    2.48 @@ -481,21 +494,36 @@
    2.49      }
    2.50  }
    2.51  
    2.52 +static SDL_bool
    2.53 +ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
    2.54 +{
    2.55 +    const char *hint;
    2.56 +
    2.57 +    if (!mouse->SetRelativeMouseMode) {
    2.58 +        return SDL_TRUE;
    2.59 +    }
    2.60 +
    2.61 +    hint = SDL_GetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP);
    2.62 +    if (hint) {
    2.63 +        if (*hint == '0') {
    2.64 +            return SDL_FALSE;
    2.65 +        } else {
    2.66 +            return SDL_TRUE;
    2.67 +        }
    2.68 +    }
    2.69 +    return SDL_FALSE;
    2.70 +}
    2.71 +
    2.72  int
    2.73  SDL_SetRelativeMouseMode(SDL_bool enabled)
    2.74  {
    2.75      SDL_Mouse *mouse = SDL_GetMouse();
    2.76      SDL_Window *focusWindow = SDL_GetKeyboardFocus();
    2.77 -    int original_x = mouse->x, original_y = mouse->y;
    2.78  
    2.79      if (enabled == mouse->relative_mode) {
    2.80          return 0;
    2.81      }
    2.82  
    2.83 -    if (!mouse->SetRelativeMouseMode) {
    2.84 -        return SDL_Unsupported();
    2.85 -    }
    2.86 -
    2.87      if (enabled && focusWindow) {
    2.88          /* Center it in the focused window to prevent clicks from going through
    2.89           * to background windows.
    2.90 @@ -504,23 +532,26 @@
    2.91          SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
    2.92      }
    2.93  
    2.94 -    if (mouse->SetRelativeMouseMode(enabled) < 0) {
    2.95 +    /* Set the relative mode */
    2.96 +    if (!enabled && mouse->relative_mode_warp) {
    2.97 +        mouse->relative_mode_warp = SDL_FALSE;
    2.98 +    } else if (enabled && ShouldUseRelativeModeWarp(mouse)) {
    2.99 +        mouse->relative_mode_warp = SDL_TRUE;
   2.100 +    } else if (mouse->SetRelativeMouseMode(enabled) < 0) {
   2.101          return -1;
   2.102      }
   2.103 -
   2.104 -    /* Set the relative mode */
   2.105      mouse->relative_mode = enabled;
   2.106  
   2.107 -    if (enabled) {
   2.108 -        /* Save the expected mouse position */
   2.109 -        mouse->original_x = original_x;
   2.110 -        mouse->original_y = original_y;
   2.111 -    } else if (mouse->focus) {
   2.112 -        /* Restore the expected mouse position */
   2.113 -        SDL_WarpMouseInWindow(mouse->focus, mouse->original_x, mouse->original_y);
   2.114 +    if (mouse->focus) {
   2.115 +        SDL_UpdateWindowGrab(mouse->focus);
   2.116 +
   2.117 +        /* Put the cursor back to where the application expects it */
   2.118 +        if (!enabled) {
   2.119 +            SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
   2.120 +        }
   2.121      }
   2.122  
   2.123 -    /* Flush pending mouse motion */
   2.124 +    /* Flush pending mouse motion - ideally we would pump events, but that's not always safe */
   2.125      SDL_FlushEvent(SDL_MOUSEMOTION);
   2.126  
   2.127      /* Update cursor visibility */
     3.1 --- a/src/events/SDL_mouse_c.h	Mon Dec 23 17:15:32 2013 -0800
     3.2 +++ b/src/events/SDL_mouse_c.h	Mon Dec 23 17:37:22 2013 -0800
     3.3 @@ -73,8 +73,7 @@
     3.4      int last_x, last_y;         /* the last reported x and y coordinates */
     3.5      Uint32 buttonstate;
     3.6      SDL_bool relative_mode;
     3.7 -    /* the x and y coordinates when relative mode was activated */
     3.8 -    int original_x, original_y;
     3.9 +    SDL_bool relative_mode_warp;
    3.10  
    3.11      /* Data for double-click tracking */
    3.12      int num_clickstates;
     4.1 --- a/src/video/SDL_video.c	Mon Dec 23 17:15:32 2013 -0800
     4.2 +++ b/src/video/SDL_video.c	Mon Dec 23 17:37:22 2013 -0800
     4.3 @@ -2055,8 +2055,9 @@
     4.4  {
     4.5      if (_this->SetWindowGrab) {
     4.6          SDL_bool grabbed;
     4.7 -        if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
     4.8 -            (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
     4.9 +        if (SDL_GetMouse()->relative_mode_warp ||
    4.10 +            ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
    4.11 +             (window->flags & SDL_WINDOW_INPUT_FOCUS))) {
    4.12              grabbed = SDL_TRUE;
    4.13          } else {
    4.14              grabbed = SDL_FALSE;
     5.1 --- a/src/video/windows/SDL_windowsevents.c	Mon Dec 23 17:15:32 2013 -0800
     5.2 +++ b/src/video/windows/SDL_windowsevents.c	Mon Dec 23 17:37:22 2013 -0800
     5.3 @@ -290,6 +290,7 @@
     5.4  WIN_UpdateClipCursor(SDL_Window *window)
     5.5  {
     5.6      SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
     5.7 +    SDL_Mouse *mouse = SDL_GetMouse();
     5.8  
     5.9      /* Don't clip the cursor while we're in the modal resize or move loop */
    5.10      if (data->in_modal_loop) {
    5.11 @@ -297,7 +298,7 @@
    5.12          return;
    5.13      }
    5.14          
    5.15 -    if (SDL_GetMouse()->relative_mode) {
    5.16 +    if (mouse->relative_mode && !mouse->relative_mode_warp) {
    5.17          LONG cx, cy;
    5.18          RECT rect;
    5.19          GetWindowRect(data->hwnd, &rect);
    5.20 @@ -312,8 +313,9 @@
    5.21          rect.bottom = cy+1;
    5.22  
    5.23          ClipCursor(&rect);
    5.24 -    } else if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
    5.25 -               (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
    5.26 +    } else if (mouse->relative_mode_warp ||
    5.27 +               ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
    5.28 +                (window->flags & SDL_WINDOW_INPUT_FOCUS))) {
    5.29          RECT rect;
    5.30          if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
    5.31              ClientToScreen(data->hwnd, (LPPOINT) & rect);
    5.32 @@ -426,8 +428,12 @@
    5.33          break;
    5.34  
    5.35      case WM_MOUSEMOVE:
    5.36 -        if( !SDL_GetMouse()->relative_mode )
    5.37 -            SDL_SendMouseMotion(data->window, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
    5.38 +        {
    5.39 +            SDL_Mouse *mouse = SDL_GetMouse();
    5.40 +            if (!mouse->relative_mode || mouse->relative_mode_warp) {
    5.41 +                SDL_SendMouseMotion(data->window, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
    5.42 +            }
    5.43 +        }
    5.44          /* don't break here, fall through to check the wParam like the button presses */
    5.45      case WM_LBUTTONUP:
    5.46      case WM_RBUTTONUP:
    5.47 @@ -437,49 +443,50 @@
    5.48      case WM_RBUTTONDOWN:
    5.49      case WM_MBUTTONDOWN:
    5.50      case WM_XBUTTONDOWN:
    5.51 -        if( !SDL_GetMouse()->relative_mode )
    5.52 -            WIN_CheckWParamMouseButtons( wParam, data );
    5.53 +        {
    5.54 +            SDL_Mouse *mouse = SDL_GetMouse();
    5.55 +            if (!mouse->relative_mode || mouse->relative_mode_warp) {
    5.56 +                WIN_CheckWParamMouseButtons(wParam, data);
    5.57 +            }
    5.58 +        }
    5.59          break;
    5.60  
    5.61      case WM_INPUT:
    5.62 -    {
    5.63 -        HRAWINPUT hRawInput = (HRAWINPUT)lParam;
    5.64 -        RAWINPUT inp;
    5.65 -        UINT size = sizeof(inp);
    5.66 +        {
    5.67 +            SDL_Mouse *mouse = SDL_GetMouse();
    5.68 +            HRAWINPUT hRawInput = (HRAWINPUT)lParam;
    5.69 +            RAWINPUT inp;
    5.70 +            UINT size = sizeof(inp);
    5.71  
    5.72 -        if(!SDL_GetMouse()->relative_mode)
    5.73 -            break;
    5.74 +            if (!mouse->relative_mode || mouse->relative_mode_warp) {
    5.75 +                break;
    5.76 +            }
    5.77  
    5.78 -        GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
    5.79 +            GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
    5.80  
    5.81 -        /* Mouse data */
    5.82 -        if(inp.header.dwType == RIM_TYPEMOUSE)
    5.83 -        {
    5.84 -            RAWMOUSE* mouse = &inp.data.mouse;
    5.85 +            /* Mouse data */
    5.86 +            if (inp.header.dwType == RIM_TYPEMOUSE) {
    5.87 +                RAWMOUSE* mouse = &inp.data.mouse;
    5.88  
    5.89 -            if((mouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE)
    5.90 -            {
    5.91 -                SDL_SendMouseMotion(data->window, 0, 1, (int)mouse->lLastX, (int)mouse->lLastY);
    5.92 -            }
    5.93 -            else
    5.94 -            {
    5.95 -                /* synthesize relative moves from the abs position */
    5.96 -                static SDL_Point initialMousePoint;
    5.97 -                if ( initialMousePoint.x == 0 && initialMousePoint.y == 0 )
    5.98 -                {
    5.99 +                if ((mouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE) {
   5.100 +                    SDL_SendMouseMotion(data->window, 0, 1, (int)mouse->lLastX, (int)mouse->lLastY);
   5.101 +                } else {
   5.102 +                    /* synthesize relative moves from the abs position */
   5.103 +                    static SDL_Point initialMousePoint;
   5.104 +                    if (initialMousePoint.x == 0 && initialMousePoint.y == 0) {
   5.105 +                        initialMousePoint.x = mouse->lLastX;
   5.106 +                        initialMousePoint.y = mouse->lLastY;
   5.107 +                    }
   5.108 +
   5.109 +                    SDL_SendMouseMotion(data->window, 0, 1, (int)(mouse->lLastX-initialMousePoint.x), (int)(mouse->lLastY-initialMousePoint.y) );
   5.110 +
   5.111                      initialMousePoint.x = mouse->lLastX;
   5.112                      initialMousePoint.y = mouse->lLastY;
   5.113                  }
   5.114 -
   5.115 -                SDL_SendMouseMotion(data->window, 0, 1, (int)(mouse->lLastX-initialMousePoint.x), (int)(mouse->lLastY-initialMousePoint.y) );
   5.116 -
   5.117 -                initialMousePoint.x = mouse->lLastX;
   5.118 -                initialMousePoint.y = mouse->lLastY;
   5.119 +                WIN_CheckRawMouseButtons( mouse->usButtonFlags, data );
   5.120              }
   5.121 -            WIN_CheckRawMouseButtons( mouse->usButtonFlags, data );
   5.122          }
   5.123          break;
   5.124 -    }
   5.125  
   5.126      case WM_MOUSEWHEEL:
   5.127          {
   5.128 @@ -497,8 +504,8 @@
   5.129                      s_AccumulatedMotion += WHEEL_DELTA;
   5.130                  }
   5.131              }
   5.132 -            break;
   5.133          }
   5.134 +        break;
   5.135  
   5.136      case WM_MOUSEHWHEEL:
   5.137          {
   5.138 @@ -516,8 +523,8 @@
   5.139                      s_AccumulatedMotion += WHEEL_DELTA;
   5.140                  }
   5.141              }
   5.142 -            break;
   5.143          }
   5.144 +        break;
   5.145  
   5.146  #ifdef WM_MOUSELEAVE
   5.147      case WM_MOUSELEAVE:
   5.148 @@ -549,8 +556,8 @@
   5.149  
   5.150              GetKeyboardState(keyboardState);
   5.151              if (ToUnicode(wParam, (lParam >> 16) & 0xff, keyboardState, (LPWSTR)&utf32, 1, 0) > 0) {
   5.152 -                WORD repitition;
   5.153 -                for (repitition = lParam & 0xffff; repitition > 0; repitition--) {
   5.154 +                WORD repetition;
   5.155 +                for (repetition = lParam & 0xffff; repetition > 0; repetition--) {
   5.156                      WIN_ConvertUTF32toUTF8(utf32, text);
   5.157                      SDL_SendKeyboardText(text);
   5.158                  }
     6.1 --- a/src/video/x11/SDL_x11events.c	Mon Dec 23 17:15:32 2013 -0800
     6.2 +++ b/src/video/x11/SDL_x11events.c	Mon Dec 23 17:37:22 2013 -0800
     6.3 @@ -698,7 +698,7 @@
     6.4  
     6.5      case MotionNotify:{
     6.6              SDL_Mouse *mouse = SDL_GetMouse();
     6.7 -            if(!mouse->relative_mode) {
     6.8 +            if(!mouse->relative_mode || mouse->relative_mode_warp) {
     6.9  #ifdef DEBUG_MOTION
    6.10                  printf("window %p: X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
    6.11  #endif
     7.1 --- a/src/video/x11/SDL_x11xinput2.c	Mon Dec 23 17:15:32 2013 -0800
     7.2 +++ b/src/video/x11/SDL_x11xinput2.c	Mon Dec 23 17:37:22 2013 -0800
     7.3 @@ -134,7 +134,7 @@
     7.4              SDL_Mouse *mouse = SDL_GetMouse();
     7.5              double relative_cords[2];
     7.6  
     7.7 -            if (!mouse->relative_mode) {
     7.8 +            if (!mouse->relative_mode || mouse->relative_mode_warp) {
     7.9                  return 0;
    7.10              }
    7.11