// 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 #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; } #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; void console_handle_input(ctx_t *ctx, SDL_Keycode key) { keycode_t code = 0; SDL_Keymod mod = SDL_GetModState(); if (mod & KMOD_CTRL) { if (key == SDLK_c) _except(ctx, E_BRK, "Ctrl+C received"); return; } switch (key) { case SDLK_SPACE: code = ' '; break; case SDLK_BACKSPACE: code = '\b'; break; case SDLK_RETURN: case SDLK_RETURN2: code = '\n'; break; case SDLK_TAB: code = '\t'; break; // COMMON QKEY(a, 'a', 'A', 0, 0, 0); QKEY(b, 'b', 'B', 0, 0, 0); QKEY(c, 'c', 'C', 0, 0, 0); QKEY(d, 'd', 'D', 0, 0, 0); QKEY(e, 'e', 'E', 0, 0, 0); QKEY(f, 'f', 'F', 0, 0, 0); QKEY(g, 'g', 'G', 0, 0, 0); QKEY(h, 'h', 'H', 0, 0, 0); QKEY(i, 'i', 'I', 0, 0, 0); QKEY(j, 'j', 'J', 0, 0, 0); QKEY(k, 'k', 'K', 0, 0, 0); QKEY(l, 'l', 'L', 0, 0, 0); QKEY(m, 'm', 'M', 0, 0, 0); QKEY(n, 'n', 'N', 0, 0, 0); QKEY(o, 'o', 'O', 0, 0, 0); QKEY(p, 'p', 'P', 0, 0, 0); QKEY(q, 'q', 'Q', 0, 0, 0); QKEY(r, 'r', 'R', 0, 0, 0); QKEY(s, 's', 'S', 0, 0, 0); QKEY(t, 't', 'T', 0, 0, 0); QKEY(u, 'u', 'U', 0, 0, 0); QKEY(v, 'v', 'V', 0, 0, 0); QKEY(w, 'w', 'W', 0, 0, 0); QKEY(x, 'x', 'X', 0, 0, 0); QKEY(y, 'y', 'Y', 0, 0, 0); QKEY(z, 'z', 'Z', 0, 0, 0); // 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(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); 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); #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); QKEY(1, '&', '1', 0, 0, 0); QKEY(2, 0, '2', '~', 0, 0); QKEY(3, '"', '3', '#', 0, 0); QKEY(4, '\'','4', '{', 0, 0); QKEY(5, '(', '5', '[', 0, 0); QKEY(6, '-', '6', '|', 0, 0); QKEY(7, 0, '7', '`', 0, 0); QKEY(8, '_', '8', '\\', 0, 0); QKEY(9, 0, '9', '^', 0, 0); QKEY(0, 0, '0', '@', 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); default: return; } if (code != 0) console_addkey(ctx, code); }