aboutsummaryrefslogtreecommitdiffstats
path: root/kbdtest.c
blob: 849e5e2ba708fafb29d8249d949f4f1385e123dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <termios.h>

#include "kbd.h"

/* ---------------------------------------------------------------------- */

static const char *keyname[KEY_CNT] = {
#include "KEY.h"
};

int main(int argc, char *argv[])
{
    struct timeval limit;
    uint32_t code, mod;
    char key[32];
    fd_set set;
    int rc,i;

    tty_raw();

    for (;;) {
        FD_ZERO(&set);
        FD_SET(0, &set);
        limit.tv_sec = 10;
        limit.tv_usec = 0;
	rc = select(1, &set, NULL, NULL, &limit);
	if (0 == rc || !FD_ISSET(0,&set))
            break;

        memset(key, 0, sizeof(key));
        rc = read(0, key, sizeof(key)-1);
        if (rc < 1) {
            /* EOF */
            break;
        }

        code = kbd_parse(key, &mod);
        fprintf(stderr, "key: \"");
        for (i = 0; key[i] != 0; i++) {
            fprintf(stderr, "%c", isprint(key[i]) ? key[i] : '.');
        }
        fprintf(stderr, "\" -> %s", keyname[code]);
        if (mod & KEY_MOD_SHIFT)
            fprintf(stderr, " +shift");
        if (mod & KEY_MOD_CTRL)
            fprintf(stderr, " +ctrl");
        fprintf(stderr, "\n");
    }
    tty_restore();
    return 0;
}