SDL: x11: Don't wait for events when the connection errored out (#8392)

From a0b6c0fd8f64826769899badc9d09c0fc96ddc0f Mon Sep 17 00:00:00 2001
From: Guldoman <[EMAIL REDACTED]>
Date: Sat, 8 Feb 2025 19:07:11 +0100
Subject: [PATCH] x11: Don't wait for events when the connection errored out
 (#8392)

Calling `X11_XIfEvent` after the X11 connection errored out can result in hangs.
---
 src/video/x11/SDL_x11window.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/video/x11/SDL_x11window.c b/src/video/x11/SDL_x11window.c
index c26ee7f5a7e03..b6356eb185a67 100644
--- a/src/video/x11/SDL_x11window.c
+++ b/src/video/x11/SDL_x11window.c
@@ -100,6 +100,14 @@ static bool X11_IsWindowMapped(SDL_VideoDevice *_this, SDL_Window *window)
     }
 }
 
+static bool X11_IsDisplayOk(Display *display)
+{
+    if (display->flags & XlibDisplayIOError) {
+        return false;
+    }
+    return true;
+}
+
 #if 0
 static bool X11_IsActionAllowed(SDL_Window *window, Atom action)
 {
@@ -1453,7 +1461,7 @@ void X11_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
         /* Blocking wait for "MapNotify" event.
          * We use X11_XIfEvent because pXWindowEvent takes a mask rather than a type,
          * and XCheckTypedWindowEvent doesn't block */
-        if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
+        if (!(window->flags & SDL_WINDOW_EXTERNAL) && X11_IsDisplayOk(display)) {
             X11_XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
         }
         X11_XFlush(display);
@@ -1507,7 +1515,7 @@ void X11_HideWindow(SDL_VideoDevice *_this, SDL_Window *window)
     if (X11_IsWindowMapped(_this, window)) {
         X11_XWithdrawWindow(display, data->xwindow, screen);
         // Blocking wait for "UnmapNotify" event
-        if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
+        if (!(window->flags & SDL_WINDOW_EXTERNAL) && X11_IsDisplayOk(display)) {
             X11_XIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
         }
         X11_XFlush(display);