From 309047f20541b831df389eb2e7a1c13130eab8b2 Mon Sep 17 00:00:00 2001 From: Bernd Busse Date: Wed, 8 Jun 2016 11:33:47 +0200 Subject: [PATCH] Consume all the lines before parsing Read input lines in non-blockin mode and parse the last one. Should fix https://github.com/LemonBoy/bar/issues/107 --- lemonbar.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lemonbar.c b/lemonbar.c index f1b7886..9b9e849 100644 --- a/lemonbar.c +++ b/lemonbar.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -1344,6 +1345,9 @@ main (int argc, char **argv) // Get the fd to Xserver pollin[1].fd = xcb_get_file_descriptor(c); + // Prevent fgets to block + fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); + for (;;) { bool redraw = false; @@ -1357,9 +1361,9 @@ main (int argc, char **argv) else break; // ...bail out } if (pollin[0].revents & POLLIN) { // New input, process it - if (fgets(input, sizeof(input), stdin) == NULL) - break; // EOF received - + input[0] = '\0'; + while (fgets(input, sizeof(input), stdin) != NULL) + ; // Drain the buffer, the last line is actually used parse(input); redraw = true; }