From abb5feeb786a711e57a63fa6027ca06002226c68 Mon Sep 17 00:00:00 2001 From: Bert Date: Fri, 2 Sep 2011 18:54:41 +0200 Subject: [PATCH] Corrected timeout handling --- main.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 13cab4c..f596733 100644 --- a/main.c +++ b/main.c @@ -160,21 +160,23 @@ void reset_timeout(timeout_f handler) { } int check_timeouts(struct timeval *t) { - int i, tdiff, tmin = -1; + int i = 0, tdiff, tmin = -1; struct timeval now; gettimeofday(&now, 0); - for (i = 0; i < LEN(timeouts); i++) { + while (i < LEN(timeouts)) { if (timeouts[i].active) { tdiff = TIMEDIFF(&timeouts[i].when, &now); if (tdiff <= 0) { timeouts[i].active = False; if (timeouts[i].handler) timeouts[i].handler(); + i = tmin = -1; } else if (tmin < 0 || tdiff < tmin) { tmin = tdiff; } } + i++; } if (tmin > 0 && t) MSEC_TO_TIMEVAL(tmin, t); @@ -375,13 +377,12 @@ void run() { } } - if (!XPending(win.env.dpy) && check_timeouts(&timeout)) { - /* handle timeouts */ + while (!XPending(win.env.dpy) && check_timeouts(&timeout)) { + /* wait for timeouts */ xfd = ConnectionNumber(win.env.dpy); FD_ZERO(&fds); FD_SET(xfd, &fds); - if (!select(xfd + 1, &fds, 0, 0, &timeout)) - check_timeouts(NULL); + select(xfd + 1, &fds, 0, 0, &timeout); } if (!XNextEvent(win.env.dpy, &ev)) {