aboutsummaryrefslogtreecommitdiffstats
path: root/kbdtest.c
blob: 215a216e9050c90efcb48a0634c82b693e467ae9 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#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 struct {
    uint32_t code;
    const char *name;
} xkbname_list[] = {
#include "xkbname.h"
};

const char *xkbname(uint32_t code)
{
    int keycount = sizeof(xkbname_list)/sizeof(xkbname_list[0]);
    int i;

    for (i = 0; i < keycount; i++)
        if (xkbname_list[i].code == code)
            return xkbname_list[i].name;
    return "unknown";
}

static void usage(FILE *fp)
{
    fprintf(fp,
            "\n"
            "usage: kbdtest [ options ]\n"
            "\n"
            "options:\n"
            "  -h         print this text\n"
            "  -i         use libinput\n"
            "\n");
}

int main(int argc, char *argv[])
{
    uint32_t code, mod;
    char key[32];
    bool use_libinput = false;
    int rc, i, c;

    for (;;) {
        c = getopt(argc, argv, "hi");
        if (c == -1)
            break;
        switch (c) {
        case 'i':
            use_libinput = true;
            break;
        case 'h':
            usage(stdout);
            exit(0);
        default:
            usage(stderr);
            exit(1);
        }
    }

    kbd_init(use_libinput, 0);

    for (;;) {
        kbd_wait(10);

        rc = kbd_read(key, sizeof(key), &code, &mod);
        if (rc < 0) {
            /* EOF */
            break;
        }
        if (code == XKB_KEY_VoidSymbol)
            continue;

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

    kbd_fini();
    return 0;
}