Allow floating point values for SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED This allows setting the brightness of the home LED on Nintendo Switch Pro controllers, in the range 0.0 - 1.0. This can be updated at runtime by setting the hint dynamically. Fixes https://github.com/libsdl-org/SDL/issues/3787
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index 3fbd91e..061f3b3 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -745,6 +745,24 @@ static SDL_bool SetHomeLED(SDL_DriverSwitch_Context *ctx, Uint8 brightness)
return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetHomeLight, rgucBuffer, sizeof(rgucBuffer), NULL);
}
+static void SDLCALL SDL_HomeLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
+{
+ SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)userdata;
+
+ if (hint && *hint) {
+ int value;
+
+ if (SDL_strchr(hint, '.') != NULL) {
+ value = (int)(100.0f * SDL_atof(hint));
+ } else if (SDL_GetStringBoolean(hint, SDL_TRUE)) {
+ value = 100;
+ } else {
+ value = 0;
+ }
+ SetHomeLED(ctx, value);
+ }
+}
+
static SDL_bool SetSlotLED(SDL_DriverSwitch_Context *ctx, Uint8 slot)
{
Uint8 led_data = (1 << slot);
@@ -1066,14 +1084,8 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
/* Set the LED state */
if (ctx->m_bHasHomeLED) {
- const char *hint = SDL_GetHint(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED);
- if (hint && *hint) {
- if (SDL_GetStringBoolean(hint, SDL_TRUE)) {
- SetHomeLED(ctx, 100);
- } else {
- SetHomeLED(ctx, 0);
- }
- }
+ SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED,
+ SDL_HomeLEDHintChanged, ctx);
}
SetSlotLED(ctx, (joystick->instance_id % 4));
@@ -1654,6 +1666,9 @@ HIDAPI_DriverSwitch_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joyst
SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
SDL_GameControllerButtonReportingHintChanged, ctx);
+ SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED,
+ SDL_HomeLEDHintChanged, ctx);
+
SDL_LockMutex(device->dev_lock);
{
SDL_hid_close(device->dev);