blob: 57d384d2d5ef2cc7c3ed9c9e21677baa340d554e [file] [log] [blame]
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020025#include <stdlib.h>
26#include <string.h>
27#include <stdio.h>
28#include <math.h>
29
30#include <unistd.h>
31#include <fcntl.h>
32
33#include "compositor.h"
34
35WL_EXPORT void
36weston_spring_init(struct weston_spring *spring,
37 double k, double current, double target)
38{
39 spring->k = k;
40 spring->friction = 400.0;
41 spring->current = current;
42 spring->previous = current;
43 spring->target = target;
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040044 spring->clip = WESTON_SPRING_OVERSHOOT;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020045}
46
47WL_EXPORT void
48weston_spring_update(struct weston_spring *spring, uint32_t msec)
49{
50 double force, v, current, step;
51
52 /* Limit the number of executions of the loop below by ensuring that
53 * the timestamp for last update of the spring is no more than 1s ago.
54 * This handles the case where time moves backwards or forwards in
55 * large jumps.
56 */
57 if (msec - spring->timestamp > 1000) {
58 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
59 spring->timestamp, msec);
60 spring->timestamp = msec - 1000;
61 }
62
63 step = 0.01;
64 while (4 < msec - spring->timestamp) {
65 current = spring->current;
66 v = current - spring->previous;
67 force = spring->k * (spring->target - current) / 10.0 +
68 (spring->previous - current) - v * spring->friction;
69
70 spring->current =
71 current + (current - spring->previous) +
72 force * step * step;
73 spring->previous = current;
74
Kristian Høgsberg7eec9b32013-06-17 09:15:22 -040075 switch (spring->clip) {
76 case WESTON_SPRING_OVERSHOOT:
77 break;
78
79 case WESTON_SPRING_CLAMP:
80 if (spring->current >= 1.0) {
81 spring->current = 1.0;
82 spring->previous = 1.0;
83 } else if (spring->current <= 0.0) {
84 spring->current = 0.0;
85 spring->previous = 0.0;
86 }
87 break;
88
89 case WESTON_SPRING_BOUNCE:
90 if (spring->current >= 1.0) {
91 spring->current = 2.0 - spring->current;
92 spring->previous = 2.0 - spring->previous;
93 } else if (spring->current <= 0.0) {
94 spring->current = -spring->current;
95 spring->previous = -spring->previous;
96 }
97 break;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020098 }
99
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200100 spring->timestamp += 4;
101 }
102}
103
104WL_EXPORT int
105weston_spring_done(struct weston_spring *spring)
106{
Kristian Høgsbergc24744e2013-06-17 08:59:20 -0400107 return fabs(spring->previous - spring->target) < 0.002 &&
108 fabs(spring->current - spring->target) < 0.002;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200109}
110
111typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation);
112
113struct weston_surface_animation {
114 struct weston_surface *surface;
115 struct weston_animation animation;
116 struct weston_spring spring;
117 struct weston_transform transform;
118 struct wl_listener listener;
119 float start, stop;
120 weston_surface_animation_frame_func_t frame;
121 weston_surface_animation_done_func_t done;
122 void *data;
123};
124
125static void
126weston_surface_animation_destroy(struct weston_surface_animation *animation)
127{
128 wl_list_remove(&animation->animation.link);
129 wl_list_remove(&animation->listener.link);
130 wl_list_remove(&animation->transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200131 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200132 if (animation->done)
133 animation->done(animation, animation->data);
134 free(animation);
135}
136
137static void
138handle_animation_surface_destroy(struct wl_listener *listener, void *data)
139{
140 struct weston_surface_animation *animation =
141 container_of(listener,
142 struct weston_surface_animation, listener);
143
144 weston_surface_animation_destroy(animation);
145}
146
147static void
148weston_surface_animation_frame(struct weston_animation *base,
149 struct weston_output *output, uint32_t msecs)
150{
151 struct weston_surface_animation *animation =
152 container_of(base,
153 struct weston_surface_animation, animation);
154
155 if (base->frame_counter <= 1)
156 animation->spring.timestamp = msecs;
157
158 weston_spring_update(&animation->spring, msecs);
159
160 if (weston_spring_done(&animation->spring)) {
161 weston_surface_animation_destroy(animation);
162 return;
163 }
164
165 if (animation->frame)
166 animation->frame(animation);
167
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200168 weston_surface_geometry_dirty(animation->surface);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +0200169 weston_compositor_schedule_repaint(animation->surface->compositor);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200170}
171
172static struct weston_surface_animation *
173weston_surface_animation_run(struct weston_surface *surface,
174 float start, float stop,
175 weston_surface_animation_frame_func_t frame,
176 weston_surface_animation_done_func_t done,
177 void *data)
178{
179 struct weston_surface_animation *animation;
180
181 animation = malloc(sizeof *animation);
182 if (!animation)
183 return NULL;
184
185 animation->surface = surface;
186 animation->frame = frame;
187 animation->done = done;
188 animation->data = data;
189 animation->start = start;
190 animation->stop = stop;
191 weston_matrix_init(&animation->transform.matrix);
192 wl_list_insert(&surface->geometry.transformation_list,
193 &animation->transform.link);
194 weston_spring_init(&animation->spring, 200.0, 0.0, 1.0);
195 animation->spring.friction = 700;
196 animation->animation.frame_counter = 0;
197 animation->animation.frame = weston_surface_animation_frame;
198 weston_surface_animation_frame(&animation->animation, NULL, 0);
199
200 animation->listener.notify = handle_animation_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500201 wl_signal_add(&surface->destroy_signal, &animation->listener);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200202
203 wl_list_insert(&surface->output->animation_list,
204 &animation->animation.link);
205
206 return animation;
207}
208
209static void
210zoom_frame(struct weston_surface_animation *animation)
211{
212 struct weston_surface *es = animation->surface;
213 float scale;
214
215 scale = animation->start +
216 (animation->stop - animation->start) *
217 animation->spring.current;
218 weston_matrix_init(&animation->transform.matrix);
219 weston_matrix_translate(&animation->transform.matrix,
220 -0.5f * es->geometry.width,
221 -0.5f * es->geometry.height, 0);
222 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
223 weston_matrix_translate(&animation->transform.matrix,
224 0.5f * es->geometry.width,
225 0.5f * es->geometry.height, 0);
226
227 es->alpha = animation->spring.current;
228 if (es->alpha > 1.0)
229 es->alpha = 1.0;
230}
231
232WL_EXPORT struct weston_surface_animation *
233weston_zoom_run(struct weston_surface *surface, float start, float stop,
234 weston_surface_animation_done_func_t done, void *data)
235{
236 return weston_surface_animation_run(surface, start, stop,
237 zoom_frame, done, data);
238}
239
240static void
241fade_frame(struct weston_surface_animation *animation)
242{
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200243 if (animation->spring.current > 0.999)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200244 animation->surface->alpha = 1;
Ander Conselvan de Oliveira8a913242013-02-21 18:35:18 +0200245 else if (animation->spring.current < 0.001 )
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200246 animation->surface->alpha = 0;
247 else
248 animation->surface->alpha = animation->spring.current;
249}
250
251WL_EXPORT struct weston_surface_animation *
252weston_fade_run(struct weston_surface *surface,
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200253 float start, float end, float k,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200254 weston_surface_animation_done_func_t done, void *data)
255{
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200256 struct weston_surface_animation *fade;
257
258 fade = weston_surface_animation_run(surface, 0, 0,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200259 fade_frame, done, data);
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +0200260
261 weston_spring_init(&fade->spring, k, start, end);
262 surface->alpha = start;
263
264 return fade;
265}
266
267WL_EXPORT void
268weston_fade_update(struct weston_surface_animation *fade,
269 float start, float end, float k)
270{
271 weston_spring_init(&fade->spring, k, start, end);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200272}
273
274static void
275slide_frame(struct weston_surface_animation *animation)
276{
277 float scale;
278
279 scale = animation->start +
280 (animation->stop - animation->start) *
281 animation->spring.current;
282 weston_matrix_init(&animation->transform.matrix);
283 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
284}
285
286WL_EXPORT struct weston_surface_animation *
287weston_slide_run(struct weston_surface *surface, float start, float stop,
288 weston_surface_animation_done_func_t done, void *data)
289{
290 struct weston_surface_animation *animation;
291
292 animation = weston_surface_animation_run(surface, start, stop,
293 slide_frame, done, data);
294 if (!animation)
295 return NULL;
296
297 animation->spring.friction = 900;
298 animation->spring.k = 300;
299
300 return animation;
301}