blob: 2ca999a3fcf9f634e1a89137bc9755237c4eb4de [file] [log] [blame]
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02001/*
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02002 * Copyright © 2011-2012 Intel Corporation
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * 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 Oliveiracbdebc22013-02-21 18:35:16 +020011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * 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 Oliveiracbdebc22013-02-21 18:35:16 +020024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030028#include <stdint.h>
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020029#include <stdlib.h>
Pekka Paalanen2829f7c2015-02-19 17:02:13 +020030#include <linux/input.h>
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020031
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020032#include <libweston/libweston.h>
Guillaume Champagnef1e8fc92020-01-27 20:14:29 -050033#include "libweston-internal.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070034#include "shared/helpers.h"
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020035#include "shared/timespec-util.h"
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020036
37struct weston_binding {
38 uint32_t key;
39 uint32_t button;
40 uint32_t axis;
41 uint32_t modifier;
42 void *handler;
43 void *data;
44 struct wl_list link;
45};
46
47static struct weston_binding *
48weston_compositor_add_binding(struct weston_compositor *compositor,
49 uint32_t key, uint32_t button, uint32_t axis,
50 uint32_t modifier, void *handler, void *data)
51{
52 struct weston_binding *binding;
53
54 binding = malloc(sizeof *binding);
55 if (binding == NULL)
56 return NULL;
57
58 binding->key = key;
59 binding->button = button;
60 binding->axis = axis;
61 binding->modifier = modifier;
62 binding->handler = handler;
63 binding->data = data;
64
65 return binding;
66}
67
68WL_EXPORT struct weston_binding *
69weston_compositor_add_key_binding(struct weston_compositor *compositor,
70 uint32_t key, uint32_t modifier,
71 weston_key_binding_handler_t handler,
72 void *data)
73{
74 struct weston_binding *binding;
75
76 binding = weston_compositor_add_binding(compositor, key, 0, 0,
77 modifier, handler, data);
78 if (binding == NULL)
79 return NULL;
80
81 wl_list_insert(compositor->key_binding_list.prev, &binding->link);
82
83 return binding;
84}
85
86WL_EXPORT struct weston_binding *
Daniel Stone96d47c02013-11-19 11:37:12 +010087weston_compositor_add_modifier_binding(struct weston_compositor *compositor,
88 uint32_t modifier,
89 weston_modifier_binding_handler_t handler,
90 void *data)
91{
92 struct weston_binding *binding;
93
94 binding = weston_compositor_add_binding(compositor, 0, 0, 0,
95 modifier, handler, data);
96 if (binding == NULL)
97 return NULL;
98
99 wl_list_insert(compositor->modifier_binding_list.prev, &binding->link);
100
101 return binding;
102}
103
104WL_EXPORT struct weston_binding *
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200105weston_compositor_add_button_binding(struct weston_compositor *compositor,
106 uint32_t button, uint32_t modifier,
107 weston_button_binding_handler_t handler,
108 void *data)
109{
110 struct weston_binding *binding;
111
112 binding = weston_compositor_add_binding(compositor, 0, button, 0,
113 modifier, handler, data);
114 if (binding == NULL)
115 return NULL;
116
117 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
118
119 return binding;
120}
121
122WL_EXPORT struct weston_binding *
Neil Robertsa28c6932013-10-03 16:43:04 +0100123weston_compositor_add_touch_binding(struct weston_compositor *compositor,
124 uint32_t modifier,
125 weston_touch_binding_handler_t handler,
126 void *data)
127{
128 struct weston_binding *binding;
129
130 binding = weston_compositor_add_binding(compositor, 0, 0, 0,
131 modifier, handler, data);
132 if (binding == NULL)
133 return NULL;
134
135 wl_list_insert(compositor->touch_binding_list.prev, &binding->link);
136
137 return binding;
138}
139
140WL_EXPORT struct weston_binding *
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200141weston_compositor_add_axis_binding(struct weston_compositor *compositor,
142 uint32_t axis, uint32_t modifier,
143 weston_axis_binding_handler_t handler,
144 void *data)
145{
146 struct weston_binding *binding;
147
148 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
149 modifier, handler, data);
150 if (binding == NULL)
151 return NULL;
152
153 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
154
155 return binding;
156}
157
158WL_EXPORT struct weston_binding *
159weston_compositor_add_debug_binding(struct weston_compositor *compositor,
160 uint32_t key,
161 weston_key_binding_handler_t handler,
162 void *data)
163{
164 struct weston_binding *binding;
165
166 binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
167 handler, data);
168
169 wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
170
171 return binding;
172}
173
174WL_EXPORT void
175weston_binding_destroy(struct weston_binding *binding)
176{
177 wl_list_remove(&binding->link);
178 free(binding);
179}
180
Derek Foreman2e6485c2015-07-15 13:00:34 -0500181void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200182weston_binding_list_destroy_all(struct wl_list *list)
183{
184 struct weston_binding *binding, *tmp;
185
186 wl_list_for_each_safe(binding, tmp, list, link)
187 weston_binding_destroy(binding);
188}
189
190struct binding_keyboard_grab {
191 uint32_t key;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400192 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200193};
194
195static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400196binding_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200197 const struct timespec *time, uint32_t key, uint32_t state_w)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200198{
199 struct binding_keyboard_grab *b =
200 container_of(grab, struct binding_keyboard_grab, grab);
201 struct wl_resource *resource;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200202 enum wl_keyboard_key_state state = state_w;
203 uint32_t serial;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400204 struct weston_keyboard *keyboard = grab->keyboard;
Rob Bradford880ebc72013-07-22 17:31:38 +0100205 struct wl_display *display = keyboard->seat->compositor->wl_display;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200206 uint32_t msecs;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200207
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200208 if (key == b->key) {
209 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400210 weston_keyboard_end_grab(grab->keyboard);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200211 if (keyboard->input_method_resource)
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400212 keyboard->grab = &keyboard->input_method_grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200213 free(b);
Giulio Camuffo943b46e2014-12-05 18:02:58 +0200214 } else {
215 /* Don't send the key press event for the binding key */
216 return;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200217 }
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200218 }
219 if (!wl_list_empty(&keyboard->focus_resource_list)) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200220 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200221 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100222 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
223 wl_keyboard_send_key(resource,
224 serial,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200225 msecs,
Neil Roberts96d790e2013-09-19 17:32:00 +0100226 key,
227 state);
228 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200229 }
230}
231
232static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400233binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200234 uint32_t mods_depressed, uint32_t mods_latched,
235 uint32_t mods_locked, uint32_t group)
236{
237 struct wl_resource *resource;
238
Neil Roberts96d790e2013-09-19 17:32:00 +0100239 wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) {
240 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
241 mods_latched, mods_locked, group);
242 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200243}
244
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200245static void
246binding_cancel(struct weston_keyboard_grab *grab)
247{
248 struct binding_keyboard_grab *binding_grab =
249 container_of(grab, struct binding_keyboard_grab, grab);
250
251 weston_keyboard_end_grab(grab->keyboard);
252 free(binding_grab);
253}
254
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400255static const struct weston_keyboard_grab_interface binding_grab = {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200256 binding_key,
257 binding_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200258 binding_cancel,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200259};
260
261static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200262install_binding_grab(struct weston_keyboard *keyboard,
263 const struct timespec *time, uint32_t key,
264 struct weston_surface *focus)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200265{
266 struct binding_keyboard_grab *grab;
267
268 grab = malloc(sizeof *grab);
269 grab->key = key;
270 grab->grab.interface = &binding_grab;
Derek Foremanb591a302015-07-15 13:00:42 -0500271 weston_keyboard_start_grab(keyboard, &grab->grab);
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200272
273 /* Notify the surface which had the focus before this binding
274 * triggered that we stole a keypress from under it, by forcing
275 * a wl_keyboard leave/enter pair. The enter event will contain
276 * the pressed key in the keys array, so the client will know
277 * the exact state of the keyboard.
278 * If the old focus surface is different than the new one it
279 * means it was changed in the binding handler, so it received
280 * the enter event already. */
Derek Foremanb591a302015-07-15 13:00:42 -0500281 if (focus && keyboard->focus == focus) {
282 weston_keyboard_set_focus(keyboard, NULL);
283 weston_keyboard_set_focus(keyboard, focus);
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200284 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200285}
286
Derek Foreman2e6485c2015-07-15 13:00:34 -0500287void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200288weston_compositor_run_key_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500289 struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200290 const struct timespec *time, uint32_t key,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200291 enum wl_keyboard_key_state state)
292{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300293 struct weston_binding *b, *tmp;
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200294 struct weston_surface *focus;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500295 struct weston_seat *seat = keyboard->seat;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200296
297 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Pekka Paalanen86b53962014-11-19 13:43:32 +0200298 return;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200299
Daniel Stone96d47c02013-11-19 11:37:12 +0100300 /* Invalidate all active modifier bindings. */
301 wl_list_for_each(b, &compositor->modifier_binding_list, link)
302 b->key = key;
303
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300304 wl_list_for_each_safe(b, tmp, &compositor->key_binding_list, link) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200305 if (b->key == key && b->modifier == seat->modifier_state) {
306 weston_key_binding_handler_t handler = b->handler;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500307 focus = keyboard->focus;
308 handler(keyboard, time, key, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200309
310 /* If this was a key binding and it didn't
311 * install a keyboard grab, install one now to
Giulio Camuffo943b46e2014-12-05 18:02:58 +0200312 * swallow the key press. */
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500313 if (keyboard->grab ==
314 &keyboard->default_grab)
315 install_binding_grab(keyboard,
Derek Foremanb591a302015-07-15 13:00:42 -0500316 time,
317 key,
318 focus);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200319 }
320 }
321}
322
Derek Foreman2e6485c2015-07-15 13:00:34 -0500323void
Daniel Stone96d47c02013-11-19 11:37:12 +0100324weston_compositor_run_modifier_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500325 struct weston_keyboard *keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +0100326 enum weston_keyboard_modifier modifier,
327 enum wl_keyboard_key_state state)
328{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300329 struct weston_binding *b, *tmp;
Daniel Stone96d47c02013-11-19 11:37:12 +0100330
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500331 if (keyboard->grab != &keyboard->default_grab)
Emilio Pozuelo Monfort1539ea22013-11-27 10:34:32 +0100332 return;
333
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300334 wl_list_for_each_safe(b, tmp, &compositor->modifier_binding_list, link) {
Daniel Stone96d47c02013-11-19 11:37:12 +0100335 weston_modifier_binding_handler_t handler = b->handler;
336
337 if (b->modifier != modifier)
338 continue;
339
340 /* Prime the modifier binding. */
341 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
342 b->key = 0;
343 continue;
344 }
345 /* Ignore the binding if a key was pressed in between. */
346 else if (b->key != 0) {
347 return;
348 }
349
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500350 handler(keyboard, modifier, b->data);
Daniel Stone96d47c02013-11-19 11:37:12 +0100351 }
352}
353
Derek Foreman2e6485c2015-07-15 13:00:34 -0500354void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200355weston_compositor_run_button_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500356 struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200357 const struct timespec *time,
358 uint32_t button,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200359 enum wl_pointer_button_state state)
360{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300361 struct weston_binding *b, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200362
363 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
364 return;
365
Daniel Stone96d47c02013-11-19 11:37:12 +0100366 /* Invalidate all active modifier bindings. */
367 wl_list_for_each(b, &compositor->modifier_binding_list, link)
368 b->key = button;
369
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300370 wl_list_for_each_safe(b, tmp, &compositor->button_binding_list, link) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500371 if (b->button == button &&
372 b->modifier == pointer->seat->modifier_state) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200373 weston_button_binding_handler_t handler = b->handler;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500374 handler(pointer, time, button, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200375 }
376 }
377}
378
Derek Foreman2e6485c2015-07-15 13:00:34 -0500379void
Neil Robertsa28c6932013-10-03 16:43:04 +0100380weston_compositor_run_touch_binding(struct weston_compositor *compositor,
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200381 struct weston_touch *touch,
382 const struct timespec *time,
Neil Robertsa28c6932013-10-03 16:43:04 +0100383 int touch_type)
384{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300385 struct weston_binding *b, *tmp;
Neil Robertsa28c6932013-10-03 16:43:04 +0100386
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500387 if (touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN)
Neil Robertsa28c6932013-10-03 16:43:04 +0100388 return;
389
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300390 wl_list_for_each_safe(b, tmp, &compositor->touch_binding_list, link) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500391 if (b->modifier == touch->seat->modifier_state) {
Neil Robertsa28c6932013-10-03 16:43:04 +0100392 weston_touch_binding_handler_t handler = b->handler;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500393 handler(touch, time, b->data);
Neil Robertsa28c6932013-10-03 16:43:04 +0100394 }
395 }
396}
397
Derek Foreman2e6485c2015-07-15 13:00:34 -0500398int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200399weston_compositor_run_axis_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500400 struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200401 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000402 struct weston_pointer_axis_event *event)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200403{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300404 struct weston_binding *b, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200405
Daniel Stone96d47c02013-11-19 11:37:12 +0100406 /* Invalidate all active modifier bindings. */
407 wl_list_for_each(b, &compositor->modifier_binding_list, link)
Peter Hutterer89b6a492016-01-18 15:58:17 +1000408 b->key = event->axis;
Daniel Stone96d47c02013-11-19 11:37:12 +0100409
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300410 wl_list_for_each_safe(b, tmp, &compositor->axis_binding_list, link) {
Peter Hutterer89b6a492016-01-18 15:58:17 +1000411 if (b->axis == event->axis &&
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500412 b->modifier == pointer->seat->modifier_state) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200413 weston_axis_binding_handler_t handler = b->handler;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000414 handler(pointer, time, event, b->data);
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100415 return 1;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200416 }
417 }
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100418
419 return 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200420}
421
Derek Foreman2e6485c2015-07-15 13:00:34 -0500422int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200423weston_compositor_run_debug_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500424 struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200425 const struct timespec *time, uint32_t key,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200426 enum wl_keyboard_key_state state)
427{
428 weston_key_binding_handler_t handler;
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300429 struct weston_binding *binding, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200430 int count = 0;
431
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300432 wl_list_for_each_safe(binding, tmp, &compositor->debug_binding_list, link) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200433 if (key != binding->key)
434 continue;
435
436 count++;
437 handler = binding->handler;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500438 handler(keyboard, time, key, binding->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200439 }
440
441 return count;
442}
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200443
444struct debug_binding_grab {
445 struct weston_keyboard_grab grab;
446 struct weston_seat *seat;
447 uint32_t key[2];
448 int key_released[2];
449};
450
451static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200452debug_binding_key(struct weston_keyboard_grab *grab, const struct timespec *time,
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200453 uint32_t key, uint32_t state)
454{
455 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
456 struct weston_compositor *ec = db->seat->compositor;
457 struct wl_display *display = ec->wl_display;
458 struct wl_resource *resource;
459 uint32_t serial;
460 int send = 0, terminate = 0;
461 int check_binding = 1;
462 int i;
463 struct wl_list *resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200464 uint32_t msecs;
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200465
466 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
467 /* Do not run bindings on key releases */
468 check_binding = 0;
469
470 for (i = 0; i < 2; i++)
471 if (key == db->key[i])
472 db->key_released[i] = 1;
473
474 if (db->key_released[0] && db->key_released[1]) {
475 /* All key releases been swalled so end the grab */
476 terminate = 1;
477 } else if (key != db->key[0] && key != db->key[1]) {
478 /* Should not swallow release of other keys */
479 send = 1;
480 }
481 } else if (key == db->key[0] && !db->key_released[0]) {
482 /* Do not check bindings for the first press of the binding
483 * key. This allows it to be used as a debug shortcut.
484 * We still need to swallow this event. */
485 check_binding = 0;
486 } else if (db->key[1]) {
487 /* If we already ran a binding don't process another one since
488 * we can't keep track of all the binding keys that were
489 * pressed in order to swallow the release events. */
490 send = 1;
491 check_binding = 0;
492 }
493
494 if (check_binding) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500495 if (weston_compositor_run_debug_binding(ec, grab->keyboard,
496 time, key, state)) {
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200497 /* We ran a binding so swallow the press and keep the
498 * grab to swallow the released too. */
499 send = 0;
500 terminate = 0;
501 db->key[1] = key;
502 } else {
503 /* Terminate the grab since the key pressed is not a
504 * debug binding key. */
505 send = 1;
506 terminate = 1;
507 }
508 }
509
510 if (send) {
511 serial = wl_display_next_serial(display);
512 resource_list = &grab->keyboard->focus_resource_list;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200513 msecs = timespec_to_msec(time);
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200514 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200515 wl_keyboard_send_key(resource, serial, msecs, key, state);
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200516 }
517 }
518
519 if (terminate) {
520 weston_keyboard_end_grab(grab->keyboard);
521 if (grab->keyboard->input_method_resource)
522 grab->keyboard->grab = &grab->keyboard->input_method_grab;
523 free(db);
524 }
525}
526
527static void
528debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
529 uint32_t mods_depressed, uint32_t mods_latched,
530 uint32_t mods_locked, uint32_t group)
531{
532 struct wl_resource *resource;
533 struct wl_list *resource_list;
534
535 resource_list = &grab->keyboard->focus_resource_list;
536
537 wl_resource_for_each(resource, resource_list) {
538 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
539 mods_latched, mods_locked, group);
540 }
541}
542
543static void
544debug_binding_cancel(struct weston_keyboard_grab *grab)
545{
546 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
547
548 weston_keyboard_end_grab(grab->keyboard);
549 free(db);
550}
551
552struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
553 debug_binding_key,
554 debug_binding_modifiers,
555 debug_binding_cancel,
556};
557
558static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200559debug_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -0500560 uint32_t key, void *data)
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200561{
562 struct debug_binding_grab *grab;
563
564 grab = calloc(1, sizeof *grab);
565 if (!grab)
566 return;
567
Derek Foreman8ae2db52015-07-15 13:00:36 -0500568 grab->seat = keyboard->seat;
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200569 grab->key[0] = key;
570 grab->grab.interface = &debug_binding_keyboard_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -0500571 weston_keyboard_start_grab(keyboard, &grab->grab);
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200572}
573
574/** Install the trigger binding for debug bindings.
575 *
576 * \param compositor The compositor.
577 * \param mod The modifier.
578 *
579 * This will add a key binding for modifier+SHIFT+SPACE that will trigger
580 * debug key bindings.
581 */
582WL_EXPORT void
583weston_install_debug_key_binding(struct weston_compositor *compositor,
584 uint32_t mod)
585{
586 weston_compositor_add_key_binding(compositor, KEY_SPACE,
587 mod | MODIFIER_SHIFT,
588 debug_binding, NULL);
589}