From 2f4cb88d5c60f725323739300bb49dfa8923e7d5 Mon Sep 17 00:00:00 2001 From: Albert Cervin Date: Wed, 2 Nov 2022 22:20:04 +0100 Subject: =?UTF-8?q?=F0=9F=8E=89=20And=20so=20it=20begins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/keyboard.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/keyboard.c (limited to 'src/keyboard.c') diff --git a/src/keyboard.c b/src/keyboard.c new file mode 100644 index 0000000..2cf9e8c --- /dev/null +++ b/src/keyboard.c @@ -0,0 +1,56 @@ +#include "keyboard.h" + +#include +#include + +struct keyboard keyboard_create() { + // TODO: should input term stuff be set here? + return (struct keyboard){}; +} + +void parse_keys(uint8_t *bytes, uint32_t nbytes, struct key *out_keys, + uint32_t *out_nkeys) { + uint32_t nkps = 0; + for (uint32_t bytei = 0; bytei < nbytes; ++bytei) { + uint8_t b = bytes[bytei]; + + struct key *kp = &out_keys[nkps]; + + if (b == 0x1b) { // meta + kp->mod |= Meta; + } else if (b >= 0x00 && b <= 0x1f) { // ctrl char + kp->mod |= Ctrl; + kp->c = b | 0x40; + + } else if (b == 0x7f) { // ^? + kp->mod |= Ctrl; + kp->c = '?'; + } else { // normal char (or part of char) + kp->c = b; + } + + ++nkps; + } + + *out_nkeys = nkps; +} + +struct keyboard_update keyboard_begin_frame(struct keyboard *kbd) { + uint8_t bytes[32] = {0}; + int nbytes = read(STDIN_FILENO, bytes, 32); + + struct keyboard_update upd = + (struct keyboard_update){.keys = {0}, .nkeys = 0}; + + if (nbytes > 0) { + parse_keys(bytes, nbytes, upd.keys, &upd.nkeys); + } + + return upd; +} + +void keyboard_end_frame(struct keyboard *kbd) {} + +bool key_equal(struct key *key, uint8_t mod, uint8_t c) { + return key->c == c && key->mod == mod; +} -- cgit v1.2.3