SDL: Fixed continually resetting keyboard and mouse readings

From ca29304ce1ed29d87343792678a25e37b3a1c1d2 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 12 Feb 2025 12:33:17 -0800
Subject: [PATCH] Fixed continually resetting keyboard and mouse readings

---
 src/video/windows/SDL_windowsgameinput.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/src/video/windows/SDL_windowsgameinput.c b/src/video/windows/SDL_windowsgameinput.c
index 79b3d98ab2d05..183733a380470 100644
--- a/src/video/windows/SDL_windowsgameinput.c
+++ b/src/video/windows/SDL_windowsgameinput.c
@@ -469,12 +469,13 @@ void WIN_UpdateGameInput(SDL_VideoDevice *_this)
                         device->last_mouse_reading = reading;
                     }
                     if (hr != GAMEINPUT_E_READING_NOT_FOUND) {
-                        // The last reading is too old, resynchronize
-                        IGameInputReading_Release(device->last_mouse_reading);
-                        device->last_mouse_reading = NULL;
+                        if (SUCCEEDED(IGameInput_GetCurrentReading(data->pGameInput, GameInputKindMouse, device->pDevice, &reading))) {
+                            GAMEINPUT_HandleMouseDelta(data, window, device, device->last_mouse_reading, reading);
+                            IGameInputReading_Release(device->last_mouse_reading);
+                            device->last_mouse_reading = reading;
+                        }
                     }
-                }
-                if (!device->last_mouse_reading) {
+                } else {
                     if (SUCCEEDED(IGameInput_GetCurrentReading(data->pGameInput, GameInputKindMouse, device->pDevice, &reading))) {
                         GAMEINPUT_InitialMouseReading(data, window, device, reading);
                         device->last_mouse_reading = reading;
@@ -498,12 +499,13 @@ void WIN_UpdateGameInput(SDL_VideoDevice *_this)
                             device->last_keyboard_reading = reading;
                         }
                         if (hr != GAMEINPUT_E_READING_NOT_FOUND) {
-                            // The last reading is too old, resynchronize
-                            IGameInputReading_Release(device->last_keyboard_reading);
-                            device->last_keyboard_reading = NULL;
+                            if (SUCCEEDED(IGameInput_GetCurrentReading(data->pGameInput, GameInputKindKeyboard, device->pDevice, &reading))) {
+                                GAMEINPUT_HandleKeyboardDelta(data, window, device, device->last_keyboard_reading, reading);
+                                IGameInputReading_Release(device->last_keyboard_reading);
+                                device->last_keyboard_reading = reading;
+                            }
                         }
-                    }
-                    if (!device->last_keyboard_reading) {
+                    } else {
                         if (SUCCEEDED(IGameInput_GetCurrentReading(data->pGameInput, GameInputKindKeyboard, device->pDevice, &reading))) {
                             GAMEINPUT_InitialKeyboardReading(data, window, device, reading);
                             device->last_keyboard_reading = reading;