Author: manolo
Date: 2012-06-16 01:58:27 -0700 (Sat, 16 Jun 2012)
New Revision: 9619
Log:
Mac OS: some optimization of timer objects + code reordering.

Modified:
   branches/branch-3.0/src/fltk3/cocoa.mm

Modified: branches/branch-3.0/src/fltk3/cocoa.mm
===================================================================
--- branches/branch-3.0/src/fltk3/cocoa.mm      2012-06-16 08:49:52 UTC (rev 
9618)
+++ branches/branch-3.0/src/fltk3/cocoa.mm      2012-06-16 08:58:27 UTC (rev 
9619)
@@ -509,26 +509,111 @@
                      kCFRunLoopDefaultMode);
     CFRelease(t.timer);
     memset(&t, 0, sizeof(MacTimeout));
+    if (&t == current_timer) current_timer = NULL;
   }
 }
 
 static void do_timer(CFRunLoopTimerRef timer, void* data)
 {
-  for (int i = 0;  i < mac_timer_used;  ++i) {
+  current_timer = (MacTimeout*)data;
+  current_timer->pending = 0;
+  (current_timer->callback)(current_timer->data);
+  if (current_timer && current_timer->pending == 0)
+    delete_timer(*current_timer);
+  current_timer = NULL;
+  breakMacEventLoop();
+}
+
+void fltk3::add_timeout(double time, fltk3::TimeoutHandler cb, void* data)
+{
+  // check, if this timer slot exists already
+  for (int i = 0; i < mac_timer_used; ++i) {
     MacTimeout& t = mac_timers[i];
-    if (t.timer == timer  &&  t.data == data) {
-      t.pending = 0;
-      current_timer = &t;
-      (*t.callback)(data);
-      current_timer = NULL;
-      if (t.pending==0)
-        delete_timer(t);
+    // if so, simply change the fire interval
+    if (t.callback == cb  &&  t.data == data) {
+      t.next_timeout = CFAbsoluteTimeGetCurrent() + time;
+      CFRunLoopTimerSetNextFireDate(t.timer, t.next_timeout );
+      t.pending = 1;
+      return;
+    }
+  }
+  // no existing timer to use. Create a new one:
+  int 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;
     }
   }
-  breakMacEventLoop();
+  // 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, &t, 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 fltk3::repeat_timeout(double time, fltk3::TimeoutHandler cb, void* data)
+{
+  if (current_timer) {
+    // 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;
+    return;
+  }
+  add_timeout(time, cb, data);
+}
+
+int fltk3::has_timeout(fltk3::TimeoutHandler 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 fltk3::remove_timeout(fltk3::TimeoutHandler 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);
+    }
+  }
+}
+
 @interface FLWindow : NSWindow 
 {
   @private
@@ -2517,96 +2602,6 @@
   receiver.handle(fltk3::PASTE);
 }
 
-void fltk3::add_timeout(double time, fltk3::TimeoutHandler cb, void* data)
-{
-  // check, if this timer slot exists already
-  for (int i = 0; i < mac_timer_used; ++i) {
-    MacTimeout& t = mac_timers[i];
-    // if so, simply change the fire interval
-    if (t.callback == cb  &&  t.data == data) {
-      t.next_timeout = CFAbsoluteTimeGetCurrent() + time;
-      CFRunLoopTimerSetNextFireDate(t.timer, t.next_timeout );
-      t.pending = 1;
-      return;
-    }
-  }
-  // no existing timer to use. Create a new one:
-  int 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, data, 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 fltk3::repeat_timeout(double time, fltk3::TimeoutHandler cb, void* data)
-{
-  if (current_timer) {
-    // 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;
-    return;
-  }
-  add_timeout(time, cb, data);
-}
-
-int fltk3::has_timeout(fltk3::TimeoutHandler 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 fltk3::remove_timeout(fltk3::TimeoutHandler 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);
-    }
-  }
-}
-
 int Fl_X::unlink(Fl_X *start) {
   if (start) {
     Fl_X *pc = start;

_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit

Reply via email to