Add rest of main keys and kbd controller status

This commit is contained in:
Bad Diode 2022-03-05 10:23:30 +01:00
parent e835289d29
commit 9005cb1eb9
1 changed files with 48 additions and 6 deletions

View File

@ -36,12 +36,15 @@ time_elapsed(Time since){
typedef struct Keyboard { typedef struct Keyboard {
int fd; int fd;
char map[KEY_MAX / 8 + 1]; char map[KEY_MAX / 8 + 1];
u8 controller;
} Keyboard; } Keyboard;
static Keyboard keyboard; static Keyboard keyboard;
void void
init_input(void) { init_input(void) {
memset(&keyboard, 0, sizeof(Keyboard));
const char *dev = "/dev/input/by-id/usb-Apple_Inc._Magic_Keyboard_with_Numeric_Keypad_F0T0167000DHTHKAD-if01-event-kbd"; const char *dev = "/dev/input/by-id/usb-Apple_Inc._Magic_Keyboard_with_Numeric_Keypad_F0T0167000DHTHKAD-if01-event-kbd";
keyboard.fd = open(dev, O_RDONLY); keyboard.fd = open(dev, O_RDONLY);
if (keyboard.fd == -1) { if (keyboard.fd == -1) {
@ -50,8 +53,6 @@ init_input(void) {
fprintf(stderr, "error: no couldn't open keyboard %s: %s.\n", dev, strerror(errno)); fprintf(stderr, "error: no couldn't open keyboard %s: %s.\n", dev, strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
memset(keyboard.map, 0, sizeof(keyboard.map));
} }
void void
@ -92,28 +93,38 @@ handle_keyboard(void) {
} }
// Handle normal keys. // Handle normal keys.
u8 controller_now = 0;
for (size_t i = 0; i < sizeof(keyboard.map); i++) { for (size_t i = 0; i < sizeof(keyboard.map); i++) {
for (size_t j = 0; j < 8; j++) { for (size_t j = 0; j < 8; j++) {
char key = keyboard.map[i] & (1 << j); char key = keyboard.map[i] & (1 << j);
if (key) { if (key) {
char key_code = i * 8 + j; char key_code = i * 8 + j;
// Special keys.
// TODO: ...
// Normal keys. // Normal keys.
char rune = '\0'; u8 rune = '\0';
switch (key_code) { switch (key_code) {
case KEY_KP1:
case KEY_1: { rune = shift_mod ? '!' : '1'; } break; case KEY_1: { rune = shift_mod ? '!' : '1'; } break;
case KEY_KP2:
case KEY_2: { rune = shift_mod ? '@' : '2'; } break; case KEY_2: { rune = shift_mod ? '@' : '2'; } break;
case KEY_KP3:
case KEY_3: { rune = shift_mod ? '#' : '3'; } break; case KEY_3: { rune = shift_mod ? '#' : '3'; } break;
case KEY_KP4:
case KEY_4: { rune = shift_mod ? '$' : '4'; } break; case KEY_4: { rune = shift_mod ? '$' : '4'; } break;
case KEY_KP5:
case KEY_5: { rune = shift_mod ? '%' : '5'; } break; case KEY_5: { rune = shift_mod ? '%' : '5'; } break;
case KEY_KP6:
case KEY_6: { rune = shift_mod ? '^' : '6'; } break; case KEY_6: { rune = shift_mod ? '^' : '6'; } break;
case KEY_KP7:
case KEY_7: { rune = shift_mod ? '&' : '7'; } break; case KEY_7: { rune = shift_mod ? '&' : '7'; } break;
case KEY_KP8:
case KEY_8: { rune = shift_mod ? '*' : '8'; } break; case KEY_8: { rune = shift_mod ? '*' : '8'; } break;
case KEY_KP9:
case KEY_9: { rune = shift_mod ? '(' : '9'; } break; case KEY_9: { rune = shift_mod ? '(' : '9'; } break;
case KEY_KP0:
case KEY_0: { rune = shift_mod ? ')' : '0'; } break; case KEY_0: { rune = shift_mod ? ')' : '0'; } break;
case KEY_KPMINUS:
case KEY_MINUS: { rune = shift_mod ? '_' : '-'; } break; case KEY_MINUS: { rune = shift_mod ? '_' : '-'; } break;
case KEY_KPEQUAL:
case KEY_EQUAL: { rune = shift_mod ? '+' : '+'; } break; case KEY_EQUAL: { rune = shift_mod ? '+' : '+'; } break;
case KEY_Q: { rune = shift_mod ? 'Q' : 'q'; } break; case KEY_Q: { rune = shift_mod ? 'Q' : 'q'; } break;
case KEY_W: { rune = shift_mod ? 'W' : 'w'; } break; case KEY_W: { rune = shift_mod ? 'W' : 'w'; } break;
@ -149,9 +160,14 @@ handle_keyboard(void) {
case KEY_M: { rune = shift_mod ? 'M' : 'm'; } break; case KEY_M: { rune = shift_mod ? 'M' : 'm'; } break;
case KEY_COMMA: { rune = shift_mod ? '<' : ','; } break; case KEY_COMMA: { rune = shift_mod ? '<' : ','; } break;
case KEY_DOT: { rune = shift_mod ? '>' : '.'; } break; case KEY_DOT: { rune = shift_mod ? '>' : '.'; } break;
case KEY_KPSLASH:
case KEY_SLASH: { rune = shift_mod ? '?' : '/'; } break; case KEY_SLASH: { rune = shift_mod ? '?' : '/'; } break;
case KEY_KPASTERISK: { rune = '*'; } break;
case KEY_KPPLUS: { rune = '+'; } break;
case KEY_KPCOMMA: { rune = '.'; } break;
case KEY_SPACE: { rune = ' '; } break; case KEY_SPACE: { rune = ' '; } break;
case KEY_TAB: { rune = '\t'; } break; case KEY_TAB: { rune = '\t'; } break;
case KEY_ESC: { rune = 0x1b; } break;
case KEY_BACKSPACE: { rune = 0x08; } break; case KEY_BACKSPACE: { rune = 0x08; } break;
case KEY_ENTER: case KEY_ENTER:
case KEY_KPENTER: { rune = 0x0d; } break; case KEY_KPENTER: { rune = 0x0d; } break;
@ -163,9 +179,35 @@ handle_keyboard(void) {
devctrl->dat[3] = 0; devctrl->dat[3] = 0;
continue; continue;
} }
// Special keys.
switch (key_code) {
case KEY_LEFTCTRL: { rune = 0x01; } break;
case KEY_LEFTALT: { rune = 0x02; } break;
case KEY_LEFTSHIFT: { rune = 0x04; } break;
case KEY_HOME: { rune = 0x08; } break;
case KEY_UP: { rune = 0x10; } break;
case KEY_DOWN: { rune = 0x20; } break;
case KEY_LEFT: { rune = 0x40; } break;
case KEY_RIGHT: { rune = 0x80; } break;
default: break;
}
if (rune) {
controller_now |= rune;
continue;
}
} }
} }
} }
if (controller_now != keyboard.controller) {
devctrl->dat[2] = controller_now;
uxn_eval(&u, mempeek16(devctrl->dat, 0));
keyboard.controller = controller_now;
}
// Reset keyboard state.
devctrl->dat[3] = 0;
memset(keyboard.map, 0, sizeof(keyboard.map)); memset(keyboard.map, 0, sizeof(keyboard.map));
} }