Commit 11ae43ca167476070225d493a642fe6e5bbb96da

Sam Lantinga 2021-11-11T07:49:38

Only lock the pointer for mouse relative mode, there isn't really a concept of grab and confinement on iOS Locking the pointer prevents the on-screen cursor from moving, which isn't what we want with a grab behavior. Fixes https://github.com/libsdl-org/SDL/issues/4941

diff --git a/src/video/uikit/SDL_uikitevents.m b/src/video/uikit/SDL_uikitevents.m
index 4169c9b..e5615fa 100644
--- a/src/video/uikit/SDL_uikitevents.m
+++ b/src/video/uikit/SDL_uikitevents.m
@@ -24,9 +24,10 @@
 
 #include "../../events/SDL_events_c.h"
 
-#include "SDL_uikitvideo.h"
 #include "SDL_uikitevents.h"
 #include "SDL_uikitopengles.h"
+#include "SDL_uikitvideo.h"
+#include "SDL_uikitwindow.h"
 
 #import <Foundation/Foundation.h>
 
@@ -184,20 +185,20 @@ static id mouse_connect_observer = nil;
 static id mouse_disconnect_observer = nil;
 static bool mouse_relative_mode = SDL_FALSE;
 
-static void UpdateMouseGrab()
+static void UpdatePointerLock()
 {
     SDL_VideoDevice *_this = SDL_GetVideoDevice();
     SDL_Window *window;
 
     for (window = _this->windows; window != NULL; window = window->next) {
-        SDL_UpdateWindowGrab(window);
+        UIKit_UpdatePointerLock(_this, window);
     }
 }
 
 static int SetGCMouseRelativeMode(SDL_bool enabled)
 {
     mouse_relative_mode = enabled;
-    UpdateMouseGrab();
+    UpdatePointerLock();
     return 0;
 }
 
@@ -245,7 +246,7 @@ static void OnGCMouseConnected(GCMouse *mouse) API_AVAILABLE(macos(11.0), ios(14
 
     ++mice_connected;
 
-    UpdateMouseGrab();
+    UpdatePointerLock();
 }
 
 static void OnGCMouseDisconnected(GCMouse *mouse) API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0))
@@ -262,7 +263,7 @@ static void OnGCMouseDisconnected(GCMouse *mouse) API_AVAILABLE(macos(11.0), ios
         button.pressedChangedHandler = nil;
     }
 
-    UpdateMouseGrab();
+    UpdatePointerLock();
 }
 
 void SDL_InitGCMouse(void)
diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m
index 0bc5676..bd54ad6 100644
--- a/src/video/uikit/SDL_uikitview.m
+++ b/src/video/uikit/SDL_uikitview.m
@@ -160,7 +160,7 @@ extern int SDL_AppleTVRemoteOpenedAsJoystick;
 
 #if !TARGET_OS_TV && defined(__IPHONE_13_4)
 - (UIPointerRegion *)pointerInteraction:(UIPointerInteraction *)interaction regionForRequest:(UIPointerRegionRequest *)request defaultRegion:(UIPointerRegion *)defaultRegion API_AVAILABLE(ios(13.4)){
-    if (request != nil && (!SDL_HasGCMouse() || !SDL_GCMouseRelativeMode())) {
+    if (request != nil && !SDL_GCMouseRelativeMode()) {
         CGPoint origin = self.bounds.origin;
         CGPoint point = request.location;
 
diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m
index c51d1ae..b2db403 100644
--- a/src/video/uikit/SDL_uikitviewcontroller.m
+++ b/src/video/uikit/SDL_uikitviewcontroller.m
@@ -250,14 +250,7 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
 
 - (BOOL)prefersPointerLocked
 {
-    SDL_VideoDevice *_this = SDL_GetVideoDevice();
-    
-    if (SDL_HasGCMouse() &&
-        (SDL_GCMouseRelativeMode() || _this->grabbed_window == window)) {
-        return YES;
-    } else {
-        return NO;
-    }
+    return SDL_GCMouseRelativeMode() ? YES : NO;
 }
 
 #endif /* !TARGET_OS_TV */
diff --git a/src/video/uikit/SDL_uikitwindow.h b/src/video/uikit/SDL_uikitwindow.h
index 23beea7..6f86a5d 100644
--- a/src/video/uikit/SDL_uikitwindow.h
+++ b/src/video/uikit/SDL_uikitwindow.h
@@ -34,6 +34,7 @@ extern void UIKit_RaiseWindow(_THIS, SDL_Window * window);
 extern void UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered);
 extern void UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
 extern void UIKit_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
+extern void UIKit_UpdatePointerLock(_THIS, SDL_Window * window);
 extern void UIKit_DestroyWindow(_THIS, SDL_Window * window);
 extern SDL_bool UIKit_GetWindowWMInfo(_THIS, SDL_Window * window,
                                       struct SDL_SysWMinfo * info);
diff --git a/src/video/uikit/SDL_uikitwindow.m b/src/video/uikit/SDL_uikitwindow.m
index c71314b..d2713ed 100644
--- a/src/video/uikit/SDL_uikitwindow.m
+++ b/src/video/uikit/SDL_uikitwindow.m
@@ -323,6 +323,12 @@ UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display
 void
 UIKit_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
 {
+    /* There really isn't a concept of window grab or cursor confinement on iOS */
+}
+
+void
+UIKit_UpdatePointerLock(_THIS, SDL_Window * window)
+{
 #if !TARGET_OS_TV
 #if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
     @autoreleasepool {