sdl12-compat: checkkeys test: Improve printing of Unicode-Chars

From 50f2f2d23789b07511ca2fd40724347c07aa872e Mon Sep 17 00:00:00 2001
From: Daniel Gibson <[EMAIL REDACTED]>
Date: Mon, 8 Nov 2021 00:33:30 +0100
Subject: [PATCH] checkkeys test: Improve printing of Unicode-Chars
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

.. at least on platforms that use UTF-8, like modern Linux (Windows
should work as well as before).
Also unified the output to print sth like "'Ä' (0x00C4)", i.e. both the
actual char and the unicode constant, on all platforms.
---
 test/checkkeys.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/test/checkkeys.c b/test/checkkeys.c
index b37da93..2df1d90 100644
--- a/test/checkkeys.c
+++ b/test/checkkeys.c
@@ -68,13 +68,19 @@ static void PrintKey(SDL_keysym *sym, int pressed)
 			printf(" (^%c)", sym->unicode+'@');
 		} else {
 #ifdef UNICODE
-			printf(" (%c)", sym->unicode);
-#else
+			printf(" '%c' (0x%.4X)", sym->unicode, (int)sym->unicode);
+#elif defined(_WIN32)
 			/* This is a Latin-1 program, so only show 8-bits */
 			if ( !(sym->unicode & 0xFF00) )
-				printf(" (%c)", sym->unicode);
+				printf(" '%c' (0x%.4X)", sym->unicode, (int)sym->unicode);
 			else
-				printf(" (0x%X)", sym->unicode);
+				printf(" (0x%.4X)", (int)sym->unicode);
+#else /* other platforms than Windows hopefully use UTF-8 for 8bit chars */
+			Uint32 utf32str[2] = { sym->unicode, 0 };
+			const char* utf32type = (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "UTF-32LE" : "UTF-32BE";
+			char* utf8str = SDL_iconv_string("UTF-8", utf32type, (const char*)utf32str, 2*4);
+			printf(" '%s' (0x%.4X)", utf8str, (int)sym->unicode);
+			SDL_free(utf8str);
 #endif
 		}
 	}