Browse Source

Set the pad lights on the PS5 controller corresponding to the player index

Also allow setting the player index from testgamecontroller using the number keys
Sam Lantinga 4 years ago
parent
commit
058a0ab47f
2 changed files with 32 additions and 4 deletions
  1. 24 4
      src/joystick/hidapi/SDL_hidapi_ps5.c
  2. 8 0
      test/testgamecontroller.c

+ 24 - 4
src/joystick/hidapi/SDL_hidapi_ps5.c

@@ -205,9 +205,9 @@ SetLedsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
         { 0x40, 0x00, 0x00 }, /* Red */
         { 0x00, 0x40, 0x00 }, /* Green */
         { 0x20, 0x00, 0x20 }, /* Pink */
-        { 0x02, 0x01, 0x00 }, /* Orange */
-        { 0x00, 0x01, 0x01 }, /* Teal */
-        { 0x01, 0x01, 0x01 }  /* White */
+        { 0x20, 0x10, 0x00 }, /* Orange */
+        { 0x00, 0x10, 0x10 }, /* Teal */
+        { 0x10, 0x10, 0x10 }  /* White */
     };
 
     if (player_index >= 0) {
@@ -221,6 +221,24 @@ SetLedsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
     effects->ucLedBlue = colors[player_index][2];
 }
 
+static void
+SetLightsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
+{
+    static const Uint8 lights[] = {
+        0x04,
+        0x0A,
+        0x15,
+        0x1B
+    };
+
+    if (player_index >= 0 && player_index < SDL_arraysize(lights)) {
+        /* Bitmask, 0x1F enables all lights, 0x20 changes instantly instead of fade */
+        effects->ucPadLights = lights[player_index];
+    } else {
+        effects->ucPadLights = 0x00;
+    }
+}
+
 static SDL_bool
 HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
 {
@@ -425,7 +443,7 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, EDS5Effect effect)
     case k_EDS5EffectPadLights:
         effects->ucEnableBits2 |= 0x10; /* Enable touchpad lights */
 
-        effects->ucPadLights = 0x00;    /* Bitmask, 0x1F enables all lights, 0x20 changes instantly instead of fade */
+        SetLightsForPlayerIndex(effects, ctx->player_index);
         break;
     case k_EDS5EffectMicLight:
         effects->ucEnableBits2 |= 0x01; /* Enable microphone light */
@@ -498,6 +516,7 @@ HIDAPI_DriverPS5_SetEffectsSupported(SDL_HIDAPI_Device *device, SDL_Joystick *jo
     SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL);
 
     HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
+    HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectPadLights);
 }
 
 static void
@@ -513,6 +532,7 @@ HIDAPI_DriverPS5_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID
 
     /* This will set the new LED state based on the new player index */
     HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
+    HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectPadLights);
 }
 
 static SDL_bool

+ 8 - 0
test/testgamecontroller.c

@@ -303,6 +303,14 @@ loop(void *arg)
             break;
 
         case SDL_KEYDOWN:
+            if (event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) {
+                if (gamecontroller) {
+                    int player_index = (event.key.keysym.sym - SDLK_0);
+
+                    SDL_GameControllerSetPlayerIndex(gamecontroller, player_index);
+                }
+                break;
+            }
             if (event.key.keysym.sym != SDLK_ESCAPE) {
                 break;
             }