diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2022-01-31 22:27:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-31 22:27:17 +0100 |
| commit | 29d9e31c51e6c11d6e33abf9bc4551afd9de3836 (patch) | |
| tree | 82dcb5f84dde9bb6cbfe50983094baa12215e57e /src/drivers/Cocoa | |
| parent | cf4a832e6a801b46c38f6236369c74056e8f89ec (diff) | |
Consolidate timeout handling across platforms (#379)
Add Fl_Timeout class
Move platform independent code of Fl::wait() to main part
- basic timeout handling
- Fl::run_checks()
- Fl::run_idle()
- Fl::idle()
- waiting time calculation (next timeout)
- remove platform dependent "screen driver" stuff
Diffstat (limited to 'src/drivers/Cocoa')
| -rw-r--r-- | src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H | 7 | ||||
| -rw-r--r-- | src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx | 142 |
2 files changed, 2 insertions, 147 deletions
diff --git a/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H b/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H index dcd5c4f57..7dd6fc14a 100644 --- a/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H +++ b/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.H @@ -2,7 +2,7 @@ // Definition of Apple Cocoa Screen interface // for the Fast Light Tool Kit (FLTK). // -// Copyright 2010-2018 by Bill Spitzak and others. +// Copyright 2010-2022 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -80,11 +80,6 @@ public: virtual void grab(Fl_Window* win); // --- global colors virtual void get_system_colors(); - // --- global timers - virtual void add_timeout(double time, Fl_Timeout_Handler cb, void *argp); - virtual void repeat_timeout(double time, Fl_Timeout_Handler cb, void *argp); - virtual int has_timeout(Fl_Timeout_Handler cb, void *argp); - virtual void remove_timeout(Fl_Timeout_Handler cb, void *argp); virtual int has_marked_text() const; static void reset_marked_text(); static void insertion_point_location(int x, int y, int height); diff --git a/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx b/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx index 1a36149e2..e213e5fd0 100644 --- a/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx +++ b/src/drivers/Cocoa/Fl_Cocoa_Screen_Driver.cxx @@ -1,7 +1,7 @@ // // Definition of Apple Cocoa Screen interface. // -// Copyright 1998-2018 by Bill Spitzak and others. +// Copyright 1998-2022 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -401,143 +401,3 @@ Fl_RGB_Image *Fl_Cocoa_Screen_Driver::read_win_rectangle(int X, int Y, int w, in rgb->alloc_array = 1; return rgb; } - -// -// MacOS X timers -// - -struct MacTimeout { - Fl_Timeout_Handler callback; - void* data; - CFRunLoopTimerRef timer; - char pending; - CFAbsoluteTime next_timeout; // scheduled time for this timer -}; -static MacTimeout* mac_timers; -static int mac_timer_alloc; -static int mac_timer_used; -static MacTimeout* current_timer; // the timer that triggered its callback function - -static void realloc_timers() -{ - if (mac_timer_alloc == 0) { - mac_timer_alloc = 8; - fl_open_display(); // needed because the timer creates an event - } - mac_timer_alloc *= 2; - MacTimeout* new_timers = new MacTimeout[mac_timer_alloc]; - memset(new_timers, 0, sizeof(MacTimeout)*mac_timer_alloc); - memcpy(new_timers, mac_timers, sizeof(MacTimeout) * mac_timer_used); - if (current_timer) { - MacTimeout* newCurrent = new_timers + (current_timer - mac_timers); - current_timer = newCurrent; - } - MacTimeout* delete_me = mac_timers; - mac_timers = new_timers; - delete [] delete_me; -} - -static void delete_timer(MacTimeout& t) -{ - if (t.timer) { - CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), - t.timer, - kCFRunLoopDefaultMode); - CFRelease(t.timer); - memset(&t, 0, sizeof(MacTimeout)); - } -} - -static void do_timer(CFRunLoopTimerRef timer, void* data) -{ - fl_lock_function(); - fl_intptr_t timerId = (fl_intptr_t)data; - current_timer = &mac_timers[timerId]; - current_timer->pending = 0; - (current_timer->callback)(current_timer->data); - if (current_timer && current_timer->pending == 0) - delete_timer(*current_timer); - current_timer = NULL; - - Fl_Cocoa_Screen_Driver::breakMacEventLoop(); - fl_unlock_function(); -} - -void Fl_Cocoa_Screen_Driver::add_timeout(double time, Fl_Timeout_Handler cb, void* data) -{ - // always create a new timer entry - fl_intptr_t timer_id = -1; - // find an empty slot in the timer array - for (int i = 0; i < mac_timer_used; ++i) { - if ( !mac_timers[i].timer ) { - timer_id = i; - break; - } - } - // if there was no empty slot, append a new timer - if (timer_id == -1) { - // make space if needed - if (mac_timer_used == mac_timer_alloc) { - realloc_timers(); - } - timer_id = mac_timer_used++; - } - // now install a brand new timer - MacTimeout& t = mac_timers[timer_id]; - CFRunLoopTimerContext context = {0, (void*)timer_id, NULL,NULL,NULL}; - CFRunLoopTimerRef timerRef = CFRunLoopTimerCreate(kCFAllocatorDefault, - CFAbsoluteTimeGetCurrent() + time, - 1E30, - 0, - 0, - do_timer, - &context - ); - if (timerRef) { - CFRunLoopAddTimer(CFRunLoopGetCurrent(), - timerRef, - kCFRunLoopDefaultMode); - t.callback = cb; - t.data = data; - t.timer = timerRef; - t.pending = 1; - t.next_timeout = CFRunLoopTimerGetNextFireDate(timerRef); - } -} - -void Fl_Cocoa_Screen_Driver::repeat_timeout(double time, Fl_Timeout_Handler cb, void* data) -{ - if (!current_timer) { - add_timeout(time, cb, data); - return; - } - // k = how many times 'time' seconds after the last scheduled timeout until the future - double k = ceil( (CFAbsoluteTimeGetCurrent() - current_timer->next_timeout) / time); - if (k < 1) k = 1; - current_timer->next_timeout += k * time; - CFRunLoopTimerSetNextFireDate(current_timer->timer, current_timer->next_timeout ); - current_timer->callback = cb; - current_timer->data = data; - current_timer->pending = 1; -} - -int Fl_Cocoa_Screen_Driver::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 && t.pending) { - return 1; - } - } - return 0; -} - -void Fl_Cocoa_Screen_Driver::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); - } - } -} |
