SDL: wayland: Refine window raising for new windows

From 8586f63d2a900303701676a253a3e18593a6a6ae Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Tue, 14 Jul 2026 13:24:43 -0400
Subject: [PATCH] wayland: Refine window raising for new windows

Generate activation tokens for new windows when necessary, and check the hint for activating windows when shown. This will allow new windows to correctly take focus in environments with elevated focus stealing prevention when possible, and it will try to prevent new windows from being activated if the hint is set to false.
---
 src/video/wayland/SDL_waylandwindow.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index cfa1c7ecc7645..b630bdf9759d9 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -2435,8 +2435,11 @@ void Wayland_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
     Wayland_SetWindowResizable(_this, window, !!(window->flags & SDL_WINDOW_RESIZABLE));
 
     // We're finally done putting the window together, raise if possible
-    if (c->activation_manager) {
-        /* Note that we don't check for empty strings, as that is still
+    if (c->activation_manager && SDL_GetHintBoolean(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, true)) {
+        /* if the process was passed an activation token, use it when showing
+         * the initial window.
+         *
+         * Note that we don't check for empty strings, as that is still
          * considered a valid activation token!
          */
         const char *activation_token = SDL_getenv("XDG_ACTIVATION_TOKEN");
@@ -2447,7 +2450,13 @@ void Wayland_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
 
             // Clear this variable, per the protocol's request
             SDL_unsetenv_unsafe("XDG_ACTIVATION_TOKEN");
+        } else {
+            // Try to generate an activation token for this window.
+            Wayland_RaiseWindow(_this, window);
         }
+    } else {
+        // Clear the ignored activation token, as it won't be used.
+        SDL_unsetenv_unsafe("XDG_ACTIVATION_TOKEN");
     }
 
     // No frame callback on an external surface, as it may already have one attached.
@@ -2685,9 +2694,6 @@ void Wayland_RaiseWindow(SDL_VideoDevice *_this, SDL_Window *window)
             xdg_activation_v1_activate(viddata->activation_manager,
                                        activation_token,
                                        wind->surface);
-
-            // Clear this variable, per the protocol's request.
-            SDL_unsetenv_unsafe("XDG_ACTIVATION_TOKEN");
             return;
         }
     }