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

@ -320,6 +320,28 @@ SDL_JoystickNameForIndex(int device_index)
return name;
}
/*
* Get the implementation dependent path of a joystick
*/
const char *
SDL_JoystickPathForIndex(int device_index)
{
SDL_JoystickDriver *driver;
const char *path = NULL;
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
path = driver->GetDevicePath(device_index);
}
SDL_UnlockJoysticks();
/* FIXME: Really we should reference count this path so it doesn't go away after unlock */
if (!path) {
SDL_Unsupported();
}
return path;
}
/*
* Get the player index of a joystick, or -1 if it's not available
*/
@ -386,6 +408,7 @@ SDL_JoystickOpen(int device_index)
SDL_Joystick *joystick;
SDL_Joystick *joysticklist;
const char *joystickname = NULL;
const char *joystickpath = NULL;
SDL_JoystickPowerLevel initial_power_level;
SDL_LockJoysticks();
@ -436,6 +459,13 @@ SDL_JoystickOpen(int device_index)
joystick->name = NULL;
}
joystickpath = driver->GetDevicePath(device_index);
if (joystickpath) {
joystick->path = SDL_strdup(joystickpath);
} else {
joystick->path = NULL;
}
joystick->guid = driver->GetDeviceGUID(device_index);
if (joystick->naxes > 0) {
@ -840,6 +870,23 @@ SDL_JoystickName(SDL_Joystick *joystick)
return joystick->name;
}
/*
* Get the implementation dependent path of this joystick
*/
const char *
SDL_JoystickPath(SDL_Joystick *joystick)
{
if (!SDL_PrivateJoystickValid(joystick)) {
return NULL;
}
if (!joystick->path) {
SDL_Unsupported();
return NULL;
}
return joystick->path;
}
/**
* Get the player index of an opened joystick, or -1 if it's not available
*/
@ -1106,6 +1153,7 @@ SDL_JoystickClose(SDL_Joystick *joystick)
}
SDL_free(joystick->name);
SDL_free(joystick->path);
SDL_free(joystick->serial);
/* Free the data associated with this joystick */