This example shows how to setup timer callbacks. It starts a timer that will tick (expire) every 1 second, and then setup other timers that will expire only once, but each of them will affect the first timer still executing with a different API, to demonstrate its usage. To see the full code for this example, click here.
To demonstrate this, let's define some constants that will determine at which time each timer will expire:
#include <Ecore.h>
#include <unistd.h>
#define TIMEOUT_1 1.0
#define TIMEOUT_2 3.0
#define TIMEOUT_3 8.2
#define TIMEOUT_4 11.0
#define TIMEOUT_5 14.0
#define TIMEOUT_6 18.0
#define TIMEOUT_7 1.1
#define TIMEOUT_8 1.2
#define DELAY_1 3.0
#define INTERVAL1 2.0
These constants should tell by themselves what will be the behavior of the program, but I'll explain it anyway. The first timer is set to tick every 1 second, but all the other timers until the 6th one will be started concurrently at the beginning of the program. Each of them will expire at the specified time in these constants:
- The timer2, after 3 seconds of the program being executed, will add a delay of 3 seconds to timer1;
- The timer3 will pause timer1 at 8.2 seconds;
- timer4 will resume timer1 at 11.0 seconds;
- timer5 will will change the interval of timer1 to 2 seconds;
- timer6 will stop timer1 and start timer7 and timer8, with 1.1 and 1.2 seconds of interval, respectively; it also sets the precision to 0.2 seconds;
- timer7 and timer8 will just print their expiration time.
static double _initial_time = 0;
struct context
{
};
static double
_get_current_time(void)
{
double ecore_time_get(void)
Retrieves the current system time as a floating point value in seconds.
Definition: ecore_time.c:33
Eo Ecore_Timer
A handle for timers.
Definition: Ecore_Common.h:3079
}
As almost all the other examples, we create a context structure to pass to our callbacks, so they can have access to the other timers. We also store the time of the program start in _initial_time
, and use the function _get_current_time
to retrieve the current time relative to that time. This will help demonstrate what is going on.
Now, the behavior and relationship between the timers that was described above is dictated by the following timer callbacks:
{
printf("Timer1 expired after %0.3f seconds.\n", _get_current_time());
}
_timer2_cb(void *data)
{
struct context *ctxt = data;
printf("Timer2 expired after %0.3f seconds. "
"Adding delay of %0.3f seconds to timer1.\n",
_get_current_time(), DELAY_1);
ctxt->timer2 = NULL;
}
_timer3_cb(void *data)
{
struct context *ctxt = data;
printf("Timer3 expired after %0.3f seconds. "
"Freezing timer1.\n", _get_current_time());
ctxt->timer3 = NULL;
}
_timer4_cb(void *data)
{
struct context *ctxt = data;
printf("Timer4 expired after %0.3f seconds. "
"Resuming timer1, which has %0.3f seconds left to expire.\n",
ctxt->timer4 = NULL;
}
_timer5_cb(void *data)
{
struct context *ctxt = data;
printf("Timer5 expired after %0.3f seconds. "
"Changing interval of timer1 from %0.3f to %0.3f seconds.\n",
_get_current_time(), interval, INTERVAL1);
ctxt->timer5 = NULL;
}
_timer7_cb(void *data)
{
struct context *ctxt = data;
printf("Timer7 expired after %0.3f seconds.\n", _get_current_time());
ctxt->timer7 = NULL;
}
_timer8_cb(void *data)
{
struct context *ctxt = data;
printf("Timer8 expired after %0.3f seconds.\n", _get_current_time());
ctxt->timer8 = NULL;
}
_timer6_cb(void *data)
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition: Ecore_Common.h:153
#define ECORE_CALLBACK_CANCEL
Return value to remove a callback.
Definition: Ecore_Common.h:152
double ecore_timer_interval_get(const Efl_Loop_Timer *obj)
Interval the timer ticks on.
Definition: efl_loop_timer_eo.legacy.c:9
void ecore_timer_interval_set(Efl_Loop_Timer *obj, double in)
Interval the timer ticks on.
Definition: efl_loop_timer_eo.legacy.c:3
void ecore_timer_delay(Efl_Loop_Timer *obj, double add)
Adds a delay to the next occurrence of a timer.
Definition: efl_loop_timer_eo.legacy.c:33
void ecore_timer_freeze(Ecore_Timer *timer)
Pauses a running timer.
Definition: ecore_timer.c:334
void ecore_timer_thaw(Ecore_Timer *timer)
Resumes a frozen (paused) timer.
Definition: ecore_timer.c:377
double ecore_timer_pending_get(const Efl_Loop_Timer *obj)
Pending time regarding a timer.
Definition: efl_loop_timer_eo.legacy.c:15
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
{
struct context *ctxt = data;
printf("Timer6 expired after %0.3f seconds.\n", _get_current_time());
printf("Stopping timer1.\n");
ctxt->timer1 = NULL;
printf("Starting timer7 (%0.3fs) and timer8 (%0.3fs).\n",
TIMEOUT_7, TIMEOUT_8);
ctxt->timer6 = NULL;
}
void * ecore_timer_del(Ecore_Timer *timer)
Deletes the specified timer from the timer list.
Definition: ecore_timer.c:238
Ecore_Timer * ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
Creates a timer to call the given function in the given period of time.
Definition: ecore_timer.c:189
void ecore_timer_precision_set(double precision)
Sets the precision to be used by timer infrastructure.
Definition: ecore_timer.c:76
It's possible to see the same behavior as other Ecore callbacks here, returning ECORE_CALLBACK_RENEW when the timer needs to continue ticking, and ECORE_CALLBACK_CANCEL when it needs to stop its execution. Also notice that later on our program we are checking for the timers pointers in the context to see if they are still executing before deleting them, so we need to set these timer pointers to NULL
when we are returning ECORE_CALLBACK_CANCEL. Otherwise the pointer would still be not NULL
, but pointing to something that is invalid, since the timer would have already expired without renewing.
Now the main code, which will start the timers:
int
main(void)
{
struct context ctxt = {0};
{
printf("ERROR: Cannot init Ecore!\n");
return -1;
}
printf("start the main loop.\n");
if (ctxt.timer1)
if (ctxt.timer2)
if (ctxt.timer3)
if (ctxt.timer4)
if (ctxt.timer5)
if (ctxt.timer6)
if (ctxt.timer7)
if (ctxt.timer8)
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
return 0;
}
This code is very simple. Just after starting the library, it will save the current time to _initial_time
, start all timers from 1 to 6, and begin the main loop. Everything should be running right now, displaying the time which each timer is expiring, and what it is doing to affect the other timers.
After returning from the main loop, every timer is checked to see if it's still alive and, in that case, deleted, before finalizing the library. This is not really necessary, since ecore_shutdown() will already delete them for you, but it's good practice if you have other things going on after this point that could restart the main loop.