]>
git.scottworley.com Git - keystroke-timestamps/blob - keystroke-timestamps.c
97936c6fca37741d8774271b0c25e032e562d2bc
5 #include <linux/input.h>
9 #include <sys/select.h>
12 // This constant is defined in include/uapi/linux/input-event-codes.h
18 int main (int argc
, char **argv
)
21 char* device
= "/dev/input/by-path/*kbd*";
23 struct option long_options
[] = {
24 {"device", required_argument
, 0, 'd' },
25 {"output", required_argument
, 0, 'o' },
26 {"usec", no_argument
, &print_usec
, 1 },
30 int c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
40 if (c
!= 0) exit(EX_USAGE
);
43 /* Open output file */
48 output_file
= fopen(output
, "a");
49 if (output_file
== NULL
) {
50 err(EX_CANTCREAT
, "Could not open output file: %s", output
);
54 /* Open device inputs */
59 int glob_return
= glob(device
, GLOB_ERR
| GLOB_NOSORT
, NULL
, &glob_result
);
60 if (glob_return
== GLOB_NOMATCH
) {
61 errx(EX_NOINPUT
, "Could not find keyboard event file(s): %s", device
);
63 if (glob_return
!= 0) {
64 err(EX_NOINPUT
, "Could not glob keyboard event file(s): %s", device
);
66 for (unsigned i
= 0; i
< glob_result
.gl_pathc
; i
++) {
67 int device_fd
= open(glob_result
.gl_pathv
[i
], O_RDONLY
);
69 err(EX_NOINPUT
, "Could not open keyboard event file: %s", glob_result
.gl_pathv
[i
]);
71 if (device_fd
> maxfd
) {
74 FD_SET(device_fd
, &all_inputs
);
76 globfree(&glob_result
);
78 /* Report keystroke timestamps */
81 fd_set ready_inputs
= all_inputs
;
82 if (select(maxfd
+1, &ready_inputs
, NULL
, NULL
, NULL
) != 1) {
83 err(EX_IOERR
, "select");
85 for (int fd
=0; fd
<= maxfd
; fd
++) {
86 if (FD_ISSET(fd
, &ready_inputs
)) {
87 int read_return
= read(fd
, &i
, sizeof(i
));
88 if (read_return
== -1) {
89 err(EX_IOERR
, "read");
91 if (read_return
!= sizeof(i
)) {
92 errx(EX_IOERR
, "Unexpected EOF");
94 if (i
.type
== EV_KEY
&& i
.value
== 1) {
96 fprintf(output_file
, "%ld.%06ld\n", i
.time
.tv_sec
, i
.time
.tv_usec
);
98 fprintf(output_file
, "%ld\n", i
.time
.tv_sec
);