Added support for indicating player index on DS4 controllers
authorCameron Gutman <aicommander@gmail.com>
Sat, 07 Mar 2020 13:17:28 -0800
changeset 13593ea9492fc2eae
parent 13592 3ff45857428d
child 13594 9b5e16574ea9
Added support for indicating player index on DS4 controllers
src/joystick/hidapi/SDL_hidapi_ps4.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_ps4.c	Sat Mar 07 08:48:04 2020 -0800
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_ps4.c	Sat Mar 07 13:17:28 2020 -0800
     1.3 @@ -100,6 +100,7 @@
     1.4      SDL_bool is_bluetooth;
     1.5      SDL_bool audio_supported;
     1.6      SDL_bool rumble_supported;
     1.7 +    int player_index;
     1.8      Uint8 volume;
     1.9      Uint32 last_volume_check;
    1.10      PS4StatePacket_t last_state;
    1.11 @@ -182,6 +183,33 @@
    1.12      return SDL_TRUE;
    1.13  }
    1.14  
    1.15 +static void
    1.16 +SetLedsForPlayerIndex(DS4EffectsState_t *effects, int player_index)
    1.17 +{
    1.18 +    /* This list is the same as what hid-sony.c uses in the Linux kernel.
    1.19 +       The first 4 values correspond to what the PS4 assigns.
    1.20 +    */
    1.21 +    static const Uint8 colors[7][3] = {
    1.22 +        { 0x00, 0x00, 0x40 }, /* Blue */
    1.23 +        { 0x40, 0x00, 0x00 }, /* Red */
    1.24 +        { 0x00, 0x40, 0x00 }, /* Green */
    1.25 +        { 0x20, 0x00, 0x20 }, /* Pink */
    1.26 +        { 0x02, 0x01, 0x00 }, /* Orange */
    1.27 +        { 0x00, 0x01, 0x01 }, /* Teal */
    1.28 +        { 0x01, 0x01, 0x01 }  /* White */
    1.29 +    };
    1.30 +
    1.31 +    if (player_index >= 0) {
    1.32 +        player_index %= SDL_arraysize(colors);
    1.33 +    } else {
    1.34 +        player_index = 0;
    1.35 +    }
    1.36 +
    1.37 +    effects->ucLedRed = colors[player_index][0];
    1.38 +    effects->ucLedGreen = colors[player_index][1];
    1.39 +    effects->ucLedBlue = colors[player_index][2];
    1.40 +}
    1.41 +
    1.42  static SDL_bool
    1.43  HIDAPI_DriverPS4_InitDevice(SDL_HIDAPI_Device *device)
    1.44  {
    1.45 @@ -194,12 +222,22 @@
    1.46      return -1;
    1.47  }
    1.48  
    1.49 +static int HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
    1.50 +
    1.51  static void
    1.52  HIDAPI_DriverPS4_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
    1.53  {
    1.54 -}
    1.55 +    SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
    1.56 +
    1.57 +    if (!ctx) {
    1.58 +        return;
    1.59 +    }
    1.60  
    1.61 -static int HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
    1.62 +    ctx->player_index = player_index;
    1.63 +
    1.64 +    /* This will set the new LED state based on the new player index */
    1.65 +    HIDAPI_DriverPS4_RumbleJoystick(device, SDL_JoystickFromInstanceID(instance_id), 0, 0);
    1.66 +}
    1.67  
    1.68  static SDL_bool
    1.69  HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
    1.70 @@ -248,6 +286,9 @@
    1.71          }
    1.72      }
    1.73  
    1.74 +    /* Initialize player index (needed for setting LEDs) */
    1.75 +    ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
    1.76 +
    1.77      /* Initialize LED and effect state */
    1.78      HIDAPI_DriverPS4_RumbleJoystick(device, joystick, 0, 0);
    1.79  
    1.80 @@ -293,9 +334,8 @@
    1.81      effects->ucRumbleLeft = (low_frequency_rumble >> 8);
    1.82      effects->ucRumbleRight = (high_frequency_rumble >> 8);
    1.83  
    1.84 -    effects->ucLedRed = 0;
    1.85 -    effects->ucLedGreen = 0;
    1.86 -    effects->ucLedBlue = 80;
    1.87 +    /* Populate the LED state with the appropriate color from our lookup table */
    1.88 +    SetLedsForPlayerIndex(effects, ctx->player_index);
    1.89  
    1.90      if (ctx->is_bluetooth) {
    1.91          /* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */