blob: e947d723c6832e1379993faadb4980abd6512ae9 [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
23#include <stdlib.h>
24#include <string.h>
25#include <stdio.h>
26#include <math.h>
27
28#include <unistd.h>
29#include <fcntl.h>
30
31#include "compositor.h"
32
33WL_EXPORT void
34weston_spring_init(struct weston_spring *spring,
35 double k, double current, double target)
36{
37 spring->k = k;
38 spring->friction = 400.0;
39 spring->current = current;
40 spring->previous = current;
41 spring->target = target;
42}
43
44WL_EXPORT void
45weston_spring_update(struct weston_spring *spring, uint32_t msec)
46{
47 double force, v, current, step;
48
49 /* Limit the number of executions of the loop below by ensuring that
50 * the timestamp for last update of the spring is no more than 1s ago.
51 * This handles the case where time moves backwards or forwards in
52 * large jumps.
53 */
54 if (msec - spring->timestamp > 1000) {
55 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
56 spring->timestamp, msec);
57 spring->timestamp = msec - 1000;
58 }
59
60 step = 0.01;
61 while (4 < msec - spring->timestamp) {
62 current = spring->current;
63 v = current - spring->previous;
64 force = spring->k * (spring->target - current) / 10.0 +
65 (spring->previous - current) - v * spring->friction;
66
67 spring->current =
68 current + (current - spring->previous) +
69 force * step * step;
70 spring->previous = current;
71
72#if 0
73 if (spring->current >= 1.0) {
74#ifdef TWEENER_BOUNCE
75 spring->current = 2.0 - spring->current;
76 spring->previous = 2.0 - spring->previous;
77#else
78 spring->current = 1.0;
79 spring->previous = 1.0;
80#endif
81 }
82
83 if (spring->current <= 0.0) {
84 spring->current = 0.0;
85 spring->previous = 0.0;
86 }
87#endif
88 spring->timestamp += 4;
89 }
90}
91
92WL_EXPORT int
93weston_spring_done(struct weston_spring *spring)
94{
95 return fabs(spring->previous - spring->target) < 0.0002 &&
96 fabs(spring->current - spring->target) < 0.0002;
97}
98
99typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation);
100
101struct weston_surface_animation {
102 struct weston_surface *surface;
103 struct weston_animation animation;
104 struct weston_spring spring;
105 struct weston_transform transform;
106 struct wl_listener listener;
107 float start, stop;
108 weston_surface_animation_frame_func_t frame;
109 weston_surface_animation_done_func_t done;
110 void *data;
111};
112
113static void
114weston_surface_animation_destroy(struct weston_surface_animation *animation)
115{
116 wl_list_remove(&animation->animation.link);
117 wl_list_remove(&animation->listener.link);
118 wl_list_remove(&animation->transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200119 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200120 if (animation->done)
121 animation->done(animation, animation->data);
122 free(animation);
123}
124
125static void
126handle_animation_surface_destroy(struct wl_listener *listener, void *data)
127{
128 struct weston_surface_animation *animation =
129 container_of(listener,
130 struct weston_surface_animation, listener);
131
132 weston_surface_animation_destroy(animation);
133}
134
135static void
136weston_surface_animation_frame(struct weston_animation *base,
137 struct weston_output *output, uint32_t msecs)
138{
139 struct weston_surface_animation *animation =
140 container_of(base,
141 struct weston_surface_animation, animation);
142
143 if (base->frame_counter <= 1)
144 animation->spring.timestamp = msecs;
145
146 weston_spring_update(&animation->spring, msecs);
147
148 if (weston_spring_done(&animation->spring)) {
149 weston_surface_animation_destroy(animation);
150 return;
151 }
152
153 if (animation->frame)
154 animation->frame(animation);
155
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200156 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200157 weston_compositor_schedule_repaint(animation->surface->compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200158}
159
160static struct weston_surface_animation *
161weston_surface_animation_run(struct weston_surface *surface,
162 float start, float stop,
163 weston_surface_animation_frame_func_t frame,
164 weston_surface_animation_done_func_t done,
165 void *data)
166{
167 struct weston_surface_animation *animation;
168
169 animation = malloc(sizeof *animation);
170 if (!animation)
171 return NULL;
172
173 animation->surface = surface;
174 animation->frame = frame;
175 animation->done = done;
176 animation->data = data;
177 animation->start = start;
178 animation->stop = stop;
179 weston_matrix_init(&animation->transform.matrix);
180 wl_list_insert(&surface->geometry.transformation_list,
181 &animation->transform.link);
182 weston_spring_init(&animation->spring, 200.0, 0.0, 1.0);
183 animation->spring.friction = 700;
184 animation->animation.frame_counter = 0;
185 animation->animation.frame = weston_surface_animation_frame;
186 weston_surface_animation_frame(&animation->animation, NULL, 0);
187
188 animation->listener.notify = handle_animation_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400189 wl_signal_add(&surface->resource.destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200190
191 wl_list_insert(&surface->output->animation_list,
192 &animation->animation.link);
193
194 return animation;
195}
196
197static void
198zoom_frame(struct weston_surface_animation *animation)
199{
200 struct weston_surface *es = animation->surface;
201 float scale;
202
203 scale = animation->start +
204 (animation->stop - animation->start) *
205 animation->spring.current;
206 weston_matrix_init(&animation->transform.matrix);
207 weston_matrix_translate(&animation->transform.matrix,
208 -0.5f * es->geometry.width,
209 -0.5f * es->geometry.height, 0);
210 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
211 weston_matrix_translate(&animation->transform.matrix,
212 0.5f * es->geometry.width,
213 0.5f * es->geometry.height, 0);
214
215 es->alpha = animation->spring.current;
216 if (es->alpha > 1.0)
217 es->alpha = 1.0;
218}
219
220WL_EXPORT struct weston_surface_animation *
221weston_zoom_run(struct weston_surface *surface, float start, float stop,
222 weston_surface_animation_done_func_t done, void *data)
223{
224 return weston_surface_animation_run(surface, start, stop,
225 zoom_frame, done, data);
226}
227
228static void
229fade_frame(struct weston_surface_animation *animation)
230{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200231 if (animation->spring.current > 0.999)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200232 animation->surface->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200233 else if (animation->spring.current < 0.001 )
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200234 animation->surface->alpha = 0;
235 else
236 animation->surface->alpha = animation->spring.current;
237}
238
239WL_EXPORT struct weston_surface_animation *
240weston_fade_run(struct weston_surface *surface,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200241 float start, float end, float k,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200242 weston_surface_animation_done_func_t done, void *data)
243{
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200244 struct weston_surface_animation *fade;
245
246 fade = weston_surface_animation_run(surface, 0, 0,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200247 fade_frame, done, data);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200248
249 weston_spring_init(&fade->spring, k, start, end);
250 surface->alpha = start;
251
252 return fade;
253}
254
255WL_EXPORT void
256weston_fade_update(struct weston_surface_animation *fade,
257 float start, float end, float k)
258{
259 weston_spring_init(&fade->spring, k, start, end);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200260}
261
262static void
263slide_frame(struct weston_surface_animation *animation)
264{
265 float scale;
266
267 scale = animation->start +
268 (animation->stop - animation->start) *
269 animation->spring.current;
270 weston_matrix_init(&animation->transform.matrix);
271 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
272}
273
274WL_EXPORT struct weston_surface_animation *
275weston_slide_run(struct weston_surface *surface, float start, float stop,
276 weston_surface_animation_done_func_t done, void *data)
277{
278 struct weston_surface_animation *animation;
279
280 animation = weston_surface_animation_run(surface, start, stop,
281 slide_frame, done, data);
282 if (!animation)
283 return NULL;
284
285 animation->spring.friction = 900;
286 animation->spring.k = 300;
287
288 return animation;
289}