1999-10-04 15:06:00 -07:00
|
|
|
/*
|
2007-01-13 11:14:29 -08:00
|
|
|
* Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
|
2012-03-21 14:10:59 -07:00
|
|
|
* Copyright (C) 2006-2012 Kim Woelders
|
2000-04-05 16:22:56 -07:00
|
|
|
*
|
1999-10-04 15:06:00 -07:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2000-04-05 16:22:56 -07:00
|
|
|
*
|
1999-10-04 15:06:00 -07:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies of the Software, its documentation and marketing & publicity
|
|
|
|
* materials, and acknowledgment shall be given in the documentation, materials
|
|
|
|
* and software packages that this Software was used.
|
2000-04-05 16:22:56 -07:00
|
|
|
*
|
1999-10-04 15:06:00 -07:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
1999-08-17 15:56:46 -07:00
|
|
|
#include "E.h"
|
2006-04-09 03:18:34 -07:00
|
|
|
#include "e16-ecore_list.h"
|
2006-04-16 06:27:38 -07:00
|
|
|
#include "timers.h"
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
struct _timer {
|
2011-01-05 10:05:52 -08:00
|
|
|
unsigned int in_time;
|
|
|
|
unsigned int at_time;
|
2008-05-24 11:13:17 -07:00
|
|
|
struct _timer *next;
|
|
|
|
int (*func) (void *data);
|
|
|
|
void *data;
|
|
|
|
char again;
|
2006-04-16 06:27:38 -07:00
|
|
|
};
|
|
|
|
|
2011-01-05 10:05:52 -08:00
|
|
|
static int
|
|
|
|
tdiff(unsigned int t1, unsigned int t2)
|
|
|
|
{
|
|
|
|
return (int)(t1 - t2);
|
|
|
|
}
|
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
static Timer *q_first = NULL;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
static void
|
|
|
|
_TimerSet(Timer * timer)
|
1999-08-17 15:56:46 -07:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
Timer *ptr, *pptr;
|
2007-01-28 17:26:12 -08:00
|
|
|
|
2012-07-15 09:17:11 -07:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS) > 1)
|
|
|
|
Eprintf("%s %p: func=%p data=%p\n", __func__, timer, timer->func,
|
2008-05-24 11:13:17 -07:00
|
|
|
timer->data);
|
1999-08-17 15:56:46 -07:00
|
|
|
|
|
|
|
/* if there is no queue it becomes the queue */
|
|
|
|
if (!q_first)
|
2006-01-08 08:13:21 -08:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
q_first = timer;
|
|
|
|
timer->next = NULL;
|
2006-01-08 08:13:21 -08:00
|
|
|
}
|
1999-08-17 15:56:46 -07:00
|
|
|
else
|
2000-07-26 11:00:47 -07:00
|
|
|
{
|
2003-05-22 12:15:03 -07:00
|
|
|
pptr = NULL;
|
2006-01-08 08:13:21 -08:00
|
|
|
for (ptr = q_first; ptr; pptr = ptr, ptr = ptr->next)
|
2003-05-22 12:15:03 -07:00
|
|
|
{
|
2011-01-05 10:05:52 -08:00
|
|
|
if (tdiff(ptr->at_time, timer->at_time) > 0)
|
2006-01-08 08:13:21 -08:00
|
|
|
break;
|
2003-05-22 12:15:03 -07:00
|
|
|
}
|
|
|
|
if (pptr)
|
2008-05-24 11:13:17 -07:00
|
|
|
pptr->next = timer;
|
2003-05-22 12:15:03 -07:00
|
|
|
else
|
2008-05-24 11:13:17 -07:00
|
|
|
q_first = timer;
|
|
|
|
timer->next = ptr;
|
2000-07-26 11:00:47 -07:00
|
|
|
}
|
1999-08-17 15:56:46 -07:00
|
|
|
}
|
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
static void
|
|
|
|
_TimerDel(Timer * timer)
|
|
|
|
{
|
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS))
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s %p: func=%p data=%p\n", __func__, timer, timer->func,
|
2008-05-24 11:13:17 -07:00
|
|
|
timer->data);
|
|
|
|
Efree(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer *
|
2011-01-05 10:05:57 -08:00
|
|
|
TimerAdd(int dt_ms, int (*func) (void *data), void *data)
|
2008-05-24 11:13:17 -07:00
|
|
|
{
|
|
|
|
Timer *timer;
|
|
|
|
|
|
|
|
timer = EMALLOC(Timer, 1);
|
|
|
|
if (!timer)
|
|
|
|
return NULL;
|
|
|
|
|
2011-01-05 10:05:52 -08:00
|
|
|
timer->in_time = (unsigned int)dt_ms;
|
|
|
|
timer->at_time = GetTimeMs() + dt_ms;
|
2008-05-24 11:13:17 -07:00
|
|
|
timer->func = func;
|
|
|
|
timer->data = data;
|
|
|
|
|
2009-03-22 05:40:20 -07:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS))
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s %p: func=%p data=%p: %8d\n", __func__, timer,
|
2011-01-05 10:05:52 -08:00
|
|
|
timer->func, timer->data, dt_ms);
|
2009-03-22 05:40:20 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
_TimerSet(timer); /* Add to timer queue */
|
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2012-07-15 09:17:13 -07:00
|
|
|
void
|
2011-01-05 10:05:52 -08:00
|
|
|
TimersRun(unsigned int t_ms)
|
1999-08-17 15:56:46 -07:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
Timer *timer, *q_old, *q_run;
|
2006-04-16 06:27:38 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
timer = q_first;
|
2011-01-05 10:05:48 -08:00
|
|
|
if (!timer)
|
2012-07-15 09:17:13 -07:00
|
|
|
return; /* No timers pending */
|
2006-04-16 06:27:38 -07:00
|
|
|
|
2011-01-05 10:05:48 -08:00
|
|
|
q_run = q_old = timer;
|
2008-05-24 11:13:17 -07:00
|
|
|
for (; timer; timer = q_first)
|
2007-01-28 17:26:12 -08:00
|
|
|
{
|
2012-07-15 09:17:11 -07:00
|
|
|
if (tdiff(timer->at_time, t_ms) > 0)
|
2007-01-28 17:26:12 -08:00
|
|
|
break;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-01-26 03:43:12 -08:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS))
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s - run %p: func=%p data=%p: %8d\n", __func__, timer,
|
|
|
|
timer->func, timer->data, timer->at_time - t_ms);
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
q_first = timer->next;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
/* Run this callback */
|
|
|
|
timer->again = timer->func(timer->data);
|
|
|
|
q_run = timer;
|
|
|
|
}
|
2007-01-28 17:26:12 -08:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
if (q_old != q_first)
|
|
|
|
{
|
|
|
|
/* At least one timer has run */
|
|
|
|
q_run->next = NULL; /* Terminate expired timer list */
|
|
|
|
|
|
|
|
/* Re-schedule/remove timers that have run */
|
|
|
|
for (timer = q_old; timer; timer = q_old)
|
|
|
|
{
|
|
|
|
q_old = timer->next;
|
|
|
|
if (timer->again)
|
|
|
|
{
|
|
|
|
timer->at_time += timer->in_time;
|
|
|
|
_TimerSet(timer); /* Add to timer queue */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_TimerDel(timer);
|
|
|
|
}
|
|
|
|
}
|
2007-01-28 17:26:12 -08:00
|
|
|
}
|
|
|
|
|
2008-01-26 03:43:12 -08:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS) > 1)
|
2007-01-28 17:26:12 -08:00
|
|
|
{
|
2011-01-05 10:05:52 -08:00
|
|
|
for (timer = q_first; timer; timer = timer->next)
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s - pend %p: func=%p data=%p: %8d (%d)\n", __func__,
|
|
|
|
timer, timer->func, timer->data, timer->at_time - t_ms,
|
|
|
|
timer->in_time);
|
2007-01-28 17:26:12 -08:00
|
|
|
}
|
2012-07-15 09:17:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
TimersRunNextIn(unsigned int t_ms)
|
|
|
|
{
|
|
|
|
Timer *timer;
|
|
|
|
unsigned int tn;
|
2006-04-16 06:27:38 -07:00
|
|
|
|
2011-01-05 10:05:52 -08:00
|
|
|
timer = q_first;
|
2011-02-09 13:08:20 -08:00
|
|
|
|
|
|
|
/* If the next (rescheduled) timer is already expired, set timeout time
|
|
|
|
* to 1 ms. This avoids starving the fd's and should maintain the intended
|
|
|
|
* (mean) timer rate.
|
|
|
|
* The (mean) amount of work done in a timer function should of course not
|
|
|
|
* exceed the timeout time. */
|
|
|
|
if (timer)
|
2012-07-15 09:17:11 -07:00
|
|
|
tn = (int)(timer->at_time - t_ms) > 0 ? timer->at_time - t_ms : 1;
|
2011-02-09 13:08:20 -08:00
|
|
|
else
|
|
|
|
tn = 0;
|
2006-04-16 06:27:38 -07:00
|
|
|
|
2008-01-26 03:43:12 -08:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS))
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s - next in %8u\n", __func__, tn);
|
2007-01-28 17:26:12 -08:00
|
|
|
|
2011-01-05 10:05:52 -08:00
|
|
|
return tn;
|
|
|
|
}
|
|
|
|
|
2011-01-05 10:05:48 -08:00
|
|
|
void
|
2008-05-24 11:13:17 -07:00
|
|
|
TimerDel(Timer * timer)
|
1999-08-17 15:56:46 -07:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
Timer *qe, *ptr, *pptr;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
|
|
|
pptr = NULL;
|
2006-01-08 08:13:21 -08:00
|
|
|
for (ptr = q_first; ptr; pptr = ptr, ptr = ptr->next)
|
2000-07-26 11:00:47 -07:00
|
|
|
{
|
2003-05-22 12:15:03 -07:00
|
|
|
qe = ptr;
|
2008-05-24 11:13:17 -07:00
|
|
|
if (qe != timer)
|
2006-01-08 08:13:21 -08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Match - remove it from the queue */
|
|
|
|
if (pptr)
|
|
|
|
pptr->next = qe->next;
|
|
|
|
else
|
|
|
|
q_first = qe->next;
|
2008-05-24 11:13:17 -07:00
|
|
|
|
2006-01-08 08:13:21 -08:00
|
|
|
/* free it */
|
2008-05-24 11:13:17 -07:00
|
|
|
_TimerDel(timer);
|
2011-01-14 23:45:36 -08:00
|
|
|
break;
|
2000-07-26 11:00:47 -07:00
|
|
|
}
|
1999-08-17 15:56:46 -07:00
|
|
|
}
|
2006-04-09 03:18:34 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
void
|
2011-01-05 10:05:57 -08:00
|
|
|
TimerSetInterval(Timer * timer, int dt_ms)
|
2008-05-24 11:13:17 -07:00
|
|
|
{
|
2011-01-05 10:05:52 -08:00
|
|
|
timer->in_time = (unsigned int)dt_ms;
|
2008-05-24 11:13:17 -07:00
|
|
|
}
|
|
|
|
|
2007-01-27 20:59:46 -08:00
|
|
|
/*
|
|
|
|
* Idlers
|
|
|
|
*/
|
2006-04-09 03:18:34 -07:00
|
|
|
static Ecore_List *idler_list = NULL;
|
|
|
|
|
|
|
|
typedef void (IdlerFunc) (void *data);
|
|
|
|
|
2008-03-23 04:54:24 -07:00
|
|
|
struct _idler {
|
2006-04-09 03:18:34 -07:00
|
|
|
IdlerFunc *func;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2006-04-16 06:27:38 -07:00
|
|
|
Idler *
|
2008-05-10 16:15:32 -07:00
|
|
|
IdlerAdd(IdlerFunc * func, void *data)
|
2006-04-09 03:18:34 -07:00
|
|
|
{
|
|
|
|
Idler *id;
|
|
|
|
|
2007-01-16 17:10:44 -08:00
|
|
|
id = EMALLOC(Idler, 1);
|
2006-04-09 03:18:34 -07:00
|
|
|
if (!id)
|
2006-04-16 06:27:38 -07:00
|
|
|
return NULL;
|
2006-04-09 03:18:34 -07:00
|
|
|
|
|
|
|
id->func = func;
|
|
|
|
id->data = data;
|
|
|
|
|
|
|
|
if (!idler_list)
|
|
|
|
idler_list = ecore_list_new();
|
|
|
|
|
|
|
|
ecore_list_append(idler_list, id);
|
2006-04-16 06:27:38 -07:00
|
|
|
|
|
|
|
return id;
|
2006-04-09 03:18:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IdlerDel(Idler * id)
|
|
|
|
{
|
2007-07-25 13:02:57 -07:00
|
|
|
ecore_list_node_remove(idler_list, id);
|
2006-04-09 03:18:34 -07:00
|
|
|
Efree(id);
|
|
|
|
}
|
|
|
|
|
2008-03-22 23:49:50 -07:00
|
|
|
static void
|
|
|
|
_IdlerRun(void *_id, void *prm __UNUSED__)
|
|
|
|
{
|
|
|
|
Idler *id = (Idler *) _id;
|
|
|
|
|
|
|
|
id->func(id->data);
|
|
|
|
}
|
|
|
|
|
2006-04-09 03:18:34 -07:00
|
|
|
void
|
|
|
|
IdlersRun(void)
|
|
|
|
{
|
2008-01-26 03:43:12 -08:00
|
|
|
if (EDebug(EDBUG_TYPE_IDLERS))
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s\n", __func__);
|
2007-01-28 17:26:12 -08:00
|
|
|
|
2008-03-22 23:49:50 -07:00
|
|
|
ecore_list_for_each(idler_list, _IdlerRun, NULL);
|
2006-04-09 03:18:34 -07:00
|
|
|
}
|
2007-01-27 20:59:46 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Animators
|
|
|
|
*/
|
|
|
|
#define DEBUG_ANIMATORS 0
|
|
|
|
static Ecore_List *animator_list = NULL;
|
2008-05-24 11:13:17 -07:00
|
|
|
static Timer *animator_timer = NULL;
|
2007-01-27 20:59:46 -08:00
|
|
|
|
|
|
|
typedef int (AnimatorFunc) (void *data);
|
|
|
|
|
2008-03-23 04:54:24 -07:00
|
|
|
struct _animator {
|
2007-01-27 20:59:46 -08:00
|
|
|
AnimatorFunc *func;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2008-03-22 23:49:50 -07:00
|
|
|
_AnimatorRun(void *_an, void *prm __UNUSED__)
|
2007-01-27 20:59:46 -08:00
|
|
|
{
|
2008-03-22 23:49:50 -07:00
|
|
|
Animator *an = (Animator *) _an;
|
2007-01-27 20:59:46 -08:00
|
|
|
int again;
|
|
|
|
|
2011-06-13 09:29:02 -07:00
|
|
|
#if DEBUG_ANIMATORS > 2
|
|
|
|
Eprintf("%s: %p\n", __func__, an);
|
2007-01-27 20:59:46 -08:00
|
|
|
#endif
|
2008-03-22 23:49:50 -07:00
|
|
|
again = an->func(an->data);
|
|
|
|
if (!again)
|
|
|
|
AnimatorDel(an);
|
|
|
|
}
|
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
static int
|
|
|
|
AnimatorsRun(void *data __UNUSED__)
|
2008-03-22 23:49:50 -07:00
|
|
|
{
|
2011-06-13 09:29:02 -07:00
|
|
|
static unsigned int tms_last = 0;
|
|
|
|
unsigned int dt_ms;
|
|
|
|
|
|
|
|
dt_ms = Mode.events.time_ms - tms_last;
|
|
|
|
if (2 * dt_ms < Conf.animation.step)
|
|
|
|
{
|
|
|
|
#if DEBUG_ANIMATORS > 1
|
|
|
|
Eprintf("%s: dt=%.3f - SKIP\n", __func__, 1e-3f * dt_ms);
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-03-22 23:49:50 -07:00
|
|
|
ecore_list_for_each(animator_list, _AnimatorRun, NULL);
|
2007-01-27 20:59:46 -08:00
|
|
|
|
2011-06-13 09:29:02 -07:00
|
|
|
#if DEBUG_ANIMATORS > 1
|
|
|
|
Eprintf("%s: dt=%.3f run=%.3f\n", __func__, 1e-3f * dt_ms,
|
|
|
|
1e-3f * (GetTimeMs() - Mode.events.time_ms));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tms_last = Mode.events.time_ms;
|
|
|
|
|
2007-07-25 13:02:57 -07:00
|
|
|
if (ecore_list_count(animator_list))
|
2008-05-24 11:13:17 -07:00
|
|
|
{
|
2011-01-05 10:05:52 -08:00
|
|
|
TimerSetInterval(animator_timer, Conf.animation.step);
|
2008-05-24 11:13:17 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
animator_timer = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-01-27 20:59:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Animator *
|
|
|
|
AnimatorAdd(AnimatorFunc * func, void *data)
|
|
|
|
{
|
|
|
|
Animator *an;
|
|
|
|
|
2007-02-01 16:19:32 -08:00
|
|
|
an = EMALLOC(Animator, 1);
|
2007-01-27 20:59:46 -08:00
|
|
|
if (!an)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
#if DEBUG_ANIMATORS
|
2011-06-13 09:29:02 -07:00
|
|
|
Eprintf("%s: %p func=%p data=%p\n", __func__, an, func, data);
|
2007-01-27 20:59:46 -08:00
|
|
|
#endif
|
|
|
|
an->func = func;
|
|
|
|
an->data = data;
|
|
|
|
|
|
|
|
if (!animator_list)
|
|
|
|
animator_list = ecore_list_new();
|
|
|
|
|
|
|
|
ecore_list_append(animator_list, an);
|
|
|
|
|
2007-07-25 13:02:57 -07:00
|
|
|
if (ecore_list_count(animator_list) == 1)
|
2007-01-27 20:59:46 -08:00
|
|
|
{
|
|
|
|
if (Conf.animation.step <= 0)
|
|
|
|
Conf.animation.step = 1;
|
|
|
|
/* Animator list was empty - Add to timer qeueue */
|
2011-01-05 10:05:57 -08:00
|
|
|
TIMER_ADD(animator_timer, Conf.animation.step, AnimatorsRun, NULL);
|
2007-01-27 20:59:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return an;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AnimatorDel(Animator * an)
|
|
|
|
{
|
|
|
|
#if DEBUG_ANIMATORS
|
2011-06-13 09:29:02 -07:00
|
|
|
Eprintf("%s: %p func=%p data=%p\n", __func__, an, an->func, an->data);
|
2007-01-27 20:59:46 -08:00
|
|
|
#endif
|
|
|
|
|
2007-07-25 13:02:57 -07:00
|
|
|
ecore_list_node_remove(animator_list, an);
|
2007-01-27 20:59:46 -08:00
|
|
|
Efree(an);
|
|
|
|
|
2007-07-25 13:02:57 -07:00
|
|
|
if (ecore_list_count(animator_list) == 0)
|
2007-01-27 20:59:46 -08:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
/* Animator list is empty - Remove from timer qeueue */
|
|
|
|
TIMER_DEL(animator_timer);
|
2007-01-27 20:59:46 -08:00
|
|
|
}
|
|
|
|
}
|