blob: 521e4f18bac5b2ffeeb9397e0d4cf5717013690a [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,
Jason Ekstranda7af7042013-10-12 22:38:11 -050037 double k, double current, double target)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020038{
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
Jason Ekstranda7af7042013-10-12 22:38:11 -0500117typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200118
Jason Ekstranda7af7042013-10-12 22:38:11 -0500119struct weston_view_animation {
120 struct weston_view *view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200121 struct weston_animation animation;
122 struct weston_spring spring;
123 struct weston_transform transform;
124 struct wl_listener listener;
125 float start, stop;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500126 weston_view_animation_frame_func_t frame;
127 weston_view_animation_frame_func_t reset;
128 weston_view_animation_done_func_t done;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200129 void *data;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100130 void *private;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200131};
132
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100133WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500134weston_view_animation_destroy(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200135{
136 wl_list_remove(&animation->animation.link);
137 wl_list_remove(&animation->listener.link);
138 wl_list_remove(&animation->transform.link);
Axel Davy5e396ae2013-09-17 14:55:55 +0200139 if (animation->reset)
140 animation->reset(animation);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500141 weston_view_geometry_dirty(animation->view);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200142 if (animation->done)
143 animation->done(animation, animation->data);
144 free(animation);
145}
146
147static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500148handle_animation_view_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200149{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500150 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200151 container_of(listener,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500152 struct weston_view_animation, listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200153
Jason Ekstranda7af7042013-10-12 22:38:11 -0500154 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200155}
156
157static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500158weston_view_animation_frame(struct weston_animation *base,
159 struct weston_output *output, uint32_t msecs)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200160{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500161 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200162 container_of(base,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500163 struct weston_view_animation, animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200164
165 if (base->frame_counter <= 1)
166 animation->spring.timestamp = msecs;
167
168 weston_spring_update(&animation->spring, msecs);
169
170 if (weston_spring_done(&animation->spring)) {
Kristian Høgsberg90dfb112013-10-30 09:07:04 -0700171 weston_view_schedule_repaint(animation->view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500172 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200173 return;
174 }
175
176 if (animation->frame)
177 animation->frame(animation);
178
Jason Ekstranda7af7042013-10-12 22:38:11 -0500179 weston_view_geometry_dirty(animation->view);
180 weston_view_schedule_repaint(animation->view);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200181}
182
Jason Ekstranda7af7042013-10-12 22:38:11 -0500183static struct weston_view_animation *
184weston_view_animation_run(struct weston_view *view,
185 float start, float stop,
186 weston_view_animation_frame_func_t frame,
187 weston_view_animation_frame_func_t reset,
188 weston_view_animation_done_func_t done,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100189 void *data,
190 void *private)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200191{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500192 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200193
194 animation = malloc(sizeof *animation);
195 if (!animation)
196 return NULL;
197
Jason Ekstranda7af7042013-10-12 22:38:11 -0500198 animation->view = view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200199 animation->frame = frame;
Axel Davy5e396ae2013-09-17 14:55:55 +0200200 animation->reset = reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200201 animation->done = done;
202 animation->data = data;
203 animation->start = start;
204 animation->stop = stop;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100205 animation->private = private;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200206 weston_matrix_init(&animation->transform.matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500207 wl_list_insert(&view->geometry.transformation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200208 &animation->transform.link);
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700209 weston_spring_init(&animation->spring, 200.0, start, stop);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200210 animation->spring.friction = 700;
211 animation->animation.frame_counter = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500212 animation->animation.frame = weston_view_animation_frame;
213 weston_view_animation_frame(&animation->animation, NULL, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200214
Jason Ekstranda7af7042013-10-12 22:38:11 -0500215 animation->listener.notify = handle_animation_view_destroy;
216 wl_signal_add(&view->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200217
Jason Ekstranda7af7042013-10-12 22:38:11 -0500218 wl_list_insert(&view->output->animation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200219 &animation->animation.link);
220
221 return animation;
222}
223
224static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500225reset_alpha(struct weston_view_animation *animation)
Axel Davy5e396ae2013-09-17 14:55:55 +0200226{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500227 struct weston_view *view = animation->view;
Axel Davy5e396ae2013-09-17 14:55:55 +0200228
Jason Ekstranda7af7042013-10-12 22:38:11 -0500229 view->alpha = animation->stop;
Axel Davy5e396ae2013-09-17 14:55:55 +0200230}
231
232static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500233zoom_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200234{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500235 struct weston_view *es = animation->view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200236 float scale;
237
238 scale = animation->start +
239 (animation->stop - animation->start) *
240 animation->spring.current;
241 weston_matrix_init(&animation->transform.matrix);
242 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600243 -0.5f * es->surface->width,
244 -0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200245 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
246 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600247 0.5f * es->surface->width,
248 0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200249
250 es->alpha = animation->spring.current;
251 if (es->alpha > 1.0)
252 es->alpha = 1.0;
253}
254
Jason Ekstranda7af7042013-10-12 22:38:11 -0500255WL_EXPORT struct weston_view_animation *
256weston_zoom_run(struct weston_view *view, float start, float stop,
257 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200258{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500259 struct weston_view_animation *zoom;
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400260
Jason Ekstranda7af7042013-10-12 22:38:11 -0500261 zoom = weston_view_animation_run(view, start, stop,
262 zoom_frame, reset_alpha,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100263 done, data, NULL);
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400264
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800265 if (zoom == NULL)
266 return NULL;
267
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400268 weston_spring_init(&zoom->spring, 300.0, start, stop);
269 zoom->spring.friction = 1400;
270 zoom->spring.previous = start - (stop - start) * 0.03;
271
272 return zoom;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200273}
274
275static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276fade_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200277{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200278 if (animation->spring.current > 0.999)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 animation->view->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200280 else if (animation->spring.current < 0.001 )
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 animation->view->alpha = 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200282 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500283 animation->view->alpha = animation->spring.current;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200284}
285
Jason Ekstranda7af7042013-10-12 22:38:11 -0500286WL_EXPORT struct weston_view_animation *
287weston_fade_run(struct weston_view *view,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200288 float start, float end, float k,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200290{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291 struct weston_view_animation *fade;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200292
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700293 fade = weston_view_animation_run(view, start, end,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294 fade_frame, reset_alpha,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100295 done, data, NULL);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200296
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800297 if (fade == NULL)
298 return NULL;
299
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700300 fade->spring.k = 1000.0;
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400301
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700302 fade->spring.friction = 4000;
303 fade->spring.previous = start - (end - start) * 0.1;
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400304
Jason Ekstranda7af7042013-10-12 22:38:11 -0500305 view->alpha = start;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200306
307 return fade;
308}
309
310WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311weston_fade_update(struct weston_view_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200312{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400313 fade->spring.target = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200314}
315
316static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100317stable_fade_frame(struct weston_view_animation *animation)
318{
319 struct weston_view *back_view;
320
321 if (animation->spring.current > 0.999)
322 animation->view->alpha = 1;
323 else if (animation->spring.current < 0.001 )
324 animation->view->alpha = 0;
325 else
326 animation->view->alpha = animation->spring.current;
327
328 back_view = (struct weston_view *) animation->private;
329 back_view->alpha =
330 (animation->spring.target - animation->view->alpha) /
331 (1.0 - animation->view->alpha);
332 weston_view_geometry_dirty(back_view);
333}
334
335WL_EXPORT struct weston_view_animation *
336weston_stable_fade_run(struct weston_view *front_view, float start,
337 struct weston_view *back_view, float end,
338 weston_view_animation_done_func_t done, void *data)
339{
340 struct weston_view_animation *fade;
341
342 fade = weston_view_animation_run(front_view, 0, 0,
343 stable_fade_frame, NULL,
344 done, data, back_view);
345
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800346 if (fade == NULL)
347 return NULL;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100348
349 weston_spring_init(&fade->spring, 400, start, end);
350 fade->spring.friction = 1150;
351
352 front_view->alpha = start;
353 back_view->alpha = end;
354
355 return fade;
356}
357
358static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500359slide_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200360{
361 float scale;
362
363 scale = animation->start +
364 (animation->stop - animation->start) *
365 animation->spring.current;
366 weston_matrix_init(&animation->transform.matrix);
367 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
368}
369
Jason Ekstranda7af7042013-10-12 22:38:11 -0500370WL_EXPORT struct weston_view_animation *
371weston_slide_run(struct weston_view *view, float start, float stop,
372 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200373{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500374 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200375
Jason Ekstranda7af7042013-10-12 22:38:11 -0500376 animation = weston_view_animation_run(view, start, stop,
377 slide_frame, NULL, done,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100378 data, NULL);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200379 if (!animation)
380 return NULL;
381
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400382 animation->spring.friction = 600;
383 animation->spring.k = 400;
384 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200385
386 return animation;
387}
Daniel Stonea67e6b92013-11-19 11:37:13 +0100388
389struct weston_move_animation {
390 int dx;
391 int dy;
392 int reverse;
393 weston_view_animation_done_func_t done;
394};
395
396static void
397move_frame(struct weston_view_animation *animation)
398{
399 struct weston_move_animation *move = animation->private;
400 float scale;
401 float progress = animation->spring.current;
402
403 if (move->reverse)
404 progress = 1.0 - progress;
405
406 scale = animation->start +
407 (animation->stop - animation->start) *
408 progress;
409 weston_matrix_init(&animation->transform.matrix);
410 weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f);
411 weston_matrix_translate(&animation->transform.matrix,
412 move->dx * progress, move->dy * progress,
413 0);
414}
415
416static void
417move_done(struct weston_view_animation *animation, void *data)
418{
419 struct weston_move_animation *move = animation->private;
420
421 if (move->done)
422 move->done(animation, data);
423
424 free(move);
425}
426
427WL_EXPORT struct weston_view_animation *
428weston_move_scale_run(struct weston_view *view, int dx, int dy,
429 float start, float end, int reverse,
430 weston_view_animation_done_func_t done, void *data)
431{
432 struct weston_move_animation *move;
433 struct weston_view_animation *animation;
434
435 move = malloc(sizeof(*move));
436 if (!move)
437 return NULL;
438 move->dx = dx;
439 move->dy = dy;
440 move->reverse = reverse;
441 move->done = done;
442
443 animation = weston_view_animation_run(view, start, end, move_frame,
444 NULL, move_done, data, move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800445
446 if (animation == NULL)
447 return NULL;
448
Daniel Stonea67e6b92013-11-19 11:37:13 +0100449 animation->spring.k = 400;
450 animation->spring.friction = 1150;
451
452 return animation;
453}