sdl12-compat: quirks: Fixes for symbol-based quirks to force X11

From 0fb5c56eaa5c74044c0830909627935718dd02f2 Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Mon, 17 Oct 2022 17:35:14 +0800
Subject: [PATCH] quirks: Fixes for symbol-based quirks to force X11

Make some changes to how the automatic quirks for apps which link
against gl[x]ew and Cg functions work. In particular:
- They can be overridden by a user-set SDL_VIDEODRIVER env variable.
- They only print warnings if debug logging
  (SDL12COMPAT_DEBUG_LOGGING=1) is enabled, to match the other quirks.
- The messages are no-longer wrapped.

Fixes: 2a96bb35d756 ("quirks: Force X11 on Linux if certain symbols are in the process.")
---
 src/SDL12_compat.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index cb97408e5..2451e7a5d 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -1307,6 +1307,7 @@ static void
 SDL12Compat_ApplyQuirks(void)
 {
     const char *exe_name = SDL12Compat_GetExeName();
+    const char *videodriver_env = SDL20_getenv("SDL_VIDEODRIVER");
     int i;
 
     if (*exe_name == '\0') {
@@ -1329,6 +1330,7 @@ SDL12Compat_ApplyQuirks(void)
     }
 
     #ifdef __linux__
+    if (!videodriver_env || SDL20_strcmp(videodriver_env, "x11"))
     {
         void *global_symbols = dlopen(NULL, RTLD_LOCAL|RTLD_NOW);
         SDL_bool force_x11 = SDL_FALSE;
@@ -1344,10 +1346,14 @@ SDL12Compat_ApplyQuirks(void)
         dlclose(global_symbols);
 
         if (force_x11) {
-            SDL20_Log("sdl12-compat: We are forcing this app to use X11, because it probably");
-            SDL20_Log("sdl12-compat: talks to an X server directly, outside of SDL. If possible,");
-            SDL20_Log("sdl12-compat: this app should be fixed, to be compatible with Wayland, etc.");
-            SDL20_setenv("SDL_VIDEODRIVER", "x11", 1);
+            if (!videodriver_env) {
+                if (WantDebugLogging) {
+                    SDL20_Log("Forcing this app to use X11, because it probably talks to an X server directly, outside of SDL. If possible, this app should be fixed, to be compatible with Wayland, etc.");
+                }
+                SDL20_setenv("SDL_VIDEODRIVER", "x11", 1);
+            } else if (videodriver_env && WantDebugLogging) {
+                SDL20_Log("This app looks like it requires X11, but the SDL_VIDEODRIVER environment variable is set to \"%s\". If you have issues, try setting SDL_VIDEODRIVER=x11", videodriver_env);
+            }
         }
     }
     #endif