SDL: Don't treat mouse buttons as focus clicks if the window has mouse capture

From 26567df87838d4da74f51613847d7ace46fa9b2a Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 21 Oct 2024 09:42:55 -0700
Subject: [PATCH] Don't treat mouse buttons as focus clicks if the window has
 mouse capture

This fixes the following sequence:
* Press mouse button down
* Alt-tab away from the window
* Alt-tab back to the window
* Release mouse button

Fixes https://github.com/libsdl-org/SDL/issues/7747
---
 src/video/windows/SDL_windowsevents.c | 32 ++++++++++++++-------------
 1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index 305e4fa35189f..b78422e8ae43a 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -308,21 +308,23 @@ static void WIN_UpdateFocus(SDL_Window *window, bool expect_focus)
     if (has_focus) {
         POINT cursorPos;
 
-        bool swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
-        if (GetAsyncKeyState(VK_LBUTTON)) {
-            data->focus_click_pending |= !swapButtons ? SDL_BUTTON_LMASK : SDL_BUTTON_RMASK;
-        }
-        if (GetAsyncKeyState(VK_RBUTTON)) {
-            data->focus_click_pending |= !swapButtons ? SDL_BUTTON_RMASK : SDL_BUTTON_LMASK;
-        }
-        if (GetAsyncKeyState(VK_MBUTTON)) {
-            data->focus_click_pending |= SDL_BUTTON_MMASK;
-        }
-        if (GetAsyncKeyState(VK_XBUTTON1)) {
-            data->focus_click_pending |= SDL_BUTTON_X1MASK;
-        }
-        if (GetAsyncKeyState(VK_XBUTTON2)) {
-            data->focus_click_pending |= SDL_BUTTON_X2MASK;
+        if (!(window->flags & SDL_WINDOW_MOUSE_CAPTURE)) {
+            bool swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
+            if (GetAsyncKeyState(VK_LBUTTON)) {
+                data->focus_click_pending |= !swapButtons ? SDL_BUTTON_LMASK : SDL_BUTTON_RMASK;
+            }
+            if (GetAsyncKeyState(VK_RBUTTON)) {
+                data->focus_click_pending |= !swapButtons ? SDL_BUTTON_RMASK : SDL_BUTTON_LMASK;
+            }
+            if (GetAsyncKeyState(VK_MBUTTON)) {
+                data->focus_click_pending |= SDL_BUTTON_MMASK;
+            }
+            if (GetAsyncKeyState(VK_XBUTTON1)) {
+                data->focus_click_pending |= SDL_BUTTON_X1MASK;
+            }
+            if (GetAsyncKeyState(VK_XBUTTON2)) {
+                data->focus_click_pending |= SDL_BUTTON_X2MASK;
+            }
         }
 
         SDL_SetKeyboardFocus(data->keyboard_focus ? data->keyboard_focus : window);