SDL: wayland: Try to detach at the beginning of ShowWindow, just in case.

From 3ada694e6279fdc788ee155877dfedd19fd24101 Mon Sep 17 00:00:00 2001
From: Ethan Lee <[EMAIL REDACTED]>
Date: Wed, 6 Apr 2022 01:18:03 -0400
Subject: [PATCH] wayland: Try to detach at the beginning of ShowWindow, just
 in case.

It's possible that an external component (probably a GL/VK context) committed, so we need to cover our bases and detach in both HideWindow and ShowWindow.

Fixes a crash in UE5 editor's pop-ups.
---
 src/video/wayland/SDL_waylandwindow.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index aee1b80799c..6e201a1bb1d 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -995,6 +995,21 @@ void Wayland_ShowWindow(_THIS, SDL_Window *window)
     SDL_VideoData *c = _this->driverdata;
     SDL_WindowData *data = window->driverdata;
 
+    /* Detach any previous buffers before resetting everything, otherwise when
+     * calling this a second time you'll get an annoying protocol error!
+     *
+     * FIXME: This was originally moved to HideWindow, which _should_ make
+     * sense, but for whatever reason UE5's popups require that this actually
+     * be in both places at once? Possibly from renderers making commits? I can't
+     * fully remember if this location caused crashes or if I was fixing a pair
+     * of Hide/Show calls. In any case, UE gives us a pretty good test and having
+     * both detach calls passes.
+     *
+     * -flibit
+     */
+    wl_surface_attach(data->surface, NULL, 0, 0);
+    wl_surface_commit(data->surface);
+
     /* Create the shell surface and map the toplevel */
 #ifdef HAVE_LIBDECOR_H
     if (c->shell.libdecor) {