2019-06-21 19:38:31 +02:00
|
|
|
// The OS/K Team licenses this file to you under the MIT license.
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
#include <pc/console.h>
|
|
|
|
|
|
|
|
#define KEYBUFSIZE 8192
|
|
|
|
|
|
|
|
typedef uint keycode_t;
|
|
|
|
|
|
|
|
keycode_t keybuf[KEYBUFSIZE] = { 0 };
|
|
|
|
keycode_t *keybuf_last = &keybuf[0];
|
|
|
|
keycode_t *keybuf_ptr = &keybuf[0];
|
|
|
|
|
|
|
|
void console_addkey(ctx_t *ctx, keycode_t key)
|
|
|
|
{
|
|
|
|
*keybuf_ptr++ = key;
|
|
|
|
|
|
|
|
if (keybuf_ptr == keybuf + KEYBUFSIZE)
|
|
|
|
keybuf_ptr = &keybuf[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
keycode_t console_getkey(ctx_t *ctx)
|
|
|
|
{
|
|
|
|
keycode_t rc = *keybuf_last++;
|
|
|
|
|
|
|
|
if (keybuf_last == keybuf + KEYBUFSIZE)
|
|
|
|
keybuf_last = &keybuf[0];
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
keycode_t console_scankeybuf(ctx_t *ctx)
|
|
|
|
{
|
|
|
|
if (keybuf_last != keybuf_ptr)
|
|
|
|
return console_getkey(ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void console_handle_input(ctx_t *ctx, SDL_Keycode key)
|
|
|
|
{
|
|
|
|
keycode_t code = 0;
|
|
|
|
|
|
|
|
SDL_Keymod mod = SDL_GetModState();
|
|
|
|
|
|
|
|
if (key <= 'z' && key >= 'a')
|
|
|
|
{
|
|
|
|
if (mod & (KMOD_CTRL|KMOD_ALT))
|
|
|
|
{
|
|
|
|
if (key == 'c')
|
|
|
|
_except(ctx, E_BRK, "Ctrl+C received");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod & KMOD_SHIFT)
|
|
|
|
key -= ('a' - 'A');
|
|
|
|
|
|
|
|
console_addkey(ctx, key);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-07 15:36:47 +02:00
|
|
|
#define QKEY(key, c, cshift, cmode, calt, cgui) \
|
|
|
|
_QKEY(SDLK_##key, c, cshift, cmode, calt, cgui)
|
|
|
|
|
|
|
|
#define _QKEY(key, c, cshift, cmode, calt, cgui) \
|
|
|
|
case key: \
|
|
|
|
if (__builtin_expect((mod & \
|
|
|
|
(KMOD_SHIFT|KMOD_ALT|KMOD_MODE|KMOD_GUI)) == 0, 1)) \
|
|
|
|
code = c; \
|
|
|
|
else if ((mod & KMOD_SHIFT)) code = cshift; \
|
|
|
|
else if ((mod & KMOD_LALT)) code = calt; \
|
|
|
|
else if ((mod & KMOD_RALT)) code = cmode; \
|
|
|
|
else if ((mod & KMOD_GUI)) code = cgui; \
|
|
|
|
break;
|
2019-06-21 19:38:31 +02:00
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case SDLK_SPACE: code = ' '; break;
|
|
|
|
case SDLK_BACKSPACE: code = '\b'; break;
|
|
|
|
|
|
|
|
case SDLK_RETURN:
|
|
|
|
case SDLK_RETURN2:
|
|
|
|
code = '\n';
|
|
|
|
break;
|
|
|
|
|
2019-07-07 15:36:47 +02:00
|
|
|
case SDLK_TAB: code = '\t'; break;
|
|
|
|
|
|
|
|
// AZERTY
|
|
|
|
|
|
|
|
QKEY(EXCLAIM, '!', 0, 0, 0, 0);
|
|
|
|
QKEY(DOLLAR, '$', 0, 0, 0, 0);
|
|
|
|
QKEY(ASTERISK, '*', 0, 0, 0, 0);
|
|
|
|
QKEY(COLON, ':', '/', 0, 0, 0);
|
|
|
|
QKEY(LESS, '<', '>', 0, 0, 0);
|
|
|
|
QKEY(CARET, '^', 0, 0, 0, 0);
|
|
|
|
|
|
|
|
QKEY(AMPERSAND, '&', '1', 0, 0, 0);
|
|
|
|
QKEY(QUOTEDBL, '"', '3', '#', 0, 0);
|
|
|
|
QKEY(LEFTPAREN, '(', '5', '[', 0, 0);
|
|
|
|
QKEY(UNDERSCORE, '_', '8', '\\', 0, 0);
|
|
|
|
QKEY(RIGHTPAREN, ')', 0, ']', 0, 0);
|
|
|
|
|
|
|
|
// QWERTY
|
|
|
|
|
|
|
|
QKEY(BACKSLASH, '\\', '|', 0, 0, 0);
|
|
|
|
QKEY(PERIOD, '.', '>', 0, 0, 0);
|
|
|
|
QKEY(SLASH, '/', '?', 0, 0, 0);
|
|
|
|
QKEY(1, '1', '!', 0, 0, 0);
|
|
|
|
QKEY(2, '2', '@', 0, 0, 0);
|
|
|
|
QKEY(3, '3', '#', 0, 0, 0);
|
|
|
|
QKEY(4, '4', '$', 0, 0, 0);
|
|
|
|
QKEY(5, '5', '%', 0, 0, 0);
|
|
|
|
QKEY(6, '6', '^', 0, 0, 0);
|
|
|
|
QKEY(7, '7', '&', 0, 0, 0);
|
|
|
|
QKEY(8, '8', '*', 0, 0, 0);
|
|
|
|
QKEY(9, '9', '(', 0, 0, 0);
|
|
|
|
QKEY(0, '0', ')', 0, 0, 0);
|
|
|
|
QKEY(LEFTBRACKET, '[', '{', 0, 0, 0);
|
|
|
|
QKEY(RIGHTBRACKET, ']', '}', 0, 0, 0);
|
|
|
|
QKEY(BACKQUOTE, '`', '~', 0, 0, 0);
|
|
|
|
|
|
|
|
// MIXED
|
|
|
|
|
|
|
|
#ifdef QWERTY
|
|
|
|
|
|
|
|
QKEY(QUOTE, '\'', '"', 0, 0, 0);
|
|
|
|
QKEY(COMMA, ',', '<', 0, 0, 0);
|
|
|
|
QKEY(SEMICOLON, ';', ':', 0, 0, 0);
|
|
|
|
QKEY(EQUALS, '=', '+', 0, 0, 0);
|
|
|
|
QKEY(MINUS, '-', '_', 0, 0, 0);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
QKEY(QUOTE, '\'', '4', '{', 0, 0);
|
|
|
|
QKEY(COMMA, ',', '?', 0, 0, 0);
|
|
|
|
QKEY(SEMICOLON, ';', '.', 0, 0, 0);
|
|
|
|
QKEY(EQUALS, '=', '+', '}', 0, 0);
|
|
|
|
QKEY(MINUS, '-', '6', '|', 0, 0);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Other
|
|
|
|
|
|
|
|
QKEY(HASH, '#', 0, 0, 0, 0);
|
|
|
|
QKEY(PERCENT, '%', 0, 0, 0, 0);
|
|
|
|
QKEY(PLUS, '+', 0, 0, 0, 0);
|
|
|
|
QKEY(GREATER, '>', 0, 0, 0, 0);
|
|
|
|
QKEY(QUESTION, '?', 0, 0, 0, 0);
|
|
|
|
QKEY(AT, '@', 0, 0, 0, 0);
|
|
|
|
|
|
|
|
// Found experimentally
|
|
|
|
_QKEY(231, 0, '0', '@', 0, 0);
|
|
|
|
_QKEY(232, 0, '7', '`', 0, 0);
|
|
|
|
_QKEY(233, 0, '2', '~', 0, 0);
|
|
|
|
_QKEY(249, 0, '%', 0, 0, 0);
|
|
|
|
|
2019-06-21 19:38:31 +02:00
|
|
|
default:
|
2019-07-07 15:36:47 +02:00
|
|
|
trace("??%d!!\n", key);
|
2019-06-21 19:38:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-07 15:36:47 +02:00
|
|
|
if (code != 0)
|
|
|
|
console_addkey(ctx, code);
|
2019-06-21 19:38:31 +02:00
|
|
|
}
|
|
|
|
|