]>
git.scottworley.com Git - keystroke-timestamps/blob - keystroke-timestamps.c
5 #include <linux/input.h>
10 #include <sys/select.h>
13 int main (int argc
, char **argv
)
16 char* device
= strdup("/dev/input/by-path/*kbd*");
17 struct option long_options
[] = {
18 {"device", required_argument
, 0, 'd' },
19 {"usec", no_argument
, &print_usec
, 1 },
23 int c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
27 device
= strdup(optarg
);
30 if (c
!= 0) exit(EX_USAGE
);
37 int glob_return
= glob(device
, GLOB_ERR
| GLOB_NOSORT
, NULL
, &glob_result
);
38 if (glob_return
== GLOB_NOMATCH
) {
39 errx(EX_NOINPUT
, "Could not find keyboard event file(s): %s", device
);
41 if (glob_return
!= 0) {
42 err(EX_NOINPUT
, "Could not glob keyboard event file(s): %s", device
);
45 for (unsigned i
= 0; i
< glob_result
.gl_pathc
; i
++) {
46 int open_fd
= open(glob_result
.gl_pathv
[i
], O_RDONLY
);
48 err(EX_NOINPUT
, "Could not open keyboard event file: %s", glob_result
.gl_pathv
[i
]);
50 if (open_fd
> maxfd
) {
53 FD_SET(open_fd
, &all_inputs
);
55 globfree(&glob_result
);
59 fd_set ready_inputs
= all_inputs
;
60 if (select(maxfd
+1, &ready_inputs
, NULL
, NULL
, NULL
) != 1) {
61 err(EX_IOERR
, "select");
63 for (int fd
=0; fd
<= maxfd
; fd
++) {
64 if (FD_ISSET(fd
, &ready_inputs
)) {
65 int read_return
= read(fd
, &i
, sizeof(i
));
66 if (read_return
== -1) {
67 err(EX_IOERR
, "read");
69 if (read_return
!= sizeof(i
)) {
70 errx(EX_IOERR
, "Unexpected EOF");
72 if (i
.type
== 1 && i
.value
== 1) {
74 printf("%ld.%06ld\n", i
.time
.tv_sec
, i
.time
.tv_usec
);
76 printf("%ld\n", i
.time
.tv_sec
);