blob: ed65739a248e9b010fd02dc5a768644b5948a946 [file] [log] [blame]
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020025#include <stdlib.h>
26#include <string.h>
27#include <stdio.h>
28#include <math.h>
29
30#include <unistd.h>
31#include <fcntl.h>
32
33#include "compositor.h"
34
35WL_EXPORT void
36weston_spring_init(struct weston_spring *spring,
37 double k, double current, double target)
38{
39 spring->k = k;
40 spring->friction = 400.0;
41 spring->current = current;
42 spring->previous = current;
43 spring->target = target;
44}
45
46WL_EXPORT void
47weston_spring_update(struct weston_spring *spring, uint32_t msec)
48{
49 double force, v, current, step;
50
51 /* Limit the number of executions of the loop below by ensuring that
52 * the timestamp for last update of the spring is no more than 1s ago.
53 * This handles the case where time moves backwards or forwards in
54 * large jumps.
55 */
56 if (msec - spring->timestamp > 1000) {
57 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
58 spring->timestamp, msec);
59 spring->timestamp = msec - 1000;
60 }
61
62 step = 0.01;
63 while (4 < msec - spring->timestamp) {
64 current = spring->current;
65 v = current - spring->previous;
66 force = spring->k * (spring->target - current) / 10.0 +
67 (spring->previous - current) - v * spring->friction;
68
69 spring->current =
70 current + (current - spring->previous) +
71 force * step * step;
72 spring->previous = current;
73
74#if 0
75 if (spring->current >= 1.0) {
76#ifdef TWEENER_BOUNCE
77 spring->current = 2.0 - spring->current;
78 spring->previous = 2.0 - spring->previous;
79#else
80 spring->current = 1.0;
81 spring->previous = 1.0;
82#endif
83 }
84
85 if (spring->current <= 0.0) {
86 spring->current = 0.0;
87 spring->previous = 0.0;
88 }
89#endif
90 spring->timestamp += 4;
91 }
92}
93
94WL_EXPORT int
95weston_spring_done(struct weston_spring *spring)
96{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -040097 return fabs(spring->previous - spring->target) < 0.002 &&
98 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020099}
100
101typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation);
102
103struct weston_surface_animation {
104 struct weston_surface *surface;
105 struct weston_animation animation;
106 struct weston_spring spring;
107 struct weston_transform transform;
108 struct wl_listener listener;
109 float start, stop;
110 weston_surface_animation_frame_func_t frame;
111 weston_surface_animation_done_func_t done;
112 void *data;
113};
114
115static void
116weston_surface_animation_destroy(struct weston_surface_animation *animation)
117{
118 wl_list_remove(&animation->animation.link);
119 wl_list_remove(&animation->listener.link);
120 wl_list_remove(&animation->transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200121 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200122 if (animation->done)
123 animation->done(animation, animation->data);
124 free(animation);
125}
126
127static void
128handle_animation_surface_destroy(struct wl_listener *listener, void *data)
129{
130 struct weston_surface_animation *animation =
131 container_of(listener,
132 struct weston_surface_animation, listener);
133
134 weston_surface_animation_destroy(animation);
135}
136
137static void
138weston_surface_animation_frame(struct weston_animation *base,
139 struct weston_output *output, uint32_t msecs)
140{
141 struct weston_surface_animation *animation =
142 container_of(base,
143 struct weston_surface_animation, animation);
144
145 if (base->frame_counter <= 1)
146 animation->spring.timestamp = msecs;
147
148 weston_spring_update(&animation->spring, msecs);
149
150 if (weston_spring_done(&animation->spring)) {
151 weston_surface_animation_destroy(animation);
152 return;
153 }
154
155 if (animation->frame)
156 animation->frame(animation);
157
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200158 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200159 weston_compositor_schedule_repaint(animation->surface->compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200160}
161
162static struct weston_surface_animation *
163weston_surface_animation_run(struct weston_surface *surface,
164 float start, float stop,
165 weston_surface_animation_frame_func_t frame,
166 weston_surface_animation_done_func_t done,
167 void *data)
168{
169 struct weston_surface_animation *animation;
170
171 animation = malloc(sizeof *animation);
172 if (!animation)
173 return NULL;
174
175 animation->surface = surface;
176 animation->frame = frame;
177 animation->done = done;
178 animation->data = data;
179 animation->start = start;
180 animation->stop = stop;
181 weston_matrix_init(&animation->transform.matrix);
182 wl_list_insert(&surface->geometry.transformation_list,
183 &animation->transform.link);
184 weston_spring_init(&animation->spring, 200.0, 0.0, 1.0);
185 animation->spring.friction = 700;
186 animation->animation.frame_counter = 0;
187 animation->animation.frame = weston_surface_animation_frame;
188 weston_surface_animation_frame(&animation->animation, NULL, 0);
189
190 animation->listener.notify = handle_animation_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500191 wl_signal_add(&surface->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200192
193 wl_list_insert(&surface->output->animation_list,
194 &animation->animation.link);
195
196 return animation;
197}
198
199static void
200zoom_frame(struct weston_surface_animation *animation)
201{
202 struct weston_surface *es = animation->surface;
203 float scale;
204
205 scale = animation->start +
206 (animation->stop - animation->start) *
207 animation->spring.current;
208 weston_matrix_init(&animation->transform.matrix);
209 weston_matrix_translate(&animation->transform.matrix,
210 -0.5f * es->geometry.width,
211 -0.5f * es->geometry.height, 0);
212 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
213 weston_matrix_translate(&animation->transform.matrix,
214 0.5f * es->geometry.width,
215 0.5f * es->geometry.height, 0);
216
217 es->alpha = animation->spring.current;
218 if (es->alpha > 1.0)
219 es->alpha = 1.0;
220}
221
222WL_EXPORT struct weston_surface_animation *
223weston_zoom_run(struct weston_surface *surface, float start, float stop,
224 weston_surface_animation_done_func_t done, void *data)
225{
226 return weston_surface_animation_run(surface, start, stop,
227 zoom_frame, done, data);
228}
229
230static void
231fade_frame(struct weston_surface_animation *animation)
232{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200233 if (animation->spring.current > 0.999)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200234 animation->surface->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200235 else if (animation->spring.current < 0.001 )
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200236 animation->surface->alpha = 0;
237 else
238 animation->surface->alpha = animation->spring.current;
239}
240
241WL_EXPORT struct weston_surface_animation *
242weston_fade_run(struct weston_surface *surface,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200243 float start, float end, float k,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200244 weston_surface_animation_done_func_t done, void *data)
245{
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200246 struct weston_surface_animation *fade;
247
248 fade = weston_surface_animation_run(surface, 0, 0,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200249 fade_frame, done, data);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200250
251 weston_spring_init(&fade->spring, k, start, end);
252 surface->alpha = start;
253
254 return fade;
255}
256
257WL_EXPORT void
258weston_fade_update(struct weston_surface_animation *fade,
259 float start, float end, float k)
260{
261 weston_spring_init(&fade->spring, k, start, end);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200262}
263
264static void
265slide_frame(struct weston_surface_animation *animation)
266{
267 float scale;
268
269 scale = animation->start +
270 (animation->stop - animation->start) *
271 animation->spring.current;
272 weston_matrix_init(&animation->transform.matrix);
273 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
274}
275
276WL_EXPORT struct weston_surface_animation *
277weston_slide_run(struct weston_surface *surface, float start, float stop,
278 weston_surface_animation_done_func_t done, void *data)
279{
280 struct weston_surface_animation *animation;
281
282 animation = weston_surface_animation_run(surface, start, stop,
283 slide_frame, done, data);
284 if (!animation)
285 return NULL;
286
287 animation->spring.friction = 900;
288 animation->spring.k = 300;
289
290 return animation;
291}