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