summaryrefslogtreecommitdiff
path: root/src/lib/ecore/ecore_idle_enterer.c
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2012-12-02 22:35:45 +0000
committerVincent Torri <vincent.torri@gmail.com>2012-12-02 22:35:45 +0000
commit7d6010b12c47a20e492da808e3192c3f87dab619 (patch)
tree26c6fd189e046a76560c0bc740b85f4d767ae399 /src/lib/ecore/ecore_idle_enterer.c
parent53fc441d5475155965d92da89502fe4634a561b2 (diff)
merge: add escape ecore, fix several bugs
SVN revision: 79995
Diffstat (limited to 'src/lib/ecore/ecore_idle_enterer.c')
-rw-r--r--src/lib/ecore/ecore_idle_enterer.c313
1 files changed, 313 insertions, 0 deletions
diff --git a/src/lib/ecore/ecore_idle_enterer.c b/src/lib/ecore/ecore_idle_enterer.c
new file mode 100644
index 0000000..e7d10e5
--- /dev/null
+++ b/src/lib/ecore/ecore_idle_enterer.c
@@ -0,0 +1,313 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <stdlib.h>
6
7#include <Eo.h>
8
9#include "Ecore.h"
10#include "ecore_private.h"
11
12#define MY_CLASS ECORE_IDLE_ENTERER_CLASS
13
14#define MY_CLASS_NAME "ecore_idle_enterer"
15
16EAPI Eo_Op ECORE_IDLE_ENTERER_BASE_ID = EO_NOOP;
17
18struct _Ecore_Idle_Enterer_Private_Data
19{
20 EINA_INLIST;
21 Ecore_Idle_Enterer *obj;
22 Ecore_Task_Cb func;
23 void *data;
24 int references;
25 Eina_Bool delete_me : 1;
26};
27typedef struct _Ecore_Idle_Enterer_Private_Data Ecore_Idle_Enterer_Private_Data;
28
29static Ecore_Idle_Enterer_Private_Data *idle_enterers = NULL;
30static Ecore_Idle_Enterer_Private_Data *idle_enterer_current = NULL;
31static int idle_enterers_delete_me = 0;
32
33static void *
34_ecore_idle_enterer_del(Ecore_Idle_Enterer *idle_enterer);
35
36/**
37 * @addtogroup Ecore_Idle_Group
38 *
39 * @{
40 */
41
42static Eina_Bool
43_ecore_idle_enterer_add(Ecore_Idle_Enterer *obj,
44 Ecore_Idle_Enterer_Private_Data *ie,
45 Ecore_Task_Cb func,
46 const void *data)
47{
48 if (EINA_UNLIKELY(!eina_main_loop_is()))
49 {
50 eo_error_set(obj);
51 EINA_MAIN_LOOP_CHECK_RETURN_VAL(EINA_FALSE);
52 }
53
54 ie->obj = obj;
55 eo_do_super(obj, eo_constructor());
56 eo_manual_free_set(obj, EINA_TRUE);
57
58 if (!func)
59 {
60 eo_error_set(obj);
61 ERR("callback function must be set up for an object of class: '%s'", MY_CLASS_NAME);
62 return EINA_FALSE;
63 }
64
65 ie->func = func;
66 ie->data = (void *)data;
67 return EINA_TRUE;
68}
69
70/**
71 * Add an idle enterer handler.
72 * @param func The function to call when entering an idle state.
73 * @param data The data to be passed to the @p func call
74 * @return A handle to the idle enterer callback if successful. Otherwise,
75 * NULL is returned.
76 * @note The function func will be called every time the main loop is entering
77 * idle state, as long as it returns 1 (or ECORE_CALLBACK_RENEW). A return of 0
78 * (or ECORE_CALLBACK_CANCEL) deletes the idle enterer.
79 */
80EAPI Ecore_Idle_Enterer *
81ecore_idle_enterer_add(Ecore_Task_Cb func,
82 const void *data)
83{
84 Ecore_Idle_Enterer *ie = NULL;
85 ie = eo_add_custom(MY_CLASS, _ecore_parent, ecore_idle_enterer_after_constructor(func, data));
86 eo_unref(ie);
87 return ie;
88}
89
90static void
91_idle_enterer_after_constructor(Eo *obj, void *_pd, va_list *list)
92{
93 Ecore_Task_Cb func = va_arg(*list, Ecore_Task_Cb);
94 const void *data = va_arg(*list, const void *);
95
96 _ecore_lock();
97 Ecore_Idle_Enterer_Private_Data *ie = _pd;
98 if (!_ecore_idle_enterer_add(obj, ie, func, data)) goto unlock;
99
100 idle_enterers = (Ecore_Idle_Enterer_Private_Data *)eina_inlist_append(EINA_INLIST_GET(idle_enterers), EINA_INLIST_GET(ie));
101
102unlock:
103 _ecore_unlock();
104}
105
106/**
107 * Add an idle enterer handler at the start of the list so it gets called earlier than others.
108 * @param func The function to call when entering an idle state.
109 * @param data The data to be passed to the @p func call
110 * @return A handle to the idle enterer callback if successful. Otherwise,
111 * NULL is returned.
112 * @note The function func will be called every time the main loop is entering
113 * idle state, as long as it returns 1 (or ECORE_CALLBACK_RENEW). A return of 0
114 * (or ECORE_CALLBACK_CANCEL) deletes the idle enterer.
115 */
116EAPI Ecore_Idle_Enterer *
117ecore_idle_enterer_before_add(Ecore_Task_Cb func,
118 const void *data)
119{
120 Ecore_Idle_Enterer *ie = NULL;
121 ie = eo_add_custom(MY_CLASS, _ecore_parent, ecore_idle_enterer_before_constructor(func, data));
122 eo_unref(ie);
123 return ie;
124}
125
126static void
127_idle_enterer_before_constructor(Eo *obj, void *_pd, va_list *list)
128{
129 Ecore_Task_Cb func = va_arg(*list, Ecore_Task_Cb);
130 const void *data = va_arg(*list, const void *);
131
132 _ecore_lock();
133 Ecore_Idle_Enterer_Private_Data *ie = _pd;
134 if (!_ecore_idle_enterer_add(obj, ie, func, data)) goto unlock;
135
136 idle_enterers = (Ecore_Idle_Enterer_Private_Data *)eina_inlist_prepend(EINA_INLIST_GET(idle_enterers), EINA_INLIST_GET(ie));
137
138unlock:
139 _ecore_unlock();
140}
141
142static void
143_constructor(Eo *obj, void *_pd EINA_UNUSED, va_list *list EINA_UNUSED)
144{
145 eo_error_set(obj);
146 ERR("only custom constructor can be used with '%s' class", MY_CLASS_NAME);
147}
148
149/**
150 * Delete an idle enterer callback.
151 * @param idle_enterer The idle enterer to delete
152 * @return The data pointer passed to the idler enterer callback on success.
153 * NULL otherwise.
154 */
155EAPI void *
156ecore_idle_enterer_del(Ecore_Idle_Enterer *idle_enterer)
157{
158 void *data = NULL;
159
160 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
161
162 _ecore_lock();
163 data = _ecore_idle_enterer_del(idle_enterer);
164 _ecore_unlock();
165 return data;
166}
167
168/**
169 * @}
170 */
171
172
173static void *
174_ecore_idle_enterer_del(Ecore_Idle_Enterer *obj)
175{
176 Ecore_Idle_Enterer_Private_Data *idle_enterer = eo_data_get(obj, MY_CLASS);
177
178 EINA_SAFETY_ON_TRUE_RETURN_VAL(idle_enterer->delete_me, NULL);
179 idle_enterer->delete_me = 1;
180 idle_enterers_delete_me = 1;
181 return idle_enterer->data;
182}
183
184static void
185_destructor(Eo *obj, void *_pd, va_list *list EINA_UNUSED)
186{
187 Ecore_Idle_Enterer_Private_Data *idle_enterer = _pd;
188
189 idle_enterer->delete_me = 1;
190 idle_enterers_delete_me = 1;
191
192 eo_do_super(obj, eo_destructor());
193}
194
195void
196_ecore_idle_enterer_shutdown(void)
197{
198 Ecore_Idle_Enterer_Private_Data *ie;
199 while ((ie = idle_enterers))
200 {
201 idle_enterers = (Ecore_Idle_Enterer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(idle_enterers), EINA_INLIST_GET(idle_enterers));
202
203 eo_parent_set(ie->obj, NULL);
204 if (eo_destructed_is(ie->obj))
205 eo_manual_free(ie->obj);
206 else
207 eo_manual_free_set(ie->obj, EINA_FALSE);
208 }
209 idle_enterers_delete_me = 0;
210 idle_enterer_current = NULL;
211}
212
213void
214_ecore_idle_enterer_call(void)
215{
216 if (!idle_enterer_current)
217 {
218 /* regular main loop, start from head */
219 idle_enterer_current = idle_enterers;
220 }
221 else
222 {
223 /* recursive main loop, continue from where we were */
224 idle_enterer_current =
225 (Ecore_Idle_Enterer_Private_Data *)EINA_INLIST_GET(idle_enterer_current)->next;
226 }
227
228 while (idle_enterer_current)
229 {
230 Ecore_Idle_Enterer_Private_Data *ie = (Ecore_Idle_Enterer_Private_Data *)idle_enterer_current;
231 if (!ie->delete_me)
232 {
233 ie->references++;
234 if (!_ecore_call_task_cb(ie->func, ie->data))
235 {
236 if (!ie->delete_me) _ecore_idle_enterer_del(ie->obj);
237 }
238 ie->references--;
239 }
240 if (idle_enterer_current) /* may have changed in recursive main loops */
241 idle_enterer_current =
242 (Ecore_Idle_Enterer_Private_Data *)EINA_INLIST_GET(idle_enterer_current)->next;
243 }
244 if (idle_enterers_delete_me)
245 {
246 Ecore_Idle_Enterer_Private_Data *l;
247 int deleted_idler_enterers_in_use = 0;
248
249 for (l = idle_enterers; l; )
250 {
251 Ecore_Idle_Enterer_Private_Data *ie = l;
252 l = (Ecore_Idle_Enterer_Private_Data *)EINA_INLIST_GET(l)->next;
253 if (ie->delete_me)
254 {
255 if (ie->references)
256 {
257 deleted_idler_enterers_in_use++;
258 continue;
259 }
260
261 idle_enterers = (Ecore_Idle_Enterer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(idle_enterers), EINA_INLIST_GET(ie));
262
263 eo_parent_set(ie->obj, NULL);
264 if (eo_destructed_is(ie->obj))
265 eo_manual_free(ie->obj);
266 else
267 eo_manual_free_set(ie->obj, EINA_FALSE);
268 }
269 }
270 if (!deleted_idler_enterers_in_use)
271 idle_enterers_delete_me = 0;
272 }
273}
274
275int
276_ecore_idle_enterer_exist(void)
277{
278 if (idle_enterers) return 1;
279 return 0;
280}
281
282static void
283_class_constructor(Eo_Class *klass)
284{
285 const Eo_Op_Func_Description func_desc[] = {
286 EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor),
287 EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_DESTRUCTOR), _destructor),
288
289 EO_OP_FUNC(ECORE_IDLE_ENTERER_ID(ECORE_IDLE_ENTERER_SUB_ID_AFTER_CONSTRUCTOR), _idle_enterer_after_constructor),
290 EO_OP_FUNC(ECORE_IDLE_ENTERER_ID(ECORE_IDLE_ENTERER_SUB_ID_BEFORE_CONSTRUCTOR), _idle_enterer_before_constructor),
291 EO_OP_FUNC_SENTINEL
292 };
293
294 eo_class_funcs_set(klass, func_desc);
295}
296
297static const Eo_Op_Description op_desc[] = {
298 EO_OP_DESCRIPTION(ECORE_IDLE_ENTERER_SUB_ID_AFTER_CONSTRUCTOR, "Add an idle enterer handler."),
299 EO_OP_DESCRIPTION(ECORE_IDLE_ENTERER_SUB_ID_BEFORE_CONSTRUCTOR, "Add an idle enterer handler at the start of the list so it gets called earlier than others."),
300 EO_OP_DESCRIPTION_SENTINEL
301};
302static const Eo_Class_Description class_desc = {
303 EO_VERSION,
304 MY_CLASS_NAME,
305 EO_CLASS_TYPE_REGULAR,
306 EO_CLASS_DESCRIPTION_OPS(&ECORE_IDLE_ENTERER_BASE_ID, op_desc, ECORE_IDLE_ENTERER_SUB_ID_LAST),
307 NULL,
308 sizeof(Ecore_Idle_Enterer_Private_Data),
309 _class_constructor,
310 NULL
311};
312
313EO_DEFINE_CLASS(ecore_idle_enterer_class_get, &class_desc, EO_BASE_CLASS, NULL)