]>
git.scottworley.com Git - keystroke-timestamps/blob - keystroke-timestamps.c
037919f01d035b1d5b8f3d8756a0ec2bd11acdc8
5 #include <linux/input.h>
9 #include <sys/select.h>
12 int main (int argc
, char **argv
)
15 char* device
= "/dev/input/by-path/*kbd*";
16 struct option long_options
[] = {
17 {"device", required_argument
, 0, 'd' },
18 {"usec", no_argument
, &print_usec
, 1 },
22 int c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
28 if (c
!= 0) exit(EX_USAGE
);
35 int glob_return
= glob(device
, GLOB_ERR
| GLOB_NOSORT
, NULL
, &glob_result
);
36 if (glob_return
== GLOB_NOMATCH
) {
37 errx(EX_NOINPUT
, "Could not find keyboard event file(s): %s", device
);
39 if (glob_return
!= 0) {
40 err(EX_NOINPUT
, "Could not glob keyboard event file(s): %s", device
);
42 for (unsigned i
= 0; i
< glob_result
.gl_pathc
; i
++) {
43 int open_fd
= open(glob_result
.gl_pathv
[i
], O_RDONLY
);
45 err(EX_NOINPUT
, "Could not open keyboard event file: %s", glob_result
.gl_pathv
[i
]);
47 if (open_fd
> maxfd
) {
50 FD_SET(open_fd
, &all_inputs
);
52 globfree(&glob_result
);
56 fd_set ready_inputs
= all_inputs
;
57 if (select(maxfd
+1, &ready_inputs
, NULL
, NULL
, NULL
) != 1) {
58 err(EX_IOERR
, "select");
60 for (int fd
=0; fd
<= maxfd
; fd
++) {
61 if (FD_ISSET(fd
, &ready_inputs
)) {
62 int read_return
= read(fd
, &i
, sizeof(i
));
63 if (read_return
== -1) {
64 err(EX_IOERR
, "read");
66 if (read_return
!= sizeof(i
)) {
67 errx(EX_IOERR
, "Unexpected EOF");
69 if (i
.type
== 1 && i
.value
== 1) {
71 printf("%ld.%06ld\n", i
.time
.tv_sec
, i
.time
.tv_usec
);
73 printf("%ld\n", i
.time
.tv_sec
);