SDL: wayland: Try to skip the Wayland driver if not connecting to or in a Wayland session

From 836927edf8da93ca2d3c8dddf7653113da65ce65 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Mon, 9 Oct 2023 11:44:32 -0400
Subject: [PATCH] wayland: Try to skip the Wayland driver if not connecting to
 or in a Wayland session

When initializing the Wayland driver, check if the application is being started in, or trying to connect to, a Wayland session and skip to another driver if not. If neither WAYLAND_DISPLAY nor XDG_SESSION_TYPE are set, try to start anyway, and if the Wayland library is missing or no Wayland sessions are started, initialization will fail later in the process as it previously did.

This fixes the case where a Wayland session is running on a different VT, but an application wishes to run via KMSDRM on the current VT.
---
 src/video/wayland/SDL_waylandvideo.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index bd9b7d4041a4..9117410e23e6 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -128,6 +128,14 @@ static SDL_VideoDevice *Wayland_CreateDevice(void)
     SDL_VideoData *data;
     struct wl_display *display;
 
+    /* Are we trying to connect to or are currently in a Wayland session? */
+    if (!getenv("WAYLAND_DISPLAY")) {
+        const char *session = getenv("XDG_SESSION_TYPE");
+        if (session && SDL_strcasecmp(session, "wayland")) {
+            return NULL;
+        }
+    }
+
     if (!SDL_WAYLAND_LoadSymbols()) {
         return NULL;
     }