* Here is a page with some Ecore examples explained:
*
* @li @ref ecore_time_example_c
+ * @li @ref ecore_timer_example_c
* @li @ref ecore_idler_example_c
* @li @ref ecore_job_example_c
* @li @ref ecore_event_example_c
+ * @li @ref ecore_fd_handler_example_c
*
*/
*/
/**
+ * @page ecore_timer_example_c ecore timers - Timed events
+ * @dontinclude ecore_timer_example.c
+ *
+ * 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 firts timer still executing with
+ * a different API, to demonstrate its usage. To see the full code for this
+ * example, click @ref ecore_timer_example.c "here".
+ *
+ * To demonstrate this, let's define some constants that will determine at which
+ * time each timer will expire:
+ *
+ * @until INTERVAL1
+ *
+ * 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:
+ *
+ * @li The timer2, after 3 seconds of the program being executed, will add a delay
+ * of 3 seconds to timer1;
+ * @li The timer3 will pause timer1 at 8.2 seconds;
+ * @li timer4 will resume timer1 at 11.0 seconds;
+ * @li timer5 will will change the interval of timer1 to 2 seconds;
+ * @li 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;
+ * @li timer7 and timer8 will just print their expiration time.
+ *
+ * @until ecore_time_get
+ * @until }
+ *
+ * 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 @c _initial_time, and use the function
+ * @c _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:
+ *
+ * @until _timer6_cb
+ * @until }
+ *
+ * It's possible to see the same behavior as other Ecore callbacks here,
+ * returning @ref ECORE_CALLBACK_RENEW when the timer needs to continue ticking,
+ * and @ref 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 @c NULL when we are returning @ref
+ * ECORE_CALLBACK_CANCEL. Otherwise the pointer would still be not @c 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:
+ *
+ * @until ecore_shutdown
+ * @until }
+ *
+ * This code is very simple. Just after starting the library, it will save the
+ * current time to @c _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.
+ *
+ */
+
+/**
* @page ecore_idler_example_c ecore idle state - Idlers, enterers and exiters
*
* This example demonstrates how to manage the idle state of the main loop. Once
*/
/**
- * @page ecore_event_example_c ecore fd handlers - Monitoring file descriptors
+ * @page ecore_fd_handler_example_c ecore fd handlers - Monitoring file descriptors
* @dontinclude ecore_fd_handler_example.c
*
* This is a very simple example where we will start monitoring the stdin of the
* program and, whenever there's something to be read, we call our callback that
* will read it.
*
+ * Check the full code for this example @ref ecore_fd_handler_example.c "here".
+ *
* This seems to be stupid, since a similar result could be achieved by the
* following code:
*
*/
/**
+ * @example ecore_timer_example.c
+ * This example show how to use timers to have timed events inside ecore.
+ * See @ref ecore_timer_example_c "the example explained".
+ */
+
+/**
* @example ecore_fd_handler_example.c
* This example shows how to setup and use an fd_handler. See
* @ref ecore_fd_handler_example_c "the explanation here".
--- /dev/null
+#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)
+{
+ printf("Timer1 expired after %0.3f seconds.\n", _get_current_time());
+ return ECORE_CALLBACK_RENEW;
+}
+
+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;
+ return ECORE_CALLBACK_CANCEL;
+}
+
+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;
+ return ECORE_CALLBACK_CANCEL;
+}
+
+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;
+ return ECORE_CALLBACK_CANCEL;
+}
+
+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;
+ return ECORE_CALLBACK_CANCEL;
+}
+
+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;
+ return ECORE_CALLBACK_CANCEL;
+}
+
+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;
+ return ECORE_CALLBACK_CANCEL;
+}
+
+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);
+
+ ecore_timer_precision_set(0.2);
+
+ ctxt->timer6 = NULL;
+ return ECORE_CALLBACK_CANCEL;
+}
+
+int main(int argc, char **argv)
+{
+ 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");
+
+ ecore_main_loop_begin();
+
+ 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);
+
+ ecore_shutdown();
+
+ return 0;
+}