Added support for indicating player index on DS4 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
diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c
index c2e590a..8288d1f 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps4.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps4.c
@@ -100,6 +100,7 @@ typedef struct {
SDL_bool is_bluetooth;
SDL_bool audio_supported;
SDL_bool rumble_supported;
+ int player_index;
Uint8 volume;
Uint32 last_volume_check;
PS4StatePacket_t last_state;
@@ -182,6 +183,33 @@ static SDL_bool HIDAPI_DriverPS4_CanRumble(Uint16 vendor_id, Uint16 product_id)
return SDL_TRUE;
}
+static void
+SetLedsForPlayerIndex(DS4EffectsState_t *effects, int player_index)
+{
+ /* This list is the same as what hid-sony.c uses in the Linux kernel.
+ The first 4 values correspond to what the PS4 assigns.
+ */
+ static const Uint8 colors[7][3] = {
+ { 0x00, 0x00, 0x40 }, /* Blue */
+ { 0x40, 0x00, 0x00 }, /* Red */
+ { 0x00, 0x40, 0x00 }, /* Green */
+ { 0x20, 0x00, 0x20 }, /* Pink */
+ { 0x02, 0x01, 0x00 }, /* Orange */
+ { 0x00, 0x01, 0x01 }, /* Teal */
+ { 0x01, 0x01, 0x01 } /* White */
+ };
+
+ if (player_index >= 0) {
+ player_index %= SDL_arraysize(colors);
+ } else {
+ player_index = 0;
+ }
+
+ effects->ucLedRed = colors[player_index][0];
+ effects->ucLedGreen = colors[player_index][1];
+ effects->ucLedBlue = colors[player_index][2];
+}
+
static SDL_bool
HIDAPI_DriverPS4_InitDevice(SDL_HIDAPI_Device *device)
{
@@ -194,12 +222,22 @@ HIDAPI_DriverPS4_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID
return -1;
}
+static int HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
+
static void
HIDAPI_DriverPS4_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
{
-}
+ SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
-static int HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
+ if (!ctx) {
+ return;
+ }
+
+ ctx->player_index = player_index;
+
+ /* This will set the new LED state based on the new player index */
+ HIDAPI_DriverPS4_RumbleJoystick(device, SDL_JoystickFromInstanceID(instance_id), 0, 0);
+}
static SDL_bool
HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
@@ -248,6 +286,9 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
}
}
+ /* Initialize player index (needed for setting LEDs) */
+ ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
+
/* Initialize LED and effect state */
HIDAPI_DriverPS4_RumbleJoystick(device, joystick, 0, 0);
@@ -293,9 +334,8 @@ HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
effects->ucRumbleLeft = (low_frequency_rumble >> 8);
effects->ucRumbleRight = (high_frequency_rumble >> 8);
- effects->ucLedRed = 0;
- effects->ucLedGreen = 0;
- effects->ucLedBlue = 80;
+ /* Populate the LED state with the appropriate color from our lookup table */
+ SetLedsForPlayerIndex(effects, ctx->player_index);
if (ctx->is_bluetooth) {
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */