SDL: video: Try to get the display for fullscreen windows via the window position

From d6033718488f915c294aacc7d8e9cb3f2be0bc82 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Mon, 6 Feb 2023 19:25:19 -0500
Subject: [PATCH] video: Try to get the display for fullscreen windows via the
 window position

Attempt to retrieve the display for fullscreen windows using the window position so that the correct display ID is returned if an exclusive fullscreen window is moved to another display.
---
 src/video/SDL_video.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 32baa6efd071..3e926648be88 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -1256,9 +1256,6 @@ SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
      * (for example if the window is off-screen), but other code may expect it
      * to succeed in that situation, so we fall back to a generic position-
      * based implementation in that case. */
-    if (!displayID && (window->flags & SDL_WINDOW_FULLSCREEN)) {
-        displayID = window->fullscreen_mode.displayID;
-    }
     if (!displayID) {
         displayID = SDL_GetDisplayForWindowCoordinate(window->windowed.x);
     }
@@ -1267,10 +1264,14 @@ SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
     }
     if (!displayID) {
         displayID = GetDisplayForRect(window->x, window->y, window->w, window->h);
-        if (!displayID) {
-            /* Use the primary display for a window if we can't find it anywhere else */
-            displayID = SDL_GetPrimaryDisplay();
-        }
+    }
+    if (!displayID && (window->flags & SDL_WINDOW_FULLSCREEN)) {
+        /* Use the explicit fullscreen display if retrieval via the window position fails */
+        displayID = window->fullscreen_mode.displayID;
+    }
+    if (!displayID) {
+        /* Use the primary display for a window if we can't find it anywhere else */
+        displayID = SDL_GetPrimaryDisplay();
     }
     return displayID;
 }
@@ -1514,8 +1515,16 @@ int SDL_SetWindowFullscreenMode(SDL_Window *window, const SDL_DisplayMode *mode)
 
     if (SDL_WINDOW_FULLSCREEN_VISIBLE(window)) {
         SDL_UpdateFullscreenMode(window, SDL_TRUE);
-    } else {
-        SDL_CheckWindowDisplayChanged(window);
+    } else if (window->flags & SDL_WINDOW_FULLSCREEN) {
+        /* If fullscreen and not visible, just update the position so the window will be
+         * on the correct display when shown/restored.
+         */
+        if (mode) {
+            SDL_Rect r;
+            if (SDL_GetDisplayBounds(mode->displayID, &r) == 0) {
+                SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, r.x, r.y);
+            }
+        }
     }
     return 0;
 }