Date:2011-06-07 01:22:57 (9 years 2 months ago)
Author:Werner Almesberger
Commit:6ca63fc6e7d0bac9166945da4e2baa3c73b2a7cc
Message: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
Files: tools/atrf-path/gui.c (4 diffs)

Change Details

tools/atrf-path/gui.c
1515#include <stdio.h>
1616#include <unistd.h>
1717#include <string.h>
18#include <termios.h>
19#include <fcntl.h>
20#include <errno.h>
1821
1922#include "SDL.h"
2023#include "SDL_gfxPrimitives.h"
...... 
213216}
214217
215218
219/* ----- Console input ----------------------------------------------------- */
220
221
222static struct termios old_term;
223
224
225static void restore_term(void)
226{
227    if (tcsetattr(0, TCSAFLUSH, &old_term) < 0)
228        perror("tcsetattr");
229}
230
231
232static void raw(void)
233{
234    struct termios term;
235
236    if (tcgetattr(0, &old_term) < 0) {
237        perror("tcgetattr");
238        exit(1);
239    }
240    term = old_term;
241    cfmakeraw(&term);
242    if (tcsetattr(0, TCSAFLUSH, &term) < 0) {
243        perror("tcsetattr");
244        exit(1);
245    }
246    atexit(restore_term);
247    if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) {
248        perror("fcntl");
249        exit(1);
250    }
251}
252
253
254static char get_key(void)
255{
256    ssize_t got;
257        char ch;
258
259    got = read(0, &ch, 1);
260    if (got == 1)
261        return ch;
262    if (got >= 0) {
263        fprintf(stderr, "unexpected read() return value %d\n",
264            (int) got);
265        exit(1);
266    }
267    if (errno == EAGAIN)
268        return 0;
269    perror("read");
270    exit(1);
271}
272
273
216274/* --- temporarily, for optimizing --- */
217275
218276#if 0
...... 
264322    }
265323    atexit(SDL_Quit);
266324
325    raw();
326
267327    surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE);
268328    if (!surf) {
269329        fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
...... 
314374                break;
315375            }
316376
377        switch (get_key()) {
378        case 'F':
379        case 'f':
380            if (cycle && fail < 0)
381                return -1;
382            break;
383        case 'P':
384        case 'p':
385            if (cycle && !fail)
386                return 1;
387            break;
388        case 'D':
389        case 'd':
390            if (cycle)
391                dump(sweep, res);
392            break;
393        case 'Q':
394        case 'q':
395        case 3: /* Ctrl-C */
396            return 0;
397        default:
398            break;
399        }
400
317401        tstart();
318402        fail = do_sweep(sweep, res);
319403        tstop();

Archive Download the corresponding diff file



interactive