src/video/cocoa/SDL_cocoamouse.m
changeset 7114 02b2fe147478
parent 7113 7b4b596b3cfb
child 7158 ff52fba70795
--- a/src/video/cocoa/SDL_cocoamouse.m	Thu Apr 25 18:40:22 2013 -0700
+++ b/src/video/cocoa/SDL_cocoamouse.m	Thu Apr 25 18:40:29 2013 -0700
@@ -24,7 +24,7 @@
 
 #include "SDL_assert.h"
 #include "SDL_events.h"
-#include "SDL_cocoavideo.h"
+#include "SDL_cocoamouse.h"
 
 #include "../../events/SDL_mouse_c.h"
 
@@ -193,6 +193,16 @@
     point.x = (float)window->x + x;
     point.y = (float)window->y + y;
 
+    {
+        /* This makes Cocoa_HandleMouseEvent ignore this delta in the next
+         * movement event.
+         */
+        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
+        NSPoint location =  [NSEvent mouseLocation];
+        driverdata->deltaXOffset = location.x - point.x;
+        driverdata->deltaYOffset = point.y - location.y;
+    }
+
     /* According to the docs, this was deprecated in 10.6, but it's still
      * around. The substitute requires a CGEventSource, but I'm not entirely
      * sure how we'd procure the right one for this event.
@@ -228,6 +238,8 @@
 {
     SDL_Mouse *mouse = SDL_GetMouse();
 
+    mouse->driverdata = SDL_calloc(1, sizeof(SDL_MouseData));
+
     mouse->CreateCursor = Cocoa_CreateCursor;
     mouse->CreateSystemCursor = Cocoa_CreateSystemCursor;
     mouse->ShowCursor = Cocoa_ShowCursor;
@@ -248,8 +260,11 @@
          [event type] == NSLeftMouseDragged ||
          [event type] == NSRightMouseDragged ||
          [event type] == NSOtherMouseDragged)) {
-        float x = [event deltaX];
-        float y = [event deltaY];
+        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
+        float x = [event deltaX] + driverdata->deltaXOffset;
+        float y = [event deltaY] + driverdata->deltaYOffset;
+        driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
+
         SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
     }
 }
@@ -278,6 +293,10 @@
 void
 Cocoa_QuitMouse(_THIS)
 {
+    SDL_Mouse *mouse = SDL_GetMouse();
+    if (mouse) {
+        SDL_free(mouse->driverdata);
+    }
 }
 
 #endif /* SDL_VIDEO_DRIVER_COCOA */