From 9837653b9d20d48b30caa66f93951b8604e7e9ea Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 2 Jun 2023 15:52:56 -0700 Subject: [PATCH] Allow the application to send commands to Nintendo Switch controllers --- src/joystick/hidapi/SDL_hidapi_switch.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c index b51f961c5..289555ace 100644 --- a/src/joystick/hidapi/SDL_hidapi_switch.c +++ b/src/joystick/hidapi/SDL_hidapi_switch.c @@ -393,7 +393,7 @@ static SDL_bool ReadProprietaryReply(SDL_DriverSwitch_Context *ctx, ESwitchPropr return SDL_FALSE; } -static void ConstructSubcommand(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs ucCommandID, Uint8 *pBuf, Uint8 ucLen, SwitchSubcommandOutputPacket_t *outPacket) +static void ConstructSubcommand(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs ucCommandID, const Uint8 *pBuf, Uint8 ucLen, SwitchSubcommandOutputPacket_t *outPacket) { SDL_memset(outPacket, 0, sizeof(*outPacket)); @@ -432,7 +432,7 @@ static SDL_bool WritePacket(SDL_DriverSwitch_Context *ctx, void *pBuf, Uint8 ucL } } -static SDL_bool WriteSubcommand(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs ucCommandID, Uint8 *pBuf, Uint8 ucLen, SwitchSubcommandInputPacket_t **ppReply) +static SDL_bool WriteSubcommand(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs ucCommandID, const Uint8 *pBuf, Uint8 ucLen, SwitchSubcommandInputPacket_t **ppReply) { SwitchSubcommandInputPacket_t *reply = NULL; int nTries; @@ -648,7 +648,7 @@ static SDL_bool SetVibrationEnabled(SDL_DriverSwitch_Context *ctx, Uint8 enabled } static SDL_bool SetInputMode(SDL_DriverSwitch_Context *ctx, Uint8 input_mode) { - return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetInputReportMode, &input_mode, 1, NULL); + return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetInputReportMode, &input_mode, sizeof(input_mode), NULL); } static SDL_bool SetHomeLED(SDL_DriverSwitch_Context *ctx, Uint8 brightness) @@ -1579,6 +1579,19 @@ static int HIDAPI_DriverSwitch_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL ctx->m_bRumblePending = SDL_FALSE; ctx->m_bRumbleZeroPending = SDL_FALSE; return 0; + } else if (size >= 2 && size <= 256) { + const Uint8 *payload = (const Uint8 *)data; + ESwitchSubcommandIDs cmd = (ESwitchSubcommandIDs)payload[0]; + + if (cmd == k_eSwitchSubcommandIDs_SetInputReportMode && !device->is_bluetooth) { + /* Going into simple mode over USB disables input reports, so don't do that */ + return 0; + } + + if (!WriteSubcommand(ctx, cmd, &payload[1], (Uint8)(size - 1), NULL)) { + return -1; + } + return 0; } return SDL_Unsupported(); }