diff options
| -rw-r--r-- | src/Fl.cxx | 100 | ||||
| -rw-r--r-- | src/Fl_mac.cxx | 100 |
2 files changed, 101 insertions, 99 deletions
diff --git a/src/Fl.cxx b/src/Fl.cxx index 2492c5050..bb7cee968 100644 --- a/src/Fl.cxx +++ b/src/Fl.cxx @@ -116,105 +116,7 @@ int Fl::event_inside(const Fl_Widget *o) /*const*/ { #elif defined(__APPLE__) -// -// MacOS X timers -// - -struct MacTimeout { - Fl_Timeout_Handler callback; - void* data; - EventLoopTimerRef timer; -}; -static MacTimeout* mac_timers; -static int mac_timer_alloc; -static int mac_timer_used; - - -static void realloc_timers() -{ - if (mac_timer_alloc == 0) { - mac_timer_alloc = 8; - } - MacTimeout* new_timers = new MacTimeout[mac_timer_alloc * 2]; - memmove(new_timers, mac_timers, sizeof(MacTimeout) * mac_timer_used); - MacTimeout* delete_me = mac_timers; - mac_timers = new_timers; - delete [] delete_me; - mac_timer_alloc *= 2; -} - -static void delete_timer(MacTimeout& t) -{ - RemoveEventLoopTimer(t.timer); - memset(&t, 0, sizeof(MacTimeout)); -} - - -static pascal void do_timer(EventLoopTimerRef timer, void* data) -{ - for (int i = 0; i < mac_timer_used; ++i) { - MacTimeout& t = mac_timers[i]; - if (t.timer == timer && t.data == data) { - return (*t.callback)(data); - } - } -} - - -void Fl::add_timeout(double time, Fl_Timeout_Handler cb, void* data) -{ - int timer_id = -1; - for (int i = 0; i < mac_timer_used; ++i) { - if ( !mac_timers[i].timer ) { - timer_id = i; - break; - } - } - if (timer_id == -1) { - if (mac_timer_used == mac_timer_alloc) { - realloc_timers(); - } - timer_id = mac_timer_used++; - } - - EventTimerInterval fireDelay = (EventTimerInterval) time; - EventLoopTimerUPP timerUPP = NewEventLoopTimerUPP(do_timer); - EventLoopTimerRef timerRef; - OSStatus err = InstallEventLoopTimer(GetMainEventLoop(), fireDelay, 0, timerUPP, data, &timerRef); - if (err == noErr) { - mac_timers[timer_id].callback = cb; - mac_timers[timer_id].data = data; - mac_timers[timer_id].timer = timerRef; - } -} - -void Fl::repeat_timeout(double time, Fl_Timeout_Handler cb, void* data) -{ - remove_timeout(cb, data); - add_timeout(time, cb, data); -} - -int Fl::has_timeout(Fl_Timeout_Handler cb, void* data) -{ - for (int i = 0; i < mac_timer_used; ++i) { - MacTimeout& t = mac_timers[i]; - if (t.callback == cb && t.data == data) { - return 1; - } - } - return 0; -} - -void Fl::remove_timeout(Fl_Timeout_Handler cb, void* data) -{ - for (int i = 0; i < mac_timer_used; ++i) { - MacTimeout& t = mac_timers[i]; - if (t.callback == cb && ( t.data == data || data == NULL)) { - delete_timer(t); - } - } -} - +/// implementation in Fl_mac.cxx #else diff --git a/src/Fl_mac.cxx b/src/Fl_mac.cxx index 2338fd421..606570b69 100644 --- a/src/Fl_mac.cxx +++ b/src/Fl_mac.cxx @@ -599,6 +599,51 @@ static void breakMacEventLoop() fl_unlock_function(); } +// +// MacOS X timers +// + +struct MacTimeout { + Fl_Timeout_Handler callback; + void* data; + EventLoopTimerRef timer; +}; +static MacTimeout* mac_timers; +static int mac_timer_alloc; +static int mac_timer_used; + + +static void realloc_timers() +{ + if (mac_timer_alloc == 0) { + mac_timer_alloc = 8; + } + MacTimeout* new_timers = new MacTimeout[mac_timer_alloc * 2]; + memmove(new_timers, mac_timers, sizeof(MacTimeout) * mac_timer_used); + MacTimeout* delete_me = mac_timers; + mac_timers = new_timers; + delete [] delete_me; + mac_timer_alloc *= 2; +} + +static void delete_timer(MacTimeout& t) +{ + RemoveEventLoopTimer(t.timer); + memset(&t, 0, sizeof(MacTimeout)); +} + + +static pascal void do_timer(EventLoopTimerRef timer, void* data) +{ + for (int i = 0; i < mac_timer_used; ++i) { + MacTimeout& t = mac_timers[i]; + if (t.timer == timer && t.data == data) { + (*t.callback)(data); + break; + } + } + breakMacEventLoop(); +} /** * This function is the central event handler. @@ -2136,6 +2181,61 @@ void Fl::paste(Fl_Widget &receiver, int clipboard) { return; } +void Fl::add_timeout(double time, Fl_Timeout_Handler cb, void* data) +{ + int timer_id = -1; + for (int i = 0; i < mac_timer_used; ++i) { + if ( !mac_timers[i].timer ) { + timer_id = i; + break; + } + } + if (timer_id == -1) { + if (mac_timer_used == mac_timer_alloc) { + realloc_timers(); + } + timer_id = mac_timer_used++; + } + + EventTimerInterval fireDelay = (EventTimerInterval) time; + EventLoopTimerUPP timerUPP = NewEventLoopTimerUPP(do_timer); + EventLoopTimerRef timerRef; + OSStatus err = InstallEventLoopTimer(GetMainEventLoop(), fireDelay, 0, timerUPP, data, &timerRef); + if (err == noErr) { + mac_timers[timer_id].callback = cb; + mac_timers[timer_id].data = data; + mac_timers[timer_id].timer = timerRef; + } +} + +void Fl::repeat_timeout(double time, Fl_Timeout_Handler cb, void* data) +{ + remove_timeout(cb, data); + add_timeout(time, cb, data); +} + +int Fl::has_timeout(Fl_Timeout_Handler cb, void* data) +{ + for (int i = 0; i < mac_timer_used; ++i) { + MacTimeout& t = mac_timers[i]; + if (t.callback == cb && t.data == data) { + return 1; + } + } + return 0; +} + +void Fl::remove_timeout(Fl_Timeout_Handler cb, void* data) +{ + for (int i = 0; i < mac_timer_used; ++i) { + MacTimeout& t = mac_timers[i]; + if (t.callback == cb && ( t.data == data || data == NULL)) { + delete_timer(t); + } + } +} + + // // End of "$Id$". |
