Updated GameInput device info to match other joystick drivers

This commit is contained in:
Sam Lantinga 2024-02-17 17:52:48 -08:00
parent 8f0f14c312
commit fd9a4eff9f

View file

@ -33,15 +33,15 @@
typedef struct GAMEINPUT_InternalDevice typedef struct GAMEINPUT_InternalDevice
{ {
IGameInputDevice *device; 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 vendor;
Uint16 product; Uint16 product;
SDL_JoystickGUID joystickGuid; /* generated by SDL. */ SDL_JoystickGUID guid; /* generated by SDL */
SDL_JoystickID instanceId; /* generated by SDL. */ SDL_JoystickID device_instance; /* generated by SDL */
int playerIndex;
GameInputRumbleMotors supportedRumbleMotors; GameInputRumbleMotors supportedRumbleMotors;
char devicePath[(APP_LOCAL_DEVICE_ID_SIZE * 2) + 1]; SDL_bool isAdded;
SDL_bool isAdded, isDeleteRequested; SDL_bool isDeleteRequested;
} GAMEINPUT_InternalDevice; } GAMEINPUT_InternalDevice;
typedef struct GAMEINPUT_InternalList typedef struct GAMEINPUT_InternalList
@ -72,7 +72,7 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
Uint16 vendor = 0; Uint16 vendor = 0;
Uint16 product = 0; Uint16 product = 0;
Uint16 version = 0; Uint16 version = 0;
char tmpbuff[4]; char tmp[4];
int idx = 0; int idx = 0;
if (!pDevice) { if (!pDevice) {
@ -105,8 +105,8 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
/* generate a device name */ /* generate a device name */
for (idx = 0; idx < APP_LOCAL_DEVICE_ID_SIZE; ++idx) { for (idx = 0; idx < APP_LOCAL_DEVICE_ID_SIZE; ++idx) {
SDL_snprintf(tmpbuff, SDL_arraysize(tmpbuff), "%02hhX", devinfo->deviceId.value[idx]); SDL_snprintf(tmp, SDL_arraysize(tmp), "%02hhX", devinfo->deviceId.value[idx]);
SDL_strlcat(elem->devicePath, tmpbuff, SDL_arraysize(tmpbuff)); SDL_strlcat(elem->path, tmp, SDL_arraysize(tmp));
} }
if (devinfo->capabilities & GameInputDeviceCapabilityWireless) { if (devinfo->capabilities & GameInputDeviceCapabilityWireless) {
bus = SDL_HARDWARE_BUS_BLUETOOTH; bus = SDL_HARDWARE_BUS_BLUETOOTH;
@ -120,12 +120,12 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
g_GameInputList.devices = devicelist; g_GameInputList.devices = devicelist;
IGameInputDevice_AddRef(pDevice); IGameInputDevice_AddRef(pDevice);
elem->device = pDevice; elem->device = pDevice;
elem->deviceName = "GameInput Gamepad"; elem->name = "GameInput Gamepad";
elem->vendor = vendor; elem->vendor = vendor;
elem->product = product; 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->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; g_GameInputList.devices[g_GameInputList.count] = elem;
/* finally increment the count and return */ /* finally increment the count and return */
@ -172,11 +172,7 @@ static int GAMEINPUT_InternalRemoveByIndex(int idx)
static GAMEINPUT_InternalDevice *GAMEINPUT_InternalFindByIndex(int idx) static GAMEINPUT_InternalDevice *GAMEINPUT_InternalFindByIndex(int idx)
{ {
if (idx < 0 || idx >= g_GameInputList.count) { /* We're guaranteed that the index is in range when this is called */
SDL_SetError("GAMEINPUT_InternalFindByIndex argument idx %d out of range", idx);
return NULL;
}
return g_GameInputList.devices[idx]; return g_GameInputList.devices[idx];
} }
@ -265,12 +261,12 @@ static void GAMEINPUT_JoystickDetect(void)
} }
if (!elem->isAdded) { if (!elem->isAdded) {
SDL_PrivateJoystickAdded(elem->instanceId); SDL_PrivateJoystickAdded(elem->device_instance);
elem->isAdded = SDL_TRUE; elem->isAdded = SDL_TRUE;
} }
if (elem->isDeleteRequested || !(IGameInputDevice_GetDeviceStatus(elem->device) & GameInputDeviceConnected)) { if (elem->isDeleteRequested || !(IGameInputDevice_GetDeviceStatus(elem->device) & GameInputDeviceConnected)) {
SDL_PrivateJoystickRemoved(elem->instanceId); SDL_PrivateJoystickRemoved(elem->device_instance);
GAMEINPUT_InternalRemoveByIndex(idx--); 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) static const char *GAMEINPUT_JoystickGetDeviceName(int device_index)
{ {
GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); return GAMEINPUT_InternalFindByIndex(device_index)->name;
if (!elem) {
return NULL;
}
return elem->deviceName;
} }
static const char *GAMEINPUT_JoystickGetDevicePath(int device_index) 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 */ /* 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) 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) 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 -1;
} }
return elem->playerIndex;
}
static void GAMEINPUT_JoystickSetDevicePlayerIndex(int device_index, int player_index) 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) static SDL_JoystickGUID GAMEINPUT_JoystickGetDeviceGUID(int device_index)
{ {
GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); return GAMEINPUT_InternalFindByIndex(device_index)->guid;
if (!elem) {
static SDL_JoystickGUID emptyGUID;
return emptyGUID;
}
return elem->joystickGuid;
} }
static SDL_JoystickID GAMEINPUT_JoystickGetDeviceInstanceID(int device_index) static SDL_JoystickID GAMEINPUT_JoystickGetDeviceInstanceID(int device_index)
{ {
GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index); return GAMEINPUT_InternalFindByIndex(device_index)->device_instance;
if (!elem) {
return 0;
}
return elem->instanceId;
} }
static int GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index) static int GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)