Skip to content

Commit

Permalink
Fixed bug 2655 - OSX: Window position and global mouse coord spaces a…
Browse files Browse the repository at this point in the history
…re 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.
  • Loading branch information
slouken committed Aug 17, 2014
1 parent de3d381 commit 5e50180
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
46 changes: 14 additions & 32 deletions src/video/cocoa/SDL_cocoamouse.m
Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down
17 changes: 8 additions & 9 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 5e50180

Please sign in to comment.