Added functions to get the platform dependent name for a joystick or game controller

This commit is contained in:
Sam Lantinga
2022-04-26 14:54:14 -07:00
parent b293888c7d
commit e551384a99
28 changed files with 306 additions and 26 deletions

View File

@@ -456,7 +456,7 @@ EnumJoystickDetectCallback(LPCDIDEVICEINSTANCE pDeviceInstance, LPVOID pContext)
pNewJoystick = *(JoyStick_DeviceData**)pContext;
while (pNewJoystick) {
/* update GUIDs of joysticks with matching paths, in case they're not open yet */
if (SDL_strcmp(pNewJoystick->hidPath, hidPath) == 0) {
if (SDL_strcmp(pNewJoystick->path, hidPath) == 0) {
/* if we are replacing the front of the list then update it */
if (pNewJoystick == *(JoyStick_DeviceData**)pContext) {
*(JoyStick_DeviceData**)pContext = pNewJoystick->pNext;
@@ -483,7 +483,7 @@ EnumJoystickDetectCallback(LPCDIDEVICEINSTANCE pDeviceInstance, LPVOID pContext)
CHECK(pNewJoystick);
SDL_zerop(pNewJoystick);
SDL_strlcpy(pNewJoystick->hidPath, hidPath, SDL_arraysize(pNewJoystick->hidPath));
SDL_strlcpy(pNewJoystick->path, hidPath, SDL_arraysize(pNewJoystick->path));
SDL_memcpy(&pNewJoystick->dxdevice, pDeviceInstance, sizeof(DIDEVICEINSTANCE));
SDL_memset(pNewJoystick->guid.data, 0, sizeof(pNewJoystick->guid.data));

View File

@@ -105,6 +105,7 @@ typedef struct _SDL_RAWINPUT_Device
{
SDL_atomic_t refcount;
char *name;
char *path;
Uint16 vendor_id;
Uint16 product_id;
Uint16 version;
@@ -691,6 +692,7 @@ RAWINPUT_ReleaseDevice(SDL_RAWINPUT_Device *device)
if (SDL_AtomicDecRef(&device->refcount)) {
SDL_free(device->preparsed_data);
SDL_free(device->name);
SDL_free(device->path);
SDL_free(device);
}
}
@@ -796,6 +798,7 @@ RAWINPUT_AddDevice(HANDLE hDevice)
SDL_free(product_string);
}
}
device->path = SDL_strdup(dev_name);
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
@@ -828,8 +831,12 @@ err:
CloseHandle(hFile);
}
if (device) {
if (device->name)
if (device->name) {
SDL_free(device->name);
}
if (device->path) {
SDL_free(device->path);
}
SDL_free(device);
}
#undef CHECK
@@ -1032,6 +1039,12 @@ RAWINPUT_JoystickGetDeviceName(int device_index)
return RAWINPUT_GetDeviceByIndex(device_index)->name;
}
static const char *
RAWINPUT_JoystickGetDevicePath(int device_index)
{
return RAWINPUT_GetDeviceByIndex(device_index)->path;
}
static int
RAWINPUT_JoystickGetDevicePlayerIndex(int device_index)
{
@@ -2009,6 +2022,7 @@ SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver =
RAWINPUT_JoystickGetCount,
RAWINPUT_JoystickDetect,
RAWINPUT_JoystickGetDeviceName,
RAWINPUT_JoystickGetDevicePath,
RAWINPUT_JoystickGetDevicePlayerIndex,
RAWINPUT_JoystickSetDevicePlayerIndex,
RAWINPUT_JoystickGetDeviceGUID,

View File

@@ -586,6 +586,12 @@ WGI_JoystickGetDeviceName(int device_index)
return wgi.controllers[device_index].name;
}
static const char *
WGI_JoystickGetDevicePath(int device_index)
{
return NULL;
}
static int
WGI_JoystickGetDevicePlayerIndex(int device_index)
{
@@ -893,6 +899,7 @@ SDL_JoystickDriver SDL_WGI_JoystickDriver =
WGI_JoystickGetCount,
WGI_JoystickDetect,
WGI_JoystickGetDeviceName,
WGI_JoystickGetDevicePath,
WGI_JoystickGetDevicePlayerIndex,
WGI_JoystickSetDevicePlayerIndex,
WGI_JoystickGetDeviceGUID,

View File

@@ -555,7 +555,6 @@ WINDOWS_JoystickDetect(void)
}
}
/* Function to get the device-dependent name of a joystick */
static const char *
WINDOWS_JoystickGetDeviceName(int device_index)
{
@@ -568,6 +567,18 @@ WINDOWS_JoystickGetDeviceName(int device_index)
return device->joystickname;
}
static const char *
WINDOWS_JoystickGetDevicePath(int device_index)
{
JoyStick_DeviceData *device = SYS_Joystick;
int index;
for (index = device_index; index > 0; index--)
device = device->pNext;
return device->path;
}
static int
WINDOWS_JoystickGetDevicePlayerIndex(int device_index)
{
@@ -755,6 +766,7 @@ SDL_JoystickDriver SDL_WINDOWS_JoystickDriver =
WINDOWS_JoystickGetCount,
WINDOWS_JoystickDetect,
WINDOWS_JoystickGetDeviceName,
WINDOWS_JoystickGetDevicePath,
WINDOWS_JoystickGetDevicePlayerIndex,
WINDOWS_JoystickSetDevicePlayerIndex,
WINDOWS_JoystickGetDeviceGUID,

View File

@@ -37,7 +37,7 @@ typedef struct JoyStick_DeviceData
BYTE SubType;
Uint8 XInputUserId;
DIDEVICEINSTANCE dxdevice;
char hidPath[MAX_PATH];
char path[MAX_PATH];
struct JoyStick_DeviceData *pNext;
} JoyStick_DeviceData;

View File

@@ -297,6 +297,7 @@ AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
SDL_free(pNewJoystick);
return; /* better luck next time? */
}
SDL_snprintf(pNewJoystick->path, sizeof(pNewJoystick->path), "XInput#%d", userid);
if (SDL_ShouldIgnoreJoystick(pNewJoystick->joystickname, pNewJoystick->guid)) {
SDL_free(pNewJoystick);