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, |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 283 | struct weston_keyboard *keyboard, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 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; |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 289 | struct weston_seat *seat = keyboard->seat; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 290 | |
| 291 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 292 | return; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 293 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 294 | /* Invalidate all active modifier bindings. */ |
| 295 | wl_list_for_each(b, &compositor->modifier_binding_list, link) |
| 296 | b->key = key; |
| 297 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 298 | 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] | 299 | if (b->key == key && b->modifier == seat->modifier_state) { |
| 300 | weston_key_binding_handler_t handler = b->handler; |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 301 | focus = keyboard->focus; |
| 302 | handler(keyboard, time, key, b->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 303 | |
| 304 | /* If this was a key binding and it didn't |
| 305 | * install a keyboard grab, install one now to |
Giulio Camuffo | 943b46e | 2014-12-05 18:02:58 +0200 | [diff] [blame] | 306 | * swallow the key press. */ |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 307 | if (keyboard->grab == |
| 308 | &keyboard->default_grab) |
| 309 | install_binding_grab(keyboard, |
Derek Foreman | b591a30 | 2015-07-15 13:00:42 -0500 | [diff] [blame] | 310 | time, |
| 311 | key, |
| 312 | focus); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 317 | void |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 318 | weston_compositor_run_modifier_binding(struct weston_compositor *compositor, |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 319 | struct weston_keyboard *keyboard, |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 320 | enum weston_keyboard_modifier modifier, |
| 321 | enum wl_keyboard_key_state state) |
| 322 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 323 | struct weston_binding *b, *tmp; |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 324 | |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 325 | if (keyboard->grab != &keyboard->default_grab) |
Emilio Pozuelo Monfort | 1539ea2 | 2013-11-27 10:34:32 +0100 | [diff] [blame] | 326 | return; |
| 327 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 328 | wl_list_for_each_safe(b, tmp, &compositor->modifier_binding_list, link) { |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 329 | weston_modifier_binding_handler_t handler = b->handler; |
| 330 | |
| 331 | if (b->modifier != modifier) |
| 332 | continue; |
| 333 | |
| 334 | /* Prime the modifier binding. */ |
| 335 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 336 | b->key = 0; |
| 337 | continue; |
| 338 | } |
| 339 | /* Ignore the binding if a key was pressed in between. */ |
| 340 | else if (b->key != 0) { |
| 341 | return; |
| 342 | } |
| 343 | |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 344 | handler(keyboard, modifier, b->data); |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 348 | void |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 349 | weston_compositor_run_button_binding(struct weston_compositor *compositor, |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 350 | struct weston_pointer *pointer, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 351 | uint32_t time, uint32_t button, |
| 352 | enum wl_pointer_button_state state) |
| 353 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 354 | struct weston_binding *b, *tmp; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 355 | |
| 356 | if (state == WL_POINTER_BUTTON_STATE_RELEASED) |
| 357 | return; |
| 358 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 359 | /* Invalidate all active modifier bindings. */ |
| 360 | wl_list_for_each(b, &compositor->modifier_binding_list, link) |
| 361 | b->key = button; |
| 362 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 363 | wl_list_for_each_safe(b, tmp, &compositor->button_binding_list, link) { |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 364 | if (b->button == button && |
| 365 | b->modifier == pointer->seat->modifier_state) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 366 | weston_button_binding_handler_t handler = b->handler; |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 367 | handler(pointer, time, button, b->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 372 | void |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 373 | weston_compositor_run_touch_binding(struct weston_compositor *compositor, |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 374 | struct weston_touch *touch, uint32_t time, |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 375 | int touch_type) |
| 376 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 377 | struct weston_binding *b, *tmp; |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 378 | |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 379 | if (touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN) |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 380 | return; |
| 381 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 382 | wl_list_for_each_safe(b, tmp, &compositor->touch_binding_list, link) { |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 383 | if (b->modifier == touch->seat->modifier_state) { |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 384 | weston_touch_binding_handler_t handler = b->handler; |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 385 | handler(touch, time, b->data); |
Neil Roberts | a28c693 | 2013-10-03 16:43:04 +0100 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 390 | int |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 391 | weston_compositor_run_axis_binding(struct weston_compositor *compositor, |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 392 | struct weston_pointer *pointer, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 393 | uint32_t time, uint32_t axis, |
| 394 | wl_fixed_t value) |
| 395 | { |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 396 | struct weston_binding *b, *tmp; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 397 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 398 | /* Invalidate all active modifier bindings. */ |
| 399 | wl_list_for_each(b, &compositor->modifier_binding_list, link) |
| 400 | b->key = axis; |
| 401 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 402 | wl_list_for_each_safe(b, tmp, &compositor->axis_binding_list, link) { |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 403 | if (b->axis == axis && |
| 404 | b->modifier == pointer->seat->modifier_state) { |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 405 | weston_axis_binding_handler_t handler = b->handler; |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 406 | handler(pointer, time, axis, value, b->data); |
Rune K. Svendsen | 14b2fe7 | 2013-03-07 21:50:00 +0100 | [diff] [blame] | 407 | return 1; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 408 | } |
| 409 | } |
Rune K. Svendsen | 14b2fe7 | 2013-03-07 21:50:00 +0100 | [diff] [blame] | 410 | |
| 411 | return 0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Derek Foreman | 2e6485c | 2015-07-15 13:00:34 -0500 | [diff] [blame] | 414 | int |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 415 | weston_compositor_run_debug_binding(struct weston_compositor *compositor, |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 416 | struct weston_keyboard *keyboard, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 417 | uint32_t time, uint32_t key, |
| 418 | enum wl_keyboard_key_state state) |
| 419 | { |
| 420 | weston_key_binding_handler_t handler; |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 421 | struct weston_binding *binding, *tmp; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 422 | int count = 0; |
| 423 | |
Giulio Camuffo | 24b98d0 | 2014-10-03 23:36:34 +0300 | [diff] [blame] | 424 | 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] | 425 | if (key != binding->key) |
| 426 | continue; |
| 427 | |
| 428 | count++; |
| 429 | handler = binding->handler; |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 430 | handler(keyboard, time, key, binding->data); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | return count; |
| 434 | } |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 435 | |
| 436 | struct debug_binding_grab { |
| 437 | struct weston_keyboard_grab grab; |
| 438 | struct weston_seat *seat; |
| 439 | uint32_t key[2]; |
| 440 | int key_released[2]; |
| 441 | }; |
| 442 | |
| 443 | static void |
| 444 | debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time, |
| 445 | uint32_t key, uint32_t state) |
| 446 | { |
| 447 | struct debug_binding_grab *db = (struct debug_binding_grab *) grab; |
| 448 | struct weston_compositor *ec = db->seat->compositor; |
| 449 | struct wl_display *display = ec->wl_display; |
| 450 | struct wl_resource *resource; |
| 451 | uint32_t serial; |
| 452 | int send = 0, terminate = 0; |
| 453 | int check_binding = 1; |
| 454 | int i; |
| 455 | struct wl_list *resource_list; |
| 456 | |
| 457 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) { |
| 458 | /* Do not run bindings on key releases */ |
| 459 | check_binding = 0; |
| 460 | |
| 461 | for (i = 0; i < 2; i++) |
| 462 | if (key == db->key[i]) |
| 463 | db->key_released[i] = 1; |
| 464 | |
| 465 | if (db->key_released[0] && db->key_released[1]) { |
| 466 | /* All key releases been swalled so end the grab */ |
| 467 | terminate = 1; |
| 468 | } else if (key != db->key[0] && key != db->key[1]) { |
| 469 | /* Should not swallow release of other keys */ |
| 470 | send = 1; |
| 471 | } |
| 472 | } else if (key == db->key[0] && !db->key_released[0]) { |
| 473 | /* Do not check bindings for the first press of the binding |
| 474 | * key. This allows it to be used as a debug shortcut. |
| 475 | * We still need to swallow this event. */ |
| 476 | check_binding = 0; |
| 477 | } else if (db->key[1]) { |
| 478 | /* If we already ran a binding don't process another one since |
| 479 | * we can't keep track of all the binding keys that were |
| 480 | * pressed in order to swallow the release events. */ |
| 481 | send = 1; |
| 482 | check_binding = 0; |
| 483 | } |
| 484 | |
| 485 | if (check_binding) { |
Derek Foreman | 99a6a2d | 2015-07-15 13:00:43 -0500 | [diff] [blame^] | 486 | if (weston_compositor_run_debug_binding(ec, grab->keyboard, |
| 487 | time, key, state)) { |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 488 | /* We ran a binding so swallow the press and keep the |
| 489 | * grab to swallow the released too. */ |
| 490 | send = 0; |
| 491 | terminate = 0; |
| 492 | db->key[1] = key; |
| 493 | } else { |
| 494 | /* Terminate the grab since the key pressed is not a |
| 495 | * debug binding key. */ |
| 496 | send = 1; |
| 497 | terminate = 1; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (send) { |
| 502 | serial = wl_display_next_serial(display); |
| 503 | resource_list = &grab->keyboard->focus_resource_list; |
| 504 | wl_resource_for_each(resource, resource_list) { |
| 505 | wl_keyboard_send_key(resource, serial, time, key, state); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (terminate) { |
| 510 | weston_keyboard_end_grab(grab->keyboard); |
| 511 | if (grab->keyboard->input_method_resource) |
| 512 | grab->keyboard->grab = &grab->keyboard->input_method_grab; |
| 513 | free(db); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | static void |
| 518 | debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial, |
| 519 | uint32_t mods_depressed, uint32_t mods_latched, |
| 520 | uint32_t mods_locked, uint32_t group) |
| 521 | { |
| 522 | struct wl_resource *resource; |
| 523 | struct wl_list *resource_list; |
| 524 | |
| 525 | resource_list = &grab->keyboard->focus_resource_list; |
| 526 | |
| 527 | wl_resource_for_each(resource, resource_list) { |
| 528 | wl_keyboard_send_modifiers(resource, serial, mods_depressed, |
| 529 | mods_latched, mods_locked, group); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static void |
| 534 | debug_binding_cancel(struct weston_keyboard_grab *grab) |
| 535 | { |
| 536 | struct debug_binding_grab *db = (struct debug_binding_grab *) grab; |
| 537 | |
| 538 | weston_keyboard_end_grab(grab->keyboard); |
| 539 | free(db); |
| 540 | } |
| 541 | |
| 542 | struct weston_keyboard_grab_interface debug_binding_keyboard_grab = { |
| 543 | debug_binding_key, |
| 544 | debug_binding_modifiers, |
| 545 | debug_binding_cancel, |
| 546 | }; |
| 547 | |
| 548 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 549 | debug_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 550 | uint32_t key, void *data) |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 551 | { |
| 552 | struct debug_binding_grab *grab; |
| 553 | |
| 554 | grab = calloc(1, sizeof *grab); |
| 555 | if (!grab) |
| 556 | return; |
| 557 | |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 558 | grab->seat = keyboard->seat; |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 559 | grab->key[0] = key; |
| 560 | grab->grab.interface = &debug_binding_keyboard_grab; |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 561 | weston_keyboard_start_grab(keyboard, &grab->grab); |
Pekka Paalanen | 2829f7c | 2015-02-19 17:02:13 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /** Install the trigger binding for debug bindings. |
| 565 | * |
| 566 | * \param compositor The compositor. |
| 567 | * \param mod The modifier. |
| 568 | * |
| 569 | * This will add a key binding for modifier+SHIFT+SPACE that will trigger |
| 570 | * debug key bindings. |
| 571 | */ |
| 572 | WL_EXPORT void |
| 573 | weston_install_debug_key_binding(struct weston_compositor *compositor, |
| 574 | uint32_t mod) |
| 575 | { |
| 576 | weston_compositor_add_key_binding(compositor, KEY_SPACE, |
| 577 | mod | MODIFIER_SHIFT, |
| 578 | debug_binding, NULL); |
| 579 | } |