SDL: Added support for the GameSir-G7 Controller for Xbox (4a0bd)

From 4a0bd06d58a2e793c70b25ce3700282c2945b3c7 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 11 May 2023 17:44:56 -0700
Subject: [PATCH] Added support for the GameSir-G7 Controller for Xbox

(cherry picked from commit 2e6c48dcb4b52df4dd9244610ba23a8f5e3debe5)
---
 .../main/java/org/libsdl/app/HIDDeviceManager.java |  1 +
 src/hidapi/libusb/hid.c                            |  1 +
 src/joystick/SDL_joystick.c                        | 14 ++++++++++----
 src/joystick/hidapi/SDL_hidapijoystick.c           |  1 +
 src/joystick/usb_ids.h                             |  2 ++
 5 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
index 5310d6016ad1..af605ac2db64 100644
--- a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java
@@ -285,6 +285,7 @@ private boolean isXboxOneController(UsbDevice usbDevice, UsbInterface usbInterfa
             0x24c6, // PowerA
             0x2dc8, // 8BitDo
             0x2e24, // Hyperkin
+            0x3537, // GameSir
         };
 
         if (usbInterface.getId() == 0 &&
diff --git a/src/hidapi/libusb/hid.c b/src/hidapi/libusb/hid.c
index e5ee31b9a93e..008131bd9d2d 100644
--- a/src/hidapi/libusb/hid.c
+++ b/src/hidapi/libusb/hid.c
@@ -723,6 +723,7 @@ static int is_xboxone(unsigned short vendor_id, const struct libusb_interface_de
 		0x24c6, /* PowerA */
 		0x2dc8, /* 8BitDo */
 		0x2e24, /* Hyperkin */
+        0x3537, /* GameSir */
 	};
 
 	if (intf_desc->bInterfaceNumber == 0 &&
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 3d4488b65ca8..cf9330081153 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -1962,11 +1962,12 @@ char *SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_n
     } replacements[] = {
         { "ASTRO Gaming", "ASTRO" },
         { "Bensussen Deutsch & Associates,Inc.(BDA)", "BDA" },
-        { "NVIDIA Corporation ", "" },
-        { "Performance Designed Products", "PDP" },
-        { "HORI CO.,LTD.", "HORI" },
+        { "Guangzhou Chicken Run Network Technology Co., Ltd.", "GameSir" },
         { "HORI CO.,LTD", "HORI" },
+        { "HORI CO.,LTD.", "HORI" },
         { "Mad Catz Inc.", "Mad Catz" },
+        { "NVIDIA Corporation ", "" },
+        { "Performance Designed Products", "PDP" },
         { "QANBA USA, LLC", "Qanba" },
         { "QANBA USA,LLC", "Qanba" },
         { "Unknown ", "" },
@@ -2077,7 +2078,7 @@ char *SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_n
     for (i = 1; i < (len - 1); ++i) {
         int matchlen = PrefixMatch(name, &name[i]);
         while (matchlen > 0) {
-            if (name[matchlen] == ' ') {
+            if (name[matchlen] == ' ' || name[matchlen] == '-') {
                 SDL_memmove(name, name + matchlen + 1, len - matchlen);
                 break;
             }
@@ -2351,6 +2352,11 @@ SDL_bool SDL_IsJoystickXboxSeriesX(Uint16 vendor_id, Uint16 product_id)
             return SDL_TRUE;
         }
     }
+    if (vendor_id == USB_VENDOR_GAMESIR) {
+        if (product_id == USB_PRODUCT_GAMESIR_G7) {
+            return SDL_TRUE;
+        }
+    }
     return SDL_FALSE;
 }
 
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index c9d2d23a0abf..bc0102d937df 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -277,6 +277,7 @@ static SDL_GameControllerType SDL_GetJoystickGameControllerProtocol(const char *
             0x24c6, /* PowerA */
             0x2dc8, /* 8BitDo */
             0x2e24, /* Hyperkin */
+            0x3537, /* GameSir */
         };
 
         int i;
diff --git a/src/joystick/usb_ids.h b/src/joystick/usb_ids.h
index 6d70752d61b7..013265288133 100644
--- a/src/joystick/usb_ids.h
+++ b/src/joystick/usb_ids.h
@@ -29,6 +29,7 @@
 #define USB_VENDOR_APPLE        0x05ac
 #define USB_VENDOR_ASTRO        0x9886
 #define USB_VENDOR_BACKBONE     0x358a
+#define USB_VENDOR_GAMESIR      0x3537
 #define USB_VENDOR_DRAGONRISE   0x0079
 #define USB_VENDOR_GOOGLE       0x18d1
 #define USB_VENDOR_HORI         0x0f0d
@@ -58,6 +59,7 @@
 #define USB_PRODUCT_ASTRO_C40_XBOX360                     0x0024
 #define USB_PRODUCT_BACKBONE_ONE_IOS                      0x0103
 #define USB_PRODUCT_BACKBONE_ONE_IOS_PS5                  0x0104
+#define USB_PRODUCT_GAMESIR_G7                            0x1001
 #define USB_PRODUCT_GOOGLE_STADIA_CONTROLLER              0x9400
 #define USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER1            0x1843
 #define USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER2            0x1846