blob: 68c07a22f3f18af395c27bf298e0322c06a6c446 [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>
Jon Cruz867d50e2015-06-15 15:37:10 -070033#include "shared/helpers.h"
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020034#include "shared/timespec-util.h"
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020035
36struct 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
46static struct weston_binding *
47weston_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
67WL_EXPORT struct weston_binding *
68weston_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
85WL_EXPORT struct weston_binding *
Daniel Stone96d47c02013-11-19 11:37:12 +010086weston_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
103WL_EXPORT struct weston_binding *
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200104weston_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
121WL_EXPORT struct weston_binding *
Neil Robertsa28c6932013-10-03 16:43:04 +0100122weston_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
139WL_EXPORT struct weston_binding *
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200140weston_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
157WL_EXPORT struct weston_binding *
158weston_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
173WL_EXPORT void
174weston_binding_destroy(struct weston_binding *binding)
175{
176 wl_list_remove(&binding->link);
177 free(binding);
178}
179
Derek Foreman2e6485c2015-07-15 13:00:34 -0500180void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200181weston_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
189struct binding_keyboard_grab {
190 uint32_t key;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400191 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200192};
193
194static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400195binding_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200196 const struct timespec *time, uint32_t key, uint32_t state_w)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200197{
198 struct binding_keyboard_grab *b =
199 container_of(grab, struct binding_keyboard_grab, grab);
200 struct wl_resource *resource;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200201 enum wl_keyboard_key_state state = state_w;
202 uint32_t serial;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400203 struct weston_keyboard *keyboard = grab->keyboard;
Rob Bradford880ebc72013-07-22 17:31:38 +0100204 struct wl_display *display = keyboard->seat->compositor->wl_display;
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200205 uint32_t msecs;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200206
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200207 if (key == b->key) {
208 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400209 weston_keyboard_end_grab(grab->keyboard);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200210 if (keyboard->input_method_resource)
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400211 keyboard->grab = &keyboard->input_method_grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200212 free(b);
Giulio Camuffo943b46e2014-12-05 18:02:58 +0200213 } else {
214 /* Don't send the key press event for the binding key */
215 return;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200216 }
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200217 }
218 if (!wl_list_empty(&keyboard->focus_resource_list)) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200219 serial = wl_display_next_serial(display);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200220 msecs = timespec_to_msec(time);
Neil Roberts96d790e2013-09-19 17:32:00 +0100221 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
222 wl_keyboard_send_key(resource,
223 serial,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200224 msecs,
Neil Roberts96d790e2013-09-19 17:32:00 +0100225 key,
226 state);
227 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200228 }
229}
230
231static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400232binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200233 uint32_t mods_depressed, uint32_t mods_latched,
234 uint32_t mods_locked, uint32_t group)
235{
236 struct wl_resource *resource;
237
Neil Roberts96d790e2013-09-19 17:32:00 +0100238 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 Oliveiracbdebc22013-02-21 18:35:16 +0200242}
243
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200244static void
245binding_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øgsberg29139d42013-04-18 15:25:39 -0400254static const struct weston_keyboard_grab_interface binding_grab = {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200255 binding_key,
256 binding_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200257 binding_cancel,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200258};
259
260static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200261install_binding_grab(struct weston_keyboard *keyboard,
262 const struct timespec *time, uint32_t key,
263 struct weston_surface *focus)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200264{
265 struct binding_keyboard_grab *grab;
266
267 grab = malloc(sizeof *grab);
268 grab->key = key;
269 grab->grab.interface = &binding_grab;
Derek Foremanb591a302015-07-15 13:00:42 -0500270 weston_keyboard_start_grab(keyboard, &grab->grab);
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200271
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 Foremanb591a302015-07-15 13:00:42 -0500280 if (focus && keyboard->focus == focus) {
281 weston_keyboard_set_focus(keyboard, NULL);
282 weston_keyboard_set_focus(keyboard, focus);
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200283 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200284}
285
Derek Foreman2e6485c2015-07-15 13:00:34 -0500286void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200287weston_compositor_run_key_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500288 struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200289 const struct timespec *time, uint32_t key,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200290 enum wl_keyboard_key_state state)
291{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300292 struct weston_binding *b, *tmp;
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200293 struct weston_surface *focus;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500294 struct weston_seat *seat = keyboard->seat;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200295
296 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Pekka Paalanen86b53962014-11-19 13:43:32 +0200297 return;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200298
Daniel Stone96d47c02013-11-19 11:37:12 +0100299 /* Invalidate all active modifier bindings. */
300 wl_list_for_each(b, &compositor->modifier_binding_list, link)
301 b->key = key;
302
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300303 wl_list_for_each_safe(b, tmp, &compositor->key_binding_list, link) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200304 if (b->key == key && b->modifier == seat->modifier_state) {
305 weston_key_binding_handler_t handler = b->handler;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500306 focus = keyboard->focus;
307 handler(keyboard, time, key, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200308
309 /* If this was a key binding and it didn't
310 * install a keyboard grab, install one now to
Giulio Camuffo943b46e2014-12-05 18:02:58 +0200311 * swallow the key press. */
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500312 if (keyboard->grab ==
313 &keyboard->default_grab)
314 install_binding_grab(keyboard,
Derek Foremanb591a302015-07-15 13:00:42 -0500315 time,
316 key,
317 focus);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200318 }
319 }
320}
321
Derek Foreman2e6485c2015-07-15 13:00:34 -0500322void
Daniel Stone96d47c02013-11-19 11:37:12 +0100323weston_compositor_run_modifier_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500324 struct weston_keyboard *keyboard,
Daniel Stone96d47c02013-11-19 11:37:12 +0100325 enum weston_keyboard_modifier modifier,
326 enum wl_keyboard_key_state state)
327{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300328 struct weston_binding *b, *tmp;
Daniel Stone96d47c02013-11-19 11:37:12 +0100329
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500330 if (keyboard->grab != &keyboard->default_grab)
Emilio Pozuelo Monfort1539ea22013-11-27 10:34:32 +0100331 return;
332
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300333 wl_list_for_each_safe(b, tmp, &compositor->modifier_binding_list, link) {
Daniel Stone96d47c02013-11-19 11:37:12 +0100334 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 Foreman99a6a2d2015-07-15 13:00:43 -0500349 handler(keyboard, modifier, b->data);
Daniel Stone96d47c02013-11-19 11:37:12 +0100350 }
351}
352
Derek Foreman2e6485c2015-07-15 13:00:34 -0500353void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200354weston_compositor_run_button_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500355 struct weston_pointer *pointer,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200356 const struct timespec *time,
357 uint32_t button,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200358 enum wl_pointer_button_state state)
359{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300360 struct weston_binding *b, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200361
362 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
363 return;
364
Daniel Stone96d47c02013-11-19 11:37:12 +0100365 /* Invalidate all active modifier bindings. */
366 wl_list_for_each(b, &compositor->modifier_binding_list, link)
367 b->key = button;
368
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300369 wl_list_for_each_safe(b, tmp, &compositor->button_binding_list, link) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500370 if (b->button == button &&
371 b->modifier == pointer->seat->modifier_state) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200372 weston_button_binding_handler_t handler = b->handler;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500373 handler(pointer, time, button, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200374 }
375 }
376}
377
Derek Foreman2e6485c2015-07-15 13:00:34 -0500378void
Neil Robertsa28c6932013-10-03 16:43:04 +0100379weston_compositor_run_touch_binding(struct weston_compositor *compositor,
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200380 struct weston_touch *touch,
381 const struct timespec *time,
Neil Robertsa28c6932013-10-03 16:43:04 +0100382 int touch_type)
383{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300384 struct weston_binding *b, *tmp;
Neil Robertsa28c6932013-10-03 16:43:04 +0100385
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500386 if (touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN)
Neil Robertsa28c6932013-10-03 16:43:04 +0100387 return;
388
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300389 wl_list_for_each_safe(b, tmp, &compositor->touch_binding_list, link) {
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500390 if (b->modifier == touch->seat->modifier_state) {
Neil Robertsa28c6932013-10-03 16:43:04 +0100391 weston_touch_binding_handler_t handler = b->handler;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500392 handler(touch, time, b->data);
Neil Robertsa28c6932013-10-03 16:43:04 +0100393 }
394 }
395}
396
Derek Foreman2e6485c2015-07-15 13:00:34 -0500397int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200398weston_compositor_run_axis_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500399 struct weston_pointer *pointer,
Alexandros Frantzis80321942017-11-16 18:20:56 +0200400 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000401 struct weston_pointer_axis_event *event)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200402{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300403 struct weston_binding *b, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200404
Daniel Stone96d47c02013-11-19 11:37:12 +0100405 /* Invalidate all active modifier bindings. */
406 wl_list_for_each(b, &compositor->modifier_binding_list, link)
Peter Hutterer89b6a492016-01-18 15:58:17 +1000407 b->key = event->axis;
Daniel Stone96d47c02013-11-19 11:37:12 +0100408
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300409 wl_list_for_each_safe(b, tmp, &compositor->axis_binding_list, link) {
Peter Hutterer89b6a492016-01-18 15:58:17 +1000410 if (b->axis == event->axis &&
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500411 b->modifier == pointer->seat->modifier_state) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200412 weston_axis_binding_handler_t handler = b->handler;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000413 handler(pointer, time, event, b->data);
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100414 return 1;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200415 }
416 }
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100417
418 return 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200419}
420
Derek Foreman2e6485c2015-07-15 13:00:34 -0500421int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200422weston_compositor_run_debug_binding(struct weston_compositor *compositor,
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500423 struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200424 const struct timespec *time, uint32_t key,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200425 enum wl_keyboard_key_state state)
426{
427 weston_key_binding_handler_t handler;
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300428 struct weston_binding *binding, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200429 int count = 0;
430
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300431 wl_list_for_each_safe(binding, tmp, &compositor->debug_binding_list, link) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200432 if (key != binding->key)
433 continue;
434
435 count++;
436 handler = binding->handler;
Derek Foreman99a6a2d2015-07-15 13:00:43 -0500437 handler(keyboard, time, key, binding->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200438 }
439
440 return count;
441}
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200442
443struct 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
450static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200451debug_binding_key(struct weston_keyboard_grab *grab, const struct timespec *time,
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200452 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 Frantzis47e79c82017-11-16 18:20:57 +0200463 uint32_t msecs;
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200464
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 Foreman99a6a2d2015-07-15 13:00:43 -0500494 if (weston_compositor_run_debug_binding(ec, grab->keyboard,
495 time, key, state)) {
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200496 /* 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 Frantzis47e79c82017-11-16 18:20:57 +0200512 msecs = timespec_to_msec(time);
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200513 wl_resource_for_each(resource, resource_list) {
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200514 wl_keyboard_send_key(resource, serial, msecs, key, state);
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200515 }
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
526static void
527debug_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
542static void
543debug_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
551struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
552 debug_binding_key,
553 debug_binding_modifiers,
554 debug_binding_cancel,
555};
556
557static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +0200558debug_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -0500559 uint32_t key, void *data)
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200560{
561 struct debug_binding_grab *grab;
562
563 grab = calloc(1, sizeof *grab);
564 if (!grab)
565 return;
566
Derek Foreman8ae2db52015-07-15 13:00:36 -0500567 grab->seat = keyboard->seat;
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200568 grab->key[0] = key;
569 grab->grab.interface = &debug_binding_keyboard_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -0500570 weston_keyboard_start_grab(keyboard, &grab->grab);
Pekka Paalanen2829f7c2015-02-19 17:02:13 +0200571}
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 */
581WL_EXPORT void
582weston_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}