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 | |
Pekka Paalanen | 3d5d947 | 2019-03-28 16:28:47 +0200 | [diff] [blame] | 38 | #include <libweston/libweston.h> |
Guillaume Champagne | f1e8fc9 | 2020-01-27 20:14:29 -0500 | [diff] [blame] | 39 | #include "libweston-internal.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 40 | #include "shared/helpers.h" |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 41 | #include "shared/timespec-util.h" |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 42 | |
| 43 | WL_EXPORT void |
| 44 | weston_spring_init(struct weston_spring *spring, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 45 | double k, double current, double target) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 46 | { |
| 47 | spring->k = k; |
| 48 | spring->friction = 400.0; |
| 49 | spring->current = current; |
| 50 | spring->previous = current; |
| 51 | spring->target = target; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 52 | spring->clip = WESTON_SPRING_OVERSHOOT; |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 53 | spring->min = 0.0; |
| 54 | spring->max = 1.0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | WL_EXPORT void |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 58 | 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] | 59 | { |
| 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 Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 67 | 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | step = 0.01; |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 76 | while (4 < timespec_sub_to_msec(time, &spring->timestamp)) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 77 | 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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 87 | switch (spring->clip) { |
| 88 | case WESTON_SPRING_OVERSHOOT: |
| 89 | break; |
| 90 | |
| 91 | case WESTON_SPRING_CLAMP: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 92 | 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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 98 | } |
| 99 | break; |
| 100 | |
| 101 | case WESTON_SPRING_BOUNCE: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 102 | 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øgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 112 | } |
| 113 | break; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 116 | timespec_add_msec(&spring->timestamp, &spring->timestamp, 4); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | WL_EXPORT int |
| 121 | weston_spring_done(struct weston_spring *spring) |
| 122 | { |
Kristian Høgsberg | c24744e | 2013-06-17 08:59:20 -0400 | [diff] [blame] | 123 | return fabs(spring->previous - spring->target) < 0.002 && |
| 124 | fabs(spring->current - spring->target) < 0.002; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 127 | 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] | 128 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 129 | struct weston_view_animation { |
| 130 | struct weston_view *view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 131 | struct weston_animation animation; |
| 132 | struct weston_spring spring; |
| 133 | struct weston_transform transform; |
| 134 | struct wl_listener listener; |
| 135 | float start, stop; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 136 | 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 139 | void *data; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 140 | void *private; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 141 | }; |
| 142 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 143 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 144 | weston_view_animation_destroy(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 145 | { |
| 146 | wl_list_remove(&animation->animation.link); |
| 147 | wl_list_remove(&animation->listener.link); |
| 148 | wl_list_remove(&animation->transform.link); |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 149 | if (animation->reset) |
| 150 | animation->reset(animation); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 151 | weston_view_geometry_dirty(animation->view); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 152 | if (animation->done) |
| 153 | animation->done(animation, animation->data); |
| 154 | free(animation); |
| 155 | } |
| 156 | |
| 157 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 158 | handle_animation_view_destroy(struct wl_listener *listener, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 159 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 160 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 161 | container_of(listener, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 162 | struct weston_view_animation, listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 163 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 164 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 168 | weston_view_animation_frame(struct weston_animation *base, |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 169 | struct weston_output *output, |
| 170 | const struct timespec *time) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 171 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 172 | struct weston_view_animation *animation = |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 173 | container_of(base, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 174 | struct weston_view_animation, animation); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame] | 175 | struct weston_compositor *compositor = |
| 176 | animation->view->surface->compositor; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 177 | |
| 178 | if (base->frame_counter <= 1) |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 179 | animation->spring.timestamp = *time; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 180 | |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 181 | weston_spring_update(&animation->spring, time); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 182 | |
| 183 | if (weston_spring_done(&animation->spring)) { |
Kristian Høgsberg | 90dfb11 | 2013-10-30 09:07:04 -0700 | [diff] [blame] | 184 | weston_view_schedule_repaint(animation->view); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 185 | weston_view_animation_destroy(animation); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (animation->frame) |
| 190 | animation->frame(animation); |
| 191 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 192 | weston_view_geometry_dirty(animation->view); |
| 193 | weston_view_schedule_repaint(animation->view); |
Jonny Lamb | f8bfd05 | 2014-05-22 22:41:33 +0200 | [diff] [blame] | 194 | |
| 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 205 | static void |
| 206 | idle_animation_destroy(void *data) |
| 207 | { |
| 208 | struct weston_view_animation *animation = data; |
| 209 | |
| 210 | weston_view_animation_destroy(animation); |
| 211 | } |
| 212 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 213 | static struct weston_view_animation * |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 214 | weston_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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 221 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 222 | struct weston_view_animation *animation; |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 223 | struct weston_compositor *ec = view->surface->compositor; |
| 224 | struct wl_event_loop *loop; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 225 | |
| 226 | animation = malloc(sizeof *animation); |
| 227 | if (!animation) |
| 228 | return NULL; |
| 229 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 230 | animation->view = view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 231 | animation->frame = frame; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 232 | animation->reset = reset; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 233 | animation->done = done; |
| 234 | animation->data = data; |
| 235 | animation->start = start; |
| 236 | animation->stop = stop; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 237 | animation->private = private; |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 238 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 239 | weston_matrix_init(&animation->transform.matrix); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 240 | wl_list_insert(&view->geometry.transformation_list, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 241 | &animation->transform.link); |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 242 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 243 | animation->animation.frame = weston_view_animation_frame; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 244 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 245 | animation->listener.notify = handle_animation_view_destroy; |
| 246 | wl_signal_add(&view->destroy_signal, &animation->listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 247 | |
Armin Krezović | 75e7106 | 2016-08-11 15:49:58 +0200 | [diff] [blame] | 248 | 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 Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 256 | |
| 257 | return animation; |
| 258 | } |
| 259 | |
| 260 | static void |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 261 | weston_view_animation_run(struct weston_view_animation *animation) |
| 262 | { |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 263 | struct timespec zero_time = { 0 }; |
| 264 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 265 | animation->animation.frame_counter = 0; |
Alexandros Frantzis | 8250a61 | 2017-11-16 18:20:52 +0200 | [diff] [blame] | 266 | weston_view_animation_frame(&animation->animation, NULL, &zero_time); |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 270 | reset_alpha(struct weston_view_animation *animation) |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 271 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 272 | struct weston_view *view = animation->view; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 273 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 274 | view->alpha = animation->stop; |
Axel Davy | 5e396ae | 2013-09-17 14:55:55 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 278 | zoom_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 279 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 280 | struct weston_view *es = animation->view; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 281 | 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 Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 288 | -0.5f * es->surface->width, |
| 289 | -0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 290 | weston_matrix_scale(&animation->transform.matrix, scale, scale, scale); |
| 291 | weston_matrix_translate(&animation->transform.matrix, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 292 | 0.5f * es->surface->width, |
| 293 | 0.5f * es->surface->height, 0); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 294 | |
| 295 | es->alpha = animation->spring.current; |
| 296 | if (es->alpha > 1.0) |
| 297 | es->alpha = 1.0; |
| 298 | } |
| 299 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 300 | WL_EXPORT struct weston_view_animation * |
| 301 | weston_zoom_run(struct weston_view *view, float start, float stop, |
| 302 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 303 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 304 | struct weston_view_animation *zoom; |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 305 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 306 | zoom = weston_view_animation_create(view, start, stop, |
| 307 | zoom_frame, reset_alpha, |
| 308 | done, data, NULL); |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 309 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 310 | if (zoom == NULL) |
| 311 | return NULL; |
| 312 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 313 | 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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 317 | weston_view_animation_run(zoom); |
| 318 | |
Kristian Høgsberg | 1cfd406 | 2013-06-17 11:08:11 -0400 | [diff] [blame] | 319 | return zoom; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 323 | fade_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 324 | { |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 325 | if (animation->spring.current > 0.999) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 326 | animation->view->alpha = 1; |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 327 | else if (animation->spring.current < 0.001 ) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 328 | animation->view->alpha = 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 329 | else |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 330 | animation->view->alpha = animation->spring.current; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 333 | WL_EXPORT struct weston_view_animation * |
| 334 | weston_fade_run(struct weston_view *view, |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 335 | float start, float end, float k, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 336 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 337 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 338 | struct weston_view_animation *fade; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 339 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 340 | fade = weston_view_animation_create(view, start, end, |
| 341 | fade_frame, reset_alpha, |
| 342 | done, data, NULL); |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 343 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 344 | if (fade == NULL) |
| 345 | return NULL; |
| 346 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 347 | weston_spring_init(&fade->spring, 1000.0, start, end); |
Kristian Høgsberg | 3a86901 | 2014-03-19 16:45:23 -0700 | [diff] [blame] | 348 | fade->spring.friction = 4000; |
| 349 | fade->spring.previous = start - (end - start) * 0.1; |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 350 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 351 | view->alpha = start; |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 352 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 353 | weston_view_animation_run(fade); |
| 354 | |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 355 | return fade; |
| 356 | } |
| 357 | |
| 358 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 359 | weston_fade_update(struct weston_view_animation *fade, float target) |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 360 | { |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 361 | fade->spring.target = target; |
Jonny Lamb | 70ee3ad | 2014-07-30 00:56:18 +0100 | [diff] [blame] | 362 | fade->stop = target; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 366 | stable_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 | |
| 384 | WL_EXPORT struct weston_view_animation * |
| 385 | weston_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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 391 | fade = weston_view_animation_create(front_view, 0, 0, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 392 | stable_fade_frame, NULL, |
| 393 | done, data, back_view); |
| 394 | |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 395 | if (fade == NULL) |
| 396 | return NULL; |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 397 | |
| 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 Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 404 | weston_view_animation_run(fade); |
| 405 | |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 406 | return fade; |
| 407 | } |
| 408 | |
| 409 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 410 | slide_frame(struct weston_view_animation *animation) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 411 | { |
| 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 Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 421 | WL_EXPORT struct weston_view_animation * |
| 422 | weston_slide_run(struct weston_view *view, float start, float stop, |
| 423 | weston_view_animation_done_func_t done, void *data) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 424 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 425 | struct weston_view_animation *animation; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 426 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 427 | animation = weston_view_animation_create(view, start, stop, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 428 | slide_frame, NULL, done, |
Louis-Francis Ratté-Boulianne | b482dbd | 2013-11-19 11:37:11 +0100 | [diff] [blame] | 429 | data, NULL); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 430 | if (!animation) |
| 431 | return NULL; |
| 432 | |
Ander Conselvan de Oliveira | a4a6f16 | 2014-04-14 15:48:06 +0300 | [diff] [blame] | 433 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 434 | animation->spring.friction = 600; |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 435 | animation->spring.clip = WESTON_SPRING_BOUNCE; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 436 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 437 | weston_view_animation_run(animation); |
| 438 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 439 | return animation; |
| 440 | } |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 441 | |
| 442 | struct weston_move_animation { |
| 443 | int dx; |
| 444 | int dy; |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 445 | bool reverse; |
| 446 | bool scale; |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 447 | weston_view_animation_done_func_t done; |
| 448 | }; |
| 449 | |
| 450 | static void |
| 451 | move_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 Peyrot | eff793a | 2021-07-31 17:25:41 +0200 | [diff] [blame] | 461 | (animation->stop - animation->start) * |
| 462 | progress; |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 463 | weston_matrix_init(&animation->transform.matrix); |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 464 | if (move->scale) |
| 465 | weston_matrix_scale(&animation->transform.matrix, scale, scale, |
| 466 | 1.0f); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 467 | weston_matrix_translate(&animation->transform.matrix, |
Emmanuel Gil Peyrot | eff793a | 2021-07-31 17:25:41 +0200 | [diff] [blame] | 468 | move->dx * progress, move->dy * progress, |
| 469 | 0); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static void |
| 473 | move_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 Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 483 | static struct weston_view_animation * |
| 484 | weston_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 Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 488 | { |
| 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 Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 498 | move->scale = scale; |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 499 | move->done = done; |
| 500 | |
Ander Conselvan de Oliveira | f5cc2b5 | 2014-04-14 15:48:05 +0300 | [diff] [blame] | 501 | animation = weston_view_animation_create(view, start, end, move_frame, |
| 502 | NULL, move_done, data, move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 503 | |
Dongjin Kim | ba89f00 | 2024-09-30 17:49:36 +0900 | [diff] [blame^] | 504 | if (animation == NULL) { |
Lucas Tanure | ed8dd4e | 2015-10-03 11:18:32 -0300 | [diff] [blame] | 505 | free(move); |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 506 | return NULL; |
Lucas Tanure | ed8dd4e | 2015-10-03 11:18:32 -0300 | [diff] [blame] | 507 | } |
U. Artie Eoff | a62e0e0 | 2014-01-17 15:08:51 -0800 | [diff] [blame] | 508 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 509 | weston_spring_init(&animation->spring, 400.0, 0.0, 1.0); |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 510 | animation->spring.friction = 1150; |
| 511 | |
Jonny Lamb | 91f80f2 | 2014-05-22 22:41:30 +0200 | [diff] [blame] | 512 | weston_view_animation_run(animation); |
| 513 | |
Daniel Stone | a67e6b9 | 2013-11-19 11:37:13 +0100 | [diff] [blame] | 514 | return animation; |
| 515 | } |
Quentin Glidic | 24d306c | 2016-08-10 15:53:33 +0200 | [diff] [blame] | 516 | |
| 517 | WL_EXPORT struct weston_view_animation * |
| 518 | weston_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 | |
| 526 | WL_EXPORT struct weston_view_animation * |
| 527 | weston_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 | } |