Added the hint SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED to control whether the player LED is set on Nintendo Switch controllers
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 368cb0a..9c11eb1 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -821,6 +821,15 @@ extern "C" {
#define SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED "SDL_JOYSTICK_HIDAPI_SWITCH_HOME_LED"
/**
+ * \brief A variable controlling whether the player LEDs should be lit to indicate which player is associated with a Nintendo Switch controller.
+ *
+ * This variable can be set to the following values:
+ * "0" - player LEDs are not enabled
+ * "1" - player LEDs are enabled (the default)
+ */
+#define SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED "SDL_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED"
+
+/**
* \brief A variable controlling whether the HIDAPI driver for XBox controllers should be used.
*
* This variable can be set to the following values:
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index c1ef34d..9b9be14 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -253,6 +253,8 @@ typedef struct {
SDL_bool m_bUsingBluetooth;
SDL_bool m_bIsGameCube;
SDL_bool m_bUseButtonLabels;
+ SDL_bool m_bPlayerLights;
+ int m_nPlayerIndex;
SDL_bool m_bSyncWrite;
int m_nMaxWriteAttempts;
ESwitchDeviceInfoControllerType m_eControllerType;
@@ -835,10 +837,26 @@ static void SDLCALL SDL_HomeLEDHintChanged(void *userdata, const char *name, con
}
}
-static SDL_bool SetSlotLED(SDL_DriverSwitch_Context *ctx, Uint8 slot)
+static void UpdateSlotLED(SDL_DriverSwitch_Context *ctx)
{
- Uint8 led_data = (1 << slot);
- return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetPlayerLights, &led_data, sizeof(led_data), NULL);
+ if (!ctx->m_bInputOnly) {
+ Uint8 slot = (ctx->m_nPlayerIndex % 4);
+ Uint8 led_data = ctx->m_bPlayerLights ? (1 << slot) : 0;
+
+ WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetPlayerLights, &led_data, sizeof(led_data), NULL);
+ }
+}
+
+static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
+{
+ SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)userdata;
+ SDL_bool bPlayerLights = SDL_GetStringBoolean(hint, SDL_TRUE);
+
+ if (bPlayerLights != ctx->m_bPlayerLights) {
+ ctx->m_bPlayerLights = bPlayerLights;
+
+ UpdateSlotLED(ctx);
+ }
}
static SDL_bool SetIMUEnabled(SDL_DriverSwitch_Context* ctx, SDL_bool enabled)
@@ -1171,6 +1189,15 @@ HIDAPI_DriverSwitch_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_Joystick
static void
HIDAPI_DriverSwitch_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
{
+ SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
+
+ if (!ctx) {
+ return;
+ }
+
+ ctx->m_nPlayerIndex = player_index;
+
+ UpdateSlotLED(ctx);
}
static SDL_bool
@@ -1281,7 +1308,6 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED,
SDL_HomeLEDHintChanged, ctx);
}
- SetSlotLED(ctx, (joystick->instance_id % 4));
/* Set the serial number */
{
@@ -1313,6 +1339,14 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
SDL_GameControllerButtonReportingHintChanged, ctx);
}
+ /* Initialize player index (needed for setting LEDs) */
+ ctx->m_nPlayerIndex = SDL_JoystickGetPlayerIndex(joystick);
+ ctx->m_bPlayerLights = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED, SDL_TRUE);
+ UpdateSlotLED(ctx);
+
+ SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED,
+ SDL_PlayerLEDHintChanged, ctx);
+
/* Initialize the joystick capabilities */
joystick->nbuttons = 16;
joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
@@ -2013,6 +2047,9 @@ HIDAPI_DriverSwitch_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joyst
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED,
SDL_HomeLEDHintChanged, ctx);
+ SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED,
+ SDL_PlayerLEDHintChanged, ctx);
+
SDL_LockMutex(device->dev_lock);
{
SDL_hid_close(device->dev);