efl

Форк
0
/
ecore_timer_example.c 
189 строк · 4.6 Кб
1
//Compile with:
2
// gcc -o ecore_timer_example ecore_timer_example.c `pkg-config --libs --cflags ecore`
3

4
#include <Ecore.h>
5
#include <unistd.h>
6

7
#define TIMEOUT_1 1.0 // interval for timer1
8
#define TIMEOUT_2 3.0 // timer2 - delay timer1
9
#define TIMEOUT_3 8.2 // timer3 - pause timer1
10
#define TIMEOUT_4 11.0 // timer4 - resume timer1
11
#define TIMEOUT_5 14.0 // timer5 - change interval of timer1
12
#define TIMEOUT_6 18.0 // top timer1 and start timer7 and timer8 with changed precision
13
#define TIMEOUT_7 1.1 // interval for timer7
14
#define TIMEOUT_8 1.2 // interval for timer8
15
#define DELAY_1   3.0 // delay time for timer1 - used by timer2
16
#define INTERVAL1 2.0 // new interval for timer1 - used by timer5
17

18
static double _initial_time = 0;
19

20
struct context   // helper struct to give some context to the callbacks
21
{
22
   Ecore_Timer *timer1;
23
   Ecore_Timer *timer2;
24
   Ecore_Timer *timer3;
25
   Ecore_Timer *timer4;
26
   Ecore_Timer *timer5;
27
   Ecore_Timer *timer6;
28
   Ecore_Timer *timer7;
29
   Ecore_Timer *timer8;
30
};
31

32
static double
33
_get_current_time(void)
34
{
35
   return ecore_time_get() - _initial_time;
36
}
37

38
static Eina_Bool
39
_timer1_cb(void *data EINA_UNUSED)
40
{
41
   printf("Timer1 expired after %0.3f seconds.\n", _get_current_time());
42
   return ECORE_CALLBACK_RENEW;
43
}
44

45
static Eina_Bool
46
_timer2_cb(void *data)
47
{
48
   struct context *ctxt = data;
49
   printf("Timer2 expired after %0.3f seconds. "
50
          "Adding delay of %0.3f seconds to timer1.\n",
51
          _get_current_time(), DELAY_1);
52

53
   ecore_timer_delay(ctxt->timer1, DELAY_1);
54

55
   ctxt->timer2 = NULL;
56
   return ECORE_CALLBACK_CANCEL;
57
}
58

59
static Eina_Bool
60
_timer3_cb(void *data)
61
{
62
   struct context *ctxt = data;
63
   printf("Timer3 expired after %0.3f seconds. "
64
          "Freezing timer1.\n", _get_current_time());
65

66
   ecore_timer_freeze(ctxt->timer1);
67

68
   ctxt->timer3 = NULL;
69
   return ECORE_CALLBACK_CANCEL;
70
}
71

72
static Eina_Bool
73
_timer4_cb(void *data)
74
{
75
   struct context *ctxt = data;
76
   printf("Timer4 expired after %0.3f seconds. "
77
          "Resuming timer1, which has %0.3f seconds left to expire.\n",
78
          _get_current_time(), ecore_timer_pending_get(ctxt->timer1));
79

80
   ecore_timer_thaw(ctxt->timer1);
81

82
   ctxt->timer4 = NULL;
83
   return ECORE_CALLBACK_CANCEL;
84
}
85

86
static Eina_Bool
87
_timer5_cb(void *data)
88
{
89
   struct context *ctxt = data;
90
   double interval = ecore_timer_interval_get(ctxt->timer1);
91

92
   printf("Timer5 expired after %0.3f seconds. "
93
          "Changing interval of timer1 from %0.3f to %0.3f seconds.\n",
94
          _get_current_time(), interval, INTERVAL1);
95

96
   ecore_timer_interval_set(ctxt->timer1, INTERVAL1);
97

98
   ctxt->timer5 = NULL;
99
   return ECORE_CALLBACK_CANCEL;
100
}
101

102
static Eina_Bool
103
_timer7_cb(void *data)
104
{
105
   struct context *ctxt = data;
106
   printf("Timer7 expired after %0.3f seconds.\n", _get_current_time());
107

108
   ctxt->timer7 = NULL;
109
   return ECORE_CALLBACK_CANCEL;
110
}
111

112
static Eina_Bool
113
_timer8_cb(void *data)
114
{
115
   struct context *ctxt = data;
116
   printf("Timer8 expired after %0.3f seconds.\n", _get_current_time());
117

118
   ctxt->timer8 = NULL;
119
   return ECORE_CALLBACK_CANCEL;
120
}
121

122
static Eina_Bool
123
_timer6_cb(void *data)
124
{
125
   struct context *ctxt = data;
126
   printf("Timer6 expired after %0.3f seconds.\n", _get_current_time());
127

128
   printf("Stopping timer1.\n");
129

130
   ecore_timer_del(ctxt->timer1);
131
   ctxt->timer1 = NULL;
132

133
   printf("Starting timer7 (%0.3fs) and timer8 (%0.3fs).\n",
134
          TIMEOUT_7, TIMEOUT_8);
135

136
   ctxt->timer7 = ecore_timer_add(TIMEOUT_7, _timer7_cb, ctxt);
137
   ctxt->timer8 = ecore_timer_add(TIMEOUT_8, _timer8_cb, ctxt);
138

139
   ecore_timer_precision_set(0.2);
140

141
   ctxt->timer6 = NULL;
142
   return ECORE_CALLBACK_CANCEL;
143
}
144

145
int
146
main(void)
147
{
148
   struct context ctxt = {0};
149

150
   if (!ecore_init())
151
     {
152
        printf("ERROR: Cannot init Ecore!\n");
153
        return -1;
154
     }
155

156
   _initial_time = ecore_time_get();
157

158
   ctxt.timer1 = ecore_timer_add(TIMEOUT_1, _timer1_cb, &ctxt);
159
   ctxt.timer2 = ecore_timer_add(TIMEOUT_2, _timer2_cb, &ctxt);
160
   ctxt.timer3 = ecore_timer_add(TIMEOUT_3, _timer3_cb, &ctxt);
161
   ctxt.timer4 = ecore_timer_add(TIMEOUT_4, _timer4_cb, &ctxt);
162
   ctxt.timer5 = ecore_timer_add(TIMEOUT_5, _timer5_cb, &ctxt);
163
   ctxt.timer6 = ecore_timer_add(TIMEOUT_6, _timer6_cb, &ctxt);
164

165
   printf("start the main loop.\n");
166

167
   ecore_main_loop_begin();
168

169
   if (ctxt.timer1)
170
     ecore_timer_del(ctxt.timer1);
171
   if (ctxt.timer2)
172
     ecore_timer_del(ctxt.timer2);
173
   if (ctxt.timer3)
174
     ecore_timer_del(ctxt.timer3);
175
   if (ctxt.timer4)
176
     ecore_timer_del(ctxt.timer4);
177
   if (ctxt.timer5)
178
     ecore_timer_del(ctxt.timer5);
179
   if (ctxt.timer6)
180
     ecore_timer_del(ctxt.timer6);
181
   if (ctxt.timer7)
182
     ecore_timer_del(ctxt.timer7);
183
   if (ctxt.timer8)
184
     ecore_timer_del(ctxt.timer8);
185

186
   ecore_shutdown();
187

188
   return 0;
189
}
190

191

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.