nsxivabb5fee
Corrected timeout handling
+7 −6
1 file changed
- M main.c +7 −6
| @@ -160,21 +160,23 @@ void reset_timeout(timeout_f handler) { | |||
| 160 | 160 | } | |
| 161 | 161 | ||
| 162 | 162 | int check_timeouts(struct timeval *t) { | |
| 163 | − | int i, tdiff, tmin = -1; | |
| 163 | + | int i = 0, tdiff, tmin = -1; | |
| 164 | 164 | struct timeval now; | |
| 165 | 165 | ||
| 166 | 166 | gettimeofday(&now, 0); | |
| 167 | − | for (i = 0; i < LEN(timeouts); i++) { | |
| 167 | + | while (i < LEN(timeouts)) { | |
| 168 | 168 | if (timeouts[i].active) { | |
| 169 | 169 | tdiff = TIMEDIFF(&timeouts[i].when, &now); | |
| 170 | 170 | if (tdiff <= 0) { | |
| 171 | 171 | timeouts[i].active = False; | |
| 172 | 172 | if (timeouts[i].handler) | |
| 173 | 173 | timeouts[i].handler(); | |
| 174 | + | i = tmin = -1; | |
| 174 | 175 | } else if (tmin < 0 || tdiff < tmin) { | |
| 175 | 176 | tmin = tdiff; | |
| 176 | 177 | } | |
| 177 | 178 | } | |
| 179 | + | i++; | |
| 178 | 180 | } | |
| 179 | 181 | if (tmin > 0 && t) | |
| 180 | 182 | MSEC_TO_TIMEVAL(tmin, t); | |
| @@ -375,13 +377,12 @@ void run() { | |||
| 375 | 377 | } | |
| 376 | 378 | } | |
| 377 | 379 | ||
| 378 | − | if (!XPending(win.env.dpy) && check_timeouts(&timeout)) { | |
| 379 | − | /* handle timeouts */ | |
| 380 | + | while (!XPending(win.env.dpy) && check_timeouts(&timeout)) { | |
| 381 | + | /* wait for timeouts */ | |
| 380 | 382 | xfd = ConnectionNumber(win.env.dpy); | |
| 381 | 383 | FD_ZERO(&fds); | |
| 382 | 384 | FD_SET(xfd, &fds); | |
| 383 | − | if (!select(xfd + 1, &fds, 0, 0, &timeout)) | |
| 384 | − | check_timeouts(NULL); | |
| 385 | + | select(xfd + 1, &fds, 0, 0, &timeout); | |
| 385 | 386 | } | |
| 386 | 387 | ||
| 387 | 388 | if (!XNextEvent(win.env.dpy, &ev)) { | |