SDL: Losing keyboard focus doesn't automatically mean you lose mouse capture

From 5322b3528ae41e9f47aec5a0da7c8900ae9e48b8 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 29 Jun 2024 01:28:43 -0700
Subject: [PATCH] Losing keyboard focus doesn't automatically mean you lose
 mouse capture

Fixes https://github.com/libsdl-org/SDL/issues/5616
---
 src/events/SDL_keyboard.c             | 17 -----------------
 src/video/wayland/SDL_waylandevents.c |  1 -
 src/video/wayland/SDL_waylandwindow.c |  6 ------
 3 files changed, 24 deletions(-)

diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index c7f815aa384ab..5156e0a36550c 100644
--- a/src/events/SDL_keyboard.c
+++ b/src/events/SDL_keyboard.c
@@ -283,23 +283,6 @@ int SDL_SetKeyboardFocus(SDL_Window *window)
 
     /* See if the current window has lost focus */
     if (keyboard->focus && keyboard->focus != window) {
-
-        /* new window shouldn't think it has mouse captured. */
-        SDL_assert(window == NULL || !(window->flags & SDL_WINDOW_MOUSE_CAPTURE));
-
-        /* old window must lose an existing mouse capture. */
-        if (keyboard->focus->flags & SDL_WINDOW_MOUSE_CAPTURE) {
-            SDL_Mouse *mouse = SDL_GetMouse();
-
-            if (mouse->CaptureMouse) {
-                SDL_CaptureMouse(SDL_FALSE); /* drop the capture. */
-                SDL_UpdateMouseCapture(SDL_TRUE);
-                SDL_assert(!(keyboard->focus->flags & SDL_WINDOW_MOUSE_CAPTURE));
-            } else {
-                keyboard->focus->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
-            }
-        }
-
         SDL_SendWindowEvent(keyboard->focus, SDL_EVENT_WINDOW_FOCUS_LOST, 0, 0);
 
         /* Ensures IME compositions are committed */
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index 693655b399967..68b65f92aa78e 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -1558,7 +1558,6 @@ static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
 
     wind->keyboard_device = NULL;
     window = wind->sdlwindow;
-    window->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
 
     /* Stop key repeat before clearing keyboard focus */
     keyboard_repeat_clear(&input->keyboard_repeat);
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index cd0d163990115..9a5ab6c6e2c65 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -1530,7 +1530,6 @@ static const struct frog_color_managed_surface_listener frog_surface_listener =
 
 static void SetKeyboardFocus(SDL_Window *window)
 {
-    SDL_Window *kb_focus = SDL_GetKeyboardFocus();
     SDL_Window *topmost = window;
 
     /* Find the topmost parent */
@@ -1540,11 +1539,6 @@ static void SetKeyboardFocus(SDL_Window *window)
 
     topmost->driverdata->keyboard_focus = window;
 
-    /* Clear the mouse capture flags before changing keyboard focus */
-    if (kb_focus) {
-        kb_focus->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
-    }
-    window->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
     SDL_SetKeyboardFocus(window);
 }