blob: a93d26e2384f1088314e98f73690a5370d3763ce [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;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040044 spring->clip = WESTON_SPRING_OVERSHOOT;
Kristian Høgsberg091b0962013-06-17 09:23:14 -040045 spring->min = 0.0;
46 spring->max = 1.0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020047}
48
49WL_EXPORT void
50weston_spring_update(struct weston_spring *spring, uint32_t msec)
51{
52 double force, v, current, step;
53
54 /* Limit the number of executions of the loop below by ensuring that
55 * the timestamp for last update of the spring is no more than 1s ago.
56 * This handles the case where time moves backwards or forwards in
57 * large jumps.
58 */
59 if (msec - spring->timestamp > 1000) {
60 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
61 spring->timestamp, msec);
62 spring->timestamp = msec - 1000;
63 }
64
65 step = 0.01;
66 while (4 < msec - spring->timestamp) {
67 current = spring->current;
68 v = current - spring->previous;
69 force = spring->k * (spring->target - current) / 10.0 +
70 (spring->previous - current) - v * spring->friction;
71
72 spring->current =
73 current + (current - spring->previous) +
74 force * step * step;
75 spring->previous = current;
76
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040077 switch (spring->clip) {
78 case WESTON_SPRING_OVERSHOOT:
79 break;
80
81 case WESTON_SPRING_CLAMP:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040082 if (spring->current > spring->max) {
83 spring->current = spring->max;
84 spring->previous = spring->max;
85 } else if (spring->current < 0.0) {
86 spring->current = spring->min;
87 spring->previous = spring->min;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040088 }
89 break;
90
91 case WESTON_SPRING_BOUNCE:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040092 if (spring->current > spring->max) {
93 spring->current =
94 2 * spring->max - spring->current;
95 spring->previous =
96 2 * spring->max - spring->previous;
97 } else if (spring->current < spring->min) {
98 spring->current =
99 2 * spring->min - spring->current;
100 spring->previous =
101 2 * spring->min - spring->previous;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -0400102 }
103 break;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200104 }
105
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200106 spring->timestamp += 4;
107 }
108}
109
110WL_EXPORT int
111weston_spring_done(struct weston_spring *spring)
112{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -0400113 return fabs(spring->previous - spring->target) < 0.002 &&
114 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200115}
116
117typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation);
118
119struct weston_surface_animation {
120 struct weston_surface *surface;
121 struct weston_animation animation;
122 struct weston_spring spring;
123 struct weston_transform transform;
124 struct wl_listener listener;
125 float start, stop;
126 weston_surface_animation_frame_func_t frame;
127 weston_surface_animation_done_func_t done;
128 void *data;
129};
130
131static void
132weston_surface_animation_destroy(struct weston_surface_animation *animation)
133{
134 wl_list_remove(&animation->animation.link);
135 wl_list_remove(&animation->listener.link);
136 wl_list_remove(&animation->transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200137 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200138 if (animation->done)
139 animation->done(animation, animation->data);
140 free(animation);
141}
142
143static void
144handle_animation_surface_destroy(struct wl_listener *listener, void *data)
145{
146 struct weston_surface_animation *animation =
147 container_of(listener,
148 struct weston_surface_animation, listener);
149
150 weston_surface_animation_destroy(animation);
151}
152
153static void
154weston_surface_animation_frame(struct weston_animation *base,
155 struct weston_output *output, uint32_t msecs)
156{
157 struct weston_surface_animation *animation =
158 container_of(base,
159 struct weston_surface_animation, animation);
160
161 if (base->frame_counter <= 1)
162 animation->spring.timestamp = msecs;
163
164 weston_spring_update(&animation->spring, msecs);
165
166 if (weston_spring_done(&animation->spring)) {
167 weston_surface_animation_destroy(animation);
168 return;
169 }
170
171 if (animation->frame)
172 animation->frame(animation);
173
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200174 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200175 weston_compositor_schedule_repaint(animation->surface->compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200176}
177
178static struct weston_surface_animation *
179weston_surface_animation_run(struct weston_surface *surface,
180 float start, float stop,
181 weston_surface_animation_frame_func_t frame,
182 weston_surface_animation_done_func_t done,
183 void *data)
184{
185 struct weston_surface_animation *animation;
186
187 animation = malloc(sizeof *animation);
188 if (!animation)
189 return NULL;
190
191 animation->surface = surface;
192 animation->frame = frame;
193 animation->done = done;
194 animation->data = data;
195 animation->start = start;
196 animation->stop = stop;
197 weston_matrix_init(&animation->transform.matrix);
198 wl_list_insert(&surface->geometry.transformation_list,
199 &animation->transform.link);
200 weston_spring_init(&animation->spring, 200.0, 0.0, 1.0);
201 animation->spring.friction = 700;
202 animation->animation.frame_counter = 0;
203 animation->animation.frame = weston_surface_animation_frame;
204 weston_surface_animation_frame(&animation->animation, NULL, 0);
205
206 animation->listener.notify = handle_animation_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500207 wl_signal_add(&surface->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200208
209 wl_list_insert(&surface->output->animation_list,
210 &animation->animation.link);
211
212 return animation;
213}
214
215static void
216zoom_frame(struct weston_surface_animation *animation)
217{
218 struct weston_surface *es = animation->surface;
219 float scale;
220
221 scale = animation->start +
222 (animation->stop - animation->start) *
223 animation->spring.current;
224 weston_matrix_init(&animation->transform.matrix);
225 weston_matrix_translate(&animation->transform.matrix,
226 -0.5f * es->geometry.width,
227 -0.5f * es->geometry.height, 0);
228 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
229 weston_matrix_translate(&animation->transform.matrix,
230 0.5f * es->geometry.width,
231 0.5f * es->geometry.height, 0);
232
233 es->alpha = animation->spring.current;
234 if (es->alpha > 1.0)
235 es->alpha = 1.0;
236}
237
238WL_EXPORT struct weston_surface_animation *
239weston_zoom_run(struct weston_surface *surface, float start, float stop,
240 weston_surface_animation_done_func_t done, void *data)
241{
242 return weston_surface_animation_run(surface, start, stop,
243 zoom_frame, done, data);
244}
245
246static void
247fade_frame(struct weston_surface_animation *animation)
248{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200249 if (animation->spring.current > 0.999)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200250 animation->surface->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200251 else if (animation->spring.current < 0.001 )
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200252 animation->surface->alpha = 0;
253 else
254 animation->surface->alpha = animation->spring.current;
255}
256
257WL_EXPORT struct weston_surface_animation *
258weston_fade_run(struct weston_surface *surface,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200259 float start, float end, float k,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200260 weston_surface_animation_done_func_t done, void *data)
261{
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200262 struct weston_surface_animation *fade;
263
264 fade = weston_surface_animation_run(surface, 0, 0,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200265 fade_frame, done, data);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200266
267 weston_spring_init(&fade->spring, k, start, end);
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400268
269 fade->spring.friction = 1400;
270 fade->spring.previous = -(end - start) * 0.03;
271
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200272 surface->alpha = start;
273
274 return fade;
275}
276
277WL_EXPORT void
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400278weston_fade_update(struct weston_surface_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200279{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400280 fade->spring.target = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200281}
282
283static void
284slide_frame(struct weston_surface_animation *animation)
285{
286 float scale;
287
288 scale = animation->start +
289 (animation->stop - animation->start) *
290 animation->spring.current;
291 weston_matrix_init(&animation->transform.matrix);
292 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
293}
294
295WL_EXPORT struct weston_surface_animation *
296weston_slide_run(struct weston_surface *surface, float start, float stop,
297 weston_surface_animation_done_func_t done, void *data)
298{
299 struct weston_surface_animation *animation;
300
301 animation = weston_surface_animation_run(surface, start, stop,
302 slide_frame, done, data);
303 if (!animation)
304 return NULL;
305
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400306 animation->spring.friction = 600;
307 animation->spring.k = 400;
308 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200309
310 return animation;
311}