SDL: Added SDL_GameControllerType enumerations for the Amazon Luna and Google Stadia controllers

From 94b7a8764508a001917806623010a1f13b664e79 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 24 Jul 2021 09:10:18 -0700
Subject: [PATCH] Added SDL_GameControllerType enumerations for the Amazon Luna
 and Google Stadia controllers

Fixes bug https://github.com/libsdl-org/SDL/issues/4019
---
 include/SDL_gamecontroller.h            |  4 +++-
 src/joystick/SDL_joystick.c             |  7 +++++++
 src/joystick/hidapi/SDL_hidapi_luna.c   |  6 +-----
 src/joystick/hidapi/SDL_hidapi_stadia.c |  5 +----
 test/testgamecontroller.c               | 21 +++++++++++++++------
 5 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/include/SDL_gamecontroller.h b/include/SDL_gamecontroller.h
index de019ae74..720a75a6f 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 aa03a51c6..3c5e59584 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 e39b56848..78d5c5ed6 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 896b480cf..c11aadd05 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 71de6417c..8a3ff92b0 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";