diff --git a/tools/atrf-path/gui.c b/tools/atrf-path/gui.c index 8c4e83b..98a0a09 100644 --- a/tools/atrf-path/gui.c +++ b/tools/atrf-path/gui.c @@ -15,9 +15,6 @@ #include #include #include -#include -#include -#include #include "SDL.h" #include "SDL_gfxPrimitives.h" @@ -25,6 +22,7 @@ #include "at86rf230.h" #include "atrf.h" #include "misctxrx.h" +#include "getkey.h" #include "sweep.h" #include "gui.h" @@ -216,61 +214,6 @@ 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 @@ -322,7 +265,7 @@ int gui(const struct sweep *sweep, int sweeps) } atexit(SDL_Quit); - raw(); + get_key_init(); surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE); if (!surf) { diff --git a/tools/include/getkey.h b/tools/include/getkey.h new file mode 100644 index 0000000..069c87b --- /dev/null +++ b/tools/include/getkey.h @@ -0,0 +1,19 @@ +/* + * include/getkey.h - Get single characters from standard input + * + * Written 2011 by Werner Almesberger + * Copyright 2011 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef GETKEY_H +#define GETKEY_H + +void get_key_init(void); +char get_key(void); + +#endif /* !GETKEY_H */ diff --git a/tools/lib/Makefile b/tools/lib/Makefile index 1433067..14f6719 100644 --- a/tools/lib/Makefile +++ b/tools/lib/Makefile @@ -19,7 +19,7 @@ OBJS_host = atusb.o atusb-spi.o atusb-common.o usbopen.o OBJS_ben_jlime = atben.o OBJS_ben_openwrt = atben.o -OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o timeout.o \ +OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o timeout.o getkey.o \ $(OBJS_$(TARGET)) .PHONY: all clean spotless diff --git a/tools/lib/getkey.c b/tools/lib/getkey.c new file mode 100644 index 0000000..17d9284 --- /dev/null +++ b/tools/lib/getkey.c @@ -0,0 +1,78 @@ +/* + * lib/getkey.c - Get single characters from standard input + * + * Written 2011 by Werner Almesberger + * Copyright 2011 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include +#include +#include +#include + + + +static struct termios old_term; + + +static void restore_term(void) +{ + if (tcsetattr(0, TCSAFLUSH, &old_term) < 0) + perror("tcsetattr"); +} + + +void get_key_init(void) +{ + static int initialized = 0; + struct termios term; + + if (initialized) + return; + initialized = 1; + + 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); + } +} + + +char get_key(void) +{ + ssize_t got; + char ch; + + get_key_init(); + 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); +}