From da86fe6fd8bed7741bdf86492f820243953f4d15 Mon Sep 17 00:00:00 2001 From: Vadim Grigoruk Date: Sat, 25 Aug 2018 11:36:57 +0300 Subject: [PATCH] #707 added very dirty hack to support international keyboard input --- src/studio.c | 42 +++++++++---------- src/system.c | 114 +++++++++++++++++++++++++++++++++++---------------- src/system.h | 2 - 3 files changed, 97 insertions(+), 61 deletions(-) diff --git a/src/studio.c b/src/studio.c index cb1b756..cfff72c 100644 --- a/src/studio.c +++ b/src/studio.c @@ -237,35 +237,31 @@ static struct char getKeyboardText() { - char text = getSystem()->getKeyboardText(); + tic_mem* tic = impl.studio.tic; - if(text) return text; + static const char Symbols[] = " abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ "; + static const char Shift[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ)!@#$%^&*(_+{}|:\"~<>? "; - tic_mem* tic = impl.studio.tic; + enum{Count = sizeof Symbols}; - static const char Symbols[] = " abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ "; - static const char Shift[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ)!@#$%^&*(_+{}|:\"~<>? "; + for(s32 i = 0; i < TIC80_KEY_BUFFER; i++) + { + tic_key key = tic->ram.input.keyboard.keys[i]; - enum{Count = sizeof Symbols}; + if(key > 0 && key < Count && tic->api.keyp(tic, key, KEYBOARD_HOLD, KEYBOARD_PERIOD)) + { + bool caps = tic->api.key(tic, tic_key_capslock); + bool shift = tic->api.key(tic, tic_key_shift); - for(s32 i = 0; i < TIC80_KEY_BUFFER; i++) - { - tic_key key = tic->ram.input.keyboard.keys[i]; + return caps + ? key >= tic_key_a && key <= tic_key_z + ? shift ? Symbols[key] : Shift[key] + : shift ? Shift[key] : Symbols[key] + : shift ? Shift[key] : Symbols[key]; + } + } - if(key > 0 && key < Count && tic->api.keyp(tic, key, KEYBOARD_HOLD, KEYBOARD_PERIOD)) - { - bool caps = tic->api.key(tic, tic_key_capslock); - bool shift = tic->api.key(tic, tic_key_shift); - - return caps - ? key >= tic_key_a && key <= tic_key_z - ? shift ? Symbols[key] : Shift[key] - : shift ? Shift[key] : Symbols[key] - : shift ? Shift[key] : Symbols[key]; - } - } - - return 0; + return 0; } bool keyWasPressed(tic_key key) diff --git a/src/system.c b/src/system.c index 51c7566..282a7a2 100644 --- a/src/system.c +++ b/src/system.c @@ -395,55 +395,97 @@ static void processMouse() } } -// TODO: ugly hack, but I didn't find a better solution -// will try to fix it later -static char getKeyboardText() -{ - char text = platform.keyboard.text; - platform.keyboard.text = 0; - return text; -} - static void processKeyboard() { - static const u32 KeyboardCodes[tic_keys_count] = - { - #include "keycodes.inl" - }; - tic80_input* input = &platform.studio->tic->ram.input; input->keyboard.data = 0; enum{BufSize = COUNT_OF(input->keyboard.keys)}; - s32 c = 0; + SDL_Keymod mod = SDL_GetModState(); + + // TODO: the ugliest hack ever + // will try to remove it + if(mod & KMOD_RALT) { - SDL_Keymod mod = SDL_GetModState(); - if(mod & KMOD_SHIFT) input->keyboard.keys[c++] = tic_key_shift; - if(mod & (KMOD_CTRL | KMOD_GUI)) input->keyboard.keys[c++] = tic_key_ctrl; - if(mod & KMOD_ALT) input->keyboard.keys[c++] = tic_key_alt; - if(mod & KMOD_CAPS) input->keyboard.keys[c++] = tic_key_capslock; + static const struct {tic_keycode code; tic_keycode shift;} TextCodes[] = + { + ['-'] = {tic_key_minus, tic_key_unknown}, + ['='] = {tic_key_equals, tic_key_unknown}, + ['['] = {tic_key_leftbracket, tic_key_unknown}, + [']'] = {tic_key_rightbracket, tic_key_unknown}, + ['\\'] = {tic_key_backslash, tic_key_unknown}, + [';'] = {tic_key_semicolon, tic_key_unknown}, + ['\''] = {tic_key_apostrophe, tic_key_unknown}, + ['`'] = {tic_key_grave, tic_key_unknown}, + [','] = {tic_key_comma, tic_key_unknown}, + ['.'] = {tic_key_period, tic_key_unknown}, + ['/'] = {tic_key_slash, tic_key_unknown}, + [')'] = {tic_key_0, tic_key_shift}, + ['!'] = {tic_key_1, tic_key_shift}, + ['@'] = {tic_key_2, tic_key_shift}, + ['#'] = {tic_key_3, tic_key_shift}, + ['$'] = {tic_key_4, tic_key_shift}, + ['%'] = {tic_key_5, tic_key_shift}, + ['^'] = {tic_key_6, tic_key_shift}, + ['&'] = {tic_key_7, tic_key_shift}, + ['*'] = {tic_key_8, tic_key_shift}, + ['('] = {tic_key_9, tic_key_shift}, + ['_'] = {tic_key_minus, tic_key_shift}, + ['+'] = {tic_key_equals, tic_key_shift}, + ['{'] = {tic_key_leftbracket, tic_key_shift}, + ['}'] = {tic_key_rightbracket, tic_key_shift}, + ['|'] = {tic_key_backslash, tic_key_shift}, + [':'] = {tic_key_semicolon, tic_key_shift}, + ['"'] = {tic_key_apostrophe, tic_key_shift}, + ['~'] = {tic_key_grave, tic_key_shift}, + ['<'] = {tic_key_comma, tic_key_shift}, + ['>'] = {tic_key_period, tic_key_shift}, + ['?'] = {tic_key_slash, tic_key_shift}, + }; + + u8 text = platform.keyboard.text; + + if(text && text < COUNT_OF(TextCodes)) + { + input->keyboard.keys[0] = TextCodes[text].code; + input->keyboard.keys[1] = TextCodes[text].shift; + } } - - const u8* keyboard = SDL_GetKeyboardState(NULL); - - for(s32 i = 0; i < SDL_NUM_SCANCODES && c < BufSize; i++) + else { - if(keyboard[i]) - { - SDL_Keycode keycode = i == SDL_SCANCODE_AC_BACK - ? SDLK_ESCAPE - : SDL_GetKeyFromScancode(i); + static const u32 KeyboardCodes[tic_keys_count] = + { + #include "keycodes.inl" + }; - for(s32 k = 0; k < COUNT_OF(KeyboardCodes); k++) - { - if(KeyboardCodes[k] == keycode) + s32 c = 0; + { + if(mod & KMOD_SHIFT) input->keyboard.keys[c++] = tic_key_shift; + if(mod & KMOD_CTRL) input->keyboard.keys[c++] = tic_key_ctrl; + if(mod & KMOD_CAPS) input->keyboard.keys[c++] = tic_key_capslock; + } + + const u8* keyboard = SDL_GetKeyboardState(NULL); + + for(s32 i = 0; i < SDL_NUM_SCANCODES && c < BufSize; i++) + { + if(keyboard[i]) + { + SDL_Keycode keycode = i == SDL_SCANCODE_AC_BACK + ? SDLK_ESCAPE + : SDL_GetKeyFromScancode(i); + + for(s32 k = 0; k < COUNT_OF(KeyboardCodes); k++) { - input->keyboard.keys[c++] = k; - break; + if(KeyboardCodes[k] == keycode) + { + input->keyboard.keys[c++] = k; + break; + } } } - } + } } } @@ -756,6 +798,7 @@ static void pollEvent() { input->mouse.btns = 0; + platform.keyboard.text = 0; } SDL_Event event; @@ -1311,7 +1354,6 @@ static System systemInterface = .preseed = preseed, .poll = pollEvent, .updateConfig = updateConfig, - .getKeyboardText = getKeyboardText, }; static void gpuTick() diff --git a/src/system.h b/src/system.h index cb34d5b..7473064 100644 --- a/src/system.h +++ b/src/system.h @@ -29,8 +29,6 @@ typedef struct void (*updateConfig)(); - char (*getKeyboardText)(); - } System; typedef struct