2012-10-09 09:33:46 +04:00
|
|
|
/*
|
|
|
|
* General-purpose keyboard conversion library.
|
|
|
|
*
|
|
|
|
* This provides similar functionality to xkb:
|
|
|
|
* - It provides mappings for keyboards from locales
|
|
|
|
* - It translates incoming key presses to key names
|
|
|
|
* - It translates incoming keys to escape sequences
|
|
|
|
*/
|
2012-10-15 06:53:16 +04:00
|
|
|
|
2012-10-09 09:33:46 +04:00
|
|
|
#include "kbd.h"
|
|
|
|
|
2012-12-10 07:57:04 +04:00
|
|
|
#define DEBUG_SCANCODES 0
|
|
|
|
|
2012-10-15 06:53:16 +04:00
|
|
|
#define KEY_UP_MASK 0x80
|
|
|
|
#define KEY_CODE_MASK 0x7F
|
|
|
|
#define KEY_CTRL_MASK 0x40
|
|
|
|
|
|
|
|
#define norm 0x01
|
|
|
|
#define spec 0x02
|
|
|
|
#define func 0x03
|
|
|
|
|
2013-06-19 10:42:53 +04:00
|
|
|
#define SET_UNSET(a,b,c) (a) = (c) ? ((a) | (b)) : ((a) & ~(b))
|
|
|
|
|
2012-10-15 06:53:16 +04:00
|
|
|
char key_method[] = {
|
|
|
|
/* 00 */ 0, spec, norm, norm, norm, norm, norm, norm,
|
|
|
|
/* 08 */ norm, norm, norm, norm, norm, norm, norm, norm,
|
|
|
|
/* 10 */ norm, norm, norm, norm, norm, norm, norm, norm,
|
|
|
|
/* 18 */ norm, norm, norm, norm, norm, spec, norm, norm,
|
|
|
|
/* 20 */ norm, norm, norm, norm, norm, norm, norm, norm,
|
|
|
|
/* 28 */ norm, norm, spec, norm, norm, norm, norm, norm,
|
|
|
|
/* 30 */ norm, norm, norm, norm, norm, norm, spec, norm,
|
|
|
|
/* 38 */ spec, norm, spec, func, func, func, func, func,
|
|
|
|
/* 40 */ func, func, func, func, func, spec, spec, spec,
|
|
|
|
/* 48 */ spec, spec, spec, spec, spec, spec, spec, spec,
|
|
|
|
/* 50 */ spec, spec, spec, spec, spec, spec, spec, func,
|
|
|
|
/* 58 */ func, spec, spec, spec, spec, spec, spec, spec,
|
|
|
|
/* 60 */ spec, spec, spec, spec, spec, spec, spec, spec,
|
|
|
|
/* 68 */ spec, spec, spec, spec, spec, spec, spec, spec,
|
|
|
|
/* 70 */ spec, spec, spec, spec, spec, spec, spec, spec,
|
|
|
|
/* 78 */ spec, spec, spec, spec, spec, spec, spec, spec,
|
|
|
|
};
|
|
|
|
|
|
|
|
char kbd_us[128] = {
|
|
|
|
0, 27,
|
|
|
|
'1','2','3','4','5','6','7','8','9','0',
|
|
|
|
'-','=','\b',
|
|
|
|
'\t', /* tab */
|
|
|
|
'q','w','e','r','t','y','u','i','o','p','[',']','\n',
|
|
|
|
0, /* control */
|
|
|
|
'a','s','d','f','g','h','j','k','l',';','\'', '`',
|
|
|
|
0, /* left shift */
|
|
|
|
'\\','z','x','c','v','b','n','m',',','.','/',
|
|
|
|
0, /* right shift */
|
|
|
|
'*',
|
|
|
|
0, /* alt */
|
|
|
|
' ', /* space */
|
|
|
|
0, /* caps lock */
|
|
|
|
0, /* F1 [59] */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, /* ... F10 */
|
|
|
|
0, /* 69 num lock */
|
|
|
|
0, /* scroll lock */
|
|
|
|
0, /* home */
|
|
|
|
0, /* up */
|
|
|
|
0, /* page up */
|
|
|
|
'-',
|
|
|
|
0, /* left arrow */
|
|
|
|
0,
|
|
|
|
0, /* right arrow */
|
|
|
|
'+',
|
|
|
|
0, /* 79 end */
|
|
|
|
0, /* down */
|
|
|
|
0, /* page down */
|
|
|
|
0, /* insert */
|
|
|
|
0, /* delete */
|
|
|
|
0, 0, 0,
|
|
|
|
0, /* F11 */
|
|
|
|
0, /* F12 */
|
|
|
|
0, /* everything else */
|
|
|
|
};
|
|
|
|
|
|
|
|
char kbd_us_l2[128] = {
|
|
|
|
0, 27,
|
|
|
|
'!','@','#','$','%','^','&','*','(',')',
|
|
|
|
'_','+','\b',
|
|
|
|
'\t', /* tab */
|
|
|
|
'Q','W','E','R','T','Y','U','I','O','P','{','}','\n',
|
|
|
|
0, /* control */
|
|
|
|
'A','S','D','F','G','H','J','K','L',':','"', '~',
|
|
|
|
0, /* left shift */
|
|
|
|
'|','Z','X','C','V','B','N','M','<','>','?',
|
|
|
|
0, /* right shift */
|
|
|
|
'*',
|
|
|
|
0, /* alt */
|
|
|
|
' ', /* space */
|
|
|
|
0, /* caps lock */
|
|
|
|
0, /* F1 [59] */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, /* ... F10 */
|
|
|
|
0, /* 69 num lock */
|
|
|
|
0, /* scroll lock */
|
|
|
|
0, /* home */
|
|
|
|
0, /* up */
|
|
|
|
0, /* page up */
|
|
|
|
'-',
|
|
|
|
0, /* left arrow */
|
|
|
|
0,
|
|
|
|
0, /* right arrow */
|
|
|
|
'+',
|
|
|
|
0, /* 79 end */
|
|
|
|
0, /* down */
|
|
|
|
0, /* page down */
|
|
|
|
0, /* insert */
|
|
|
|
0, /* delete */
|
|
|
|
0, 0, 0,
|
|
|
|
0, /* F11 */
|
|
|
|
0, /* F12 */
|
|
|
|
0, /* everything else */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts from incoming terminal keys to kbd_keys
|
|
|
|
*/
|
2014-04-15 08:03:23 +04:00
|
|
|
kbd_key_t kbd_key(key_event_state_t * state, unsigned char c) {
|
|
|
|
switch (state->kbd_state) {
|
2012-10-09 09:33:46 +04:00
|
|
|
case KBD_NORMAL:
|
|
|
|
switch (c) {
|
|
|
|
case 0x1b:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_state = KBD_ESC_A;
|
2012-10-09 09:33:46 +04:00
|
|
|
return KEY_NONE;
|
|
|
|
default:
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
case KBD_ESC_A:
|
|
|
|
switch (c) {
|
|
|
|
case 0x5b:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_state = KBD_ESC_B;
|
2012-10-09 09:33:46 +04:00
|
|
|
return KEY_NONE;
|
|
|
|
default:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_state = KBD_NORMAL;
|
2012-10-09 09:33:46 +04:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
case KBD_ESC_B:
|
|
|
|
switch (c) {
|
|
|
|
case 0x41:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_state = KBD_NORMAL;
|
2012-10-09 09:33:46 +04:00
|
|
|
return KEY_ARROW_UP;
|
|
|
|
case 0x42:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_state = KBD_NORMAL;
|
2012-10-09 09:33:46 +04:00
|
|
|
return KEY_ARROW_DOWN;
|
|
|
|
case 0x43:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_state = KBD_NORMAL;
|
2012-10-09 09:33:46 +04:00
|
|
|
return KEY_ARROW_RIGHT;
|
|
|
|
case 0x44:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_state = KBD_NORMAL;
|
2012-10-09 09:33:46 +04:00
|
|
|
return KEY_ARROW_LEFT;
|
|
|
|
default:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_state = KBD_NORMAL;
|
2012-10-09 09:33:46 +04:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return KEY_BAD_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return KEY_BAD_STATE;
|
|
|
|
}
|
|
|
|
|
2014-04-15 08:03:23 +04:00
|
|
|
int kbd_scancode(key_event_state_t * state, unsigned char c, key_event_t * event) {
|
2012-10-15 06:53:16 +04:00
|
|
|
/* Convert scancodes to a series of keys */
|
|
|
|
|
|
|
|
event->keycode = 0;
|
|
|
|
event->action = 0;
|
|
|
|
event->modifiers = 0;
|
|
|
|
event->key = 0;
|
|
|
|
|
2012-12-10 07:57:04 +04:00
|
|
|
#if DEBUG_SCANCODES
|
2014-04-15 08:03:23 +04:00
|
|
|
fprintf(stderr, "[%d] %d\n", state->kbd_s_state, (int)c);
|
2012-12-10 07:57:04 +04:00
|
|
|
#endif
|
|
|
|
|
2014-04-15 08:03:23 +04:00
|
|
|
event->modifiers |= state->kl_ctrl ? KEY_MOD_LEFT_CTRL : 0;
|
|
|
|
event->modifiers |= state->kl_shift ? KEY_MOD_LEFT_SHIFT : 0;
|
|
|
|
event->modifiers |= state->kl_alt ? KEY_MOD_LEFT_ALT : 0;
|
|
|
|
event->modifiers |= state->kl_super ? KEY_MOD_LEFT_SUPER : 0;
|
2012-12-10 07:57:04 +04:00
|
|
|
|
2014-04-15 08:03:23 +04:00
|
|
|
event->modifiers |= state->kr_ctrl ? KEY_MOD_RIGHT_CTRL : 0;
|
|
|
|
event->modifiers |= state->kr_shift ? KEY_MOD_RIGHT_SHIFT : 0;
|
|
|
|
event->modifiers |= state->kr_alt ? KEY_MOD_RIGHT_ALT : 0;
|
|
|
|
event->modifiers |= state->kr_super ? KEY_MOD_RIGHT_SUPER : 0;
|
2012-12-10 07:57:04 +04:00
|
|
|
|
2014-04-15 08:03:23 +04:00
|
|
|
if (!state->kbd_s_state) {
|
2012-10-15 06:53:16 +04:00
|
|
|
if (c == 0xE0) {
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_s_state = 1;
|
2012-10-15 06:53:16 +04:00
|
|
|
/* Literally nothing */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c & KEY_UP_MASK) {
|
|
|
|
c ^= KEY_UP_MASK;
|
|
|
|
event->action = KEY_ACTION_UP;
|
|
|
|
} else {
|
|
|
|
event->action = KEY_ACTION_DOWN;
|
|
|
|
}
|
|
|
|
int down = (event->action == KEY_ACTION_DOWN);
|
|
|
|
|
|
|
|
switch (key_method[c]) {
|
|
|
|
case norm:
|
|
|
|
{
|
2012-12-08 12:24:43 +04:00
|
|
|
event->keycode = kbd_us[c];
|
2014-04-15 08:03:23 +04:00
|
|
|
if (state->k_ctrl) {
|
2012-10-15 06:53:16 +04:00
|
|
|
int out = (int)(kbd_us_l2[c] - KEY_CTRL_MASK);
|
|
|
|
if (out < 0 || out > 0x1F) {
|
|
|
|
event->key = kbd_us[c];
|
|
|
|
} else {
|
|
|
|
event->key = out;
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-15 08:03:23 +04:00
|
|
|
event->key = state->k_shift ? kbd_us_l2[c] : kbd_us[c];
|
2012-10-15 06:53:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case spec:
|
|
|
|
switch (c) {
|
|
|
|
case 0x01:
|
|
|
|
event->key = '\033';
|
|
|
|
event->keycode = KEY_ESCAPE;
|
|
|
|
break;
|
|
|
|
case 0x1D:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->k_ctrl = down;
|
|
|
|
state->kl_ctrl = down;
|
2013-06-19 10:42:53 +04:00
|
|
|
SET_UNSET(event->modifiers, KEY_MOD_LEFT_CTRL, down);
|
2012-10-15 06:53:16 +04:00
|
|
|
break;
|
|
|
|
case 0x2A:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->k_shift = down;
|
|
|
|
state->kl_shift = down;
|
2013-06-19 10:42:53 +04:00
|
|
|
SET_UNSET(event->modifiers, KEY_MOD_LEFT_SHIFT, down);
|
2012-10-15 06:53:16 +04:00
|
|
|
break;
|
2013-02-02 10:39:57 +04:00
|
|
|
case 0x36:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->k_shift = down;
|
|
|
|
state->kr_shift = down;
|
2013-06-19 10:42:53 +04:00
|
|
|
SET_UNSET(event->modifiers, KEY_MOD_RIGHT_SHIFT, down);
|
2013-02-02 10:39:57 +04:00
|
|
|
break;
|
2012-10-15 06:53:16 +04:00
|
|
|
case 0x38:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->k_alt = down;
|
|
|
|
state->kl_alt = down;
|
2013-06-19 10:42:53 +04:00
|
|
|
SET_UNSET(event->modifiers, KEY_MOD_LEFT_ALT, down);
|
2012-10-15 06:53:16 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case func:
|
2012-10-15 09:38:43 +04:00
|
|
|
switch (c) {
|
|
|
|
case KEY_SCANCODE_F1:
|
|
|
|
event->keycode = KEY_F1;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F2:
|
|
|
|
event->keycode = KEY_F2;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F3:
|
|
|
|
event->keycode = KEY_F3;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F4:
|
|
|
|
event->keycode = KEY_F4;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F5:
|
|
|
|
event->keycode = KEY_F5;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F6:
|
|
|
|
event->keycode = KEY_F6;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F7:
|
|
|
|
event->keycode = KEY_F7;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F8:
|
|
|
|
event->keycode = KEY_F8;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F9:
|
|
|
|
event->keycode = KEY_F9;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F10:
|
|
|
|
event->keycode = KEY_F10;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F11:
|
|
|
|
event->keycode = KEY_F11;
|
|
|
|
break;
|
|
|
|
case KEY_SCANCODE_F12:
|
|
|
|
event->keycode = KEY_F12;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-15 06:53:16 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->key) {
|
|
|
|
return down;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-04-15 08:03:23 +04:00
|
|
|
} else if (state->kbd_s_state == 1) {
|
2012-10-15 06:53:16 +04:00
|
|
|
|
|
|
|
if (c & KEY_UP_MASK) {
|
|
|
|
c ^= KEY_UP_MASK;
|
|
|
|
event->action = KEY_ACTION_UP;
|
|
|
|
} else {
|
|
|
|
event->action = KEY_ACTION_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
int down = (event->action == KEY_ACTION_DOWN);
|
|
|
|
switch (c) {
|
|
|
|
case 0x5B:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->k_super = down;
|
|
|
|
state->kl_super = down;
|
2013-06-19 10:42:53 +04:00
|
|
|
SET_UNSET(event->modifiers, KEY_MOD_LEFT_SUPER, down);
|
2012-10-15 06:53:16 +04:00
|
|
|
break;
|
|
|
|
case 0x5C:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->k_super = down;
|
|
|
|
state->kr_super = down;
|
2013-06-19 10:42:53 +04:00
|
|
|
SET_UNSET(event->modifiers, KEY_MOD_RIGHT_SUPER, down);
|
2012-10-15 06:53:16 +04:00
|
|
|
break;
|
|
|
|
case 0x1D:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kr_ctrl = down;
|
|
|
|
state->k_ctrl = down;
|
2013-06-19 10:42:53 +04:00
|
|
|
SET_UNSET(event->modifiers, KEY_MOD_RIGHT_CTRL, down);
|
2012-10-15 06:53:16 +04:00
|
|
|
break;
|
|
|
|
case 0x38:
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kr_alt = down;
|
|
|
|
state->k_alt = down;
|
2013-06-19 10:42:53 +04:00
|
|
|
SET_UNSET(event->modifiers, KEY_MOD_RIGHT_ALT, down);
|
2012-10-15 06:53:16 +04:00
|
|
|
break;
|
|
|
|
case 0x48:
|
|
|
|
event->keycode = KEY_ARROW_UP;
|
|
|
|
break;
|
|
|
|
case 0x4D:
|
|
|
|
event->keycode = KEY_ARROW_RIGHT;
|
|
|
|
break;
|
|
|
|
case 0x50:
|
|
|
|
event->keycode = KEY_ARROW_DOWN;
|
|
|
|
break;
|
|
|
|
case 0x4B:
|
|
|
|
event->keycode = KEY_ARROW_LEFT;
|
|
|
|
break;
|
2012-12-10 07:57:04 +04:00
|
|
|
case 0x49:
|
|
|
|
event->keycode = KEY_PAGE_UP;
|
|
|
|
break;
|
|
|
|
case 0x51:
|
|
|
|
event->keycode = KEY_PAGE_DOWN;
|
|
|
|
break;
|
2012-10-15 06:53:16 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-15 08:03:23 +04:00
|
|
|
state->kbd_s_state = 0;
|
2012-10-15 06:53:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|