From b7940c29cc756dd3126dd796727950cd35392249 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 23 Sep 2022 00:15:40 -0700
Subject: [PATCH] Allow HIDAPI controllers to override the default joystick
type
---
src/joystick/SDL_joystick.c | 6 ++++++
src/joystick/hidapi/SDL_hidapijoystick.c | 19 +++++++++++++++++++
src/joystick/hidapi/SDL_hidapijoystick_c.h | 4 ++++
3 files changed, 29 insertions(+)
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index e445fe53ad7..4d81495e7a7 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -2525,6 +2525,12 @@ static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)
return SDL_JOYSTICK_TYPE_THROTTLE;
}
+#ifdef SDL_JOYSTICK_HIDAPI
+ if (SDL_IsJoystickHIDAPI(guid)) {
+ return HIDAPI_GetJoystickTypeFromGUID(guid);
+ }
+#endif /* SDL_JOYSTICK_HIDAPI */
+
if (GuessControllerType(vendor, product) != k_eControllerType_UnknownNonSteamController) {
return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
}
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index fd2ce71405f..87bcfce7859 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -688,6 +688,7 @@ HIDAPI_AddDevice(const struct SDL_hid_device_info *info, int num_children, SDL_H
/* FIXME: Is there any way to tell whether this is a Bluetooth device? */
device->guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_USB, device->vendor_id, device->product_id, device->version, device->name, 'h', 0);
+ device->joystick_type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
device->type = SDL_GetJoystickGameControllerProtocol(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol);
if (num_children > 0) {
@@ -1010,6 +1011,24 @@ HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, cons
return result;
}
+SDL_JoystickType
+HIDAPI_GetJoystickTypeFromGUID(SDL_JoystickGUID guid)
+{
+ SDL_HIDAPI_Device *device;
+ SDL_JoystickType type = SDL_JOYSTICK_TYPE_UNKNOWN;
+
+ SDL_LockJoysticks();
+ for (device = SDL_HIDAPI_devices; device; device = device->next) {
+ if (SDL_memcmp(&guid, &device->guid, sizeof(guid)) == 0) {
+ type = device->joystick_type;
+ break;
+ }
+ }
+ SDL_UnlockJoysticks();
+
+ return type;
+}
+
SDL_GameControllerType
HIDAPI_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid)
{
diff --git a/src/joystick/hidapi/SDL_hidapijoystick_c.h b/src/joystick/hidapi/SDL_hidapijoystick_c.h
index c653d15bea4..3b0e9490440 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick_c.h
+++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h
@@ -73,6 +73,7 @@ typedef struct _SDL_HIDAPI_Device
int interface_protocol;
Uint16 usage_page; /* Available on Windows and Mac OS X */
Uint16 usage; /* Available on Windows and Mac OS X */
+ SDL_JoystickType joystick_type;
SDL_GameControllerType type;
struct _SDL_HIDAPI_DeviceDriver *driver;
@@ -145,6 +146,9 @@ extern SDL_bool HIDAPI_IsDeviceTypePresent(SDL_GameControllerType type);
/* Return true if a HID device is present and supported as a joystick */
extern SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
+/* Return the type of a joystick if it's present and supported */
+extern SDL_GameControllerType HIDAPI_GetJoystickTypeFromGUID(SDL_JoystickGUID guid);
+
/* Return the type of a game controller if it's present and supported */
extern SDL_GameControllerType HIDAPI_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid);