vxKernel/src/driver/mpu/x86/sdl2/keyboard.c

130 lines
3.0 KiB
C

#include <vhex/keyboard/interface.h>
#include <vhex/driver.h>
#include <vhex/keyboard.h>
#include <SDL2/SDL.h>
//---
// fake interrupt handler
//---
#define __SDL2_KEYCODE_SUPPORTED 16
static struct {
vkey_event_t keycache[__SDL2_KEYCODE_SUPPORTED];
struct {
vkey_event_t *list;
int nb_evt_max;
int idx;
} queue;
} keyinfo;
static struct {
int sdl2_id;
int vhex_id;
} key_translation[16] = {
{ .sdl2_id = 49, .vhex_id = VKEY_F1 }, // 1
{ .sdl2_id = 50, .vhex_id = VKEY_F2 }, // 2
{ .sdl2_id = 51, .vhex_id = VKEY_F3 }, // 3
{ .sdl2_id = 52, .vhex_id = VKEY_F4 }, // 4
{ .sdl2_id = 53, .vhex_id = VKEY_F5 }, // 5
{ .sdl2_id = 54, .vhex_id = VKEY_F6 }, // 6
{ .sdl2_id = 0x4000004f, .vhex_id = VKEY_RIGHT }, // right arrow
{ .sdl2_id = 0x40000050, .vhex_id = VKEY_LEFT }, // left arrow
{ .sdl2_id = 0x40000051, .vhex_id = VKEY_DOWN }, // down arrow
{ .sdl2_id = 0x40000052, .vhex_id = VKEY_UP }, // up arrow
{ .sdl2_id = 13, .vhex_id = VKEY_EXE }, // enter
{ .sdl2_id = 0x400000e0, .vhex_id = VKEY_ALPHA }, // shift
{ .sdl2_id = 0x400000e1, .vhex_id = VKEY_SHIFT }, // ctrl
{ .sdl2_id = 27, .vhex_id = VKEY_EXIT }, // echap
{ .sdl2_id = 8, .vhex_id = VKEY_DEL }, // backspace
{ .sdl2_id = 9, .vhex_id = VKEY_MENU }, // tab
};
//---
// keycache primitives
//---
static int sdl_keycache_event_pop(vkey_event_t *event)
{
SDL_Event evt;
int ret;
if (event == NULL)
return -1;
while (1)
{
ret = SDL_PollEvent(&evt);
if (ret == 0) {
event->key = VKEY_NONE;
return 0;
}
if (evt.type == SDL_QUIT)
exit(0);
if (evt.type != SDL_KEYDOWN && evt.type != SDL_KEYUP)
continue;
for (int i = 0; i < __SDL2_KEYCODE_SUPPORTED ; ++i) {
if (evt.key.keysym.sym != key_translation[i].sdl2_id)
continue;
event->time = 0;
event->type = (evt.type == SDL_KEYDOWN) ? VKEYEV_DOWN : VKEYEV_UP;
event->key = key_translation[i].vhex_id;
return 1;
}
}
}
//---
// Internal fake driver definition
//---
/* __keysc_configure() : configure the keyboard module */
static void __keysc_configure(void)
{
// Nothing to do, this is a fake driver
;
}
/* __keysc_hsave() : save hardware information */
static void __keysc_hsave(void)
{
// Nothing to do, this is a fake driver
;
}
/* __keysc_hrestore() : restore hadware information */
static void __keysc_hrestore(void)
{
// Nothing to do, this is a fake driver
;
}
struct vhex_driver drv_keysc = {
.name = "SDL2 Keyboard",
.hsave = (void*)&__keysc_hsave,
.hrestore = (void*)&__keysc_hrestore,
.configure = (void*)&__keysc_configure,
.state_size = 4,
.flags = {
.KEYBOARD = 1,
.SHARED = 0,
.UNUSED = 0,
},
.module_data = &(struct keyboard_drv_interface){
.keycache_init = NULL,
.keycache_keydown = NULL,
.keycache_event_wait = NULL,
.keycache_event_pop = &sdl_keycache_event_pop,
.keycache_event_push = NULL,
.keycache_quit = NULL,
}
};
VHEX_DECLARE_DRIVER(03, drv_keysc);