SDL: add 8BitDo Controller (#12964)

From 2b3c481215c9ff57510a62d57aa1601f54af58af Mon Sep 17 00:00:00 2001
From: 8BitDo <[EMAIL REDACTED]>
Date: Wed, 7 May 2025 10:47:10 +0800
Subject: [PATCH] add 8BitDo Controller (#12964)

add SN30 Pro, SF30 Pro, Pro 2.
Supported versions:
Pro 2 v3.06  above
SF30 Pro/SN30 Pro v2.05 above
---
 src/joystick/SDL_joystick.c             | 15 ++++++++++++-
 src/joystick/hidapi/SDL_hidapi_8bitdo.c | 28 ++++++++++++++++++++++++-
 src/joystick/usb_ids.h                  |  5 +++++
 3 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index df6988909a35b..c88b15cbe3030 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -3177,7 +3177,20 @@ bool SDL_IsJoystickHoriSteamController(Uint16 vendor_id, Uint16 product_id)
 
 bool SDL_IsJoystick8BitDoController(Uint16 vendor_id, Uint16 product_id)
 {
-    return vendor_id == USB_VENDOR_8BITDO && (product_id == USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS);
+    if (vendor_id == USB_VENDOR_8BITDO) {
+        switch (product_id) {
+        case USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS:
+        case USB_PRODUCT_8BITDO_SN30_PRO:
+        case USB_PRODUCT_8BITDO_SN30_PRO_BT:
+        case USB_PRODUCT_8BITDO_SF30_PRO:
+        case USB_PRODUCT_8BITDO_PRO_2:
+        case USB_PRODUCT_8BITDO_PRO_2_BT:
+            return true;
+        default:
+            break;
+        }
+    }
+    return false;
 }
 
 bool SDL_IsJoystickSteamDeck(Uint16 vendor_id, Uint16 product_id)
diff --git a/src/joystick/hidapi/SDL_hidapi_8bitdo.c b/src/joystick/hidapi/SDL_hidapi_8bitdo.c
index ca60118006137..1f12f85957cbb 100644
--- a/src/joystick/hidapi/SDL_hidapi_8bitdo.c
+++ b/src/joystick/hidapi/SDL_hidapi_8bitdo.c
@@ -42,6 +42,11 @@ enum
     SDL_GAMEPAD_NUM_8BITDO_BUTTONS,
 };
 
+#define SDL_8BITDO_FEATURE_REPORTID_ENABLE_SDL_REPORTID         0x06
+#define SDL_8BITDO_REPORTID_SDL_REPORTID                        0x04
+#define SDL_8BITDO_REPORTID_NOT_SUPPORTED_SDL_REPORTID          0x03
+#define SDL_8BITDO_BT_REPORTID_SDL_REPORTID                     0x01
+
 #define ABITDO_ACCEL_SCALE 4096.f
 #define SENSOR_INTERVAL_NS 8000000ULL
 
@@ -112,6 +117,13 @@ static bool HIDAPI_Driver8BitDo_IsEnabled(void)
     return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_8BITDO, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
 }
 
+static int ReadFeatureReport(SDL_hid_device *dev, Uint8 report_id, Uint8 *report, size_t length)
+{
+    SDL_memset(report, 0, length);
+    report[0] = report_id;
+    return SDL_hid_get_feature_report(dev, report, length);
+}
+
 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)
 {
     return SDL_IsJoystick8BitDoController(vendor_id, product_id);
@@ -135,6 +147,19 @@ static bool HIDAPI_Driver8BitDo_InitDevice(SDL_HIDAPI_Device *device)
             ctx->rumble_supported = true;
             ctx->powerstate_supported = true;
         }
+    } else if (device->product_id == USB_PRODUCT_8BITDO_SN30_PRO || device->product_id == USB_PRODUCT_8BITDO_SN30_PRO_BT ||
+               device->product_id == USB_PRODUCT_8BITDO_SF30_PRO  || device->product_id == USB_PRODUCT_8BITDO_PRO_2 ||
+                device->product_id == USB_PRODUCT_8BITDO_PRO_2_BT) {
+        Uint8 data[USB_PACKET_LENGTH];
+        int size = ReadFeatureReport(device->dev, SDL_8BITDO_FEATURE_REPORTID_ENABLE_SDL_REPORTID, data, sizeof(data));
+        if (size > 0) {
+            ctx->sensors_supported = true;
+            ctx->rumble_supported = true;
+            ctx->powerstate_supported = true;
+        } else {
+            SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
+                         "HIDAPI_Driver8BitDo_InitDevice(): Couldn't read feature report 0x06");
+        }
     }
 
     return HIDAPI_JoystickConnected(device, NULL);
@@ -236,7 +261,8 @@ static void HIDAPI_Driver8BitDo_HandleStatePacket(SDL_Joystick *joystick, SDL_Dr
 {
     Sint16 axis;
     Uint64 timestamp = SDL_GetTicksNS();
-    if (data[0] != 0x03 && data[0] != 0x01) {
+    if (data[0] != SDL_8BITDO_REPORTID_SDL_REPORTID && data[0] != SDL_8BITDO_REPORTID_NOT_SUPPORTED_SDL_REPORTID &&
+        data[0] != SDL_8BITDO_BT_REPORTID_SDL_REPORTID) {
         // We don't know how to handle this report
         return;
     }
diff --git a/src/joystick/usb_ids.h b/src/joystick/usb_ids.h
index 6692cd412d2d9..812d0a4805fe5 100644
--- a/src/joystick/usb_ids.h
+++ b/src/joystick/usb_ids.h
@@ -60,6 +60,11 @@
 #define USB_VENDOR_ZEROPLUS     0x0c12
 
 #define USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS             0x6012
+#define USB_PRODUCT_8BITDO_SN30_PRO                       0x6001    // B + START
+#define USB_PRODUCT_8BITDO_SN30_PRO_BT                    0x6101    // B + START
+#define USB_PRODUCT_8BITDO_SF30_PRO                       0x6000    // B + START
+#define USB_PRODUCT_8BITDO_PRO_2                          0x6003    // mode switch to D 
+#define USB_PRODUCT_8BITDO_PRO_2_BT                       0x6006    // mode switch to D 
 #define USB_PRODUCT_AMAZON_LUNA_CONTROLLER                0x0419
 #define USB_PRODUCT_ASTRO_C40_XBOX360                     0x0024
 #define USB_PRODUCT_BACKBONE_ONE_IOS                      0x0103