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 | |
parent | 03c8b71a085afb2a856fcf9a6e53c0b83cd9795c (diff) | |
download | fbida-1e49f9371f39475be7d2a3a9be2994ce5d27870b.tar.gz |
abstract away some kbd details
-rw-r--r-- | fbi.c | 45 | ||||
-rw-r--r-- | fbpdf.c | 8 | ||||
-rw-r--r-- | kbd.c | 74 | ||||
-rw-r--r-- | kbd.h | 8 | ||||
-rw-r--r-- | kbdtest.c | 21 |
5 files changed, 79 insertions, 77 deletions
@@ -779,10 +779,7 @@ svga_show(struct flist *f, struct flist *prev, int rc; char key[16]; uint32_t keycode, keymod; - fd_set set; - struct timeval limit; char linebuffer[80]; - int fdmax; *nr = 0; if (NULL == img) @@ -835,29 +832,17 @@ svga_show(struct flist *f, struct flist *prev, return -1; } - FD_ZERO(&set); - FD_SET(0, &set); - fdmax = 1; - limit.tv_sec = timeout; - limit.tv_usec = 0; - rc = select(fdmax, &set, NULL, NULL, - (0 != timeout && !paused) ? &limit : NULL); + rc = kbd_wait(timeout); if (check_console_switch()) { continue; } - if (0 == rc) + if (rc < 1) return -1; /* timeout */ - if (FD_ISSET(0,&set)) { - /* stdin, i.e. keyboard */ - rc = read(0, key, sizeof(key)-1); - if (rc < 1) { - /* EOF */ - return KEY_ESC; - } - key[rc] = 0; - } - keycode = kbd_parse(key, &keymod); + rc = kbd_read(key, sizeof(key), &keycode, &keymod); + if (rc < 0) + return KEY_ESC; /* EOF */ + switch (keycode) { case KEY_SPACE: if (textreading && f->top < (int)(img->i.height - gfx->vdisplay)) { @@ -1064,24 +1049,18 @@ static char edit_line(struct ida_image *img, char *line, int max) int rc; char key[16]; uint32_t keycode, keymod; - fd_set set; do { status_edit(line,pos); - FD_SET(0, &set); - rc = select(1, &set, NULL, NULL, NULL); + kbd_wait(0); if (check_console_switch()) { continue; } - rc = read(0, key, sizeof(key)-1); - if (rc < 1) { - /* EOF */ - return KEY_ESC; - } - key[rc] = 0; - keycode = kbd_parse(key, &keymod); + rc = kbd_read(key, sizeof(key), &keycode, &keymod); + if (rc < 0) + return KEY_ESC; /* EOF */ switch (keycode) { case KEY_ENTER: @@ -1324,7 +1303,7 @@ static void exit_signals_init(void) static void cleanup_and_exit(int code) { shadow_fini(); - tty_restore(); + kbd_fini(); gfx->cleanup_display(); console_switch_cleanup(); flist_print_tagged(stdout); @@ -1463,7 +1442,7 @@ int main(int argc, char *argv[]) shadow_init(gfx); /* svga main loop */ - tty_raw(); + kbd_init(); desc = NULL; info = NULL; for (;;) { @@ -340,7 +340,7 @@ int main(int argc, char *argv[]) gfx->stride); } - tty_raw(); + kbd_init(); index = 0; newpage = true; @@ -364,9 +364,7 @@ int main(int argc, char *argv[]) continue; } - memset(key, 0, sizeof(key)); - read(0, key, sizeof(key)-1); - keycode = kbd_parse(key, &keymod); + kbd_read(key, sizeof(key), &keycode, &keymod); switch (keycode) { case KEY_ESC: @@ -417,7 +415,7 @@ int main(int argc, char *argv[]) } } - tty_restore(); + kbd_fini(); cleanup_and_exit(0); return 0; } @@ -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; +} @@ -3,8 +3,8 @@ #define KEY_MOD_SHIFT (1 << 0) #define KEY_MOD_CTRL (1 << 1) -uint32_t kbd_parse(const char *key, uint32_t *mod); +void kbd_init(void); int kbd_wait(int timeout); - -void tty_raw(void); -void tty_restore(void); +int kbd_read(char *buf, uint32_t len, + uint32_t *keycode, uint32_t *modifier); +void kbd_fini(void); @@ -19,31 +19,21 @@ static const char *keyname[KEY_CNT] = { int main(int argc, char *argv[]) { - struct timeval limit; uint32_t code, mod; char key[32]; - fd_set set; int rc,i; - tty_raw(); + kbd_init(); for (;;) { - FD_ZERO(&set); - FD_SET(0, &set); - limit.tv_sec = 10; - limit.tv_usec = 0; - rc = select(1, &set, NULL, NULL, &limit); - if (0 == rc || !FD_ISSET(0,&set)) - break; + kbd_wait(10); - memset(key, 0, sizeof(key)); - rc = read(0, key, sizeof(key)-1); - if (rc < 1) { + rc = kbd_read(key, sizeof(key), &code, &mod); + if (rc < 0) { /* EOF */ break; } - code = kbd_parse(key, &mod); fprintf(stderr, "key: \""); for (i = 0; key[i] != 0; i++) { fprintf(stderr, "%c", isprint(key[i]) ? key[i] : '.'); @@ -55,6 +45,7 @@ int main(int argc, char *argv[]) fprintf(stderr, " +ctrl"); fprintf(stderr, "\n"); } - tty_restore(); + + kbd_fini(); return 0; } |