SDL: Fix compilation errors with GameInput v4

From 5725487eb6dde032bcbb46cf6b7cd71a1666f97a Mon Sep 17 00:00:00 2001
From: Nintorch <[EMAIL REDACTED]>
Date: Wed, 21 Oct 2026 14:07:49 +0500
Subject: [PATCH] Fix compilation errors with GameInput v4

This PR fixes the compilation errors introduced with updating to GameInput v4.
---
 src/joystick/gdk/SDL_gameinputjoystick.cpp | 254 +++++++++++++++++----
 src/video/windows/SDL_windowsgameinput.cpp |  67 +++++-
 2 files changed, 273 insertions(+), 48 deletions(-)

diff --git a/src/joystick/gdk/SDL_gameinputjoystick.cpp b/src/joystick/gdk/SDL_gameinputjoystick.cpp
index a92ef06ab3cf8..525ff1cb7b88e 100644
--- a/src/joystick/gdk/SDL_gameinputjoystick.cpp
+++ b/src/joystick/gdk/SDL_gameinputjoystick.cpp
@@ -53,7 +53,9 @@ typedef struct GAMEINPUT_InternalDevice
     char *name;
     SDL_GUID guid;          // generated by SDL
     SDL_JoystickID device_instance; // generated by SDL
+#if GAMEINPUT_API_VERSION < 4
     const GameInputDeviceInfo *info;
+#endif
     Uint16 vendor;
     Uint16 product;
     int raw_type;
@@ -73,7 +75,9 @@ typedef struct joystick_hwdata
     GAMEINPUT_InternalDevice *devref;
     bool report_sensors;
     GameInputRumbleParams rumbleParams;
+#if GAMEINPUT_API_VERSION < 4 // g_pGameInput->RegisterSystemButtonCallback no longer exists in GameInput v4, so no need for a callback token
     GameInputCallbackToken system_button_callback_token;
+#endif
 } GAMEINPUT_InternalJoystickHwdata;
 
 static GAMEINPUT_InternalList g_GameInputList = { NULL };
@@ -81,23 +85,52 @@ static IGameInput *g_pGameInput = NULL;
 static GameInputCallbackToken g_GameInputCallbackToken = 0;
 static Uint64 g_GameInputTimestampOffset;
 
-
-static bool GAMEINPUT_InternalIsGamepad(const GameInputDeviceInfo *info)
+#if GAMEINPUT_API_VERSION < 4
+static const GameInputDeviceInfo *GAMEINPUT_GetGameInputDeviceInfo(IGameInputDevice *pDevice)
 {
-    if (info->supportedInput & GameInputKindGamepad) {
-        return true;
+    const GameInputDeviceInfo *info = NULL;
+#if GAMEINPUT_API_VERSION >= 1
+    HRESULT hr = pDevice->GetDeviceInfo(&info);
+    if (FAILED(hr)) {
+        WIN_SetErrorFromHRESULT("IGameInputDevice::GetDeviceInfo", hr);
+        return NULL;
     }
-    return false;
+#else
+    info = pDevice->GetDeviceInfo();
+#endif
+    return info;
 }
+#endif
 
-static Uint8 GAMEINPUT_GetDeviceRawType(const GameInputDeviceInfo *info)
+static bool GAMEINPUT_InternalIsGamepad(IGameInputDevice *pDevice)
+{
+#if GAMEINPUT_API_VERSION >= 4
+    GameInputKind supportedInput = pDevice->GetSupportedInput();
+#else
+    GameInputKind supportedInput = GAMEINPUT_GetGameInputDeviceInfo(pDevice)->supportedInput;
+#endif
+    return (supportedInput & GameInputKindGamepad) != 0;
+}
+
+static Uint8 GAMEINPUT_GetDeviceRawType(IGameInputDevice *pDevice)
 {
 #if GAMEINPUT_API_VERSION >= 3
+
+#if GAMEINPUT_API_VERSION >= 4
+    GameInputKind supportedInput = pDevice->GetSupportedInput();
+    uint16_t vendorId = pDevice->GetVendorId();
+    uint16_t productId = pDevice->GetProductId();
+#else
+    const GameInputDeviceInfo *info = GAMEINPUT_GetGameInputDeviceInfo(pDevice);
     GameInputKind supportedInput = info->supportedInput;
+    uint16_t vendorId = info->vendorId;
+    uint16_t productId = info->productId;
+#endif
+
     if (supportedInput & GameInputKindRawDeviceReport) {
-        switch (info->vendorId) {
+        switch (vendorId) {
             case USB_VENDOR_MADCATZ:
-                switch (info->productId) {
+                switch (productId) {
                     case USB_PRODUCT_MADCATZ_XB1_STRATOCASTER_GUITAR:
                         return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR;
                     case USB_PRODUCT_MADCATZ_XB1_DRUM_KIT:
@@ -109,7 +142,7 @@ static Uint8 GAMEINPUT_GetDeviceRawType(const GameInputDeviceInfo *info)
                 }
                 break;
             case USB_VENDOR_PDP:
-                switch (info->productId) {
+                switch (productId) {
                     case USB_PRODUCT_PDP_XB1_JAGUAR_GUITAR:
                     case USB_PRODUCT_PDP_XB1_RIFFMASTER_GUITAR:
                         return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR;
@@ -120,7 +153,7 @@ static Uint8 GAMEINPUT_GetDeviceRawType(const GameInputDeviceInfo *info)
                 }
                 break;
             case USB_VENDOR_CRKD:
-                switch (info->productId) {
+                switch (productId) {
                     case USB_PRODUCT_RED_OCTANE_XB1_STAGE_TOUR_GUITAR:
                         return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR;
                     default:
@@ -128,7 +161,7 @@ static Uint8 GAMEINPUT_GetDeviceRawType(const GameInputDeviceInfo *info)
                 }
                 break;
             case USB_VENDOR_RED_OCTANE:
-                switch (info->productId) {
+                switch (productId) {
                     case USB_PRODUCT_RED_OCTANE_XB1_GUITAR_HERO_LIVE_GUITAR:
                         return SDL_GAMEINPUT_RAWTYPE_GUITAR_HERO_LIVE_GUITAR;
                     default:
@@ -136,7 +169,7 @@ static Uint8 GAMEINPUT_GetDeviceRawType(const GameInputDeviceInfo *info)
                 }
                 break;
             case USB_VENDOR_RED_OCTANE_GAMES:
-                switch (info->productId) {
+                switch (productId) {
                     case USB_PRODUCT_RED_OCTANE_XB1_STAGE_TOUR_GUITAR:
                         return SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR;
                     case USB_PRODUCT_RED_OCTANE_XB1_STAGE_TOUR_DRUMS:
@@ -150,10 +183,14 @@ static Uint8 GAMEINPUT_GetDeviceRawType(const GameInputDeviceInfo *info)
 #endif // GAMEINPUT_API_VERSION >= 3
     return SDL_GAMEINPUT_RAWTYPE_NONE;
 }
-static Uint8 GAMEINPUT_GetDeviceSubtype(const GameInputDeviceInfo *info)
+static Uint8 GAMEINPUT_GetDeviceSubtype(IGameInputDevice *pDevice)
 {
-    GameInputKind supportedInput = info->supportedInput;
-    Uint8 rawType = GAMEINPUT_GetDeviceRawType(info);
+#if GAMEINPUT_API_VERSION >= 4
+    GameInputKind supportedInput = pDevice->GetSupportedInput();
+#else
+    GameInputKind supportedInput = GAMEINPUT_GetGameInputDeviceInfo(pDevice)->supportedInput;
+#endif
+    Uint8 rawType = GAMEINPUT_GetDeviceRawType(pDevice);
     if (rawType == SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_GUITAR || rawType == SDL_GAMEINPUT_RAWTYPE_GUITAR_HERO_LIVE_GUITAR) {
         return SDL_JOYSTICK_TYPE_GUITAR;
     }
@@ -200,9 +237,9 @@ static bool IsXbox360WirelessAdapter(Uint16 vendor, Uint16 product)
     return false;
 }
 
-static void QueryDeviceName(const GameInputDeviceInfo *info, Uint16 vendor_id, Uint16 product_id, char **manufacturer_string, char **product_string)
+static void QueryDeviceName(IGameInputDevice *pDevice, Uint16 vendor_id, Uint16 product_id, char **manufacturer_string, char **product_string)
 {
-    if (!info || !manufacturer_string || !product_string) {
+    if (!pDevice || !manufacturer_string || !product_string) {
         return;
     }
 
@@ -215,6 +252,15 @@ static void QueryDeviceName(const GameInputDeviceInfo *info, Uint16 vendor_id, U
 #endif
 
     *manufacturer_string = NULL;
+#if GAMEINPUT_API_VERSION >= 4
+    char buffer[256];
+    if (pDevice->GetDisplayName(sizeof(buffer), buffer) > 0) {
+        *product_string = SDL_strdup(buffer);
+    } else {
+        *product_string = NULL;
+    }
+#else
+    const GameInputDeviceInfo *info = GAMEINPUT_GetGameInputDeviceInfo(pDevice);
 #if GAMEINPUT_API_VERSION >= 1
     if (info->displayName) {
         *product_string = SDL_strdup(info->displayName);
@@ -227,13 +273,16 @@ static void QueryDeviceName(const GameInputDeviceInfo *info, Uint16 vendor_id, U
     if (!info->displayName) {
         *product_string = NULL;
     }
+#endif
 }
 
 static void GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
 {
     GAMEINPUT_InternalDevice **devicelist = NULL;
     GAMEINPUT_InternalDevice *elem = NULL;
+#if GAMEINPUT_API_VERSION < 4
     const GameInputDeviceInfo *info = NULL;
+#endif
     Uint16 bus = SDL_HARDWARE_BUS_USB;
     Uint16 vendor = 0;
     Uint16 product = 0;
@@ -248,6 +297,10 @@ static void GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
 
     SDL_AssertJoysticksLocked();
 
+#if GAMEINPUT_API_VERSION >= 4
+    vendor = pDevice->GetVendorId();
+    product = pDevice->GetProductId();
+#else
 #if GAMEINPUT_API_VERSION >= 1
     HRESULT hr = pDevice->GetDeviceInfo(&info);
     if (FAILED(hr)) {
@@ -257,18 +310,20 @@ static void GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
 #else
     info = pDevice->GetDeviceInfo();
 #endif
+    vendor = info->vendorId;
+    product = info->productId;
+#endif
+
     if (false /*info->capabilities & GameInputDeviceCapabilityWireless*/) {
         bus = SDL_HARDWARE_BUS_BLUETOOTH;
     } else {
         bus = SDL_HARDWARE_BUS_USB;
     }
-    vendor = info->vendorId;
-    product = info->productId;
     //version = (info->firmwareVersion.major << 8) | info->firmwareVersion.minor;
-    subtype = GAMEINPUT_GetDeviceSubtype(info);
-    raw_type = GAMEINPUT_GetDeviceRawType(info);
+    subtype = GAMEINPUT_GetDeviceSubtype(pDevice);
+    raw_type = GAMEINPUT_GetDeviceRawType(pDevice);
 
-    QueryDeviceName(info, vendor, product, &manufacturer_string, &product_string);
+    QueryDeviceName(pDevice, vendor, product, &manufacturer_string, &product_string);
 
     if (IsXbox360WirelessAdapter(vendor, product) ||
         SDL_ShouldIgnoreJoystick(vendor, product, version, product_string) ||
@@ -278,12 +333,18 @@ static void GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
 
 #if defined(SDL_JOYSTICK_DINPUT) && defined(SDL_HAPTIC_DINPUT)
     // This joystick backend currently doesn't provide a haptic backend, so fallback to DirectInput for haptic-capable devices.
-    if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_DIRECTINPUT, true) && info->forceFeedbackMotorCount > 0 && pDevice->IsForceFeedbackMotorPoweredOn(0)) {
+    if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_DIRECTINPUT, true) &&
+#if GAMEINPUT_API_VERSION >= 4
+        pDevice->GetForceFeedbackMotorCount() > 0 &&
+#else
+        info->forceFeedbackMotorCount > 0 &&
+#endif
+        pDevice->IsForceFeedbackMotorPoweredOn(0)) {
         goto done;
     }
 #endif
 
-    if (GAMEINPUT_InternalIsGamepad(info)) {
+    if (GAMEINPUT_InternalIsGamepad(pDevice)) {
         if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_GAMEINPUT, SDL_GAMEINPUT_DEFAULT)) {
             goto done;
         }
@@ -294,7 +355,12 @@ static void GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
             goto done;
         }
 #endif
-        if (info->supportedInput & GameInputKindController) {
+#if GAMEINPUT_API_VERSION >= 4
+        if (pDevice->GetSupportedInput() & GameInputKindController)
+#else
+        if (info->supportedInput & GameInputKindController)
+#endif
+        {
             // Maintain GUID compatibility with DirectInput controller mappings.
             driver_signature = 0;
             subtype = 0;
@@ -325,21 +391,36 @@ static void GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
     }
 
     // Generate a device path
+#if GAMEINPUT_API_VERSION >= 4
+    APP_LOCAL_DEVICE_ID deviceId;
+    pDevice->GetDeviceId(&deviceId);
+    for (idx = 0; idx < APP_LOCAL_DEVICE_ID_SIZE; ++idx) {
+        SDL_snprintf(tmp, SDL_arraysize(tmp), "%02X", deviceId.value[idx]);
+        SDL_strlcat(elem->path, tmp, SDL_arraysize(elem->path));
+    }
+#else
     for (idx = 0; idx < APP_LOCAL_DEVICE_ID_SIZE; ++idx) {
         SDL_snprintf(tmp, SDL_arraysize(tmp), "%02X", info->deviceId.value[idx]);
         SDL_strlcat(elem->path, tmp, SDL_arraysize(elem->path));
     }
+#endif
 
     pDevice->AddRef();
     elem->device = pDevice;
     elem->name = SDL_CreateJoystickName(vendor, product, manufacturer_string, product_string);
     elem->guid = SDL_CreateJoystickGUID(bus, vendor, product, version, manufacturer_string, product_string, driver_signature, subtype);
     elem->device_instance = SDL_GetNextObjectID();
+#if GAMEINPUT_API_VERSION < 4
     elem->info = info;
+#endif
     elem->vendor = vendor;
     elem->product = product;
     elem->raw_type = raw_type;
-#if GAMEINPUT_API_VERSION >= 1
+#if GAMEINPUT_API_VERSION >= 4
+    char pnpPath[256];
+    pDevice->GetPnpPath(sizeof(pnpPath), pnpPath);
+    elem->steam_virtual_gamepad_slot = GetSteamVirtualGamepadSlot(pnpPath);
+#elif GAMEINPUT_API_VERSION >= 1
     elem->steam_virtual_gamepad_slot = GetSteamVirtualGamepadSlot(info->pnpPath);
 #else
     elem->steam_virtual_gamepad_slot = -1;
@@ -399,10 +480,15 @@ static GAMEINPUT_InternalDevice *GAMEINPUT_InternalFindByIndex(int idx)
 static void CALLBACK GAMEINPUT_InternalJoystickDeviceCallback(
     _In_ GameInputCallbackToken callbackToken,
     _In_ void *context,
+#if GAMEINPUT_API_VERSION >= 4
+    _In_ GameInputDeviceEvent deviceEvent,
+    _In_ IGameInputDevice* device)
+#else
     _In_ IGameInputDevice *device,
     _In_ uint64_t timestamp,
     _In_ GameInputDeviceStatus currentStatus,
     _In_ GameInputDeviceStatus previousStatus)
+#endif
 {
     int idx = 0;
     GAMEINPUT_InternalDevice *elem = NULL;
@@ -414,7 +500,12 @@ static void CALLBACK GAMEINPUT_InternalJoystickDeviceCallback(
 
     SDL_LockJoysticks();
 
-    if (currentStatus & GameInputDeviceConnected) {
+#if GAMEINPUT_API_VERSION >= 4
+    if (device->IsConnected())
+#else
+    if (currentStatus & GameInputDeviceConnected)
+#endif
+    {
         GAMEINPUT_InternalAddOrFind(device);
     } else {
         for (idx = 0; idx < g_GameInputList.count; ++idx) {
@@ -454,9 +545,11 @@ static bool GAMEINPUT_JoystickInit(void)
         return false;
     }
 
-#if GAMEINPUT_API_VERSION >= 2
     // Allow background controller input
     // SDL manages focus policy at a higher level, so we can set this unconditionally.
+#if GAMEINPUT_API_VERSION >= 4
+    g_pGameInput->EnableBackgroundInput(true);
+#elif GAMEINPUT_API_VERSION >= 2
     g_pGameInput->SetFocusPolicy(GameInputEnableBackgroundInput | GameInputEnableBackgroundGuideButton | GameInputEnableBackgroundShareButton);
 #endif
 
@@ -472,8 +565,10 @@ static bool GAMEINPUT_JoystickInit(void)
 
     hr = g_pGameInput->RegisterDeviceCallback(NULL,
                                            kind,
+#if GAMEINPUT_API_VERSION < 4
                                            GameInputDeviceConnected,
                                            GameInputBlockingEnumeration,
+#endif
                                            NULL,
                                            GAMEINPUT_InternalJoystickDeviceCallback,
                                            &g_GameInputCallbackToken);
@@ -503,6 +598,7 @@ static void GAMEINPUT_JoystickDetect(void)
 {
     int idx;
     GAMEINPUT_InternalDevice *elem = NULL;
+    bool connected;
 
     SDL_AssertJoysticksLocked();
 
@@ -517,7 +613,12 @@ static void GAMEINPUT_JoystickDetect(void)
             elem->isAdded = true;
         }
 
-        if (elem->isDeleteRequested || !(elem->device->GetDeviceStatus() & GameInputDeviceConnected)) {
+#if GAMEINPUT_API_VERSION >= 4
+        connected = elem->device->IsConnected();
+#else
+        connected = elem->device->GetDeviceStatus() & GameInputDeviceConnected;
+#endif
+        if (elem->isDeleteRequested || !connected) {
             SDL_PrivateJoystickRemoved(elem->device_instance);
             GAMEINPUT_InternalRemoveByIndex(idx--);
         }
@@ -539,7 +640,12 @@ static bool GAMEINPUT_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_i
 
         for (int i = 0; i < g_GameInputList.count; ++i) {
             GAMEINPUT_InternalDevice *elem = g_GameInputList.devices[i];
-            if (elem && vendor_id == elem->info->vendorId && product_id == elem->info->productId) {
+#if GAMEINPUT_API_VERSION >= 4
+            if (elem && vendor_id == elem->device->GetVendorId() && product_id == elem->device->GetProductId())
+#else
+            if (elem && vendor_id == elem->info->vendorId && product_id == elem->info->productId)
+#endif
+            {
                 return true;
             }
         }
@@ -616,7 +722,7 @@ static void GAMEINPUT_UpdatePowerInfo(SDL_Joystick *joystick, IGameInputDevice *
 #endif
 }
 
-#if GAMEINPUT_API_VERSION >= 1
+#if GAMEINPUT_API_VERSION >= 1 && GAMEINPUT_API_VERSION < 4
 static void CALLBACK GAMEINPUT_InternalSystemButtonCallback(
     _In_ GameInputCallbackToken callbackToken,
     _In_ void * context,
@@ -648,13 +754,18 @@ static void CALLBACK GAMEINPUT_InternalSystemButtonCallback(
 static bool GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
 {
     GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);
+#if GAMEINPUT_API_VERSION < 4
     const GameInputDeviceInfo *info = elem->info;
+#endif
     GAMEINPUT_InternalJoystickHwdata *hwdata = NULL;
-
+    GameInputRumbleMotors supportedRumbleMotors;
+    IGameInputDevice *device;
     if (!elem) {
         return false;
     }
 
+    device = elem->device;
+
     hwdata = (GAMEINPUT_InternalJoystickHwdata *)SDL_calloc(1, sizeof(*hwdata));
     if (!hwdata) {
         return false;
@@ -663,12 +774,17 @@ static bool GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
     hwdata->devref = elem;
 
     joystick->hwdata = hwdata;
-    if (GAMEINPUT_InternalIsGamepad(info) || GAMEINPUT_GetDeviceRawType(info) != SDL_GAMEINPUT_RAWTYPE_NONE) {
+    if (GAMEINPUT_InternalIsGamepad(device) || GAMEINPUT_GetDeviceRawType(device) != SDL_GAMEINPUT_RAWTYPE_NONE) {
         joystick->naxes = 6;
         joystick->nbuttons = 11;
         joystick->nhats = 1;
 
-#if GAMEINPUT_API_VERSION >= 1
+#if GAMEINPUT_API_VERSION >= 4
+        if (SDL_IsJoystickXboxSeriesX(elem->vendor, elem->product) &&
+            device->GetSupportedSystemButtons() & GameInputSystemButtonShare) {
+            ++joystick->nbuttons;
+        }
+#elif GAMEINPUT_API_VERSION >= 1
         if (info->supportedSystemButtons != GameInputSystemButtonNone) {
             GameInputSystemButtons buttons = GameInputSystemButtonGuide;
 
@@ -682,7 +798,11 @@ static bool GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         }
 #endif // GAMEINPUT_API_VERSION >= 1
     } else {
-#if GAMEINPUT_API_VERSION >= 3
+#if GAMEINPUT_API_VERSION >= 4
+        joystick->naxes = device->GetControllerAxisCount();
+        joystick->nbuttons = device->GetControllerButtonCount();
+        joystick->nhats = device->GetControllerSwitchCount();
+#elif GAMEINPUT_API_VERSION >= 3
         joystick->naxes = info->controllerInfo->controllerAxisCount;
         joystick->nbuttons = info->controllerInfo->controllerButtonCount;
         joystick->nhats = info->controllerInfo->controllerSwitchCount;
@@ -693,14 +813,30 @@ static bool GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
 #endif // GAMEINPUT_API_VERSION >= 3
     }
 
-    if (info->supportedRumbleMotors & (GameInputRumbleLowFrequency | GameInputRumbleHighFrequency)) {
+#if GAMEINPUT_API_VERSION >= 4
+    supportedRumbleMotors = device->GetSupportedRumbleMotors();
+#else
+    supportedRumbleMotors = info->supportedRumbleMotors;
+#endif
+    if (supportedRumbleMotors & (GameInputRumbleLowFrequency | GameInputRumbleHighFrequency)) {
         SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true);
     }
-    if (info->supportedRumbleMotors & (GameInputRumbleLeftTrigger | GameInputRumbleRightTrigger)) {
+    if (supportedRumbleMotors & (GameInputRumbleLeftTrigger | GameInputRumbleRightTrigger)) {
         SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, true);
     }
 
-#if GAMEINPUT_API_VERSION >= 3
+#if GAMEINPUT_API_VERSION >= 4
+    if (device->GetSupportedInput() & GameInputKindSensors) {
+        GameInputSensorsInfo sensorsInfo;
+        device->GetSensorsInfo(&sensorsInfo);
+        if (sensorsInfo.supportedSensors & GameInputSensorsGyrometer) {
+            SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 60.0f);
+        }
+        if (sensorsInfo.supportedSensors & GameInputSensorsAccelerometer) {
+            SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 60.0f);
+        }
+    }
+#elif GAMEINPUT_API_VERSION >= 3
     if (info->supportedInput & GameInputKindSensors) {
         if (info->sensorsInfo->supportedSensors & GameInputSensorsGyrometer) {
             SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 60.0f);
@@ -875,6 +1011,15 @@ static void GAMEINPUT_GamepadUpdate(SDL_Joystick *joystick, IGameInputReading *r
             bool down = ((state.buttons & button_mask) != 0);
             SDL_SendJoystickButton(timestamp, joystick, btnidx, down);
         }
+#if GAMEINPUT_API_VERSION >= 4
+        GameInputSystemButtons systemButtons;
+        if (reading->GetSystemButtonState(&systemButtons)) {
+            SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, (systemButtons & GameInputSystemButtonGuide) != 0);
+            SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE, (systemButtons & GameInputSystemButtonShare) != 0);
+        }
+#else
+        // GAMEINPUT_InternalSystemButtonCallback is used instead
+#endif
 
         if (state.buttons & GameInputGamepadDPadUp) {
             hat |= SDL_HAT_UP;
@@ -975,12 +1120,18 @@ static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
     GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
     GAMEINPUT_InternalDevice *internal_device = hwdata->devref;
     IGameInputDevice *device = hwdata->devref->device;
+#if GAMEINPUT_API_VERSION < 4
     const GameInputDeviceInfo *info = hwdata->devref->info;
+#endif
     IGameInputReading *reading = NULL;
     Uint64 timestamp;
     HRESULT hr;
 
+#if GAMEINPUT_API_VERSION >= 4
+    hr = g_pGameInput->GetCurrentReading(device->GetSupportedInput(), device, &reading);
+#else
     hr = g_pGameInput->GetCurrentReading(info->supportedInput, device, &reading);
+#endif
     if (FAILED(hr)) {
         // don't SetError here since there can be a legitimate case when there's no reading avail
         return;
@@ -991,7 +1142,7 @@ static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
         GAMEINPUT_GuitarUpdate(joystick, reading, timestamp);
     } else if (internal_device->raw_type == SDL_GAMEINPUT_RAWTYPE_ROCK_BAND_DRUM_KIT) {
         GAMEINPUT_DrumUpdate(joystick, reading, timestamp);
-    } else if (GAMEINPUT_InternalIsGamepad(info)) {
+    } else if (GAMEINPUT_InternalIsGamepad(device)) {
         GAMEINPUT_GamepadUpdate(joystick, reading, timestamp);
     } else {
         GAMEINPUT_ControllerUpdate(joystick, reading, timestamp);
@@ -1002,7 +1153,15 @@ static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
         GameInputSensorsState sensor_state;
 
         if (reading->GetSensorsState(&sensor_state)) {
-            if ((info->sensorsInfo->supportedSensors & GameInputSensorsGyrometer) != 0) {
+            GameInputSensorsKind supportedSensors;
+#if GAMEINPUT_API_VERSION >= 4
+            GameInputSensorsInfo sensorsInfo;
+            device->GetSensorsInfo(&sensorsInfo);
+            supportedSensors = sensorsInfo.supportedSensors;
+#else
+            supportedSensors = info->sensorsInfo->supportedSensors;
+#endif
+            if ((supportedSensors & GameInputSensorsGyrometer) != 0) {
                 float data[3] = {
                     sensor_state.angularVelocityInRadPerSecX,
                     sensor_state.angularVelocityInRadPerSecY,
@@ -1010,7 +1169,7 @@ static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
                 };
                 SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, timestamp, data, SDL_arraysize(data));
             }
-            if ((info->sensorsInfo->supportedSensors & GameInputSensorsAccelerometer) != 0) {
+            if ((supportedSensors & GameInputSensorsAccelerometer) != 0) {
                 float data[3] = {
                     sensor_state.accelerationInGX * SDL_STANDARD_GRAVITY,
                     sensor_state.accelerationInGY * SDL_STANDARD_GRAVITY,
@@ -1032,6 +1191,7 @@ static void GAMEINPUT_JoystickClose(SDL_Joystick *joystick)
 {
     GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
 
+#if GAMEINPUT_API_VERSION < 4
     if (hwdata->system_button_callback_token) {
 #if GAMEINPUT_API_VERSION >= 1
         g_pGameInput->UnregisterCallback(hwdata->system_button_callback_token);
@@ -1039,6 +1199,7 @@ static void GAMEINPUT_JoystickClose(SDL_Joystick *joystick)
         g_pGameInput->UnregisterCallback(hwdata->system_button_callback_token, 10000);
 #endif
     }
+#endif
     SDL_free(hwdata);
 
     joystick->hwdata = NULL;
@@ -1071,7 +1232,7 @@ static bool GAMEINPUT_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
 {
     GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);
 
-    if (!GAMEINPUT_InternalIsGamepad(elem->info) && elem->raw_type == SDL_GAMEINPUT_RAWTYPE_NONE) {
+    if (!GAMEINPUT_InternalIsGamepad(elem->device) && elem->raw_type == SDL_GAMEINPUT_RAWTYPE_NONE) {
         return false;
     }
 
@@ -1091,13 +1252,18 @@ static bool GAMEINPUT_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
     out->back.target = SDL_GAMEPAD_BUTTON_BACK;
 
 #if GAMEINPUT_API_VERSION >= 1
-    if (elem->info->supportedSystemButtons & GameInputSystemButtonGuide) {
+#if GAMEINPUT_API_VERSION >= 4
+    GameInputSystemButtons supportedSystemButtons = elem->device->GetSupportedSystemButtons();
+#else
+    GameInputSystemButtons supportedSystemButtons = elem->info->supportedSystemButtons;
+#endif
+    if (supportedSystemButtons & GameInputSystemButtonGuide) {
         out->guide.kind = EMappingKind_Button;
         out->guide.target = SDL_GAMEPAD_BUTTON_GUIDE;
     }
 
     if (SDL_IsJoystickXboxSeriesX(elem->vendor, elem->product) &&
-        (elem->info->supportedSystemButtons & GameInputSystemButtonShare)) {
+        (supportedSystemButtons & GameInputSystemButtonShare)) {
         out->misc1.kind = EMappingKind_Button;
         out->misc1.target = SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE;
     }
diff --git a/src/video/windows/SDL_windowsgameinput.cpp b/src/video/windows/SDL_windowsgameinput.cpp
index 62edcdb8e46cd..d8976f9f02655 100644
--- a/src/video/windows/SDL_windowsgameinput.cpp
+++ b/src/video/windows/SDL_windowsgameinput.cpp
@@ -46,7 +46,9 @@ static const Uint8 GAMEINPUT_button_map[MAX_GAMEINPUT_BUTTONS] = {
 typedef struct GAMEINPUT_Device
 {
     IGameInputDevice *pDevice;
+#if GAMEINPUT_API_VERSION < 4
     const GameInputDeviceInfo *info;
+#endif
     char *name;
     Uint32 instance_id; // generated by SDL
     bool registered;
@@ -70,8 +72,9 @@ static bool GAMEINPUT_InternalAddOrFind(WIN_GameInputData *data, IGameInputDevic
 {
     GAMEINPUT_Device **devicelist = NULL;
     GAMEINPUT_Device *device = NULL;
-    const GameInputDeviceInfo *info;
     bool result = false;
+#if GAMEINPUT_API_VERSION < 4
+    const GameInputDeviceInfo *info;
 
 #if GAMEINPUT_API_VERSION >= 1
     HRESULT hr = pDevice->GetDeviceInfo(&info);
@@ -80,6 +83,7 @@ static bool GAMEINPUT_InternalAddOrFind(WIN_GameInputData *data, IGameInputDevic
     }
 #else
     info = pDevice->GetDeviceInfo();
+#endif
 #endif
 
     SDL_LockMutex(data->lock);
@@ -105,14 +109,22 @@ static bool GAMEINPUT_InternalAddOrFind(WIN_GameInputData *data, IGameInputDevic
             goto done;
         }
 
-        if (info->displayName) {
+#if GAMEINPUT_API_VERSION >= 4
+        char buffer[256];
+        if (pDevice->GetDisplayName(sizeof(buffer), buffer) > 0)
+#else
+        if (info->displayName)
+#endif
+        {
             // This could give us a product string, but it's NULL for all the devices I've tested
         }
 
         pDevice->AddRef();
         device->pDevice = pDevice;
         device->instance_id = SDL_GetNextObjectID();
+#if GAMEINPUT_API_VERSION < 4
         device->info = info;
+#endif
 
         data->devices = devicelist;
         data->devices[data->num_devices++] = device;
@@ -141,12 +153,21 @@ static bool GAMEINPUT_InternalRemoveByIndex(WIN_GameInputData *data, int idx)
         device = data->devices[idx];
         if (device) {
             if (device->registered) {
+#if GAMEINPUT_API_VERSION >= 4
+                if (device->pDevice->GetSupportedInput() & GameInputKindMouse) {
+                    SDL_RemoveMouse(device->instance_id);
+                }
+                if (device->pDevice->GetSupportedInput() & GameInputKindKeyboard) {
+                    SDL_RemoveKeyboard(device->instance_id);
+                }
+#else
                 if (device->info->supportedInput & GameInputKindMouse) {
                     SDL_RemoveMouse(device->instance_id);
                 }
                 if (device->info->supportedInput & GameInputKindKeyboard) {
                     SDL_RemoveKeyboard(device->instance_id);
                 }
+#endif
                 if (device->last_mouse_reading) {
                     device->last_mouse_reading->Release();
                     device->last_mouse_reading = NULL;
@@ -186,10 +207,15 @@ static bool GAMEINPUT_InternalRemoveByIndex(WIN_GameInputData *data, int idx)
 static void CALLBACK GAMEINPUT_InternalDeviceCallback(
     _In_ GameInputCallbackToken callbackToken,
     _In_ void *context,
+#if GAMEINPUT_API_VERSION >= 4
+    _In_ GameInputDeviceEvent deviceEvent,
+    _In_ IGameInputDevice *pDevice)
+#else
     _In_ IGameInputDevice *pDevice,
     _In_ uint64_t timestamp,
     _In_ GameInputDeviceStatus currentStatus,
     _In_ GameInputDeviceStatus previousStatus)
+#endif
 {
     WIN_GameInputData *data = (WIN_GameInputData *)context;
     int idx = 0;
@@ -200,7 +226,12 @@ static void CALLBACK GAMEINPUT_InternalDeviceCallback(
         return;
     }
 
-    if (currentStatus & GameInputDeviceConnected) {
+#if GAMEINPUT_API_VERSION >= 4
+    if (pDevice->IsConnected())
+#else
+    if (currentStatus & GameInputDeviceConnected)
+#endif
+    {
         GAMEINPUT_InternalAddOrFind(data, pDevice);
     } else {
         for (idx = 0; idx < data->num_devices; ++idx) {
@@ -243,8 +274,10 @@ bool WIN_InitGameInput(SDL_VideoDevice *_this)
 
     hr = data->pGameInput->RegisterDeviceCallback(NULL,
                                                   (GameInputKindMouse | GameInputKindKeyboard),
+#if GAMEINPUT_API_VERSION < 4
                                                   GameInputDeviceConnected,
                                                   GameInputBlockingEnumeration,
+#endif
                                                   data,
                                                   GAMEINPUT_InternalDeviceCallback,
                                                   &data->gameinput_callback_token);
@@ -347,7 +380,13 @@ static void GAMEINPUT_InitialKeyboardReading(WIN_GameInputData *data, SDL_Window
     Uint64 timestamp = SDL_US_TO_NS(reading->GetTimestamp() + data->timestamp_offset);
     SDL_KeyboardID keyboardID = device->instance_id;
 
+#if GAMEINPUT_API_VERSION >= 4
+    GameInputKeyboardInfo keyboardInfo;
+    device->pDevice->GetKeyboardInfo(&keyboardInfo);
+    uint32_t max_keys = keyboardInfo.maxSimultaneousKeys;
+#else
     uint32_t max_keys = device->info->keyboardInfo->maxSimultaneousKeys;
+#endif
     GameInputKeyState *keys = SDL_stack_alloc(GameInputKeyState, max_keys);
     if (!keys) {
         return;
@@ -392,7 +431,13 @@ static void GAMEINPUT_HandleKeyboardDelta(WIN_GameInputData *data, SDL_Window *w
     Uint64 timestamp = SDL_US_TO_NS(reading->GetTimestamp() + data->timestamp_offset);
     SDL_KeyboardID keyboardID = device->instance_id;
 
+#if GAMEINPUT_API_VERSION >= 4
+    GameInputKeyboardInfo keyboardInfo;
+    device->pDevice->GetKeyboardInfo(&keyboardInfo);
+    uint32_t max_keys = keyboardInfo.maxSimultaneousKeys;
+#

(Patch may be truncated, please check the link at the top of this post.)