SDL: Poll for the initial controller state when using DirectInput (8fb82)

From 8fb82da2265e27d73497b05435896928cc1d19bb Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 20 Mar 2024 10:45:11 -0700
Subject: [PATCH] Poll for the initial controller state when using DirectInput

This fixes getting a deflected axis as the first buffered input reading on a generic USB controller (G-Shark GS-GP702)

(cherry picked from commit 70b0d33106e98176bf44de5d301855d49587fa50)
(cherry picked from commit a9ac34984675213b5e43dd1ad5fe9196114bd318)
---
 src/joystick/windows/SDL_dinputjoystick.c    | 10 +++++++++-
 src/joystick/windows/SDL_windowsjoystick_c.h |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/joystick/windows/SDL_dinputjoystick.c b/src/joystick/windows/SDL_dinputjoystick.c
index 89daf0464dc98..fb46445e1095f 100644
--- a/src/joystick/windows/SDL_dinputjoystick.c
+++ b/src/joystick/windows/SDL_dinputjoystick.c
@@ -855,6 +855,7 @@ int SDL_DINPUT_JoystickOpen(SDL_Joystick *joystick, JoyStick_DeviceData *joystic
     } else if (FAILED(result)) {
         return SetDIerror("IDirectInputDevice8::SetProperty", result);
     }
+    joystick->hwdata->first_update = SDL_TRUE;
 
     /* Poll and wait for initial device state to be populated */
     result = IDirectInputDevice8_Poll(joystick->hwdata->InputDevice);
@@ -1130,7 +1131,14 @@ void SDL_DINPUT_JoystickUpdate(SDL_Joystick *joystick)
         IDirectInputDevice8_Poll(joystick->hwdata->InputDevice);
     }
 
-    if (joystick->hwdata->buffered) {
+    if (joystick->hwdata->first_update) {
+        /* Poll to get the initial state of the joystick */
+        UpdateDINPUTJoystickState_Polled(joystick);
+        joystick->hwdata->first_update = SDL_FALSE;
+        return;
+    }
+
+    if (joystick->hwdata->buffered ) {
         UpdateDINPUTJoystickState_Buffered(joystick);
     } else {
         UpdateDINPUTJoystickState_Polled(joystick);
diff --git a/src/joystick/windows/SDL_windowsjoystick_c.h b/src/joystick/windows/SDL_windowsjoystick_c.h
index c39481ffc23f1..18c39e80388a6 100644
--- a/src/joystick/windows/SDL_windowsjoystick_c.h
+++ b/src/joystick/windows/SDL_windowsjoystick_c.h
@@ -77,6 +77,7 @@ struct joystick_hwdata
     LPDIRECTINPUTDEVICE8 InputDevice;
     DIDEVCAPS Capabilities;
     SDL_bool buffered;
+    SDL_bool first_update;
     input_t Inputs[MAX_INPUTS];
     int NumInputs;
     int NumSliders;