7c9d03fca31693576e4bc5f420512e5acf79ed5b
[framework/uifw/ecore.git] / src / lib / ecore / ecore_idle_enterer.c
1 #include "ecore_private.h"
2 #include "Ecore.h"
3
4 static Ecore_Idle_Enterer *idle_enterers = NULL;
5 static int                 idle_enterers_delete_me = 0;
6
7 /**
8  * Add an idle enterer handler.
9  * @param   func The function to call when entering an idle state.
10  * @param   data The data to be passed to the @p func call
11  * @return  A handle to the idle enterer callback if successful.  Otherwise,
12  *          NULL is returned.
13  * @ingroup Idle_Group
14  */
15 EAPI Ecore_Idle_Enterer *
16 ecore_idle_enterer_add(int (*func) (void *data), const void *data)
17 {
18    Ecore_Idle_Enterer *ie;
19
20    if (!func) return NULL;
21    ie = calloc(1, sizeof(Ecore_Idle_Enterer));
22    if (!ie) return NULL;
23    ECORE_MAGIC_SET(ie, ECORE_MAGIC_IDLE_ENTERER);
24    ie->func = func;
25    ie->data = (void *)data;
26    idle_enterers = _ecore_list2_append(idle_enterers, ie);
27    return ie;
28 }
29
30 /**
31  * Add an idle enterer handler at the start of the list so it gets called earlier than others.
32  * @param   func The function to call when entering an idle state.
33  * @param   data The data to be passed to the @p func call
34  * @return  A handle to the idle enterer callback if successful.  Otherwise,
35  *          NULL is returned.
36  * @ingroup Idle_Group
37  */
38 EAPI Ecore_Idle_Enterer *
39 ecore_idle_enterer_before_add(int (*func) (void *data), const void *data)
40 {
41    Ecore_Idle_Enterer *ie;
42
43    if (!func) return NULL;
44    ie = calloc(1, sizeof(Ecore_Idle_Enterer));
45    if (!ie) return NULL;
46    ECORE_MAGIC_SET(ie, ECORE_MAGIC_IDLE_ENTERER);
47    ie->func = func;
48    ie->data = (void *)data;
49    idle_enterers = _ecore_list2_prepend(idle_enterers, ie);
50    return ie;
51 }
52
53 /**
54  * Delete an idle enterer callback.
55  * @param   idle_enterer The idle enterer to delete
56  * @return  The data pointer passed to the idler enterer callback on success.
57  *          NULL otherwise.
58  * @ingroup Idle_Group
59  */
60 EAPI void *
61 ecore_idle_enterer_del(Ecore_Idle_Enterer *idle_enterer)
62 {
63    if (!ECORE_MAGIC_CHECK(idle_enterer, ECORE_MAGIC_IDLE_ENTERER))
64      {
65         ECORE_MAGIC_FAIL(idle_enterer, ECORE_MAGIC_IDLE_ENTERER,
66                          "ecore_idle_enterer_del");
67         return NULL;
68      }
69    idle_enterer->delete_me = 1;
70    idle_enterers_delete_me = 1;
71    return idle_enterer->data;
72 }
73
74 void
75 _ecore_idle_enterer_shutdown(void)
76 {
77    while (idle_enterers)
78      {
79         Ecore_Idle_Enterer *ie;
80         
81         ie = idle_enterers;
82         idle_enterers = _ecore_list2_remove(idle_enterers, ie);
83         ECORE_MAGIC_SET(ie, ECORE_MAGIC_NONE);
84         free(ie);
85      }
86    idle_enterers_delete_me = 0;
87 }
88
89 void
90 _ecore_idle_enterer_call(void)
91 {
92    Ecore_List2 *l;
93    
94    for (l = (Ecore_List2 *)idle_enterers; l; l = l->next)
95      {
96         Ecore_Idle_Enterer *ie;
97         
98         ie = (Ecore_Idle_Enterer *)l;
99         if (!ie->delete_me)
100           {
101              if (!ie->func(ie->data)) ecore_idle_enterer_del(ie);
102           }
103      }
104    if (idle_enterers_delete_me)
105      {
106         for (l = (Ecore_List2 *)idle_enterers; l;)
107           {
108              Ecore_Idle_Enterer *ie;
109              
110              ie = (Ecore_Idle_Enterer *)l;
111              l = l->next;
112              if (ie->delete_me)
113                {
114                   idle_enterers = _ecore_list2_remove(idle_enterers, ie);
115                   ECORE_MAGIC_SET(ie, ECORE_MAGIC_NONE);
116                   free(ie);
117                }
118           }
119         idle_enterers_delete_me = 0;
120      }
121 }
122
123 int
124 _ecore_idle_enterer_exist(void)
125 {
126    if (idle_enterers) return 1;
127    return 0;
128 }