Skip to content

Commit

Permalink
Add SDL_JoystickSetLED.
Browse files Browse the repository at this point in the history
Currently, this is only supported by the PS4 HIDAPI driver.
  • Loading branch information
flibitijibibo committed Apr 30, 2020
1 parent 1b8dee7 commit 83cddd2
Show file tree
Hide file tree
Showing 24 changed files with 229 additions and 1 deletion.
12 changes: 12 additions & 0 deletions include/SDL_gamecontroller.h
Expand Up @@ -404,6 +404,18 @@ extern DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *ga
*/
extern DECLSPEC int SDLCALL SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);

/**
* Update a controller's LED color.
*
* \param controller The joystick to update
* \param red The intensity of the red LED
* \param green The intensity of the green LED
* \param blue The intensity of the blue LED
*
* \return 0, or -1 if this controller does not have a modifiable LED
*/
extern DECLSPEC int SDLCALL SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 green, Uint8 blue);

/**
* Close a controller previously opened with SDL_GameControllerOpen().
*/
Expand Down
12 changes: 12 additions & 0 deletions include/SDL_joystick.h
Expand Up @@ -432,6 +432,18 @@ extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetButton(SDL_Joystick * joystick,
*/
extern DECLSPEC int SDLCALL SDL_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);

/**
* Update a joystick's LED color.
*
* \param joystick The joystick to update
* \param red The intensity of the red LED
* \param green The intensity of the green LED
* \param blue The intensity of the blue LED
*
* \return 0, or -1 if this joystick does not have a modifiable LED
*/
extern DECLSPEC int SDLCALL SDL_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue);

/**
* Close a joystick previously opened with SDL_JoystickOpen().
*/
Expand Down
6 changes: 6 additions & 0 deletions src/joystick/SDL_gamecontroller.c
Expand Up @@ -2107,6 +2107,12 @@ SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequenc
return SDL_JoystickRumble(SDL_GameControllerGetJoystick(gamecontroller), low_frequency_rumble, high_frequency_rumble, duration_ms);
}

int
SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_JoystickSetLED(SDL_GameControllerGetJoystick(gamecontroller), red, green, blue);
}

void
SDL_GameControllerClose(SDL_GameController * gamecontroller)
{
Expand Down
30 changes: 30 additions & 0 deletions src/joystick/SDL_joystick.c
Expand Up @@ -901,6 +901,36 @@ SDL_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16
return result;
}

int
SDL_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
{
int result;

if (!SDL_PrivateJoystickValid(joystick)) {
return -1;
}

SDL_LockJoysticks();

if (red == joystick->led_red &&
green == joystick->led_green &&
blue == joystick->led_blue) {
/* Avoid spamming the driver */
result = 0;
} else {
result = joystick->driver->SetLED(joystick, red, green, blue);
}

/* Save the LED value regardless of success, so we don't spam the driver */
joystick->led_red = red;
joystick->led_green = green;
joystick->led_blue = blue;

SDL_UnlockJoysticks();

return result;
}

/*
* Close a joystick previously opened with SDL_JoystickOpen()
*/
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/SDL_sysjoystick.h
Expand Up @@ -64,6 +64,10 @@ struct _SDL_Joystick
Uint16 high_frequency_rumble;
Uint32 rumble_expiration;

Uint8 led_red;
Uint8 led_green;
Uint8 led_blue;

SDL_bool attached;
SDL_bool is_game_controller;
SDL_bool delayed_guide_button; /* SDL_TRUE if this device has the guide button event delayed */
Expand Down Expand Up @@ -123,6 +127,9 @@ typedef struct _SDL_JoystickDriver
/* Rumble functionality */
int (*Rumble)(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);

/* LED functionality */
int (*SetLED)(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue);

/* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly,
* but instead should call SDL_PrivateJoystick*() to deliver events
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -633,6 +633,12 @@ ANDROID_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uin
return SDL_Unsupported();
}

static int
ANDROID_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

static void
ANDROID_JoystickUpdate(SDL_Joystick * joystick)
{
Expand Down Expand Up @@ -711,6 +717,7 @@ SDL_JoystickDriver SDL_ANDROID_JoystickDriver =
ANDROID_JoystickGetDeviceInstanceID,
ANDROID_JoystickOpen,
ANDROID_JoystickRumble,
ANDROID_JoystickSetLED,
ANDROID_JoystickUpdate,
ANDROID_JoystickClose,
ANDROID_JoystickQuit,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/bsd/SDL_sysjoystick.c
Expand Up @@ -768,6 +768,12 @@ BSD_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
return SDL_FALSE;
}

static int
BSD_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

SDL_JoystickDriver SDL_BSD_JoystickDriver =
{
BSD_JoystickInit,
Expand All @@ -780,6 +786,7 @@ SDL_JoystickDriver SDL_BSD_JoystickDriver =
BSD_JoystickGetDeviceInstanceID,
BSD_JoystickOpen,
BSD_JoystickRumble,
BSD_JoystickSetLED,
BSD_JoystickUpdate,
BSD_JoystickClose,
BSD_JoystickQuit,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/darwin/SDL_sysjoystick.c
Expand Up @@ -922,6 +922,12 @@ DARWIN_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint
return 0;
}

static int
DARWIN_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

static void
DARWIN_JoystickUpdate(SDL_Joystick * joystick)
{
Expand Down Expand Up @@ -1069,6 +1075,7 @@ SDL_JoystickDriver SDL_DARWIN_JoystickDriver =
DARWIN_JoystickGetDeviceInstanceID,
DARWIN_JoystickOpen,
DARWIN_JoystickRumble,
DARWIN_JoystickSetLED,
DARWIN_JoystickUpdate,
DARWIN_JoystickClose,
DARWIN_JoystickQuit,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/dummy/SDL_sysjoystick.c
Expand Up @@ -89,6 +89,12 @@ DUMMY_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint1
return SDL_Unsupported();
}

static int
DUMMY_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

static void
DUMMY_JoystickUpdate(SDL_Joystick * joystick)
{
Expand Down Expand Up @@ -122,6 +128,7 @@ SDL_JoystickDriver SDL_DUMMY_JoystickDriver =
DUMMY_JoystickGetDeviceInstanceID,
DUMMY_JoystickOpen,
DUMMY_JoystickRumble,
DUMMY_JoystickSetLED,
DUMMY_JoystickUpdate,
DUMMY_JoystickClose,
DUMMY_JoystickQuit,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/emscripten/SDL_sysjoystick.c
Expand Up @@ -409,6 +409,12 @@ EMSCRIPTEN_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
return SDL_FALSE;
}

static int
EMSCRIPTEN_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver =
{
EMSCRIPTEN_JoystickInit,
Expand All @@ -421,6 +427,7 @@ SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver =
EMSCRIPTEN_JoystickGetDeviceInstanceID,
EMSCRIPTEN_JoystickOpen,
EMSCRIPTEN_JoystickRumble,
EMSCRIPTEN_JoystickSetLED,
EMSCRIPTEN_JoystickUpdate,
EMSCRIPTEN_JoystickClose,
EMSCRIPTEN_JoystickQuit,
Expand Down
6 changes: 6 additions & 0 deletions src/joystick/haiku/SDL_haikujoystick.cc
Expand Up @@ -265,6 +265,11 @@ extern "C"
return SDL_FALSE;
}

static int HAIKU_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

SDL_JoystickDriver SDL_HAIKU_JoystickDriver =
{
HAIKU_JoystickInit,
Expand All @@ -277,6 +282,7 @@ extern "C"
HAIKU_JoystickGetDeviceInstanceID,
HAIKU_JoystickOpen,
HAIKU_JoystickRumble,
HAIKU_JoystickSetLED,
HAIKU_JoystickUpdate,
HAIKU_JoystickClose,
HAIKU_JoystickQuit,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/hidapi/SDL_hidapi_gamecube.c
Expand Up @@ -363,6 +363,12 @@ HIDAPI_DriverGameCube_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *jo
return -1;
}

static int
HIDAPI_DriverGameCube_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

static void
HIDAPI_DriverGameCube_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
Expand Down Expand Up @@ -402,6 +408,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube =
HIDAPI_DriverGameCube_UpdateDevice,
HIDAPI_DriverGameCube_OpenJoystick,
HIDAPI_DriverGameCube_RumbleJoystick,
HIDAPI_DriverGameCube_SetJoystickLED,
HIDAPI_DriverGameCube_CloseJoystick,
HIDAPI_DriverGameCube_FreeDevice,
NULL,
Expand Down
31 changes: 30 additions & 1 deletion src/joystick/hidapi/SDL_hidapi_ps4.c
Expand Up @@ -100,6 +100,12 @@ typedef struct {
SDL_bool audio_supported;
SDL_bool rumble_supported;
int player_index;
Uint16 rumble_left;
Uint16 rumble_right;
Uint8 color_set;
Uint8 led_red;
Uint8 led_green;
Uint8 led_blue;
Uint8 volume;
Uint32 last_volume_check;
PS4StatePacket_t last_state;
Expand Down Expand Up @@ -330,11 +336,19 @@ HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
}
effects = (DS4EffectsState_t *)&data[offset];

ctx->rumble_left = low_frequency_rumble;
ctx->rumble_right = high_frequency_rumble;
effects->ucRumbleLeft = (low_frequency_rumble >> 8);
effects->ucRumbleRight = (high_frequency_rumble >> 8);

/* Populate the LED state with the appropriate color from our lookup table */
SetLedsForPlayerIndex(effects, ctx->player_index);
if (ctx->color_set) {
effects->ucLedRed = ctx->led_red;
effects->ucLedGreen = ctx->led_green;
effects->ucLedBlue = ctx->led_blue;
} else {
SetLedsForPlayerIndex(effects, ctx->player_index);
}

if (ctx->is_bluetooth) {
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
Expand All @@ -351,6 +365,20 @@ HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
return 0;
}

static int
HIDAPI_DriverPS4_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;

ctx->color_set = 1;
ctx->led_red = red;
ctx->led_green = green;
ctx->led_blue = blue;

/* FIXME: Is there a better way to send this without sending another rumble packet? */
return HIDAPI_DriverPS4_RumbleJoystick(device, joystick, ctx->rumble_left, ctx->rumble_right);
}

static void
HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS4_Context *ctx, PS4StatePacket_t *packet)
{
Expand Down Expand Up @@ -536,6 +564,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4 =
HIDAPI_DriverPS4_UpdateDevice,
HIDAPI_DriverPS4_OpenJoystick,
HIDAPI_DriverPS4_RumbleJoystick,
HIDAPI_DriverPS4_SetJoystickLED,
HIDAPI_DriverPS4_CloseJoystick,
HIDAPI_DriverPS4_FreeDevice,
NULL
Expand Down
8 changes: 8 additions & 0 deletions src/joystick/hidapi/SDL_hidapi_steam.c
Expand Up @@ -1034,6 +1034,13 @@ HIDAPI_DriverSteam_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joyst
return SDL_Unsupported();
}

static int
HIDAPI_DriverSteam_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
/* You should use the full Steam Input API for LED support */
return SDL_Unsupported();
}

static SDL_bool
HIDAPI_DriverSteam_UpdateDevice(SDL_HIDAPI_Device *device)
{
Expand Down Expand Up @@ -1164,6 +1171,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam =
HIDAPI_DriverSteam_UpdateDevice,
HIDAPI_DriverSteam_OpenJoystick,
HIDAPI_DriverSteam_RumbleJoystick,
HIDAPI_DriverSteam_SetJoystickLED,
HIDAPI_DriverSteam_CloseJoystick,
HIDAPI_DriverSteam_FreeDevice,
NULL
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/hidapi/SDL_hidapi_switch.c
Expand Up @@ -920,6 +920,12 @@ HIDAPI_DriverSwitch_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joys
return HIDAPI_DriverSwitch_ActuallyRumbleJoystick(ctx, low_frequency_rumble, high_frequency_rumble);
}

static int
HIDAPI_DriverSwitch_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

static void HandleInputOnlyControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchInputOnlyControllerStatePacket_t *packet)
{
Sint16 axis;
Expand Down Expand Up @@ -1267,6 +1273,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch =
HIDAPI_DriverSwitch_UpdateDevice,
HIDAPI_DriverSwitch_OpenJoystick,
HIDAPI_DriverSwitch_RumbleJoystick,
HIDAPI_DriverSwitch_SetJoystickLED,
HIDAPI_DriverSwitch_CloseJoystick,
HIDAPI_DriverSwitch_FreeDevice,
NULL
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/hidapi/SDL_hidapi_xbox360.c
Expand Up @@ -796,6 +796,12 @@ HIDAPI_DriverXbox360_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joy
return 0;
}

static int
HIDAPI_DriverXbox360_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}

#ifdef __WIN32__
/* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
however with this interface there is no rumble support, no guide button,
Expand Down Expand Up @@ -1302,6 +1308,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
HIDAPI_DriverXbox360_UpdateDevice,
HIDAPI_DriverXbox360_OpenJoystick,
HIDAPI_DriverXbox360_RumbleJoystick,
HIDAPI_DriverXbox360_SetJoystickLED,
HIDAPI_DriverXbox360_CloseJoystick,
HIDAPI_DriverXbox360_FreeDevice,
HIDAPI_DriverXbox360_PostUpdate,
Expand Down

0 comments on commit 83cddd2

Please sign in to comment.