ff2dd7ece5176cdec94973912fb30575cb7a0c57
[framework/uifw/ecore.git] / src / lib / ecore / ecore_idler.c
1 #include "ecore_private.h"
2 #include "Ecore.h"
3
4 static Ecore_Idler *idlers = NULL;
5 static int          idlers_delete_me = 0;
6
7 /**
8  * Add an idler handler.
9  * @param  func The function to call when idling.
10  * @param  data The data to be passed to this @p func call.
11  * @return A idler handle if successfully added.  NULL otherwise.
12  * @ingroup Idle_Group
13  *
14  * Add an idler handle to the event loop, returning a handle on success and
15  * NULL otherwise.  The function @p func will be called repeatedly while
16  * no other events are ready to be processed, as long as it returns 1 
17  * (or ECORE_CALLBACK_RENEW). A return of 0 (or ECORE_CALLBACK_CANCEL) deletes 
18  * the idler.
19  *
20  * Idlers are useful for progressively prossessing data without blocking.
21  */
22 EAPI Ecore_Idler *
23 ecore_idler_add(int (*func) (void *data), const void *data)
24 {
25    Ecore_Idler *ie;
26
27    if (!func) return NULL;
28    ie = calloc(1, sizeof(Ecore_Idler));
29    if (!ie) return NULL;
30    ECORE_MAGIC_SET(ie, ECORE_MAGIC_IDLER);
31    ie->func = func;
32    ie->data = (void *)data;
33    idlers = _ecore_list2_append(idlers, ie);
34    return ie;
35 }
36
37 /**
38  * Delete an idler callback from the list to be executed.
39  * @param  idler The handle of the idler callback to delete
40  * @return The data pointer passed to the idler callback on success.  NULL
41  *         otherwise.
42  * @ingroup Idle_Group
43  */
44 EAPI void *
45 ecore_idler_del(Ecore_Idler *idler)
46 {
47    if (!ECORE_MAGIC_CHECK(idler, ECORE_MAGIC_IDLER))
48      {
49         ECORE_MAGIC_FAIL(idler, ECORE_MAGIC_IDLER,
50                          "ecore_idler_del");
51         return NULL;
52      }
53    idler->delete_me = 1;
54    idlers_delete_me = 1;
55    return idler->data;
56 }
57
58 void
59 _ecore_idler_shutdown(void)
60 {
61    while (idlers)
62      {
63         Ecore_Idler *ie;
64         
65         ie = idlers;
66         idlers = _ecore_list2_remove(idlers, ie);
67         ECORE_MAGIC_SET(ie, ECORE_MAGIC_NONE);
68         free(ie);
69      }
70    idlers_delete_me = 0;
71 }
72
73 int
74 _ecore_idler_call(void)
75 {
76    Ecore_List2 *l;
77
78    for (l = (Ecore_List2 *)idlers; l; l = l->next)
79      {
80         Ecore_Idler *ie;
81         
82         ie = (Ecore_Idler *)l;
83         if (!ie->delete_me)
84           {
85              if (!ie->func(ie->data)) ecore_idler_del(ie);
86           }
87      }
88    if (idlers_delete_me)
89      {
90         for (l = (Ecore_List2 *)idlers; l;)
91           {
92              Ecore_Idler *ie;
93              
94              ie = (Ecore_Idler *)l;
95              l = l->next;
96              if (ie->delete_me)
97                {
98                   idlers = _ecore_list2_remove(idlers, ie);
99                   ECORE_MAGIC_SET(ie, ECORE_MAGIC_NONE);
100                   free(ie);
101                }
102           }
103         idlers_delete_me = 0;
104      }
105    if (idlers) return 1;
106    return 0;
107 }
108
109 int
110 _ecore_idler_exist(void)
111 {
112    if (idlers) return 1;
113    return 0;
114 }