Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 1 | /* |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 2 | * Copyright © 2011-2012 Intel Corporation |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 3 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 11 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 28 | #include <stdlib.h> |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 29 | #include <linux/input.h> |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 30 | |
| 31 | #include "compositor.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 32 | #include "shared/helpers.h" |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 33 | |
| 34 | struct weston_binding { |
| 35 | uint32_t key; |
| 36 | uint32_t button; |
| 37 | uint32_t axis; |
| 38 | uint32_t modifier; |
| 39 | void *handler; |
| 40 | void *data; |
| 41 | struct wl_list link; |
| 42 | }; |
| 43 | |
| 44 | static struct weston_binding * |
| 45 | weston_compositor_add_binding(struct weston_compositor *compositor, |
| 46 | uint32_t key, uint32_t button, uint32_t axis, |
| 47 | uint32_t modifier, void *handler, void *data) |
| 48 | { |
| 49 | struct weston_binding *binding; |
| 50 | |
| 51 | binding = malloc(sizeof *binding); |
| 52 | if (binding == NULL) |
| 53 | return NULL; |
| 54 | |
| 55 | binding->key = key; |
| 56 | binding->button = button; |
| 57 | binding->axis = axis; |
| 58 | binding->modifier = modifier; |
| 59 | binding->handler = handler; |
| 60 | binding->data = data; |
| 61 | |
| 62 | return binding; |
| 63 | } |
| 64 | |
| 65 | WL_EXPORT struct weston_binding * |
| 66 | weston_compositor_add_key_binding(struct weston_compositor *compositor, |
| 67 | uint32_t key, uint32_t modifier, |
| 68 | weston_key_binding_handler_t handler, |
| 69 | void *data) |
| 70 | { |
| 71 | struct weston_binding *binding; |
| 72 | |
| 73 | binding = weston_compositor_add_binding(compositor, key, 0, 0, |
| 74 | modifier, handler, data); |
| 75 | if (binding == NULL) |
| 76 | return NULL; |
| 77 | |
| 78 | wl_list_insert(compositor->key_binding_list.prev, &binding->link); |
| 79 | |
| 80 | return binding; |
| 81 | } |
| 82 | |
| 83 | WL_EXPORT struct weston_binding * |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 84 | weston_compositor_add_modifier_binding(struct weston_compositor *compositor, |
| 85 | uint32_t modifier, |
| 86 | weston_modifier_binding_handler_t handler, |
| 87 | void *data) |
| 88 | { |
| 89 | struct weston_binding *binding; |
| 90 | |
| 91 | binding = weston_compositor_add_binding(compositor, 0, 0, 0, |
| 92 | modifier, handler, data); |
| 93 | if (binding == NULL) |
| 94 | return NULL; |
| 95 | |
| 96 | wl_list_insert(compositor->modifier_binding_list.prev, &binding->link); |
| 97 | |
| 98 | return binding; |
| 99 | } |
| 100 | |
| 101 | WL_EXPORT struct weston_binding * |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 102 | weston_compositor_add_button_binding(struct weston_compositor *compositor, |
| 103 | uint32_t button, uint32_t modifier, |
| 104 | weston_button_binding_handler_t handler, |
| 105 | void *data) |
| 106 | { |
| 107 | struct weston_binding *binding; |
| 108 | |
| 109 | binding = weston_compositor_add_binding(compositor, 0, button, 0, |
| 110 | modifier, handler, data); |
| 111 | if (binding == NULL) |
| 112 | return NULL; |
| 113 | |
| 114 | wl_list_insert(compositor->button_binding_list.prev, &binding->link); |
| 115 | |
| 116 | return binding; |
| 117 | } |
| 118 | |
| 119 | WL_EXPORT struct weston_binding * |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 120 | weston_compositor_add_touch_binding(struct weston_compositor *compositor, |
| 121 | uint32_t modifier, |
| 122 | weston_touch_binding_handler_t handler, |
| 123 | void *data) |
| 124 | { |
| 125 | struct weston_binding *binding; |
| 126 | |
| 127 | binding = weston_compositor_add_binding(compositor, 0, 0, 0, |
| 128 | modifier, handler, data); |
| 129 | if (binding == NULL) |
| 130 | return NULL; |
| 131 | |
| 132 | wl_list_insert(compositor->touch_binding_list.prev, &binding->link); |
| 133 | |
| 134 | return binding; |
| 135 | } |
| 136 | |
| 137 | WL_EXPORT struct weston_binding * |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 138 | weston_compositor_add_axis_binding(struct weston_compositor *compositor, |
| 139 | uint32_t axis, uint32_t modifier, |
| 140 | weston_axis_binding_handler_t handler, |
| 141 | void *data) |
| 142 | { |
| 143 | struct weston_binding *binding; |
| 144 | |
| 145 | binding = weston_compositor_add_binding(compositor, 0, 0, axis, |
| 146 | modifier, handler, data); |
| 147 | if (binding == NULL) |
| 148 | return NULL; |
| 149 | |
| 150 | wl_list_insert(compositor->axis_binding_list.prev, &binding->link); |
| 151 | |
| 152 | return binding; |
| 153 | } |
| 154 | |
| 155 | WL_EXPORT struct weston_binding * |
| 156 | weston_compositor_add_debug_binding(struct weston_compositor *compositor, |
| 157 | uint32_t key, |
| 158 | weston_key_binding_handler_t handler, |
| 159 | void *data) |
| 160 | { |
| 161 | struct weston_binding *binding; |
| 162 | |
| 163 | binding = weston_compositor_add_binding(compositor, key, 0, 0, 0, |
| 164 | handler, data); |
| 165 | |
| 166 | wl_list_insert(compositor->debug_binding_list.prev, &binding->link); |
| 167 | |
| 168 | return binding; |
| 169 | } |
| 170 | |
| 171 | WL_EXPORT void |
| 172 | weston_binding_destroy(struct weston_binding *binding) |
| 173 | { |
| 174 | wl_list_remove(&binding->link); |
| 175 | free(binding); |
| 176 | } |
| 177 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 178 | void |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 179 | weston_binding_list_destroy_all(struct wl_list *list) |
| 180 | { |
| 181 | struct weston_binding *binding, *tmp; |
| 182 | |
| 183 | wl_list_for_each_safe(binding, tmp, list, link) |
| 184 | weston_binding_destroy(binding); |
| 185 | } |
| 186 | |
| 187 | struct binding_keyboard_grab { |
| 188 | uint32_t key; |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 189 | struct weston_keyboard_grab grab; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 193 | binding_key(struct weston_keyboard_grab *grab, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 194 | uint32_t time, uint32_t key, uint32_t state_w) |
| 195 | { |
| 196 | struct binding_keyboard_grab *b = |
| 197 | container_of(grab, struct binding_keyboard_grab, grab); |
| 198 | struct wl_resource *resource; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 199 | enum wl_keyboard_key_state state = state_w; |
| 200 | uint32_t serial; |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 201 | struct weston_keyboard *keyboard = grab->keyboard; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 202 | struct wl_display *display = keyboard->seat->compositor->wl_display; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 203 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 204 | if (key == b->key) { |
| 205 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 206 | weston_keyboard_end_grab(grab->keyboard); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 207 | if (keyboard->input_method_resource) |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 208 | keyboard->grab = &keyboard->input_method_grab; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 209 | free(b); |
Giulio Camuffo | 943b46e | 2014-12-05 18:02:58 +0200 | [diff] [blame] | 210 | } else { |
| 211 | /* Don't send the key press event for the binding key */ |
| 212 | return; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 213 | } |
Giulio Camuffo | a20ca81 | 2014-11-22 11:16:56 +0200 | [diff] [blame] | 214 | } |
| 215 | if (!wl_list_empty(&keyboard->focus_resource_list)) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 216 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 217 | wl_resource_for_each(resource, &keyboard->focus_resource_list) { |
| 218 | wl_keyboard_send_key(resource, |
| 219 | serial, |
| 220 | time, |
| 221 | key, |
| 222 | state); |
| 223 | } |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 228 | binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 229 | uint32_t mods_depressed, uint32_t mods_latched, |
| 230 | uint32_t mods_locked, uint32_t group) |
| 231 | { |
| 232 | struct wl_resource *resource; |
| 233 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 234 | wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) { |
| 235 | wl_keyboard_send_modifiers(resource, serial, mods_depressed, |
| 236 | mods_latched, mods_locked, group); |
| 237 | } |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 240 | static void |
| 241 | binding_cancel(struct weston_keyboard_grab *grab) |
| 242 | { |
| 243 | struct binding_keyboard_grab *binding_grab = |
| 244 | container_of(grab, struct binding_keyboard_grab, grab); |
| 245 | |
| 246 | weston_keyboard_end_grab(grab->keyboard); |
| 247 | free(binding_grab); |
| 248 | } |
| 249 | |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 250 | static const struct weston_keyboard_grab_interface binding_grab = { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 251 | binding_key, |
| 252 | binding_modifiers, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 253 | binding_cancel, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | static void |
Derek Foreman | b591a30 | 2015-07-15 13:00:42 -0500 | [diff] [blame^] | 257 | install_binding_grab(struct weston_keyboard *keyboard, uint32_t time, |
| 258 | uint32_t key, struct weston_surface *focus) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 259 | { |
| 260 | struct binding_keyboard_grab *grab; |
| 261 | |
| 262 | grab = malloc(sizeof *grab); |
| 263 | grab->key = key; |
| 264 | grab->grab.interface = &binding_grab; |
Derek Foreman | b591a30 | 2015-07-15 13:00:42 -0500 | [diff] [blame^] | 265 | weston_keyboard_start_grab(keyboard, &grab->grab); |
Giulio Camuffo | a20ca81 | 2014-11-22 11:16:56 +0200 | [diff] [blame] | 266 | |
| 267 | /* Notify the surface which had the focus before this binding |
| 268 | * triggered that we stole a keypress from under it, by forcing |
| 269 | * a wl_keyboard leave/enter pair. The enter event will contain |
| 270 | * the pressed key in the keys array, so the client will know |
| 271 | * the exact state of the keyboard. |
| 272 | * If the old focus surface is different than the new one it |
| 273 | * means it was changed in the binding handler, so it received |
| 274 | * the enter event already. */ |
Derek Foreman | b591a30 | 2015-07-15 13:00:42 -0500 | [diff] [blame^] | 275 | if (focus && keyboard->focus == focus) { |
| 276 | weston_keyboard_set_focus(keyboard, NULL); |
| 277 | weston_keyboard_set_focus(keyboard, focus); |
Giulio Camuffo | a20ca81 | 2014-11-22 11:16:56 +0200 | [diff] [blame] | 278 | } |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 281 | void |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 282 | weston_compositor_run_key_binding(struct weston_compositor *compositor, |
| 283 | struct weston_seat *seat, |
| 284 | uint32_t time, uint32_t key, |
| 285 | enum wl_keyboard_key_state state) |
| 286 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 287 | struct weston_binding *b, *tmp; |
Giulio Camuffo | a20ca81 | 2014-11-22 11:16:56 +0200 | [diff] [blame] | 288 | struct weston_surface *focus; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 289 | |
| 290 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 291 | return; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 292 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 293 | /* Invalidate all active modifier bindings. */ |
| 294 | wl_list_for_each(b, &compositor->modifier_binding_list, link) |
| 295 | b->key = key; |
| 296 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 297 | wl_list_for_each_safe(b, tmp, &compositor->key_binding_list, link) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 298 | if (b->key == key && b->modifier == seat->modifier_state) { |
| 299 | weston_key_binding_handler_t handler = b->handler; |
Giulio Camuffo | a20ca81 | 2014-11-22 11:16:56 +0200 | [diff] [blame] | 300 | focus = seat->keyboard->focus; |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 301 | handler(seat->keyboard, time, key, b->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 302 | |
| 303 | /* If this was a key binding and it didn't |
| 304 | * install a keyboard grab, install one now to |
Giulio Camuffo | 943b46e | 2014-12-05 18:02:58 +0200 | [diff] [blame] | 305 | * swallow the key press. */ |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 306 | if (seat->keyboard->grab == |
| 307 | &seat->keyboard->default_grab) |
Derek Foreman | b591a30 | 2015-07-15 13:00:42 -0500 | [diff] [blame^] | 308 | install_binding_grab(seat->keyboard, |
| 309 | time, |
| 310 | key, |
| 311 | focus); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 316 | void |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 317 | weston_compositor_run_modifier_binding(struct weston_compositor *compositor, |
| 318 | struct weston_seat *seat, |
| 319 | enum weston_keyboard_modifier modifier, |
| 320 | enum wl_keyboard_key_state state) |
| 321 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 322 | struct weston_binding *b, *tmp; |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 323 | |
Emilio Pozuelo Monfort | 1539ea2 | 2013-11-27 10:34:32 +0100 | [diff] [blame] | 324 | if (seat->keyboard->grab != &seat->keyboard->default_grab) |
| 325 | return; |
| 326 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 327 | wl_list_for_each_safe(b, tmp, &compositor->modifier_binding_list, link) { |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 328 | weston_modifier_binding_handler_t handler = b->handler; |
| 329 | |
| 330 | if (b->modifier != modifier) |
| 331 | continue; |
| 332 | |
| 333 | /* Prime the modifier binding. */ |
| 334 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 335 | b->key = 0; |
| 336 | continue; |
| 337 | } |
| 338 | /* Ignore the binding if a key was pressed in between. */ |
| 339 | else if (b->key != 0) { |
| 340 | return; |
| 341 | } |
| 342 | |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 343 | handler(seat->keyboard, modifier, b->data); |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 347 | void |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 348 | weston_compositor_run_button_binding(struct weston_compositor *compositor, |
| 349 | struct weston_seat *seat, |
| 350 | uint32_t time, uint32_t button, |
| 351 | enum wl_pointer_button_state state) |
| 352 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 353 | struct weston_binding *b, *tmp; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 354 | |
| 355 | if (state == WL_POINTER_BUTTON_STATE_RELEASED) |
| 356 | return; |
| 357 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 358 | /* Invalidate all active modifier bindings. */ |
| 359 | wl_list_for_each(b, &compositor->modifier_binding_list, link) |
| 360 | b->key = button; |
| 361 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 362 | wl_list_for_each_safe(b, tmp, &compositor->button_binding_list, link) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 363 | if (b->button == button && b->modifier == seat->modifier_state) { |
| 364 | weston_button_binding_handler_t handler = b->handler; |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 365 | handler(seat->pointer, time, button, b->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 370 | void |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 371 | weston_compositor_run_touch_binding(struct weston_compositor *compositor, |
| 372 | struct weston_seat *seat, uint32_t time, |
| 373 | int touch_type) |
| 374 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 375 | struct weston_binding *b, *tmp; |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 376 | |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 377 | if (seat->touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN) |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 378 | return; |
| 379 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 380 | wl_list_for_each_safe(b, tmp, &compositor->touch_binding_list, link) { |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 381 | if (b->modifier == seat->modifier_state) { |
| 382 | weston_touch_binding_handler_t handler = b->handler; |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 383 | handler(seat->touch, time, b->data); |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 388 | int |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 389 | weston_compositor_run_axis_binding(struct weston_compositor *compositor, |
| 390 | struct weston_seat *seat, |
| 391 | uint32_t time, uint32_t axis, |
| 392 | wl_fixed_t value) |
| 393 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 394 | struct weston_binding *b, *tmp; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 395 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 396 | /* Invalidate all active modifier bindings. */ |
| 397 | wl_list_for_each(b, &compositor->modifier_binding_list, link) |
| 398 | b->key = axis; |
| 399 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 400 | wl_list_for_each_safe(b, tmp, &compositor->axis_binding_list, link) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 401 | if (b->axis == axis && b->modifier == seat->modifier_state) { |
| 402 | weston_axis_binding_handler_t handler = b->handler; |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 403 | handler(seat->pointer, time, axis, value, b->data); |
Rune K. Svendsen | 14b2fe7 | 2013-03-07 21:50:00 +0100 | [diff] [blame] | 404 | return 1; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 405 | } |
| 406 | } |
Rune K. Svendsen | 14b2fe7 | 2013-03-07 21:50:00 +0100 | [diff] [blame] | 407 | |
| 408 | return 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 411 | int |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 412 | weston_compositor_run_debug_binding(struct weston_compositor *compositor, |
| 413 | struct weston_seat *seat, |
| 414 | uint32_t time, uint32_t key, |
| 415 | enum wl_keyboard_key_state state) |
| 416 | { |
| 417 | weston_key_binding_handler_t handler; |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 418 | struct weston_binding *binding, *tmp; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 419 | int count = 0; |
| 420 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 421 | wl_list_for_each_safe(binding, tmp, &compositor->debug_binding_list, link) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 422 | if (key != binding->key) |
| 423 | continue; |
| 424 | |
| 425 | count++; |
| 426 | handler = binding->handler; |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 427 | handler(seat->keyboard, time, key, binding->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return count; |
| 431 | } |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 432 | |
| 433 | struct debug_binding_grab { |
| 434 | struct weston_keyboard_grab grab; |
| 435 | struct weston_seat *seat; |
| 436 | uint32_t key[2]; |
| 437 | int key_released[2]; |
| 438 | }; |
| 439 | |
| 440 | static void |
| 441 | debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time, |
| 442 | uint32_t key, uint32_t state) |
| 443 | { |
| 444 | struct debug_binding_grab *db = (struct debug_binding_grab *) grab; |
| 445 | struct weston_compositor *ec = db->seat->compositor; |
| 446 | struct wl_display *display = ec->wl_display; |
| 447 | struct wl_resource *resource; |
| 448 | uint32_t serial; |
| 449 | int send = 0, terminate = 0; |
| 450 | int check_binding = 1; |
| 451 | int i; |
| 452 | struct wl_list *resource_list; |
| 453 | |
| 454 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) { |
| 455 | /* Do not run bindings on key releases */ |
| 456 | check_binding = 0; |
| 457 | |
| 458 | for (i = 0; i < 2; i++) |
| 459 | if (key == db->key[i]) |
| 460 | db->key_released[i] = 1; |
| 461 | |
| 462 | if (db->key_released[0] && db->key_released[1]) { |
| 463 | /* All key releases been swalled so end the grab */ |
| 464 | terminate = 1; |
| 465 | } else if (key != db->key[0] && key != db->key[1]) { |
| 466 | /* Should not swallow release of other keys */ |
| 467 | send = 1; |
| 468 | } |
| 469 | } else if (key == db->key[0] && !db->key_released[0]) { |
| 470 | /* Do not check bindings for the first press of the binding |
| 471 | * key. This allows it to be used as a debug shortcut. |
| 472 | * We still need to swallow this event. */ |
| 473 | check_binding = 0; |
| 474 | } else if (db->key[1]) { |
| 475 | /* If we already ran a binding don't process another one since |
| 476 | * we can't keep track of all the binding keys that were |
| 477 | * pressed in order to swallow the release events. */ |
| 478 | send = 1; |
| 479 | check_binding = 0; |
| 480 | } |
| 481 | |
| 482 | if (check_binding) { |
| 483 | if (weston_compositor_run_debug_binding(ec, db->seat, time, |
| 484 | key, state)) { |
| 485 | /* We ran a binding so swallow the press and keep the |
| 486 | * grab to swallow the released too. */ |
| 487 | send = 0; |
| 488 | terminate = 0; |
| 489 | db->key[1] = key; |
| 490 | } else { |
| 491 | /* Terminate the grab since the key pressed is not a |
| 492 | * debug binding key. */ |
| 493 | send = 1; |
| 494 | terminate = 1; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (send) { |
| 499 | serial = wl_display_next_serial(display); |
| 500 | resource_list = &grab->keyboard->focus_resource_list; |
| 501 | wl_resource_for_each(resource, resource_list) { |
| 502 | wl_keyboard_send_key(resource, serial, time, key, state); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | if (terminate) { |
| 507 | weston_keyboard_end_grab(grab->keyboard); |
| 508 | if (grab->keyboard->input_method_resource) |
| 509 | grab->keyboard->grab = &grab->keyboard->input_method_grab; |
| 510 | free(db); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | static void |
| 515 | debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial, |
| 516 | uint32_t mods_depressed, uint32_t mods_latched, |
| 517 | uint32_t mods_locked, uint32_t group) |
| 518 | { |
| 519 | struct wl_resource *resource; |
| 520 | struct wl_list *resource_list; |
| 521 | |
| 522 | resource_list = &grab->keyboard->focus_resource_list; |
| 523 | |
| 524 | wl_resource_for_each(resource, resource_list) { |
| 525 | wl_keyboard_send_modifiers(resource, serial, mods_depressed, |
| 526 | mods_latched, mods_locked, group); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static void |
| 531 | debug_binding_cancel(struct weston_keyboard_grab *grab) |
| 532 | { |
| 533 | struct debug_binding_grab *db = (struct debug_binding_grab *) grab; |
| 534 | |
| 535 | weston_keyboard_end_grab(grab->keyboard); |
| 536 | free(db); |
| 537 | } |
| 538 | |
| 539 | struct weston_keyboard_grab_interface debug_binding_keyboard_grab = { |
| 540 | debug_binding_key, |
| 541 | debug_binding_modifiers, |
| 542 | debug_binding_cancel, |
| 543 | }; |
| 544 | |
| 545 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 546 | debug_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 547 | uint32_t key, void *data) |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 548 | { |
| 549 | struct debug_binding_grab *grab; |
| 550 | |
| 551 | grab = calloc(1, sizeof *grab); |
| 552 | if (!grab) |
| 553 | return; |
| 554 | |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 555 | grab->seat = keyboard->seat; |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 556 | grab->key[0] = key; |
| 557 | grab->grab.interface = &debug_binding_keyboard_grab; |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 558 | weston_keyboard_start_grab(keyboard, &grab->grab); |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /** Install the trigger binding for debug bindings. |
| 562 | * |
| 563 | * \param compositor The compositor. |
| 564 | * \param mod The modifier. |
| 565 | * |
| 566 | * This will add a key binding for modifier+SHIFT+SPACE that will trigger |
| 567 | * debug key bindings. |
| 568 | */ |
| 569 | WL_EXPORT void |
| 570 | weston_install_debug_key_binding(struct weston_compositor *compositor, |
| 571 | uint32_t mod) |
| 572 | { |
| 573 | weston_compositor_add_key_binding(compositor, KEY_SPACE, |
| 574 | mod | MODIFIER_SHIFT, |
| 575 | debug_binding, NULL); |
| 576 | } |