Tizen 2.0 Release
[profile/ivi/ecore.git] / src / examples / ecore_timer_example.c
1 #include <Ecore.h>
2 #include <unistd.h>
3
4 #define TIMEOUT_1 1.0 // interval for timer1
5 #define TIMEOUT_2 3.0 // timer2 - delay timer1
6 #define TIMEOUT_3 8.2 // timer3 - pause timer1
7 #define TIMEOUT_4 11.0 // timer4 - resume timer1
8 #define TIMEOUT_5 14.0 // timer5 - change interval of timer1
9 #define TIMEOUT_6 18.0 // top timer1 and start timer7 and timer8 with changed precision
10 #define TIMEOUT_7 1.1 // interval for timer7
11 #define TIMEOUT_8 1.2 // interval for timer8
12 #define DELAY_1   3.0 // delay time for timer1 - used by timer2
13 #define INTERVAL1 2.0 // new interval for timer1 - used by timer5
14
15 static double _initial_time = 0;
16
17 struct context   // helper struct to give some context to the callbacks
18 {
19    Ecore_Timer *timer1;
20    Ecore_Timer *timer2;
21    Ecore_Timer *timer3;
22    Ecore_Timer *timer4;
23    Ecore_Timer *timer5;
24    Ecore_Timer *timer6;
25    Ecore_Timer *timer7;
26    Ecore_Timer *timer8;
27 };
28
29 static double
30 _get_current_time(void)
31 {
32    return ecore_time_get() - _initial_time;
33 }
34
35 static Eina_Bool
36 _timer1_cb(void *data)
37 {
38    printf("Timer1 expired after %0.3f seconds.\n", _get_current_time());
39    return ECORE_CALLBACK_RENEW;
40 }
41
42 static Eina_Bool
43 _timer2_cb(void *data)
44 {
45    struct context *ctxt = data;
46    printf("Timer2 expired after %0.3f seconds. "
47           "Adding delay of %0.3f seconds to timer1.\n",
48           _get_current_time(), DELAY_1);
49
50    ecore_timer_delay(ctxt->timer1, DELAY_1);
51
52    ctxt->timer2 = NULL;
53    return ECORE_CALLBACK_CANCEL;
54 }
55
56 static Eina_Bool
57 _timer3_cb(void *data)
58 {
59    struct context *ctxt = data;
60    printf("Timer3 expired after %0.3f seconds. "
61           "Freezing timer1.\n", _get_current_time());
62
63    ecore_timer_freeze(ctxt->timer1);
64
65    ctxt->timer3 = NULL;
66    return ECORE_CALLBACK_CANCEL;
67 }
68
69 static Eina_Bool
70 _timer4_cb(void *data)
71 {
72    struct context *ctxt = data;
73    printf("Timer4 expired after %0.3f seconds. "
74           "Resuming timer1, which has %0.3f seconds left to expire.\n",
75           _get_current_time(), ecore_timer_pending_get(ctxt->timer1));
76
77    ecore_timer_thaw(ctxt->timer1);
78
79    ctxt->timer4 = NULL;
80    return ECORE_CALLBACK_CANCEL;
81 }
82
83 static Eina_Bool
84 _timer5_cb(void *data)
85 {
86    struct context *ctxt = data;
87    double interval = ecore_timer_interval_get(ctxt->timer1);
88
89    printf("Timer5 expired after %0.3f seconds. "
90           "Changing interval of timer1 from %0.3f to %0.3f seconds.\n",
91           _get_current_time(), interval, INTERVAL1);
92
93    ecore_timer_interval_set(ctxt->timer1, INTERVAL1);
94
95    ctxt->timer5 = NULL;
96    return ECORE_CALLBACK_CANCEL;
97 }
98
99 static Eina_Bool
100 _timer7_cb(void *data)
101 {
102    struct context *ctxt = data;
103    printf("Timer7 expired after %0.3f seconds.\n", _get_current_time());
104
105    ctxt->timer7 = NULL;
106    return ECORE_CALLBACK_CANCEL;
107 }
108
109 static Eina_Bool
110 _timer8_cb(void *data)
111 {
112    struct context *ctxt = data;
113    printf("Timer8 expired after %0.3f seconds.\n", _get_current_time());
114
115    ctxt->timer8 = NULL;
116    return ECORE_CALLBACK_CANCEL;
117 }
118
119 static Eina_Bool
120 _timer6_cb(void *data)
121 {
122    struct context *ctxt = data;
123    printf("Timer6 expired after %0.3f seconds.\n", _get_current_time());
124
125    printf("Stopping timer1.\n");
126
127    ecore_timer_del(ctxt->timer1);
128    ctxt->timer1 = NULL;
129
130    printf("Starting timer7 (%0.3fs) and timer8 (%0.3fs).\n",
131           TIMEOUT_7, TIMEOUT_8);
132
133    ctxt->timer7 = ecore_timer_add(TIMEOUT_7, _timer7_cb, ctxt);
134    ctxt->timer8 = ecore_timer_add(TIMEOUT_8, _timer8_cb, ctxt);
135
136    ecore_timer_precision_set(0.2);
137
138    ctxt->timer6 = NULL;
139    return ECORE_CALLBACK_CANCEL;
140 }
141
142 int
143 main(int argc, char **argv)
144 {
145    struct context ctxt = {0};
146
147    if (!ecore_init())
148      {
149         printf("ERROR: Cannot init Ecore!\n");
150         return -1;
151      }
152
153    _initial_time = ecore_time_get();
154
155    ctxt.timer1 = ecore_timer_add(TIMEOUT_1, _timer1_cb, &ctxt);
156    ctxt.timer2 = ecore_timer_add(TIMEOUT_2, _timer2_cb, &ctxt);
157    ctxt.timer3 = ecore_timer_add(TIMEOUT_3, _timer3_cb, &ctxt);
158    ctxt.timer4 = ecore_timer_add(TIMEOUT_4, _timer4_cb, &ctxt);
159    ctxt.timer5 = ecore_timer_add(TIMEOUT_5, _timer5_cb, &ctxt);
160    ctxt.timer6 = ecore_timer_add(TIMEOUT_6, _timer6_cb, &ctxt);
161
162    printf("start the main loop.\n");
163
164    ecore_main_loop_begin();
165
166    if (ctxt.timer1)
167      ecore_timer_del(ctxt.timer1);
168    if (ctxt.timer2)
169      ecore_timer_del(ctxt.timer2);
170    if (ctxt.timer3)
171      ecore_timer_del(ctxt.timer3);
172    if (ctxt.timer4)
173      ecore_timer_del(ctxt.timer4);
174    if (ctxt.timer5)
175      ecore_timer_del(ctxt.timer5);
176    if (ctxt.timer6)
177      ecore_timer_del(ctxt.timer6);
178    if (ctxt.timer7)
179      ecore_timer_del(ctxt.timer7);
180    if (ctxt.timer8)
181      ecore_timer_del(ctxt.timer8);
182
183    ecore_shutdown();
184
185    return 0;
186 }
187