From: Scott Worley Date: Thu, 26 Dec 2013 08:34:45 +0000 (-0800) Subject: Use select() to wait for data X-Git-Tag: v1.0.0~11 X-Git-Url: http://git.scottworley.com/keystroke-timestamps/commitdiff_plain/8eac1646453f1ffd2873c12f394abe9d779c109f?ds=sidebyside;hp=6fb4fdb90b700a290c3ad129338fc1715b478e39 Use select() to wait for data In preparation for handling multiple inputs. --- 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);