SDL: Only pass UIPress from game controllers and remotes if the're not open

From f844f3e10b8fd21dd09460321c9d7fe1a99bd023 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 14 Oct 2025 12:40:53 -0700
Subject: [PATCH] Only pass UIPress from game controllers and remotes if the're
 not open

Fixes https://github.com/libsdl-org/SDL/issues/14080
---
 src/video/uikit/SDL_uikitview.m | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m
index da5b6d75d70e6..e3e1dbce32e85 100644
--- a/src/video/uikit/SDL_uikitview.m
+++ b/src/video/uikit/SDL_uikitview.m
@@ -27,6 +27,7 @@
 #include "../../events/SDL_mouse_c.h"
 #include "../../events/SDL_touch_c.h"
 #include "../../events/SDL_events_c.h"
+#include "../../joystick/SDL_joystick_c.h"
 
 #include "SDL_uikitappdelegate.h"
 #include "SDL_uikitevents.h"
@@ -38,9 +39,7 @@
 #define MAX_MOUSE_BUTTONS 5
 
 // This is defined in SDL_sysjoystick.m
-#ifndef SDL_JOYSTICK_DISABLED
 extern int SDL_AppleTVRemoteOpenedAsJoystick;
-#endif
 
 @implementation SDL_uikitview
 {
@@ -519,9 +518,18 @@ - (SDL_Scancode)scancodeFromPress:(UIPress *)press
         return (SDL_Scancode)press.key.keyCode;
     }
 
-#ifndef SDL_JOYSTICK_DISABLED
-    // Presses from Apple TV remote
-    if (!SDL_AppleTVRemoteOpenedAsJoystick) {
+    // Presses from Apple TV remote or game controller
+    bool controller_opened = false;
+#ifdef SDL_PLATFORM_TVOS
+    // tvOS doesn't send these for game controllers, but does for the Siri remote
+    controller_opened = (SDL_AppleTVRemoteOpenedAsJoystick > 0);
+#else
+    // iOS doesn't have a Siri remote, but does send these for game controllers as of iOS 26
+    // We don't currently have any way of telling what controller sent this, so assume if any
+    // controllers are opened, that the application is handling the controller as a gamepad
+    controller_opened = SDL_JoysticksOpened();
+#endif
+    if (!controller_opened) {
         switch (press.type) {
         case UIPressTypeUpArrow:
             return SDL_SCANCODE_UP;
@@ -544,7 +552,6 @@ - (SDL_Scancode)scancodeFromPress:(UIPress *)press
             break;
         }
     }
-#endif // !SDL_JOYSTICK_DISABLED
 
     return SDL_SCANCODE_UNKNOWN;
 }
@@ -601,8 +608,9 @@ - (void)swipeGesture:(UISwipeGestureRecognizer *)gesture
 {
     // Swipe gestures don't trigger begin states.
     if (gesture.state == UIGestureRecognizerStateEnded) {
-#ifndef SDL_JOYSTICK_DISABLED
-        if (!SDL_AppleTVRemoteOpenedAsJoystick) {
+        // tvOS doesn't send these for game controllers, but does for the Siri remote
+        bool controller_opened = (SDL_AppleTVRemoteOpenedAsJoystick > 0);
+        if (!controller_opened) {
             /* Send arrow key presses for now, as we don't have an external API
              * which better maps to swipe gestures. */
             switch (gesture.direction) {
@@ -620,7 +628,6 @@ - (void)swipeGesture:(UISwipeGestureRecognizer *)gesture
                 break;
             }
         }
-#endif // !SDL_JOYSTICK_DISABLED
     }
 }
 #endif // SDL_PLATFORM_TVOS