From b92e06b108f0482351a03e398370c9ad3441654c Mon Sep 17 00:00:00 2001 From: Maxime Vincent Date: Tue, 12 Dec 2017 10:09:06 +0100 Subject: [PATCH] AT keycodes to doom keys --- fbdoom/i_input_tty.c | 356 +++++++++++++++++++------------------------ 1 file changed, 155 insertions(+), 201 deletions(-) diff --git a/fbdoom/i_input_tty.c b/fbdoom/i_input_tty.c index 6e72c94..e66e17b 100644 --- a/fbdoom/i_input_tty.c +++ b/fbdoom/i_input_tty.c @@ -13,11 +13,10 @@ // GNU General Public License for more details. // // DESCRIPTION: -// DOOM graphics stuff for SDL. +// DOOM keyboard input via linux tty // -#include "SDL/SDL.h" #include #include #include @@ -53,9 +52,142 @@ int vanilla_keyboard_mapping = 1; // Is the shift key currently down? static int shiftdown = 0; + +// Lookup table for mapping AT keycodes to their doom keycode +static const char at_to_doom[] = +{ + /* 0x00 */ 0x00, + /* 0x01 */ KEY_ESCAPE, + /* 0x02 */ '1', + /* 0x03 */ '2', + /* 0x04 */ '3', + /* 0x05 */ '4', + /* 0x06 */ '5', + /* 0x07 */ '6', + /* 0x08 */ '7', + /* 0x09 */ '8', + /* 0x0a */ '9', + /* 0x0b */ '0', + /* 0x0c */ '-', + /* 0x0d */ '=', + /* 0x0e */ KEY_BACKSPACE, + /* 0x0f */ KEY_TAB, + /* 0x10 */ 'q', + /* 0x11 */ 'w', + /* 0x12 */ 'e', + /* 0x13 */ 'r', + /* 0x14 */ 't', + /* 0x15 */ 'y', + /* 0x16 */ 'u', + /* 0x17 */ 'i', + /* 0x18 */ 'o', + /* 0x19 */ 'p', + /* 0x1a */ '[', + /* 0x1b */ ']', + /* 0x1c */ KEY_ENTER, + /* 0x1d */ KEY_FIRE, /* KEY_RCTRL, */ + /* 0x1e */ 'a', + /* 0x1f */ 's', + /* 0x20 */ 'd', + /* 0x21 */ 'f', + /* 0x22 */ 'g', + /* 0x23 */ 'h', + /* 0x24 */ 'j', + /* 0x25 */ 'k', + /* 0x26 */ 'l', + /* 0x27 */ ';', + /* 0x28 */ '\'', + /* 0x29 */ '`', + /* 0x2a */ KEY_RSHIFT, + /* 0x2b */ '\\', + /* 0x2c */ 'z', + /* 0x2d */ 'x', + /* 0x2e */ 'c', + /* 0x2f */ 'v', + /* 0x30 */ 'b', + /* 0x31 */ 'n', + /* 0x32 */ 'm', + /* 0x33 */ ',', + /* 0x34 */ '.', + /* 0x35 */ '/', + /* 0x36 */ KEY_RSHIFT, + /* 0x37 */ KEYP_MULTIPLY, + /* 0x38 */ KEY_LALT, + /* 0x39 */ KEY_USE, + /* 0x3a */ KEY_CAPSLOCK, + /* 0x3b */ KEY_F1, + /* 0x3c */ KEY_F2, + /* 0x3d */ KEY_F3, + /* 0x3e */ KEY_F4, + /* 0x3f */ KEY_F5, + /* 0x40 */ KEY_F6, + /* 0x41 */ KEY_F7, + /* 0x42 */ KEY_F8, + /* 0x43 */ KEY_F9, + /* 0x44 */ KEY_F10, + /* 0x45 */ KEY_NUMLOCK, + /* 0x46 */ 0x0, + /* 0x47 */ 0x0, /* 47 (Keypad-7/Home) */ + /* 0x48 */ 0x0, /* 48 (Keypad-8/Up) */ + /* 0x49 */ 0x0, /* 49 (Keypad-9/PgUp) */ + /* 0x4a */ 0x0, /* 4a (Keypad--) */ + /* 0x4b */ 0x0, /* 4b (Keypad-4/Left) */ + /* 0x4c */ 0x0, /* 4c (Keypad-5) */ + /* 0x4d */ 0x0, /* 4d (Keypad-6/Right) */ + /* 0x4e */ 0x0, /* 4e (Keypad-+) */ + /* 0x4f */ 0x0, /* 4f (Keypad-1/End) */ + /* 0x50 */ 0x0, /* 50 (Keypad-2/Down) */ + /* 0x51 */ 0x0, /* 51 (Keypad-3/PgDn) */ + /* 0x52 */ 0x0, /* 52 (Keypad-0/Ins) */ + /* 0x53 */ 0x0, /* 53 (Keypad-./Del) */ + /* 0x54 */ 0x0, /* 54 (Alt-SysRq) on a 84+ key keyboard */ + /* 0x55 */ 0x0, + /* 0x56 */ 0x0, + /* 0x57 */ 0x0, + /* 0x58 */ 0x0, + /* 0x59 */ 0x0, + /* 0x5a */ 0x0, + /* 0x5b */ 0x0, + /* 0x5c */ 0x0, + /* 0x5d */ 0x0, + /* 0x5e */ 0x0, + /* 0x5f */ 0x0, + /* 0x60 */ 0x0, + /* 0x61 */ 0x0, + /* 0x62 */ 0x0, + /* 0x63 */ 0x0, + /* 0x64 */ 0x0, + /* 0x65 */ 0x0, + /* 0x66 */ 0x0, + /* 0x67 */ KEY_UPARROW, + /* 0x68 */ 0x0, + /* 0x69 */ KEY_LEFTARROW, + /* 0x6a */ KEY_RIGHTARROW, + /* 0x6b */ 0x0, + /* 0x6c */ KEY_DOWNARROW, + /* 0x6d */ 0x0, + /* 0x6e */ 0x0, + /* 0x6f */ 0x0, + /* 0x70 */ 0x0, + /* 0x71 */ 0x0, + /* 0x72 */ 0x0, + /* 0x73 */ 0x0, + /* 0x74 */ 0x0, + /* 0x75 */ 0x0, + /* 0x76 */ 0x0, + /* 0x77 */ 0x0, + /* 0x78 */ 0x0, + /* 0x79 */ 0x0, + /* 0x7a */ 0x0, + /* 0x7b */ 0x0, + /* 0x7c */ 0x0, + /* 0x7d */ 0x0, + /* 0x7e */ 0x0, + /* 0x7f */ KEY_FIRE, //KEY_RCTRL, +}; + // Lookup table for mapping ASCII characters to their equivalent when // shift is pressed on an American layout keyboard: - static const char shiftxform[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, @@ -149,8 +281,6 @@ static int kbd_init(void) int i; int flags; - printf(">>>>>>>>>>>>>> %s\r\n", __func__); - /* First we need to find a file descriptor that represents the system's keyboard. This should be /dev/tty, /dev/console, stdin, stdout, or stderr. We'll try them in that order. @@ -233,162 +363,20 @@ int kbd_read(int *pressed, unsigned char *key) /* Print the keycode. The top bit is the pressed/released flag, and the lower seven are the keycode. */ - printf("%s: 0x%2X (%i)\n", *pressed ? "Released" : " Pressed", (unsigned int)*key, (unsigned int)*key); - //if (*key == 0x0E) { - // printf("Backspace pressed.\n"); - // kbd_shutdown(); - //} + //printf("%s: 0x%2X (%i)\n", *pressed ? "Released" : " Pressed", (unsigned int)*key, (unsigned int)*key); return 1; } - -// -// Translates the SDL key -// - -#define TTY_ESC (0x01) -#define TTY_ENTER (0x1C) -#define TTY_W (0x11) -#define TTY_A (0x1E) -#define TTY_S (0x1F) -#define TTY_D (0x20) -#define TTY_LCTRL (0x1D) -#define TTY_RCTRL (0x61) -#define TTY_UP (0x67) -#define TTY_LEFT (0x69) -#define TTY_DOWN (0x6C) -#define TTY_RIGHT (0x6A) -#define TTY_Y (0x15) -#define TTY_N (0x31) - -#if 0 -enum sdl_keys { - SDLK_a = 97, - SDLK_b = 98, - SDLK_c = 99, - SDLK_d = 100, - SDLK_e = 101, - SDLK_f = 102, - SDLK_g = 103, - SDLK_h = 104, - SDLK_i = 105, - SDLK_j = 106, - SDLK_k = 107, - SDLK_l = 108, - SDLK_m = 109, - SDLK_n = 110, - SDLK_o = 111, - SDLK_p = 112, - SDLK_q = 113, - SDLK_r = 114, - SDLK_s = 115, - SDLK_t = 116, - SDLK_u = 117, - SDLK_v = 118, - SDLK_w = 119, - SDLK_x = 120, - SDLK_y = 121, - SDLK_z = 122, -}; -#endif - static unsigned char TranslateKey(unsigned char key) { - switch(key) - { - case TTY_ENTER: return KEY_ENTER; - case TTY_ESC: return KEY_ESCAPE; - case TTY_W: return 'w'; - case TTY_A: return 'a'; - case TTY_S: return 's'; - case TTY_D: return 'd'; - case TTY_LEFT: return KEY_LEFTARROW; - case TTY_RIGHT: return KEY_RIGHTARROW; - case TTY_DOWN: return KEY_DOWNARROW; - case TTY_UP: return KEY_UPARROW; - case TTY_Y: printf("Y\r\n"); return SDLK_y; - case TTY_N: printf("N\r\n"); return SDLK_n; + if (key < sizeof(at_to_doom)) + return at_to_doom[key]; + else + return 0x0; - case TTY_LCTRL: - case TTY_RCTRL: - return KEY_RCTRL; - -// 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_PRINT: return KEY_PRTSCR; -// -// case SDLK_BACKSPACE: return KEY_BACKSPACE; -// case SDLK_DELETE: return KEY_DEL; -// -// case SDLK_PAUSE: return KEY_PAUSE; -// -// case SDLK_MINUS: return KEY_MINUS; -// -// 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_SCROLLOCK: return KEY_SCRLCK; -// case SDLK_NUMLOCK: return KEY_NUMLOCK; -// -// case SDLK_KP0: return KEYP_0; -// case SDLK_KP1: return KEYP_1; -// case SDLK_KP2: return KEYP_2; -// case SDLK_KP3: return KEYP_3; -// case SDLK_KP4: return KEYP_4; -// case SDLK_KP5: return KEYP_5; -// case SDLK_KP6: return KEYP_6; -// case SDLK_KP7: return KEYP_7; -// case SDLK_KP8: return KEYP_8; -// case SDLK_KP9: return KEYP_9; -// -// 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 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: - // return tolower(key); - } + //default: + // return tolower(key); } // Get the equivalent ASCII (Unicode?) character for a keypress. @@ -399,17 +387,17 @@ static unsigned char GetTypedChar(unsigned char key) // Is shift held down? If so, perform a translation. - //if (shiftdown > 0) - //{ - // if (key >= 0 && key < arrlen(shiftxform)) - // { - // key = shiftxform[key]; - // } - // else - // { - // key = 0; - // } - //} + if (shiftdown > 0) + { + if (key >= 0 && key < arrlen(shiftxform)) + { + key = shiftxform[key]; + } + else + { + key = 0; + } + } return key; } @@ -424,7 +412,7 @@ static void UpdateShiftStatus(int pressed, unsigned char key) change = -1; } - if (key == SDLK_LSHIFT || key == SDLK_RSHIFT) { + if (key == 0x2a || key == 0x36) { shiftdown += change; } } @@ -445,7 +433,7 @@ void I_GetEvent(void) I_Quit(); } - //UpdateShiftStatus(pressed, key); + UpdateShiftStatus(pressed, key); // process event @@ -459,7 +447,6 @@ void I_GetEvent(void) if (event.data1 != 0) { - printf("Post keydown event.data1 = 0x%x\r\n", event.data1); D_PostEvent(&event); } } @@ -478,7 +465,6 @@ void I_GetEvent(void) if (event.data1 != 0) { - printf("Post keyup event.data1 = 0x%x\r\n", event.data1); D_PostEvent(&event); } break; @@ -497,42 +483,10 @@ void I_GetEvent(void) */ } - -//void I_ShutdownGraphics(void) -//{ -// //SDL_QuitSubSystem(SDL_INIT_VIDEO); -//} - -// -// I_StartTic -// -//void I_StartTic (void) - void I_InitInput(void) { - SDL_Event dummy; - kbd_init(); - if (SDL_Init(SDL_INIT_VIDEO) < 0) - { - I_Error("Failed to initialize video: %s", SDL_GetError()); - } - //UpdateFocus(); - - // We need SDL to give us translated versions of keys as well - - SDL_EnableUNICODE(1); - - // Repeat key presses - this is what Vanilla Doom does - // Not sure about repeat rate - probably dependent on which DOS - // driver is used. This is good enough though. - - SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - - // clear out any events waiting at the start and center the mouse - - while (SDL_PollEvent(&dummy)); }