Skip to content

Commit

Permalink
Added a hint to control whether the player LEDs should be lit to indi…
Browse files Browse the repository at this point in the history
…cate which player is associated with a PS5 controller.
  • Loading branch information
slouken committed Dec 29, 2020
1 parent 6116ccf commit 0684572
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 9 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -645,6 +645,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.
*
Expand Down
25 changes: 24 additions & 1 deletion src/joystick/hidapi/SDL_hidapi_ps5.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 0684572

Please sign in to comment.