1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/pc/keybd.c

85 lines
1.6 KiB
C
Raw Normal View History

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;
}
if (key <= '0' && key >= '9')
console_addkey(ctx, key);
switch (key)
{
case SDLK_SPACE: code = ' '; break;
case SDLK_BACKSLASH: code = '\\'; break;
case SDLK_BACKSPACE: code = '\b'; break;
case SDLK_RETURN:
case SDLK_RETURN2:
code = '\n';
break;
default:
return;
}
console_addkey(ctx, code);
}