b9d4128a2ff78a2097750357cda74d683438c96d
[profile/ivi/ecore.git] / src / lib / ecore / ecore_idle_exiter.c
1 #include "ecore_private.h"
2 #include "Ecore.h"
3
4 static Ecore_Idle_Exiter *idle_exiters = NULL;
5 static int                idle_exiters_delete_me = 0;
6
7 /**
8  * Add an idle exiter handler.
9  * @param func The function to call when exiting an idle state.
10  * @param data The data to be passed to the @p func call
11  * @return A handle to the idle exiter callback on success.  NULL otherwise.
12  * @ingroup Idle_Group
13  */
14 EAPI Ecore_Idle_Exiter *
15 ecore_idle_exiter_add(int (*func) (void *data), const void *data)
16 {
17    Ecore_Idle_Exiter *ie;
18
19    if (!func) return NULL;
20    ie = calloc(1, sizeof(Ecore_Idle_Exiter));
21    if (!ie) return NULL;
22    ECORE_MAGIC_SET(ie, ECORE_MAGIC_IDLE_EXITER);
23    ie->func = func;
24    ie->data = (void *)data;
25    idle_exiters = _ecore_list2_append(idle_exiters, ie);
26    return ie;
27 }
28
29 /**
30  * Delete an idle exiter handler from the list to be run on exiting idle state.
31  * @param idle_exiter The idle exiter to delete
32  * @return The data pointer that was being being passed to the handler if
33  *         successful.  NULL otherwise.
34  * @ingroup Idle_Group
35  */
36 EAPI void *
37 ecore_idle_exiter_del(Ecore_Idle_Exiter *idle_exiter)
38 {
39    if (!ECORE_MAGIC_CHECK(idle_exiter, ECORE_MAGIC_IDLE_EXITER))
40      {
41         ECORE_MAGIC_FAIL(idle_exiter, ECORE_MAGIC_IDLE_EXITER,
42                          "ecore_idle_exiter_del");
43         return NULL;
44      }
45    idle_exiter->delete_me = 1;
46    idle_exiters_delete_me = 1;
47    return idle_exiter->data;
48 }
49
50 void
51 _ecore_idle_exiter_shutdown(void)
52 {
53    while (idle_exiters)
54      {
55         Ecore_Idle_Exiter *ie;
56         
57         ie = idle_exiters;
58         idle_exiters = _ecore_list2_remove(idle_exiters, ie);
59         ECORE_MAGIC_SET(ie, ECORE_MAGIC_NONE);
60         free(ie);
61      }
62    idle_exiters_delete_me = 0;
63 }
64
65 void
66 _ecore_idle_exiter_call(void)
67 {
68    Ecore_List2 *l;
69    
70    for (l = (Ecore_List2 *)idle_exiters; l; l = l->next)
71      {
72         Ecore_Idle_Exiter *ie;
73         
74         ie = (Ecore_Idle_Exiter *)l;
75         if (!ie->delete_me)
76           {
77              if (!ie->func(ie->data)) ecore_idle_exiter_del(ie);
78           }
79      }
80    if (idle_exiters_delete_me)
81      {
82         for (l = (Ecore_List2 *)idle_exiters; l;)
83           {
84              Ecore_Idle_Exiter *ie;
85              
86              ie = (Ecore_Idle_Exiter *)l;
87              l = l->next;
88              if (ie->delete_me)
89                {
90                   idle_exiters = _ecore_list2_remove(idle_exiters, ie);
91                   ECORE_MAGIC_SET(ie, ECORE_MAGIC_NONE);
92                   free(ie);
93                }
94           }
95         idle_exiters_delete_me = 0;
96      }
97 }
98
99 int
100 _ecore_idle_exiter_exist(void)
101 {
102    if (idle_exiters) return 1;
103    return 0;
104 }