From 8eac1646453f1ffd2873c12f394abe9d779c109f Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 26 Dec 2013 00:34:45 -0800 Subject: [PATCH] Use select() to wait for data In preparation for handling multiple inputs. --- keystroke-timestamps.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/keystroke-timestamps.c b/keystroke-timestamps.c index 6935600..d25d572 100644 --- a/keystroke-timestamps.c +++ b/keystroke-timestamps.c @@ -6,6 +6,7 @@ #include #include #include +#include #include int main (int argc, char **argv) @@ -34,7 +35,17 @@ int main (int argc, char **argv) } free(device); struct input_event i; - while (read(fd, &i, sizeof(i)) == sizeof(i)) { + fd_set all_inputs, ready_inputs; + FD_ZERO(&all_inputs); + FD_SET(fd, &all_inputs); + while (1) { + ready_inputs = all_inputs; + if (select(fd+1, &ready_inputs, NULL, NULL, NULL) != 1) { + err(EX_IOERR, "select"); + } + if (read(fd, &i, sizeof(i)) != sizeof(i)) { + break; + } if (i.type == 1 && i.value == 1) { if (print_usec) { printf("%ld.%06ld\n", i.time.tv_sec, i.time.tv_usec); -- 2.51.2