9 #include "ecore_private.h"
12 struct _Ecore_Animator
20 Eina_Bool delete_me : 1;
21 Eina_Bool suspended : 1;
25 static Eina_Bool _ecore_animator(void *data);
27 static Ecore_Timer *timer = NULL;
28 static int animators_delete_me = 0;
29 static Ecore_Animator *animators = NULL;
30 static double animators_frametime = 1.0 / 30.0;
33 * Add a animator to tick off at every animaton tick during main loop execution.
34 * @param func The function to call when it ticks off
35 * @param data The data to pass to the function
36 * @return A handle to the new animator
37 * @ingroup Ecore_Animator_Group
39 * This function adds a animator and returns its handle on success and NULL on
40 * failure. The function @p func will be called every N seconds where N is the
41 * frametime interval set by ecore_animator_frametime_set(). The function will
42 * be passed the @p data pointer as its parameter.
44 * When the animator @p func is called, it must return a value of either 1 or 0.
45 * If it returns 1 (or ECORE_CALLBACK_RENEW), it will be called again at the
46 * next tick, or if it returns 0 (or ECORE_CALLBACK_CANCEL) it will be deleted
47 * automatically making any references/handles for it invalid.
50 ecore_animator_add(Ecore_Task_Cb func, const void *data)
52 Ecore_Animator *animator;
54 if (!func) return NULL;
55 animator = calloc(1, sizeof(Ecore_Animator));
56 if (!animator) return NULL;
57 ECORE_MAGIC_SET(animator, ECORE_MAGIC_ANIMATOR);
58 animator->func = func;
59 animator->data = (void *)data;
60 animators = (Ecore_Animator *)eina_inlist_append(EINA_INLIST_GET(animators), EINA_INLIST_GET(animator));
63 double t_loop = ecore_loop_time_get();
65 double d = -fmod(t_loop - sync_0, animators_frametime);
67 timer = ecore_timer_loop_add(animators_frametime, _ecore_animator, NULL);
68 ecore_timer_delay(timer, d);
74 * Delete the specified animator from the animator list.
75 * @param animator The animator to delete
76 * @return The data pointer set for the animator
77 * @ingroup Ecore_Animator_Group
79 * Delete the specified @p aqnimator from the set of animators that are executed
80 * during main loop execution. This function returns the data parameter that
81 * was being passed to the callback on success, or NULL on failure. After this
82 * call returns the specified animator object @p animator is invalid and should not
83 * be used again. It will not get called again after deletion.
86 ecore_animator_del(Ecore_Animator *animator)
88 if (!ECORE_MAGIC_CHECK(animator, ECORE_MAGIC_ANIMATOR))
90 ECORE_MAGIC_FAIL(animator, ECORE_MAGIC_ANIMATOR,
91 "ecore_animator_del");
94 if (animator->delete_me) return animator->data;
95 animator->delete_me = EINA_TRUE;
96 animators_delete_me++;
97 return animator->data;
101 * Set the animator call interval in seconds.
102 * @param frametime The time in seconds in between animator ticks.
104 * This function sets the time interval (in seconds) inbetween animator ticks.
107 ecore_animator_frametime_set(double frametime)
109 if (frametime < 0.0) frametime = 0.0;
110 if (animators_frametime == frametime) return;
111 animators_frametime = frametime;
114 ecore_timer_del(timer);
118 timer = ecore_timer_add(animators_frametime, _ecore_animator, NULL);
122 * Get the animator call interval in seconds.
123 * @return The time in second in between animator ticks.
125 * this function retrieves the time inbetween animator ticks, in seconds.
128 ecore_animator_frametime_get(void)
130 return animators_frametime;
134 * Suspend the specified animator.
135 * @param animator The animator to delete
136 * @ingroup Ecore_Animator_Group
138 * The specified @p animator will be temporarly removed from the set of animators
139 * that are executed during main loop execution.
142 ecore_animator_freeze(Ecore_Animator *animator)
144 if (!ECORE_MAGIC_CHECK(animator, ECORE_MAGIC_ANIMATOR))
146 ECORE_MAGIC_FAIL(animator, ECORE_MAGIC_ANIMATOR,
147 "ecore_animator_del");
150 if (animator->delete_me) return;
151 animator->suspended = EINA_TRUE;
155 * Restore execution of the specified animator.
156 * @param animator The animator to delete
157 * @ingroup Ecore_Animator_Group
159 * The specified @p animator will be put back in the set of animators
160 * that are executed during main loop execution.
163 ecore_animator_thaw(Ecore_Animator *animator)
165 if (!ECORE_MAGIC_CHECK(animator, ECORE_MAGIC_ANIMATOR))
167 ECORE_MAGIC_FAIL(animator, ECORE_MAGIC_ANIMATOR,
168 "ecore_animator_del");
171 if (animator->delete_me) return;
172 animator->suspended = EINA_FALSE;
176 _ecore_animator_shutdown(void)
180 ecore_timer_del(timer);
185 Ecore_Animator *animator;
187 animator = animators;
188 animators = (Ecore_Animator *) eina_inlist_remove(EINA_INLIST_GET(animators), EINA_INLIST_GET(animators));
189 ECORE_MAGIC_SET(animator, ECORE_MAGIC_NONE);
195 _ecore_animator(void *data __UNUSED__)
197 Ecore_Animator *animator;
199 EINA_INLIST_FOREACH(animators, animator)
201 if (!animator->delete_me && !animator->suspended)
203 if (!animator->func(animator->data))
205 animator->delete_me = EINA_TRUE;
206 animators_delete_me++;
210 if (animators_delete_me)
213 for(l = animators; l;)
216 l = (Ecore_Animator *) EINA_INLIST_GET(l)->next;
217 if (animator->delete_me)
219 animators = (Ecore_Animator *) eina_inlist_remove(EINA_INLIST_GET(animators), EINA_INLIST_GET(animator));
220 ECORE_MAGIC_SET(animator, ECORE_MAGIC_NONE);
222 animators_delete_me--;
223 if (animators_delete_me == 0) break;
230 return ECORE_CALLBACK_CANCEL;
232 return ECORE_CALLBACK_RENEW;