2 * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3 * Copyright (C) 2006-2009 Kim Woelders
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies of the Software, its documentation and marketing & publicity
14 * materials, and acknowledgment shall be given in the documentation, materials
15 * and software packages that this Software was used.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "e16-ecore_list.h"
29 #define DEBUG_TIMERS 0
35 int (*func) (void *data);
45 gettimeofday(&timev, NULL);
46 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
49 static Timer *q_first = NULL;
52 _TimerSet(Timer * timer)
57 if (EDebug(EDBUG_TYPE_TIMERS))
58 Eprintf("_TimerSet %p: func=%p data=%p\n", timer, timer->func,
62 /* if there is no queue it becomes the queue */
71 for (ptr = q_first; ptr; pptr = ptr, ptr = ptr->next)
73 if (ptr->at_time > timer->at_time)
85 _TimerDel(Timer * timer)
88 if (EDebug(EDBUG_TYPE_TIMERS))
89 Eprintf("_TimerDel %p: func=%p data=%p\n", timer, timer->func,
96 TimerAdd(double in_time, int (*func) (void *data), void *data)
100 timer = EMALLOC(Timer, 1);
104 if (in_time < 0.) /* No negative in-times */
108 timer->in_time = in_time;
109 timer->at_time = GetTime() + in_time;
112 if (EDebug(EDBUG_TYPE_TIMERS))
113 Eprintf("TimerAdd %p: func=%p data=%p: %8.3f\n", timer,
114 timer->func, timer->data, in_time);
116 _TimerSet(timer); /* Add to timer queue */
124 Timer *timer, *q_old, *q_run;
129 return 0.; /* No timers pending */
135 q_run = q_old = q_first;
136 for (; timer; timer = q_first)
138 if (timer->at_time > t + 200e-6) /* Within 200 us is close enough */
141 if (EDebug(EDBUG_TYPE_TIMERS))
142 Eprintf("TimersRun - run %p: func=%p data=%p: %8.3lf\n", timer,
143 timer->func, timer->data, timer->at_time - t);
145 q_first = timer->next;
147 /* Run this callback */
148 timer->again = timer->func(timer->data);
152 if (q_old != q_first)
154 /* At least one timer has run */
155 q_run->next = NULL; /* Terminate expired timer list */
157 /* Re-schedule/remove timers that have run */
158 for (timer = q_old; timer; timer = q_old)
163 timer->at_time += timer->in_time;
164 _TimerSet(timer); /* Add to timer queue */
173 if (tt <= 0.) /* Avoid some redundant debug output */
178 if (EDebug(EDBUG_TYPE_TIMERS) > 1)
182 for (qp = timer; qp; qp = qp->next)
183 Eprintf("TimersRun - pend %p: func=%p data=%p: %8.3lf\n", qp,
184 qp->func, qp->data, qp->at_time - t);
189 t = timer->at_time - t;
198 if (EDebug(EDBUG_TYPE_TIMERS))
199 Eprintf("TimersRun - next in %8.3lf\n", t);
205 TimerDel(Timer * timer)
207 Timer *qe, *ptr, *pptr;
210 for (ptr = q_first; ptr; pptr = ptr, ptr = ptr->next)
216 /* Match - remove it from the queue */
218 pptr->next = qe->next;
233 TimerSetInterval(Timer * timer, double dt)
241 static Ecore_List *idler_list = NULL;
243 typedef void (IdlerFunc) (void *data);
251 IdlerAdd(IdlerFunc * func, void *data)
255 id = EMALLOC(Idler, 1);
263 idler_list = ecore_list_new();
265 ecore_list_append(idler_list, id);
273 ecore_list_node_remove(idler_list, id);
278 _IdlerRun(void *_id, void *prm __UNUSED__)
280 Idler *id = (Idler *) _id;
288 if (EDebug(EDBUG_TYPE_IDLERS))
289 Eprintf("IdlersRun\n");
291 ecore_list_for_each(idler_list, _IdlerRun, NULL);
297 #define DEBUG_ANIMATORS 0
298 static Ecore_List *animator_list = NULL;
299 static Timer *animator_timer = NULL;
301 typedef int (AnimatorFunc) (void *data);
309 _AnimatorRun(void *_an, void *prm __UNUSED__)
311 Animator *an = (Animator *) _an;
314 #if DEBUG_ANIMATORS > 1
315 Eprintf("AnimatorRun %p\n", an);
317 again = an->func(an->data);
323 AnimatorsRun(void *data __UNUSED__)
325 ecore_list_for_each(animator_list, _AnimatorRun, NULL);
327 if (ecore_list_count(animator_list))
329 TimerSetInterval(animator_timer, 1e-3 * Conf.animation.step);
334 animator_timer = NULL;
340 AnimatorAdd(AnimatorFunc * func, void *data)
344 an = EMALLOC(Animator, 1);
349 Eprintf("AnimatorAdd %p func=%p data=%p\n", an, func, data);
355 animator_list = ecore_list_new();
357 ecore_list_append(animator_list, an);
359 if (ecore_list_count(animator_list) == 1)
361 if (Conf.animation.step <= 0)
362 Conf.animation.step = 1;
363 /* Animator list was empty - Add to timer qeueue */
364 TIMER_ADD(animator_timer, 1e-3 * Conf.animation.step,
372 AnimatorDel(Animator * an)
375 Eprintf("AnimatorDel %p func=%p data=%p\n", an, an->func, an->data);
378 ecore_list_node_remove(animator_list, an);
381 if (ecore_list_count(animator_list) == 0)
383 /* Animator list is empty - Remove from timer qeueue */
384 TIMER_DEL(animator_timer);