Date:2011-07-05 01:11:16 (8 years 2 months ago)
Author:Werner Almesberger
Commit:56f8b2d03816d92958bcd473d8b1afd1bb6516b7
Message:tools/: moved get_key from atrf-path/gui.c to libatrf, for sharing

The old "raw" function becomes get_key_init. Calling get_key_init is
only necessary if one wants to control the moment standard input is
switched to raw mode. If get_key is invoked without a prior call to
get_key_init, it will initialize automatically.

- atrf-path/gui.c (raw, main): renamed "raw" to get_key_init
- atrf-path/gui.c (old_term, restore_term, get_key_init, get_key):
moved to include/getkey.h and lib/getkey.c
- lib/getkey.c (get_key_init, get_key): made calling get_key_init
optional
- lib/Makefile (OBJS): added getkey.o
Files: tools/atrf-path/gui.c (4 diffs)
tools/include/getkey.h (1 diff)
tools/lib/Makefile (1 diff)
tools/lib/getkey.c (1 diff)

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>
2118
2219#include "SDL.h"
2320#include "SDL_gfxPrimitives.h"
...... 
2522#include "at86rf230.h"
2623#include "atrf.h"
2724#include "misctxrx.h"
25#include "getkey.h"
2826
2927#include "sweep.h"
3028#include "gui.h"
...... 
216214}
217215
218216
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
274217/* --- temporarily, for optimizing --- */
275218
276219#if 0
...... 
322265    }
323266    atexit(SDL_Quit);
324267
325    raw();
268    get_key_init();
326269
327270    surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE);
328271    if (!surf) {
tools/include/getkey.h
1/*
2 * include/getkey.h - Get single characters from standard input
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef GETKEY_H
14#define GETKEY_H
15
16void get_key_init(void);
17char get_key(void);
18
19#endif /* !GETKEY_H */
tools/lib/Makefile
1919OBJS_ben_jlime = atben.o
2020OBJS_ben_openwrt = atben.o
2121
22OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o timeout.o \
22OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o timeout.o getkey.o \
2323       $(OBJS_$(TARGET))
2424
2525.PHONY: all clean spotless
tools/lib/getkey.c
1/*
2 * lib/getkey.c - Get single characters from standard input
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <fcntl.h>
18#include <termios.h>
19#include <errno.h>
20
21
22
23static struct termios old_term;
24
25
26static void restore_term(void)
27{
28    if (tcsetattr(0, TCSAFLUSH, &old_term) < 0)
29        perror("tcsetattr");
30}
31
32
33void get_key_init(void)
34{
35    static int initialized = 0;
36    struct termios term;
37
38    if (initialized)
39        return;
40    initialized = 1;
41
42    if (tcgetattr(0, &old_term) < 0) {
43        perror("tcgetattr");
44        exit(1);
45    }
46    term = old_term;
47    cfmakeraw(&term);
48    if (tcsetattr(0, TCSAFLUSH, &term) < 0) {
49        perror("tcsetattr");
50        exit(1);
51    }
52    atexit(restore_term);
53    if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) {
54        perror("fcntl");
55        exit(1);
56    }
57}
58
59
60char get_key(void)
61{
62    ssize_t got;
63        char ch;
64
65    get_key_init();
66    got = read(0, &ch, 1);
67    if (got == 1)
68        return ch;
69    if (got >= 0) {
70        fprintf(stderr, "unexpected read() return value %d\n",
71            (int) got);
72        exit(1);
73    }
74    if (errno == EAGAIN)
75        return 0;
76    perror("read");
77    exit(1);
78}

Archive Download the corresponding diff file



interactive