SDL: Allow the application to send rumble packets to Nintendo Switch controllers

From 28f891ad0ab7d6d38667649d85962e6adecc4a97 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 9 May 2023 17:08:47 -0700
Subject: [PATCH] Allow the application to send rumble packets to Nintendo
 Switch controllers

---
 src/joystick/hidapi/SDL_hidapi_switch.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index ea2b5f9e2c3b..632a2e94fc8c 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -1560,6 +1560,26 @@ static int HIDAPI_DriverSwitch_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joy
 
 static int HIDAPI_DriverSwitch_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
 {
+    SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
+
+    if (size == sizeof(SwitchCommonOutputPacket_t)) {
+        const SwitchCommonOutputPacket_t *packet = (SwitchCommonOutputPacket_t *)data;
+
+        if (packet->ucPacketType != k_eSwitchOutputReportIDs_Rumble) {
+            return SDL_SetError("Unknown Nintendo Switch Pro effect type");
+        }
+
+        SDL_copyp(&ctx->m_RumblePacket.rumbleData[0], &packet->rumbleData[0]);
+        SDL_copyp(&ctx->m_RumblePacket.rumbleData[1], &packet->rumbleData[1]);
+        if (!WriteRumble(ctx)) {
+            return -1;
+        }
+
+        /* This overwrites any internal rumble */
+        ctx->m_bRumblePending = SDL_FALSE;
+        ctx->m_bRumbleZeroPending = SDL_FALSE;
+        return 0;
+    }
     return SDL_Unsupported();
 }