Commit 94b7a8764508a001917806623010a1f13b664e79

Sam Lantinga 2021-07-24T09:10:18

Added SDL_GameControllerType enumerations for the Amazon Luna and Google Stadia controllers Fixes bug https://github.com/libsdl-org/SDL/issues/4019

diff --git a/include/SDL_gamecontroller.h b/include/SDL_gamecontroller.h
index de019ae..720a75a 100644
--- a/include/SDL_gamecontroller.h
+++ b/include/SDL_gamecontroller.h
@@ -67,7 +67,9 @@ typedef enum
     SDL_CONTROLLER_TYPE_PS4,
     SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO,
     SDL_CONTROLLER_TYPE_VIRTUAL,
-    SDL_CONTROLLER_TYPE_PS5
+    SDL_CONTROLLER_TYPE_PS5,
+    SDL_CONTROLLER_TYPE_AMAZON_LUNA,
+    SDL_CONTROLLER_TYPE_GOOGLE_STADIA
 } SDL_GameControllerType;
 
 typedef enum
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index aa03a51..3c5e595 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -1898,6 +1898,13 @@ SDL_GetJoystickGameControllerType(const char *name, Uint16 vendor, Uint16 produc
         } else if (vendor == 0x0001 && product == 0x0001) {
             type = SDL_CONTROLLER_TYPE_UNKNOWN;
 
+        } else if ((vendor == USB_VENDOR_AMAZON && product == USB_PRODUCT_AMAZON_LUNA_CONTROLLER) ||
+                   (vendor == BLUETOOTH_VENDOR_AMAZON && product == BLUETOOTH_PRODUCT_LUNA_CONTROLLER)) {
+            type = SDL_CONTROLLER_TYPE_AMAZON_LUNA;
+
+        } else if (vendor == USB_VENDOR_GOOGLE && product == USB_PRODUCT_GOOGLE_STADIA_CONTROLLER) {
+            type = SDL_CONTROLLER_TYPE_GOOGLE_STADIA;
+
         } else {
             switch (GuessControllerType(vendor, product)) {
             case k_eControllerType_XBox360Controller:
diff --git a/src/joystick/hidapi/SDL_hidapi_luna.c b/src/joystick/hidapi/SDL_hidapi_luna.c
index e39b568..78d5c5e 100644
--- a/src/joystick/hidapi/SDL_hidapi_luna.c
+++ b/src/joystick/hidapi/SDL_hidapi_luna.c
@@ -50,11 +50,7 @@ typedef struct {
 static SDL_bool
 HIDAPI_DriverLuna_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
 {
-    if ((vendor_id == USB_VENDOR_AMAZON && product_id == USB_PRODUCT_AMAZON_LUNA_CONTROLLER) ||
-        (vendor_id == BLUETOOTH_VENDOR_AMAZON && product_id == BLUETOOTH_PRODUCT_LUNA_CONTROLLER)) {
-        return SDL_TRUE;
-    }
-    return SDL_FALSE;
+    return (type == SDL_CONTROLLER_TYPE_AMAZON_LUNA) ? SDL_TRUE : SDL_FALSE;
 }
 
 static const char *
diff --git a/src/joystick/hidapi/SDL_hidapi_stadia.c b/src/joystick/hidapi/SDL_hidapi_stadia.c
index 896b480..c11aadd 100644
--- a/src/joystick/hidapi/SDL_hidapi_stadia.c
+++ b/src/joystick/hidapi/SDL_hidapi_stadia.c
@@ -51,10 +51,7 @@ typedef struct {
 static SDL_bool
 HIDAPI_DriverStadia_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
 {
-    if (vendor_id == USB_VENDOR_GOOGLE && product_id == USB_PRODUCT_GOOGLE_STADIA_CONTROLLER) {
-        return SDL_TRUE;
-    }
-    return SDL_FALSE;
+    return (type == SDL_CONTROLLER_TYPE_GOOGLE_STADIA) ? SDL_TRUE : SDL_FALSE;
 }
 
 static const char *
diff --git a/test/testgamecontroller.c b/test/testgamecontroller.c
index 71de641..8a3ff92 100644
--- a/test/testgamecontroller.c
+++ b/test/testgamecontroller.c
@@ -554,11 +554,14 @@ main(int argc, char *argv[])
             controller_count++;
             name = SDL_GameControllerNameForIndex(i);
             switch (SDL_GameControllerTypeForIndex(i)) {
-            case SDL_CONTROLLER_TYPE_XBOX360:
-                description = "XBox 360 Controller";
+            case SDL_CONTROLLER_TYPE_AMAZON_LUNA:
+                description = "Amazon Luna Controller";
                 break;
-            case SDL_CONTROLLER_TYPE_XBOXONE:
-                description = "XBox One Controller";
+            case SDL_CONTROLLER_TYPE_GOOGLE_STADIA:
+                description = "Google Stadia Controller";
+                break;
+            case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO:
+                description = "Nintendo Switch Pro Controller";
                 break;
             case SDL_CONTROLLER_TYPE_PS3:
                 description = "PS3 Controller";
@@ -566,8 +569,14 @@ main(int argc, char *argv[])
             case SDL_CONTROLLER_TYPE_PS4:
                 description = "PS4 Controller";
                 break;
-            case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO:
-                description = "Nintendo Switch Pro Controller";
+            case SDL_CONTROLLER_TYPE_PS5:
+                description = "PS5 Controller";
+                break;
+            case SDL_CONTROLLER_TYPE_XBOX360:
+                description = "XBox 360 Controller";
+                break;
+            case SDL_CONTROLLER_TYPE_XBOXONE:
+                description = "XBox One Controller";
                 break;
             case SDL_CONTROLLER_TYPE_VIRTUAL:
                 description = "Virtual Game Controller";