Commit 0684572ccc17db9b49ecf32842fce17592fba27f

Sam Lantinga 2020-12-29T12:13:10

Added a hint to control whether the player LEDs should be lit to indicate which player is associated with a PS5 controller.

diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 80b572b..a20fe6d 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -646,6 +646,15 @@ extern "C" {
 #define SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE "SDL_JOYSTICK_HIDAPI_PS5_RUMBLE"
 
 /**
+ *  \brief  A variable controlling whether the player LEDs should be lit to indicate which player is associated with a PS5 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_PS5_PLAYER_LED "SDL_JOYSTICK_HIDAPI_PS5_PLAYER_LED"
+
+/**
  *  \brief  A variable controlling whether the HIDAPI driver for Steam Controllers should be used.
  *
  *  This variable can be set to the following values:
diff --git a/src/joystick/hidapi/SDL_hidapi_ps5.c b/src/joystick/hidapi/SDL_hidapi_ps5.c
index 3f21740..0248c86 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps5.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps5.c
@@ -159,6 +159,7 @@ typedef struct {
     IMUCalibrationData calibration[6];
     Uint32 last_packet;
     int player_index;
+    SDL_bool player_lights;
     Uint8 rumble_left;
     Uint8 rumble_right;
     SDL_bool color_set;
@@ -445,7 +446,11 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, int effect_mask)
     if ((effect_mask & k_EDS5EffectPadLights) != 0) {
         effects->ucEnableBits2 |= 0x10; /* Enable touchpad lights */
 
-        SetLightsForPlayerIndex(effects, ctx->player_index);
+        if (ctx->player_lights) {
+            SetLightsForPlayerIndex(effects, ctx->player_index);
+        } else {
+            effects->ucPadLights = 0x00;
+        }
     }
     if ((effect_mask & k_EDS5EffectMicLight) != 0) {
         effects->ucEnableBits2 |= 0x01; /* Enable microphone light */
@@ -547,6 +552,18 @@ static void SDLCALL SDL_PS5RumbleHintChanged(void *userdata, const char *name, c
     }
 }
 
+static void SDLCALL SDL_PS5PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
+{
+    SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)userdata;
+    SDL_bool player_lights = SDL_GetStringBoolean(hint, SDL_TRUE);
+
+    if (player_lights != ctx->player_lights) {
+        ctx->player_lights = player_lights;
+
+        HIDAPI_DriverPS5_UpdateEffects(ctx->device, k_EDS5EffectPadLights);
+    }
+}
+
 static void
 HIDAPI_DriverPS5_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
 {
@@ -641,6 +658,7 @@ HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
 
     /* Initialize player index (needed for setting LEDs) */
     ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
+    ctx->player_lights = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED, SDL_TRUE);
 
     /* Initialize the joystick capabilities
      *
@@ -656,6 +674,8 @@ HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
         SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
                             SDL_PS5RumbleHintChanged, ctx);
     }
+    SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
+                        SDL_PS5PlayerLEDHintChanged, ctx);
     return SDL_TRUE;
 }
 
@@ -1022,6 +1042,9 @@ HIDAPI_DriverPS5_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick
     SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
                         SDL_PS5RumbleHintChanged, ctx);
 
+    SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
+                        SDL_PS5PlayerLEDHintChanged, ctx);
+
     hid_close(device->dev);
     device->dev = NULL;