SDL: x11: Accommodate the borders when setting the initial window position

From 1a0a94b501d9046878db5afb6346dfc97de11b6a Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Sat, 22 Feb 2025 09:51:35 -0500
Subject: [PATCH] x11: Accommodate the borders when setting the initial window
 position

---
 src/video/x11/SDL_x11window.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/video/x11/SDL_x11window.c b/src/video/x11/SDL_x11window.c
index 01bc64b786f8c..55620533eb27d 100644
--- a/src/video/x11/SDL_x11window.c
+++ b/src/video/x11/SDL_x11window.c
@@ -1437,6 +1437,7 @@ void X11_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
     Display *display = data->videodata->display;
     bool bActivate = SDL_GetHintBoolean(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, true);
     bool position_is_absolute = false;
+    bool set_position = false;
     XEvent event;
 
     if (SDL_WINDOW_IS_POPUP(window)) {
@@ -1446,6 +1447,7 @@ void X11_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
 
         // Coordinates after X11_ConstrainPopup() are already in the global space.
         position_is_absolute = true;
+        set_position = true;
     }
 
     /* Whether XMapRaised focuses the window is based on the window type and it is
@@ -1454,20 +1456,6 @@ void X11_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
 
     if (!X11_IsWindowMapped(_this, window)) {
         X11_XMapRaised(display, data->xwindow);
-        if (data->pending_position) {
-            int x, y;
-            if (position_is_absolute) {
-                x = window->pending.x;
-                y = window->pending.y;
-            } else {
-                SDL_RelativeToGlobalForWindow(window,
-                                              window->pending.x, window->pending.y,
-                                              &x, &y);
-            }
-            data->pending_position = false;
-            X11_XMoveWindow(display, data->xwindow, x, y);
-        }
-
         /* Blocking wait for "MapNotify" event.
          * We use X11_XIfEvent because pXWindowEvent takes a mask rather than a type,
          * and XCheckTypedWindowEvent doesn't block */
@@ -1475,6 +1463,8 @@ void X11_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
             X11_XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
         }
         X11_XFlush(display);
+        set_position = data->pending_position ||
+                       (!(window->flags & SDL_WINDOW_BORDERLESS) && !window->undefined_x && !window->undefined_y);
     }
 
     if (!data->videodata->net_wm) {
@@ -1494,6 +1484,23 @@ void X11_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
         X11_GetBorderValues(data);
     }
 
+    if (set_position) {
+        // Apply the window position, accounting for offsets due to the borders appearing.
+        const int tx = data->pending_position ? window->pending.x : window->x;
+        const int ty = data->pending_position ? window->pending.y : window->y;
+        int x, y;
+        if (position_is_absolute) {
+            x = tx;
+            y = ty;
+        } else {
+            SDL_RelativeToGlobalForWindow(window,
+                                          tx - data->border_left, ty - data->border_top,
+                                          &x, &y);
+        }
+        data->pending_position = false;
+        X11_XMoveWindow(display, data->xwindow, x, y);
+    }
+
     /* Some window managers can send garbage coordinates while mapping the window, so don't emit size and position
      * events during the initial configure events.
      */