Mac: Fix relative mode message after gaining focus.
authorJørgen P. Tjernø
Thu, 25 Apr 2013 18:40:29 -0700
changeset 711402b2fe147478
parent 7113 7b4b596b3cfb
child 7115 38893755cab7
Mac: Fix relative mode message after gaining focus.

This fixes a bug where relative mode would give a large jump if the
cursor was moved when the app doesn't have focus.
src/events/SDL_mouse_c.h
src/video/cocoa/SDL_cocoamouse.h
src/video/cocoa/SDL_cocoamouse.m
     1.1 --- a/src/events/SDL_mouse_c.h	Thu Apr 25 18:40:22 2013 -0700
     1.2 +++ b/src/events/SDL_mouse_c.h	Thu Apr 25 18:40:29 2013 -0700
     1.3 @@ -73,6 +73,9 @@
     1.4      SDL_Cursor *def_cursor;
     1.5      SDL_Cursor *cur_cursor;
     1.6      SDL_bool cursor_shown;
     1.7 +
     1.8 +    /* Driver-dependent data. */
     1.9 +    void *driverdata;
    1.10  } SDL_Mouse;
    1.11  
    1.12  
     2.1 --- a/src/video/cocoa/SDL_cocoamouse.h	Thu Apr 25 18:40:22 2013 -0700
     2.2 +++ b/src/video/cocoa/SDL_cocoamouse.h	Thu Apr 25 18:40:29 2013 -0700
     2.3 @@ -23,11 +23,18 @@
     2.4  #ifndef _SDL_cocoamouse_h
     2.5  #define _SDL_cocoamouse_h
     2.6  
     2.7 +#include "SDL_cocoavideo.h"
     2.8 +
     2.9  extern void Cocoa_InitMouse(_THIS);
    2.10  extern void Cocoa_HandleMouseEvent(_THIS, NSEvent * event);
    2.11  extern void Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent * event);
    2.12  extern void Cocoa_QuitMouse(_THIS);
    2.13  
    2.14 +typedef struct {
    2.15 +    int deltaXOffset;
    2.16 +    int deltaYOffset;
    2.17 +} SDL_MouseData;
    2.18 +
    2.19  #endif /* _SDL_cocoamouse_h */
    2.20  
    2.21  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Thu Apr 25 18:40:22 2013 -0700
     3.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Thu Apr 25 18:40:29 2013 -0700
     3.3 @@ -24,7 +24,7 @@
     3.4  
     3.5  #include "SDL_assert.h"
     3.6  #include "SDL_events.h"
     3.7 -#include "SDL_cocoavideo.h"
     3.8 +#include "SDL_cocoamouse.h"
     3.9  
    3.10  #include "../../events/SDL_mouse_c.h"
    3.11  
    3.12 @@ -193,6 +193,16 @@
    3.13      point.x = (float)window->x + x;
    3.14      point.y = (float)window->y + y;
    3.15  
    3.16 +    {
    3.17 +        /* This makes Cocoa_HandleMouseEvent ignore this delta in the next
    3.18 +         * movement event.
    3.19 +         */
    3.20 +        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
    3.21 +        NSPoint location =  [NSEvent mouseLocation];
    3.22 +        driverdata->deltaXOffset = location.x - point.x;
    3.23 +        driverdata->deltaYOffset = point.y - location.y;
    3.24 +    }
    3.25 +
    3.26      /* According to the docs, this was deprecated in 10.6, but it's still
    3.27       * around. The substitute requires a CGEventSource, but I'm not entirely
    3.28       * sure how we'd procure the right one for this event.
    3.29 @@ -228,6 +238,8 @@
    3.30  {
    3.31      SDL_Mouse *mouse = SDL_GetMouse();
    3.32  
    3.33 +    mouse->driverdata = SDL_calloc(1, sizeof(SDL_MouseData));
    3.34 +
    3.35      mouse->CreateCursor = Cocoa_CreateCursor;
    3.36      mouse->CreateSystemCursor = Cocoa_CreateSystemCursor;
    3.37      mouse->ShowCursor = Cocoa_ShowCursor;
    3.38 @@ -248,8 +260,11 @@
    3.39           [event type] == NSLeftMouseDragged ||
    3.40           [event type] == NSRightMouseDragged ||
    3.41           [event type] == NSOtherMouseDragged)) {
    3.42 -        float x = [event deltaX];
    3.43 -        float y = [event deltaY];
    3.44 +        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
    3.45 +        float x = [event deltaX] + driverdata->deltaXOffset;
    3.46 +        float y = [event deltaY] + driverdata->deltaYOffset;
    3.47 +        driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
    3.48 +
    3.49          SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
    3.50      }
    3.51  }
    3.52 @@ -278,6 +293,10 @@
    3.53  void
    3.54  Cocoa_QuitMouse(_THIS)
    3.55  {
    3.56 +    SDL_Mouse *mouse = SDL_GetMouse();
    3.57 +    if (mouse) {
    3.58 +        SDL_free(mouse->driverdata);
    3.59 +    }
    3.60  }
    3.61  
    3.62  #endif /* SDL_VIDEO_DRIVER_COCOA */