SDL: Fixed Flydigi Vader input stopping after hotplug while the Flydigi Space Station service is running

From 7729f91ff76781d38f5cf13d76a738c9f89168fc Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 2 Nov 2026 16:09:00 -0800
Subject: [PATCH] Fixed Flydigi Vader input stopping after hotplug while the
 Flydigi Space Station service is running

---
 src/joystick/hidapi/SDL_hidapi_flydigi.c | 81 ++++++++++++++++--------
 1 file changed, 54 insertions(+), 27 deletions(-)

diff --git a/src/joystick/hidapi/SDL_hidapi_flydigi.c b/src/joystick/hidapi/SDL_hidapi_flydigi.c
index 08b54a8b563cf..3effb638f53e6 100644
--- a/src/joystick/hidapi/SDL_hidapi_flydigi.c
+++ b/src/joystick/hidapi/SDL_hidapi_flydigi.c
@@ -97,7 +97,9 @@ typedef struct
     float accelScale;
     float gyroScale;
     Uint64 next_heartbeat;
+    Uint64 next_status_request;
     Uint64 last_packet;
+    bool acquired;
     Uint8 last_state[USB_PACKET_LENGTH];
 } SDL_DriverFlydigi_Context;
 
@@ -493,53 +495,60 @@ static void HIDAPI_DriverFlydigi_HandleInfoResponse(SDL_Joystick *joystick, SDL_
     SDL_SendJoystickPowerInfo(joystick, state, percent);
 }
 
-static bool SDL_HIDAPI_Flydigi_SendStatusRequest(SDL_HIDAPI_Device *device)
+static bool SDL_HIDAPI_Flydigi_SendAcquireRequest(SDL_HIDAPI_Device *device, bool acquire)
 {
-    const Uint8 cmd[] = {
+    const Uint8 cmd[32] = {
         FLYDIGI_V2_CMD_REPORT_ID,
         FLYDIGI_V2_MAGIC1,
         FLYDIGI_V2_MAGIC2,
-        FLYDIGI_V2_GET_STATUS_COMMAND
+        FLYDIGI_V2_ACQUIRE_CONTROLLER_COMMAND,
+        23,
+        acquire ? 1 : 0,
+        'S', 'D', 'L', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     };
 
     if (HIDAPI_DriverFlydigi_WritePacket(device, cmd, sizeof(cmd)) < 0) {
-        return SDL_SetError("Couldn't query controller status");
+        return SDL_SetError("Couldn't send acquire command");
     }
     return true;
 }
 
-static void HIDAPI_DriverFlydigi_HandleStatusResponse(SDL_HIDAPI_Device *device, Uint8 *data, int size)
+static void HIDAPI_DriverFlydigi_HandleAcquireResponse(SDL_HIDAPI_Device *device, Uint8 *data, int size)
 {
-    if (data[9] == 1) {
-        HIDAPI_DriverFlydigi_SetAvailable(device, true);
-    } else {
-        // Click "Allow third-party apps to take over mappings" in the FlyDigi Space Station app
-        HIDAPI_DriverFlydigi_SetAvailable(device, false);
-    }
+    // This response might be from another application (e.g. the Space Station Service)
+    // so let's rely on the status response to determine whether the controller is available
 }
 
-static bool SDL_HIDAPI_Flydigi_SendAcquireRequest(SDL_HIDAPI_Device *device, bool acquire)
+static bool SDL_HIDAPI_Flydigi_SendStatusRequest(SDL_HIDAPI_Device *device)
 {
-    const Uint8 cmd[32] = {
+    const Uint8 cmd[] = {
         FLYDIGI_V2_CMD_REPORT_ID,
         FLYDIGI_V2_MAGIC1,
         FLYDIGI_V2_MAGIC2,
-        FLYDIGI_V2_ACQUIRE_CONTROLLER_COMMAND,
-        23,
-        acquire ? 1 : 0,
-        'S', 'D', 'L', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+        FLYDIGI_V2_GET_STATUS_COMMAND
     };
 
     if (HIDAPI_DriverFlydigi_WritePacket(device, cmd, sizeof(cmd)) < 0) {
-        return SDL_SetError("Couldn't send acquire command");
+        return SDL_SetError("Couldn't query controller status");
     }
     return true;
 }
 
-static void HIDAPI_DriverFlydigi_HandleAcquireResponse(SDL_HIDAPI_Device *device, Uint8 *data, int size)
+static void HIDAPI_DriverFlydigi_HandleStatusResponse(SDL_HIDAPI_Device *device, Uint8 *data, int size)
 {
-    if (data[5] != 1 && data[6] == 0) {
-        // Controller acquiring failed or has been disabled
+    SDL_DriverFlydigi_Context *ctx = (SDL_DriverFlydigi_Context *)device->context;
+
+    if (data[9] == 1) {
+        HIDAPI_DriverFlydigi_SetAvailable(device, true);
+
+        // The Space Station service can stop input while third party apps are still allowed,
+        // so release and re-acquire the controller if it looks like we should be able to.
+        if (device->num_joysticks > 0 && ctx->acquired && data[6] == 0) {
+            SDL_HIDAPI_Flydigi_SendAcquireRequest(device, false);
+            SDL_HIDAPI_Flydigi_SendAcquireRequest(device, true);
+        }
+    } else {
+        // Click "Allow third-party apps to take over mappings" in the FlyDigi Space Station app
         HIDAPI_DriverFlydigi_SetAvailable(device, false);
     }
 }
@@ -624,7 +633,10 @@ static bool HIDAPI_DriverFlydigi_InitControllerV2(SDL_HIDAPI_Device *device)
     HIDAPI_DriverFlydigi_UpdateDeviceIdentity(device);
 
     // See whether we can acquire the controller
-    SDL_HIDAPI_Flydigi_SendStatusRequest(device);
+    if (SDL_HIDAPI_Flydigi_SendStatusRequest(device) &&
+        GetReply(device, FLYDIGI_V2_GET_STATUS_COMMAND, data, sizeof(data))) {
+        HIDAPI_DriverFlydigi_HandleStatusResponse(device, data, sizeof(data));
+    }
 
     return true;
 }
@@ -1049,11 +1061,26 @@ static bool HIDAPI_DriverFlydigi_UpdateDevice(SDL_HIDAPI_Device *device)
         joystick = SDL_GetJoystickFromID(device->joysticks[0]);
     }
 
-    if (device->vendor_id == USB_VENDOR_FLYDIGI_V2 && joystick) {
-        if (!ctx->next_heartbeat || now >= ctx->next_heartbeat) {
-            SDL_HIDAPI_Flydigi_SendAcquireRequest(device, true);
-            SDL_HIDAPI_Flydigi_SendInfoRequest(device);
-            ctx->next_heartbeat = now + FLYDIGI_ACQUIRE_CONTROLLER_HEARTBEAT_TIME;
+    if (device->vendor_id == USB_VENDOR_FLYDIGI_V2) {
+        if (joystick) {
+            if (!ctx->next_heartbeat || now >= ctx->next_heartbeat) {
+                if (SDL_HIDAPI_Flydigi_SendAcquireRequest(device, true)) {
+                    ctx->acquired = true;
+                }
+                SDL_HIDAPI_Flydigi_SendInfoRequest(device);
+                SDL_HIDAPI_Flydigi_SendStatusRequest(device);
+                ctx->next_heartbeat = now + FLYDIGI_ACQUIRE_CONTROLLER_HEARTBEAT_TIME;
+
+                // Time the input timeout from the heartbeat so we don't send it again before the replies arrive
+                ctx->last_packet = now;
+            }
+        } else if (device->num_joysticks == 0 && !device->unavailable) {
+            // We don't know yet whether we can use the controller, and the status reply might have been lost
+            const int STATUS_RETRY_MS = 1000;
+            if (now >= ctx->next_status_request) {
+                SDL_HIDAPI_Flydigi_SendStatusRequest(device);
+                ctx->next_status_request = now + STATUS_RETRY_MS;
+            }
         }
     }