blob: 909e24365f018a862c09e964fe911da08cc1dbb2 [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;
Axel Davy5e396ae2013-09-17 14:55:55 +0200127 weston_surface_animation_frame_func_t reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200128 weston_surface_animation_done_func_t done;
129 void *data;
130};
131
132static void
133weston_surface_animation_destroy(struct weston_surface_animation *animation)
134{
135 wl_list_remove(&animation->animation.link);
136 wl_list_remove(&animation->listener.link);
137 wl_list_remove(&animation->transform.link);
Axel Davy5e396ae2013-09-17 14:55:55 +0200138 if (animation->reset)
139 animation->reset(animation);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200140 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200141 if (animation->done)
142 animation->done(animation, animation->data);
143 free(animation);
144}
145
146static void
147handle_animation_surface_destroy(struct wl_listener *listener, void *data)
148{
149 struct weston_surface_animation *animation =
150 container_of(listener,
151 struct weston_surface_animation, listener);
152
153 weston_surface_animation_destroy(animation);
154}
155
156static void
157weston_surface_animation_frame(struct weston_animation *base,
158 struct weston_output *output, uint32_t msecs)
159{
160 struct weston_surface_animation *animation =
161 container_of(base,
162 struct weston_surface_animation, animation);
163
164 if (base->frame_counter <= 1)
165 animation->spring.timestamp = msecs;
166
167 weston_spring_update(&animation->spring, msecs);
168
169 if (weston_spring_done(&animation->spring)) {
170 weston_surface_animation_destroy(animation);
171 return;
172 }
173
174 if (animation->frame)
175 animation->frame(animation);
176
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200177 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200178 weston_compositor_schedule_repaint(animation->surface->compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200179}
180
181static struct weston_surface_animation *
182weston_surface_animation_run(struct weston_surface *surface,
183 float start, float stop,
184 weston_surface_animation_frame_func_t frame,
Axel Davy5e396ae2013-09-17 14:55:55 +0200185 weston_surface_animation_frame_func_t reset,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200186 weston_surface_animation_done_func_t done,
187 void *data)
188{
189 struct weston_surface_animation *animation;
190
191 animation = malloc(sizeof *animation);
192 if (!animation)
193 return NULL;
194
195 animation->surface = surface;
196 animation->frame = frame;
Axel Davy5e396ae2013-09-17 14:55:55 +0200197 animation->reset = reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200198 animation->done = done;
199 animation->data = data;
200 animation->start = start;
201 animation->stop = stop;
202 weston_matrix_init(&animation->transform.matrix);
203 wl_list_insert(&surface->geometry.transformation_list,
204 &animation->transform.link);
205 weston_spring_init(&animation->spring, 200.0, 0.0, 1.0);
206 animation->spring.friction = 700;
207 animation->animation.frame_counter = 0;
208 animation->animation.frame = weston_surface_animation_frame;
209 weston_surface_animation_frame(&animation->animation, NULL, 0);
210
211 animation->listener.notify = handle_animation_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500212 wl_signal_add(&surface->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200213
214 wl_list_insert(&surface->output->animation_list,
215 &animation->animation.link);
216
217 return animation;
218}
219
220static void
Axel Davy5e396ae2013-09-17 14:55:55 +0200221reset_alpha(struct weston_surface_animation *animation)
222{
223 struct weston_surface *surface = animation->surface;
224
225 surface->alpha = animation->stop;
226}
227
228static void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200229zoom_frame(struct weston_surface_animation *animation)
230{
231 struct weston_surface *es = animation->surface;
232 float scale;
233
234 scale = animation->start +
235 (animation->stop - animation->start) *
236 animation->spring.current;
237 weston_matrix_init(&animation->transform.matrix);
238 weston_matrix_translate(&animation->transform.matrix,
239 -0.5f * es->geometry.width,
240 -0.5f * es->geometry.height, 0);
241 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
242 weston_matrix_translate(&animation->transform.matrix,
243 0.5f * es->geometry.width,
244 0.5f * es->geometry.height, 0);
245
246 es->alpha = animation->spring.current;
247 if (es->alpha > 1.0)
248 es->alpha = 1.0;
249}
250
251WL_EXPORT struct weston_surface_animation *
252weston_zoom_run(struct weston_surface *surface, float start, float stop,
253 weston_surface_animation_done_func_t done, void *data)
254{
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400255 struct weston_surface_animation *zoom;
256
257 zoom = weston_surface_animation_run(surface, start, stop,
Axel Davy5e396ae2013-09-17 14:55:55 +0200258 zoom_frame, reset_alpha,
259 done, data);
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400260
261 weston_spring_init(&zoom->spring, 300.0, start, stop);
262 zoom->spring.friction = 1400;
263 zoom->spring.previous = start - (stop - start) * 0.03;
264
265 return zoom;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200266}
267
268static void
269fade_frame(struct weston_surface_animation *animation)
270{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200271 if (animation->spring.current > 0.999)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200272 animation->surface->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200273 else if (animation->spring.current < 0.001 )
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200274 animation->surface->alpha = 0;
275 else
276 animation->surface->alpha = animation->spring.current;
277}
278
279WL_EXPORT struct weston_surface_animation *
280weston_fade_run(struct weston_surface *surface,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200281 float start, float end, float k,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200282 weston_surface_animation_done_func_t done, void *data)
283{
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200284 struct weston_surface_animation *fade;
285
Axel Davy5e396ae2013-09-17 14:55:55 +0200286 fade = weston_surface_animation_run(surface, 0, end,
287 fade_frame, reset_alpha,
288 done, data);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200289
290 weston_spring_init(&fade->spring, k, start, end);
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400291
292 fade->spring.friction = 1400;
293 fade->spring.previous = -(end - start) * 0.03;
294
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200295 surface->alpha = start;
296
297 return fade;
298}
299
300WL_EXPORT void
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400301weston_fade_update(struct weston_surface_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200302{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400303 fade->spring.target = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200304}
305
306static void
307slide_frame(struct weston_surface_animation *animation)
308{
309 float scale;
310
311 scale = animation->start +
312 (animation->stop - animation->start) *
313 animation->spring.current;
314 weston_matrix_init(&animation->transform.matrix);
315 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
316}
317
318WL_EXPORT struct weston_surface_animation *
319weston_slide_run(struct weston_surface *surface, float start, float stop,
320 weston_surface_animation_done_func_t done, void *data)
321{
322 struct weston_surface_animation *animation;
323
324 animation = weston_surface_animation_run(surface, start, stop,
Axel Davy5e396ae2013-09-17 14:55:55 +0200325 slide_frame, NULL, done,
326 data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200327 if (!animation)
328 return NULL;
329
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400330 animation->spring.friction = 600;
331 animation->spring.k = 400;
332 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200333
334 return animation;
335}