sdl2-compat: Updated for addition of SDL_LOG_PRIORITY_TRACE in SDL3

From 08f2cc71ae3c3faa1820f3528cb91f7912d16ec0 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 17 Sep 2024 08:16:41 -0700
Subject: [PATCH] Updated for addition of SDL_LOG_PRIORITY_TRACE in SDL3

Fixes https://github.com/libsdl-org/sdl2-compat/issues/223
---
 src/dynapi/SDL_dynapi.c       | 12 ++---
 src/dynapi/SDL_dynapi_procs.h | 10 ++--
 src/sdl2_compat.c             | 98 ++++++++++++++++++++++++++++++++---
 src/sdl2_compat.h             | 11 ++++
 src/sdl2_protos.h             | 10 ++--
 src/sdl3_include_wrapper.h    |  5 ++
 src/sdl3_syms.h               |  8 +--
 7 files changed, 128 insertions(+), 26 deletions(-)

diff --git a/src/dynapi/SDL_dynapi.c b/src/dynapi/SDL_dynapi.c
index d0985a8..172fa20 100644
--- a/src/dynapi/SDL_dynapi.c
+++ b/src/dynapi/SDL_dynapi.c
@@ -72,7 +72,7 @@ static void SDL_InitDynamicAPI(void);
         va_list ap;                                                                                          \
         initcall;                                                                                            \
         va_start(ap, fmt);                                                                                   \
-        jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap);                              \
+        jump_table.SDL_LogMessageV(category, SDL2_LOG_PRIORITY_##prio, fmt, ap);                              \
         va_end(ap);                                                                                          \
     }
 
@@ -138,10 +138,10 @@ static void SDL_InitDynamicAPI(void);
         va_list ap;                                                                                                                       \
         initcall;                                                                                                                         \
         va_start(ap, fmt);                                                                                                                \
-        jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);                                         \
+        jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL2_LOG_PRIORITY_INFO, fmt, ap);                                         \
         va_end(ap);                                                                                                                       \
     }                                                                                                                                     \
-    _static void SDLCALL SDL_LogMessage##name(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)      \
+    _static void SDLCALL SDL_LogMessage##name(int category, SDL2_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)      \
     {                                                                                                                                     \
         va_list ap;                                                                                                                       \
         initcall;                                                                                                                         \
@@ -269,10 +269,10 @@ static void SDLCALL SDL_Log_LOGSDLCALLS(SDL_PRINTF_FORMAT_STRING const char *fmt
     va_list ap;
     SDL_Log_REAL("SDL2CALL SDL_Log");
     va_start(ap, fmt);
-    SDL_LogMessageV_REAL(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
+    SDL_LogMessageV_REAL(SDL_LOG_CATEGORY_APPLICATION, SDL2_LOG_PRIORITY_INFO, fmt, ap);
     va_end(ap);
 }
-static void SDLCALL SDL_LogMessage_LOGSDLCALLS(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
+static void SDLCALL SDL_LogMessage_LOGSDLCALLS(int category, SDL2_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
 {
     va_list ap;
     SDL_Log_REAL("SDL2CALL SDL_LogMessage");
@@ -284,7 +284,7 @@ static void SDLCALL SDL_LogMessage_LOGSDLCALLS(int category, SDL_LogPriority pri
     static void SDLCALL SDL_Log##logname##_LOGSDLCALLS(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
         va_list ap; va_start(ap, fmt); \
         SDL_Log_REAL("SDL2CALL SDL_Log%s", #logname); \
-        SDL_LogMessageV_REAL(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
+        SDL_LogMessageV_REAL(category, SDL2_LOG_PRIORITY_##prio, fmt, ap); \
         va_end(ap); \
     }
 SDL_DYNAPI_VARARGS_LOGFN_LOGSDLCALLS(Verbose, VERBOSE)
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 888b6a3..1f4abb8 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -37,7 +37,7 @@ SDL_DYNAPI_PROC(void,SDL_LogInfo,(int a, SDL_PRINTF_FORMAT_STRING const char *b,
 SDL_DYNAPI_PROC(void,SDL_LogWarn,(int a, SDL_PRINTF_FORMAT_STRING const char *b, ...),(a,b),)
 SDL_DYNAPI_PROC(void,SDL_LogError,(int a, SDL_PRINTF_FORMAT_STRING const char *b, ...),(a,b),)
 SDL_DYNAPI_PROC(void,SDL_LogCritical,(int a, SDL_PRINTF_FORMAT_STRING const char *b, ...),(a,b),)
-SDL_DYNAPI_PROC(void,SDL_LogMessage,(int a, SDL_LogPriority b, SDL_PRINTF_FORMAT_STRING const char *c, ...),(a,b,c),)
+SDL_DYNAPI_PROC(void,SDL_LogMessage,(int a, SDL2_LogPriority b, SDL_PRINTF_FORMAT_STRING const char *c, ...),(a,b,c),)
 SDL_DYNAPI_PROC(int,SDL_sscanf,(const char *a, SDL_SCANF_FORMAT_STRING const char *b, ...),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_snprintf,(SDL_OUT_Z_CAP(b) char *a, size_t b, SDL_PRINTF_FORMAT_STRING const char *c, ...),(a,b,c),return)
 #endif
@@ -263,11 +263,11 @@ SDL_DYNAPI_PROC(SDL2_bool,SDL_IsScreenKeyboardShown,(SDL_Window *a),(a),return)
 SDL_DYNAPI_PROC(void*,SDL_LoadObject,(const char *a),(a),return)
 SDL_DYNAPI_PROC(void*,SDL_LoadFunction,(void *a, const char *b),(a,b),return)
 SDL_DYNAPI_PROC(void,SDL_UnloadObject,(void *a),(a),)
-SDL_DYNAPI_PROC(void,SDL_LogSetAllPriority,(SDL_LogPriority a),(a),)
-SDL_DYNAPI_PROC(void,SDL_LogSetPriority,(int a, SDL_LogPriority b),(a,b),)
-SDL_DYNAPI_PROC(SDL_LogPriority,SDL_LogGetPriority,(int a),(a),return)
+SDL_DYNAPI_PROC(void,SDL_LogSetAllPriority,(SDL2_LogPriority a),(a),)
+SDL_DYNAPI_PROC(void,SDL_LogSetPriority,(int a, SDL2_LogPriority b),(a,b),)
+SDL_DYNAPI_PROC(SDL2_LogPriority,SDL_LogGetPriority,(int a),(a),return)
 SDL_DYNAPI_PROC(void,SDL_LogResetPriorities,(void),(),)
-SDL_DYNAPI_PROC(void,SDL_LogMessageV,(int a, SDL_LogPriority b, const char *c, va_list d),(a,b,c,d),)
+SDL_DYNAPI_PROC(void,SDL_LogMessageV,(int a, SDL2_LogPriority b, const char *c, va_list d),(a,b,c,d),)
 SDL_DYNAPI_PROC(void,SDL_LogGetOutputFunction,(SDL_LogOutputFunction *a, void **b),(a,b),)
 SDL_DYNAPI_PROC(void,SDL_LogSetOutputFunction,(SDL_LogOutputFunction a, void *b),(a,b),)
 SDL_DYNAPI_PROC(void,SDL_SetMainReady,(void),(),)
diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 43d9909..5fec75a 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -454,8 +454,7 @@ static struct {
     { "SDL_VIDEO_X11_WMCLASS", "SDL_APP_ID" },
 };
 
-static const char *
-SDL2_to_SDL3_hint(const char *name)
+static const char *SDL2_to_SDL3_hint(const char *name)
 {
     unsigned int i;
 
@@ -467,16 +466,43 @@ SDL2_to_SDL3_hint(const char *name)
     return name;
 }
 
+static const char *SDL2_to_SDL3_hint_value(const char *name, const char *value, SDL_bool *free_value)
+{
+    if (name && value && SDL3_strcmp(name, SDL_HINT_LOGGING) == 0) {
+        /* Rewrite numeric priorities for SDL3 */
+        char *value3 = SDL_strdup(value);
+        if (value3) {
+            char *sep;
+            for (sep = SDL3_strchr(value3, '='); sep; sep = SDL3_strchr(sep + 1, '=')) {
+                if (SDL3_isdigit(sep[1]) && sep[1] != '0') {
+                    sep[1] = '0' + SDL_atoi(&sep[1]) + 1;
+                }
+            }
+        }
+        *free_value = SDL_TRUE;
+        return value3;
+    } else {
+        *free_value = SDL_FALSE;
+        return value;
+    }
+}
+
 SDL_DECLSPEC SDL2_bool SDLCALL
 SDL_SetHintWithPriority(const char *name, const char *value, SDL_HintPriority priority)
 {
-    return SDL3_SetHintWithPriority(SDL2_to_SDL3_hint(name), value, priority) ? SDL2_TRUE : SDL2_FALSE;
+    SDL_bool free_value = SDL_FALSE;
+    const char *value3 = SDL2_to_SDL3_hint_value(name, value, &free_value);
+    SDL2_bool result = SDL3_SetHintWithPriority(SDL2_to_SDL3_hint(name), value3, priority) ? SDL2_TRUE : SDL2_FALSE;
+    if (free_value) {
+        SDL3_free((void *)value3);
+    }
+    return result;
 }
 
 SDL_DECLSPEC SDL2_bool SDLCALL
 SDL_SetHint(const char *name, const char *value)
 {
-    return SDL3_SetHint(SDL2_to_SDL3_hint(name), value) ? SDL2_TRUE : SDL2_FALSE;
+    return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
 }
 
 SDL_DECLSPEC const char * SDLCALL
@@ -1024,6 +1050,61 @@ SDL_asprintf(char **str, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
     return retval;
 }
 
+static SDL2_LogPriority LogPriority3to2(SDL_LogPriority priority)
+{
+    switch (priority) {
+    case SDL_LOG_PRIORITY_VERBOSE:
+        return SDL2_LOG_PRIORITY_VERBOSE;
+    case SDL_LOG_PRIORITY_DEBUG:
+        return SDL2_LOG_PRIORITY_DEBUG;
+    case SDL_LOG_PRIORITY_INFO:
+        return SDL2_LOG_PRIORITY_INFO;
+    case SDL_LOG_PRIORITY_WARN:
+        return SDL2_LOG_PRIORITY_WARN;
+    case SDL_LOG_PRIORITY_ERROR:
+        return SDL2_LOG_PRIORITY_ERROR;
+    case SDL_LOG_PRIORITY_CRITICAL:
+        return SDL2_LOG_PRIORITY_CRITICAL;
+    default:
+        return SDL2_NUM_LOG_PRIORITIES;
+    }
+}
+
+static SDL_LogPriority LogPriority2to3(SDL2_LogPriority priority)
+{
+    switch (priority) {
+    case SDL2_LOG_PRIORITY_VERBOSE:
+        return SDL_LOG_PRIORITY_VERBOSE;
+    case SDL2_LOG_PRIORITY_DEBUG:
+        return SDL_LOG_PRIORITY_DEBUG;
+    case SDL2_LOG_PRIORITY_INFO:
+        return SDL_LOG_PRIORITY_INFO;
+    case SDL2_LOG_PRIORITY_WARN:
+        return SDL_LOG_PRIORITY_WARN;
+    case SDL2_LOG_PRIORITY_ERROR:
+        return SDL_LOG_PRIORITY_ERROR;
+    case SDL2_LOG_PRIORITY_CRITICAL:
+        return SDL_LOG_PRIORITY_CRITICAL;
+    default:
+        return SDL_LOG_PRIORITY_INVALID;
+    }
+}
+
+SDL_DECLSPEC void SDLCALL SDL_LogSetAllPriority(SDL2_LogPriority priority)
+{
+    SDL3_SetLogPriorities(LogPriority2to3(priority));
+}
+
+SDL_DECLSPEC void SDLCALL SDL_LogSetPriority(int category, SDL2_LogPriority priority)
+{
+    SDL3_SetLogPriority(category, LogPriority2to3(priority));
+}
+
+SDL_DECLSPEC SDL2_LogPriority SDLCALL SDL_LogGetPriority(int category)
+{
+    return LogPriority3to2(SDL3_GetLogPriority(category));
+}
+
 SDL_DECLSPEC void SDLCALL
 SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
 {
@@ -1034,14 +1115,19 @@ SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
 }
 
 SDL_DECLSPEC void SDLCALL
-SDL_LogMessage(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
+SDL_LogMessage(int category, SDL2_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
 {
     va_list ap;
     va_start(ap, fmt);
-    SDL3_LogMessageV(category, priority, fmt, ap);
+    SDL3_LogMessageV(category, LogPriority2to3(priority), fmt, ap);
     va_end(ap);
 }
 
+SDL_DECLSPEC void SDLCALL SDL_LogMessageV(int category, SDL2_LogPriority priority, const char *fmt, va_list ap)
+{
+    SDL3_LogMessageV(category, LogPriority2to3(priority), fmt, ap);
+}
+
 #define SDL_LOG_IMPL(name, prio)                                             \
 SDL_DECLSPEC void SDLCALL                                                        \
 SDL_Log##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
diff --git a/src/sdl2_compat.h b/src/sdl2_compat.h
index 80fb887..9094f9d 100644
--- a/src/sdl2_compat.h
+++ b/src/sdl2_compat.h
@@ -107,6 +107,17 @@ typedef enum SDL_errorcode
     SDL_LASTERROR
 } SDL_errorcode;
 
+typedef enum SDL2_LogPriority
+{
+    SDL2_LOG_PRIORITY_VERBOSE = 1,
+    SDL2_LOG_PRIORITY_DEBUG,
+    SDL2_LOG_PRIORITY_INFO,
+    SDL2_LOG_PRIORITY_WARN,
+    SDL2_LOG_PRIORITY_ERROR,
+    SDL2_LOG_PRIORITY_CRITICAL,
+    SDL2_NUM_LOG_PRIORITIES
+} SDL2_LogPriority;
+
 typedef SDL_AtomicInt SDL_atomic_t;
 
 typedef SDL_Condition SDL_cond;
diff --git a/src/sdl2_protos.h b/src/sdl2_protos.h
index 8d939d9..103f4e2 100644
--- a/src/sdl2_protos.h
+++ b/src/sdl2_protos.h
@@ -39,7 +39,7 @@ SDL2_PROTO(void,LogInfo,(int a, SDL_PRINTF_FORMAT_STRING const char *b, ...))
 SDL2_PROTO(void,LogWarn,(int a, SDL_PRINTF_FORMAT_STRING const char *b, ...))
 SDL2_PROTO(void,LogError,(int a, SDL_PRINTF_FORMAT_STRING const char *b, ...))
 SDL2_PROTO(void,LogCritical,(int a, SDL_PRINTF_FORMAT_STRING const char *b, ...))
-SDL2_PROTO(void,LogMessage,(int a, SDL_LogPriority b, SDL_PRINTF_FORMAT_STRING const char *c, ...))
+SDL2_PROTO(void,LogMessage,(int a, SDL2_LogPriority b, SDL_PRINTF_FORMAT_STRING const char *c, ...))
 SDL2_PROTO(int,sscanf,(const char *a, SDL_SCANF_FORMAT_STRING const char *b, ...))
 SDL2_PROTO(int,snprintf,(SDL_OUT_Z_CAP(b) char *a, size_t b, SDL_PRINTF_FORMAT_STRING const char *c, ...))
 
@@ -261,11 +261,11 @@ SDL2_PROTO(SDL2_bool,IsScreenKeyboardShown,(SDL_Window *a))
 SDL2_PROTO(void*,LoadObject,(const char *a))
 SDL2_PROTO(void*,LoadFunction,(void *a, const char *b))
 SDL2_PROTO(void,UnloadObject,(void *a))
-SDL2_PROTO(void,LogSetAllPriority,(SDL_LogPriority a))
-SDL2_PROTO(void,LogSetPriority,(int a, SDL_LogPriority b))
-SDL2_PROTO(SDL_LogPriority,LogGetPriority,(int a))
+SDL2_PROTO(void,LogSetAllPriority,(SDL2_LogPriority a))
+SDL2_PROTO(void,LogSetPriority,(int a, SDL2_LogPriority b))
+SDL2_PROTO(SDL2_LogPriority,LogGetPriority,(int a))
 SDL2_PROTO(void,LogResetPriorities,(void))
-SDL2_PROTO(void,LogMessageV,(int a, SDL_LogPriority b, const char *c, va_list d))
+SDL2_PROTO(void,LogMessageV,(int a, SDL2_LogPriority b, const char *c, va_list d))
 SDL2_PROTO(void,LogGetOutputFunction,(SDL_LogOutputFunction *a, void **b))
 SDL2_PROTO(void,LogSetOutputFunction,(SDL_LogOutputFunction a, void *b))
 SDL2_PROTO(void,SetMainReady,(void))
diff --git a/src/sdl3_include_wrapper.h b/src/sdl3_include_wrapper.h
index e477327..b14f06c 100644
--- a/src/sdl3_include_wrapper.h
+++ b/src/sdl3_include_wrapper.h
@@ -661,6 +661,7 @@
 #define SDL_LogInfo IGNORE_THIS_VERSION_OF_SDL_LogInfo
 #define SDL_LogMessage IGNORE_THIS_VERSION_OF_SDL_LogMessage
 #define SDL_LogMessageV IGNORE_THIS_VERSION_OF_SDL_LogMessageV
+#define SDL_LogTrace IGNORE_THIS_VERSION_OF_SDL_LogTrace
 #define SDL_LogVerbose IGNORE_THIS_VERSION_OF_SDL_LogVerbose
 #define SDL_LogWarn IGNORE_THIS_VERSION_OF_SDL_LogWarn
 #define SDL_MapGPUTransferBuffer IGNORE_THIS_VERSION_OF_SDL_MapGPUTransferBuffer
@@ -3735,6 +3736,10 @@
 #undef SDL_LogMessageV
 #endif
 
+#ifdef SDL_LogTrace
+#undef SDL_LogTrace
+#endif
+
 #ifdef SDL_LogVerbose
 #undef SDL_LogVerbose
 #endif
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index 6531b5d..ae18086 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -518,12 +518,12 @@ SDL3_SYM(SDL_bool,LockSurface,(SDL_Surface *a),(a),return)
 SDL3_SYM_PASSTHROUGH_RETCODE(SDL_bool,LockTexture,(SDL_Texture *a, const SDL_Rect *b, void **c, int *d),(a,b,c,d),return)
 SDL3_SYM(SDL_bool,LockTextureToSurface,(SDL_Texture *a, const SDL_Rect *b, SDL_Surface **c),(a,b,c),return)
 SDL3_SYM_RENAMED(void,LogGetOutputFunction,GetLogOutputFunction,(SDL_LogOutputFunction *a, void **b),(a,b),)
-SDL3_SYM_RENAMED(SDL_LogPriority,LogGetPriority,GetLogPriority,(int a),(a),return)
-SDL3_SYM_PASSTHROUGH(void,LogMessageV,(int a, SDL_LogPriority b, const char *c, va_list d),(a,b,c,d),)
+SDL3_SYM(SDL_LogPriority,GetLogPriority,(int a),(a),return)
+SDL3_SYM(void,LogMessageV,(int a, SDL_LogPriority b, const char *c, va_list d),(a,b,c,d),)
 SDL3_SYM_RENAMED(void,LogResetPriorities,ResetLogPriorities,(void),(),)
-SDL3_SYM_RENAMED(void,LogSetAllPriority,SetLogPriorities,(SDL_LogPriority a),(a),)
+SDL3_SYM(void,SetLogPriorities,(SDL_LogPriority a),(a),)
 SDL3_SYM_RENAMED(void,LogSetOutputFunction,SetLogOutputFunction,(SDL_LogOutputFunction a, void *b),(a,b),)
-SDL3_SYM_RENAMED(void,LogSetPriority,SetLogPriority,(int a, SDL_LogPriority b),(a,b),)
+SDL3_SYM(void,SetLogPriority,(int a, SDL_LogPriority b),(a,b),)
 SDL3_SYM(Uint32,MapRGB,(const SDL_PixelFormatDetails *a, const SDL_Palette *b, Uint8 c, Uint8 d, Uint8 e),(a,b,c,d,e),return)
 SDL3_SYM(Uint32,MapRGBA,(const SDL_PixelFormatDetails *a, const SDL_Palette *b, Uint8 c, Uint8 d, Uint8 e, Uint8 f),(a,b,c,d,e,f),return)
 SDL3_SYM(SDL_bool,MaximizeWindow,(SDL_Window *a),(a),return)