foxygit / doom Log in
commit 30f853b65820280931e06e07fd5f624313670305
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Wed Dec 21 15:11:22 2016 +0000
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Wed Dec 21 15:11:22 2016 +0000

    textscreen: Fix input of Unicode characters.

    For non-ASCII characters we map into a higher range to avoid conflict
    with special keys. Restore this behavior.
---
 textscreen/txt_main.h | 5 +++++
 textscreen/txt_sdl.c  | 5 ++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/textscreen/txt_main.h b/textscreen/txt_main.h
index 4a2c585a..72aae918 100644
--- a/textscreen/txt_main.h
+++ b/textscreen/txt_main.h
@@ -61,6 +61,11 @@
         ( (x) < 128 ? (x) :                                               \
           (x) >= TXT_UNICODE_BASE ? ((x) - TXT_UNICODE_BASE + 128) : 0 )

+// Convert a Unicode character to a key value:
+
+#define TXT_UNICODE_TO_KEY(u)                                            \
+        ( (u) < 128 ? (u) : ((u) - 128 + TXT_UNICODE_BASE) )
+
 // Screen size

 #define TXT_SCREEN_W 80
diff --git a/textscreen/txt_sdl.c b/textscreen/txt_sdl.c
index a79dbb44..e936b85a 100644
--- a/textscreen/txt_sdl.c
+++ b/textscreen/txt_sdl.c
@@ -671,7 +671,10 @@ signed int TXT_GetChar(void)
                 {
                     // TODO: Support input of more than just the first char?
                     const char *p = ev.text.text;
-                    return TXT_DecodeUTF8(&p);
+                    int result = TXT_DecodeUTF8(&p);
+                    // 0-127 is ASCII, but we map non-ASCII Unicode chars into
+                    // a higher range to avoid conflicts with special keys.
+                    return TXT_UNICODE_TO_KEY(result);
                 }
                 break;