Allow HIDAPI controllers to override the default joystick type

This commit is contained in:
Sam Lantinga 2022-09-23 00:15:40 -07:00
parent 899a1e7cba
commit b7940c29cc
3 changed files with 29 additions and 0 deletions

View File

@ -2525,6 +2525,12 @@ static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)
return SDL_JOYSTICK_TYPE_THROTTLE; 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) { if (GuessControllerType(vendor, product) != k_eControllerType_UnknownNonSteamController) {
return SDL_JOYSTICK_TYPE_GAMECONTROLLER; return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
} }

View File

@ -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? */ /* 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->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); 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) { if (num_children > 0) {
@ -1010,6 +1011,24 @@ HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, cons
return result; 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 SDL_GameControllerType
HIDAPI_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid) HIDAPI_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid)
{ {

View File

@ -73,6 +73,7 @@ typedef struct _SDL_HIDAPI_Device
int interface_protocol; int interface_protocol;
Uint16 usage_page; /* Available on Windows and Mac OS X */ Uint16 usage_page; /* Available on Windows and Mac OS X */
Uint16 usage; /* Available on Windows and Mac OS X */ Uint16 usage; /* Available on Windows and Mac OS X */
SDL_JoystickType joystick_type;
SDL_GameControllerType type; SDL_GameControllerType type;
struct _SDL_HIDAPI_DeviceDriver *driver; 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 */ /* 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); 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 */ /* Return the type of a game controller if it's present and supported */
extern SDL_GameControllerType HIDAPI_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid); extern SDL_GameControllerType HIDAPI_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid);