Added SDL_GameControllerSendEffect() and SDL_JoystickSendEffect() to allow applications to send custom effects to the PS4 and PS5 controllers

See testgamecontroller.c for an example of a custom PS5 trigger effect
This commit is contained in:
Sam Lantinga 2021-07-08 13:22:41 -07:00
parent 073cbc40cb
commit d135c0762f
36 changed files with 533 additions and 186 deletions

View File

@ -820,6 +820,16 @@ extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasLED(SDL_GameController *ga
*/ */
extern DECLSPEC int SDLCALL SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 green, Uint8 blue); extern DECLSPEC int SDLCALL SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 green, Uint8 blue);
/**
* Send a controller specific effect packet
*
* \param gamecontroller The controller to affect
* \param data The data to send to the controller
* \param size The size of the data to send to the controller
* \returns 0, or -1 if this controller or driver doesn't support effect packets
*/
extern DECLSPEC int SDLCALL SDL_GameControllerSendEffect(SDL_GameController *gamecontroller, const void *data, int size);
/** /**
* Close a game controller previously opened with SDL_GameControllerOpen(). * Close a game controller previously opened with SDL_GameControllerOpen().
* *

View File

@ -782,6 +782,16 @@ extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasLED(SDL_Joystick *joystick);
*/ */
extern DECLSPEC int SDLCALL SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue); extern DECLSPEC int SDLCALL SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
/**
* Send a joystick specific effect packet
*
* \param joystick The joystick to affect
* \param data The data to send to the joystick
* \param size The size of the data to send to the joystick
* \returns 0, or -1 if this joystick or driver doesn't support effect packets
*/
extern DECLSPEC int SDLCALL SDL_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size);
/** /**
* Close a joystick previously opened with SDL_JoystickOpen(). * Close a joystick previously opened with SDL_JoystickOpen().
* *

View File

@ -812,3 +812,5 @@
#define SDL_TLSCleanup SDL_TLSCleanup_REAL #define SDL_TLSCleanup SDL_TLSCleanup_REAL
#define SDL_SetWindowAlwaysOnTop SDL_SetWindowAlwaysOnTop_REAL #define SDL_SetWindowAlwaysOnTop SDL_SetWindowAlwaysOnTop_REAL
#define SDL_FlashWindow SDL_FlashWindow_REAL #define SDL_FlashWindow SDL_FlashWindow_REAL
#define SDL_GameControllerSendEffect SDL_GameControllerSendEffect_REAL
#define SDL_JoystickSendEffect SDL_JoystickSendEffect_REAL

View File

@ -877,3 +877,5 @@ SDL_DYNAPI_PROC(int,SDL_GetAudioDeviceSpec,(int a, int b, SDL_AudioSpec *c),(a,b
SDL_DYNAPI_PROC(void,SDL_TLSCleanup,(void),(),) SDL_DYNAPI_PROC(void,SDL_TLSCleanup,(void),(),)
SDL_DYNAPI_PROC(void,SDL_SetWindowAlwaysOnTop,(SDL_Window *a, SDL_bool b),(a,b),) SDL_DYNAPI_PROC(void,SDL_SetWindowAlwaysOnTop,(SDL_Window *a, SDL_bool b),(a,b),)
SDL_DYNAPI_PROC(int,SDL_FlashWindow,(SDL_Window *a, Uint32 b),(a, b),return) SDL_DYNAPI_PROC(int,SDL_FlashWindow,(SDL_Window *a, Uint32 b),(a, b),return)
SDL_DYNAPI_PROC(int,SDL_GameControllerSendEffect,(SDL_GameController *a, const void *b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_JoystickSendEffect,(SDL_Joystick *a, const void *b, int c),(a,b,c),return)

View File

@ -2411,6 +2411,12 @@ SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 gr
return SDL_JoystickSetLED(SDL_GameControllerGetJoystick(gamecontroller), red, green, blue); return SDL_JoystickSetLED(SDL_GameControllerGetJoystick(gamecontroller), red, green, blue);
} }
int
SDL_GameControllerSendEffect(SDL_GameController *gamecontroller, const void *data, int size)
{
return SDL_JoystickSendEffect(SDL_GameControllerGetJoystick(gamecontroller), data, size);
}
void void
SDL_GameControllerClose(SDL_GameController *gamecontroller) SDL_GameControllerClose(SDL_GameController *gamecontroller)
{ {

View File

@ -986,6 +986,24 @@ SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
return result; return result;
} }
int
SDL_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
int result;
if (!SDL_PrivateJoystickValid(joystick)) {
return -1;
}
SDL_LockJoysticks();
result = joystick->driver->SendEffect(joystick, data, size);
SDL_UnlockJoysticks();
return result;
}
/* /*
* Close a joystick previously opened with SDL_JoystickOpen() * Close a joystick previously opened with SDL_JoystickOpen()
*/ */

View File

@ -166,6 +166,9 @@ typedef struct _SDL_JoystickDriver
SDL_bool (*HasLED)(SDL_Joystick *joystick); SDL_bool (*HasLED)(SDL_Joystick *joystick);
int (*SetLED)(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue); int (*SetLED)(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
/* General effects */
int (*SendEffect)(SDL_Joystick *joystick, const void *data, int size);
/* Sensor functionality */ /* Sensor functionality */
int (*SetSensorsEnabled)(SDL_Joystick *joystick, SDL_bool enabled); int (*SetSensorsEnabled)(SDL_Joystick *joystick, SDL_bool enabled);

View File

@ -628,6 +628,12 @@ ANDROID_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 bl
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
ANDROID_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
ANDROID_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) ANDROID_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -715,6 +721,7 @@ SDL_JoystickDriver SDL_ANDROID_JoystickDriver =
ANDROID_JoystickRumbleTriggers, ANDROID_JoystickRumbleTriggers,
ANDROID_JoystickHasLED, ANDROID_JoystickHasLED,
ANDROID_JoystickSetLED, ANDROID_JoystickSetLED,
ANDROID_JoystickSendEffect,
ANDROID_JoystickSetSensorsEnabled, ANDROID_JoystickSetSensorsEnabled,
ANDROID_JoystickUpdate, ANDROID_JoystickUpdate,
ANDROID_JoystickClose, ANDROID_JoystickClose,

View File

@ -789,6 +789,12 @@ BSD_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
BSD_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
BSD_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) BSD_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -810,6 +816,7 @@ SDL_JoystickDriver SDL_BSD_JoystickDriver =
BSD_JoystickRumbleTriggers, BSD_JoystickRumbleTriggers,
BSD_JoystickHasLED, BSD_JoystickHasLED,
BSD_JoystickSetLED, BSD_JoystickSetLED,
BSD_JoystickSendEffect,
BSD_JoystickSetSensorsEnabled, BSD_JoystickSetSensorsEnabled,
BSD_JoystickUpdate, BSD_JoystickUpdate,
BSD_JoystickClose, BSD_JoystickClose,

View File

@ -951,6 +951,12 @@ DARWIN_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blu
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
DARWIN_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
DARWIN_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) DARWIN_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1107,6 +1113,7 @@ SDL_JoystickDriver SDL_DARWIN_JoystickDriver =
DARWIN_JoystickRumbleTriggers, DARWIN_JoystickRumbleTriggers,
DARWIN_JoystickHasLED, DARWIN_JoystickHasLED,
DARWIN_JoystickSetLED, DARWIN_JoystickSetLED,
DARWIN_JoystickSendEffect,
DARWIN_JoystickSetSensorsEnabled, DARWIN_JoystickSetSensorsEnabled,
DARWIN_JoystickUpdate, DARWIN_JoystickUpdate,
DARWIN_JoystickClose, DARWIN_JoystickClose,

View File

@ -107,6 +107,12 @@ DUMMY_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
DUMMY_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
DUMMY_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) DUMMY_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -149,6 +155,7 @@ SDL_JoystickDriver SDL_DUMMY_JoystickDriver =
DUMMY_JoystickRumbleTriggers, DUMMY_JoystickRumbleTriggers,
DUMMY_JoystickHasLED, DUMMY_JoystickHasLED,
DUMMY_JoystickSetLED, DUMMY_JoystickSetLED,
DUMMY_JoystickSendEffect,
DUMMY_JoystickSetSensorsEnabled, DUMMY_JoystickSetSensorsEnabled,
DUMMY_JoystickUpdate, DUMMY_JoystickUpdate,
DUMMY_JoystickClose, DUMMY_JoystickClose,

View File

@ -426,6 +426,12 @@ EMSCRIPTEN_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
EMSCRIPTEN_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
EMSCRIPTEN_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) EMSCRIPTEN_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -447,6 +453,7 @@ SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver =
EMSCRIPTEN_JoystickRumbleTriggers, EMSCRIPTEN_JoystickRumbleTriggers,
EMSCRIPTEN_JoystickHasLED, EMSCRIPTEN_JoystickHasLED,
EMSCRIPTEN_JoystickSetLED, EMSCRIPTEN_JoystickSetLED,
EMSCRIPTEN_JoystickSendEffect,
EMSCRIPTEN_JoystickSetSensorsEnabled, EMSCRIPTEN_JoystickSetSensorsEnabled,
EMSCRIPTEN_JoystickUpdate, EMSCRIPTEN_JoystickUpdate,
EMSCRIPTEN_JoystickClose, EMSCRIPTEN_JoystickClose,

View File

@ -281,6 +281,12 @@ extern "C"
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int HAIKU_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int HAIKU_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) static int HAIKU_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
return SDL_Unsupported(); return SDL_Unsupported();
@ -301,6 +307,7 @@ extern "C"
HAIKU_JoystickRumbleTriggers, HAIKU_JoystickRumbleTriggers,
HAIKU_JoystickHasLED, HAIKU_JoystickHasLED,
HAIKU_JoystickSetLED, HAIKU_JoystickSetLED,
HAIKU_JoystickSendEffect,
HAIKU_JoystickSetSensorsEnabled, HAIKU_JoystickSetSensorsEnabled,
HAIKU_JoystickUpdate, HAIKU_JoystickUpdate,
HAIKU_JoystickClose, HAIKU_JoystickClose,

View File

@ -476,6 +476,12 @@ HIDAPI_DriverGameCube_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *jo
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
HIDAPI_DriverGameCube_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
HIDAPI_DriverGameCube_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverGameCube_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -528,6 +534,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube =
HIDAPI_DriverGameCube_RumbleJoystickTriggers, HIDAPI_DriverGameCube_RumbleJoystickTriggers,
HIDAPI_DriverGameCube_HasJoystickLED, HIDAPI_DriverGameCube_HasJoystickLED,
HIDAPI_DriverGameCube_SetJoystickLED, HIDAPI_DriverGameCube_SetJoystickLED,
HIDAPI_DriverGameCube_SendJoystickEffect,
HIDAPI_DriverGameCube_SetJoystickSensorsEnabled, HIDAPI_DriverGameCube_SetJoystickSensorsEnabled,
HIDAPI_DriverGameCube_CloseJoystick, HIDAPI_DriverGameCube_CloseJoystick,
HIDAPI_DriverGameCube_FreeDevice, HIDAPI_DriverGameCube_FreeDevice,

View File

@ -148,6 +148,12 @@ HIDAPI_DriverLuna_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
HIDAPI_DriverLuna_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
HIDAPI_DriverLuna_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverLuna_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -441,6 +447,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverLuna =
HIDAPI_DriverLuna_RumbleJoystickTriggers, HIDAPI_DriverLuna_RumbleJoystickTriggers,
HIDAPI_DriverLuna_HasJoystickLED, HIDAPI_DriverLuna_HasJoystickLED,
HIDAPI_DriverLuna_SetJoystickLED, HIDAPI_DriverLuna_SetJoystickLED,
HIDAPI_DriverLuna_SendJoystickEffect,
HIDAPI_DriverLuna_SetJoystickSensorsEnabled, HIDAPI_DriverLuna_SetJoystickSensorsEnabled,
HIDAPI_DriverLuna_CloseJoystick, HIDAPI_DriverLuna_CloseJoystick,
HIDAPI_DriverLuna_FreeDevice, HIDAPI_DriverLuna_FreeDevice,

View File

@ -146,6 +146,8 @@ typedef struct {
} SDL_DriverPS4_Context; } SDL_DriverPS4_Context;
static int HIDAPI_DriverPS4_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size);
static SDL_bool static SDL_bool
HIDAPI_DriverPS4_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol) HIDAPI_DriverPS4_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
{ {
@ -385,61 +387,26 @@ static int
HIDAPI_DriverPS4_UpdateEffects(SDL_HIDAPI_Device *device) HIDAPI_DriverPS4_UpdateEffects(SDL_HIDAPI_Device *device)
{ {
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context; SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
DS4EffectsState_t *effects; DS4EffectsState_t effects;
Uint8 data[78];
int report_size, offset;
if (!ctx->effects_supported) {
return SDL_Unsupported();
}
if (!ctx->enhanced_mode) { if (!ctx->enhanced_mode) {
return SDL_Unsupported(); return SDL_Unsupported();
} }
SDL_zero(data); SDL_zero(effects);
if (ctx->is_bluetooth) { effects.ucRumbleLeft = ctx->rumble_left;
data[0] = k_EPS4ReportIdBluetoothEffects; effects.ucRumbleRight = ctx->rumble_right;
data[1] = 0xC0 | 0x04; /* Magic value HID + CRC, also sets interval to 4ms for samples */
data[3] = 0x03; /* 0x1 is rumble, 0x2 is lightbar, 0x4 is the blink interval */
report_size = 78;
offset = 6;
} else {
data[0] = k_EPS4ReportIdUsbEffects;
data[1] = 0x07; /* Magic value */
report_size = 32;
offset = 4;
}
effects = (DS4EffectsState_t *)&data[offset];
effects->ucRumbleLeft = ctx->rumble_left;
effects->ucRumbleRight = ctx->rumble_right;
/* Populate the LED state with the appropriate color from our lookup table */ /* Populate the LED state with the appropriate color from our lookup table */
if (ctx->color_set) { if (ctx->color_set) {
effects->ucLedRed = ctx->led_red; effects.ucLedRed = ctx->led_red;
effects->ucLedGreen = ctx->led_green; effects.ucLedGreen = ctx->led_green;
effects->ucLedBlue = ctx->led_blue; effects.ucLedBlue = ctx->led_blue;
} else { } else {
SetLedsForPlayerIndex(effects, ctx->player_index); SetLedsForPlayerIndex(&effects, ctx->player_index);
} }
return HIDAPI_DriverPS4_SendJoystickEffect(device, ctx->joystick, &effects, sizeof(effects));
if (ctx->is_bluetooth) {
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
Uint32 unCRC;
unCRC = SDL_crc32(0, &ubHdr, 1);
unCRC = SDL_crc32(unCRC, data, (size_t)(report_size - sizeof(unCRC)));
SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
}
if (SDL_HIDAPI_SendRumble(device, data, report_size) != report_size) {
return SDL_SetError("Couldn't send rumble packet");
}
return 0;
} }
static void static void
@ -650,6 +617,55 @@ HIDAPI_DriverPS4_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
return HIDAPI_DriverPS4_UpdateEffects(device); return HIDAPI_DriverPS4_UpdateEffects(device);
} }
static int
HIDAPI_DriverPS4_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
{
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
Uint8 data[78];
int report_size, offset;
if (!ctx->effects_supported) {
return SDL_Unsupported();
}
if (!ctx->enhanced_mode) {
HIDAPI_DriverPS4_SetEnhancedMode(device, joystick);
}
SDL_zero(data);
if (ctx->is_bluetooth) {
data[0] = k_EPS4ReportIdBluetoothEffects;
data[1] = 0xC0 | 0x04; /* Magic value HID + CRC, also sets interval to 4ms for samples */
data[3] = 0x03; /* 0x1 is rumble, 0x2 is lightbar, 0x4 is the blink interval */
report_size = 78;
offset = 6;
} else {
data[0] = k_EPS4ReportIdUsbEffects;
data[1] = 0x07; /* Magic value */
report_size = 32;
offset = 4;
}
SDL_memcpy(&data[offset], effect, SDL_min((sizeof(data) - offset), size));
if (ctx->is_bluetooth) {
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
Uint32 unCRC;
unCRC = SDL_crc32(0, &ubHdr, 1);
unCRC = SDL_crc32(unCRC, data, (size_t)(report_size - sizeof(unCRC)));
SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
}
if (SDL_HIDAPI_SendRumble(device, data, report_size) != report_size) {
return SDL_SetError("Couldn't send rumble packet");
}
return 0;
}
static int static int
HIDAPI_DriverPS4_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverPS4_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -921,6 +937,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4 =
HIDAPI_DriverPS4_RumbleJoystickTriggers, HIDAPI_DriverPS4_RumbleJoystickTriggers,
HIDAPI_DriverPS4_HasJoystickLED, HIDAPI_DriverPS4_HasJoystickLED,
HIDAPI_DriverPS4_SetJoystickLED, HIDAPI_DriverPS4_SetJoystickLED,
HIDAPI_DriverPS4_SendJoystickEffect,
HIDAPI_DriverPS4_SetJoystickSensorsEnabled, HIDAPI_DriverPS4_SetJoystickSensorsEnabled,
HIDAPI_DriverPS4_CloseJoystick, HIDAPI_DriverPS4_CloseJoystick,
HIDAPI_DriverPS4_FreeDevice, HIDAPI_DriverPS4_FreeDevice,

View File

@ -175,6 +175,8 @@ typedef struct {
} SDL_DriverPS5_Context; } SDL_DriverPS5_Context;
static int HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size);
static SDL_bool static SDL_bool
HIDAPI_DriverPS5_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol) HIDAPI_DriverPS5_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
{ {
@ -375,32 +377,13 @@ static int
HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, int effect_mask) HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, int effect_mask)
{ {
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context; SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
DS5EffectsState_t *effects; DS5EffectsState_t effects;
Uint8 data[78];
int report_size, offset;
Uint8 *pending_data;
int *pending_size;
int maximum_size;
if (!ctx->enhanced_mode) { if (!ctx->enhanced_mode) {
return SDL_Unsupported(); return SDL_Unsupported();
} }
SDL_zero(data); SDL_zero(effects);
if (ctx->is_bluetooth) {
data[0] = k_EPS5ReportIdBluetoothEffects;
data[1] = 0x02; /* Magic value */
report_size = 78;
offset = 2;
} else {
data[0] = k_EPS5ReportIdUsbEffects;
report_size = 48;
offset = 1;
}
effects = (DS5EffectsState_t *)&data[offset];
/* Make sure the Bluetooth connection sequence has completed before sending LED color change */ /* Make sure the Bluetooth connection sequence has completed before sending LED color change */
if (ctx->is_bluetooth && if (ctx->is_bluetooth &&
@ -412,79 +395,53 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, int effect_mask)
} }
if (ctx->rumble_left || ctx->rumble_right) { if (ctx->rumble_left || ctx->rumble_right) {
effects->ucEnableBits1 |= 0x01; /* Enable rumble emulation */ effects.ucEnableBits1 |= 0x01; /* Enable rumble emulation */
effects->ucEnableBits1 |= 0x02; /* Disable audio haptics */ effects.ucEnableBits1 |= 0x02; /* Disable audio haptics */
/* Shift to reduce effective rumble strength to match Xbox controllers */ /* Shift to reduce effective rumble strength to match Xbox controllers */
effects->ucRumbleLeft = ctx->rumble_left >> 1; effects.ucRumbleLeft = ctx->rumble_left >> 1;
effects->ucRumbleRight = ctx->rumble_right >> 1; effects.ucRumbleRight = ctx->rumble_right >> 1;
} else { } else {
/* Leaving emulated rumble bits off will restore audio haptics */ /* Leaving emulated rumble bits off will restore audio haptics */
} }
if ((effect_mask & k_EDS5EffectRumbleStart) != 0) { if ((effect_mask & k_EDS5EffectRumbleStart) != 0) {
effects->ucEnableBits1 |= 0x02; /* Disable audio haptics */ effects.ucEnableBits1 |= 0x02; /* Disable audio haptics */
} }
if ((effect_mask & k_EDS5EffectRumble) != 0) { if ((effect_mask & k_EDS5EffectRumble) != 0) {
/* Already handled above */ /* Already handled above */
} }
if ((effect_mask & k_EDS5EffectLEDReset) != 0) { if ((effect_mask & k_EDS5EffectLEDReset) != 0) {
effects->ucEnableBits2 |= 0x08; /* Reset LED state */ effects.ucEnableBits2 |= 0x08; /* Reset LED state */
} }
if ((effect_mask & k_EDS5EffectLED) != 0) { if ((effect_mask & k_EDS5EffectLED) != 0) {
effects->ucEnableBits2 |= 0x04; /* Enable LED color */ effects.ucEnableBits2 |= 0x04; /* Enable LED color */
/* Populate the LED state with the appropriate color from our lookup table */ /* Populate the LED state with the appropriate color from our lookup table */
if (ctx->color_set) { if (ctx->color_set) {
effects->ucLedRed = ctx->led_red; effects.ucLedRed = ctx->led_red;
effects->ucLedGreen = ctx->led_green; effects.ucLedGreen = ctx->led_green;
effects->ucLedBlue = ctx->led_blue; effects.ucLedBlue = ctx->led_blue;
} else { } else {
SetLedsForPlayerIndex(effects, ctx->player_index); SetLedsForPlayerIndex(&effects, ctx->player_index);
} }
} }
if ((effect_mask & k_EDS5EffectPadLights) != 0) { if ((effect_mask & k_EDS5EffectPadLights) != 0) {
effects->ucEnableBits2 |= 0x10; /* Enable touchpad lights */ effects.ucEnableBits2 |= 0x10; /* Enable touchpad lights */
if (ctx->player_lights) { if (ctx->player_lights) {
SetLightsForPlayerIndex(effects, ctx->player_index); SetLightsForPlayerIndex(&effects, ctx->player_index);
} else { } else {
effects->ucPadLights = 0x00; effects.ucPadLights = 0x00;
} }
} }
if ((effect_mask & k_EDS5EffectMicLight) != 0) { if ((effect_mask & k_EDS5EffectMicLight) != 0) {
effects->ucEnableBits2 |= 0x01; /* Enable microphone light */ effects.ucEnableBits2 |= 0x01; /* Enable microphone light */
effects->ucMicLightMode = 0; /* Bitmask, 0x00 = off, 0x01 = solid, 0x02 = pulse */ effects.ucMicLightMode = 0; /* Bitmask, 0x00 = off, 0x01 = solid, 0x02 = pulse */
} }
if (ctx->is_bluetooth) { return HIDAPI_DriverPS5_SendJoystickEffect(device, ctx->joystick, &effects, sizeof(effects));
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
Uint32 unCRC;
unCRC = SDL_crc32(0, &ubHdr, 1);
unCRC = SDL_crc32(unCRC, data, (size_t)(report_size - sizeof(unCRC)));
SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
}
if (SDL_HIDAPI_LockRumble() < 0) {
return -1;
}
/* See if we can update an existing pending request */
if (SDL_HIDAPI_GetPendingRumbleLocked(device, &pending_data, &pending_size, &maximum_size)) {
DS5EffectsState_t *pending_effects = (DS5EffectsState_t *)&pending_data[offset];
if (report_size == *pending_size &&
effects->ucEnableBits1 == pending_effects->ucEnableBits1 &&
effects->ucEnableBits2 == pending_effects->ucEnableBits2) {
/* We're simply updating the data for this request */
SDL_memcpy(pending_data, data, report_size);
SDL_HIDAPI_UnlockRumble();
return 0;
}
}
return SDL_HIDAPI_SendRumbleAndUnlock(device, data, report_size);
} }
static void static void
@ -725,6 +682,67 @@ HIDAPI_DriverPS5_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
return HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED); return HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
} }
static int
HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
{
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
Uint8 data[78];
int report_size, offset;
Uint8 *pending_data;
int *pending_size;
int maximum_size;
if (!ctx->enhanced_mode) {
HIDAPI_DriverPS5_SetEnhancedMode(device, joystick);
}
SDL_zero(data);
if (ctx->is_bluetooth) {
data[0] = k_EPS5ReportIdBluetoothEffects;
data[1] = 0x02; /* Magic value */
report_size = 78;
offset = 2;
} else {
data[0] = k_EPS5ReportIdUsbEffects;
report_size = 48;
offset = 1;
}
SDL_memcpy(&data[offset], effect, SDL_min((sizeof(data) - offset), size));
if (ctx->is_bluetooth) {
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
Uint32 unCRC;
unCRC = SDL_crc32(0, &ubHdr, 1);
unCRC = SDL_crc32(unCRC, data, (size_t)(report_size - sizeof(unCRC)));
SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
}
if (SDL_HIDAPI_LockRumble() < 0) {
return -1;
}
/* See if we can update an existing pending request */
if (SDL_HIDAPI_GetPendingRumbleLocked(device, &pending_data, &pending_size, &maximum_size)) {
DS5EffectsState_t *effects = (DS5EffectsState_t *)&data[offset];
DS5EffectsState_t *pending_effects = (DS5EffectsState_t *)&pending_data[offset];
if (report_size == *pending_size &&
effects->ucEnableBits1 == pending_effects->ucEnableBits1 &&
effects->ucEnableBits2 == pending_effects->ucEnableBits2) {
/* We're simply updating the data for this request */
SDL_memcpy(pending_data, data, report_size);
SDL_HIDAPI_UnlockRumble();
return 0;
}
}
return SDL_HIDAPI_SendRumbleAndUnlock(device, data, report_size);
}
static int static int
HIDAPI_DriverPS5_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverPS5_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1082,6 +1100,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5 =
HIDAPI_DriverPS5_RumbleJoystickTriggers, HIDAPI_DriverPS5_RumbleJoystickTriggers,
HIDAPI_DriverPS5_HasJoystickLED, HIDAPI_DriverPS5_HasJoystickLED,
HIDAPI_DriverPS5_SetJoystickLED, HIDAPI_DriverPS5_SetJoystickLED,
HIDAPI_DriverPS5_SendJoystickEffect,
HIDAPI_DriverPS5_SetJoystickSensorsEnabled, HIDAPI_DriverPS5_SetJoystickSensorsEnabled,
HIDAPI_DriverPS5_CloseJoystick, HIDAPI_DriverPS5_CloseJoystick,
HIDAPI_DriverPS5_FreeDevice, HIDAPI_DriverPS5_FreeDevice,

View File

@ -141,6 +141,12 @@ HIDAPI_DriverStadia_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joys
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
HIDAPI_DriverStadia_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
HIDAPI_DriverStadia_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverStadia_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -314,6 +320,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia =
HIDAPI_DriverStadia_RumbleJoystickTriggers, HIDAPI_DriverStadia_RumbleJoystickTriggers,
HIDAPI_DriverStadia_HasJoystickLED, HIDAPI_DriverStadia_HasJoystickLED,
HIDAPI_DriverStadia_SetJoystickLED, HIDAPI_DriverStadia_SetJoystickLED,
HIDAPI_DriverStadia_SendJoystickEffect,
HIDAPI_DriverStadia_SetJoystickSensorsEnabled, HIDAPI_DriverStadia_SetJoystickSensorsEnabled,
HIDAPI_DriverStadia_CloseJoystick, HIDAPI_DriverStadia_CloseJoystick,
HIDAPI_DriverStadia_FreeDevice, HIDAPI_DriverStadia_FreeDevice,

View File

@ -1058,6 +1058,12 @@ HIDAPI_DriverSteam_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joyst
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
HIDAPI_DriverSteam_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
HIDAPI_DriverSteam_SetSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverSteam_SetSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1207,6 +1213,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam =
HIDAPI_DriverSteam_RumbleJoystickTriggers, HIDAPI_DriverSteam_RumbleJoystickTriggers,
HIDAPI_DriverSteam_HasJoystickLED, HIDAPI_DriverSteam_HasJoystickLED,
HIDAPI_DriverSteam_SetJoystickLED, HIDAPI_DriverSteam_SetJoystickLED,
HIDAPI_DriverSteam_SendJoystickEffect,
HIDAPI_DriverSteam_SetSensorsEnabled, HIDAPI_DriverSteam_SetSensorsEnabled,
HIDAPI_DriverSteam_CloseJoystick, HIDAPI_DriverSteam_CloseJoystick,
HIDAPI_DriverSteam_FreeDevice, HIDAPI_DriverSteam_FreeDevice,

View File

@ -1052,6 +1052,12 @@ HIDAPI_DriverSwitch_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joys
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
HIDAPI_DriverSwitch_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
HIDAPI_DriverSwitch_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverSwitch_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1497,6 +1503,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch =
HIDAPI_DriverSwitch_RumbleJoystickTriggers, HIDAPI_DriverSwitch_RumbleJoystickTriggers,
HIDAPI_DriverSwitch_HasJoystickLED, HIDAPI_DriverSwitch_HasJoystickLED,
HIDAPI_DriverSwitch_SetJoystickLED, HIDAPI_DriverSwitch_SetJoystickLED,
HIDAPI_DriverSwitch_SendJoystickEffect,
HIDAPI_DriverSwitch_SetJoystickSensorsEnabled, HIDAPI_DriverSwitch_SetJoystickSensorsEnabled,
HIDAPI_DriverSwitch_CloseJoystick, HIDAPI_DriverSwitch_CloseJoystick,
HIDAPI_DriverSwitch_FreeDevice, HIDAPI_DriverSwitch_FreeDevice,

View File

@ -216,6 +216,12 @@ HIDAPI_DriverXbox360_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joy
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
HIDAPI_DriverXbox360_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
HIDAPI_DriverXbox360_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverXbox360_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -342,6 +348,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
HIDAPI_DriverXbox360_RumbleJoystickTriggers, HIDAPI_DriverXbox360_RumbleJoystickTriggers,
HIDAPI_DriverXbox360_HasJoystickLED, HIDAPI_DriverXbox360_HasJoystickLED,
HIDAPI_DriverXbox360_SetJoystickLED, HIDAPI_DriverXbox360_SetJoystickLED,
HIDAPI_DriverXbox360_SendJoystickEffect,
HIDAPI_DriverXbox360_SetJoystickSensorsEnabled, HIDAPI_DriverXbox360_SetJoystickSensorsEnabled,
HIDAPI_DriverXbox360_CloseJoystick, HIDAPI_DriverXbox360_CloseJoystick,
HIDAPI_DriverXbox360_FreeDevice, HIDAPI_DriverXbox360_FreeDevice,

View File

@ -186,6 +186,12 @@ HIDAPI_DriverXbox360W_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *jo
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
HIDAPI_DriverXbox360W_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
HIDAPI_DriverXbox360W_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverXbox360W_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -339,6 +345,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W =
HIDAPI_DriverXbox360W_RumbleJoystickTriggers, HIDAPI_DriverXbox360W_RumbleJoystickTriggers,
HIDAPI_DriverXbox360W_HasJoystickLED, HIDAPI_DriverXbox360W_HasJoystickLED,
HIDAPI_DriverXbox360W_SetJoystickLED, HIDAPI_DriverXbox360W_SetJoystickLED,
HIDAPI_DriverXbox360W_SendJoystickEffect,
HIDAPI_DriverXbox360W_SetJoystickSensorsEnabled, HIDAPI_DriverXbox360W_SetJoystickSensorsEnabled,
HIDAPI_DriverXbox360W_CloseJoystick, HIDAPI_DriverXbox360W_CloseJoystick,
HIDAPI_DriverXbox360W_FreeDevice, HIDAPI_DriverXbox360W_FreeDevice,

View File

@ -439,6 +439,12 @@ HIDAPI_DriverXboxOne_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joy
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
HIDAPI_DriverXboxOne_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
HIDAPI_DriverXboxOne_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_DriverXboxOne_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1078,6 +1084,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne =
HIDAPI_DriverXboxOne_RumbleJoystickTriggers, HIDAPI_DriverXboxOne_RumbleJoystickTriggers,
HIDAPI_DriverXboxOne_HasJoystickLED, HIDAPI_DriverXboxOne_HasJoystickLED,
HIDAPI_DriverXboxOne_SetJoystickLED, HIDAPI_DriverXboxOne_SetJoystickLED,
HIDAPI_DriverXboxOne_SendJoystickEffect,
HIDAPI_DriverXboxOne_SetJoystickSensorsEnabled, HIDAPI_DriverXboxOne_SetJoystickSensorsEnabled,
HIDAPI_DriverXboxOne_CloseJoystick, HIDAPI_DriverXboxOne_CloseJoystick,
HIDAPI_DriverXboxOne_FreeDevice, HIDAPI_DriverXboxOne_FreeDevice,

View File

@ -1359,6 +1359,23 @@ HIDAPI_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blu
return result; return result;
} }
static int
HIDAPI_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
int result;
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
result = device->driver->SendJoystickEffect(device, joystick, data, size);
} else {
SDL_SetError("SendEffect failed, device disconnected");
result = -1;
}
return result;
}
static int static int
HIDAPI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) HIDAPI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1470,6 +1487,7 @@ SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
HIDAPI_JoystickRumbleTriggers, HIDAPI_JoystickRumbleTriggers,
HIDAPI_JoystickHasLED, HIDAPI_JoystickHasLED,
HIDAPI_JoystickSetLED, HIDAPI_JoystickSetLED,
HIDAPI_JoystickSendEffect,
HIDAPI_JoystickSetSensorsEnabled, HIDAPI_JoystickSetSensorsEnabled,
HIDAPI_JoystickUpdate, HIDAPI_JoystickUpdate,
HIDAPI_JoystickClose, HIDAPI_JoystickClose,

View File

@ -99,6 +99,7 @@ typedef struct _SDL_HIDAPI_DeviceDriver
int (*RumbleJoystickTriggers)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble); int (*RumbleJoystickTriggers)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);
SDL_bool (*HasJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick); SDL_bool (*HasJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
int (*SetJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue); int (*SetJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
int (*SendJoystickEffect)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size);
int (*SetJoystickSensorsEnabled)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled); int (*SetJoystickSensorsEnabled)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled);
void (*CloseJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick); void (*CloseJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
void (*FreeDevice)(SDL_HIDAPI_Device *device); void (*FreeDevice)(SDL_HIDAPI_Device *device);

View File

@ -1283,6 +1283,12 @@ IOS_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
IOS_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
IOS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) IOS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1434,6 +1440,7 @@ SDL_JoystickDriver SDL_IOS_JoystickDriver =
IOS_JoystickRumbleTriggers, IOS_JoystickRumbleTriggers,
IOS_JoystickHasLED, IOS_JoystickHasLED,
IOS_JoystickSetLED, IOS_JoystickSetLED,
IOS_JoystickSendEffect,
IOS_JoystickSetSensorsEnabled, IOS_JoystickSetSensorsEnabled,
IOS_JoystickUpdate, IOS_JoystickUpdate,
IOS_JoystickClose, IOS_JoystickClose,

View File

@ -1089,6 +1089,12 @@ LINUX_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
LINUX_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
LINUX_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) LINUX_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1603,6 +1609,7 @@ SDL_JoystickDriver SDL_LINUX_JoystickDriver =
LINUX_JoystickRumbleTriggers, LINUX_JoystickRumbleTriggers,
LINUX_JoystickHasLED, LINUX_JoystickHasLED,
LINUX_JoystickSetLED, LINUX_JoystickSetLED,
LINUX_JoystickSendEffect,
LINUX_JoystickSetSensorsEnabled, LINUX_JoystickSetSensorsEnabled,
LINUX_JoystickUpdate, LINUX_JoystickUpdate,
LINUX_JoystickClose, LINUX_JoystickClose,

View File

@ -484,6 +484,11 @@ static int OS2_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Ui
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int OS2_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int OS2_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) static int OS2_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
return SDL_Unsupported(); return SDL_Unsupported();
@ -777,6 +782,7 @@ SDL_JoystickDriver SDL_OS2_JoystickDriver =
OS2_JoystickRumbleTriggers, OS2_JoystickRumbleTriggers,
OS2_JoystickHasLED, OS2_JoystickHasLED,
OS2_JoystickSetLED, OS2_JoystickSetLED,
OS2_JoystickSendEffect,
OS2_JoystickSetSensorsEnabled, OS2_JoystickSetSensorsEnabled,
OS2_JoystickUpdate, OS2_JoystickUpdate,
OS2_JoystickClose, OS2_JoystickClose,

View File

@ -220,6 +220,12 @@ PSP_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
PSP_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int PSP_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) static int PSP_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
return SDL_Unsupported(); return SDL_Unsupported();
@ -307,6 +313,7 @@ SDL_JoystickDriver SDL_PSP_JoystickDriver =
PSP_JoystickRumbleTriggers, PSP_JoystickRumbleTriggers,
PSP_JoystickHasLED, PSP_JoystickHasLED,
PSP_JoystickSetLED, PSP_JoystickSetLED,
PSP_JoystickSendEffect,
PSP_JoystickSetSensorsEnabled, PSP_JoystickSetSensorsEnabled,
PSP_JoystickUpdate, PSP_JoystickUpdate,
PSP_JoystickClose, PSP_JoystickClose,

View File

@ -358,6 +358,12 @@ VIRTUAL_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 bl
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
VIRTUAL_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
VIRTUAL_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) VIRTUAL_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -438,6 +444,7 @@ SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver =
VIRTUAL_JoystickRumbleTriggers, VIRTUAL_JoystickRumbleTriggers,
VIRTUAL_JoystickHasLED, VIRTUAL_JoystickHasLED,
VIRTUAL_JoystickSetLED, VIRTUAL_JoystickSetLED,
VIRTUAL_JoystickSendEffect,
VIRTUAL_JoystickSetSensorsEnabled, VIRTUAL_JoystickSetSensorsEnabled,
VIRTUAL_JoystickUpdate, VIRTUAL_JoystickUpdate,
VIRTUAL_JoystickClose, VIRTUAL_JoystickClose,

View File

@ -381,6 +381,12 @@ VITA_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
return 0; return 0;
} }
static int
VITA_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
VITA_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) VITA_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -405,6 +411,7 @@ SDL_JoystickDriver SDL_VITA_JoystickDriver =
VITA_JoystickHasLED, VITA_JoystickHasLED,
VITA_JoystickSetLED, VITA_JoystickSetLED,
VITA_JoystickSendEffect,
VITA_JoystickSetSensorsEnabled, VITA_JoystickSetSensorsEnabled,
VITA_JoystickUpdate, VITA_JoystickUpdate,

View File

@ -345,6 +345,12 @@ WINMM_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
WINMM_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int WINMM_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) static int WINMM_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
return SDL_Unsupported(); return SDL_Unsupported();
@ -496,6 +502,7 @@ SDL_JoystickDriver SDL_WINMM_JoystickDriver =
WINMM_JoystickRumbleTriggers, WINMM_JoystickRumbleTriggers,
WINMM_JoystickHasLED, WINMM_JoystickHasLED,
WINMM_JoystickSetLED, WINMM_JoystickSetLED,
WINMM_JoystickSendEffect,
WINMM_JoystickSetSensorsEnabled, WINMM_JoystickSetSensorsEnabled,
WINMM_JoystickUpdate, WINMM_JoystickUpdate,
WINMM_JoystickClose, WINMM_JoystickClose,

View File

@ -1289,6 +1289,12 @@ RAWINPUT_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 bl
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
RAWINPUT_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
RAWINPUT_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) RAWINPUT_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -1924,6 +1930,7 @@ SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver =
RAWINPUT_JoystickRumbleTriggers, RAWINPUT_JoystickRumbleTriggers,
RAWINPUT_JoystickHasLED, RAWINPUT_JoystickHasLED,
RAWINPUT_JoystickSetLED, RAWINPUT_JoystickSetLED,
RAWINPUT_JoystickSendEffect,
RAWINPUT_JoystickSetSensorsEnabled, RAWINPUT_JoystickSetSensorsEnabled,
RAWINPUT_JoystickUpdate, RAWINPUT_JoystickUpdate,
RAWINPUT_JoystickClose, RAWINPUT_JoystickClose,

View File

@ -611,6 +611,12 @@ WGI_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
WGI_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
WGI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) WGI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -762,6 +768,7 @@ SDL_JoystickDriver SDL_WGI_JoystickDriver =
WGI_JoystickRumbleTriggers, WGI_JoystickRumbleTriggers,
WGI_JoystickHasLED, WGI_JoystickHasLED,
WGI_JoystickSetLED, WGI_JoystickSetLED,
WGI_JoystickSendEffect,
WGI_JoystickSetSensorsEnabled, WGI_JoystickSetSensorsEnabled,
WGI_JoystickUpdate, WGI_JoystickUpdate,
WGI_JoystickClose, WGI_JoystickClose,

View File

@ -579,6 +579,12 @@ WINDOWS_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 bl
return SDL_Unsupported(); return SDL_Unsupported();
} }
static int
WINDOWS_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int static int
WINDOWS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled) WINDOWS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{ {
@ -659,6 +665,7 @@ SDL_JoystickDriver SDL_WINDOWS_JoystickDriver =
WINDOWS_JoystickRumbleTriggers, WINDOWS_JoystickRumbleTriggers,
WINDOWS_JoystickHasLED, WINDOWS_JoystickHasLED,
WINDOWS_JoystickSetLED, WINDOWS_JoystickSetLED,
WINDOWS_JoystickSendEffect,
WINDOWS_JoystickSetSensorsEnabled, WINDOWS_JoystickSetSensorsEnabled,
WINDOWS_JoystickUpdate, WINDOWS_JoystickUpdate,
WINDOWS_JoystickClose, WINDOWS_JoystickClose,

View File

@ -61,15 +61,16 @@ static const struct { int x; int y; double angle; } axis_positions[] = {
{375, -20, 0.0}, /* TRIGGERRIGHT */ {375, -20, 0.0}, /* TRIGGERRIGHT */
}; };
SDL_Window *window = NULL; static SDL_Window *window = NULL;
SDL_Renderer *screen = NULL; static SDL_Renderer *screen = NULL;
SDL_bool retval = SDL_FALSE; static SDL_bool retval = SDL_FALSE;
SDL_bool done = SDL_FALSE; static SDL_bool done = SDL_FALSE;
SDL_bool set_LED = SDL_FALSE; static SDL_bool set_LED = SDL_FALSE;
SDL_Texture *background_front, *background_back, *button, *axis; static int trigger_effect = 0;
SDL_GameController *gamecontroller; static SDL_Texture *background_front, *background_back, *button, *axis;
SDL_GameController **gamecontrollers; static SDL_GameController *gamecontroller;
int num_controllers = 0; static SDL_GameController **gamecontrollers;
static int num_controllers = 0;
static void UpdateWindowTitle() static void UpdateWindowTitle()
{ {
@ -146,6 +147,7 @@ static void AddController(int device_index, SDL_bool verbose)
controllers[num_controllers++] = controller; controllers[num_controllers++] = controller;
gamecontrollers = controllers; gamecontrollers = controllers;
gamecontroller = controller; gamecontroller = controller;
trigger_effect = 0;
if (verbose) { if (verbose) {
const char *name = SDL_GameControllerName(gamecontroller); const char *name = SDL_GameControllerName(gamecontroller);
@ -245,6 +247,57 @@ static Uint16 ConvertAxisToRumble(Sint16 axis)
} }
} }
/* PS5 trigger effect documentation:
https://controllers.fandom.com/wiki/Sony_DualSense#FFB_Trigger_Modes
*/
typedef struct
{
Uint8 ucEnableBits1; /* 0 */
Uint8 ucEnableBits2; /* 1 */
Uint8 ucRumbleRight; /* 2 */
Uint8 ucRumbleLeft; /* 3 */
Uint8 ucHeadphoneVolume; /* 4 */
Uint8 ucSpeakerVolume; /* 5 */
Uint8 ucMicrophoneVolume; /* 6 */
Uint8 ucAudioEnableBits; /* 7 */
Uint8 ucMicLightMode; /* 8 */
Uint8 ucAudioMuteBits; /* 9 */
Uint8 rgucRightTriggerEffect[11]; /* 10 */
Uint8 rgucLeftTriggerEffect[11]; /* 21 */
Uint8 rgucUnknown1[6]; /* 32 */
Uint8 ucLedFlags; /* 38 */
Uint8 rgucUnknown2[2]; /* 39 */
Uint8 ucLedAnim; /* 41 */
Uint8 ucLedBrightness; /* 42 */
Uint8 ucPadLights; /* 43 */
Uint8 ucLedRed; /* 44 */
Uint8 ucLedGreen; /* 45 */
Uint8 ucLedBlue; /* 46 */
} DS5EffectsState_t;
static void CyclePS5TriggerEffect()
{
DS5EffectsState_t state;
Uint8 effects[3][11] =
{
/* Clear trigger effect */
{ 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
/* Constant resistance across entire trigger pull */
{ 0x01, 0, 110, 0, 0, 0, 0, 0, 0, 0, 0 },
/* Resistance and vibration when trigger is pulled */
{ 0x06, 15, 63, 128, 0, 0, 0, 0, 0, 0, 0 },
};
trigger_effect = (trigger_effect + 1) % SDL_arraysize(effects);
SDL_zero(state);
state.ucEnableBits1 |= (0x04 | 0x08); /* Modify right and left trigger effect respectively */
SDL_memcpy(state.rgucRightTriggerEffect, effects[trigger_effect], sizeof(effects[trigger_effect]));
SDL_memcpy(state.rgucLeftTriggerEffect, effects[trigger_effect], sizeof(effects[trigger_effect]));
SDL_GameControllerSendEffect(gamecontroller, &state, sizeof(state));
}
void void
loop(void *arg) loop(void *arg)
{ {
@ -279,6 +332,8 @@ loop(void *arg)
event.ctouchpad.pressure); event.ctouchpad.pressure);
break; break;
#define VERBOSE_SENSORS
#ifdef VERBOSE_SENSORS
case SDL_CONTROLLERSENSORUPDATE: case SDL_CONTROLLERSENSORUPDATE:
SDL_Log("Controller %d sensor %s: %.2f, %.2f, %.2f\n", SDL_Log("Controller %d sensor %s: %.2f, %.2f, %.2f\n",
event.csensor.which, event.csensor.which,
@ -288,13 +343,17 @@ loop(void *arg)
event.csensor.data[1], event.csensor.data[1],
event.csensor.data[2]); event.csensor.data[2]);
break; break;
#endif /* VERBOSE_SENSORS */
#define VERBOSE_AXES
#ifdef VERBOSE_AXES
case SDL_CONTROLLERAXISMOTION: case SDL_CONTROLLERAXISMOTION:
if (event.caxis.value <= (-SDL_JOYSTICK_AXIS_MAX / 2) || event.caxis.value >= (SDL_JOYSTICK_AXIS_MAX / 2)) { if (event.caxis.value <= (-SDL_JOYSTICK_AXIS_MAX / 2) || event.caxis.value >= (SDL_JOYSTICK_AXIS_MAX / 2)) {
SetController(event.caxis.which); SetController(event.caxis.which);
} }
SDL_Log("Controller %d axis %s changed to %d\n", event.caxis.which, SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value); SDL_Log("Controller %d axis %s changed to %d\n", event.caxis.which, SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value);
break; break;
#endif /* VERBOSE_AXES */
case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP: case SDL_CONTROLLERBUTTONUP:
@ -302,6 +361,13 @@ loop(void *arg)
SetController(event.cbutton.which); SetController(event.cbutton.which);
} }
SDL_Log("Controller %d button %s %s\n", event.cbutton.which, SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released"); SDL_Log("Controller %d button %s %s\n", event.cbutton.which, SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released");
/* Cycle PS5 trigger effects when the microphone button is pressed */
if (event.type == SDL_CONTROLLERBUTTONDOWN &&
event.cbutton.button == SDL_CONTROLLER_BUTTON_MISC1 &&
SDL_GameControllerGetType(gamecontroller) == SDL_CONTROLLER_TYPE_PS5) {
CyclePS5TriggerEffect();
}
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
@ -408,6 +474,7 @@ loop(void *arg)
} }
} }
if (trigger_effect == 0) {
/* Update rumble based on trigger state */ /* Update rumble based on trigger state */
{ {
Sint16 left = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERLEFT); Sint16 left = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
@ -427,6 +494,7 @@ loop(void *arg)
SDL_GameControllerRumbleTriggers(gamecontroller, left_rumble, right_rumble, 250); SDL_GameControllerRumbleTriggers(gamecontroller, left_rumble, right_rumble, 250);
} }
} }
}
SDL_RenderPresent(screen); SDL_RenderPresent(screen);
@ -577,6 +645,12 @@ main(int argc, char *argv[])
} }
#endif #endif
/* Reset trigger state */
if (trigger_effect != 0) {
trigger_effect = -1;
CyclePS5TriggerEffect();
}
SDL_DestroyRenderer(screen); SDL_DestroyRenderer(screen);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER); SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);