blob: c2f8b9bad05d45c3446daad18dcdcd7e2ae5b17f [file] [log] [blame]
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020028#include <stdlib.h>
29#include <string.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020031#include <stdio.h>
32#include <math.h>
Alexandros Frantzis8250a612017-11-16 18:20:52 +020033#include <inttypes.h>
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020034
35#include <unistd.h>
36#include <fcntl.h>
37
38#include "compositor.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070039#include "shared/helpers.h"
Alexandros Frantzis8250a612017-11-16 18:20:52 +020040#include "shared/timespec-util.h"
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020041
42WL_EXPORT void
43weston_spring_init(struct weston_spring *spring,
Jason Ekstranda7af7042013-10-12 22:38:11 -050044 double k, double current, double target)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020045{
46 spring->k = k;
47 spring->friction = 400.0;
48 spring->current = current;
49 spring->previous = current;
50 spring->target = target;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040051 spring->clip = WESTON_SPRING_OVERSHOOT;
Kristian Høgsberg091b0962013-06-17 09:23:14 -040052 spring->min = 0.0;
53 spring->max = 1.0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020054}
55
56WL_EXPORT void
Alexandros Frantzis8250a612017-11-16 18:20:52 +020057weston_spring_update(struct weston_spring *spring, const struct timespec *time)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020058{
59 double force, v, current, step;
60
61 /* Limit the number of executions of the loop below by ensuring that
62 * the timestamp for last update of the spring is no more than 1s ago.
63 * This handles the case where time moves backwards or forwards in
64 * large jumps.
65 */
Alexandros Frantzis8250a612017-11-16 18:20:52 +020066 if (timespec_sub_to_msec(time, &spring->timestamp) > 1000) {
67 weston_log("unexpectedly large timestamp jump "
68 "(from %" PRId64 " to %" PRId64 ")\n",
69 timespec_to_msec(&spring->timestamp),
70 timespec_to_msec(time));
71 timespec_add_msec(&spring->timestamp, time, -1000);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020072 }
73
74 step = 0.01;
Alexandros Frantzis8250a612017-11-16 18:20:52 +020075 while (4 < timespec_sub_to_msec(time, &spring->timestamp)) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020076 current = spring->current;
77 v = current - spring->previous;
78 force = spring->k * (spring->target - current) / 10.0 +
79 (spring->previous - current) - v * spring->friction;
80
81 spring->current =
82 current + (current - spring->previous) +
83 force * step * step;
84 spring->previous = current;
85
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040086 switch (spring->clip) {
87 case WESTON_SPRING_OVERSHOOT:
88 break;
89
90 case WESTON_SPRING_CLAMP:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040091 if (spring->current > spring->max) {
92 spring->current = spring->max;
93 spring->previous = spring->max;
94 } else if (spring->current < 0.0) {
95 spring->current = spring->min;
96 spring->previous = spring->min;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040097 }
98 break;
99
100 case WESTON_SPRING_BOUNCE:
Kristian Høgsberg091b0962013-06-17 09:23:14 -0400101 if (spring->current > spring->max) {
102 spring->current =
103 2 * spring->max - spring->current;
104 spring->previous =
105 2 * spring->max - spring->previous;
106 } else if (spring->current < spring->min) {
107 spring->current =
108 2 * spring->min - spring->current;
109 spring->previous =
110 2 * spring->min - spring->previous;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -0400111 }
112 break;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200113 }
114
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200115 timespec_add_msec(&spring->timestamp, &spring->timestamp, 4);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200116 }
117}
118
119WL_EXPORT int
120weston_spring_done(struct weston_spring *spring)
121{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -0400122 return fabs(spring->previous - spring->target) < 0.002 &&
123 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200124}
125
Jason Ekstranda7af7042013-10-12 22:38:11 -0500126typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200127
Jason Ekstranda7af7042013-10-12 22:38:11 -0500128struct weston_view_animation {
129 struct weston_view *view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200130 struct weston_animation animation;
131 struct weston_spring spring;
132 struct weston_transform transform;
133 struct wl_listener listener;
134 float start, stop;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500135 weston_view_animation_frame_func_t frame;
136 weston_view_animation_frame_func_t reset;
137 weston_view_animation_done_func_t done;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200138 void *data;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100139 void *private;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200140};
141
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100142WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500143weston_view_animation_destroy(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200144{
145 wl_list_remove(&animation->animation.link);
146 wl_list_remove(&animation->listener.link);
147 wl_list_remove(&animation->transform.link);
Axel Davy5e396ae2013-09-17 14:55:55 +0200148 if (animation->reset)
149 animation->reset(animation);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500150 weston_view_geometry_dirty(animation->view);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200151 if (animation->done)
152 animation->done(animation, animation->data);
153 free(animation);
154}
155
156static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500157handle_animation_view_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200158{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500159 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200160 container_of(listener,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500161 struct weston_view_animation, listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200162
Jason Ekstranda7af7042013-10-12 22:38:11 -0500163 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200164}
165
166static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500167weston_view_animation_frame(struct weston_animation *base,
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200168 struct weston_output *output,
169 const struct timespec *time)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200170{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500171 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200172 container_of(base,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500173 struct weston_view_animation, animation);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200174 struct weston_compositor *compositor =
175 animation->view->surface->compositor;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200176
177 if (base->frame_counter <= 1)
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200178 animation->spring.timestamp = *time;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200179
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200180 weston_spring_update(&animation->spring, time);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200181
182 if (weston_spring_done(&animation->spring)) {
Kristian Høgsberg90dfb112013-10-30 09:07:04 -0700183 weston_view_schedule_repaint(animation->view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500184 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200185 return;
186 }
187
188 if (animation->frame)
189 animation->frame(animation);
190
Jason Ekstranda7af7042013-10-12 22:38:11 -0500191 weston_view_geometry_dirty(animation->view);
192 weston_view_schedule_repaint(animation->view);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200193
194 /* The view's output_mask will be zero if its position is
195 * offscreen. Animations should always run but as they are also
196 * run off the repaint cycle, if there's nothing to repaint
197 * the animation stops running. Therefore if we catch this situation
198 * and schedule a repaint on all outputs it will be avoided.
199 */
200 if (animation->view->output_mask == 0)
201 weston_compositor_schedule_repaint(compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200202}
203
Armin Krezović75e71062016-08-11 15:49:58 +0200204static void
205idle_animation_destroy(void *data)
206{
207 struct weston_view_animation *animation = data;
208
209 weston_view_animation_destroy(animation);
210}
211
Jason Ekstranda7af7042013-10-12 22:38:11 -0500212static struct weston_view_animation *
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300213weston_view_animation_create(struct weston_view *view,
214 float start, float stop,
215 weston_view_animation_frame_func_t frame,
216 weston_view_animation_frame_func_t reset,
217 weston_view_animation_done_func_t done,
218 void *data,
219 void *private)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200220{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500221 struct weston_view_animation *animation;
Armin Krezović75e71062016-08-11 15:49:58 +0200222 struct weston_compositor *ec = view->surface->compositor;
223 struct wl_event_loop *loop;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200224
225 animation = malloc(sizeof *animation);
226 if (!animation)
227 return NULL;
228
Jason Ekstranda7af7042013-10-12 22:38:11 -0500229 animation->view = view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200230 animation->frame = frame;
Axel Davy5e396ae2013-09-17 14:55:55 +0200231 animation->reset = reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200232 animation->done = done;
233 animation->data = data;
234 animation->start = start;
235 animation->stop = stop;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100236 animation->private = private;
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300237
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200238 weston_matrix_init(&animation->transform.matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500239 wl_list_insert(&view->geometry.transformation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200240 &animation->transform.link);
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300241
Jason Ekstranda7af7042013-10-12 22:38:11 -0500242 animation->animation.frame = weston_view_animation_frame;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200243
Jason Ekstranda7af7042013-10-12 22:38:11 -0500244 animation->listener.notify = handle_animation_view_destroy;
245 wl_signal_add(&view->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200246
Armin Krezović75e71062016-08-11 15:49:58 +0200247 if (view->output) {
248 wl_list_insert(&view->output->animation_list,
249 &animation->animation.link);
250 } else {
251 wl_list_init(&animation->animation.link);
252 loop = wl_display_get_event_loop(ec->wl_display);
253 wl_event_loop_add_idle(loop, idle_animation_destroy, animation);
254 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200255
256 return animation;
257}
258
259static void
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300260weston_view_animation_run(struct weston_view_animation *animation)
261{
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200262 struct timespec zero_time = { 0 };
263
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300264 animation->animation.frame_counter = 0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200265 weston_view_animation_frame(&animation->animation, NULL, &zero_time);
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300266}
267
268static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500269reset_alpha(struct weston_view_animation *animation)
Axel Davy5e396ae2013-09-17 14:55:55 +0200270{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 struct weston_view *view = animation->view;
Axel Davy5e396ae2013-09-17 14:55:55 +0200272
Jason Ekstranda7af7042013-10-12 22:38:11 -0500273 view->alpha = animation->stop;
Axel Davy5e396ae2013-09-17 14:55:55 +0200274}
275
276static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277zoom_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200278{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 struct weston_view *es = animation->view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200280 float scale;
281
282 scale = animation->start +
283 (animation->stop - animation->start) *
284 animation->spring.current;
285 weston_matrix_init(&animation->transform.matrix);
286 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600287 -0.5f * es->surface->width,
288 -0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200289 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
290 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600291 0.5f * es->surface->width,
292 0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200293
294 es->alpha = animation->spring.current;
295 if (es->alpha > 1.0)
296 es->alpha = 1.0;
297}
298
Jason Ekstranda7af7042013-10-12 22:38:11 -0500299WL_EXPORT struct weston_view_animation *
300weston_zoom_run(struct weston_view *view, float start, float stop,
301 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200302{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303 struct weston_view_animation *zoom;
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400304
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300305 zoom = weston_view_animation_create(view, start, stop,
306 zoom_frame, reset_alpha,
307 done, data, NULL);
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400308
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800309 if (zoom == NULL)
310 return NULL;
311
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400312 weston_spring_init(&zoom->spring, 300.0, start, stop);
313 zoom->spring.friction = 1400;
314 zoom->spring.previous = start - (stop - start) * 0.03;
315
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300316 weston_view_animation_run(zoom);
317
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400318 return zoom;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200319}
320
321static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500322fade_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200323{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200324 if (animation->spring.current > 0.999)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500325 animation->view->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200326 else if (animation->spring.current < 0.001 )
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 animation->view->alpha = 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200328 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500329 animation->view->alpha = animation->spring.current;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200330}
331
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332WL_EXPORT struct weston_view_animation *
333weston_fade_run(struct weston_view *view,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200334 float start, float end, float k,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500335 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200336{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500337 struct weston_view_animation *fade;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200338
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300339 fade = weston_view_animation_create(view, start, end,
340 fade_frame, reset_alpha,
341 done, data, NULL);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200342
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800343 if (fade == NULL)
344 return NULL;
345
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300346 weston_spring_init(&fade->spring, 1000.0, start, end);
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700347 fade->spring.friction = 4000;
348 fade->spring.previous = start - (end - start) * 0.1;
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400349
Jason Ekstranda7af7042013-10-12 22:38:11 -0500350 view->alpha = start;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200351
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300352 weston_view_animation_run(fade);
353
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200354 return fade;
355}
356
357WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500358weston_fade_update(struct weston_view_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200359{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400360 fade->spring.target = target;
Jonny Lamb70ee3ad2014-07-30 00:56:18 +0100361 fade->stop = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200362}
363
364static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100365stable_fade_frame(struct weston_view_animation *animation)
366{
367 struct weston_view *back_view;
368
369 if (animation->spring.current > 0.999)
370 animation->view->alpha = 1;
371 else if (animation->spring.current < 0.001 )
372 animation->view->alpha = 0;
373 else
374 animation->view->alpha = animation->spring.current;
375
376 back_view = (struct weston_view *) animation->private;
377 back_view->alpha =
378 (animation->spring.target - animation->view->alpha) /
379 (1.0 - animation->view->alpha);
380 weston_view_geometry_dirty(back_view);
381}
382
383WL_EXPORT struct weston_view_animation *
384weston_stable_fade_run(struct weston_view *front_view, float start,
385 struct weston_view *back_view, float end,
386 weston_view_animation_done_func_t done, void *data)
387{
388 struct weston_view_animation *fade;
389
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300390 fade = weston_view_animation_create(front_view, 0, 0,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100391 stable_fade_frame, NULL,
392 done, data, back_view);
393
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800394 if (fade == NULL)
395 return NULL;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100396
397 weston_spring_init(&fade->spring, 400, start, end);
398 fade->spring.friction = 1150;
399
400 front_view->alpha = start;
401 back_view->alpha = end;
402
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300403 weston_view_animation_run(fade);
404
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100405 return fade;
406}
407
408static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500409slide_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200410{
411 float scale;
412
413 scale = animation->start +
414 (animation->stop - animation->start) *
415 animation->spring.current;
416 weston_matrix_init(&animation->transform.matrix);
417 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
418}
419
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420WL_EXPORT struct weston_view_animation *
421weston_slide_run(struct weston_view *view, float start, float stop,
422 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200423{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200425
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300426 animation = weston_view_animation_create(view, start, stop,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500427 slide_frame, NULL, done,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100428 data, NULL);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200429 if (!animation)
430 return NULL;
431
Ander Conselvan de Oliveiraa4a6f162014-04-14 15:48:06 +0300432 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400433 animation->spring.friction = 600;
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400434 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200435
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300436 weston_view_animation_run(animation);
437
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200438 return animation;
439}
Daniel Stonea67e6b92013-11-19 11:37:13 +0100440
441struct weston_move_animation {
442 int dx;
443 int dy;
Quentin Glidic24d306c2016-08-10 15:53:33 +0200444 bool reverse;
445 bool scale;
Daniel Stonea67e6b92013-11-19 11:37:13 +0100446 weston_view_animation_done_func_t done;
447};
448
449static void
450move_frame(struct weston_view_animation *animation)
451{
452 struct weston_move_animation *move = animation->private;
453 float scale;
454 float progress = animation->spring.current;
455
456 if (move->reverse)
457 progress = 1.0 - progress;
458
459 scale = animation->start +
460 (animation->stop - animation->start) *
461 progress;
462 weston_matrix_init(&animation->transform.matrix);
Quentin Glidic24d306c2016-08-10 15:53:33 +0200463 if (move->scale)
464 weston_matrix_scale(&animation->transform.matrix, scale, scale,
465 1.0f);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100466 weston_matrix_translate(&animation->transform.matrix,
467 move->dx * progress, move->dy * progress,
468 0);
469}
470
471static void
472move_done(struct weston_view_animation *animation, void *data)
473{
474 struct weston_move_animation *move = animation->private;
475
476 if (move->done)
477 move->done(animation, data);
478
479 free(move);
480}
481
Quentin Glidic24d306c2016-08-10 15:53:33 +0200482static struct weston_view_animation *
483weston_move_scale_run_internal(struct weston_view *view, int dx, int dy,
484 float start, float end, bool reverse, bool scale,
485 weston_view_animation_done_func_t done,
486 void *data)
Daniel Stonea67e6b92013-11-19 11:37:13 +0100487{
488 struct weston_move_animation *move;
489 struct weston_view_animation *animation;
490
491 move = malloc(sizeof(*move));
492 if (!move)
493 return NULL;
494 move->dx = dx;
495 move->dy = dy;
496 move->reverse = reverse;
Quentin Glidic24d306c2016-08-10 15:53:33 +0200497 move->scale = scale;
Daniel Stonea67e6b92013-11-19 11:37:13 +0100498 move->done = done;
499
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300500 animation = weston_view_animation_create(view, start, end, move_frame,
501 NULL, move_done, data, move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800502
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300503 if (animation == NULL){
504 free(move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800505 return NULL;
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300506 }
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800507
Jonny Lamb91f80f22014-05-22 22:41:30 +0200508 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100509 animation->spring.friction = 1150;
510
Jonny Lamb91f80f22014-05-22 22:41:30 +0200511 weston_view_animation_run(animation);
512
Daniel Stonea67e6b92013-11-19 11:37:13 +0100513 return animation;
514}
Quentin Glidic24d306c2016-08-10 15:53:33 +0200515
516WL_EXPORT struct weston_view_animation *
517weston_move_scale_run(struct weston_view *view, int dx, int dy,
518 float start, float end, bool reverse,
519 weston_view_animation_done_func_t done, void *data)
520{
521 return weston_move_scale_run_internal(view, dx, dy, start, end, reverse,
522 true, done, data);
523}
524
525WL_EXPORT struct weston_view_animation *
526weston_move_run(struct weston_view *view, int dx, int dy,
527 float start, float end, bool reverse,
528 weston_view_animation_done_func_t done, void *data)
529{
530 return weston_move_scale_run_internal(view, dx, dy, start, end, reverse,
531 false, done, data);
532}