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 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | #include <fcntl.h> |
| 30 | |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 31 | #include "compositor.h" |
| 32 | |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 33 | WL_EXPORT void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 34 | weston_spring_init(struct weston_spring *spring, |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 35 | double k, double current, double target) |
| 36 | { |
| 37 | spring->k = k; |
| 38 | spring->friction = 400.0; |
| 39 | spring->current = current; |
| 40 | spring->previous = current; |
| 41 | spring->target = target; |
| 42 | } |
| 43 | |
| 44 | WL_EXPORT void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 45 | weston_spring_update(struct weston_spring *spring, uint32_t msec) |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 46 | { |
| 47 | double force, v, current, step; |
| 48 | |
Rob Bradford | 84cf541 | 2012-08-09 15:35:49 +0100 | [diff] [blame] | 49 | /* Limit the number of executions of the loop below by ensuring that |
| 50 | * the timestamp for last update of the spring is no more than 1s ago. |
| 51 | * This handles the case where time moves backwards or forwards in |
| 52 | * large jumps. |
| 53 | */ |
| 54 | if (msec - spring->timestamp > 1000) { |
| 55 | weston_log("unexpectedly large timestamp jump (from %u to %u)\n", |
| 56 | spring->timestamp, msec); |
| 57 | spring->timestamp = msec - 1000; |
Rob Bradford | c4f3338 | 2012-08-03 17:02:04 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 60 | step = 0.01; |
| 61 | while (4 < msec - spring->timestamp) { |
| 62 | current = spring->current; |
| 63 | v = current - spring->previous; |
| 64 | force = spring->k * (spring->target - current) / 10.0 + |
| 65 | (spring->previous - current) - v * spring->friction; |
| 66 | |
| 67 | spring->current = |
| 68 | current + (current - spring->previous) + |
| 69 | force * step * step; |
| 70 | spring->previous = current; |
| 71 | |
| 72 | #if 0 |
| 73 | if (spring->current >= 1.0) { |
| 74 | #ifdef TWEENER_BOUNCE |
| 75 | spring->current = 2.0 - spring->current; |
| 76 | spring->previous = 2.0 - spring->previous; |
| 77 | #else |
| 78 | spring->current = 1.0; |
| 79 | spring->previous = 1.0; |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | if (spring->current <= 0.0) { |
| 84 | spring->current = 0.0; |
| 85 | spring->previous = 0.0; |
| 86 | } |
| 87 | #endif |
| 88 | spring->timestamp += 4; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | WL_EXPORT int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 93 | weston_spring_done(struct weston_spring *spring) |
Kristian Høgsberg | 4bfb82a | 2011-12-04 15:47:16 -0500 | [diff] [blame] | 94 | { |
| 95 | return fabs(spring->previous - spring->target) < 0.0002 && |
| 96 | fabs(spring->current - spring->target) < 0.0002; |
| 97 | } |
| 98 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 99 | typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation); |
| 100 | |
| 101 | struct weston_surface_animation { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 102 | struct weston_surface *surface; |
| 103 | struct weston_animation animation; |
| 104 | struct weston_spring spring; |
| 105 | struct weston_transform transform; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 106 | struct wl_listener listener; |
John Kåre Alsaker | 490d02a | 2012-09-30 02:57:21 +0200 | [diff] [blame^] | 107 | float start, stop; |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 108 | weston_surface_animation_frame_func_t frame; |
| 109 | weston_surface_animation_done_func_t done; |
Juan Zhao | 2abd07b | 2012-04-25 19:09:51 +0800 | [diff] [blame] | 110 | void *data; |
| 111 | }; |
| 112 | |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 113 | static void |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 114 | weston_surface_animation_destroy(struct weston_surface_animation *animation) |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 115 | { |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 116 | wl_list_remove(&animation->animation.link); |
| 117 | wl_list_remove(&animation->listener.link); |
| 118 | wl_list_remove(&animation->transform.link); |
| 119 | animation->surface->geometry.dirty = 1; |
| 120 | if (animation->done) |
| 121 | animation->done(animation, animation->data); |
| 122 | free(animation); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static void |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 126 | handle_animation_surface_destroy(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 127 | { |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 128 | struct weston_surface_animation *animation = |
| 129 | container_of(listener, |
| 130 | struct weston_surface_animation, listener); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 131 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 132 | weston_surface_animation_destroy(animation); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 136 | weston_surface_animation_frame(struct weston_animation *base, |
| 137 | struct weston_output *output, uint32_t msecs) |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 138 | { |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 139 | struct weston_surface_animation *animation = |
| 140 | container_of(base, |
| 141 | struct weston_surface_animation, animation); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 142 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 143 | if (base->frame_counter <= 1) |
| 144 | animation->spring.timestamp = msecs; |
Scott Moreau | e2949db | 2012-06-11 13:09:23 -0600 | [diff] [blame] | 145 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 146 | weston_spring_update(&animation->spring, msecs); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 147 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 148 | if (weston_spring_done(&animation->spring)) { |
| 149 | weston_surface_animation_destroy(animation); |
Pekka Paalanen | 2da6d5f | 2012-01-03 13:27:41 +0200 | [diff] [blame] | 150 | return; |
| 151 | } |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 152 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 153 | if (animation->frame) |
| 154 | animation->frame(animation); |
| 155 | |
| 156 | animation->surface->geometry.dirty = 1; |
| 157 | weston_compositor_schedule_repaint(animation->surface->compositor); |
| 158 | } |
| 159 | |
| 160 | static struct weston_surface_animation * |
| 161 | weston_surface_animation_run(struct weston_surface *surface, |
John Kåre Alsaker | 490d02a | 2012-09-30 02:57:21 +0200 | [diff] [blame^] | 162 | float start, float stop, |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 163 | weston_surface_animation_frame_func_t frame, |
| 164 | weston_surface_animation_done_func_t done, |
| 165 | void *data) |
| 166 | { |
| 167 | struct weston_surface_animation *animation; |
| 168 | |
| 169 | animation = malloc(sizeof *animation); |
| 170 | if (!animation) |
| 171 | return NULL; |
| 172 | |
| 173 | animation->surface = surface; |
| 174 | animation->frame = frame; |
| 175 | animation->done = done; |
| 176 | animation->data = data; |
| 177 | animation->start = start; |
| 178 | animation->stop = stop; |
| 179 | weston_matrix_init(&animation->transform.matrix); |
| 180 | wl_list_insert(&surface->geometry.transformation_list, |
| 181 | &animation->transform.link); |
| 182 | weston_spring_init(&animation->spring, 200.0, 0.0, 1.0); |
| 183 | animation->spring.friction = 700; |
| 184 | animation->animation.frame_counter = 0; |
| 185 | animation->animation.frame = weston_surface_animation_frame; |
| 186 | weston_surface_animation_frame(&animation->animation, NULL, 0); |
| 187 | |
| 188 | animation->listener.notify = handle_animation_surface_destroy; |
| 189 | wl_signal_add(&surface->surface.resource.destroy_signal, |
| 190 | &animation->listener); |
| 191 | |
| 192 | wl_list_insert(&surface->output->animation_list, |
| 193 | &animation->animation.link); |
| 194 | |
| 195 | return animation; |
| 196 | } |
| 197 | |
| 198 | static void |
| 199 | zoom_frame(struct weston_surface_animation *animation) |
| 200 | { |
| 201 | struct weston_surface *es = animation->surface; |
John Kåre Alsaker | 490d02a | 2012-09-30 02:57:21 +0200 | [diff] [blame^] | 202 | float scale; |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 203 | |
| 204 | scale = animation->start + |
| 205 | (animation->stop - animation->start) * |
| 206 | animation->spring.current; |
| 207 | weston_matrix_init(&animation->transform.matrix); |
| 208 | weston_matrix_translate(&animation->transform.matrix, |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 209 | -0.5f * es->geometry.width, |
| 210 | -0.5f * es->geometry.height, 0); |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 211 | weston_matrix_scale(&animation->transform.matrix, scale, scale, scale); |
| 212 | weston_matrix_translate(&animation->transform.matrix, |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 213 | 0.5f * es->geometry.width, |
| 214 | 0.5f * es->geometry.height, 0); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 215 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 216 | es->alpha = animation->spring.current; |
Kristian Høgsberg | a416fa1 | 2012-05-21 14:06:52 -0400 | [diff] [blame] | 217 | if (es->alpha > 1.0) |
| 218 | es->alpha = 1.0; |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 221 | WL_EXPORT struct weston_surface_animation * |
John Kåre Alsaker | 490d02a | 2012-09-30 02:57:21 +0200 | [diff] [blame^] | 222 | weston_zoom_run(struct weston_surface *surface, float start, float stop, |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 223 | weston_surface_animation_done_func_t done, void *data) |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 224 | { |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 225 | return weston_surface_animation_run(surface, start, stop, |
| 226 | zoom_frame, done, data); |
| 227 | } |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 228 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 229 | static void |
| 230 | fade_frame(struct weston_surface_animation *animation) |
| 231 | { |
| 232 | if (animation->spring.current > 1) |
| 233 | animation->surface->alpha = 1; |
| 234 | else if (animation->spring.current < 0 ) |
| 235 | animation->surface->alpha = 0; |
| 236 | else |
| 237 | animation->surface->alpha = animation->spring.current; |
| 238 | } |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 239 | |
Kristian Høgsberg | 414bd42 | 2012-06-21 22:07:30 -0400 | [diff] [blame] | 240 | WL_EXPORT struct weston_surface_animation * |
| 241 | weston_fade_run(struct weston_surface *surface, |
| 242 | weston_surface_animation_done_func_t done, void *data) |
| 243 | { |
| 244 | return weston_surface_animation_run(surface, 0, 0, |
| 245 | fade_frame, done, data); |
Kristian Høgsberg | 698c058 | 2011-12-04 15:20:19 -0500 | [diff] [blame] | 246 | } |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 247 | |
Kristian Høgsberg | 1ce6a2a | 2012-06-21 22:34:39 -0400 | [diff] [blame] | 248 | static void |
| 249 | slide_frame(struct weston_surface_animation *animation) |
| 250 | { |
John Kåre Alsaker | 490d02a | 2012-09-30 02:57:21 +0200 | [diff] [blame^] | 251 | float scale; |
Kristian Høgsberg | 1ce6a2a | 2012-06-21 22:34:39 -0400 | [diff] [blame] | 252 | |
| 253 | scale = animation->start + |
| 254 | (animation->stop - animation->start) * |
| 255 | animation->spring.current; |
| 256 | weston_matrix_init(&animation->transform.matrix); |
| 257 | weston_matrix_translate(&animation->transform.matrix, 0, scale, 0); |
| 258 | } |
| 259 | |
| 260 | WL_EXPORT struct weston_surface_animation * |
John Kåre Alsaker | 490d02a | 2012-09-30 02:57:21 +0200 | [diff] [blame^] | 261 | weston_slide_run(struct weston_surface *surface, float start, float stop, |
Kristian Høgsberg | 1ce6a2a | 2012-06-21 22:34:39 -0400 | [diff] [blame] | 262 | weston_surface_animation_done_func_t done, void *data) |
| 263 | { |
| 264 | struct weston_surface_animation *animation; |
| 265 | |
| 266 | animation = weston_surface_animation_run(surface, start, stop, |
| 267 | slide_frame, done, data); |
| 268 | animation->spring.friction = 900; |
| 269 | animation->spring.k = 300; |
| 270 | |
| 271 | return animation; |
| 272 | } |
| 273 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 274 | struct weston_binding { |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 275 | uint32_t key; |
| 276 | uint32_t button; |
Scott Moreau | 6a3633d | 2012-03-20 08:47:59 -0600 | [diff] [blame] | 277 | uint32_t axis; |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 278 | uint32_t modifier; |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 279 | void *handler; |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 280 | void *data; |
| 281 | struct wl_list link; |
| 282 | }; |
| 283 | |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 284 | static struct weston_binding * |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 285 | weston_compositor_add_binding(struct weston_compositor *compositor, |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 286 | uint32_t key, uint32_t button, uint32_t axis, |
| 287 | uint32_t modifier, void *handler, void *data) |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 288 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 289 | struct weston_binding *binding; |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 290 | |
| 291 | binding = malloc(sizeof *binding); |
| 292 | if (binding == NULL) |
| 293 | return NULL; |
| 294 | |
| 295 | binding->key = key; |
| 296 | binding->button = button; |
Scott Moreau | 6a3633d | 2012-03-20 08:47:59 -0600 | [diff] [blame] | 297 | binding->axis = axis; |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 298 | binding->modifier = modifier; |
| 299 | binding->handler = handler; |
| 300 | binding->data = data; |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 301 | |
| 302 | return binding; |
| 303 | } |
| 304 | |
| 305 | WL_EXPORT struct weston_binding * |
| 306 | weston_compositor_add_key_binding(struct weston_compositor *compositor, |
| 307 | uint32_t key, uint32_t modifier, |
| 308 | weston_key_binding_handler_t handler, |
| 309 | void *data) |
| 310 | { |
| 311 | struct weston_binding *binding; |
| 312 | |
| 313 | binding = weston_compositor_add_binding(compositor, key, 0, 0, |
| 314 | modifier, handler, data); |
| 315 | if (binding == NULL) |
| 316 | return NULL; |
| 317 | |
| 318 | wl_list_insert(compositor->key_binding_list.prev, &binding->link); |
| 319 | |
| 320 | return binding; |
| 321 | } |
| 322 | |
| 323 | WL_EXPORT struct weston_binding * |
| 324 | weston_compositor_add_button_binding(struct weston_compositor *compositor, |
| 325 | uint32_t button, uint32_t modifier, |
| 326 | weston_button_binding_handler_t handler, |
| 327 | void *data) |
| 328 | { |
| 329 | struct weston_binding *binding; |
| 330 | |
| 331 | binding = weston_compositor_add_binding(compositor, 0, button, 0, |
| 332 | modifier, handler, data); |
| 333 | if (binding == NULL) |
| 334 | return NULL; |
| 335 | |
| 336 | wl_list_insert(compositor->button_binding_list.prev, &binding->link); |
| 337 | |
| 338 | return binding; |
| 339 | } |
| 340 | |
| 341 | WL_EXPORT struct weston_binding * |
| 342 | weston_compositor_add_axis_binding(struct weston_compositor *compositor, |
| 343 | uint32_t axis, uint32_t modifier, |
| 344 | weston_axis_binding_handler_t handler, |
| 345 | void *data) |
| 346 | { |
| 347 | struct weston_binding *binding; |
| 348 | |
| 349 | binding = weston_compositor_add_binding(compositor, 0, 0, axis, |
| 350 | modifier, handler, data); |
| 351 | if (binding == NULL) |
| 352 | return NULL; |
| 353 | |
| 354 | wl_list_insert(compositor->axis_binding_list.prev, &binding->link); |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 355 | |
| 356 | return binding; |
| 357 | } |
| 358 | |
| 359 | WL_EXPORT void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 360 | weston_binding_destroy(struct weston_binding *binding) |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 361 | { |
| 362 | wl_list_remove(&binding->link); |
| 363 | free(binding); |
| 364 | } |
| 365 | |
| 366 | WL_EXPORT void |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 367 | weston_binding_list_destroy_all(struct wl_list *list) |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 368 | { |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 369 | struct weston_binding *binding, *tmp; |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 370 | |
| 371 | wl_list_for_each_safe(binding, tmp, list, link) |
Kristian Høgsberg | 3466bc8 | 2012-01-03 11:29:15 -0500 | [diff] [blame] | 372 | weston_binding_destroy(binding); |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 375 | struct binding_keyboard_grab { |
| 376 | uint32_t key; |
| 377 | struct wl_keyboard_grab grab; |
| 378 | }; |
| 379 | |
| 380 | static void |
| 381 | binding_key(struct wl_keyboard_grab *grab, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 382 | uint32_t time, uint32_t key, uint32_t state_w) |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 383 | { |
| 384 | struct binding_keyboard_grab *b = |
| 385 | container_of(grab, struct binding_keyboard_grab, grab); |
| 386 | struct wl_resource *resource; |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 387 | struct wl_display *display; |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 388 | enum wl_keyboard_key_state state = state_w; |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 389 | uint32_t serial; |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 390 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 391 | resource = grab->keyboard->focus_resource; |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 392 | if (key == b->key) { |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 393 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 394 | wl_keyboard_end_grab(grab->keyboard); |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 395 | free(b); |
| 396 | } |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 397 | } else if (resource) { |
| 398 | display = wl_client_get_display(resource->client); |
| 399 | serial = wl_display_next_serial(display); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 400 | wl_keyboard_send_key(resource, serial, time, key, state); |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 401 | } |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 404 | static void |
| 405 | binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial, |
| 406 | uint32_t mods_depressed, uint32_t mods_latched, |
| 407 | uint32_t mods_locked, uint32_t group) |
| 408 | { |
| 409 | struct wl_resource *resource; |
| 410 | |
| 411 | resource = grab->keyboard->focus_resource; |
| 412 | if (!resource) |
| 413 | return; |
| 414 | |
| 415 | wl_keyboard_send_modifiers(resource, serial, mods_depressed, |
| 416 | mods_latched, mods_locked, group); |
| 417 | } |
| 418 | |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 419 | static const struct wl_keyboard_grab_interface binding_grab = { |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 420 | binding_key, |
| 421 | binding_modifiers, |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 425 | install_binding_grab(struct wl_seat *seat, |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 426 | uint32_t time, uint32_t key) |
| 427 | { |
| 428 | struct binding_keyboard_grab *grab; |
| 429 | |
| 430 | grab = malloc(sizeof *grab); |
| 431 | grab->key = key; |
| 432 | grab->grab.interface = &binding_grab; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 433 | wl_keyboard_start_grab(seat->keyboard, &grab->grab); |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 434 | } |
| 435 | |
Pekka Paalanen | 4738f3b | 2012-01-02 15:47:07 +0200 | [diff] [blame] | 436 | WL_EXPORT void |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 437 | weston_compositor_run_key_binding(struct weston_compositor *compositor, |
| 438 | struct weston_seat *seat, |
| 439 | uint32_t time, uint32_t key, |
| 440 | enum wl_keyboard_key_state state) |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 441 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 442 | struct weston_binding *b; |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 443 | |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 444 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
| 445 | return; |
| 446 | |
| 447 | wl_list_for_each(b, &compositor->key_binding_list, link) { |
| 448 | if (b->key == key && b->modifier == seat->modifier_state) { |
| 449 | weston_key_binding_handler_t handler = b->handler; |
| 450 | handler(&seat->seat, time, key, b->data); |
Kristian Høgsberg | abcef3c | 2012-03-05 17:47:15 -0500 | [diff] [blame] | 451 | |
| 452 | /* If this was a key binding and it didn't |
| 453 | * install a keyboard grab, install one now to |
| 454 | * swallow the key release. */ |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 455 | if (seat->seat.keyboard->grab == |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 456 | &seat->seat.keyboard->default_grab) |
| 457 | install_binding_grab(&seat->seat, time, key); |
Kristian Høgsberg | f47d8fe | 2011-12-19 15:16:06 -0500 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 461 | |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 462 | WL_EXPORT void |
| 463 | weston_compositor_run_button_binding(struct weston_compositor *compositor, |
| 464 | struct weston_seat *seat, |
| 465 | uint32_t time, uint32_t button, |
| 466 | enum wl_pointer_button_state state) |
| 467 | { |
| 468 | struct weston_binding *b; |
| 469 | |
| 470 | if (state == WL_POINTER_BUTTON_STATE_RELEASED) |
| 471 | return; |
| 472 | |
| 473 | wl_list_for_each(b, &compositor->button_binding_list, link) { |
| 474 | if (b->button == button && b->modifier == seat->modifier_state) { |
| 475 | weston_button_binding_handler_t handler = b->handler; |
| 476 | handler(&seat->seat, time, button, b->data); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | WL_EXPORT void |
| 482 | weston_compositor_run_axis_binding(struct weston_compositor *compositor, |
| 483 | struct weston_seat *seat, |
| 484 | uint32_t time, uint32_t axis, |
Daniel Stone | 0c1e46e | 2012-05-30 16:31:59 +0100 | [diff] [blame] | 485 | wl_fixed_t value) |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 486 | { |
| 487 | struct weston_binding *b; |
| 488 | |
| 489 | wl_list_for_each(b, &compositor->axis_binding_list, link) { |
| 490 | if (b->axis == axis && b->modifier == seat->modifier_state) { |
| 491 | weston_axis_binding_handler_t handler = b->handler; |
| 492 | handler(&seat->seat, time, axis, value, b->data); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 497 | WL_EXPORT int |
| 498 | weston_environment_get_fd(const char *env) |
| 499 | { |
| 500 | char *e, *end; |
| 501 | int fd, flags; |
| 502 | |
| 503 | e = getenv(env); |
| 504 | if (!e) |
| 505 | return -1; |
| 506 | fd = strtol(e, &end, 0); |
| 507 | if (*end != '\0') |
| 508 | return -1; |
| 509 | |
| 510 | flags = fcntl(fd, F_GETFD); |
| 511 | if (flags == -1) |
| 512 | return -1; |
| 513 | |
| 514 | fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 515 | unsetenv(env); |
| 516 | |
| 517 | return fd; |
| 518 | } |