blob: 6320e81f85f09b4b37305ae1270737138dff2921 [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
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020038#include <libweston/libweston.h>
Guillaume Champagnef1e8fc92020-01-27 20:14:29 -050039#include "libweston-internal.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070040#include "shared/helpers.h"
Alexandros Frantzis8250a612017-11-16 18:20:52 +020041#include "shared/timespec-util.h"
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020042
43WL_EXPORT void
44weston_spring_init(struct weston_spring *spring,
Jason Ekstranda7af7042013-10-12 22:38:11 -050045 double k, double current, double target)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020046{
47 spring->k = k;
48 spring->friction = 400.0;
49 spring->current = current;
50 spring->previous = current;
51 spring->target = target;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040052 spring->clip = WESTON_SPRING_OVERSHOOT;
Kristian Høgsberg091b0962013-06-17 09:23:14 -040053 spring->min = 0.0;
54 spring->max = 1.0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020055}
56
57WL_EXPORT void
Alexandros Frantzis8250a612017-11-16 18:20:52 +020058weston_spring_update(struct weston_spring *spring, const struct timespec *time)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020059{
60 double force, v, current, step;
61
62 /* Limit the number of executions of the loop below by ensuring that
63 * the timestamp for last update of the spring is no more than 1s ago.
64 * This handles the case where time moves backwards or forwards in
65 * large jumps.
66 */
Alexandros Frantzis8250a612017-11-16 18:20:52 +020067 if (timespec_sub_to_msec(time, &spring->timestamp) > 1000) {
68 weston_log("unexpectedly large timestamp jump "
69 "(from %" PRId64 " to %" PRId64 ")\n",
70 timespec_to_msec(&spring->timestamp),
71 timespec_to_msec(time));
72 timespec_add_msec(&spring->timestamp, time, -1000);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020073 }
74
75 step = 0.01;
Alexandros Frantzis8250a612017-11-16 18:20:52 +020076 while (4 < timespec_sub_to_msec(time, &spring->timestamp)) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020077 current = spring->current;
78 v = current - spring->previous;
79 force = spring->k * (spring->target - current) / 10.0 +
80 (spring->previous - current) - v * spring->friction;
81
82 spring->current =
83 current + (current - spring->previous) +
84 force * step * step;
85 spring->previous = current;
86
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040087 switch (spring->clip) {
88 case WESTON_SPRING_OVERSHOOT:
89 break;
90
91 case WESTON_SPRING_CLAMP:
Kristian Høgsberg091b0962013-06-17 09:23:14 -040092 if (spring->current > spring->max) {
93 spring->current = spring->max;
94 spring->previous = spring->max;
95 } else if (spring->current < 0.0) {
96 spring->current = spring->min;
97 spring->previous = spring->min;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040098 }
99 break;
100
101 case WESTON_SPRING_BOUNCE:
Kristian Høgsberg091b0962013-06-17 09:23:14 -0400102 if (spring->current > spring->max) {
103 spring->current =
104 2 * spring->max - spring->current;
105 spring->previous =
106 2 * spring->max - spring->previous;
107 } else if (spring->current < spring->min) {
108 spring->current =
109 2 * spring->min - spring->current;
110 spring->previous =
111 2 * spring->min - spring->previous;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -0400112 }
113 break;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200114 }
115
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200116 timespec_add_msec(&spring->timestamp, &spring->timestamp, 4);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200117 }
118}
119
120WL_EXPORT int
121weston_spring_done(struct weston_spring *spring)
122{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -0400123 return fabs(spring->previous - spring->target) < 0.002 &&
124 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200125}
126
Jason Ekstranda7af7042013-10-12 22:38:11 -0500127typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200128
Jason Ekstranda7af7042013-10-12 22:38:11 -0500129struct weston_view_animation {
130 struct weston_view *view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200131 struct weston_animation animation;
132 struct weston_spring spring;
133 struct weston_transform transform;
134 struct wl_listener listener;
135 float start, stop;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500136 weston_view_animation_frame_func_t frame;
137 weston_view_animation_frame_func_t reset;
138 weston_view_animation_done_func_t done;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200139 void *data;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100140 void *private;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200141};
142
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100143WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500144weston_view_animation_destroy(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200145{
146 wl_list_remove(&animation->animation.link);
147 wl_list_remove(&animation->listener.link);
148 wl_list_remove(&animation->transform.link);
Axel Davy5e396ae2013-09-17 14:55:55 +0200149 if (animation->reset)
150 animation->reset(animation);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500151 weston_view_geometry_dirty(animation->view);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200152 if (animation->done)
153 animation->done(animation, animation->data);
154 free(animation);
155}
156
157static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500158handle_animation_view_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200159{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500160 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200161 container_of(listener,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500162 struct weston_view_animation, listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200163
Jason Ekstranda7af7042013-10-12 22:38:11 -0500164 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200165}
166
167static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500168weston_view_animation_frame(struct weston_animation *base,
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200169 struct weston_output *output,
170 const struct timespec *time)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200171{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500172 struct weston_view_animation *animation =
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200173 container_of(base,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500174 struct weston_view_animation, animation);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200175 struct weston_compositor *compositor =
176 animation->view->surface->compositor;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200177
178 if (base->frame_counter <= 1)
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200179 animation->spring.timestamp = *time;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200180
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200181 weston_spring_update(&animation->spring, time);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200182
183 if (weston_spring_done(&animation->spring)) {
Kristian Høgsberg90dfb112013-10-30 09:07:04 -0700184 weston_view_schedule_repaint(animation->view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500185 weston_view_animation_destroy(animation);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200186 return;
187 }
188
189 if (animation->frame)
190 animation->frame(animation);
191
Jason Ekstranda7af7042013-10-12 22:38:11 -0500192 weston_view_geometry_dirty(animation->view);
193 weston_view_schedule_repaint(animation->view);
Jonny Lambf8bfd052014-05-22 22:41:33 +0200194
195 /* The view's output_mask will be zero if its position is
196 * offscreen. Animations should always run but as they are also
197 * run off the repaint cycle, if there's nothing to repaint
198 * the animation stops running. Therefore if we catch this situation
199 * and schedule a repaint on all outputs it will be avoided.
200 */
201 if (animation->view->output_mask == 0)
202 weston_compositor_schedule_repaint(compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200203}
204
Armin Krezović75e71062016-08-11 15:49:58 +0200205static void
206idle_animation_destroy(void *data)
207{
208 struct weston_view_animation *animation = data;
209
210 weston_view_animation_destroy(animation);
211}
212
Jason Ekstranda7af7042013-10-12 22:38:11 -0500213static struct weston_view_animation *
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300214weston_view_animation_create(struct weston_view *view,
215 float start, float stop,
216 weston_view_animation_frame_func_t frame,
217 weston_view_animation_frame_func_t reset,
218 weston_view_animation_done_func_t done,
219 void *data,
220 void *private)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200221{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500222 struct weston_view_animation *animation;
Armin Krezović75e71062016-08-11 15:49:58 +0200223 struct weston_compositor *ec = view->surface->compositor;
224 struct wl_event_loop *loop;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200225
226 animation = malloc(sizeof *animation);
227 if (!animation)
228 return NULL;
229
Jason Ekstranda7af7042013-10-12 22:38:11 -0500230 animation->view = view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200231 animation->frame = frame;
Axel Davy5e396ae2013-09-17 14:55:55 +0200232 animation->reset = reset;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200233 animation->done = done;
234 animation->data = data;
235 animation->start = start;
236 animation->stop = stop;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100237 animation->private = private;
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300238
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200239 weston_matrix_init(&animation->transform.matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500240 wl_list_insert(&view->geometry.transformation_list,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200241 &animation->transform.link);
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300242
Jason Ekstranda7af7042013-10-12 22:38:11 -0500243 animation->animation.frame = weston_view_animation_frame;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200244
Jason Ekstranda7af7042013-10-12 22:38:11 -0500245 animation->listener.notify = handle_animation_view_destroy;
246 wl_signal_add(&view->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200247
Armin Krezović75e71062016-08-11 15:49:58 +0200248 if (view->output) {
249 wl_list_insert(&view->output->animation_list,
250 &animation->animation.link);
251 } else {
252 wl_list_init(&animation->animation.link);
253 loop = wl_display_get_event_loop(ec->wl_display);
254 wl_event_loop_add_idle(loop, idle_animation_destroy, animation);
255 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200256
257 return animation;
258}
259
260static void
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300261weston_view_animation_run(struct weston_view_animation *animation)
262{
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200263 struct timespec zero_time = { 0 };
264
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300265 animation->animation.frame_counter = 0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +0200266 weston_view_animation_frame(&animation->animation, NULL, &zero_time);
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300267}
268
269static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500270reset_alpha(struct weston_view_animation *animation)
Axel Davy5e396ae2013-09-17 14:55:55 +0200271{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500272 struct weston_view *view = animation->view;
Axel Davy5e396ae2013-09-17 14:55:55 +0200273
Jason Ekstranda7af7042013-10-12 22:38:11 -0500274 view->alpha = animation->stop;
Axel Davy5e396ae2013-09-17 14:55:55 +0200275}
276
277static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278zoom_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200279{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500280 struct weston_view *es = animation->view;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200281 float scale;
282
283 scale = animation->start +
284 (animation->stop - animation->start) *
285 animation->spring.current;
286 weston_matrix_init(&animation->transform.matrix);
287 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600288 -0.5f * es->surface->width,
289 -0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200290 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
291 weston_matrix_translate(&animation->transform.matrix,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600292 0.5f * es->surface->width,
293 0.5f * es->surface->height, 0);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200294
295 es->alpha = animation->spring.current;
296 if (es->alpha > 1.0)
297 es->alpha = 1.0;
298}
299
Jason Ekstranda7af7042013-10-12 22:38:11 -0500300WL_EXPORT struct weston_view_animation *
301weston_zoom_run(struct weston_view *view, float start, float stop,
302 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200303{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304 struct weston_view_animation *zoom;
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400305
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300306 zoom = weston_view_animation_create(view, start, stop,
307 zoom_frame, reset_alpha,
308 done, data, NULL);
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400309
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800310 if (zoom == NULL)
311 return NULL;
312
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400313 weston_spring_init(&zoom->spring, 300.0, start, stop);
314 zoom->spring.friction = 1400;
315 zoom->spring.previous = start - (stop - start) * 0.03;
316
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300317 weston_view_animation_run(zoom);
318
Kristian Høgsberg1cfd4062013-06-17 11:08:11 -0400319 return zoom;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200320}
321
322static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500323fade_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200324{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200325 if (animation->spring.current > 0.999)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326 animation->view->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200327 else if (animation->spring.current < 0.001 )
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 animation->view->alpha = 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200329 else
Jason Ekstranda7af7042013-10-12 22:38:11 -0500330 animation->view->alpha = animation->spring.current;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200331}
332
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333WL_EXPORT struct weston_view_animation *
334weston_fade_run(struct weston_view *view,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200335 float start, float end, float k,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500336 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200337{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 struct weston_view_animation *fade;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200339
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300340 fade = weston_view_animation_create(view, start, end,
341 fade_frame, reset_alpha,
342 done, data, NULL);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200343
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800344 if (fade == NULL)
345 return NULL;
346
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300347 weston_spring_init(&fade->spring, 1000.0, start, end);
Kristian Høgsberg3a869012014-03-19 16:45:23 -0700348 fade->spring.friction = 4000;
349 fade->spring.previous = start - (end - start) * 0.1;
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400350
Jason Ekstranda7af7042013-10-12 22:38:11 -0500351 view->alpha = start;
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200352
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300353 weston_view_animation_run(fade);
354
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200355 return fade;
356}
357
358WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500359weston_fade_update(struct weston_view_animation *fade, float target)
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200360{
Kristian Høgsberg5281fb12013-06-17 10:10:28 -0400361 fade->spring.target = target;
Jonny Lamb70ee3ad2014-07-30 00:56:18 +0100362 fade->stop = target;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200363}
364
365static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100366stable_fade_frame(struct weston_view_animation *animation)
367{
368 struct weston_view *back_view;
369
370 if (animation->spring.current > 0.999)
371 animation->view->alpha = 1;
372 else if (animation->spring.current < 0.001 )
373 animation->view->alpha = 0;
374 else
375 animation->view->alpha = animation->spring.current;
376
377 back_view = (struct weston_view *) animation->private;
378 back_view->alpha =
379 (animation->spring.target - animation->view->alpha) /
380 (1.0 - animation->view->alpha);
381 weston_view_geometry_dirty(back_view);
382}
383
384WL_EXPORT struct weston_view_animation *
385weston_stable_fade_run(struct weston_view *front_view, float start,
386 struct weston_view *back_view, float end,
387 weston_view_animation_done_func_t done, void *data)
388{
389 struct weston_view_animation *fade;
390
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300391 fade = weston_view_animation_create(front_view, 0, 0,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100392 stable_fade_frame, NULL,
393 done, data, back_view);
394
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800395 if (fade == NULL)
396 return NULL;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100397
398 weston_spring_init(&fade->spring, 400, start, end);
399 fade->spring.friction = 1150;
400
401 front_view->alpha = start;
402 back_view->alpha = end;
403
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300404 weston_view_animation_run(fade);
405
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100406 return fade;
407}
408
409static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410slide_frame(struct weston_view_animation *animation)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200411{
412 float scale;
413
414 scale = animation->start +
415 (animation->stop - animation->start) *
416 animation->spring.current;
417 weston_matrix_init(&animation->transform.matrix);
418 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
419}
420
Jason Ekstranda7af7042013-10-12 22:38:11 -0500421WL_EXPORT struct weston_view_animation *
422weston_slide_run(struct weston_view *view, float start, float stop,
423 weston_view_animation_done_func_t done, void *data)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200424{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500425 struct weston_view_animation *animation;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200426
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300427 animation = weston_view_animation_create(view, start, stop,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500428 slide_frame, NULL, done,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100429 data, NULL);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200430 if (!animation)
431 return NULL;
432
Ander Conselvan de Oliveiraa4a6f162014-04-14 15:48:06 +0300433 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400434 animation->spring.friction = 600;
Kristian Høgsbergdd2df782013-06-17 10:31:58 -0400435 animation->spring.clip = WESTON_SPRING_BOUNCE;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200436
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300437 weston_view_animation_run(animation);
438
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200439 return animation;
440}
Daniel Stonea67e6b92013-11-19 11:37:13 +0100441
442struct weston_move_animation {
443 int dx;
444 int dy;
Quentin Glidic24d306c2016-08-10 15:53:33 +0200445 bool reverse;
446 bool scale;
Daniel Stonea67e6b92013-11-19 11:37:13 +0100447 weston_view_animation_done_func_t done;
448};
449
450static void
451move_frame(struct weston_view_animation *animation)
452{
453 struct weston_move_animation *move = animation->private;
454 float scale;
455 float progress = animation->spring.current;
456
457 if (move->reverse)
458 progress = 1.0 - progress;
459
460 scale = animation->start +
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200461 (animation->stop - animation->start) *
462 progress;
Daniel Stonea67e6b92013-11-19 11:37:13 +0100463 weston_matrix_init(&animation->transform.matrix);
Quentin Glidic24d306c2016-08-10 15:53:33 +0200464 if (move->scale)
465 weston_matrix_scale(&animation->transform.matrix, scale, scale,
466 1.0f);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100467 weston_matrix_translate(&animation->transform.matrix,
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200468 move->dx * progress, move->dy * progress,
469 0);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100470}
471
472static void
473move_done(struct weston_view_animation *animation, void *data)
474{
475 struct weston_move_animation *move = animation->private;
476
477 if (move->done)
478 move->done(animation, data);
479
480 free(move);
481}
482
Quentin Glidic24d306c2016-08-10 15:53:33 +0200483static struct weston_view_animation *
484weston_move_scale_run_internal(struct weston_view *view, int dx, int dy,
485 float start, float end, bool reverse, bool scale,
486 weston_view_animation_done_func_t done,
487 void *data)
Daniel Stonea67e6b92013-11-19 11:37:13 +0100488{
489 struct weston_move_animation *move;
490 struct weston_view_animation *animation;
491
492 move = malloc(sizeof(*move));
493 if (!move)
494 return NULL;
495 move->dx = dx;
496 move->dy = dy;
497 move->reverse = reverse;
Quentin Glidic24d306c2016-08-10 15:53:33 +0200498 move->scale = scale;
Daniel Stonea67e6b92013-11-19 11:37:13 +0100499 move->done = done;
500
Ander Conselvan de Oliveiraf5cc2b52014-04-14 15:48:05 +0300501 animation = weston_view_animation_create(view, start, end, move_frame,
502 NULL, move_done, data, move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800503
Dongjin Kimba89f002024-09-30 17:49:36 +0900504 if (animation == NULL) {
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300505 free(move);
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800506 return NULL;
Lucas Tanureed8dd4e2015-10-03 11:18:32 -0300507 }
U. Artie Eoffa62e0e02014-01-17 15:08:51 -0800508
Jonny Lamb91f80f22014-05-22 22:41:30 +0200509 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
Daniel Stonea67e6b92013-11-19 11:37:13 +0100510 animation->spring.friction = 1150;
511
Jonny Lamb91f80f22014-05-22 22:41:30 +0200512 weston_view_animation_run(animation);
513
Daniel Stonea67e6b92013-11-19 11:37:13 +0100514 return animation;
515}
Quentin Glidic24d306c2016-08-10 15:53:33 +0200516
517WL_EXPORT struct weston_view_animation *
518weston_move_scale_run(struct weston_view *view, int dx, int dy,
519 float start, float end, bool reverse,
520 weston_view_animation_done_func_t done, void *data)
521{
522 return weston_move_scale_run_internal(view, dx, dy, start, end, reverse,
523 true, done, data);
524}
525
526WL_EXPORT struct weston_view_animation *
527weston_move_run(struct weston_view *view, int dx, int dy,
528 float start, float end, bool reverse,
529 weston_view_animation_done_func_t done, void *data)
530{
531 return weston_move_scale_run_internal(view, dx, dy, start, end, reverse,
532 false, done, data);
533}