]>
git.scottworley.com Git - keystroke-timestamps/blob - keystroke-timestamps.c
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*";
17 struct option long_options
[] = {
18 {"device", required_argument
, 0, 'd' },
19 {"output", required_argument
, 0, 'o' },
20 {"usec", no_argument
, &print_usec
, 1 },
24 int c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
34 if (c
!= 0) exit(EX_USAGE
);
37 /* Open output file */
42 output_file
= fopen(output
, "a");
43 if (output_file
== NULL
) {
44 err(EX_CANTCREAT
, "Could not open output file: %s", output
);
48 /* Open device inputs */
53 int glob_return
= glob(device
, GLOB_ERR
| GLOB_NOSORT
, NULL
, &glob_result
);
54 if (glob_return
== GLOB_NOMATCH
) {
55 errx(EX_NOINPUT
, "Could not find keyboard event file(s): %s", device
);
57 if (glob_return
!= 0) {
58 err(EX_NOINPUT
, "Could not glob keyboard event file(s): %s", device
);
60 for (unsigned i
= 0; i
< glob_result
.gl_pathc
; i
++) {
61 int device_fd
= open(glob_result
.gl_pathv
[i
], O_RDONLY
);
63 err(EX_NOINPUT
, "Could not open keyboard event file: %s", glob_result
.gl_pathv
[i
]);
65 if (device_fd
> maxfd
) {
68 FD_SET(device_fd
, &all_inputs
);
70 globfree(&glob_result
);
72 /* Report keystroke timestamps */
75 fd_set ready_inputs
= all_inputs
;
76 if (select(maxfd
+1, &ready_inputs
, NULL
, NULL
, NULL
) != 1) {
77 err(EX_IOERR
, "select");
79 for (int fd
=0; fd
<= maxfd
; fd
++) {
80 if (FD_ISSET(fd
, &ready_inputs
)) {
81 int read_return
= read(fd
, &i
, sizeof(i
));
82 if (read_return
== -1) {
83 err(EX_IOERR
, "read");
85 if (read_return
!= sizeof(i
)) {
86 errx(EX_IOERR
, "Unexpected EOF");
88 if (i
.type
== 1 && i
.value
== 1) {
90 fprintf(output_file
, "%ld.%06ld\n", i
.time
.tv_sec
, i
.time
.tv_usec
);
92 fprintf(output_file
, "%ld\n", i
.time
.tv_sec
);