SDL: Add Flydigi VADER 4 Pro HIDAPI support (#12874)

From 354895d97501beff39f33360bfae07fa24822dd5 Mon Sep 17 00:00:00 2001
From: bakatrouble <[EMAIL REDACTED]>
Date: Thu, 22 May 2025 19:56:17 +0300
Subject: [PATCH] Add Flydigi VADER 4 Pro HIDAPI support (#12874)

---
 VisualC-GDK/SDL/SDL.vcxproj                |   1 +
 VisualC-GDK/SDL/SDL.vcxproj.filters        |   1 +
 VisualC/SDL/SDL.vcxproj                    |   1 +
 VisualC/SDL/SDL.vcxproj.filters            |   3 +
 Xcode/SDL/SDL.xcodeproj/project.pbxproj    |   3 +
 include/SDL3/SDL_hints.h                   |  12 +
 src/hidapi/SDL_hidapi.c                    |   5 +
 src/joystick/SDL_gamepad.c                 |   4 +-
 src/joystick/SDL_joystick.c                |   5 +
 src/joystick/SDL_joystick_c.h              |   3 +
 src/joystick/hidapi/SDL_hidapi_flydigi.c   | 415 +++++++++++++++++++++
 src/joystick/hidapi/SDL_hidapijoystick.c   |   5 +-
 src/joystick/hidapi/SDL_hidapijoystick_c.h |   2 +
 src/joystick/usb_ids.h                     |   3 +
 14 files changed, 461 insertions(+), 2 deletions(-)
 create mode 100644 src/joystick/hidapi/SDL_hidapi_flydigi.c

diff --git a/VisualC-GDK/SDL/SDL.vcxproj b/VisualC-GDK/SDL/SDL.vcxproj
index 6cd9632bc8375..58194e72467cd 100644
--- a/VisualC-GDK/SDL/SDL.vcxproj
+++ b/VisualC-GDK/SDL/SDL.vcxproj
@@ -713,6 +713,7 @@
     <ClCompile Include="..\..\src\joystick\gdk\SDL_gameinputjoystick.cpp" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapijoystick.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_8bitdo.c" />
+    <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_flydigi.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_combined.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_gamecube.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_gip.c" />
diff --git a/VisualC-GDK/SDL/SDL.vcxproj.filters b/VisualC-GDK/SDL/SDL.vcxproj.filters
index 7aa7cba153133..8a988ace966c8 100644
--- a/VisualC-GDK/SDL/SDL.vcxproj.filters
+++ b/VisualC-GDK/SDL/SDL.vcxproj.filters
@@ -64,6 +64,7 @@
     <ClCompile Include="..\..\src\joystick\gdk\SDL_gameinputjoystick.cpp" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapijoystick.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_8bitdo.c" />
+    <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_flydigi.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_combined.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_gamecube.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_gip.c" />
diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj
index 4f6c5d95bcf2e..843f8e61ce613 100644
--- a/VisualC/SDL/SDL.vcxproj
+++ b/VisualC/SDL/SDL.vcxproj
@@ -603,6 +603,7 @@
     </ClCompile>
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapijoystick.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_8bitdo.c" />
+    <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_flydigi.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_combined.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_gamecube.c" />
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_gip.c" />
diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters
index e4ef251f9f7d8..375c175c0e95a 100644
--- a/VisualC/SDL/SDL.vcxproj.filters
+++ b/VisualC/SDL/SDL.vcxproj.filters
@@ -1185,6 +1185,9 @@
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_8bitdo.c">
       <Filter>joystick\hidapi</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_flydigi.c">
+      <Filter>joystick\hidapi</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_combined.c">
       <Filter>joystick\hidapi</Filter>
     </ClCompile>
diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
index a98fc779a7a90..d350542fda5c3 100644
--- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj
+++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
@@ -389,6 +389,7 @@
 		F338A1182D1B37D8007CDFDF /* SDL_tray.m in Sources */ = {isa = PBXBuildFile; fileRef = F338A1172D1B37D8007CDFDF /* SDL_tray.m */; };
 		F338A11A2D1B37E4007CDFDF /* SDL_tray.c in Sources */ = {isa = PBXBuildFile; fileRef = F338A1192D1B37E4007CDFDF /* SDL_tray.c */; };
 		F3395BA82D9A5971007246C8 /* SDL_hidapi_8bitdo.c in Sources */ = {isa = PBXBuildFile; fileRef = F3395BA72D9A5971007246C8 /* SDL_hidapi_8bitdo.c */; };
+		F3395BA82D9A5971007246C9 /* SDL_hidapi_flydigi.c in Sources */ = {isa = PBXBuildFile; fileRef = F3395BA72D9A5971007246C9 /* SDL_hidapi_flydigi.c */; };
 		F34400342D40217A003F26D7 /* LICENSE.txt in Resources */ = {isa = PBXBuildFile; fileRef = F373DA182D388A1E002158FA /* LICENSE.txt */; };
 		F34400362D40217A003F26D7 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = F373DA192D388A1E002158FA /* README.md */; };
 		F344003D2D4022E1003F26D7 /* INSTALL.md in Resources */ = {isa = PBXBuildFile; fileRef = F344003C2D4022E1003F26D7 /* INSTALL.md */; };
@@ -949,6 +950,7 @@
 		F338A1172D1B37D8007CDFDF /* SDL_tray.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDL_tray.m; sourceTree = "<group>"; };
 		F338A1192D1B37E4007CDFDF /* SDL_tray.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_tray.c; sourceTree = "<group>"; };
 		F3395BA72D9A5971007246C8 /* SDL_hidapi_8bitdo.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_8bitdo.c; sourceTree = "<group>"; };
+		F3395BA72D9A5971007246C9 /* SDL_hidapi_flydigi.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_flydigi.c; sourceTree = "<group>"; };
 		F344003C2D4022E1003F26D7 /* INSTALL.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = INSTALL.md; sourceTree = "<group>"; };
 		F362B9152B3349E200D30B94 /* controller_list.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = controller_list.h; sourceTree = "<group>"; };
 		F362B9162B3349E200D30B94 /* SDL_gamepad_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_gamepad_c.h; sourceTree = "<group>"; };
@@ -1928,6 +1930,7 @@
 			isa = PBXGroup;
 			children = (
 				F3395BA72D9A5971007246C8 /* SDL_hidapi_8bitdo.c */,
+				F3395BA72D9A5971007246C9 /* SDL_hidapi_flydigi.c */,
 				F32305FE28939F6400E66D30 /* SDL_hidapi_combined.c */,
 				A7D8A7C923E2513E00DCD162 /* SDL_hidapi_gamecube.c */,
 				F3B6B8092DC3EA54004954FD /* SDL_hidapi_gip.c */,
diff --git a/include/SDL3/SDL_hints.h b/include/SDL3/SDL_hints.h
index 325d5d868dc52..443776bebc225 100644
--- a/include/SDL3/SDL_hints.h
+++ b/include/SDL3/SDL_hints.h
@@ -1746,6 +1746,18 @@ extern "C" {
  */
 #define SDL_HINT_JOYSTICK_HIDAPI_8BITDO "SDL_JOYSTICK_HIDAPI_8BITDO"
 
+/**
+ * A variable controlling whether the HIDAPI driver for Flydigi controllers
+ * should be used.
+ *
+ * This variable can be set to the following values:
+ *
+ * "0" - HIDAPI driver is not used. "1" - HIDAPI driver is used.
+ *
+ * The default is the value of SDL_HINT_JOYSTICK_HIDAPI
+ */
+#define SDL_HINT_JOYSTICK_HIDAPI_FLYDIGI "SDL_JOYSTICK_HIDAPI_FLYDIGI"
+
 /**
  * A variable controlling whether the HIDAPI driver for Nintendo Switch
  * controllers should be used.
diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c
index a525f22391cc3..f396f80bd84db 100644
--- a/src/hidapi/SDL_hidapi.c
+++ b/src/hidapi/SDL_hidapi.c
@@ -1109,6 +1109,11 @@ bool SDL_HIDAPI_ShouldIgnoreDevice(int bus, Uint16 vendor_id, Uint16 product_id,
                 (usage == USB_USAGE_GENERIC_KEYBOARD || usage == USB_USAGE_GENERIC_MOUSE)) {
                 return true;
             }
+        } else if (vendor_id == USB_VENDOR_FLYDIGI && product_id == USB_PRODUCT_FLYDIGI_VADER4_PRO) {
+            if (usage_page == USB_USAGEPAGE_VENDOR_FLYDIGI) {
+                return false;
+            }
+            return true;
         } else if (usage_page == USB_USAGEPAGE_GENERIC_DESKTOP &&
                    (usage == USB_USAGE_GENERIC_JOYSTICK || usage == USB_USAGE_GENERIC_GAMEPAD || usage == USB_USAGE_GENERIC_MULTIAXISCONTROLLER)) {
             // This is a controller
diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c
index d5541ae649cb1..aac7d734044bb 100644
--- a/src/joystick/SDL_gamepad.c
+++ b/src/joystick/SDL_gamepad.c
@@ -829,7 +829,9 @@ static GamepadMapping_t *SDL_CreateMappingForHIDAPIGamepad(SDL_GUID guid)
             }
         } else if (SDL_IsJoystickHoriSteamController(vendor, product)) {
             /* The Wireless HORIPad for Steam has QAM, Steam, Capsense L/R Sticks, 2 rear buttons, and 2 misc buttons */
-            SDL_strlcat(mapping_string, "paddle1:b13,paddle2:b12,paddle3:b15,paddle4:b14,misc2:b11,misc3:b16,misc4:b17,", sizeof(mapping_string));
+            SDL_strlcat(mapping_string, "paddle1:b13,paddle2:b12,paddle3:b15,paddle4:b14,misc2:b11,misc3:b16,misc4:b17", sizeof(mapping_string));
+        } else if (SDL_IsJoystickFlydigiController(vendor, product)) {
+            SDL_strlcat(mapping_string, "paddle1:b11,paddle2:b12,paddle3:b13,paddle4:b14,misc1:b15,misc2:b16,misc3:b17", sizeof(mapping_string));
         } else if (vendor == USB_VENDOR_8BITDO && product == USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS) {
             SDL_strlcat(mapping_string, "paddle1:b12,paddle2:b11,paddle3:b14,paddle4:b13,", sizeof(mapping_string));
         } else {
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 3caf227f015e4..507235c2af99d 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -3175,6 +3175,11 @@ bool SDL_IsJoystickHoriSteamController(Uint16 vendor_id, Uint16 product_id)
     return vendor_id == USB_VENDOR_HORI && (product_id == USB_PRODUCT_HORI_STEAM_CONTROLLER || product_id == USB_PRODUCT_HORI_STEAM_CONTROLLER_BT);
 }
 
+bool SDL_IsJoystickFlydigiController(Uint16 vendor_id, Uint16 product_id)
+{
+    return vendor_id == USB_VENDOR_FLYDIGI && (product_id == USB_PRODUCT_FLYDIGI_VADER4_PRO);
+}
+
 bool SDL_IsJoystickSteamDeck(Uint16 vendor_id, Uint16 product_id)
 {
     EControllerType eType = GuessControllerType(vendor_id, product_id);
diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h
index 6b82365c5865b..cbc33608c4478 100644
--- a/src/joystick/SDL_joystick_c.h
+++ b/src/joystick/SDL_joystick_c.h
@@ -135,6 +135,9 @@ extern bool SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id);
 // Function to return whether a joystick is a HORI Steam controller
 extern bool SDL_IsJoystickHoriSteamController(Uint16 vendor_id, Uint16 product_id);
 
+// Function to return whether a joystick is a Flydigi controller
+extern bool SDL_IsJoystickFlydigiController(Uint16 vendor_id, Uint16 product_id);
+
 // Function to return whether a joystick is a Steam Deck
 extern bool SDL_IsJoystickSteamDeck(Uint16 vendor_id, Uint16 product_id);
 
diff --git a/src/joystick/hidapi/SDL_hidapi_flydigi.c b/src/joystick/hidapi/SDL_hidapi_flydigi.c
new file mode 100644
index 0000000000000..1220e66dd9e46
--- /dev/null
+++ b/src/joystick/hidapi/SDL_hidapi_flydigi.c
@@ -0,0 +1,415 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_internal.h"
+
+#ifdef SDL_JOYSTICK_HIDAPI
+
+#include "../SDL_sysjoystick.h"
+#include "SDL_hidapijoystick_c.h"
+#include "SDL_hidapi_rumble.h"
+
+#ifdef SDL_JOYSTICK_HIDAPI_FLYDIGI
+
+// Define this if you want to log all packets from the controller
+#if 0
+#define DEBUG_FLYDIGI_PROTOCOL
+#endif
+
+enum
+{
+    SDL_GAMEPAD_BUTTON_FLYDIGI_M1 = 11,
+    SDL_GAMEPAD_BUTTON_FLYDIGI_M2,
+    SDL_GAMEPAD_BUTTON_FLYDIGI_M3,
+    SDL_GAMEPAD_BUTTON_FLYDIGI_M4,
+    SDL_GAMEPAD_BUTTON_FLYDIGI_FN,
+    SDL_GAMEPAD_BUTTON_FLYDIGI_C,
+    SDL_GAMEPAD_BUTTON_FLYDIGI_Z,
+    SDL_GAMEPAD_NUM_FLYDIGI_BUTTONS_WITH_CZ,
+};
+#define SDL_GAMEPAD_NUM_FLYDIGI_BUTTONS_WITHOUT_CZ SDL_GAMEPAD_BUTTON_FLYDIGI_C
+
+#define FLYDIGI_ACCEL_SCALE 256.f
+#define SENSOR_INTERVAL_NS 8000000ULL
+#define FLYDIGI_CMD_REPORT_ID 0x05
+#define FLYDIGI_HAPTIC_COMMAND 0x0F
+#define FLYDIGI_GET_CONFIG_COMMAND 0xEB
+
+#define LOAD16(A, B)       (Sint16)((Uint16)(A) | (((Uint16)(B)) << 8))
+
+typedef struct
+{
+    bool sensors_supported;
+    bool sensors_enabled;
+    bool touchpad_01_supported;
+    bool touchpad_02_supported;
+    bool rumble_supported;
+    bool rumble_type;
+    bool rgb_supported;
+    bool player_led_supported;
+    bool powerstate_supported;
+    bool has_cz;
+    Uint8 serial[6];
+    Uint16 version;
+    Uint16 version_beta;
+    float accelScale;
+    float gyroScale;
+    Uint8 last_state[USB_PACKET_LENGTH];
+    Uint64 sensor_timestamp; // Microseconds. Simulate onboard clock. Advance by known rate: SENSOR_INTERVAL_NS == 8ms = 125 Hz
+} SDL_DriverFlydigi_Context;
+
+#pragma pack(push,1)
+typedef struct
+{
+    bool sensors_supported;
+    bool touchpad_01_supported;
+    bool touchpad_02_supported;
+    bool rumble_supported;
+    bool rumble_type;
+    bool rgb_supported;
+    Uint8 device_type;
+    Uint8 serial[6];
+    Uint16 version;
+    Uint16 version_beta;
+    Uint16 pid;
+} FLYDIGI_DEVICE_INFO;
+
+#pragma pack(pop)
+
+
+static void HIDAPI_DriverFlydigi_RegisterHints(SDL_HintCallback callback, void *userdata)
+{
+    SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_FLYDIGI, callback, userdata);
+}
+
+static void HIDAPI_DriverFlydigi_UnregisterHints(SDL_HintCallback callback, void *userdata)
+{
+    SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_FLYDIGI, callback, userdata);
+}
+
+static bool HIDAPI_DriverFlydigi_IsEnabled(void)
+{
+    return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_FLYDIGI, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
+}
+
+static bool HIDAPI_DriverFlydigi_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
+{
+    return SDL_IsJoystickFlydigiController(vendor_id, product_id) && interface_number == 2;
+}
+
+static bool HIDAPI_DriverFlydigi_InitDevice(SDL_HIDAPI_Device *device)
+{
+    SDL_DriverFlydigi_Context *ctx = (SDL_DriverFlydigi_Context *)SDL_calloc(1, sizeof(*ctx));
+    if (!ctx) {
+        return false;
+    }
+    device->context = ctx;
+
+    if (device->product_id == USB_PRODUCT_FLYDIGI_VADER4_PRO) {
+        const int VADER4PRO_REPORT_SIZE = 32;
+        Uint8 data[USB_PACKET_LENGTH];
+        int size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 80);
+        if (size == VADER4PRO_REPORT_SIZE) {
+            ctx->sensors_supported = true;
+            ctx->rumble_supported = true;
+        }
+        const char VADER3_NAME[] = "Flydigi VADER3";
+        const char VADER4_NAME[] = "Flydigi VADER4";
+        ctx->has_cz = SDL_strncmp(device->name, VADER3_NAME, sizeof(VADER3_NAME)) == 0 || SDL_strncmp(device->name, VADER4_NAME, sizeof(VADER4_NAME)) == 0;
+    }
+
+    return HIDAPI_JoystickConnected(device, NULL);
+}
+
+static int HIDAPI_DriverFlydigi_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
+{
+    return -1;
+}
+
+static void HIDAPI_DriverFlydigi_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
+{
+}
+
+#ifndef DEG2RAD
+#define DEG2RAD(x) ((float)(x) * (float)(SDL_PI_F / 180.f))
+#endif
+
+static bool HIDAPI_DriverFlydigi_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
+{
+    SDL_DriverFlydigi_Context *ctx = (SDL_DriverFlydigi_Context *)device->context;
+
+    SDL_AssertJoysticksLocked();
+
+    SDL_zeroa(ctx->last_state);
+
+    // Initialize the joystick capabilities
+    joystick->nbuttons = ctx->has_cz ? SDL_GAMEPAD_NUM_FLYDIGI_BUTTONS_WITH_CZ : SDL_GAMEPAD_NUM_FLYDIGI_BUTTONS_WITHOUT_CZ;
+    joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
+    joystick->nhats = 1;
+
+    if (ctx->sensors_supported) {
+        SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 125.0f);
+        SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 125.0f);
+
+
+        ctx->accelScale = SDL_STANDARD_GRAVITY / FLYDIGI_ACCEL_SCALE;
+    }
+
+    return true;
+}
+
+static bool HIDAPI_DriverFlydigi_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
+{
+    SDL_DriverFlydigi_Context *ctx = (SDL_DriverFlydigi_Context *)device->context;
+    if (ctx->rumble_supported) {
+        Uint8 rumble_packet[4] = { FLYDIGI_CMD_REPORT_ID, FLYDIGI_HAPTIC_COMMAND, 0x00, 0x00 };
+        rumble_packet[2] = low_frequency_rumble >> 8;
+        rumble_packet[3] = high_frequency_rumble >> 8;
+
+        if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
+            return SDL_SetError("Couldn't send rumble packet");
+        }
+        return true;
+    } else {
+        return SDL_Unsupported();
+    }
+}
+
+static bool HIDAPI_DriverFlydigi_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
+{
+    return SDL_Unsupported();
+}
+
+static Uint32 HIDAPI_DriverFlydigi_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
+{
+    SDL_DriverFlydigi_Context *ctx = (SDL_DriverFlydigi_Context *)device->context;
+    Uint32 caps = 0;
+    if (ctx->rumble_supported) {
+        caps |= SDL_JOYSTICK_CAP_RUMBLE;
+    }
+    return caps;
+}
+
+static bool HIDAPI_DriverFlydigi_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
+{
+    return SDL_Unsupported();
+}
+
+static bool HIDAPI_DriverFlydigi_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
+{
+    return SDL_Unsupported();
+}
+
+static bool HIDAPI_DriverFlydigi_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
+{
+    SDL_DriverFlydigi_Context *ctx = (SDL_DriverFlydigi_Context *)device->context;
+    if (ctx->sensors_supported) {
+        ctx->sensors_enabled = enabled;
+        return true;
+    }
+    return SDL_Unsupported();
+}
+static void HIDAPI_DriverFlydigi_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverFlydigi_Context *ctx, Uint8 *data, int size)
+{
+    Sint16 axis;
+    Uint64 timestamp = SDL_GetTicksNS();
+    if (data[0] != 0x04 && data[0] != 0xFE) {
+        // We don't know how to handle this report
+        return;
+    }
+
+    if (ctx->last_state[8] != data[8]) {
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_FLYDIGI_FN, ((data[8] & 0x01) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[8] & 0x08) != 0));
+    }
+
+    if (ctx->last_state[9] != data[9]) {
+        Uint8 hat;
+
+        switch (data[9] & 0x0F) {
+        case 0b0001:
+            hat = SDL_HAT_UP;
+            break;
+        case 0b0011:
+            hat = SDL_HAT_RIGHTUP;
+            break;
+        case 0b0010:
+            hat = SDL_HAT_RIGHT;
+            break;
+        case 0b0110:
+            hat = SDL_HAT_RIGHTDOWN;
+            break;
+        case 0b0100:
+            hat = SDL_HAT_DOWN;
+            break;
+        case 0b1100:
+            hat = SDL_HAT_LEFTDOWN;
+            break;
+        case 0b1000:
+            hat = SDL_HAT_LEFT;
+            break;
+        case 0b1001:
+            hat = SDL_HAT_LEFTUP;
+            break;
+        default:
+            hat = SDL_HAT_CENTERED;
+            break;
+        }
+        SDL_SendJoystickHat(timestamp, joystick, 0, hat);
+
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[9] & 0x10) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[9] & 0x20) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[9] & 0x40) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[9] & 0x80) != 0));
+    }
+
+    if (ctx->last_state[10] != data[10]) {
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[10] & 0x01) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[10] & 0x02) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[10] & 0x04) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[10] & 0x08) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[10] & 0x40) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[10] & 0x80) != 0));
+    }
+
+    if (ctx->last_state[7] != data[7]) {
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_FLYDIGI_C, ((data[7] & 0x01) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_FLYDIGI_Z, ((data[7] & 0x02) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_FLYDIGI_M1, ((data[7] & 0x04) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_FLYDIGI_M2, ((data[7] & 0x08) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_FLYDIGI_M3, ((data[7] & 0x10) != 0));
+        SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_FLYDIGI_M4, ((data[7] & 0x20) != 0));
+    }
+
+#define READ_STICK_AXIS(offset) \
+    (data[offset] == 0x7f ? 0 : (Sint16)HIDAPI_RemapVal((float)((int)data[offset] - 0x7f), -0x7f, 0xff - 0x7f, SDL_MIN_SINT16, SDL_MAX_SINT16))
+    {
+        axis = READ_STICK_AXIS(17);
+        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
+        axis = READ_STICK_AXIS(19);
+        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
+        axis = READ_STICK_AXIS(21);
+        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
+        axis = READ_STICK_AXIS(22);
+        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
+    }
+#undef READ_STICK_AXIS
+
+#define READ_TRIGGER_AXIS(offset) \
+    (Sint16)(((int)data[offset] * 257) - 32768)
+    {
+        axis = READ_TRIGGER_AXIS(23);
+        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
+        axis = READ_TRIGGER_AXIS(24);
+        SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
+    }
+#undef READ_TRIGGER_AXIS
+
+    if (ctx->sensors_enabled) {
+        Uint64 sensor_timestamp;
+        float values[3];
+
+        // Note: we cannot use the time stamp of the receiving computer due to packet delay creating "spiky" timings.
+        // The imu time stamp is intended to be the sample time of the on-board hardware.
+        // In the absence of time stamp data from the data[], we can simulate that by
+        // advancing a time stamp by the observed/known imu clock rate. This is 8ms = 125 Hz
+        sensor_timestamp = ctx->sensor_timestamp;
+        ctx->sensor_timestamp += SENSOR_INTERVAL_NS;
+
+        // This device's IMU values are reported differently from SDL
+        // Thus we perform a rotation of the coordinate system to match the SDL standard.
+        values[0] = -LOAD16(data[26], data[27]) * DEG2RAD(65536) / INT16_MAX;  // Rotation around pitch axis
+        values[1] = -LOAD16(data[18], data[20]) * DEG2RAD(65536) / INT16_MAX;   // Rotation around yaw axis
+        values[2] = -LOAD16(data[29], data[30]) * DEG2RAD(1024) / INT16_MAX;  // Rotation around roll axis
+        SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, values, 3);
+
+        values[0] = -LOAD16(data[11], data[12])  * ctx->accelScale; // Acceleration along pitch axis
+        values[1] = LOAD16(data[15], data[16]) * ctx->accelScale;  // Acceleration along yaw axis
+        values[2] = LOAD16(data[13], data[14]) * ctx->accelScale; // Acceleration along roll axis
+        SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, values, 3);
+    }
+
+    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
+}
+
+static bool HIDAPI_DriverFlydigi_UpdateDevice(SDL_HIDAPI_Device *device)
+{
+    SDL_DriverFlydigi_Context *ctx = (SDL_DriverFlydigi_Context *)device->context;
+    SDL_Joystick *joystick = NULL;
+    Uint8 data[USB_PACKET_LENGTH];
+    int size = 0;
+
+    if (device->num_joysticks > 0) {
+        joystick = SDL_GetJoystickFromID(device->joysticks[0]);
+    } else {
+        return false;
+    }
+
+    while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
+#ifdef DEBUG_FLYDIGI_PROTOCOL
+        HIDAPI_DumpPacket("Flydigi packet: size = %d", data, size);
+#endif
+        if (!joystick) {
+            continue;
+        }
+
+        HIDAPI_DriverFlydigi_HandleStatePacket(joystick, ctx, data, size);
+    }
+
+    if (size < 0) {
+        // Read error, device is disconnected
+        HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
+    }
+    return (size >= 0);
+}
+
+static void HIDAPI_DriverFlydigi_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
+{
+}
+
+static void HIDAPI_DriverFlydigi_FreeDevice(SDL_HIDAPI_Device *device)
+{
+}
+
+SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverFlydigi = {
+    SDL_HINT_JOYSTICK_HIDAPI_FLYDIGI,
+    true,
+    HIDAPI_DriverFlydigi_RegisterHints,
+    HIDAPI_DriverFlydigi_UnregisterHints,
+    HIDAPI_DriverFlydigi_IsEnabled,
+    HIDAPI_DriverFlydigi_IsSupportedDevice,
+    HIDAPI_DriverFlydigi_InitDevice,
+    HIDAPI_DriverFlydigi_GetDevicePlayerIndex,
+    HIDAPI_DriverFlydigi_SetDevicePlayerIndex,
+    HIDAPI_DriverFlydigi_UpdateDevice,
+    HIDAPI_DriverFlydigi_OpenJoystick,
+    HIDAPI_DriverFlydigi_RumbleJoystick,
+    HIDAPI_DriverFlydigi_RumbleJoystickTriggers,
+    HIDAPI_DriverFlydigi_GetJoystickCapabilities,
+    HIDAPI_DriverFlydigi_SetJoystickLED,
+    HIDAPI_DriverFlydigi_SendJoystickEffect,
+    HIDAPI_DriverFlydigi_SetJoystickSensorsEnabled,
+    HIDAPI_DriverFlydigi_CloseJoystick,
+    HIDAPI_DriverFlydigi_FreeDevice,
+};
+
+#endif // SDL_JOYSTICK_HIDAPI_FLYDIGI
+
+#endif // SDL_JOYSTICK_HIDAPI
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 2c9f7937d8061..5d26deafe8e3c 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -94,6 +94,9 @@ static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
 #ifdef SDL_JOYSTICK_HIDAPI_8BITDO
     &SDL_HIDAPI_Driver8BitDo,
 #endif
+#ifdef SDL_JOYSTICK_HIDAPI_FLYDIGI
+    &SDL_HIDAPI_DriverFlydigi,
+#endif
 };
 static int SDL_HIDAPI_numdrivers = 0;
 static SDL_AtomicInt SDL_HIDAPI_updating_devices;
@@ -351,7 +354,7 @@ static SDL_HIDAPI_DeviceDriver *HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device
         return NULL;
     }
 
-    if (device->vendor_id != USB_VENDOR_VALVE) {
+    if (device->vendor_id != USB_VENDOR_VALVE && device->vendor_id != USB_VENDOR_FLYDIGI) {
         if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
             return NULL;
         }
diff --git a/src/joystick/hidapi/SDL_hidapijoystick_c.h b/src/joystick/hidapi/SDL_hidapijoystick_c.h
index b4ba03c1150a4..f6b8ebfae4053 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick_c.h
+++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h
@@ -42,6 +42,7 @@
 #define SDL_JOYSTICK_HIDAPI_STEAM_HORI
 #define SDL_JOYSTICK_HIDAPI_LG4FF
 #define SDL_JOYSTICK_HIDAPI_8BITDO
+#define SDL_JOYSTICK_HIDAPI_FLYDIGI
 #define SDL_JOYSTICK_HIDAPI_GIP
 
 // Joystick capability definitions
@@ -163,6 +164,7 @@ extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne;
 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteamHori;
 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverLg4ff;
 extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_Driver8BitDo;
+extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverFlydigi;
 
 // Return true if a HID device is present and supported as a joystick of the given type
 extern bool HIDAPI_IsDeviceTypePresent(SDL_GamepadType type);
diff --git a/src/joystick/usb_ids.h b/src/joystick/usb_ids.h
index 26059aa289066..8da941326ce65 100644
--- a/src/joystick/usb_ids.h
+++ b/src/joystick/usb_ids.h
@@ -32,6 +32,7 @@
 #define USB_VENDOR_BACKBONE     0x358a
 #define USB_VENDOR_GAMESIR      0x3537
 #define USB_VENDOR_DRAGONRISE   0x0079
+#define USB_VENDOR_FLYDIGI      0x04B4
 #define USB_VENDOR_GOOGLE       0x18d1
 #define USB_VENDOR_HORI         0x0f0d
 #define USB_VENDOR_HP           0x03f0
@@ -77,6 +78,7 @@
 #define USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER1            0x1843
 #define USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER2            0x1844
 #define USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER3            0x1846
+#define USB_PRODUCT_FLYDIGI_VADER4_PRO                    0x2412
 #define USB_PRODUCT_HORI_FIGHTING_STICK_ALPHA_PS4         0x011c
 #define USB_PRODUCT_HORI_FIGHTING_STICK_ALPHA_PS5         0x0184
 #define USB_PRODUCT_HORI_FIGHTING_STICK_ALPHA_PS5         0x0184
@@ -160,6 +162,7 @@
 // USB usage pages
 #define USB_USAGEPAGE_GENERIC_DESKTOP 0x0001
 #define USB_USAGEPAGE_BUTTON          0x0009
+#define USB_USAGEPAGE_VENDOR_FLYDIGI  0xFFA0
 
 // USB usages for USAGE_PAGE_GENERIC_DESKTOP
 #define USB_USAGE_GENERIC_POINTER             0x0001