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> |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 33 | #include <inttypes.h> |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 34 | |
| 35 | #include <unistd.h> |
| 36 | #include <fcntl.h> |
| 37 | |
| 38 | #include "compositor.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 39 | #include "shared/helpers.h" |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 40 | #include "shared/timespec-util.h" |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 41 | |
| 42 | WL_EXPORT void |
| 43 | weston_spring_init(struct weston_spring *spring, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 44 | double k, double current, double target) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 45 | { |
| 46 | spring->k = k; |
| 47 | spring->friction = 400.0; |
| 48 | spring->current = current; |
| 49 | spring->previous = current; |
| 50 | spring->target = target; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 51 | spring->clip = WESTON_SPRING_OVERSHOOT; |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 52 | spring->min = 0.0; |
| 53 | spring->max = 1.0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | WL_EXPORT void |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 57 | weston_spring_update(struct weston_spring *spring, const struct timespec *time) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 58 | { |
| 59 | double force, v, current, step; |
| 60 | |
| 61 | /* Limit the number of executions of the loop below by ensuring that |
| 62 | * the timestamp for last update of the spring is no more than 1s ago. |
| 63 | * This handles the case where time moves backwards or forwards in |
| 64 | * large jumps. |
| 65 | */ |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 66 | if (timespec_sub_to_msec(time, &spring->timestamp) > 1000) { |
| 67 | weston_log("unexpectedly large timestamp jump " |
| 68 | "(from %" PRId64 " to %" PRId64 ")\n", |
| 69 | timespec_to_msec(&spring->timestamp), |
| 70 | timespec_to_msec(time)); |
| 71 | timespec_add_msec(&spring->timestamp, time, -1000); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | step = 0.01; |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 75 | while (4 < timespec_sub_to_msec(time, &spring->timestamp)) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 76 | current = spring->current; |
| 77 | v = current - spring->previous; |
| 78 | force = spring->k * (spring->target - current) / 10.0 + |
| 79 | (spring->previous - current) - v * spring->friction; |
| 80 | |
| 81 | spring->current = |
| 82 | current + (current - spring->previous) + |
| 83 | force * step * step; |
| 84 | spring->previous = current; |
| 85 | |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 86 | switch (spring->clip) { |
| 87 | case WESTON_SPRING_OVERSHOOT: |
| 88 | break; |
| 89 | |
| 90 | case WESTON_SPRING_CLAMP: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 91 | if (spring->current > spring->max) { |
| 92 | spring->current = spring->max; |
| 93 | spring->previous = spring->max; |
| 94 | } else if (spring->current < 0.0) { |
| 95 | spring->current = spring->min; |
| 96 | spring->previous = spring->min; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 97 | } |
| 98 | break; |
| 99 | |
| 100 | case WESTON_SPRING_BOUNCE: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 101 | if (spring->current > spring->max) { |
| 102 | spring->current = |
| 103 | 2 * spring->max - spring->current; |
| 104 | spring->previous = |
| 105 | 2 * spring->max - spring->previous; |
| 106 | } else if (spring->current < spring->min) { |
| 107 | spring->current = |
| 108 | 2 * spring->min - spring->current; |
| 109 | spring->previous = |
| 110 | 2 * spring->min - spring->previous; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 111 | } |
| 112 | break; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 115 | timespec_add_msec(&spring->timestamp, &spring->timestamp, 4); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | WL_EXPORT int |
| 120 | weston_spring_done(struct weston_spring *spring) |
| 121 | { |
Kristian Høgsberg | c24744e | 2013-06-17 08:59:20 -0400 | [diff] [blame] | 122 | return fabs(spring->previous - spring->target) < 0.002 && |
| 123 | fabs(spring->current - spring->target) < 0.002; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 126 | 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] | 127 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 128 | struct weston_view_animation { |
| 129 | struct weston_view *view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 130 | struct weston_animation animation; |
| 131 | struct weston_spring spring; |
| 132 | struct weston_transform transform; |
| 133 | struct wl_listener listener; |
| 134 | float start, stop; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 135 | weston_view_animation_frame_func_t frame; |
| 136 | weston_view_animation_frame_func_t reset; |
| 137 | weston_view_animation_done_func_t done; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 138 | void *data; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 139 | void *private; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 142 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 143 | weston_view_animation_destroy(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 144 | { |
| 145 | wl_list_remove(&animation->animation.link); |
| 146 | wl_list_remove(&animation->listener.link); |
| 147 | wl_list_remove(&animation->transform.link); |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 148 | if (animation->reset) |
| 149 | animation->reset(animation); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 150 | weston_view_geometry_dirty(animation->view); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 151 | if (animation->done) |
| 152 | animation->done(animation, animation->data); |
| 153 | free(animation); |
| 154 | } |
| 155 | |
| 156 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 157 | handle_animation_view_destroy(struct wl_listener *listener, void *data) |
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 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 160 | container_of(listener, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 161 | struct weston_view_animation, listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 162 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 163 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 167 | weston_view_animation_frame(struct weston_animation *base, |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 168 | struct weston_output *output, |
| 169 | const struct timespec *time) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 170 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 171 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 172 | container_of(base, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 173 | struct weston_view_animation, animation); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame] | 174 | struct weston_compositor *compositor = |
| 175 | animation->view->surface->compositor; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 176 | |
| 177 | if (base->frame_counter <= 1) |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 178 | animation->spring.timestamp = *time; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 179 | |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 180 | weston_spring_update(&animation->spring, time); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 181 | |
| 182 | if (weston_spring_done(&animation->spring)) { |
Kristian Høgsberg | 90dfb11 | 2013-10-30 09:07:04 -0700 | [diff] [blame] | 183 | weston_view_schedule_repaint(animation->view); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 184 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (animation->frame) |
| 189 | animation->frame(animation); |
| 190 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 191 | weston_view_geometry_dirty(animation->view); |
| 192 | weston_view_schedule_repaint(animation->view); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame] | 193 | |
| 194 | /* The view's output_mask will be zero if its position is |
| 195 | * offscreen. Animations should always run but as they are also |
| 196 | * run off the repaint cycle, if there's nothing to repaint |
| 197 | * the animation stops running. Therefore if we catch this situation |
| 198 | * and schedule a repaint on all outputs it will be avoided. |
| 199 | */ |
| 200 | if (animation->view->output_mask == 0) |
| 201 | weston_compositor_schedule_repaint(compositor); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 204 | static void |
| 205 | idle_animation_destroy(void *data) |
| 206 | { |
| 207 | struct weston_view_animation *animation = data; |
| 208 | |
| 209 | weston_view_animation_destroy(animation); |
| 210 | } |
| 211 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 212 | static struct weston_view_animation * |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 213 | weston_view_animation_create(struct weston_view *view, |
| 214 | float start, float stop, |
| 215 | weston_view_animation_frame_func_t frame, |
| 216 | weston_view_animation_frame_func_t reset, |
| 217 | weston_view_animation_done_func_t done, |
| 218 | void *data, |
| 219 | void *private) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 220 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 221 | struct weston_view_animation *animation; |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 222 | struct weston_compositor *ec = view->surface->compositor; |
| 223 | struct wl_event_loop *loop; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 224 | |
| 225 | animation = malloc(sizeof *animation); |
| 226 | if (!animation) |
| 227 | return NULL; |
| 228 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 229 | animation->view = view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 230 | animation->frame = frame; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 231 | animation->reset = reset; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 232 | animation->done = done; |
| 233 | animation->data = data; |
| 234 | animation->start = start; |
| 235 | animation->stop = stop; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 236 | animation->private = private; |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 237 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 238 | weston_matrix_init(&animation->transform.matrix); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 239 | wl_list_insert(&view->geometry.transformation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 240 | &animation->transform.link); |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 241 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 242 | animation->animation.frame = weston_view_animation_frame; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 243 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 244 | animation->listener.notify = handle_animation_view_destroy; |
| 245 | wl_signal_add(&view->destroy_signal, &animation->listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 246 | |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 247 | if (view->output) { |
| 248 | wl_list_insert(&view->output->animation_list, |
| 249 | &animation->animation.link); |
| 250 | } else { |
| 251 | wl_list_init(&animation->animation.link); |
| 252 | loop = wl_display_get_event_loop(ec->wl_display); |
| 253 | wl_event_loop_add_idle(loop, idle_animation_destroy, animation); |
| 254 | } |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 255 | |
| 256 | return animation; |
| 257 | } |
| 258 | |
| 259 | static void |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 260 | weston_view_animation_run(struct weston_view_animation *animation) |
| 261 | { |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 262 | struct timespec zero_time = { 0 }; |
| 263 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 264 | animation->animation.frame_counter = 0; |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame^] | 265 | weston_view_animation_frame(&animation->animation, NULL, &zero_time); |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 269 | reset_alpha(struct weston_view_animation *animation) |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 270 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 271 | struct weston_view *view = animation->view; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 272 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 273 | view->alpha = animation->stop; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 277 | zoom_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 278 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 279 | struct weston_view *es = animation->view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 280 | float scale; |
| 281 | |
| 282 | scale = animation->start + |
| 283 | (animation->stop - animation->start) * |
| 284 | animation->spring.current; |
| 285 | weston_matrix_init(&animation->transform.matrix); |
| 286 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 287 | -0.5f * es->surface->width, |
| 288 | -0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 289 | weston_matrix_scale(&animation->transform.matrix, scale, scale, scale); |
| 290 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 291 | 0.5f * es->surface->width, |
| 292 | 0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 293 | |
| 294 | es->alpha = animation->spring.current; |
| 295 | if (es->alpha > 1.0) |
| 296 | es->alpha = 1.0; |
| 297 | } |
| 298 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 299 | WL_EXPORT struct weston_view_animation * |
| 300 | weston_zoom_run(struct weston_view *view, float start, float stop, |
| 301 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 302 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 303 | struct weston_view_animation *zoom; |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 304 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 305 | zoom = weston_view_animation_create(view, start, stop, |
| 306 | zoom_frame, reset_alpha, |
| 307 | done, data, NULL); |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 308 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 309 | if (zoom == NULL) |
| 310 | return NULL; |
| 311 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 312 | weston_spring_init(&zoom->spring, 300.0, start, stop); |
| 313 | zoom->spring.friction = 1400; |
| 314 | zoom->spring.previous = start - (stop - start) * 0.03; |
| 315 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 316 | weston_view_animation_run(zoom); |
| 317 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 318 | return zoom; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 322 | fade_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 323 | { |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 324 | if (animation->spring.current > 0.999) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 325 | animation->view->alpha = 1; |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 326 | else if (animation->spring.current < 0.001 ) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 327 | animation->view->alpha = 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 328 | else |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 329 | animation->view->alpha = animation->spring.current; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 332 | WL_EXPORT struct weston_view_animation * |
| 333 | weston_fade_run(struct weston_view *view, |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 334 | float start, float end, float k, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 335 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 336 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 337 | struct weston_view_animation *fade; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 338 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 339 | fade = weston_view_animation_create(view, start, end, |
| 340 | fade_frame, reset_alpha, |
| 341 | done, data, NULL); |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 342 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 343 | if (fade == NULL) |
| 344 | return NULL; |
| 345 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 346 | weston_spring_init(&fade->spring, 1000.0, start, end); |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame] | 347 | fade->spring.friction = 4000; |
| 348 | fade->spring.previous = start - (end - start) * 0.1; |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 349 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 350 | view->alpha = start; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 351 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 352 | weston_view_animation_run(fade); |
| 353 | |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 354 | return fade; |
| 355 | } |
| 356 | |
| 357 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 358 | weston_fade_update(struct weston_view_animation *fade, float target) |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 359 | { |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 360 | fade->spring.target = target; |
Jonny Lamb | 70ee3ad | 2014-07-30 00:56:18 +0100 | [diff] [blame] | 361 | fade->stop = target; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static void |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 365 | stable_fade_frame(struct weston_view_animation *animation) |
| 366 | { |
| 367 | struct weston_view *back_view; |
| 368 | |
| 369 | if (animation->spring.current > 0.999) |
| 370 | animation->view->alpha = 1; |
| 371 | else if (animation->spring.current < 0.001 ) |
| 372 | animation->view->alpha = 0; |
| 373 | else |
| 374 | animation->view->alpha = animation->spring.current; |
| 375 | |
| 376 | back_view = (struct weston_view *) animation->private; |
| 377 | back_view->alpha = |
| 378 | (animation->spring.target - animation->view->alpha) / |
| 379 | (1.0 - animation->view->alpha); |
| 380 | weston_view_geometry_dirty(back_view); |
| 381 | } |
| 382 | |
| 383 | WL_EXPORT struct weston_view_animation * |
| 384 | weston_stable_fade_run(struct weston_view *front_view, float start, |
| 385 | struct weston_view *back_view, float end, |
| 386 | weston_view_animation_done_func_t done, void *data) |
| 387 | { |
| 388 | struct weston_view_animation *fade; |
| 389 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 390 | fade = weston_view_animation_create(front_view, 0, 0, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 391 | stable_fade_frame, NULL, |
| 392 | done, data, back_view); |
| 393 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 394 | if (fade == NULL) |
| 395 | return NULL; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 396 | |
| 397 | weston_spring_init(&fade->spring, 400, start, end); |
| 398 | fade->spring.friction = 1150; |
| 399 | |
| 400 | front_view->alpha = start; |
| 401 | back_view->alpha = end; |
| 402 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 403 | weston_view_animation_run(fade); |
| 404 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 405 | return fade; |
| 406 | } |
| 407 | |
| 408 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 409 | slide_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 410 | { |
| 411 | float scale; |
| 412 | |
| 413 | scale = animation->start + |
| 414 | (animation->stop - animation->start) * |
| 415 | animation->spring.current; |
| 416 | weston_matrix_init(&animation->transform.matrix); |
| 417 | weston_matrix_translate(&animation->transform.matrix, 0, scale, 0); |
| 418 | } |
| 419 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 420 | WL_EXPORT struct weston_view_animation * |
| 421 | weston_slide_run(struct weston_view *view, float start, float stop, |
| 422 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 423 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 424 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 425 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 426 | animation = weston_view_animation_create(view, start, stop, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 427 | slide_frame, NULL, done, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 428 | data, NULL); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 429 | if (!animation) |
| 430 | return NULL; |
| 431 | |
Ander Conselvan de Oliveira | a4a6f16 | 2014-04-14 15:48:06 +0300 | [diff] [blame] | 432 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 433 | animation->spring.friction = 600; |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 434 | animation->spring.clip = WESTON_SPRING_BOUNCE; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 435 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 436 | weston_view_animation_run(animation); |
| 437 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 438 | return animation; |
| 439 | } |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 440 | |
| 441 | struct weston_move_animation { |
| 442 | int dx; |
| 443 | int dy; |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 444 | bool reverse; |
| 445 | bool scale; |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 446 | weston_view_animation_done_func_t done; |
| 447 | }; |
| 448 | |
| 449 | static void |
| 450 | move_frame(struct weston_view_animation *animation) |
| 451 | { |
| 452 | struct weston_move_animation *move = animation->private; |
| 453 | float scale; |
| 454 | float progress = animation->spring.current; |
| 455 | |
| 456 | if (move->reverse) |
| 457 | progress = 1.0 - progress; |
| 458 | |
| 459 | scale = animation->start + |
| 460 | (animation->stop - animation->start) * |
| 461 | progress; |
| 462 | weston_matrix_init(&animation->transform.matrix); |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 463 | if (move->scale) |
| 464 | weston_matrix_scale(&animation->transform.matrix, scale, scale, |
| 465 | 1.0f); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 466 | weston_matrix_translate(&animation->transform.matrix, |
| 467 | move->dx * progress, move->dy * progress, |
| 468 | 0); |
| 469 | } |
| 470 | |
| 471 | static void |
| 472 | move_done(struct weston_view_animation *animation, void *data) |
| 473 | { |
| 474 | struct weston_move_animation *move = animation->private; |
| 475 | |
| 476 | if (move->done) |
| 477 | move->done(animation, data); |
| 478 | |
| 479 | free(move); |
| 480 | } |
| 481 | |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 482 | static struct weston_view_animation * |
| 483 | weston_move_scale_run_internal(struct weston_view *view, int dx, int dy, |
| 484 | float start, float end, bool reverse, bool scale, |
| 485 | weston_view_animation_done_func_t done, |
| 486 | void *data) |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 487 | { |
| 488 | struct weston_move_animation *move; |
| 489 | struct weston_view_animation *animation; |
| 490 | |
| 491 | move = malloc(sizeof(*move)); |
| 492 | if (!move) |
| 493 | return NULL; |
| 494 | move->dx = dx; |
| 495 | move->dy = dy; |
| 496 | move->reverse = reverse; |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 497 | move->scale = scale; |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 498 | move->done = done; |
| 499 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 500 | animation = weston_view_animation_create(view, start, end, move_frame, |
| 501 | NULL, move_done, data, move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 502 | |
Lucas Tanure | ed8dd4e | 2015-10-03 11:18:32 -0300 | [diff] [blame] | 503 | if (animation == NULL){ |
| 504 | free(move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 505 | return NULL; |
Lucas Tanure | ed8dd4e | 2015-10-03 11:18:32 -0300 | [diff] [blame] | 506 | } |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 507 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 508 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 509 | animation->spring.friction = 1150; |
| 510 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 511 | weston_view_animation_run(animation); |
| 512 | |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 513 | return animation; |
| 514 | } |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 515 | |
| 516 | WL_EXPORT struct weston_view_animation * |
| 517 | weston_move_scale_run(struct weston_view *view, int dx, int dy, |
| 518 | float start, float end, bool reverse, |
| 519 | weston_view_animation_done_func_t done, void *data) |
| 520 | { |
| 521 | return weston_move_scale_run_internal(view, dx, dy, start, end, reverse, |
| 522 | true, done, data); |
| 523 | } |
| 524 | |
| 525 | WL_EXPORT struct weston_view_animation * |
| 526 | weston_move_run(struct weston_view *view, int dx, int dy, |
| 527 | float start, float end, bool reverse, |
| 528 | weston_view_animation_done_func_t done, void *data) |
| 529 | { |
| 530 | return weston_move_scale_run_internal(view, dx, dy, start, end, reverse, |
| 531 | false, done, data); |
| 532 | } |