SDL: Improved reliability detecting FlyDigi Vader controllers

From b833c618a92370745d03ce0682903c35af47fde1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 16 Jun 2025 11:07:50 -0700
Subject: [PATCH] Improved reliability detecting FlyDigi Vader controllers

---
 src/joystick/hidapi/SDL_hidapi_flydigi.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/joystick/hidapi/SDL_hidapi_flydigi.c b/src/joystick/hidapi/SDL_hidapi_flydigi.c
index ac012e6600f6d..832f68e063470 100644
--- a/src/joystick/hidapi/SDL_hidapi_flydigi.c
+++ b/src/joystick/hidapi/SDL_hidapi_flydigi.c
@@ -95,19 +95,18 @@ static void UpdateDeviceIdentity(SDL_HIDAPI_Device *device)
 {
     SDL_DriverFlydigi_Context *ctx = (SDL_DriverFlydigi_Context *)device->context;
 
-    for (int attempt = 0; ctx->deviceID == 0 && attempt < 3; ++attempt) {
+    // Detecting the Vader 2 can take over 1000 read retries, so be generous here
+    for (int attempt = 0; ctx->deviceID == 0 && attempt < 30; ++attempt) {
         const Uint8 request[] = { FLYDIGI_CMD_REPORT_ID, FLYDIGI_GET_INFO_COMMAND, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-        int size = SDL_hid_write(device->dev, request, sizeof(request));
-        if (size < 0) {
-            break;
-        }
+        // This write will occasionally return -1, so ignore failure here and try again
+        (void)SDL_hid_write(device->dev, request, sizeof(request));
 
         // Read the reply
         for (int i = 0; i < 100; ++i) {
             SDL_Delay(1);
 
             Uint8 data[USB_PACKET_LENGTH];
-            size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0);
+            int size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0);
             if (size < 0) {
                 break;
             }