Cleaned up the mouse window focus handling: you always pass in the relative window when sending a mouse event.
authorSam Lantinga <slouken@libsdl.org>
Mon, 05 Jul 2010 22:48:13 -0700
changeset 4484 9322f7db8603
parent 4483 539f3eca8798
child 4485 82d661ea0d5a
Cleaned up the mouse window focus handling: you always pass in the relative window when sending a mouse event. Fixed a bug where only mouse wheel up was sent on Mac OS X Fixed a bug where mouse window focus was getting hosed by the fullscreen mouse code on Mac OS X
src/events/SDL_mouse.c
src/events/SDL_mouse_c.h
src/video/cocoa/SDL_cocoamouse.m
src/video/cocoa/SDL_cocoawindow.m
src/video/win32/SDL_win32events.c
src/video/x11/SDL_x11events.c
--- a/src/events/SDL_mouse.c	Mon Jul 05 22:45:45 2010 -0700
+++ b/src/events/SDL_mouse.c	Mon Jul 05 22:48:13 2010 -0700
@@ -112,7 +112,7 @@
 }
 
 int
-SDL_SendMouseMotion(int relative, int x, int y)
+SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y)
 {
     SDL_Mouse *mouse = &SDL_mouse;
     int posted;
@@ -120,6 +120,10 @@
     int yrel;
     int x_max = 0, y_max = 0;
 
+    if (window) {
+        SDL_SetMouseFocus(window);
+    }
+
     /* the relative motion is calculated regarding the system cursor last position */
     if (relative) {
         xrel = x;
@@ -194,12 +198,16 @@
 }
 
 int
-SDL_SendMouseButton(Uint8 state, Uint8 button)
+SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button)
 {
     SDL_Mouse *mouse = &SDL_mouse;
     int posted;
     Uint32 type;
 
+    if (window) {
+        SDL_SetMouseFocus(window);
+    }
+
     /* Figure out which event to perform */
     switch (state) {
     case SDL_PRESSED:
@@ -239,11 +247,15 @@
 }
 
 int
-SDL_SendMouseWheel(int x, int y)
+SDL_SendMouseWheel(SDL_Window * window, int x, int y)
 {
     SDL_Mouse *mouse = &SDL_mouse;
     int posted;
 
+    if (window) {
+        SDL_SetMouseFocus(window);
+    }
+
     if (!x && !y) {
         return 0;
     }
@@ -304,8 +316,7 @@
     if (mouse->WarpMouse) {
         mouse->WarpMouse(mouse, window, x, y);
     } else {
-        SDL_SetMouseFocus(window);
-        SDL_SendMouseMotion(0, x, y);
+        SDL_SendMouseMotion(window, 0, x, y);
     }
 }
 
--- a/src/events/SDL_mouse_c.h	Mon Jul 05 22:45:45 2010 -0700
+++ b/src/events/SDL_mouse_c.h	Mon Jul 05 22:48:13 2010 -0700
@@ -40,13 +40,13 @@
 extern void SDL_SetMouseFocus(SDL_Window * window);
 
 /* Send a mouse motion event */
-extern int SDL_SendMouseMotion(int relative, int x, int y);
+extern int SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y);
 
 /* Send a mouse button event */
-extern int SDL_SendMouseButton(Uint8 state, Uint8 button);
+extern int SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button);
 
 /* Send a mouse wheel event */
-extern int SDL_SendMouseWheel(int x, int y);
+extern int SDL_SendMouseWheel(SDL_Window * window, int x, int y);
 
 /* Shutdown the mouse subsystem */
 extern void SDL_MouseQuit(void);
--- a/src/video/cocoa/SDL_cocoamouse.m	Mon Jul 05 22:45:45 2010 -0700
+++ b/src/video/cocoa/SDL_cocoamouse.m	Mon Jul 05 22:48:13 2010 -0700
@@ -52,6 +52,7 @@
     int i;
     NSPoint point = { 0, 0 };
     SDL_Window *window;
+    SDL_Window *focus = SDL_GetMouseFocus();
 
     /* See if there are any fullscreen windows that might handle this event */
     window = NULL;
@@ -66,19 +67,18 @@
             point = [NSEvent mouseLocation];
             point.x = point.x - bounds.x;
             point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - bounds.y;
-            if ((point.x >= 0 && point.x < candidate->w) ||
+            if ((point.x >= 0 && point.x < candidate->w) &&
                 (point.y >= 0 && point.y < candidate->h)) {
                 /* This is it! */
                 window = candidate;
                 break;
+            } else if (candidate == focus) {
+                SDL_SetMouseFocus(NULL);
             }
         }
     }
 
-    /* Set the focus appropriately */
-    SDL_SetMouseFocus(window);
-
-    if (window) {
+    if (!window) {
         return;
     }
 
@@ -86,18 +86,18 @@
     case NSLeftMouseDown:
     case NSOtherMouseDown:
     case NSRightMouseDown:
-        SDL_SendMouseButton(SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
+        SDL_SendMouseButton(window, SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
         break;
     case NSLeftMouseUp:
     case NSOtherMouseUp:
     case NSRightMouseUp:
-        SDL_SendMouseButton(SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
+        SDL_SendMouseButton(window, SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
         break;
     case NSLeftMouseDragged:
     case NSRightMouseDragged:
     case NSOtherMouseDragged: /* usually middle mouse dragged */
     case NSMouseMoved:
-        SDL_SendMouseMotion(0, (int)point.x, (int)point.y);
+        SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
         break;
     default: /* just to avoid compiler warnings */
         break;
--- a/src/video/cocoa/SDL_cocoawindow.m	Mon Jul 05 22:45:45 2010 -0700
+++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Jul 05 22:48:13 2010 -0700
@@ -171,7 +171,7 @@
         button = [theEvent buttonNumber];
         break;
     }
-    SDL_SendMouseButton(SDL_PRESSED, button);
+    SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
 }
 
 - (void)rightMouseDown:(NSEvent *)theEvent
@@ -202,7 +202,7 @@
         button = [theEvent buttonNumber];
         break;
     }
-    SDL_SendMouseButton(SDL_RELEASED, button);
+    SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
 }
 
 - (void)rightMouseUp:(NSEvent *)theEvent
@@ -228,8 +228,7 @@
             SDL_SetMouseFocus(NULL);
         }
     } else {
-        SDL_SetMouseFocus(_data->window);
-        SDL_SendMouseMotion(0, (int)point.x, (int)point.y);
+        SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
     }
 }
 
@@ -250,7 +249,20 @@
 
 - (void)scrollWheel:(NSEvent *)theEvent
 {
-    SDL_SendMouseWheel((int)([theEvent deltaX]+0.9f), (int)([theEvent deltaY]+0.9f));
+    float x = [theEvent deltaX];
+    float y = [theEvent deltaY];
+
+    if (x > 0) {
+        x += 0.9f;
+    } else if (x < 0) {
+        x -= 0.9f;
+    }
+    if (y > 0) {
+        y += 0.9f;
+    } else if (y < 0) {
+        y -= 0.9f;
+    }
+    SDL_SendMouseWheel(_data->window, (int)x, (int)y);
 }
 
 @end
--- a/src/video/win32/SDL_win32events.c	Mon Jul 05 22:45:45 2010 -0700
+++ b/src/video/win32/SDL_win32events.c	Mon Jul 05 22:48:13 2010 -0700
@@ -177,18 +177,15 @@
         break;
 
 	case WM_MOUSEMOVE:
-        SDL_SetMouseFocus(data->window);
-        SDL_SendMouseMotion(0, LOWORD(lParam), HIWORD(lParam));
+        SDL_SendMouseMotion(data->window, 0, LOWORD(lParam), HIWORD(lParam));
         break;
 
     case WM_LBUTTONDOWN:
-        SDL_SetMouseFocus(data->window);
-        SDL_SendMouseButton(SDL_PRESSED, SDL_BUTTON_LEFT);
+        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_LEFT);
         break;
 
     case WM_LBUTTONUP:
-        SDL_SetMouseFocus(data->window);
-        SDL_SendMouseButton(SDL_RELEASED, SDL_BUTTON_LEFT);
+        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_LEFT);
         break;
 
     case WM_MOUSELEAVE:
--- a/src/video/x11/SDL_x11events.c	Mon Jul 05 22:45:45 2010 -0700
+++ b/src/video/x11/SDL_x11events.c	Mon Jul 05 22:48:13 2010 -0700
@@ -272,17 +272,17 @@
 #ifdef DEBUG_MOTION
             printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
 #endif
-            SDL_SendMouseMotion(0, xevent.xmotion.x, xevent.xmotion.y);
+            SDL_SendMouseMotion(data->window, 0, xevent.xmotion.x, xevent.xmotion.y);
         }
         break;
 
     case ButtonPress:{
-            SDL_SendMouseButton(SDL_PRESSED, xevent.xbutton.button);
+            SDL_SendMouseButton(data->window, SDL_PRESSED, xevent.xbutton.button);
         }
         break;
 
     case ButtonRelease:{
-            SDL_SendMouseButton(SDL_RELEASED, xevent.xbutton.button);
+            SDL_SendMouseButton(data->window, SDL_RELEASED, xevent.xbutton.button);
         }
         break;