SDL: 8BitDo HIDAPI driver cleanup

From 0d9ff082fe26ed1ff7bec790b9536ef528c759eb Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 1 Apr 2025 09:00:39 -0700
Subject: [PATCH] 8BitDo HIDAPI driver cleanup

---
 src/joystick/hidapi/SDL_hidapi_8bitdo.c | 29 ++++++++++---------------
 1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/src/joystick/hidapi/SDL_hidapi_8bitdo.c b/src/joystick/hidapi/SDL_hidapi_8bitdo.c
index 2685448e80ebf..69f60441e3b9a 100644
--- a/src/joystick/hidapi/SDL_hidapi_8bitdo.c
+++ b/src/joystick/hidapi/SDL_hidapi_8bitdo.c
@@ -105,9 +105,7 @@ static void HIDAPI_Driver8BitDo_UnregisterHints(SDL_HintCallback callback, void
 
 static bool HIDAPI_Driver8BitDo_IsEnabled(void)
 {
-    // We'll default this off for now, since we don't have a way to tell whether the controller is running firmware v1.03 and don't have a fallback for controllers running firmware v1.02 (the out-of-box firmware)
     return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_8BITDO, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
-    //return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_8BITDO, false);
 }
 
 static bool HIDAPI_Driver8BitDo_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
@@ -117,28 +115,21 @@ static bool HIDAPI_Driver8BitDo_IsSupportedDevice(SDL_HIDAPI_Device *device, con
 
 static bool HIDAPI_Driver8BitDo_InitDevice(SDL_HIDAPI_Device *device)
 {
-    SDL_Driver8BitDo_Context *ctx;
-    Uint8 data[USB_PACKET_LENGTH];
-    int size;
-    ctx = (SDL_Driver8BitDo_Context *)SDL_calloc(1, sizeof(*ctx));
+    SDL_Driver8BitDo_Context *ctx = (SDL_Driver8BitDo_Context *)SDL_calloc(1, sizeof(*ctx));
     if (!ctx) {
         return false;
     }
-
     device->context = ctx;
 
     if (device->product_id == USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS) {
-        ctx->sensors_supported = true;
-        ctx->rumble_supported = true;
-        ctx->rumble_type = 0;
-        ctx->powerstate_supported = true;
-
-        size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 80);
-        //ULTIMATE2_WIRELESS V1.02
-        if (size<30) {
-            ctx->powerstate_supported = false;
-            ctx->rumble_supported = false;
-            ctx->sensors_supported = false;
+        // The Ultimate 2 Wireless v1.02 firmware has X byte reports, v1.03 firmware has X byte reports
+        const int ULTIMATE2_WIRELESS_V103_REPORT_SIZE = 34;
+        Uint8 data[USB_PACKET_LENGTH];
+        int size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 80);
+        if (size >= ULTIMATE2_WIRELESS_V103_REPORT_SIZE) {
+            ctx->sensors_supported = true;
+            ctx->rumble_supported = true;
+            ctx->powerstate_supported = true;
         }
     }
 
@@ -208,9 +199,11 @@ static Uint32 HIDAPI_Driver8BitDo_GetJoystickCapabilities(SDL_HIDAPI_Device *dev
     if (ctx->rumble_supported) {
         caps |= SDL_JOYSTICK_CAP_RUMBLE;
     }
+#if 0 // HIDAPI_Driver8BitDo_SetJoystickLED() returns SDL_Unsupported()
     if (ctx->rgb_supported) {
         caps |= SDL_JOYSTICK_CAP_RGB_LED;
     }
+#endif
     return caps;
 }