Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 4 | * 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 11 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 12 | * 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 30 | #include <stdint.h> |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <math.h> |
| 33 | |
| 34 | #include <unistd.h> |
| 35 | #include <fcntl.h> |
| 36 | |
| 37 | #include "compositor.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 38 | #include "shared/helpers.h" |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 39 | |
| 40 | WL_EXPORT void |
| 41 | weston_spring_init(struct weston_spring *spring, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 42 | double k, double current, double target) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 43 | { |
| 44 | spring->k = k; |
| 45 | spring->friction = 400.0; |
| 46 | spring->current = current; |
| 47 | spring->previous = current; |
| 48 | spring->target = target; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 49 | spring->clip = WESTON_SPRING_OVERSHOOT; |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 50 | spring->min = 0.0; |
| 51 | spring->max = 1.0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | WL_EXPORT void |
| 55 | weston_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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 82 | switch (spring->clip) { |
| 83 | case WESTON_SPRING_OVERSHOOT: |
| 84 | break; |
| 85 | |
| 86 | case WESTON_SPRING_CLAMP: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 87 | 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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 93 | } |
| 94 | break; |
| 95 | |
| 96 | case WESTON_SPRING_BOUNCE: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 97 | 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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 107 | } |
| 108 | break; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 111 | spring->timestamp += 4; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | WL_EXPORT int |
| 116 | weston_spring_done(struct weston_spring *spring) |
| 117 | { |
Kristian Høgsberg | c24744e | 2013-06-17 08:59:20 -0400 | [diff] [blame] | 118 | return fabs(spring->previous - spring->target) < 0.002 && |
| 119 | fabs(spring->current - spring->target) < 0.002; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 122 | typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 123 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 124 | struct weston_view_animation { |
| 125 | struct weston_view *view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 126 | struct weston_animation animation; |
| 127 | struct weston_spring spring; |
| 128 | struct weston_transform transform; |
| 129 | struct wl_listener listener; |
| 130 | float start, stop; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 131 | 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 134 | void *data; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 135 | void *private; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 138 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 139 | weston_view_animation_destroy(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 140 | { |
| 141 | wl_list_remove(&animation->animation.link); |
| 142 | wl_list_remove(&animation->listener.link); |
| 143 | wl_list_remove(&animation->transform.link); |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 144 | if (animation->reset) |
| 145 | animation->reset(animation); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 146 | weston_view_geometry_dirty(animation->view); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 147 | if (animation->done) |
| 148 | animation->done(animation, animation->data); |
| 149 | free(animation); |
| 150 | } |
| 151 | |
| 152 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 153 | handle_animation_view_destroy(struct wl_listener *listener, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 154 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 155 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 156 | container_of(listener, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 157 | struct weston_view_animation, listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 158 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 159 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 163 | weston_view_animation_frame(struct weston_animation *base, |
| 164 | struct weston_output *output, uint32_t msecs) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 165 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 166 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 167 | container_of(base, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 168 | struct weston_view_animation, animation); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame] | 169 | struct weston_compositor *compositor = |
| 170 | animation->view->surface->compositor; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 171 | |
| 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øgsberg | 90dfb11 | 2013-10-30 09:07:04 -0700 | [diff] [blame] | 178 | weston_view_schedule_repaint(animation->view); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 179 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | |
| 183 | if (animation->frame) |
| 184 | animation->frame(animation); |
| 185 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 186 | weston_view_geometry_dirty(animation->view); |
| 187 | weston_view_schedule_repaint(animation->view); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame] | 188 | |
| 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 199 | static void |
| 200 | idle_animation_destroy(void *data) |
| 201 | { |
| 202 | struct weston_view_animation *animation = data; |
| 203 | |
| 204 | weston_view_animation_destroy(animation); |
| 205 | } |
| 206 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 207 | static struct weston_view_animation * |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 208 | weston_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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 215 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 216 | struct weston_view_animation *animation; |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 217 | struct weston_compositor *ec = view->surface->compositor; |
| 218 | struct wl_event_loop *loop; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 219 | |
| 220 | animation = malloc(sizeof *animation); |
| 221 | if (!animation) |
| 222 | return NULL; |
| 223 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 224 | animation->view = view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 225 | animation->frame = frame; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 226 | animation->reset = reset; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 227 | animation->done = done; |
| 228 | animation->data = data; |
| 229 | animation->start = start; |
| 230 | animation->stop = stop; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 231 | animation->private = private; |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 232 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 233 | weston_matrix_init(&animation->transform.matrix); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 234 | wl_list_insert(&view->geometry.transformation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 235 | &animation->transform.link); |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 236 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 237 | animation->animation.frame = weston_view_animation_frame; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 238 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 239 | animation->listener.notify = handle_animation_view_destroy; |
| 240 | wl_signal_add(&view->destroy_signal, &animation->listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 241 | |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 242 | 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 250 | |
| 251 | return animation; |
| 252 | } |
| 253 | |
| 254 | static void |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 255 | weston_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 | |
| 261 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 262 | reset_alpha(struct weston_view_animation *animation) |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 263 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 264 | struct weston_view *view = animation->view; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 265 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 266 | view->alpha = animation->stop; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 270 | zoom_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 271 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 272 | struct weston_view *es = animation->view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 273 | 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 Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 280 | -0.5f * es->surface->width, |
| 281 | -0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 282 | weston_matrix_scale(&animation->transform.matrix, scale, scale, scale); |
| 283 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 284 | 0.5f * es->surface->width, |
| 285 | 0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 286 | |
| 287 | es->alpha = animation->spring.current; |
| 288 | if (es->alpha > 1.0) |
| 289 | es->alpha = 1.0; |
| 290 | } |
| 291 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 292 | WL_EXPORT struct weston_view_animation * |
| 293 | weston_zoom_run(struct weston_view *view, float start, float stop, |
| 294 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 295 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 296 | struct weston_view_animation *zoom; |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 297 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 298 | zoom = weston_view_animation_create(view, start, stop, |
| 299 | zoom_frame, reset_alpha, |
| 300 | done, data, NULL); |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 301 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 302 | if (zoom == NULL) |
| 303 | return NULL; |
| 304 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 305 | 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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 309 | weston_view_animation_run(zoom); |
| 310 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 311 | return zoom; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 315 | fade_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 316 | { |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 317 | if (animation->spring.current > 0.999) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 318 | animation->view->alpha = 1; |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 319 | else if (animation->spring.current < 0.001 ) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 320 | animation->view->alpha = 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 321 | else |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 322 | animation->view->alpha = animation->spring.current; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 325 | WL_EXPORT struct weston_view_animation * |
| 326 | weston_fade_run(struct weston_view *view, |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 327 | float start, float end, float k, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 328 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 329 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 330 | struct weston_view_animation *fade; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 331 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 332 | fade = weston_view_animation_create(view, start, end, |
| 333 | fade_frame, reset_alpha, |
| 334 | done, data, NULL); |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 335 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 336 | if (fade == NULL) |
| 337 | return NULL; |
| 338 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 339 | weston_spring_init(&fade->spring, 1000.0, start, end); |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame] | 340 | fade->spring.friction = 4000; |
| 341 | fade->spring.previous = start - (end - start) * 0.1; |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 342 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 343 | view->alpha = start; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 344 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 345 | weston_view_animation_run(fade); |
| 346 | |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 347 | return fade; |
| 348 | } |
| 349 | |
| 350 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 351 | weston_fade_update(struct weston_view_animation *fade, float target) |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 352 | { |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 353 | fade->spring.target = target; |
Jonny Lamb | 70ee3ad | 2014-07-30 00:56:18 +0100 | [diff] [blame] | 354 | fade->stop = target; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static void |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 358 | stable_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 | |
| 376 | WL_EXPORT struct weston_view_animation * |
| 377 | weston_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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 383 | fade = weston_view_animation_create(front_view, 0, 0, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 384 | stable_fade_frame, NULL, |
| 385 | done, data, back_view); |
| 386 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 387 | if (fade == NULL) |
| 388 | return NULL; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 389 | |
| 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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 396 | weston_view_animation_run(fade); |
| 397 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 398 | return fade; |
| 399 | } |
| 400 | |
| 401 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 402 | slide_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 403 | { |
| 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 Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 413 | WL_EXPORT struct weston_view_animation * |
| 414 | weston_slide_run(struct weston_view *view, float start, float stop, |
| 415 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 416 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 417 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 418 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 419 | animation = weston_view_animation_create(view, start, stop, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 420 | slide_frame, NULL, done, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 421 | data, NULL); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 422 | if (!animation) |
| 423 | return NULL; |
| 424 | |
Ander Conselvan de Oliveira | a4a6f16 | 2014-04-14 15:48:06 +0300 | [diff] [blame] | 425 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 426 | animation->spring.friction = 600; |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 427 | animation->spring.clip = WESTON_SPRING_BOUNCE; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 428 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 429 | weston_view_animation_run(animation); |
| 430 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 431 | return animation; |
| 432 | } |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 433 | |
| 434 | struct weston_move_animation { |
| 435 | int dx; |
| 436 | int dy; |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 437 | bool reverse; |
| 438 | bool scale; |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 439 | weston_view_animation_done_func_t done; |
| 440 | }; |
| 441 | |
| 442 | static void |
| 443 | move_frame(struct weston_view_animation *animation) |
| 444 | { |
| 445 | struct weston_move_animation *move = animation->private; |
| 446 | float scale; |
| 447 | float progress = animation->spring.current; |
| 448 | |
| 449 | if (move->reverse) |
| 450 | progress = 1.0 - progress; |
| 451 | |
| 452 | scale = animation->start + |
| 453 | (animation->stop - animation->start) * |
| 454 | progress; |
| 455 | weston_matrix_init(&animation->transform.matrix); |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 456 | if (move->scale) |
| 457 | weston_matrix_scale(&animation->transform.matrix, scale, scale, |
| 458 | 1.0f); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 459 | weston_matrix_translate(&animation->transform.matrix, |
| 460 | move->dx * progress, move->dy * progress, |
| 461 | 0); |
| 462 | } |
| 463 | |
| 464 | static void |
| 465 | move_done(struct weston_view_animation *animation, void *data) |
| 466 | { |
| 467 | struct weston_move_animation *move = animation->private; |
| 468 | |
| 469 | if (move->done) |
| 470 | move->done(animation, data); |
| 471 | |
| 472 | free(move); |
| 473 | } |
| 474 | |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 475 | static struct weston_view_animation * |
| 476 | weston_move_scale_run_internal(struct weston_view *view, int dx, int dy, |
| 477 | float start, float end, bool reverse, bool scale, |
| 478 | weston_view_animation_done_func_t done, |
| 479 | void *data) |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 480 | { |
| 481 | struct weston_move_animation *move; |
| 482 | struct weston_view_animation *animation; |
| 483 | |
| 484 | move = malloc(sizeof(*move)); |
| 485 | if (!move) |
| 486 | return NULL; |
| 487 | move->dx = dx; |
| 488 | move->dy = dy; |
| 489 | move->reverse = reverse; |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 490 | move->scale = scale; |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 491 | move->done = done; |
| 492 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 493 | animation = weston_view_animation_create(view, start, end, move_frame, |
| 494 | NULL, move_done, data, move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 495 | |
Lucas Tanure | ed8dd4e | 2015-10-03 11:18:32 -0300 | [diff] [blame] | 496 | if (animation == NULL){ |
| 497 | free(move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 498 | return NULL; |
Lucas Tanure | ed8dd4e | 2015-10-03 11:18:32 -0300 | [diff] [blame] | 499 | } |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 500 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 501 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 502 | animation->spring.friction = 1150; |
| 503 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 504 | weston_view_animation_run(animation); |
| 505 | |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 506 | return animation; |
| 507 | } |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 508 | |
| 509 | WL_EXPORT struct weston_view_animation * |
| 510 | weston_move_scale_run(struct weston_view *view, int dx, int dy, |
| 511 | float start, float end, bool reverse, |
| 512 | weston_view_animation_done_func_t done, void *data) |
| 513 | { |
| 514 | return weston_move_scale_run_internal(view, dx, dy, start, end, reverse, |
| 515 | true, done, data); |
| 516 | } |
| 517 | |
| 518 | WL_EXPORT struct weston_view_animation * |
| 519 | weston_move_run(struct weston_view *view, int dx, int dy, |
| 520 | float start, float end, bool reverse, |
| 521 | weston_view_animation_done_func_t done, void *data) |
| 522 | { |
| 523 | return weston_move_scale_run_internal(view, dx, dy, start, end, reverse, |
| 524 | false, done, data); |
| 525 | } |