sdl12-compat: api: Implement X11_KeyToUnicode.

From d01bc97f5c025b2b8285242524853af77f344598 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 16 Dec 2021 00:21:43 -0500
Subject: [PATCH] api: Implement X11_KeyToUnicode.

This is for compat with some existing games that touched an internal piece
of SDL; it was not part of the public 1.2 API.

This is an _extremely_ naive implementation of this function, but I'm hoping
it's good enough for the few things that use it. Patches that don't make this
function ridiculous are welcome, though.

Fixes #153.
---
 src/SDL12_compat.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index a291837..83cd40b 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -57,8 +57,6 @@
 #undef snprintf
 #undef vsnprintf
 
-/* !!! IMPLEMENT_ME X11_KeyToUnicode ? */
-
 #define SDL_BlitSurface SDL_UpperBlit
 
 #ifdef __cplusplus
@@ -8095,6 +8093,35 @@ SDL_GL_EnableContext_Thread(void)
     SDL20_GL_MakeCurrent(enable ? VideoWindow20 : NULL, enable ? VideoGLContext20 : NULL);
 }
 
+
+/* X11_KeyToUnicode is an internal function in the SDL 1.2 x11 backend that some Linux
+   software (older versions of the Torque Engine, for example) would call directly, so
+   we're supplying an extremely naive implementation here. Apps using this should be
+   fixed if possible, and this implementation is generally incorrect but hopefully
+   enough to get apps to limp along.
+   As this isn't X11-specific, we supply it globally, so x11 binaries can transition
+   to Wayland, and if there's some wildly-misbuilt win32 software, they can call it
+   too. :) */
+
+DECLSPEC Uint16 SDLCALL
+X11_KeyToUnicode(SDL12Key key, SDL12Mod mod)
+{
+    if (((int) key) >= 127) {
+        return 0;
+    } else if ((key >= SDLK12_0) && (key <= SDLK12_9)) {
+        return (Uint16) ('0' + (key - SDLK12_0));
+    } else if ((key >= SDLK12_a) && (key <= SDLK12_z)) {
+        const int shifted = ((mod & (KMOD12_LSHIFT|KMOD12_RSHIFT)) != 0) ? 1 : 0;
+        int capital = ((mod & KMOD12_CAPS) != 0) ? 1 : 0;
+        if (shifted) {
+            capital = !capital;
+        }
+        return (Uint16) ((capital ? 'A' : 'a') + (key - SDLK12_a));
+    }
+
+    return (Uint16) key;
+}
+
 #ifdef __cplusplus
 }
 #endif