Parcourir la 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 il y a 5 ans
Parent
commit
058a0ab47f
2 fichiers modifiés avec 32 ajouts et 4 suppressions
  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 */
         { 0x40, 0x00, 0x00 }, /* Red */
         { 0x00, 0x40, 0x00 }, /* Green */
         { 0x00, 0x40, 0x00 }, /* Green */
         { 0x20, 0x00, 0x20 }, /* Pink */
         { 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) {
     if (player_index >= 0) {
@@ -221,6 +221,24 @@ SetLedsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
     effects->ucLedBlue = colors[player_index][2];
     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
 static SDL_bool
 HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
 HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
 {
 {
@@ -425,7 +443,7 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, EDS5Effect effect)
     case k_EDS5EffectPadLights:
     case k_EDS5EffectPadLights:
         effects->ucEnableBits2 |= 0x10; /* Enable touchpad lights */
         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;
         break;
     case k_EDS5EffectMicLight:
     case k_EDS5EffectMicLight:
         effects->ucEnableBits2 |= 0x01; /* Enable microphone light */
         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);
     SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL);
 
 
     HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
     HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
+    HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectPadLights);
 }
 }
 
 
 static void
 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 */
     /* This will set the new LED state based on the new player index */
     HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
     HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
+    HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectPadLights);
 }
 }
 
 
 static SDL_bool
 static SDL_bool

+ 8 - 0
test/testgamecontroller.c

@@ -303,6 +303,14 @@ loop(void *arg)
             break;
             break;
 
 
         case SDL_KEYDOWN:
         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) {
             if (event.key.keysym.sym != SDLK_ESCAPE) {
                 break;
                 break;
             }
             }