shithub: choc

Download patch

ref: 9ce34086ca86966b0df9f7cb2b32b8b9bd86396e
parent: 719ef21ccdc3eabfe49e73f49fdadf53dd30423e
author: Jonathan Dowland <jon+github@alcopop.org>
date: Wed Mar 2 16:05:43 EST 2016

textscreen: Fix handling of mapped input chars

Copy the TranslateKey definition from i_input.c.

This fixes accessing sub-menus such as advanced in the graphics
menu via 'A', for example.

For the future: could we define this (and the supporting stuff)
in just one place? Probably the canonical copy should be in
libtextscreen, since that is included/linked to by chocolate-*,
and not the other way around; but there is already some grey
area e.g. textscreen including doomkeys.h.

This now works as well as the sdl1 setup programs did, I think.
There are still some quirks and things which don't work at all
well if you wander off US-ASCII, just as with the sdl1 version.
I also suspect that the setup programs should respond to changes
in the vanilla_keyboard_mapping setting as part of fixing that.

--- a/textscreen/txt_sdl.c
+++ b/textscreen/txt_sdl.c
@@ -67,6 +67,8 @@
 // normal_font otherwise.
 static const txt_font_t highdpi_font = { "normal-highdpi", NULL, 8, 16 };
 
+static const int scancode_translate_table[] = SCANCODE_TO_KEYS_ARRAY;
+
 //#define TANGO
 
 #ifndef TANGO
@@ -475,123 +477,38 @@
 // Translates the SDL key
 //
 
+// XXX: duplicate from doomtype.h
+#define arrlen(array) (sizeof(array) / sizeof(*array))
+
 static int TranslateKey(SDL_Keysym *sym)
 {
-    switch(sym->sym)
-    {
-        case SDLK_LEFT:        return KEY_LEFTARROW;
-        case SDLK_RIGHT:       return KEY_RIGHTARROW;
-        case SDLK_DOWN:        return KEY_DOWNARROW;
-        case SDLK_UP:          return KEY_UPARROW;
-        case SDLK_ESCAPE:      return KEY_ESCAPE;
-        case SDLK_RETURN:      return KEY_ENTER;
-        case SDLK_TAB:         return KEY_TAB;
-        case SDLK_F1:          return KEY_F1;
-        case SDLK_F2:          return KEY_F2;
-        case SDLK_F3:          return KEY_F3;
-        case SDLK_F4:          return KEY_F4;
-        case SDLK_F5:          return KEY_F5;
-        case SDLK_F6:          return KEY_F6;
-        case SDLK_F7:          return KEY_F7;
-        case SDLK_F8:          return KEY_F8;
-        case SDLK_F9:          return KEY_F9;
-        case SDLK_F10:         return KEY_F10;
-        case SDLK_F11:         return KEY_F11;
-        case SDLK_F12:         return KEY_F12;
-        case SDLK_PRINTSCREEN:       return KEY_PRTSCR;
+    int scancode = sym->scancode;
 
-        case SDLK_BACKSPACE:   return KEY_BACKSPACE;
-        case SDLK_DELETE:      return KEY_DEL;
-
-        case SDLK_PAUSE:       return KEY_PAUSE;
-
-        case SDLK_LSHIFT:
-        case SDLK_RSHIFT:
-                               return KEY_RSHIFT;
-
-        case SDLK_LCTRL:
-        case SDLK_RCTRL:
-                               return KEY_RCTRL;
-
-        case SDLK_LALT:
-        case SDLK_RALT:
-                               return KEY_RALT;
-
-        case SDLK_CAPSLOCK:    return KEY_CAPSLOCK;
-        case SDLK_SCROLLLOCK:   return KEY_SCRLCK;
-
-        case SDLK_HOME:        return KEY_HOME;
-        case SDLK_INSERT:      return KEY_INS;
-        case SDLK_END:         return KEY_END;
-        case SDLK_PAGEUP:      return KEY_PGUP;
-        case SDLK_PAGEDOWN:    return KEY_PGDN;
-
-#ifdef SDL_HAVE_APP_KEYS
-        case SDLK_APP1:        return KEY_F1;
-        case SDLK_APP2:        return KEY_F2;
-        case SDLK_APP3:        return KEY_F3;
-        case SDLK_APP4:        return KEY_F4;
-        case SDLK_APP5:        return KEY_F5;
-        case SDLK_APP6:        return KEY_F6;
-#endif
-
-        default:               break;
-    }
-
-    // Returned value is different, depending on whether key mapping is
-    // enabled.  Key mapping is preferable most of the time, for typing
-    // in text, etc.  However, when we want to read raw keyboard codes
-    // for the setup keyboard configuration dialog, we want the raw
-    // key code.
-
-    if (key_mapping)
+    switch (scancode)
     {
-        // Unicode characters beyond the ASCII range need to be
-        // mapped up into textscreen's Unicode range.
+        case SDL_SCANCODE_LCTRL:
+        case SDL_SCANCODE_RCTRL:
+            return KEY_RCTRL;
 
-#if 0
-    // SDL2-TODO
-        if (sym->unicode < 128)
-        {
-            return sym->unicode;
-        }
-        else
-        {
-            return sym->unicode - 128 + TXT_UNICODE_BASE;
-        }
-#endif
-        return 0;
-    }
-    else
-    {
-        // Keypad mapping is only done when we want a raw value:
-        // most of the time, the keypad should behave as it normally
-        // does.
+        case SDL_SCANCODE_LSHIFT:
+        case SDL_SCANCODE_RSHIFT:
+            return KEY_RSHIFT;
 
-        switch (sym->sym)
-        {
-            case SDLK_KP_0:         return KEYP_0;
-            case SDLK_KP_1:         return KEYP_1;
-            case SDLK_KP_2:         return KEYP_2;
-            case SDLK_KP_3:         return KEYP_3;
-            case SDLK_KP_4:         return KEYP_4;
-            case SDLK_KP_5:         return KEYP_5;
-            case SDLK_KP_6:         return KEYP_6;
-            case SDLK_KP_7:         return KEYP_7;
-            case SDLK_KP_8:         return KEYP_8;
-            case SDLK_KP_9:         return KEYP_9;
+        case SDL_SCANCODE_LALT:
+            return KEY_LALT;
 
-            case SDLK_KP_PERIOD:   return KEYP_PERIOD;
-            case SDLK_KP_MULTIPLY: return KEYP_MULTIPLY;
-            case SDLK_KP_PLUS:     return KEYP_PLUS;
-            case SDLK_KP_MINUS:    return KEYP_MINUS;
-            case SDLK_KP_DIVIDE:   return KEYP_DIVIDE;
-            case SDLK_KP_EQUALS:   return KEYP_EQUALS;
-            case SDLK_KP_ENTER:    return KEYP_ENTER;
+        case SDL_SCANCODE_RALT:
+            return KEY_RALT;
 
-            default:
-                return tolower(sym->sym);
-        }
+        default:
+            if (scancode >= 0 && scancode < arrlen(scancode_translate_table))
+            {
+                return scancode_translate_table[scancode];
+            }
+            else
+            {
+                return 0;
+            }
     }
 }