From 5e5018041540609b9d10d596663650b0e662ba4a Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 17 Aug 2014 14:57:52 -0700 Subject: [PATCH] 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 | 46 ++++++++++--------------------- src/video/cocoa/SDL_cocoawindow.m | 17 ++++++------ 2 files changed, 22 insertions(+), 41 deletions(-) diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m index 4ea420d229d8b..f0480603d6c83 100644 --- a/src/video/cocoa/SDL_cocoamouse.m +++ b/src/video/cocoa/SDL_cocoamouse.m @@ -207,42 +207,18 @@ + (NSCursor *)invisibleCursor return 0; } -static void -Cocoa_WarpMouse(SDL_Window * window, int x, int y) -{ - SDL_WindowData *data = (SDL_WindowData *) window->driverdata; - if ([data->listener isMoving]) { - DLog("Postponing warp, window being moved."); - [data->listener setPendingMoveX:x - Y:y]; - return; - } - - SDL_Mouse *mouse = SDL_GetMouse(); - CGPoint point = CGPointMake(x + (float)window->x, y + (float)window->y); - - Cocoa_HandleMouseWarp(point.x, point.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. - */ - CGSetLocalEventsSuppressionInterval(0.0); - CGWarpMouseCursorPosition(point); - CGSetLocalEventsSuppressionInterval(0.25); - - if (!mouse->relative_mode) { - /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our - * other implementations' APIs. - */ - SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y); - } -} - static void Cocoa_WarpMouseGlobal(int x, int y) { SDL_Mouse *mouse = SDL_GetMouse(); + if (mouse->focus) { + SDL_WindowData *data = (SDL_WindowData *) mouse->focus->driverdata; + if ([data->listener isMoving]) { + DLog("Postponing warp, window being moved."); + [data->listener setPendingMoveX:x Y:y]; + return; + } + } CGPoint point = CGPointMake((float)x, (float)y); Cocoa_HandleMouseWarp(point.x, point.y); @@ -263,6 +239,12 @@ + (NSCursor *)invisibleCursor } } +static void +Cocoa_WarpMouse(SDL_Window * window, int x, int y) +{ + Cocoa_WarpMouseGlobal(x + window->x, y + window->y); +} + static int Cocoa_SetRelativeMouseMode(SDL_bool enabled) { diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m index 63d6f211dde97..29bba457c2151 100644 --- a/src/video/cocoa/SDL_cocoawindow.m +++ b/src/video/cocoa/SDL_cocoawindow.m @@ -103,8 +103,7 @@ - (void)doCommandBySelector:(SEL)aSelector static void ConvertNSRect(NSScreen *screen, BOOL fullscreen, NSRect *r) { - NSRect visibleScreen = fullscreen ? [screen frame] : [screen visibleFrame]; - r->origin.y = (visibleScreen.origin.y + visibleScreen.size.height) - r->origin.y - r->size.height; + r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height; } static void @@ -389,11 +388,11 @@ - (void)windowDidFinishMoving isMoving = NO; SDL_Mouse *mouse = SDL_GetMouse(); - if (pendingWindowWarpX >= 0 && pendingWindowWarpY >= 0) { - mouse->WarpMouse(_data->window, pendingWindowWarpX, pendingWindowWarpY); - pendingWindowWarpX = pendingWindowWarpY = -1; + if (pendingWindowWarpX != INT_MAX && pendingWindowWarpY != INT_MAX) { + mouse->WarpMouseGlobal(pendingWindowWarpX, pendingWindowWarpY); + pendingWindowWarpX = pendingWindowWarpY = INT_MAX; } - if (mouse->relative_mode && SDL_GetMouseFocus() == _data->window) { + if (mouse->relative_mode && !mouse->relative_mode_warp && mouse->focus == _data->window) { mouse->SetRelativeMouseMode(SDL_TRUE); } } @@ -413,7 +412,7 @@ - (void)windowDidExpose:(NSNotification *)aNotification - (void)windowWillMove:(NSNotification *)aNotification { if ([_data->nswindow isKindOfClass:[SDLWindow class]]) { - pendingWindowWarpX = pendingWindowWarpY = -1; + pendingWindowWarpX = pendingWindowWarpY = INT_MAX; isMoving = YES; } } @@ -500,7 +499,7 @@ - (void)windowDidBecomeKey:(NSNotification *)aNotification { SDL_Window *window = _data->window; SDL_Mouse *mouse = SDL_GetMouse(); - if (mouse->relative_mode && ![self isMoving]) { + if (mouse->relative_mode && !mouse->relative_mode_warp && ![self isMoving]) { mouse->SetRelativeMouseMode(SDL_TRUE); } @@ -532,7 +531,7 @@ - (void)windowDidBecomeKey:(NSNotification *)aNotification - (void)windowDidResignKey:(NSNotification *)aNotification { SDL_Mouse *mouse = SDL_GetMouse(); - if (mouse->relative_mode) { + if (mouse->relative_mode && !mouse->relative_mode_warp) { mouse->SetRelativeMouseMode(SDL_FALSE); }