]> git.scottworley.com Git - keystroke-timestamps/blob - keystroke-timestamps.c
Specify keyboard device as an option
[keystroke-timestamps] / keystroke-timestamps.c
1 #include <err.h>
2 #include <fcntl.h>
3 #include <getopt.h>
4 #include <linux/input.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sysexits.h>
9 #include <unistd.h>
10
11 int main (int argc, char **argv)
12 {
13 int print_usec = 0;
14 char* device = strdup("/dev/input/by-path/platform-i8042-serio-0-event-kbd");
15 struct option long_options[] = {
16 {"device", required_argument, 0, 'd' },
17 {"usec", no_argument, &print_usec, 1 },
18 {0, 0, 0, 0 },
19 };
20 while (1) {
21 int c = getopt_long(argc, argv, "", long_options, NULL);
22 if (c == -1) break;
23 if (c == 'd') {
24 free(device);
25 device = strdup(optarg);
26 continue;
27 }
28 if (c != 0) exit(EX_USAGE);
29 }
30
31 int fd = open(device, O_RDONLY);
32 if (fd < 0) {
33 err(EX_NOINPUT, "Could not open keyboard event file");
34 }
35 free(device);
36 struct input_event i;
37 while (read(fd, &i, sizeof(i)) == sizeof(i)) {
38 if (i.type == 1 && i.value == 1) {
39 if (print_usec) {
40 printf("%ld.%ld\n", i.time.tv_sec, i.time.tv_usec);
41 } else {
42 printf("%ld\n", i.time.tv_sec);
43 }
44 fflush(stdout);
45 }
46 }
47 return 0;
48 }