diff options
author | Gerd Hoffmann <kraxel@redhat.com> | 2019-01-18 10:23:41 +0100 |
---|---|---|
committer | Gerd Hoffmann <kraxel@redhat.com> | 2019-01-18 11:03:55 +0100 |
commit | 1e49f9371f39475be7d2a3a9be2994ce5d27870b (patch) | |
tree | b84ba5b35e846beddca3c78b6f01d12a6d5116a7 /kbd.c | |
parent | 03c8b71a085afb2a856fcf9a6e53c0b83cd9795c (diff) | |
download | fbida-1e49f9371f39475be7d2a3a9be2994ce5d27870b.tar.gz |
abstract away some kbd details
Diffstat (limited to 'kbd.c')
-rw-r--r-- | kbd.c | 74 |
1 files changed, 54 insertions, 20 deletions
@@ -135,7 +135,7 @@ static struct termctrl termctrl[] = { { /* EOF */ } }; -uint32_t kbd_parse(const char *key, uint32_t *mod) +static uint32_t tty_parse(const char *key, uint32_t *mod) { int i; @@ -149,27 +149,12 @@ uint32_t kbd_parse(const char *key, uint32_t *mod) return KEY_RESERVED; } -int kbd_wait(int timeout) -{ - struct timeval limit; - fd_set set; - int rc; - - FD_ZERO(&set); - FD_SET(STDIN_FILENO, &set); - limit.tv_sec = timeout; - limit.tv_usec = 0; - rc = select(STDIN_FILENO + 1, &set, NULL, NULL, - timeout ? &limit : NULL); - return rc; -} - /* ---------------------------------------------------------------------- */ -struct termios saved_attributes; -int saved_fl; +static struct termios saved_attributes; +static int saved_fl; -void tty_raw(void) +static void tty_raw(void) { struct termios tattr; @@ -184,8 +169,57 @@ void tty_raw(void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr); } -void tty_restore(void) +static void tty_restore(void) { fcntl(STDIN_FILENO, F_SETFL, saved_fl); tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes); } + +static int file_wait(int fd, int timeout) +{ + struct timeval limit; + fd_set set; + int rc; + + FD_ZERO(&set); + FD_SET(fd, &set); + limit.tv_sec = timeout; + limit.tv_usec = 0; + rc = select(fd + 1, &set, NULL, NULL, + timeout ? &limit : NULL); + return rc; +} + +/* ---------------------------------------------------------------------- */ + +void kbd_init(void) +{ + tty_raw(); +} + +void kbd_fini(void) +{ + tty_restore(); +} + +int kbd_wait(int timeout) +{ + return file_wait(STDIN_FILENO, timeout); +} + +int kbd_read(char *buf, uint32_t len, + uint32_t *keycode, uint32_t *modifier) +{ + int rc; + + memset(buf, 0, len); + *keycode = KEY_RESERVED; + *modifier = 0; + + rc = read(STDIN_FILENO, buf, len-1); + if (rc < 1) + return -1; + + *keycode = tty_parse(buf, modifier); + return rc; +} |