SDL: wayland: Defer creating cursor confinement regions until the surface is mapped

From 266ee41c86db532ba48169bbd4e558300dd95406 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Sun, 30 Mar 2025 14:32:32 -0400
Subject: [PATCH] wayland: Defer creating cursor confinement regions until the
 surface is mapped

Otherwise, the confinement area will be lost if the window is recreated when initializing the renderer.
---
 src/video/wayland/SDL_waylandevents.c | 5 +++++
 src/video/wayland/SDL_waylandwindow.c | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index ef91b4307c202..a392c6e310379 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -3312,6 +3312,11 @@ bool Wayland_input_confine_pointer(struct SDL_WaylandInput *input, SDL_Window *w
         return SDL_SetError("No pointer to confine");
     }
 
+    // The confinement region will be created when the window is mapped.
+    if (w->shell_surface_status != WAYLAND_SHELL_SURFACE_STATUS_SHOWN) {
+        return true;
+    }
+
     /* A confine may already be active, in which case we should destroy it and
      * create a new one.
      */
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index 7e5558315cd71..523d2e4bb29bd 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -707,6 +707,9 @@ static void surface_frame_done(void *data, struct wl_callback *cb, uint32_t time
             }
         }
 
+        // Create the pointer confinement region, if necessary.
+        Wayland_input_confine_pointer(wind->waylandData->input, wind->sdlwindow);
+
         /* If the window was initially set to the suspended state, send the occluded event now,
          * as we don't want to mark the window as occluded until at least one frame has been submitted.
          */