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