diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m index acb733f670..647c469c24 100644 --- a/src/video/cocoa/SDL_cocoamouse.m +++ b/src/video/cocoa/SDL_cocoamouse.m @@ -316,11 +316,19 @@ static int Cocoa_WarpMouse(SDL_Window *window, float x, float y) static int Cocoa_SetRelativeMouseMode(SDL_bool enabled) { - SDL_Window *window = SDL_GetKeyboardFocus(); CGError result; - SDL_CocoaWindowData *data; + if (enabled) { + SDL_Window *window = SDL_GetKeyboardFocus(); if (window) { + /* We will re-apply the relative mode when the window finishes being moved, + * if it is being moved right now. + */ + SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal; + if ([data.listener isMovingOrFocusClickPending]) { + return 0; + } + /* make sure the mouse isn't at the corner of the window, as this can confuse things if macOS thinks a window resize is happening on the first click. */ const CGPoint point = CGPointMake((float)(window->x + (window->w / 2)), (float)(window->y + (window->h / 2))); Cocoa_HandleMouseWarp(point.x, point.y); @@ -336,21 +344,6 @@ static int Cocoa_SetRelativeMouseMode(SDL_bool enabled) return SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed"); } - /* We will re-apply the non-relative mode when the window gets focus, if it - * doesn't have focus right now. - */ - if (!window) { - return 0; - } - - /* We will re-apply the non-relative mode when the window finishes being moved, - * if it is being moved right now. - */ - data = (__bridge SDL_CocoaWindowData *)window->internal; - if ([data.listener isMovingOrFocusClickPending]) { - return 0; - } - /* The hide/unhide calls are redundant most of the time, but they fix * https://bugzilla.libsdl.org/show_bug.cgi?id=2550 */ diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m index cbd01c9082..a457a5a53f 100644 --- a/src/video/cocoa/SDL_cocoawindow.m +++ b/src/video/cocoa/SDL_cocoawindow.m @@ -1166,18 +1166,13 @@ static NSCursor *Cocoa_GetDesiredCursor(void) - (void)windowDidBecomeKey:(NSNotification *)aNotification { SDL_Window *window = _data.window; - SDL_Mouse *mouse = SDL_GetMouse(); /* We're going to get keyboard events, since we're key. */ /* This needs to be done before restoring the relative mouse mode. */ Cocoa_SetKeyboardFocus(_data.keyboard_focus ? _data.keyboard_focus : window); - if (mouse->relative_mode && !mouse->relative_mode_warp && ![self isMovingOrFocusClickPending]) { - mouse->SetRelativeMouseMode(SDL_TRUE); - } - /* If we just gained focus we need the updated mouse position */ - if (!mouse->relative_mode) { + if (!(window->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE)) { NSPoint point; float x, y; @@ -1205,11 +1200,6 @@ static NSCursor *Cocoa_GetDesiredCursor(void) - (void)windowDidResignKey:(NSNotification *)aNotification { - SDL_Mouse *mouse = SDL_GetMouse(); - if (mouse->relative_mode && !mouse->relative_mode_warp) { - mouse->SetRelativeMouseMode(SDL_FALSE); - } - /* Some other window will get mouse events, since we're not key. */ if (SDL_GetMouseFocus() == _data.window) { SDL_SetMouseFocus(NULL);