sdl2-compat: Add compat for Hints functions, because of hint name changes (#14)

From 20e59d6e08aa75522406f5ffa7bab813386bef69 Mon Sep 17 00:00:00 2001
From: Sylvain Becker <[EMAIL REDACTED]>
Date: Wed, 28 Dec 2022 22:55:49 +0100
Subject: [PATCH] Add compat for Hints functions, because of hint name changes
 (#14)

Hints functions, because of hint name changes:
SDL_AUDIODRIVER -> SDL_AUDIO_DRIVER
SDL_VIDEODRIVER -> SDL_VIDEO_DRIVER
---
 src/sdl2_compat.c | 76 +++++++++++++++++++++++++++++++++++++++++++++--
 src/sdl3_syms.h   | 14 ++++-----
 2 files changed, 81 insertions(+), 9 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 8dccfb7..40f9297 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -316,6 +316,62 @@ SDL2Compat_GetHintBoolean(const char *name, SDL_bool default_value)
     return (SDL3_atoi(val) != 0) ? SDL_TRUE : SDL_FALSE;
 }
 
+
+/* if you change this, update also SDL2Compat_ApplyQuirks() */
+static const char *SDL2_to_SDL3_hint(const char *name)
+{
+    if (SDL_strcmp(name, "SDL_VIDEODRIVER") == 0) {
+        return "SDL_VIDEO_DRIVER";
+    }
+    if (SDL_strcmp(name, "SDL_AUDIODRIVER") == 0) {
+        return "SDL_AUDIO_DRIVER";
+    }
+    return name;
+}
+
+DECLSPEC SDL_bool SDLCALL
+SDL_SetHintWithPriority(const char *name, const char *value, SDL_HintPriority priority)
+{
+    return SDL3_SetHintWithPriority(SDL2_to_SDL3_hint(name), value, priority);
+}
+
+DECLSPEC SDL_bool SDLCALL
+SDL_SetHint(const char *name, const char *value)
+{
+    return SDL3_SetHint(SDL2_to_SDL3_hint(name), value);
+}
+
+DECLSPEC const char * SDLCALL
+SDL_GetHint(const char *name)
+{
+    return SDL3_GetHint(SDL2_to_SDL3_hint(name));
+}
+
+DECLSPEC SDL_bool SDLCALL
+SDL_ResetHint(const char *name)
+{
+    return SDL3_ResetHint(SDL2_to_SDL3_hint(name));
+}
+
+DECLSPEC SDL_bool SDLCALL
+SDL_GetHintBoolean(const char *name, SDL_bool default_value)
+{
+    return SDL3_GetHintBoolean(SDL2_to_SDL3_hint(name), default_value);
+}
+
+/* FIXME: callbacks may need to tweaking ... */
+DECLSPEC void SDLCALL
+SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
+{
+    return SDL3_AddHintCallback(SDL2_to_SDL3_hint(name), callback, userdata);
+}
+
+DECLSPEC void SDLCALL
+SDL_DelHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
+{
+    return SDL3_DelHintCallback(SDL2_to_SDL3_hint(name), callback, userdata);
+}
+
 static void
 SDL2Compat_ApplyQuirks(SDL_bool force_x11)
 {
@@ -326,6 +382,22 @@ SDL2Compat_ApplyQuirks(SDL_bool force_x11)
         SDL3_Log("This app appears to be named '%s'", exe_name);
     }
 
+    {
+        /* if you change this, update also SDL2_to_SDL3_hint() */
+        /* hint/env names updated.
+         * SDL_VIDEO_DRIVER (SDL2) to SDL_VIDEO_DRIVER (SDL3)
+         * SDL_AUDIO_DRIVER (SDL2) to SDL_AUDIO_DRIVER (SDL3)
+         */
+        const char *videodriver_env = SDL3_getenv("SDL_VIDEODRIVER");
+        const char *audiodriver_env = SDL3_getenv("SDL_AUDIODRIVER");
+        if (videodriver_env) {
+            SDL3_setenv("SDL_VIDEO_DRIVER", videodriver_env, 1);
+        }
+        if (audiodriver_env) {
+            SDL3_setenv("SDL_AUDIO_DRIVER", audiodriver_env, 1);
+        }
+    }
+
     #ifdef __linux__
     if (force_x11) {
         const char *videodriver_env = SDL3_getenv("SDL_VIDEODRIVER");
@@ -337,7 +409,7 @@ SDL2Compat_ApplyQuirks(SDL_bool force_x11)
             if (WantDebugLogging) {
                 SDL3_Log("sdl12-compat: We are 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.");
             }
-            SDL3_setenv("SDL_VIDEODRIVER", "x11", 1);
+            SDL3_setenv("SDL_VIDEO_DRIVER", "x11", 1);
         }
     }
     #endif
@@ -988,7 +1060,7 @@ SDL_GetVersion(SDL_version * ver)
         ver->major = 2;
         ver->minor = SDL2_COMPAT_VERSION_MINOR;
         ver->patch = SDL2_COMPAT_VERSION_PATCH;
-        if (SDL_GetHintBoolean("SDL_LEGACY_VERSION", SDL_FALSE)) {
+        if (SDL3_GetHintBoolean("SDL_LEGACY_VERSION", SDL_FALSE)) {
             /* Prior to SDL 2.24.0, the patch version was incremented with every release */
             ver->patch = ver->minor;
             ver->minor = 0;
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index a77eeb4..3f560f1 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -203,11 +203,11 @@ SDL3_SYM_PASSTHROUGH(int,HapticRumbleSupported,(SDL_Haptic *a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,HapticRumbleInit,(SDL_Haptic *a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,HapticRumblePlay,(SDL_Haptic *a, float b, Uint32 c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(int,HapticRumbleStop,(SDL_Haptic *a),(a),return)
-SDL3_SYM_PASSTHROUGH(SDL_bool,SetHintWithPriority,(const char *a, const char *b, SDL_HintPriority c),(a,b,c),return)
-SDL3_SYM_PASSTHROUGH(SDL_bool,SetHint,(const char *a, const char *b),(a,b),return)
-SDL3_SYM_PASSTHROUGH(const char*,GetHint,(const char *a),(a),return)
-SDL3_SYM_PASSTHROUGH(void,AddHintCallback,(const char *a, SDL_HintCallback b, void *c),(a,b,c),)
-SDL3_SYM_PASSTHROUGH(void,DelHintCallback,(const char *a, SDL_HintCallback b, void *c),(a,b,c),)
+SDL3_SYM(SDL_bool,SetHintWithPriority,(const char *a, const char *b, SDL_HintPriority c),(a,b,c),return)
+SDL3_SYM(SDL_bool,SetHint,(const char *a, const char *b),(a,b),return)
+SDL3_SYM(const char*,GetHint,(const char *a),(a),return)
+SDL3_SYM(void,AddHintCallback,(const char *a, SDL_HintCallback b, void *c),(a,b,c),)
+SDL3_SYM(void,DelHintCallback,(const char *a, SDL_HintCallback b, void *c),(a,b,c),)
 SDL3_SYM_PASSTHROUGH(void,ClearHints,(void),(),)
 SDL3_SYM_PASSTHROUGH(int,NumJoysticks,(void),(),return)
 SDL3_SYM_PASSTHROUGH(const char*,JoystickNameForIndex,(int a),(a),return)
@@ -615,7 +615,7 @@ SDL3_SYM_PASSTHROUGH(int,RenderSetIntegerScale,(SDL_Renderer *a, SDL_bool b),(a,
 SDL3_SYM_PASSTHROUGH(SDL_bool,RenderGetIntegerScale,(SDL_Renderer *a),(a),return)
 SDL3_SYM_PASSTHROUGH(Uint32,DequeueAudio,(SDL_AudioDeviceID a, void *b, Uint32 c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(void,SetWindowResizable,(SDL_Window *a, SDL_bool b),(a,b),)
-SDL3_SYM_PASSTHROUGH(SDL_bool,GetHintBoolean,(const char *a, SDL_bool b),(a,b),return)
+SDL3_SYM(SDL_bool,GetHintBoolean,(const char *a, SDL_bool b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(Uint16,JoystickGetDeviceVendor,(int a),(a),return)
 SDL3_SYM_PASSTHROUGH(Uint16,JoystickGetDeviceProduct,(int a),(a),return)
 SDL3_SYM_PASSTHROUGH(Uint16,JoystickGetDeviceProductVersion,(int a),(a),return)
@@ -929,7 +929,7 @@ SDL3_SYM_PASSTHROUGH(void,ResetKeyboard,(void),(),)
 SDL3_SYM_PASSTHROUGH(int,GetDefaultAudioInfo,(char **a, SDL_AudioSpec *b, int c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(int,GetPointDisplayIndex,(const SDL_Point *a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,GetRectDisplayIndex,(const SDL_Rect *a),(a),return)
-SDL3_SYM_PASSTHROUGH(SDL_bool,ResetHint,(const char *a),(a),return)
+SDL3_SYM(SDL_bool,ResetHint,(const char *a),(a),return)
 SDL3_SYM_PASSTHROUGH(Uint16,crc16,(Uint16 a, const void *b, size_t c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(void,GetWindowSizeInPixels,(SDL_Window *a, int *b, int *c),(a,b,c),)
 SDL3_SYM_PASSTHROUGH(void,GetJoystickGUIDInfo,(SDL_JoystickGUID a, Uint16 *b, Uint16 *c, Uint16 *d, Uint16 *e),(a,b,c,d,e),)