From 6ca63fc6e7d0bac9166945da4e2baa3c73b2a7cc Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Mon, 6 Jun 2011 20:22:57 -0300 Subject: [PATCH] atrf-path: accept keyboard input both from stdin and SDL - gui.c (old_term, restore_term, raw, get_key): raw and non-blocking console input - gui,c (gui): accept console input in addition to keypressed in the SDL window --- tools/atrf-path/gui.c | 84 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tools/atrf-path/gui.c b/tools/atrf-path/gui.c index ddb5e4b..8c4e83b 100644 --- a/tools/atrf-path/gui.c +++ b/tools/atrf-path/gui.c @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #include "SDL.h" #include "SDL_gfxPrimitives.h" @@ -213,6 +216,61 @@ static void draw_dumps(SDL_Surface *s, int cont_tx) } +/* ----- Console input ----------------------------------------------------- */ + + +static struct termios old_term; + + +static void restore_term(void) +{ + if (tcsetattr(0, TCSAFLUSH, &old_term) < 0) + perror("tcsetattr"); +} + + +static void raw(void) +{ + struct termios term; + + if (tcgetattr(0, &old_term) < 0) { + perror("tcgetattr"); + exit(1); + } + term = old_term; + cfmakeraw(&term); + if (tcsetattr(0, TCSAFLUSH, &term) < 0) { + perror("tcsetattr"); + exit(1); + } + atexit(restore_term); + if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) { + perror("fcntl"); + exit(1); + } +} + + +static char get_key(void) +{ + ssize_t got; + char ch; + + got = read(0, &ch, 1); + if (got == 1) + return ch; + if (got >= 0) { + fprintf(stderr, "unexpected read() return value %d\n", + (int) got); + exit(1); + } + if (errno == EAGAIN) + return 0; + perror("read"); + exit(1); +} + + /* --- temporarily, for optimizing --- */ #if 0 @@ -264,6 +322,8 @@ int gui(const struct sweep *sweep, int sweeps) } atexit(SDL_Quit); + raw(); + surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE); if (!surf) { fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); @@ -314,6 +374,30 @@ int gui(const struct sweep *sweep, int sweeps) break; } + switch (get_key()) { + case 'F': + case 'f': + if (cycle && fail < 0) + return -1; + break; + case 'P': + case 'p': + if (cycle && !fail) + return 1; + break; + case 'D': + case 'd': + if (cycle) + dump(sweep, res); + break; + case 'Q': + case 'q': + case 3: /* Ctrl-C */ + return 0; + default: + break; + } + tstart(); fail = do_sweep(sweep, res); tstop();