summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore/ecore_anim.c
blob: b5cd5edfea79d511de5b1a202c68a27606bb3ff5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <math.h>

#include "Ecore.h"
#include "ecore_private.h"


struct _Ecore_Animator
{
   EINA_INLIST;
   ECORE_MAGIC;

   Ecore_Task_Cb func;
   void          *data;

   Eina_Bool     delete_me : 1;
   Eina_Bool	 suspended : 1;
};


static Eina_Bool _ecore_animator(void *data);

static Ecore_Timer    *timer = NULL;
static int             animators_delete_me = 0;
static Ecore_Animator *animators = NULL;
static double          animators_frametime = 1.0 / 30.0;

/**
 * Add a animator to tick off at every animaton tick during main loop execution.
 * @param func The function to call when it ticks off
 * @param data The data to pass to the function
 * @return A handle to the new animator
 * @ingroup Ecore_Animator_Group
 *
 * This function adds a animator and returns its handle on success and NULL on
 * failure. The function @p func will be called every N seconds where N is the
 * frametime interval set by ecore_animator_frametime_set(). The function will
 * be passed the @p data pointer as its parameter.
 *
 * When the animator @p func is called, it must return a value of either 1 or 0.
 * If it returns 1 (or ECORE_CALLBACK_RENEW), it will be called again at the
 * next tick, or if it returns 0 (or ECORE_CALLBACK_CANCEL) it will be deleted
 * automatically making any references/handles for it invalid.
 */
EAPI Ecore_Animator *
ecore_animator_add(Ecore_Task_Cb func, const void *data)
{
   Ecore_Animator *animator;

   if (!func) return NULL;
   animator = calloc(1, sizeof(Ecore_Animator));
   if (!animator) return NULL;
   ECORE_MAGIC_SET(animator, ECORE_MAGIC_ANIMATOR);
   animator->func = func;
   animator->data = (void *)data;
   animators = (Ecore_Animator *)eina_inlist_append(EINA_INLIST_GET(animators), EINA_INLIST_GET(animator));
   if (!timer)
     {
        double t_loop = ecore_loop_time_get();
        double sync_0 = 0.0;
        double d = -fmod(t_loop - sync_0, animators_frametime);

        timer = ecore_timer_loop_add(animators_frametime, _ecore_animator, NULL);
        ecore_timer_delay(timer, d);
     }
   return animator;
}

/**
 * Delete the specified animator from the animator list.
 * @param animator The animator to delete
 * @return The data pointer set for the animator
 * @ingroup Ecore_Animator_Group
 *
 * Delete the specified @p aqnimator from the set of animators that are executed
 * during main loop execution. This function returns the data parameter that
 * was being passed to the callback on success, or NULL on failure. After this
 * call returns the specified animator object @p animator is invalid and should not
 * be used again. It will not get called again after deletion.
 */
EAPI void *
ecore_animator_del(Ecore_Animator *animator)
{
   if (!ECORE_MAGIC_CHECK(animator, ECORE_MAGIC_ANIMATOR))
     {
	ECORE_MAGIC_FAIL(animator, ECORE_MAGIC_ANIMATOR,
			 "ecore_animator_del");
	return NULL;
     }
   if (animator->delete_me) return animator->data;
   animator->delete_me = EINA_TRUE;
   animators_delete_me++;
   return animator->data;
}

/**
 * Set the animator call interval in seconds.
 * @param frametime The time in seconds in between animator ticks.
 *
 * This function sets the time interval (in seconds) between animator ticks.
 */
EAPI void
ecore_animator_frametime_set(double frametime)
{
   if (frametime < 0.0) frametime = 0.0;
   if (animators_frametime == frametime) return;
   animators_frametime = frametime;
   if (timer)
     {
	ecore_timer_del(timer);
	timer = NULL;
     }
   if (animators)
     timer = ecore_timer_add(animators_frametime, _ecore_animator, NULL);
}

/**
 * Get the animator call interval in seconds.
 * @return The time in second in between animator ticks.
 *
 * this function retrieves the time between animator ticks, in seconds.
 */
EAPI double
ecore_animator_frametime_get(void)
{
   return animators_frametime;
}

/**
 * Suspend the specified animator.
 * @param animator The animator to delete
 * @ingroup Ecore_Animator_Group
 *
 * The specified @p animator will be temporarly removed from the set of animators
 * that are executed during main loop execution.
 */
EAPI void
ecore_animator_freeze(Ecore_Animator *animator)
{
   if (!ECORE_MAGIC_CHECK(animator, ECORE_MAGIC_ANIMATOR))
     {
	ECORE_MAGIC_FAIL(animator, ECORE_MAGIC_ANIMATOR,
			 "ecore_animator_del");
	return;
     }
   if (animator->delete_me) return;
   animator->suspended = EINA_TRUE;
}

/**
 * Restore execution of the specified animator.
 * @param animator The animator to delete
 * @ingroup Ecore_Animator_Group
 *
 * The specified @p animator will be put back in the set of animators
 * that are executed during main loop execution.
 */
EAPI void
ecore_animator_thaw(Ecore_Animator *animator)
{
   if (!ECORE_MAGIC_CHECK(animator, ECORE_MAGIC_ANIMATOR))
     {
	ECORE_MAGIC_FAIL(animator, ECORE_MAGIC_ANIMATOR,
			 "ecore_animator_del");
	return;
     }
   if (animator->delete_me) return;
   animator->suspended = EINA_FALSE;
}

void
_ecore_animator_shutdown(void)
{
   if (timer)
     {
	ecore_timer_del(timer);
	timer = NULL;
     }
   while (animators)
     {
	Ecore_Animator *animator;

	animator = animators;
	animators = (Ecore_Animator *) eina_inlist_remove(EINA_INLIST_GET(animators), EINA_INLIST_GET(animators));
	ECORE_MAGIC_SET(animator, ECORE_MAGIC_NONE);
	free(animator);
     }
}

static Eina_Bool
_ecore_animator(void *data __UNUSED__)
{
   Ecore_Animator *animator;

   EINA_INLIST_FOREACH(animators, animator)
     {
	if (!animator->delete_me && !animator->suspended)
	  {
	     if (!animator->func(animator->data))
	       {
		  animator->delete_me = EINA_TRUE;
		  animators_delete_me++;
	       }
	  }
     }
   if (animators_delete_me)
     {
	Ecore_Animator *l;
	for(l = animators; l;)
	  {
	     animator = l;
	     l = (Ecore_Animator *) EINA_INLIST_GET(l)->next;
	     if (animator->delete_me)
	       {
		  animators = (Ecore_Animator *) eina_inlist_remove(EINA_INLIST_GET(animators), EINA_INLIST_GET(animator));
		  ECORE_MAGIC_SET(animator, ECORE_MAGIC_NONE);
		  free(animator);
		  animators_delete_me--;
		  if (animators_delete_me == 0) break;
	       }
	  }
     }
   if (!animators)
     {
	timer = NULL;
	return ECORE_CALLBACK_CANCEL;
     }
   return ECORE_CALLBACK_RENEW;
}