Replaced SDL_GetJoystickCaps() with joystick properties

Fixes https://github.com/libsdl-org/SDL/issues/8927
This commit is contained in:
Sam Lantinga
2024-01-27 14:37:30 -08:00
parent 019dc53764
commit f66fe7e221
37 changed files with 176 additions and 341 deletions

View File

@@ -723,6 +723,8 @@ static void HIDAPI_DriverPS4_SetEnhancedModeAvailable(SDL_DriverPS4_Context *ctx
if (ctx->device->is_bluetooth && ctx->official_controller) {
ctx->report_battery = SDL_TRUE;
}
HIDAPI_UpdateDeviceProperties(ctx->device);
}
static void HIDAPI_DriverPS4_SetEnhancedMode(SDL_DriverPS4_Context *ctx)

View File

@@ -818,6 +818,8 @@ static void HIDAPI_DriverPS5_SetEnhancedModeAvailable(SDL_DriverPS5_Context *ctx
if (ctx->device->is_bluetooth) {
ctx->report_battery = SDL_TRUE;
}
HIDAPI_UpdateDeviceProperties(ctx->device);
}
static void HIDAPI_DriverPS5_SetEnhancedMode(SDL_DriverPS5_Context *ctx)

View File

@@ -727,6 +727,7 @@ static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, c
ctx->m_bPlayerLights = bPlayerLights;
UpdateSlotLED(ctx);
HIDAPI_UpdateDeviceProperties(ctx->device);
}
}

View File

@@ -131,6 +131,7 @@ static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, c
ctx->player_lights = player_lights;
UpdateSlotLED(ctx);
HIDAPI_UpdateDeviceProperties(ctx->device);
}
}

View File

@@ -103,6 +103,7 @@ static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, c
ctx->player_lights = player_lights;
UpdateSlotLED(ctx);
HIDAPI_UpdateDeviceProperties(ctx->device);
}
}

View File

@@ -859,6 +859,54 @@ void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joyst
SDL_UnlockJoysticks();
}
static void HIDAPI_UpdateJoystickProperties(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_PropertiesID props = SDL_GetJoystickProperties(joystick);
Uint32 caps = device->driver->GetJoystickCapabilities(device, joystick);
if (caps & SDL_JOYSTICK_CAP_MONO_LED) {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN, SDL_TRUE);
} else {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN, SDL_FALSE);
}
if (caps & SDL_JOYSTICK_CAP_RGB_LED) {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, SDL_TRUE);
} else {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, SDL_FALSE);
}
if (caps & SDL_JOYSTICK_CAP_PLAYER_LED) {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_PLAYER_LED_BOOLEAN, SDL_TRUE);
} else {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_PLAYER_LED_BOOLEAN, SDL_FALSE);
}
if (caps & SDL_JOYSTICK_CAP_RUMBLE) {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, SDL_TRUE);
} else {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, SDL_FALSE);
}
if (caps & SDL_JOYSTICK_CAP_TRIGGER_RUMBLE) {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, SDL_TRUE);
} else {
SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, SDL_FALSE);
}
}
void HIDAPI_UpdateDeviceProperties(SDL_HIDAPI_Device *device)
{
int i;
SDL_LockJoysticks();
for (i = 0; i < device->num_joysticks; ++i) {
SDL_Joystick *joystick = SDL_GetJoystickFromInstanceID(device->joysticks[i]);
if (joystick) {
HIDAPI_UpdateJoystickProperties(device, joystick);
}
}
SDL_UnlockJoysticks();
}
static int HIDAPI_JoystickGetCount(void)
{
return SDL_HIDAPI_numjoysticks;
@@ -1471,6 +1519,8 @@ static int HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
return -1;
}
HIDAPI_UpdateJoystickProperties(device, joystick);
if (device->serial) {
joystick->serial = SDL_strdup(device->serial);
}
@@ -1520,18 +1570,6 @@ static int HIDAPI_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rum
return result;
}
static Uint32 HIDAPI_JoystickGetCapabilities(SDL_Joystick *joystick)
{
Uint32 result = 0;
SDL_HIDAPI_Device *device = NULL;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->GetJoystickCapabilities(device, joystick);
}
return result;
}
static int HIDAPI_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
int result;
@@ -1671,7 +1709,6 @@ SDL_JoystickDriver SDL_HIDAPI_JoystickDriver = {
HIDAPI_JoystickOpen,
HIDAPI_JoystickRumble,
HIDAPI_JoystickRumbleTriggers,
HIDAPI_JoystickGetCapabilities,
HIDAPI_JoystickSetLED,
HIDAPI_JoystickSendEffect,
HIDAPI_JoystickSetSensorsEnabled,

View File

@@ -40,6 +40,13 @@
#define SDL_JOYSTICK_HIDAPI_XBOXONE
#define SDL_JOYSTICK_HIDAPI_SHIELD
/* Joystick capability definitions */
#define SDL_JOYSTICK_CAP_MONO_LED 0x00000001
#define SDL_JOYSTICK_CAP_RGB_LED 0x00000002
#define SDL_JOYSTICK_CAP_PLAYER_LED 0x00000004
#define SDL_JOYSTICK_CAP_RUMBLE 0x00000010
#define SDL_JOYSTICK_CAP_TRIGGER_RUMBLE 0x00000020
/* Whether HIDAPI is enabled by default */
#define SDL_HIDAPI_DEFAULT SDL_TRUE
@@ -156,6 +163,7 @@ extern SDL_bool HIDAPI_HasConnectedUSBDevice(const char *serial);
extern void HIDAPI_DisconnectBluetoothDevice(const char *serial);
extern SDL_bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID);
extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID);
extern void HIDAPI_UpdateDeviceProperties(SDL_HIDAPI_Device *device);
extern void HIDAPI_DumpPacket(const char *prefix, const Uint8 *data, int size);