From 74504e0965b8e71330599efed3ea237e23e871c1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 8 Aug 2024 13:24:43 -0700
Subject: [PATCH] cocoa: removed relative mode handling on focus change
This is now being done at a higher level, so we don't want to duplicate it here.
Fixes the mouse cursor staying hidden if you enable relative mode, alt-tab away and then alt-tab back.
---
src/video/cocoa/SDL_cocoamouse.m | 27 ++++++++++-----------------
src/video/cocoa/SDL_cocoawindow.m | 12 +-----------
2 files changed, 11 insertions(+), 28 deletions(-)
diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m
index acb733f670825..647c469c24bc9 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 cbd01c90822c0..a457a5a53f200 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -1166,18 +1166,13 @@ - (void)windowDidDeminiaturize:(NSNotification *)aNotification
- (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 @@ - (void)windowDidBecomeKey:(NSNotification *)aNotification
- (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);