Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 24 | #include <string.h> |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 25 | #include <stdio.h> |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 26 | #include <math.h> |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 27 | |
| 28 | #include "compositor.h" |
| 29 | |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 30 | WL_EXPORT void |
| 31 | wlsc_matrix_init(struct wlsc_matrix *matrix) |
| 32 | { |
| 33 | static const struct wlsc_matrix identity = { |
| 34 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } |
| 35 | }; |
| 36 | |
| 37 | memcpy(matrix, &identity, sizeof identity); |
| 38 | } |
| 39 | |
| 40 | static void |
| 41 | wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n) |
| 42 | { |
| 43 | struct wlsc_matrix tmp; |
| 44 | const GLfloat *row, *column; |
| 45 | div_t d; |
| 46 | int i, j; |
| 47 | |
| 48 | for (i = 0; i < 16; i++) { |
| 49 | tmp.d[i] = 0; |
| 50 | d = div(i, 4); |
| 51 | row = m->d + d.quot * 4; |
| 52 | column = n->d + d.rem; |
| 53 | for (j = 0; j < 4; j++) |
| 54 | tmp.d[i] += row[j] * column[j * 4]; |
| 55 | } |
| 56 | memcpy(m, &tmp, sizeof tmp); |
| 57 | } |
| 58 | |
| 59 | WL_EXPORT void |
| 60 | wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z) |
| 61 | { |
| 62 | struct wlsc_matrix translate = { |
| 63 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 } |
| 64 | }; |
| 65 | |
| 66 | wlsc_matrix_multiply(matrix, &translate); |
| 67 | } |
| 68 | |
| 69 | WL_EXPORT void |
| 70 | wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z) |
| 71 | { |
| 72 | struct wlsc_matrix scale = { |
| 73 | { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 } |
| 74 | }; |
| 75 | |
| 76 | wlsc_matrix_multiply(matrix, &scale); |
| 77 | } |
| 78 | |
| 79 | WL_EXPORT void |
| 80 | wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v) |
| 81 | { |
| 82 | int i, j; |
| 83 | struct wlsc_vector t; |
| 84 | |
| 85 | for (i = 0; i < 4; i++) { |
| 86 | t.f[i] = 0; |
| 87 | for (j = 0; j < 4; j++) |
| 88 | t.f[i] += v->f[j] * matrix->d[i + j * 4]; |
| 89 | } |
| 90 | |
| 91 | *v = t; |
| 92 | } |
| 93 | |
| 94 | WL_EXPORT void |
| 95 | wlsc_spring_init(struct wlsc_spring *spring, |
| 96 | double k, double current, double target) |
| 97 | { |
| 98 | spring->k = k; |
| 99 | spring->friction = 400.0; |
| 100 | spring->current = current; |
| 101 | spring->previous = current; |
| 102 | spring->target = target; |
| 103 | } |
| 104 | |
| 105 | WL_EXPORT void |
| 106 | wlsc_spring_update(struct wlsc_spring *spring, uint32_t msec) |
| 107 | { |
| 108 | double force, v, current, step; |
| 109 | |
| 110 | step = 0.01; |
| 111 | while (4 < msec - spring->timestamp) { |
| 112 | current = spring->current; |
| 113 | v = current - spring->previous; |
| 114 | force = spring->k * (spring->target - current) / 10.0 + |
| 115 | (spring->previous - current) - v * spring->friction; |
| 116 | |
| 117 | spring->current = |
| 118 | current + (current - spring->previous) + |
| 119 | force * step * step; |
| 120 | spring->previous = current; |
| 121 | |
| 122 | #if 0 |
| 123 | if (spring->current >= 1.0) { |
| 124 | #ifdef TWEENER_BOUNCE |
| 125 | spring->current = 2.0 - spring->current; |
| 126 | spring->previous = 2.0 - spring->previous; |
| 127 | #else |
| 128 | spring->current = 1.0; |
| 129 | spring->previous = 1.0; |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | if (spring->current <= 0.0) { |
| 134 | spring->current = 0.0; |
| 135 | spring->previous = 0.0; |
| 136 | } |
| 137 | #endif |
| 138 | spring->timestamp += 4; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | WL_EXPORT int |
| 143 | wlsc_spring_done(struct wlsc_spring *spring) |
| 144 | { |
| 145 | return fabs(spring->previous - spring->target) < 0.0002 && |
| 146 | fabs(spring->current - spring->target) < 0.0002; |
| 147 | } |
| 148 | |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 149 | struct wlsc_zoom { |
| 150 | struct wlsc_surface *surface; |
| 151 | struct wlsc_animation animation; |
| 152 | struct wlsc_spring spring; |
| 153 | struct wlsc_transform transform; |
| 154 | struct wl_listener listener; |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 155 | GLfloat start, stop; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 156 | void (*done)(struct wlsc_zoom *zoom, void *data); |
| 157 | void *data; |
| 158 | }; |
| 159 | |
| 160 | static void |
| 161 | wlsc_zoom_destroy(struct wlsc_zoom *zoom) |
| 162 | { |
| 163 | wl_list_remove(&zoom->animation.link); |
| 164 | wl_list_remove(&zoom->listener.link); |
| 165 | zoom->surface->transform = NULL; |
| 166 | if (zoom->done) |
| 167 | zoom->done(zoom, zoom->data); |
| 168 | free(zoom); |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | handle_zoom_surface_destroy(struct wl_listener *listener, |
| 173 | struct wl_resource *resource, uint32_t time) |
| 174 | { |
| 175 | struct wlsc_zoom *zoom = |
| 176 | container_of(listener, struct wlsc_zoom, listener); |
| 177 | |
| 178 | wlsc_zoom_destroy(zoom); |
| 179 | } |
| 180 | |
| 181 | static void |
| 182 | wlsc_zoom_frame(struct wlsc_animation *animation, |
| 183 | struct wlsc_output *output, uint32_t msecs) |
| 184 | { |
| 185 | struct wlsc_zoom *zoom = |
| 186 | container_of(animation, struct wlsc_zoom, animation); |
| 187 | struct wlsc_surface *es = zoom->surface; |
| 188 | GLfloat scale; |
| 189 | |
| 190 | wlsc_spring_update(&zoom->spring, msecs); |
| 191 | |
Pekka Paalanen | 2da6d5f | 2012-01-03 13:27:41 +0200 | [diff] [blame^] | 192 | if (wlsc_spring_done(&zoom->spring)) { |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 193 | wlsc_zoom_destroy(zoom); |
Pekka Paalanen | 2da6d5f | 2012-01-03 13:27:41 +0200 | [diff] [blame^] | 194 | return; |
| 195 | } |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 196 | |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 197 | scale = zoom->start + |
| 198 | (zoom->stop - zoom->start) * zoom->spring.current; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 199 | wlsc_matrix_init(&zoom->transform.matrix); |
| 200 | wlsc_matrix_translate(&zoom->transform.matrix, |
| 201 | -(es->x + es->width / 2.0), |
| 202 | -(es->y + es->height / 2.0), 0); |
| 203 | wlsc_matrix_scale(&zoom->transform.matrix, scale, scale, scale); |
| 204 | wlsc_matrix_translate(&zoom->transform.matrix, |
| 205 | es->x + es->width / 2.0, |
| 206 | es->y + es->height / 2.0, 0); |
| 207 | |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 208 | es->alpha = zoom->spring.current * 255; |
| 209 | if (es->alpha > 255) |
| 210 | es->alpha = 255; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 211 | scale = 1.0 / zoom->spring.current; |
| 212 | wlsc_matrix_init(&zoom->transform.inverse); |
| 213 | wlsc_matrix_scale(&zoom->transform.inverse, scale, scale, scale); |
| 214 | |
| 215 | wlsc_compositor_damage_all(es->compositor); |
| 216 | } |
| 217 | |
| 218 | WL_EXPORT struct wlsc_zoom * |
| 219 | wlsc_zoom_run(struct wlsc_surface *surface, GLfloat start, GLfloat stop, |
| 220 | wlsc_zoom_done_func_t done, void *data) |
| 221 | { |
| 222 | struct wlsc_zoom *zoom; |
| 223 | |
| 224 | zoom = malloc(sizeof *zoom); |
| 225 | if (!zoom) |
| 226 | return NULL; |
| 227 | |
| 228 | zoom->surface = surface; |
| 229 | zoom->done = done; |
| 230 | zoom->data = data; |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 231 | zoom->start = start; |
| 232 | zoom->stop = stop; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 233 | surface->transform = &zoom->transform; |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 234 | wlsc_spring_init(&zoom->spring, 200.0, 0.0, 1.0); |
| 235 | zoom->spring.friction = 700; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 236 | zoom->spring.timestamp = wlsc_compositor_get_time(); |
| 237 | zoom->animation.frame = wlsc_zoom_frame; |
| 238 | wlsc_zoom_frame(&zoom->animation, NULL, zoom->spring.timestamp); |
| 239 | |
| 240 | zoom->listener.func = handle_zoom_surface_destroy; |
| 241 | wl_list_insert(surface->surface.resource.destroy_listener_list.prev, |
| 242 | &zoom->listener.link); |
| 243 | |
| 244 | wl_list_insert(surface->compositor->animation_list.prev, |
| 245 | &zoom->animation.link); |
| 246 | |
| 247 | return zoom; |
| 248 | } |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 249 | |
| 250 | struct wlsc_binding { |
| 251 | uint32_t key; |
| 252 | uint32_t button; |
| 253 | uint32_t modifier; |
| 254 | wlsc_binding_handler_t handler; |
| 255 | void *data; |
| 256 | struct wl_list link; |
| 257 | }; |
| 258 | |
| 259 | WL_EXPORT struct wlsc_binding * |
| 260 | wlsc_compositor_add_binding(struct wlsc_compositor *compositor, |
| 261 | uint32_t key, uint32_t button, uint32_t modifier, |
| 262 | wlsc_binding_handler_t handler, void *data) |
| 263 | { |
| 264 | struct wlsc_binding *binding; |
| 265 | |
| 266 | binding = malloc(sizeof *binding); |
| 267 | if (binding == NULL) |
| 268 | return NULL; |
| 269 | |
| 270 | binding->key = key; |
| 271 | binding->button = button; |
| 272 | binding->modifier = modifier; |
| 273 | binding->handler = handler; |
| 274 | binding->data = data; |
| 275 | wl_list_insert(compositor->binding_list.prev, &binding->link); |
| 276 | |
| 277 | return binding; |
| 278 | } |
| 279 | |
| 280 | WL_EXPORT void |
| 281 | wlsc_binding_destroy(struct wlsc_binding *binding) |
| 282 | { |
| 283 | wl_list_remove(&binding->link); |
| 284 | free(binding); |
| 285 | } |
| 286 | |
| 287 | WL_EXPORT void |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 288 | wlsc_binding_list_destroy_all(struct wl_list *list) |
| 289 | { |
| 290 | struct wlsc_binding *binding, *tmp; |
| 291 | |
| 292 | wl_list_for_each_safe(binding, tmp, list, link) |
| 293 | wlsc_binding_destroy(binding); |
| 294 | } |
| 295 | |
| 296 | WL_EXPORT void |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 297 | wlsc_compositor_run_binding(struct wlsc_compositor *compositor, |
| 298 | struct wlsc_input_device *device, |
| 299 | uint32_t time, |
| 300 | uint32_t key, uint32_t button, int32_t state) |
| 301 | { |
| 302 | struct wlsc_binding *b; |
| 303 | |
| 304 | wl_list_for_each(b, &compositor->binding_list, link) { |
| 305 | if (b->key == key && b->button == button && |
| 306 | b->modifier == device->modifier_state && state) { |
| 307 | b->handler(&device->input_device, |
| 308 | time, key, button, state, b->data); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |