1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-10-04 05:54:09 +03:00

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
This commit is contained in:
Werner Almesberger 2011-06-06 20:22:57 -03:00
parent 9a32c66faa
commit 6ca63fc6e7

View File

@ -15,6 +15,9 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#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();