SDL: SDL_cocoamouse.m: SetRelativeMouseMode even if out of focus

From e2d268a399cd80dfd7c192b64b2540924cde21ba Mon Sep 17 00:00:00 2001
From: FriendlyAI <[EMAIL REDACTED]>
Date: Thu, 6 Jan 2022 23:42:44 -0500
Subject: [PATCH] SDL_cocoamouse.m: SetRelativeMouseMode even if out of focus
 Should fix #3087

---
 src/video/cocoa/SDL_cocoamouse.m | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m
index 7713fb544e6..30cf84a8cb6 100644
--- a/src/video/cocoa/SDL_cocoamouse.m
+++ b/src/video/cocoa/SDL_cocoamouse.m
@@ -259,7 +259,16 @@ + (NSCursor *)invisibleCursor
 static int
 Cocoa_SetRelativeMouseMode(SDL_bool enabled)
 {
-    /* We will re-apply the relative mode when the window gets focus, if it
+    CGError result;
+    if (enabled) {
+        DLog("Turning on.");
+        result = CGAssociateMouseAndMouseCursorPosition(NO);
+        if (result != kCGErrorSuccess) {
+            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.
      */
     SDL_Window *window = SDL_GetKeyboardFocus();
@@ -267,7 +276,7 @@ + (NSCursor *)invisibleCursor
         return 0;
     }
 
-    /* We will re-apply the relative mode when the window finishes being moved,
+    /* We will re-apply the non-relative mode when the window finishes being moved,
      * if it is being moved right now.
      */
     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
@@ -275,17 +284,14 @@ + (NSCursor *)invisibleCursor
         return 0;
     }
 
-    CGError result;
-    if (enabled) {
-        DLog("Turning on.");
-        result = CGAssociateMouseAndMouseCursorPosition(NO);
-    } else {
+    if (!enabled) {
         DLog("Turning off.");
         result = CGAssociateMouseAndMouseCursorPosition(YES);
+        if (result != kCGErrorSuccess) {
+            return SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
+        }
     }
-    if (result != kCGErrorSuccess) {
-        return SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
-    }
+
 
     /* The hide/unhide calls are redundant most of the time, but they fix
      * https://bugzilla.libsdl.org/show_bug.cgi?id=2550