ecore_timer_example.c

This example shows how to use timers to have timed events inside ecore.

This example shows how to use timers to have timed events inside ecore.See the example explained.

//Compile with:
// gcc -o ecore_timer_example ecore_timer_example.c `pkg-config --libs --cflags ecore`
#include <Ecore.h>
#include <unistd.h>
#define TIMEOUT_1 1.0 // interval for timer1
#define TIMEOUT_2 3.0 // timer2 - delay timer1
#define TIMEOUT_3 8.2 // timer3 - pause timer1
#define TIMEOUT_4 11.0 // timer4 - resume timer1
#define TIMEOUT_5 14.0 // timer5 - change interval of timer1
#define TIMEOUT_6 18.0 // top timer1 and start timer7 and timer8 with changed precision
#define TIMEOUT_7 1.1 // interval for timer7
#define TIMEOUT_8 1.2 // interval for timer8
#define DELAY_1 3.0 // delay time for timer1 - used by timer2
#define INTERVAL1 2.0 // new interval for timer1 - used by timer5
static double _initial_time = 0;
struct context // helper struct to give some context to the callbacks
{
Ecore_Timer *timer1;
Ecore_Timer *timer2;
Ecore_Timer *timer3;
Ecore_Timer *timer4;
Ecore_Timer *timer5;
Ecore_Timer *timer6;
Ecore_Timer *timer7;
Ecore_Timer *timer8;
};
static double
_get_current_time(void)
{
return ecore_time_get() - _initial_time;
}
static Eina_Bool
_timer1_cb(void *data EINA_UNUSED)
{
printf("Timer1 expired after %0.3f seconds.\n", _get_current_time());
}
static Eina_Bool
_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);
ecore_timer_delay(ctxt->timer1, DELAY_1);
ctxt->timer2 = NULL;
}
static Eina_Bool
_timer3_cb(void *data)
{
struct context *ctxt = data;
printf("Timer3 expired after %0.3f seconds. "
"Freezing timer1.\n", _get_current_time());
ecore_timer_freeze(ctxt->timer1);
ctxt->timer3 = NULL;
}
static Eina_Bool
_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",
_get_current_time(), ecore_timer_pending_get(ctxt->timer1));
ecore_timer_thaw(ctxt->timer1);
ctxt->timer4 = NULL;
}
static Eina_Bool
_timer5_cb(void *data)
{
struct context *ctxt = data;
double interval = ecore_timer_interval_get(ctxt->timer1);
printf("Timer5 expired after %0.3f seconds. "
"Changing interval of timer1 from %0.3f to %0.3f seconds.\n",
_get_current_time(), interval, INTERVAL1);
ecore_timer_interval_set(ctxt->timer1, INTERVAL1);
ctxt->timer5 = NULL;
}
static Eina_Bool
_timer7_cb(void *data)
{
struct context *ctxt = data;
printf("Timer7 expired after %0.3f seconds.\n", _get_current_time());
ctxt->timer7 = NULL;
}
static Eina_Bool
_timer8_cb(void *data)
{
struct context *ctxt = data;
printf("Timer8 expired after %0.3f seconds.\n", _get_current_time());
ctxt->timer8 = NULL;
}
static Eina_Bool
_timer6_cb(void *data)
{
struct context *ctxt = data;
printf("Timer6 expired after %0.3f seconds.\n", _get_current_time());
printf("Stopping timer1.\n");
ecore_timer_del(ctxt->timer1);
ctxt->timer1 = NULL;
printf("Starting timer7 (%0.3fs) and timer8 (%0.3fs).\n",
TIMEOUT_7, TIMEOUT_8);
ctxt->timer7 = ecore_timer_add(TIMEOUT_7, _timer7_cb, ctxt);
ctxt->timer8 = ecore_timer_add(TIMEOUT_8, _timer8_cb, ctxt);
ctxt->timer6 = NULL;
}
int
main(void)
{
struct context ctxt = {0};
if (!ecore_init())
{
printf("ERROR: Cannot init Ecore!\n");
return -1;
}
_initial_time = ecore_time_get();
ctxt.timer1 = ecore_timer_add(TIMEOUT_1, _timer1_cb, &ctxt);
ctxt.timer2 = ecore_timer_add(TIMEOUT_2, _timer2_cb, &ctxt);
ctxt.timer3 = ecore_timer_add(TIMEOUT_3, _timer3_cb, &ctxt);
ctxt.timer4 = ecore_timer_add(TIMEOUT_4, _timer4_cb, &ctxt);
ctxt.timer5 = ecore_timer_add(TIMEOUT_5, _timer5_cb, &ctxt);
ctxt.timer6 = ecore_timer_add(TIMEOUT_6, _timer6_cb, &ctxt);
printf("start the main loop.\n");
if (ctxt.timer1)
ecore_timer_del(ctxt.timer1);
if (ctxt.timer2)
ecore_timer_del(ctxt.timer2);
if (ctxt.timer3)
ecore_timer_del(ctxt.timer3);
if (ctxt.timer4)
ecore_timer_del(ctxt.timer4);
if (ctxt.timer5)
ecore_timer_del(ctxt.timer5);
if (ctxt.timer6)
ecore_timer_del(ctxt.timer6);
if (ctxt.timer7)
ecore_timer_del(ctxt.timer7);
if (ctxt.timer8)
ecore_timer_del(ctxt.timer8);
return 0;
}
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
#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
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
double ecore_time_get(void)
Retrieves the current system time as a floating point value in seconds.
Definition: ecore_time.c:33
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_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_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
void ecore_timer_precision_set(double precision)
Sets the precision to be used by timer infrastructure.
Definition: ecore_timer.c:76
Eo Ecore_Timer
A handle for timers.
Definition: Ecore_Common.h:3079
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