SDL: Wait for the GCController framework to see IOKit devices

From 645823fc901c98b688512d3b3e70cc8922e8140c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 9 Mar 2023 16:10:04 -0800
Subject: [PATCH] Wait for the GCController framework to see IOKit devices

It occasionally takes a few millseconds for the GCController framework to handle the device notification and set up the device

Fixes the duplicate controller issue in https://github.com/libsdl-org/SDL/issues/6686
---
 src/joystick/apple/SDL_mfijoystick.m | 29 ++++++----------------------
 1 file changed, 6 insertions(+), 23 deletions(-)

diff --git a/src/joystick/apple/SDL_mfijoystick.m b/src/joystick/apple/SDL_mfijoystick.m
index ea84dfa0478b..d6bcf803b1d6 100644
--- a/src/joystick/apple/SDL_mfijoystick.m
+++ b/src/joystick/apple/SDL_mfijoystick.m
@@ -1667,31 +1667,14 @@ static SDL_bool IOS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMappi
 SDL_bool IOS_SupportedHIDDevice(IOHIDDeviceRef device)
 {
     if (@available(macOS 10.16, *)) {
-        if ([GCController supportsHIDDevice:device]) {
-            return SDL_TRUE;
-        }
-
-        /* GCController supportsHIDDevice may return false if the device hasn't been
-         * seen by the framework yet, so check a few controllers we know are supported.
-         */
-        {
-            Sint32 vendor = 0;
-            Sint32 product = 0;
-            CFTypeRef refCF = NULL;
-
-            refCF = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey));
-            if (refCF) {
-                CFNumberGetValue(refCF, kCFNumberSInt32Type, &vendor);
-            }
-
-            refCF = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey));
-            if (refCF) {
-                CFNumberGetValue(refCF, kCFNumberSInt32Type, &product);
-            }
-
-            if (vendor == USB_VENDOR_MICROSOFT && SDL_IsJoystickXboxSeriesX(vendor, product)) {
+        const int MAX_ATTEMPTS = 3;
+        for (int attempt = 0; attempt < MAX_ATTEMPTS; ++attempt) {
+            if ([GCController supportsHIDDevice:device]) {
                 return SDL_TRUE;
             }
+
+            /* The framework may not have seen the device yet */
+            SDL_Delay(10);
         }
     }
     return SDL_FALSE;