Fixed bug 2655 - OSX: Window position and global mouse coord spaces are different
authorSam Lantinga <slouken@libsdl.org>
Sun, 17 Aug 2014 14:57:52 -0700
changeset 9086c5e33f9a0d03
parent 9085 7cafa57c23ec
child 9087 eef2e43f60d1
Fixed bug 2655 - OSX: Window position and global mouse coord spaces are different

Tim McDaniel

On OSX, with revision 8729, the coordinate space for window position and the coordinate space for global mouse position don't match. For a non-fullscreen window, the window position is global relative to the bottom of the menubar. The global mouse position is relative to the top of the screen. This affects Cocoa_WarpMouse and potentially other things as well. Further, the coordinate system for window position is now affected by what screen it is on. For example, if I have two equal size screens oriented side by side such that the tops of the screens are equal in global space, with the menubar on one screen, and a window straddles the two screens, the window's y position makes no sense. The window's y position depends on what screen "most" of the window is on. So if I move the window horizontally just a bit, the y position of my window is now different by the size of the menubar, even though the window was not moved vertically.

I'd like to reiterate that this was a fairly fundamental change (and a breaking change for us). If SDL OSX is to really support multi-display configurations, this is especially problematic.

If the real concern is preventing windows from going under the menubar, then perhaps a solution involving something like overriding [NSWindow constrainFrameRect] would be less problematic than redefining the global window coord space for the main display.
src/video/cocoa/SDL_cocoamouse.m
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Sun Aug 17 14:44:53 2014 -0700
     1.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Sun Aug 17 14:57:52 2014 -0700
     1.3 @@ -208,41 +208,17 @@
     1.4  }
     1.5  
     1.6  static void
     1.7 -Cocoa_WarpMouse(SDL_Window * window, int x, int y)
     1.8 -{
     1.9 -    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    1.10 -    if ([data->listener isMoving]) {
    1.11 -        DLog("Postponing warp, window being moved.");
    1.12 -        [data->listener setPendingMoveX:x
    1.13 -                                      Y:y];
    1.14 -        return;
    1.15 -    }
    1.16 -
    1.17 -    SDL_Mouse *mouse = SDL_GetMouse();
    1.18 -    CGPoint point = CGPointMake(x + (float)window->x, y + (float)window->y);
    1.19 -
    1.20 -    Cocoa_HandleMouseWarp(point.x, point.y);
    1.21 -
    1.22 -    /* According to the docs, this was deprecated in 10.6, but it's still
    1.23 -     * around. The substitute requires a CGEventSource, but I'm not entirely
    1.24 -     * sure how we'd procure the right one for this event.
    1.25 -     */
    1.26 -    CGSetLocalEventsSuppressionInterval(0.0);
    1.27 -    CGWarpMouseCursorPosition(point);
    1.28 -    CGSetLocalEventsSuppressionInterval(0.25);
    1.29 -
    1.30 -    if (!mouse->relative_mode) {
    1.31 -        /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
    1.32 -         * other implementations' APIs.
    1.33 -         */
    1.34 -        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y);
    1.35 -    }
    1.36 -}
    1.37 -
    1.38 -static void
    1.39  Cocoa_WarpMouseGlobal(int x, int y)
    1.40  {
    1.41      SDL_Mouse *mouse = SDL_GetMouse();
    1.42 +    if (mouse->focus) {
    1.43 +        SDL_WindowData *data = (SDL_WindowData *) mouse->focus->driverdata;
    1.44 +        if ([data->listener isMoving]) {
    1.45 +            DLog("Postponing warp, window being moved.");
    1.46 +            [data->listener setPendingMoveX:x Y:y];
    1.47 +            return;
    1.48 +        }
    1.49 +    }
    1.50      CGPoint point = CGPointMake((float)x, (float)y);
    1.51  
    1.52      Cocoa_HandleMouseWarp(point.x, point.y);
    1.53 @@ -263,6 +239,12 @@
    1.54      }
    1.55  }
    1.56  
    1.57 +static void
    1.58 +Cocoa_WarpMouse(SDL_Window * window, int x, int y)
    1.59 +{
    1.60 +    Cocoa_WarpMouseGlobal(x + window->x, y + window->y);
    1.61 +}
    1.62 +
    1.63  static int
    1.64  Cocoa_SetRelativeMouseMode(SDL_bool enabled)
    1.65  {
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Sun Aug 17 14:44:53 2014 -0700
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Sun Aug 17 14:57:52 2014 -0700
     2.3 @@ -103,8 +103,7 @@
     2.4  
     2.5  static void ConvertNSRect(NSScreen *screen, BOOL fullscreen, NSRect *r)
     2.6  {
     2.7 -    NSRect visibleScreen = fullscreen ? [screen frame] : [screen visibleFrame];
     2.8 -    r->origin.y = (visibleScreen.origin.y + visibleScreen.size.height) - r->origin.y - r->size.height;
     2.9 +    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
    2.10  }
    2.11  
    2.12  static void
    2.13 @@ -389,11 +388,11 @@
    2.14          isMoving = NO;
    2.15  
    2.16          SDL_Mouse *mouse = SDL_GetMouse();
    2.17 -        if (pendingWindowWarpX >= 0 && pendingWindowWarpY >= 0) {
    2.18 -            mouse->WarpMouse(_data->window, pendingWindowWarpX, pendingWindowWarpY);
    2.19 -            pendingWindowWarpX = pendingWindowWarpY = -1;
    2.20 +        if (pendingWindowWarpX != INT_MAX && pendingWindowWarpY != INT_MAX) {
    2.21 +            mouse->WarpMouseGlobal(pendingWindowWarpX, pendingWindowWarpY);
    2.22 +            pendingWindowWarpX = pendingWindowWarpY = INT_MAX;
    2.23          }
    2.24 -        if (mouse->relative_mode && SDL_GetMouseFocus() == _data->window) {
    2.25 +        if (mouse->relative_mode && !mouse->relative_mode_warp && mouse->focus == _data->window) {
    2.26              mouse->SetRelativeMouseMode(SDL_TRUE);
    2.27          }
    2.28      }
    2.29 @@ -413,7 +412,7 @@
    2.30  - (void)windowWillMove:(NSNotification *)aNotification
    2.31  {
    2.32      if ([_data->nswindow isKindOfClass:[SDLWindow class]]) {
    2.33 -        pendingWindowWarpX = pendingWindowWarpY = -1;
    2.34 +        pendingWindowWarpX = pendingWindowWarpY = INT_MAX;
    2.35          isMoving = YES;
    2.36      }
    2.37  }
    2.38 @@ -500,7 +499,7 @@
    2.39  {
    2.40      SDL_Window *window = _data->window;
    2.41      SDL_Mouse *mouse = SDL_GetMouse();
    2.42 -    if (mouse->relative_mode && ![self isMoving]) {
    2.43 +    if (mouse->relative_mode && !mouse->relative_mode_warp && ![self isMoving]) {
    2.44          mouse->SetRelativeMouseMode(SDL_TRUE);
    2.45      }
    2.46  
    2.47 @@ -532,7 +531,7 @@
    2.48  - (void)windowDidResignKey:(NSNotification *)aNotification
    2.49  {
    2.50      SDL_Mouse *mouse = SDL_GetMouse();
    2.51 -    if (mouse->relative_mode) {
    2.52 +    if (mouse->relative_mode && !mouse->relative_mode_warp) {
    2.53          mouse->SetRelativeMouseMode(SDL_FALSE);
    2.54      }
    2.55