SDL: Flush any pending mouse motion when warping the mouse cursor

From 649466f4915b68cfa75e802bef7ee192b2192684 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 7 Oct 2021 13:28:44 -0700
Subject: [PATCH] Flush any pending mouse motion when warping the mouse cursor

Fixes https://github.com/libsdl-org/SDL/issues/4165
---
 src/video/windows/SDL_windowsmouse.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/video/windows/SDL_windowsmouse.c b/src/video/windows/SDL_windowsmouse.c
index da775fe137..2492243cd0 100644
--- a/src/video/windows/SDL_windowsmouse.c
+++ b/src/video/windows/SDL_windowsmouse.c
@@ -251,6 +251,18 @@ WIN_WarpMouse(SDL_Window * window, int x, int y)
     pt.y = y;
     ClientToScreen(hwnd, &pt);
     SetCursorPos(pt.x, pt.y);
+
+    /* Flush any pending mouse motion and simulate motion for this warp */
+    {
+        SDL_Mouse *mouse = SDL_GetMouse();
+        const SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+        MSG msg;
+
+        while (PeekMessage(&msg, data->hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE)) {
+            continue;
+        }
+        SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
+    }
 }
 
 static int