Commit e8adc648109546b20af2cb8a766648aa1e1f5975

Sam Lantinga 2020-11-27T10:44:55

Enable dispatching of WM_INPUT events directly, in case the application hasn't created a window with the normal message loop

diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c
index 0f505a4..d88e831 100644
--- a/src/joystick/windows/SDL_rawinputjoystick.c
+++ b/src/joystick/windows/SDL_rawinputjoystick.c
@@ -640,6 +640,20 @@ RAWINPUT_GetPendingDeviceChanges(void)
     }
 }
 
+static SDL_bool pump_device_events;
+static void
+RAWINPUT_GetPendingDeviceEvents(void)
+{
+    if (pump_device_events) {
+        MSG msg;
+        while (PeekMessage(&msg, SDL_HelperWindow, WM_INPUT, WM_INPUT + 1, PM_REMOVE)) {
+            TranslateMessage(&msg);
+            DispatchMessage(&msg);
+        }
+        pump_device_events = SDL_FALSE;
+    }
+}
+
 static int
 RAWINPUT_JoystickInit(void)
 {
@@ -673,6 +687,7 @@ RAWINPUT_JoystickInit(void)
 
     /* Get initial controller connect messages */
     RAWINPUT_GetPendingDeviceChanges();
+    pump_device_events = SDL_TRUE;
 
     return 0;
 }
@@ -917,6 +932,8 @@ RAWINPUT_PostUpdate(void)
     guide_button_candidate.joystick = NULL;
 
 #endif /* SDL_JOYSTICK_RAWINPUT_MATCHING */
+
+    pump_device_events = SDL_TRUE;
 }
 
 SDL_bool
@@ -1705,7 +1722,7 @@ RAWINPUT_UpdateOtherAPIs(SDL_Joystick *joystick)
 static void
 RAWINPUT_JoystickUpdate(SDL_Joystick *joystick)
 {
-    /* The input events have been handled in the main loop message pumping */
+    RAWINPUT_GetPendingDeviceEvents();
 
     RAWINPUT_UpdateOtherAPIs(joystick);
 }