blob: 24fc4adc0940ac20c057cea1736d31857c09b8f6 [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>
33
34#include <unistd.h>
35#include <fcntl.h>
36
37#include "compositor.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070038#include "shared/helpers.h"
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020039
40WL_EXPORT void
41weston_spring_init(struct weston_spring *spring,
Jason Ekstranda7af7042013-10-12 22:38:11 -050042 double k, double current, double target)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020043{
44 spring->k = k;
45 spring->friction = 400.0;
46 spring->current = current;
47 spring->previous = current;
48 spring->target = target;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040049 spring->clip = WESTON_SPRING_OVERSHOOT;
Kristian Høgsberg091b0962013-06-17 09:23:14 -040050 spring->min = 0.0;
51 spring->max = 1.0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020052}
53
54WL_EXPORT void
55weston_spring_update(struct weston_spring *spring, uint32_t msec)
56{
57 double force, v, current, step;
58
59 /* Limit the number of executions of the loop below by ensuring that
60 * the timestamp for last update of the spring is no more than 1s ago.
61 * This handles the case where time moves backwards or forwards in
62 * large jumps.
63 */
64 if (msec - spring->timestamp > 1000) {
65 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
66 spring->timestamp, msec);
67 spring->timestamp = msec - 1000;
68 }
69
70 step = 0.01;
71 while (4 < msec - spring->timestamp) {
72 current = spring->current;
73 v = current - spring->previous;
74 force = spring->k * (spring->target - current) / 10.0 +
75 (spring->previous - current) - v * spring->friction;
76
77 spring->current =
78 current + (current - spring->previous) +
79 force * step * step;
80 spring->previous = current;
81
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040082 switch (spring->clip) {
83 case WESTON_SPRING_OVERSHOOT:
84 break;
85
86 case WESTON_SPRING_CLAMP:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040087 if (spring->current > spring->max) {
88 spring->current = spring->max;
89 spring->previous = spring->max;
90 } else if (spring->current < 0.0) {
91 spring->current = spring->min;
92 spring->previous = spring->min;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040093 }
94 break;
95
96 case WESTON_SPRING_BOUNCE:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040097 if (spring->current > spring->max) {
98 spring->current =
99 2 * spring->max - spring->current;
100 spring->previous =
101 2 * spring->max - spring->previous;
102 } else if (spring->current < spring->min) {
103 spring->current =
104 2 * spring->min - spring->current;
105 spring->previous =
106 2 * spring->min - spring->previous;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -0400107 }
108 break;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200109 }
110
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200111 spring->timestamp += 4;
112 }
113}
114
115WL_EXPORT int
116weston_spring_done(struct weston_spring *spring)
117{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -0400118 return fabs(spring->previous - spring->target) < 0.002 &&
119 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200120}
121
Jason Ekstranda7af7042013-10-12 22:38:11 -0500122typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200123
Jason Ekstranda7af7042013-10-12 22:38:11 -0500124struct weston_view_animation {
125 struct weston_view *view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200126 struct weston_animation animation;
127 struct weston_spring spring;
128 struct weston_transform transform;
129 struct wl_listener listener;
130 float start, stop;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500131 weston_view_animation_frame_func_t frame;
132 weston_view_animation_frame_func_t reset;
133 weston_view_animation_done_func_t done;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200134 void *data;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100135 void *private;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200136};
137
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100138WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500139weston_view_animation_destroy(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200140{
141 wl_list_remove(&animation->animation.link);
142 wl_list_remove(&animation->listener.link);
143 wl_list_remove(&animation->transform.link);
Axel Davy5e396ae2013-09-17 14:55:55 +0200144 if (animation->reset)
145 animation->reset(animation);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500146 weston_view_geometry_dirty(animation->view);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200147 if (animation->done)
148 animation->done(animation, animation->data);
149 free(animation);
150}
151
152static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153handle_animation_view_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200154{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500155 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200156 container_of(listener,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500157 struct weston_view_animation, listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200158
Jason Ekstranda7af7042013-10-12 22:38:11 -0500159 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200160}
161
162static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500163weston_view_animation_frame(struct weston_animation *base,
164 struct weston_output *output, uint32_t msecs)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200165{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500166 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200167 container_of(base,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500168 struct weston_view_animation, animation);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200169 struct weston_compositor *compositor =
170 animation->view->surface->compositor;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200171
172 if (base->frame_counter <= 1)
173 animation->spring.timestamp = msecs;
174
175 weston_spring_update(&animation->spring, msecs);
176
177 if (weston_spring_done(&animation->spring)) {
Kristian Høgsberg90dfb112013-10-30 09:07:04 -0700178 weston_view_schedule_repaint(animation->view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500179 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200180 return;
181 }
182
183 if (animation->frame)
184 animation->frame(animation);
185
Jason Ekstranda7af7042013-10-12 22:38:11 -0500186 weston_view_geometry_dirty(animation->view);
187 weston_view_schedule_repaint(animation->view);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200188
189 /* The view's output_mask will be zero if its position is
190 * offscreen. Animations should always run but as they are also
191 * run off the repaint cycle, if there's nothing to repaint
192 * the animation stops running. Therefore if we catch this situation
193 * and schedule a repaint on all outputs it will be avoided.
194 */
195 if (animation->view->output_mask == 0)
196 weston_compositor_schedule_repaint(compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200197}
198
Armin Krezović75e71062016-08-11 15:49:58 +0200199static void
200idle_animation_destroy(void *data)
201{
202 struct weston_view_animation *animation = data;
203
204 weston_view_animation_destroy(animation);
205}
206
Jason Ekstranda7af7042013-10-12 22:38:11 -0500207static struct weston_view_animation *
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300208weston_view_animation_create(struct weston_view *view,
209 float start, float stop,
210 weston_view_animation_frame_func_t frame,
211 weston_view_animation_frame_func_t reset,
212 weston_view_animation_done_func_t done,
213 void *data,
214 void *private)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200215{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500216 struct weston_view_animation *animation;
Armin Krezović75e71062016-08-11 15:49:58 +0200217 struct weston_compositor *ec = view->surface->compositor;
218 struct wl_event_loop *loop;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200219
220 animation = malloc(sizeof *animation);
221 if (!animation)
222 return NULL;
223
Jason Ekstranda7af7042013-10-12 22:38:11 -0500224 animation->view = view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200225 animation->frame = frame;
Axel Davy5e396ae2013-09-17 14:55:55 +0200226 animation->reset = reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200227 animation->done = done;
228 animation->data = data;
229 animation->start = start;
230 animation->stop = stop;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100231 animation->private = private;
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300232
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200233 weston_matrix_init(&animation->transform.matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500234 wl_list_insert(&view->geometry.transformation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200235 &animation->transform.link);
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300236
Jason Ekstranda7af7042013-10-12 22:38:11 -0500237 animation->animation.frame = weston_view_animation_frame;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200238
Jason Ekstranda7af7042013-10-12 22:38:11 -0500239 animation->listener.notify = handle_animation_view_destroy;
240 wl_signal_add(&view->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200241
Armin Krezović75e71062016-08-11 15:49:58 +0200242 if (view->output) {
243 wl_list_insert(&view->output->animation_list,
244 &animation->animation.link);
245 } else {
246 wl_list_init(&animation->animation.link);
247 loop = wl_display_get_event_loop(ec->wl_display);
248 wl_event_loop_add_idle(loop, idle_animation_destroy, animation);
249 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200250
251 return animation;
252}
253
254static void
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300255weston_view_animation_run(struct weston_view_animation *animation)
256{
257 animation->animation.frame_counter = 0;
258 weston_view_animation_frame(&animation->animation, NULL, 0);
259}
260
261static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500262reset_alpha(struct weston_view_animation *animation)
Axel Davy5e396ae2013-09-17 14:55:55 +0200263{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500264 struct weston_view *view = animation->view;
Axel Davy5e396ae2013-09-17 14:55:55 +0200265
Jason Ekstranda7af7042013-10-12 22:38:11 -0500266 view->alpha = animation->stop;
Axel Davy5e396ae2013-09-17 14:55:55 +0200267}
268
269static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500270zoom_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200271{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500272 struct weston_view *es = animation->view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200273 float scale;
274
275 scale = animation->start +
276 (animation->stop - animation->start) *
277 animation->spring.current;
278 weston_matrix_init(&animation->transform.matrix);
279 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600280 -0.5f * es->surface->width,
281 -0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200282 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
283 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600284 0.5f * es->surface->width,
285 0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200286
287 es->alpha = animation->spring.current;
288 if (es->alpha > 1.0)
289 es->alpha = 1.0;
290}
291
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292WL_EXPORT struct weston_view_animation *
293weston_zoom_run(struct weston_view *view, float start, float stop,
294 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200295{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296 struct weston_view_animation *zoom;
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400297
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300298 zoom = weston_view_animation_create(view, start, stop,
299 zoom_frame, reset_alpha,
300 done, data, NULL);
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400301
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800302 if (zoom == NULL)
303 return NULL;
304
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400305 weston_spring_init(&zoom->spring, 300.0, start, stop);
306 zoom->spring.friction = 1400;
307 zoom->spring.previous = start - (stop - start) * 0.03;
308
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300309 weston_view_animation_run(zoom);
310
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400311 return zoom;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200312}
313
314static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500315fade_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200316{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200317 if (animation->spring.current > 0.999)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 animation->view->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200319 else if (animation->spring.current < 0.001 )
Jason Ekstranda7af7042013-10-12 22:38:11 -0500320 animation->view->alpha = 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200321 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500322 animation->view->alpha = animation->spring.current;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200323}
324
Jason Ekstranda7af7042013-10-12 22:38:11 -0500325WL_EXPORT struct weston_view_animation *
326weston_fade_run(struct weston_view *view,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200327 float start, float end, float k,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200329{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500330 struct weston_view_animation *fade;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200331
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300332 fade = weston_view_animation_create(view, start, end,
333 fade_frame, reset_alpha,
334 done, data, NULL);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200335
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800336 if (fade == NULL)
337 return NULL;
338
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300339 weston_spring_init(&fade->spring, 1000.0, start, end);
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700340 fade->spring.friction = 4000;
341 fade->spring.previous = start - (end - start) * 0.1;
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400342
Jason Ekstranda7af7042013-10-12 22:38:11 -0500343 view->alpha = start;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200344
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300345 weston_view_animation_run(fade);
346
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200347 return fade;
348}
349
350WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500351weston_fade_update(struct weston_view_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200352{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400353 fade->spring.target = target;
Jonny Lamb70ee3ad2014-07-30 00:56:18 +0100354 fade->stop = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200355}
356
357static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100358stable_fade_frame(struct weston_view_animation *animation)
359{
360 struct weston_view *back_view;
361
362 if (animation->spring.current > 0.999)
363 animation->view->alpha = 1;
364 else if (animation->spring.current < 0.001 )
365 animation->view->alpha = 0;
366 else
367 animation->view->alpha = animation->spring.current;
368
369 back_view = (struct weston_view *) animation->private;
370 back_view->alpha =
371 (animation->spring.target - animation->view->alpha) /
372 (1.0 - animation->view->alpha);
373 weston_view_geometry_dirty(back_view);
374}
375
376WL_EXPORT struct weston_view_animation *
377weston_stable_fade_run(struct weston_view *front_view, float start,
378 struct weston_view *back_view, float end,
379 weston_view_animation_done_func_t done, void *data)
380{
381 struct weston_view_animation *fade;
382
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300383 fade = weston_view_animation_create(front_view, 0, 0,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100384 stable_fade_frame, NULL,
385 done, data, back_view);
386
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800387 if (fade == NULL)
388 return NULL;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100389
390 weston_spring_init(&fade->spring, 400, start, end);
391 fade->spring.friction = 1150;
392
393 front_view->alpha = start;
394 back_view->alpha = end;
395
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300396 weston_view_animation_run(fade);
397
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100398 return fade;
399}
400
401static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500402slide_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200403{
404 float scale;
405
406 scale = animation->start +
407 (animation->stop - animation->start) *
408 animation->spring.current;
409 weston_matrix_init(&animation->transform.matrix);
410 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
411}
412
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413WL_EXPORT struct weston_view_animation *
414weston_slide_run(struct weston_view *view, float start, float stop,
415 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200416{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500417 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200418
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300419 animation = weston_view_animation_create(view, start, stop,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420 slide_frame, NULL, done,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100421 data, NULL);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200422 if (!animation)
423 return NULL;
424
Ander Conselvan de Oliveiraa4a6f162014-04-14 15:48:06 +0300425 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400426 animation->spring.friction = 600;
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400427 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200428
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300429 weston_view_animation_run(animation);
430
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200431 return animation;
432}
Daniel Stonea67e6b92013-11-19 11:37:13 +0100433
434struct weston_move_animation {
435 int dx;
436 int dy;
437 int reverse;
438 weston_view_animation_done_func_t done;
439};
440
441static void
442move_frame(struct weston_view_animation *animation)
443{
444 struct weston_move_animation *move = animation->private;
445 float scale;
446 float progress = animation->spring.current;
447
448 if (move->reverse)
449 progress = 1.0 - progress;
450
451 scale = animation->start +
452 (animation->stop - animation->start) *
453 progress;
454 weston_matrix_init(&animation->transform.matrix);
455 weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f);
456 weston_matrix_translate(&animation->transform.matrix,
457 move->dx * progress, move->dy * progress,
458 0);
459}
460
461static void
462move_done(struct weston_view_animation *animation, void *data)
463{
464 struct weston_move_animation *move = animation->private;
465
466 if (move->done)
467 move->done(animation, data);
468
469 free(move);
470}
471
472WL_EXPORT struct weston_view_animation *
473weston_move_scale_run(struct weston_view *view, int dx, int dy,
474 float start, float end, int reverse,
475 weston_view_animation_done_func_t done, void *data)
476{
477 struct weston_move_animation *move;
478 struct weston_view_animation *animation;
479
480 move = malloc(sizeof(*move));
481 if (!move)
482 return NULL;
483 move->dx = dx;
484 move->dy = dy;
485 move->reverse = reverse;
486 move->done = done;
487
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300488 animation = weston_view_animation_create(view, start, end, move_frame,
489 NULL, move_done, data, move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800490
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300491 if (animation == NULL){
492 free(move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800493 return NULL;
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300494 }
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800495
Jonny Lamb91f80f22014-05-22 22:41:30 +0200496 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100497 animation->spring.friction = 1150;
498
Jonny Lamb91f80f22014-05-22 22:41:30 +0200499 weston_view_animation_run(animation);
500
Daniel Stonea67e6b92013-11-19 11:37:13 +0100501 return animation;
502}