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