From fd9a4eff9f9b287a4e87dba4539f439086b0bc89 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 17 Feb 2024 17:52:48 -0800 Subject: [PATCH] Updated GameInput device info to match other joystick drivers --- src/joystick/gdk/SDL_gameinputjoystick.c | 92 ++++++------------------ 1 file changed, 20 insertions(+), 72 deletions(-) diff --git a/src/joystick/gdk/SDL_gameinputjoystick.c b/src/joystick/gdk/SDL_gameinputjoystick.c index b04d2447d..c2692c442 100644 --- a/src/joystick/gdk/SDL_gameinputjoystick.c +++ b/src/joystick/gdk/SDL_gameinputjoystick.c @@ -33,15 +33,15 @@ typedef struct GAMEINPUT_InternalDevice { IGameInputDevice *device; - const char *deviceName; /* this is a constant string literal */ + char path[(APP_LOCAL_DEVICE_ID_SIZE * 2) + 1]; + const char *name; /* this is a constant string literal */ Uint16 vendor; Uint16 product; - SDL_JoystickGUID joystickGuid; /* generated by SDL. */ - SDL_JoystickID instanceId; /* generated by SDL. */ - int playerIndex; + SDL_JoystickGUID guid; /* generated by SDL */ + SDL_JoystickID device_instance; /* generated by SDL */ GameInputRumbleMotors supportedRumbleMotors; - char devicePath[(APP_LOCAL_DEVICE_ID_SIZE * 2) + 1]; - SDL_bool isAdded, isDeleteRequested; + SDL_bool isAdded; + SDL_bool isDeleteRequested; } GAMEINPUT_InternalDevice; typedef struct GAMEINPUT_InternalList @@ -72,7 +72,7 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice) Uint16 vendor = 0; Uint16 product = 0; Uint16 version = 0; - char tmpbuff[4]; + char tmp[4]; int idx = 0; if (!pDevice) { @@ -105,8 +105,8 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice) /* generate a device name */ for (idx = 0; idx < APP_LOCAL_DEVICE_ID_SIZE; ++idx) { - SDL_snprintf(tmpbuff, SDL_arraysize(tmpbuff), "%02hhX", devinfo->deviceId.value[idx]); - SDL_strlcat(elem->devicePath, tmpbuff, SDL_arraysize(tmpbuff)); + SDL_snprintf(tmp, SDL_arraysize(tmp), "%02hhX", devinfo->deviceId.value[idx]); + SDL_strlcat(elem->path, tmp, SDL_arraysize(tmp)); } if (devinfo->capabilities & GameInputDeviceCapabilityWireless) { bus = SDL_HARDWARE_BUS_BLUETOOTH; @@ -120,12 +120,12 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice) g_GameInputList.devices = devicelist; IGameInputDevice_AddRef(pDevice); elem->device = pDevice; - elem->deviceName = "GameInput Gamepad"; + elem->name = "GameInput Gamepad"; elem->vendor = vendor; elem->product = product; + elem->guid = SDL_CreateJoystickGUID(bus, vendor, product, version, "GameInput", "Gamepad", 'g', 0); + elem->device_instance = SDL_GetNextObjectID(); elem->supportedRumbleMotors = devinfo->supportedRumbleMotors; - elem->joystickGuid = SDL_CreateJoystickGUID(bus, vendor, product, version, "GameInput", "Gamepad", 'g', 0); - elem->instanceId = SDL_GetNextObjectID(); g_GameInputList.devices[g_GameInputList.count] = elem; /* finally increment the count and return */ @@ -172,11 +172,7 @@ static int GAMEINPUT_InternalRemoveByIndex(int idx) static GAMEINPUT_InternalDevice *GAMEINPUT_InternalFindByIndex(int idx) { - if (idx < 0 || idx >= g_GameInputList.count) { - SDL_SetError("GAMEINPUT_InternalFindByIndex argument idx %d out of range", idx); - return NULL; - } - + /* We're guaranteed that the index is in range when this is called */ return g_GameInputList.devices[idx]; } @@ -265,12 +261,12 @@ static void GAMEINPUT_JoystickDetect(void) } if (!elem->isAdded) { - SDL_PrivateJoystickAdded(elem->instanceId); + SDL_PrivateJoystickAdded(elem->device_instance); elem->isAdded = SDL_TRUE; } if (elem->isDeleteRequested || !(IGameInputDevice_GetDeviceStatus(elem->device) & GameInputDeviceConnected)) { - SDL_PrivateJoystickRemoved(elem->instanceId); + SDL_PrivateJoystickRemoved(elem->device_instance); GAMEINPUT_InternalRemoveByIndex(idx--); } } @@ -298,25 +294,13 @@ static SDL_bool GAMEINPUT_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 produ static const char *GAMEINPUT_JoystickGetDeviceName(int device_index) { - GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); - - if (!elem) { - return NULL; - } - - return elem->deviceName; + return GAMEINPUT_InternalFindByIndex(device_index)->name; } static const char *GAMEINPUT_JoystickGetDevicePath(int device_index) { - GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); - - if (!elem) { - return NULL; - } - /* APP_LOCAL_DEVICE_ID as a hex string, since it's required for some association callbacks */ - return elem->devicePath; + return GAMEINPUT_InternalFindByIndex(device_index)->path; } static int GAMEINPUT_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index) @@ -327,57 +311,21 @@ static int GAMEINPUT_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index) static int GAMEINPUT_JoystickGetDevicePlayerIndex(int device_index) { - /* - * Okay, so, while XInput technically has player indicies, - * GameInput does not. It just dispatches a callback whenever a device is found. - * So if you're using true native GameInput (which this backend IS) - * you're meant to assign some index to a player yourself. - * - * GameMaker, for example, seems to do this in the order of plugging in. - * - * Sorry for the trouble! - */ - GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); - - if (!elem) { - return -1; - } - - return elem->playerIndex; + return -1; } static void GAMEINPUT_JoystickSetDevicePlayerIndex(int device_index, int player_index) { - GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); - - if (!elem) { - return; - } - - elem->playerIndex = player_index; } static SDL_JoystickGUID GAMEINPUT_JoystickGetDeviceGUID(int device_index) { - GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); - - if (!elem) { - static SDL_JoystickGUID emptyGUID; - return emptyGUID; - } - - return elem->joystickGuid; + return GAMEINPUT_InternalFindByIndex(device_index)->guid; } static SDL_JoystickID GAMEINPUT_JoystickGetDeviceInstanceID(int device_index) { - GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); - - if (!elem) { - return 0; - } - - return elem->instanceId; + return GAMEINPUT_InternalFindByIndex(device_index)->device_instance; } static int GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)