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 |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 31 | weston_spring_init(struct weston_spring *spring, |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 32 | double k, double current, double target) |
| 33 | { |
| 34 | spring->k = k; |
| 35 | spring->friction = 400.0; |
| 36 | spring->current = current; |
| 37 | spring->previous = current; |
| 38 | spring->target = target; |
| 39 | } |
| 40 | |
| 41 | WL_EXPORT void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 42 | weston_spring_update(struct weston_spring *spring, uint32_t msec) |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 43 | { |
| 44 | double force, v, current, step; |
| 45 | |
| 46 | step = 0.01; |
| 47 | while (4 < msec - spring->timestamp) { |
| 48 | current = spring->current; |
| 49 | v = current - spring->previous; |
| 50 | force = spring->k * (spring->target - current) / 10.0 + |
| 51 | (spring->previous - current) - v * spring->friction; |
| 52 | |
| 53 | spring->current = |
| 54 | current + (current - spring->previous) + |
| 55 | force * step * step; |
| 56 | spring->previous = current; |
| 57 | |
| 58 | #if 0 |
| 59 | if (spring->current >= 1.0) { |
| 60 | #ifdef TWEENER_BOUNCE |
| 61 | spring->current = 2.0 - spring->current; |
| 62 | spring->previous = 2.0 - spring->previous; |
| 63 | #else |
| 64 | spring->current = 1.0; |
| 65 | spring->previous = 1.0; |
| 66 | #endif |
| 67 | } |
| 68 | |
| 69 | if (spring->current <= 0.0) { |
| 70 | spring->current = 0.0; |
| 71 | spring->previous = 0.0; |
| 72 | } |
| 73 | #endif |
| 74 | spring->timestamp += 4; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | WL_EXPORT int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 79 | weston_spring_done(struct weston_spring *spring) |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 80 | { |
| 81 | return fabs(spring->previous - spring->target) < 0.0002 && |
| 82 | fabs(spring->current - spring->target) < 0.0002; |
| 83 | } |
| 84 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 85 | struct weston_zoom { |
| 86 | struct weston_surface *surface; |
| 87 | struct weston_animation animation; |
| 88 | struct weston_spring spring; |
| 89 | struct weston_transform transform; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 90 | struct wl_listener listener; |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 91 | GLfloat start, stop; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 92 | void (*done)(struct weston_zoom *zoom, void *data); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 93 | void *data; |
| 94 | }; |
| 95 | |
| 96 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 97 | weston_zoom_destroy(struct weston_zoom *zoom) |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 98 | { |
| 99 | wl_list_remove(&zoom->animation.link); |
| 100 | wl_list_remove(&zoom->listener.link); |
Pekka Paalanen | c61eca6 | 2012-01-06 14:10:06 +0200 | [diff] [blame] | 101 | wl_list_remove(&zoom->transform.link); |
Pekka Paalanen | bc0b7e7 | 2012-01-24 09:53:37 +0200 | [diff] [blame] | 102 | zoom->surface->geometry.dirty = 1; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 103 | if (zoom->done) |
| 104 | zoom->done(zoom, zoom->data); |
| 105 | free(zoom); |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | handle_zoom_surface_destroy(struct wl_listener *listener, |
| 110 | struct wl_resource *resource, uint32_t time) |
| 111 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 112 | struct weston_zoom *zoom = |
| 113 | container_of(listener, struct weston_zoom, listener); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 114 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 115 | weston_zoom_destroy(zoom); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 119 | weston_zoom_frame(struct weston_animation *animation, |
| 120 | struct weston_output *output, uint32_t msecs) |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 121 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 122 | struct weston_zoom *zoom = |
| 123 | container_of(animation, struct weston_zoom, animation); |
| 124 | struct weston_surface *es = zoom->surface; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 125 | GLfloat scale; |
| 126 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 127 | weston_spring_update(&zoom->spring, msecs); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 128 | |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 129 | if (weston_spring_done(&zoom->spring)) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 130 | weston_zoom_destroy(zoom); |
Pekka Paalanen | 2da6d5f | 2012-01-03 13:27:41 +0200 | [diff] [blame] | 131 | return; |
| 132 | } |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 133 | |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 134 | scale = zoom->start + |
| 135 | (zoom->stop - zoom->start) * zoom->spring.current; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 136 | weston_matrix_init(&zoom->transform.matrix); |
| 137 | weston_matrix_translate(&zoom->transform.matrix, |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 138 | -0.5f * es->geometry.width, |
| 139 | -0.5f * es->geometry.height, 0); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 140 | weston_matrix_scale(&zoom->transform.matrix, scale, scale, scale); |
| 141 | weston_matrix_translate(&zoom->transform.matrix, |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 142 | 0.5f * es->geometry.width, |
| 143 | 0.5f * es->geometry.height, 0); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 144 | |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 145 | es->alpha = zoom->spring.current * 255; |
| 146 | if (es->alpha > 255) |
| 147 | es->alpha = 255; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 148 | |
Pekka Paalanen | bc0b7e7 | 2012-01-24 09:53:37 +0200 | [diff] [blame] | 149 | zoom->surface->geometry.dirty = 1; |
Kristian Høgsberg | 2ea0944 | 2012-02-28 23:12:52 -0500 | [diff] [blame] | 150 | weston_compositor_schedule_repaint(es->compositor); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 153 | WL_EXPORT struct weston_zoom * |
| 154 | weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop, |
| 155 | weston_zoom_done_func_t done, void *data) |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 156 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 157 | struct weston_zoom *zoom; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 158 | |
| 159 | zoom = malloc(sizeof *zoom); |
| 160 | if (!zoom) |
| 161 | return NULL; |
| 162 | |
| 163 | zoom->surface = surface; |
| 164 | zoom->done = done; |
| 165 | zoom->data = data; |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 166 | zoom->start = start; |
| 167 | zoom->stop = stop; |
Pekka Paalanen | bc0b7e7 | 2012-01-24 09:53:37 +0200 | [diff] [blame] | 168 | wl_list_insert(&surface->geometry.transformation_list, |
| 169 | &zoom->transform.link); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 170 | weston_spring_init(&zoom->spring, 200.0, 0.0, 1.0); |
Kristian Høgsberg | ef45824 | 2011-12-15 11:24:25 -0500 | [diff] [blame] | 171 | zoom->spring.friction = 700; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 172 | zoom->spring.timestamp = weston_compositor_get_time(); |
| 173 | zoom->animation.frame = weston_zoom_frame; |
| 174 | weston_zoom_frame(&zoom->animation, NULL, zoom->spring.timestamp); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 175 | |
| 176 | zoom->listener.func = handle_zoom_surface_destroy; |
| 177 | wl_list_insert(surface->surface.resource.destroy_listener_list.prev, |
| 178 | &zoom->listener.link); |
| 179 | |
Pekka Paalanen | 7377204 | 2012-01-25 14:45:18 +0200 | [diff] [blame] | 180 | wl_list_insert(&surface->compositor->animation_list, |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 181 | &zoom->animation.link); |
| 182 | |
| 183 | return zoom; |
| 184 | } |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 185 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 186 | struct weston_binding { |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 187 | uint32_t key; |
| 188 | uint32_t button; |
| 189 | uint32_t modifier; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 190 | weston_binding_handler_t handler; |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 191 | void *data; |
| 192 | struct wl_list link; |
| 193 | }; |
| 194 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 195 | WL_EXPORT struct weston_binding * |
| 196 | weston_compositor_add_binding(struct weston_compositor *compositor, |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 197 | uint32_t key, uint32_t button, uint32_t modifier, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 198 | weston_binding_handler_t handler, void *data) |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 199 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 200 | struct weston_binding *binding; |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 201 | |
| 202 | binding = malloc(sizeof *binding); |
| 203 | if (binding == NULL) |
| 204 | return NULL; |
| 205 | |
| 206 | binding->key = key; |
| 207 | binding->button = button; |
| 208 | binding->modifier = modifier; |
| 209 | binding->handler = handler; |
| 210 | binding->data = data; |
| 211 | wl_list_insert(compositor->binding_list.prev, &binding->link); |
| 212 | |
| 213 | return binding; |
| 214 | } |
| 215 | |
| 216 | WL_EXPORT void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 217 | weston_binding_destroy(struct weston_binding *binding) |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 218 | { |
| 219 | wl_list_remove(&binding->link); |
| 220 | free(binding); |
| 221 | } |
| 222 | |
| 223 | WL_EXPORT void |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 224 | weston_binding_list_destroy_all(struct wl_list *list) |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 225 | { |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 226 | struct weston_binding *binding, *tmp; |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 227 | |
| 228 | wl_list_for_each_safe(binding, tmp, list, link) |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 229 | weston_binding_destroy(binding); |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame^] | 232 | struct binding_keyboard_grab { |
| 233 | uint32_t key; |
| 234 | struct wl_keyboard_grab grab; |
| 235 | }; |
| 236 | |
| 237 | static void |
| 238 | binding_key(struct wl_keyboard_grab *grab, |
| 239 | uint32_t time, uint32_t key, int32_t state) |
| 240 | { |
| 241 | struct binding_keyboard_grab *b = |
| 242 | container_of(grab, struct binding_keyboard_grab, grab); |
| 243 | struct wl_resource *resource; |
| 244 | |
| 245 | resource = grab->input_device->keyboard_focus_resource; |
| 246 | if (key == b->key) { |
| 247 | if (!state) { |
| 248 | wl_input_device_end_keyboard_grab(grab->input_device, |
| 249 | time); |
| 250 | free(b); |
| 251 | } |
| 252 | } else if (resource) |
| 253 | wl_input_device_send_key(resource, time, key, state); |
| 254 | } |
| 255 | |
| 256 | static const struct wl_keyboard_grab_interface binding_grab = { |
| 257 | binding_key |
| 258 | }; |
| 259 | |
| 260 | static void |
| 261 | install_binding_grab(struct wl_input_device *device, |
| 262 | uint32_t time, uint32_t key) |
| 263 | { |
| 264 | struct binding_keyboard_grab *grab; |
| 265 | |
| 266 | grab = malloc(sizeof *grab); |
| 267 | grab->key = key; |
| 268 | grab->grab.interface = &binding_grab; |
| 269 | wl_input_device_start_keyboard_grab(device, &grab->grab, time); |
| 270 | } |
| 271 | |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 272 | WL_EXPORT void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 273 | weston_compositor_run_binding(struct weston_compositor *compositor, |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 274 | struct weston_input_device *device, |
| 275 | uint32_t time, |
| 276 | uint32_t key, uint32_t button, int32_t state) |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 277 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 278 | struct weston_binding *b; |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 279 | |
| 280 | wl_list_for_each(b, &compositor->binding_list, link) { |
| 281 | if (b->key == key && b->button == button && |
| 282 | b->modifier == device->modifier_state && state) { |
| 283 | b->handler(&device->input_device, |
| 284 | time, key, button, state, b->data); |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame^] | 285 | |
| 286 | /* If this was a key binding and it didn't |
| 287 | * install a keyboard grab, install one now to |
| 288 | * swallow the key release. */ |
| 289 | if (b->key && |
| 290 | device->input_device.keyboard_grab == |
| 291 | &device->input_device.default_keyboard_grab) |
| 292 | install_binding_grab(&device->input_device, |
| 293 | time, key); |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |