diff options
| author | Albert Cervin <albert@acervin.com> | 2022-11-02 22:20:04 +0100 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2022-11-16 23:33:49 +0100 |
| commit | 2f4cb88d5c60f725323739300bb49dfa8923e7d5 (patch) | |
| tree | 6ec22c2be92eff05f18e5919e747faab56e555ad /src/keyboard.c | |
| download | dged-2f4cb88d5c60f725323739300bb49dfa8923e7d5.tar.gz dged-2f4cb88d5c60f725323739300bb49dfa8923e7d5.tar.xz dged-2f4cb88d5c60f725323739300bb49dfa8923e7d5.zip | |
🎉 And so it begins
Diffstat (limited to 'src/keyboard.c')
| -rw-r--r-- | src/keyboard.c | 56 |
1 files changed, 56 insertions, 0 deletions
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 <string.h> +#include <unistd.h> + +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; +} |
