Corrected timeout handling

This commit is contained in:
Bert 2011-09-02 18:54:41 +02:00
parent dcd04e526d
commit abb5feeb78

13
main.c
View file

@ -160,21 +160,23 @@ void reset_timeout(timeout_f handler) {
} }
int check_timeouts(struct timeval *t) { int check_timeouts(struct timeval *t) {
int i, tdiff, tmin = -1; int i = 0, tdiff, tmin = -1;
struct timeval now; struct timeval now;
gettimeofday(&now, 0); gettimeofday(&now, 0);
for (i = 0; i < LEN(timeouts); i++) { while (i < LEN(timeouts)) {
if (timeouts[i].active) { if (timeouts[i].active) {
tdiff = TIMEDIFF(&timeouts[i].when, &now); tdiff = TIMEDIFF(&timeouts[i].when, &now);
if (tdiff <= 0) { if (tdiff <= 0) {
timeouts[i].active = False; timeouts[i].active = False;
if (timeouts[i].handler) if (timeouts[i].handler)
timeouts[i].handler(); timeouts[i].handler();
i = tmin = -1;
} else if (tmin < 0 || tdiff < tmin) { } else if (tmin < 0 || tdiff < tmin) {
tmin = tdiff; tmin = tdiff;
} }
} }
i++;
} }
if (tmin > 0 && t) if (tmin > 0 && t)
MSEC_TO_TIMEVAL(tmin, t); MSEC_TO_TIMEVAL(tmin, t);
@ -375,13 +377,12 @@ void run() {
} }
} }
if (!XPending(win.env.dpy) && check_timeouts(&timeout)) { while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
/* handle timeouts */ /* wait for timeouts */
xfd = ConnectionNumber(win.env.dpy); xfd = ConnectionNumber(win.env.dpy);
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(xfd, &fds); FD_SET(xfd, &fds);
if (!select(xfd + 1, &fds, 0, 0, &timeout)) select(xfd + 1, &fds, 0, 0, &timeout);
check_timeouts(NULL);
} }
if (!XNextEvent(win.env.dpy, &ev)) { if (!XNextEvent(win.env.dpy, &ev)) {