shithub: choc

Download patch

ref: d745a6409c84a9ffb85e5d2029132642435c5879
parent: c818fb021632cbb68229ba265f01b060e6b6d0f4
author: Simon Howard <[email protected]>
date: Fri Feb 3 16:09:57 EST 2012

Support Unicode input by mapping typed Unicode characters >= 128 up into
a higher range to avoid conflicts with Doom's key constants.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 2490

--- a/textscreen/txt_main.h
+++ b/textscreen/txt_main.h
@@ -32,9 +32,21 @@
 
 #include "txt_sdl.h"
 
+// textscreen key values:
+// Key values are difficult because we have to support multiple conflicting
+// address spaces.
+// First, Doom's key constants use 0-127 as ASCII and extra values from
+// 128-255 to represent special keys. Second, mouse buttons are represented
+// as buttons. Finally, we want to be able to support Unicode.
+//
+// So we define different ranges:
+// 0-255:    Doom key constants, including ASCII.
+// 256-511:  Mouse buttons and other reserved.
+// >=512:    Unicode values greater than 127 are offset up into this range.
+
 // Special keypress values that correspond to mouse button clicks
 
-#define TXT_MOUSE_BASE         0x10000
+#define TXT_MOUSE_BASE         256
 #define TXT_MOUSE_LEFT         (TXT_MOUSE_BASE + 0)
 #define TXT_MOUSE_RIGHT        (TXT_MOUSE_BASE + 1)
 #define TXT_MOUSE_MIDDLE       (TXT_MOUSE_BASE + 2)
@@ -41,6 +53,22 @@
 #define TXT_MOUSE_SCROLLUP     (TXT_MOUSE_BASE + 3)
 #define TXT_MOUSE_SCROLLDOWN   (TXT_MOUSE_BASE + 4)
 #define TXT_MAX_MOUSE_BUTTONS  16
+
+#define TXT_KEY_TO_MOUSE_BUTTON(x)                                        \
+        ( (x) >= TXT_MOUSE_BASE                                           \
+       && (x) < TXT_MOUSE_BASE + TXT_MAX_MOUSE_BUTTONS ?                  \
+          (x) - TXT_MOUSE_BASE : -1 )
+
+// Unicode offset. Unicode values from 128 onwards are offset up into
+// this range, so TXT_UNICODE_BASE = Unicode character #128, and so on.
+
+#define TXT_UNICODE_BASE       512
+
+// Convert a key value to a Unicode character:
+
+#define TXT_KEY_TO_UNICODE(x)                                             \
+        ( (x) < 128 ? (x) :                                               \
+          (x) >= TXT_UNICODE_BASE ? ((x) - TXT_UNICODE_BASE + 128) : 0 )
 
 // Screen size
 
--- a/textscreen/txt_sdl.c
+++ b/textscreen/txt_sdl.c
@@ -431,7 +431,17 @@
 
     if (key_mapping)
     {
-        return sym->unicode;
+        // Unicode characters beyond the ASCII range need to be
+        // mapped up into textscreen's Unicode range.
+
+        if (sym->unicode < 128)
+        {
+            return sym->unicode;
+        }
+        else
+        {
+            return sym->unicode - 128 + TXT_UNICODE_BASE;
+        }
     }
     else
     {