Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

4.3 KB
Raw Blame
diff --git a/src/joystick/darwin/SDL_iokitjoystick.c b/src/joystick/darwin/SDL_iokitjoystick.c
--- a/src/joystick/darwin/SDL_iokitjoystick.c
+++ b/src/joystick/darwin/SDL_iokitjoystick.c
@@ -264,6 +264,28 @@
     return SDL_FALSE;
 }
 
+static SDL_bool IsGamepadUsage(uint32_t page, uint32_t usage)
+{
+    return page == kHIDPage_GenericDesktop &&
+           (usage == kHIDUsage_GD_Joystick || usage == kHIDUsage_GD_GamePad ||
+            usage == kHIDUsage_GD_MultiAxisController);
+}
+
+/* Some pads (GameSir G7 Pro over Bluetooth) list a consumer-control collection first,
+   so the device's primary usage is not a gamepad even though one of its top-level
+   collections is. Keep only the elements under that gamepad collection. */
+static SDL_bool InGamepadCollection(IOHIDElementRef element)
+{
+    while (element) {
+        if (IOHIDElementGetType(element) == kIOHIDElementTypeCollection &&
+            IOHIDElementGetCollectionType(element) == kIOHIDElementCollectionTypeApplication) {
+            return IsGamepadUsage(IOHIDElementGetUsagePage(element), IOHIDElementGetUsage(element));
+        }
+        element = IOHIDElementGetParent(element);
+    }
+    return SDL_FALSE;
+}
+
 /* See if we care about this HID element, and if so, note it in our recDevice. */
 static void AddHIDElement(const void *value, void *parameter)
 {
@@ -271,7 +293,8 @@
     IOHIDElementRef refElement = (IOHIDElementRef)value;
     const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0;
 
-    if (refElement && (elementTypeID == IOHIDElementGetTypeID())) {
+    if (refElement && (elementTypeID == IOHIDElementGetTypeID()) &&
+        (!pDevice->gamepadCollectionOnly || InGamepadCollection(refElement))) {
         const IOHIDElementCookie cookie = IOHIDElementGetCookie(refElement);
         const uint32_t usagePage = IOHIDElementGetUsagePage(refElement);
         const uint32_t usage = IOHIDElementGetUsage(refElement);
@@ -436,19 +459,33 @@
     if (refCF) {
         CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usagePage);
     }
-    if (pDevice->usagePage != kHIDPage_GenericDesktop) {
-        return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
-    }
-
     refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsageKey));
     if (refCF) {
         CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usage);
     }
 
-    if ((pDevice->usage != kHIDUsage_GD_Joystick &&
-         pDevice->usage != kHIDUsage_GD_GamePad &&
-         pDevice->usage != kHIDUsage_GD_MultiAxisController)) {
-        return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
+    if (!IsGamepadUsage(pDevice->usagePage, pDevice->usage)) {
+        CFArrayRef pairs = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDDeviceUsagePairsKey));
+        CFIndex i, count = (pairs && CFGetTypeID(pairs) == CFArrayGetTypeID()) ? CFArrayGetCount(pairs) : 0;
+
+        for (i = 0; i < count && !pDevice->gamepadCollectionOnly; i++) {
+            CFDictionaryRef pair = CFArrayGetValueAtIndex(pairs, i);
+            CFNumberRef pageRef = CFDictionaryGetValue(pair, CFSTR(kIOHIDDeviceUsagePageKey));
+            CFNumberRef usageRef = CFDictionaryGetValue(pair, CFSTR(kIOHIDDeviceUsageKey));
+            uint32_t page = 0, usage = 0;
+
+            if (pageRef && usageRef &&
+                CFNumberGetValue(pageRef, kCFNumberSInt32Type, &page) &&
+                CFNumberGetValue(usageRef, kCFNumberSInt32Type, &usage) &&
+                IsGamepadUsage(page, usage)) {
+                pDevice->usagePage = page;
+                pDevice->usage = usage;
+                pDevice->gamepadCollectionOnly = SDL_TRUE;
+            }
+        }
+        if (!pDevice->gamepadCollectionOnly) {
+            return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
+        }
     }
 
     /* Make sure we retain the use of the IOKit-provided device-object,
diff --git a/src/joystick/darwin/SDL_iokitjoystick_c.h b/src/joystick/darwin/SDL_iokitjoystick_c.h
--- a/src/joystick/darwin/SDL_iokitjoystick_c.h
+++ b/src/joystick/darwin/SDL_iokitjoystick_c.h
@@ -73,6 +73,7 @@
     SDL_JoystickGUID guid;
     int steam_virtual_gamepad_slot;
 
+    SDL_bool gamepadCollectionOnly; /* gamepad is a secondary top-level collection */
     struct joystick_hwdata *pNext; /* next device */
 };
 typedef struct joystick_hwdata recDevice;