[UI Sample] Timer Sample Overview
The Timer sample demonstrates how to use the Ecore timer and adjust the specific timers in the timer list.
The sample uses Ecore timer functions, such as ecore_timer_add() for creating a timer to call the given function in the given period of time, ecore_timer_del() to delete the specific timer from the timer list, ecore_timer_freeze() and ecore_timer_thaw() for freezing and thawing the specific timer, and ecore_timer_interval_set() for changing the interval time to the given timer.
The following log shows the result of the Timer sample.
I/USR_TAG ( 2652): start the main loop. I/USR_TAG ( 2652): Timer1 expired after 1.001 seconds. I/USR_TAG ( 2652): Timer1 expired after 2.001 seconds. I/USR_TAG ( 2652): Timer1 expired after 3.001 seconds. I/USR_TAG ( 2652): Timer2 expired after 3.001 seconds. I/USR_TAG ( 2652): Adding delay of 3.000 seconds to timer1. I/USR_TAG ( 2652): Timer1 expired after 7.004 seconds. I/USR_TAG ( 2652): Timer1 expired after 8.001 seconds. I/USR_TAG ( 2652): Timer3 expired after 8.201 seconds. I/USR_TAG ( 2652): Freezing timer1. I/USR_TAG ( 2652): Timer4 expired after 11.003 seconds. I/USR_TAG ( 2652): Resuming timer1, which has 0.799 seconds left to expire. I/USR_TAG ( 2652): Timer1 expired after 11.804 seconds. I/USR_TAG ( 2652): Timer1 expired after 12.804 seconds. I/USR_TAG ( 2652): Timer1 expired after 13.804 seconds. I/USR_TAG ( 2652): Timer5 expired after 14.001 seconds. I/USR_TAG ( 2652): Changing interval of timer1 from 1.000 to 2.000 seconds. I/USR_TAG ( 2652): Timer1 expired after 14.804 seconds. I/USR_TAG ( 2652): Timer1 expired after 16.805 seconds. I/USR_TAG ( 2652): Timer6 expired after 18.002 seconds. I/USR_TAG ( 2652): Stopping timer1. I/USR_TAG ( 2652): Starting timer7 (1.100s) and timer8 (1.200s). I/USR_TAG ( 2652): Timer7 expired after 19.203 seconds. I/USR_TAG ( 2652): Timer8 expired after 19.203 seconds.
Implementation
The create_base_gui() function creates the window that does not do anything in this sample application. However, it contains timer functions and you can see the result in the log output.
#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 { 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) { dlog_print(DLOG_INFO, "USR_TAG", "Timer1 expired after %0.3f seconds.", _get_current_time()); return ECORE_CALLBACK_RENEW; } static Eina_Bool _timer2_cb(void *data EINA_UNUSED) { struct context *ctxt = data; dlog_print(DLOG_INFO, "USR_TAG", "Timer2 expired after %0.3f seconds.", _get_current_time()); dlog_print(DLOG_INFO, "USR_TAG", "Adding delay of %0.3f seconds to timer1.", 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; dlog_print(DLOG_INFO, "USR_TAG", "Timer3 expired after %0.3f seconds.", _get_current_time()); dlog_print(DLOG_INFO, "USR_TAG", "Freezing timer1."); ecore_timer_freeze(ctxt->timer1); ctxt->timer3 = NULL; return ECORE_CALLBACK_CANCEL; } static Eina_Bool _timer4_cb(void *data) { struct context *ctxt = data; dlog_print(DLOG_INFO, "USR_TAG", "Timer4 expired after %0.3f seconds.", _get_current_time()); dlog_print(DLOG_INFO, "USR_TAG", "Resuming timer1, which has %0.3f seconds left to expire.", 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); dlog_print(DLOG_INFO, "USR_TAG", "Timer5 expired after %0.3f seconds.", _get_current_time()); dlog_print(DLOG_INFO, "USR_TAG", "Changing interval of timer1 from %0.3f to %0.3f seconds.", 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; dlog_print(DLOG_INFO, "USR_TAG", "Timer7 expired after %0.3f seconds.", _get_current_time()); ctxt->timer7 = NULL; return ECORE_CALLBACK_CANCEL; } static Eina_Bool _timer8_cb(void *data) { struct context *ctxt = data; dlog_print(DLOG_INFO, "USR_TAG", "Timer8 expired after %0.3f seconds.", _get_current_time()); ctxt->timer8 = NULL; return ECORE_CALLBACK_CANCEL; } static Eina_Bool _timer6_cb(void *data) { struct context *ctxt = data; dlog_print(DLOG_INFO, "USR_TAG", "Timer6 expired after %0.3f seconds.", _get_current_time()); dlog_print(DLOG_INFO, "USR_TAG", "Stopping timer1."); ecore_timer_del(ctxt->timer1); ctxt->timer1 = NULL; dlog_print(DLOG_INFO, "USR_TAG", "Starting timer7 (%0.3fs) and timer8 (%0.3fs).", 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; } struct context ctxt = {0}; static void create_base_gui(appdata_s *ad) { // Window ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE); elm_win_autodel_set(ad->win, EINA_TRUE); if (elm_win_wm_rotation_supported_get(ad->win)) { int rots[4] = { 0, 90, 180, 270 }; elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4); } evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL); eext_object_event_callback_add(ad->win, EEXT_CALLBACK_BACK, win_back_cb, ad); // Conformant ad->conform = elm_conformant_add(ad->win); elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW); elm_win_indicator_opacity_set(ad->win, ELM_WIN_INDICATOR_OPAQUE); evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); elm_win_resize_object_add(ad->win, ad->conform); evas_object_show(ad->conform); // Timer _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); dlog_print(DLOG_INFO, "USR_TAG", "start the main loop."); // Show the window after the base GUI is set up evas_object_show(ad->win); } static void app_terminate(void *data) { // Release all resources 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); }