Added an API function to warp the mouse cursor in global screen space: SDL_WarpMouseGlobal()
authorSam Lantinga <slouken@libsdl.org>
Wed, 04 Jun 2014 10:55:26 -0700
changeset 8815c6d0a457f3b2
parent 8814 8a6a21356035
child 8816 7e2f5d693149
Added an API function to warp the mouse cursor in global screen space: SDL_WarpMouseGlobal()
include/SDL_mouse.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/events/SDL_mouse.c
src/events/SDL_mouse_c.h
src/video/cocoa/SDL_cocoamouse.m
src/video/mir/SDL_mirmouse.c
src/video/raspberry/SDL_rpimouse.c
src/video/wayland/SDL_waylandmouse.c
src/video/windows/SDL_windowsmouse.c
src/video/x11/SDL_x11mouse.c
     1.1 --- a/include/SDL_mouse.h	Wed Jun 04 10:52:34 2014 -0700
     1.2 +++ b/include/SDL_mouse.h	Wed Jun 04 10:55:26 2014 -0700
     1.3 @@ -99,6 +99,16 @@
     1.4                                                     int x, int y);
     1.5  
     1.6  /**
     1.7 + *  \brief Moves the mouse to the given position in global screen space.
     1.8 + *
     1.9 + *  \param x The x coordinate
    1.10 + *  \param y The y coordinate
    1.11 + *
    1.12 + *  \note This function generates a mouse motion event
    1.13 + */
    1.14 +extern DECLSPEC void SDLCALL SDL_WarpMouseGlobal(int x, int y);
    1.15 +
    1.16 +/**
    1.17   *  \brief Set relative mouse mode.
    1.18   *
    1.19   *  \param enabled Whether or not to enable relative mode
     2.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Wed Jun 04 10:52:34 2014 -0700
     2.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Wed Jun 04 10:55:26 2014 -0700
     2.3 @@ -576,6 +576,7 @@
     2.4  #define SDL_GetAssertionHandler SDL_GetAssertionHandler_REAL
     2.5  #define SDL_DXGIGetOutputInfo SDL_DXGIGetOutputInfo_REAL
     2.6  #define SDL_RenderIsClipEnabled SDL_RenderIsClipEnabled_REAL
     2.7 +#define SDL_WinRTRunApp SDL_WinRTRunApp_REAL
     2.8 +#define SDL_WarpMouseGlobal SDL_WarpMouseGlobal_REAL
     2.9  #define SDL_WinRTGetFSPathUNICODE SDL_WinRTGetFSPathUNICODE_REAL
    2.10  #define SDL_WinRTGetFSPathUTF8 SDL_WinRTGetFSPathUTF8_REAL
    2.11 -#define SDL_WinRTRunApp SDL_WinRTRunApp_REAL
     3.1 --- a/src/dynapi/SDL_dynapi_procs.h	Wed Jun 04 10:52:34 2014 -0700
     3.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Wed Jun 04 10:55:26 2014 -0700
     3.3 @@ -608,7 +608,8 @@
     3.4  #endif
     3.5  SDL_DYNAPI_PROC(SDL_bool,SDL_RenderIsClipEnabled,(SDL_Renderer *a),(a),return)
     3.6  #ifdef __WINRT__
     3.7 +SDL_DYNAPI_PROC(int,SDL_WinRTRunApp,(int a, char **b, void *c),(a,b,c),return)
     3.8  SDL_DYNAPI_PROC(const wchar_t*,SDL_WinRTGetFSPathUNICODE,(SDL_WinRT_Path a),(a),return)
     3.9  SDL_DYNAPI_PROC(const char*,SDL_WinRTGetFSPathUTF8,(SDL_WinRT_Path a),(a),return)
    3.10 -SDL_DYNAPI_PROC(int,SDL_WinRTRunApp,(int a, char **b, void *c),(a,b,c),return)
    3.11  #endif
    3.12 +SDL_DYNAPI_PROC(void,SDL_WarpMouseGlobal,(int a, int b),(a,b),)
     4.1 --- a/src/events/SDL_mouse.c	Wed Jun 04 10:52:34 2014 -0700
     4.2 +++ b/src/events/SDL_mouse.c	Wed Jun 04 10:55:26 2014 -0700
     4.3 @@ -482,10 +482,10 @@
     4.4  {
     4.5      SDL_Mouse *mouse = SDL_GetMouse();
     4.6  
     4.7 -    if ( window == NULL )
     4.8 +    if (window == NULL)
     4.9          window = mouse->focus;
    4.10  
    4.11 -    if ( window == NULL )
    4.12 +    if (window == NULL)
    4.13          return;
    4.14  
    4.15      if (mouse->WarpMouse) {
    4.16 @@ -495,6 +495,16 @@
    4.17      }
    4.18  }
    4.19  
    4.20 +void
    4.21 +SDL_WarpMouseGlobal(int x, int y)
    4.22 +{
    4.23 +    SDL_Mouse *mouse = SDL_GetMouse();
    4.24 +
    4.25 +    if (mouse->WarpMouseGlobal) {
    4.26 +        mouse->WarpMouseGlobal(x, y);
    4.27 +    }
    4.28 +}
    4.29 +
    4.30  static SDL_bool
    4.31  ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
    4.32  {
     5.1 --- a/src/events/SDL_mouse_c.h	Wed Jun 04 10:52:34 2014 -0700
     5.2 +++ b/src/events/SDL_mouse_c.h	Wed Jun 04 10:55:26 2014 -0700
     5.3 @@ -57,9 +57,12 @@
     5.4      /* Free a window manager cursor */
     5.5      void (*FreeCursor) (SDL_Cursor * cursor);
     5.6  
     5.7 -    /* Warp the mouse to (x,y) */
     5.8 +    /* Warp the mouse to (x,y) within a window */
     5.9      void (*WarpMouse) (SDL_Window * window, int x, int y);
    5.10  
    5.11 +    /* Warp the mouse to (x,y) in screen space */
    5.12 +    void (*WarpMouseGlobal) (int x, int y);
    5.13 +
    5.14      /* Set relative mode */
    5.15      int (*SetRelativeMouseMode) (SDL_bool enabled);
    5.16  
     6.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Wed Jun 04 10:52:34 2014 -0700
     6.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Wed Jun 04 10:55:26 2014 -0700
     6.3 @@ -241,6 +241,30 @@
     6.4      }
     6.5  }
     6.6  
     6.7 +static void
     6.8 +Cocoa_WarpMouseGlobal(int x, int y)
     6.9 +{
    6.10 +    SDL_Mouse *mouse = SDL_GetMouse();
    6.11 +    CGPoint point = CGPointMake((float)x, (float)y);
    6.12 +
    6.13 +    Cocoa_HandleMouseWarp(point.x, point.y);
    6.14 +
    6.15 +    /* According to the docs, this was deprecated in 10.6, but it's still
    6.16 +     * around. The substitute requires a CGEventSource, but I'm not entirely
    6.17 +     * sure how we'd procure the right one for this event.
    6.18 +     */
    6.19 +    CGSetLocalEventsSuppressionInterval(0.0);
    6.20 +    CGWarpMouseCursorPosition(point);
    6.21 +    CGSetLocalEventsSuppressionInterval(0.25);
    6.22 +
    6.23 +    if (!mouse->relative_mode && mouse->focus) {
    6.24 +        /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
    6.25 +         * other implementations' APIs.
    6.26 +         */
    6.27 +        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x - mouse->focus.x, y - mouse->focus.y);
    6.28 +    }
    6.29 +}
    6.30 +
    6.31  static int
    6.32  Cocoa_SetRelativeMouseMode(SDL_bool enabled)
    6.33  {
    6.34 @@ -295,6 +319,7 @@
    6.35      mouse->ShowCursor = Cocoa_ShowCursor;
    6.36      mouse->FreeCursor = Cocoa_FreeCursor;
    6.37      mouse->WarpMouse = Cocoa_WarpMouse;
    6.38 +    mouse->WarpMouseGlobal = Cocoa_WarpMouseGlobal;
    6.39      mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
    6.40  
    6.41      SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
     7.1 --- a/src/video/mir/SDL_mirmouse.c	Wed Jun 04 10:52:34 2014 -0700
     7.2 +++ b/src/video/mir/SDL_mirmouse.c	Wed Jun 04 10:55:26 2014 -0700
     7.3 @@ -110,6 +110,12 @@
     7.4      SDL_Unsupported();
     7.5  }
     7.6  
     7.7 +static void
     7.8 +MIR_WarpMouseGlobal(int x, int y)
     7.9 +{
    7.10 +    SDL_Unsupported();
    7.11 +}
    7.12 +
    7.13  static int
    7.14  MIR_SetRelativeMouseMode(SDL_bool enabled)
    7.15  {
    7.16 @@ -126,6 +132,7 @@
    7.17      mouse->ShowCursor           = MIR_ShowCursor;
    7.18      mouse->FreeCursor           = MIR_FreeCursor;
    7.19      mouse->WarpMouse            = MIR_WarpMouse;
    7.20 +    mouse->WarpMouseGlobal      = MIR_WarpMouseGlobal;
    7.21      mouse->CreateSystemCursor   = MIR_CreateSystemCursor;
    7.22      mouse->SetRelativeMouseMode = MIR_SetRelativeMouseMode;
    7.23  
     8.1 --- a/src/video/raspberry/SDL_rpimouse.c	Wed Jun 04 10:52:34 2014 -0700
     8.2 +++ b/src/video/raspberry/SDL_rpimouse.c	Wed Jun 04 10:55:26 2014 -0700
     8.3 @@ -48,6 +48,7 @@
     8.4  static void RPI_MoveCursor(SDL_Cursor * cursor);
     8.5  static void RPI_FreeCursor(SDL_Cursor * cursor);
     8.6  static void RPI_WarpMouse(SDL_Window * window, int x, int y);
     8.7 +static void RPI_WarpMouseGlobal(int x, int y);
     8.8  
     8.9  static SDL_Cursor *
    8.10  RPI_CreateDefaultCursor(void)
    8.11 @@ -211,6 +212,13 @@
    8.12  static void
    8.13  RPI_WarpMouse(SDL_Window * window, int x, int y)
    8.14  {
    8.15 +    RPI_WarpMouseGlobal(x, y);
    8.16 +}
    8.17 +
    8.18 +/* Warp the mouse to (x,y) */
    8.19 +static void
    8.20 +RPI_WarpMouseGlobal(int x, int y)
    8.21 +{
    8.22      RPI_CursorData *curdata;
    8.23      DISPMANX_UPDATE_HANDLE_T update;
    8.24      int ret;
    8.25 @@ -254,6 +262,7 @@
    8.26      mouse->MoveCursor = RPI_MoveCursor;
    8.27      mouse->FreeCursor = RPI_FreeCursor;
    8.28      mouse->WarpMouse = RPI_WarpMouse;
    8.29 +    mouse->WarpMouseGlobal = RPI_WarpMouseGlobal;
    8.30  
    8.31      SDL_SetDefaultCursor(RPI_CreateDefaultCursor());
    8.32  }
     9.1 --- a/src/video/wayland/SDL_waylandmouse.c	Wed Jun 04 10:52:34 2014 -0700
     9.2 +++ b/src/video/wayland/SDL_waylandmouse.c	Wed Jun 04 10:55:26 2014 -0700
     9.3 @@ -345,7 +345,12 @@
     9.4  Wayland_WarpMouse(SDL_Window *window, int x, int y)
     9.5  {
     9.6      SDL_Unsupported();
     9.7 -    return;
     9.8 +}
     9.9 +
    9.10 +static void
    9.11 +Wayland_WarpMouseGlobal(int x, int y)
    9.12 +{
    9.13 +    SDL_Unsupported();
    9.14  }
    9.15  
    9.16  static int
    9.17 @@ -365,6 +370,7 @@
    9.18      mouse->ShowCursor = Wayland_ShowCursor;
    9.19      mouse->FreeCursor = Wayland_FreeCursor;
    9.20      mouse->WarpMouse = Wayland_WarpMouse;
    9.21 +    mouse->WarpMouseGlobal = Wayland_WarpMouseGlobal;
    9.22      mouse->SetRelativeMouseMode = Wayland_SetRelativeMouseMode;
    9.23  
    9.24      SDL_SetDefaultCursor(Wayland_CreateDefaultCursor());
    10.1 --- a/src/video/windows/SDL_windowsmouse.c	Wed Jun 04 10:52:34 2014 -0700
    10.2 +++ b/src/video/windows/SDL_windowsmouse.c	Wed Jun 04 10:55:26 2014 -0700
    10.3 @@ -198,6 +198,16 @@
    10.4      SetCursorPos(pt.x, pt.y);
    10.5  }
    10.6  
    10.7 +static void
    10.8 +WIN_WarpMouseGlobal(int x, int y)
    10.9 +{
   10.10 +    POINT pt;
   10.11 +
   10.12 +    pt.x = x;
   10.13 +    pt.y = y;
   10.14 +    SetCursorPos(pt.x, pt.y);
   10.15 +}
   10.16 +
   10.17  static int
   10.18  WIN_SetRelativeMouseMode(SDL_bool enabled)
   10.19  {
   10.20 @@ -229,6 +239,7 @@
   10.21      mouse->ShowCursor = WIN_ShowCursor;
   10.22      mouse->FreeCursor = WIN_FreeCursor;
   10.23      mouse->WarpMouse = WIN_WarpMouse;
   10.24 +    mouse->WarpMouseGlobal = WIN_WarpMouseGlobal;
   10.25      mouse->SetRelativeMouseMode = WIN_SetRelativeMouseMode;
   10.26  
   10.27      SDL_SetDefaultCursor(WIN_CreateDefaultCursor());
    11.1 --- a/src/video/x11/SDL_x11mouse.c	Wed Jun 04 10:52:34 2014 -0700
    11.2 +++ b/src/video/x11/SDL_x11mouse.c	Wed Jun 04 10:55:26 2014 -0700
    11.3 @@ -318,6 +318,15 @@
    11.4      X11_XSync(display, False);
    11.5  }
    11.6  
    11.7 +static void
    11.8 +X11_WarpMouseGlobal(int x, int y)
    11.9 +{
   11.10 +    Display *display = GetDisplay();
   11.11 +
   11.12 +    X11_XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, x, y);
   11.13 +    X11_XSync(display, False);
   11.14 +}
   11.15 +
   11.16  static int
   11.17  X11_SetRelativeMouseMode(SDL_bool enabled)
   11.18  {
   11.19 @@ -340,6 +349,7 @@
   11.20      mouse->ShowCursor = X11_ShowCursor;
   11.21      mouse->FreeCursor = X11_FreeCursor;
   11.22      mouse->WarpMouse = X11_WarpMouse;
   11.23 +    mouse->WarpMouseGlobal = X11_WarpMouseGlobal;
   11.24      mouse->SetRelativeMouseMode = X11_SetRelativeMouseMode;
   11.25  
   11.26      SDL_SetDefaultCursor(X11_CreateDefaultCursor());