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