mirror of
https://github.com/fltk/fltk.git
synced 2026-06-04 23:42:15 +08:00
Changed call_timeouts() and Fl::wait()/wait(double). The wait functions will now return immediately (do fl_wait(0,0)) if any timeouts were called in call_timeouts(). Motivation: An event loop like
while (work_to_do) {
work_some();
Fl::check();
} else {
Fl::wait();
}
Suppose that a timeout sets the work_to_do flag, which is initially cleared.
Fl::wait() will wait for this timeout, return, then expire the timeout and wait for user input.
With the fix, it will work as expected.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.0@669 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
+16
-11
@@ -1,5 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
//
|
//
|
||||||
// "$Id: Fl.cxx,v 1.24.2.11 1999/07/27 17:24:13 bill Exp $"
|
// "$Id: Fl.cxx,v 1.24.2.12 1999/08/22 23:31:21 gustavo Exp $"
|
||||||
//
|
//
|
||||||
// Main event handling code for the Fast Light Tool Kit (FLTK).
|
// Main event handling code for the Fast Light Tool Kit (FLTK).
|
||||||
//
|
//
|
||||||
@@ -111,17 +112,19 @@ void Fl::remove_timeout(void (*cb)(void *), void *v) {
|
|||||||
numtimeouts = j;
|
numtimeouts = j;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void call_timeouts() {
|
static int call_timeouts() {
|
||||||
|
int expired = 0;
|
||||||
while (numtimeouts) {
|
while (numtimeouts) {
|
||||||
if (timeout[0].time > 0) break;
|
if (timeout[0].time > 0) break;
|
||||||
// we must remove timeout from array before doing the callback:
|
// we must remove timeout from array before doing the callback:
|
||||||
void (*cb)(void*) = timeout[0].cb;
|
void (*cb)(void*) = timeout[0].cb;
|
||||||
void *arg = timeout[0].arg;
|
void *arg = timeout[0].arg;
|
||||||
numtimeouts--;
|
numtimeouts--; expired++;
|
||||||
if (numtimeouts) memmove(timeout, timeout+1, numtimeouts*sizeof(Timeout));
|
if (numtimeouts) memmove(timeout, timeout+1, numtimeouts*sizeof(Timeout));
|
||||||
// now it is safe for the callback to do add_timeout:
|
// now it is safe for the callback to do add_timeout:
|
||||||
cb(arg);
|
cb(arg);
|
||||||
}
|
}
|
||||||
|
return expired;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Fl::flush() {
|
void Fl::flush() {
|
||||||
@@ -154,7 +157,7 @@ static int initclock; // if false we didn't call fl_elapsed() last time
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// fl_elapsed must return the amount of time since the last time it was
|
// fl_elapsed must return the amount of time since the last time it was
|
||||||
// called. To reduce the number of system calls the to get the
|
// called. To reduce the number of system calls to get the
|
||||||
// current time, the "initclock" symbol is turned on by an indefinite
|
// current time, the "initclock" symbol is turned on by an indefinite
|
||||||
// wait. This should then reset the measured-from time and return zero
|
// wait. This should then reset the measured-from time and return zero
|
||||||
static double fl_elapsed() {
|
static double fl_elapsed() {
|
||||||
@@ -204,14 +207,15 @@ static void callidle() {
|
|||||||
|
|
||||||
int Fl::wait() {
|
int Fl::wait() {
|
||||||
callidle();
|
callidle();
|
||||||
if (numtimeouts) {fl_elapsed(); call_timeouts();}
|
int expired = 0;
|
||||||
|
if (numtimeouts) {fl_elapsed(); expired = call_timeouts();}
|
||||||
flush();
|
flush();
|
||||||
if (!Fl_X::first) return 0; // no windows
|
if (!Fl_X::first) return 0; // no windows
|
||||||
if (idle && !in_idle)
|
if ((idle && !in_idle) || expired) {
|
||||||
fl_wait(1,0.0);
|
fl_wait(1,0.0);
|
||||||
else if (numtimeouts)
|
} else if (numtimeouts) {
|
||||||
fl_wait(1, timeout[0].time);
|
fl_wait(1, timeout[0].time);
|
||||||
else {
|
} else {
|
||||||
initclock = 0;
|
initclock = 0;
|
||||||
fl_wait(0,0);
|
fl_wait(0,0);
|
||||||
}
|
}
|
||||||
@@ -220,9 +224,10 @@ int Fl::wait() {
|
|||||||
|
|
||||||
double Fl::wait(double time) {
|
double Fl::wait(double time) {
|
||||||
callidle();
|
callidle();
|
||||||
if (numtimeouts) {time -= fl_elapsed(); call_timeouts();}
|
int expired = 0;
|
||||||
|
if (numtimeouts) {time -= fl_elapsed(); expired = call_timeouts();}
|
||||||
flush();
|
flush();
|
||||||
double wait_time = idle && !in_idle ? 0.0 : time;
|
double wait_time = (idle && !in_idle) || expired ? 0.0 : time;
|
||||||
if (numtimeouts && timeout[0].time < wait_time) wait_time = timeout[0].time;
|
if (numtimeouts && timeout[0].time < wait_time) wait_time = timeout[0].time;
|
||||||
fl_wait(1, wait_time);
|
fl_wait(1, wait_time);
|
||||||
return time - fl_elapsed();
|
return time - fl_elapsed();
|
||||||
@@ -695,5 +700,5 @@ int fl_old_shortcut(const char* s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// End of "$Id: Fl.cxx,v 1.24.2.11 1999/07/27 17:24:13 bill Exp $".
|
// End of "$Id: Fl.cxx,v 1.24.2.12 1999/08/22 23:31:21 gustavo Exp $".
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user