]>
git.scottworley.com Git - keystroke-timestamps/blob - keystroke-timestamps.c
5a27279660103f6503cfc8066be0096d24f1f235
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
);
41 output_file
= fopen(output
, "a");
42 if (output_file
== NULL
) {
43 err(EX_CANTCREAT
, "Could not open output file: %s", output
);
51 int glob_return
= glob(device
, GLOB_ERR
| GLOB_NOSORT
, NULL
, &glob_result
);
52 if (glob_return
== GLOB_NOMATCH
) {
53 errx(EX_NOINPUT
, "Could not find keyboard event file(s): %s", device
);
55 if (glob_return
!= 0) {
56 err(EX_NOINPUT
, "Could not glob keyboard event file(s): %s", device
);
58 for (unsigned i
= 0; i
< glob_result
.gl_pathc
; i
++) {
59 int device_fd
= open(glob_result
.gl_pathv
[i
], O_RDONLY
);
61 err(EX_NOINPUT
, "Could not open keyboard event file: %s", glob_result
.gl_pathv
[i
]);
63 if (device_fd
> maxfd
) {
66 FD_SET(device_fd
, &all_inputs
);
68 globfree(&glob_result
);
72 fd_set ready_inputs
= all_inputs
;
73 if (select(maxfd
+1, &ready_inputs
, NULL
, NULL
, NULL
) != 1) {
74 err(EX_IOERR
, "select");
76 for (int fd
=0; fd
<= maxfd
; fd
++) {
77 if (FD_ISSET(fd
, &ready_inputs
)) {
78 int read_return
= read(fd
, &i
, sizeof(i
));
79 if (read_return
== -1) {
80 err(EX_IOERR
, "read");
82 if (read_return
!= sizeof(i
)) {
83 errx(EX_IOERR
, "Unexpected EOF");
85 if (i
.type
== 1 && i
.value
== 1) {
87 fprintf(output_file
, "%ld.%06ld\n", i
.time
.tv_sec
, i
.time
.tv_usec
);
89 fprintf(output_file
, "%ld\n", i
.time
.tv_sec
);