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 44849322f7db8603
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
     1.1 --- a/src/events/SDL_mouse.c	Mon Jul 05 22:45:45 2010 -0700
     1.2 +++ b/src/events/SDL_mouse.c	Mon Jul 05 22:48:13 2010 -0700
     1.3 @@ -112,7 +112,7 @@
     1.4  }
     1.5  
     1.6  int
     1.7 -SDL_SendMouseMotion(int relative, int x, int y)
     1.8 +SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y)
     1.9  {
    1.10      SDL_Mouse *mouse = &SDL_mouse;
    1.11      int posted;
    1.12 @@ -120,6 +120,10 @@
    1.13      int yrel;
    1.14      int x_max = 0, y_max = 0;
    1.15  
    1.16 +    if (window) {
    1.17 +        SDL_SetMouseFocus(window);
    1.18 +    }
    1.19 +
    1.20      /* the relative motion is calculated regarding the system cursor last position */
    1.21      if (relative) {
    1.22          xrel = x;
    1.23 @@ -194,12 +198,16 @@
    1.24  }
    1.25  
    1.26  int
    1.27 -SDL_SendMouseButton(Uint8 state, Uint8 button)
    1.28 +SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button)
    1.29  {
    1.30      SDL_Mouse *mouse = &SDL_mouse;
    1.31      int posted;
    1.32      Uint32 type;
    1.33  
    1.34 +    if (window) {
    1.35 +        SDL_SetMouseFocus(window);
    1.36 +    }
    1.37 +
    1.38      /* Figure out which event to perform */
    1.39      switch (state) {
    1.40      case SDL_PRESSED:
    1.41 @@ -239,11 +247,15 @@
    1.42  }
    1.43  
    1.44  int
    1.45 -SDL_SendMouseWheel(int x, int y)
    1.46 +SDL_SendMouseWheel(SDL_Window * window, int x, int y)
    1.47  {
    1.48      SDL_Mouse *mouse = &SDL_mouse;
    1.49      int posted;
    1.50  
    1.51 +    if (window) {
    1.52 +        SDL_SetMouseFocus(window);
    1.53 +    }
    1.54 +
    1.55      if (!x && !y) {
    1.56          return 0;
    1.57      }
    1.58 @@ -304,8 +316,7 @@
    1.59      if (mouse->WarpMouse) {
    1.60          mouse->WarpMouse(mouse, window, x, y);
    1.61      } else {
    1.62 -        SDL_SetMouseFocus(window);
    1.63 -        SDL_SendMouseMotion(0, x, y);
    1.64 +        SDL_SendMouseMotion(window, 0, x, y);
    1.65      }
    1.66  }
    1.67  
     2.1 --- a/src/events/SDL_mouse_c.h	Mon Jul 05 22:45:45 2010 -0700
     2.2 +++ b/src/events/SDL_mouse_c.h	Mon Jul 05 22:48:13 2010 -0700
     2.3 @@ -40,13 +40,13 @@
     2.4  extern void SDL_SetMouseFocus(SDL_Window * window);
     2.5  
     2.6  /* Send a mouse motion event */
     2.7 -extern int SDL_SendMouseMotion(int relative, int x, int y);
     2.8 +extern int SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y);
     2.9  
    2.10  /* Send a mouse button event */
    2.11 -extern int SDL_SendMouseButton(Uint8 state, Uint8 button);
    2.12 +extern int SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button);
    2.13  
    2.14  /* Send a mouse wheel event */
    2.15 -extern int SDL_SendMouseWheel(int x, int y);
    2.16 +extern int SDL_SendMouseWheel(SDL_Window * window, int x, int y);
    2.17  
    2.18  /* Shutdown the mouse subsystem */
    2.19  extern void SDL_MouseQuit(void);
     3.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Mon Jul 05 22:45:45 2010 -0700
     3.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Mon Jul 05 22:48:13 2010 -0700
     3.3 @@ -52,6 +52,7 @@
     3.4      int i;
     3.5      NSPoint point = { 0, 0 };
     3.6      SDL_Window *window;
     3.7 +    SDL_Window *focus = SDL_GetMouseFocus();
     3.8  
     3.9      /* See if there are any fullscreen windows that might handle this event */
    3.10      window = NULL;
    3.11 @@ -66,19 +67,18 @@
    3.12              point = [NSEvent mouseLocation];
    3.13              point.x = point.x - bounds.x;
    3.14              point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - bounds.y;
    3.15 -            if ((point.x >= 0 && point.x < candidate->w) ||
    3.16 +            if ((point.x >= 0 && point.x < candidate->w) &&
    3.17                  (point.y >= 0 && point.y < candidate->h)) {
    3.18                  /* This is it! */
    3.19                  window = candidate;
    3.20                  break;
    3.21 +            } else if (candidate == focus) {
    3.22 +                SDL_SetMouseFocus(NULL);
    3.23              }
    3.24          }
    3.25      }
    3.26  
    3.27 -    /* Set the focus appropriately */
    3.28 -    SDL_SetMouseFocus(window);
    3.29 -
    3.30 -    if (window) {
    3.31 +    if (!window) {
    3.32          return;
    3.33      }
    3.34  
    3.35 @@ -86,18 +86,18 @@
    3.36      case NSLeftMouseDown:
    3.37      case NSOtherMouseDown:
    3.38      case NSRightMouseDown:
    3.39 -        SDL_SendMouseButton(SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
    3.40 +        SDL_SendMouseButton(window, SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
    3.41          break;
    3.42      case NSLeftMouseUp:
    3.43      case NSOtherMouseUp:
    3.44      case NSRightMouseUp:
    3.45 -        SDL_SendMouseButton(SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
    3.46 +        SDL_SendMouseButton(window, SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
    3.47          break;
    3.48      case NSLeftMouseDragged:
    3.49      case NSRightMouseDragged:
    3.50      case NSOtherMouseDragged: /* usually middle mouse dragged */
    3.51      case NSMouseMoved:
    3.52 -        SDL_SendMouseMotion(0, (int)point.x, (int)point.y);
    3.53 +        SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
    3.54          break;
    3.55      default: /* just to avoid compiler warnings */
    3.56          break;
     4.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Mon Jul 05 22:45:45 2010 -0700
     4.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Jul 05 22:48:13 2010 -0700
     4.3 @@ -171,7 +171,7 @@
     4.4          button = [theEvent buttonNumber];
     4.5          break;
     4.6      }
     4.7 -    SDL_SendMouseButton(SDL_PRESSED, button);
     4.8 +    SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
     4.9  }
    4.10  
    4.11  - (void)rightMouseDown:(NSEvent *)theEvent
    4.12 @@ -202,7 +202,7 @@
    4.13          button = [theEvent buttonNumber];
    4.14          break;
    4.15      }
    4.16 -    SDL_SendMouseButton(SDL_RELEASED, button);
    4.17 +    SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
    4.18  }
    4.19  
    4.20  - (void)rightMouseUp:(NSEvent *)theEvent
    4.21 @@ -228,8 +228,7 @@
    4.22              SDL_SetMouseFocus(NULL);
    4.23          }
    4.24      } else {
    4.25 -        SDL_SetMouseFocus(_data->window);
    4.26 -        SDL_SendMouseMotion(0, (int)point.x, (int)point.y);
    4.27 +        SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
    4.28      }
    4.29  }
    4.30  
    4.31 @@ -250,7 +249,20 @@
    4.32  
    4.33  - (void)scrollWheel:(NSEvent *)theEvent
    4.34  {
    4.35 -    SDL_SendMouseWheel((int)([theEvent deltaX]+0.9f), (int)([theEvent deltaY]+0.9f));
    4.36 +    float x = [theEvent deltaX];
    4.37 +    float y = [theEvent deltaY];
    4.38 +
    4.39 +    if (x > 0) {
    4.40 +        x += 0.9f;
    4.41 +    } else if (x < 0) {
    4.42 +        x -= 0.9f;
    4.43 +    }
    4.44 +    if (y > 0) {
    4.45 +        y += 0.9f;
    4.46 +    } else if (y < 0) {
    4.47 +        y -= 0.9f;
    4.48 +    }
    4.49 +    SDL_SendMouseWheel(_data->window, (int)x, (int)y);
    4.50  }
    4.51  
    4.52  @end
     5.1 --- a/src/video/win32/SDL_win32events.c	Mon Jul 05 22:45:45 2010 -0700
     5.2 +++ b/src/video/win32/SDL_win32events.c	Mon Jul 05 22:48:13 2010 -0700
     5.3 @@ -177,18 +177,15 @@
     5.4          break;
     5.5  
     5.6  	case WM_MOUSEMOVE:
     5.7 -        SDL_SetMouseFocus(data->window);
     5.8 -        SDL_SendMouseMotion(0, LOWORD(lParam), HIWORD(lParam));
     5.9 +        SDL_SendMouseMotion(data->window, 0, LOWORD(lParam), HIWORD(lParam));
    5.10          break;
    5.11  
    5.12      case WM_LBUTTONDOWN:
    5.13 -        SDL_SetMouseFocus(data->window);
    5.14 -        SDL_SendMouseButton(SDL_PRESSED, SDL_BUTTON_LEFT);
    5.15 +        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_LEFT);
    5.16          break;
    5.17  
    5.18      case WM_LBUTTONUP:
    5.19 -        SDL_SetMouseFocus(data->window);
    5.20 -        SDL_SendMouseButton(SDL_RELEASED, SDL_BUTTON_LEFT);
    5.21 +        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_LEFT);
    5.22          break;
    5.23  
    5.24      case WM_MOUSELEAVE:
     6.1 --- a/src/video/x11/SDL_x11events.c	Mon Jul 05 22:45:45 2010 -0700
     6.2 +++ b/src/video/x11/SDL_x11events.c	Mon Jul 05 22:48:13 2010 -0700
     6.3 @@ -272,17 +272,17 @@
     6.4  #ifdef DEBUG_MOTION
     6.5              printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
     6.6  #endif
     6.7 -            SDL_SendMouseMotion(0, xevent.xmotion.x, xevent.xmotion.y);
     6.8 +            SDL_SendMouseMotion(data->window, 0, xevent.xmotion.x, xevent.xmotion.y);
     6.9          }
    6.10          break;
    6.11  
    6.12      case ButtonPress:{
    6.13 -            SDL_SendMouseButton(SDL_PRESSED, xevent.xbutton.button);
    6.14 +            SDL_SendMouseButton(data->window, SDL_PRESSED, xevent.xbutton.button);
    6.15          }
    6.16          break;
    6.17  
    6.18      case ButtonRelease:{
    6.19 -            SDL_SendMouseButton(SDL_RELEASED, xevent.xbutton.button);
    6.20 +            SDL_SendMouseButton(data->window, SDL_RELEASED, xevent.xbutton.button);
    6.21          }
    6.22          break;
    6.23