1 /*
2 * Copyright © 2011-2012 Intel Corporation
3 *
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:
11 *
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.
24 */
25
26 #include "config.h"
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <linux/input.h>
31
32 #include <libweston/libweston.h>
33 #include "libweston-internal.h"
34 #include "shared/helpers.h"
35 #include "shared/timespec-util.h"
36
37 struct 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
47 static struct weston_binding *
weston_compositor_add_binding(struct weston_compositor * compositor,uint32_t key,uint32_t button,uint32_t axis,uint32_t modifier,void * handler,void * data)48 weston_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
68 WL_EXPORT struct weston_binding *
weston_compositor_add_key_binding(struct weston_compositor * compositor,uint32_t key,uint32_t modifier,weston_key_binding_handler_t handler,void * data)69 weston_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
86 WL_EXPORT struct weston_binding *
weston_compositor_add_modifier_binding(struct weston_compositor * compositor,uint32_t modifier,weston_modifier_binding_handler_t handler,void * data)87 weston_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
104 WL_EXPORT struct weston_binding *
weston_compositor_add_button_binding(struct weston_compositor * compositor,uint32_t button,uint32_t modifier,weston_button_binding_handler_t handler,void * data)105 weston_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
122 WL_EXPORT struct weston_binding *
weston_compositor_add_touch_binding(struct weston_compositor * compositor,uint32_t modifier,weston_touch_binding_handler_t handler,void * data)123 weston_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
140 WL_EXPORT struct weston_binding *
weston_compositor_add_axis_binding(struct weston_compositor * compositor,uint32_t axis,uint32_t modifier,weston_axis_binding_handler_t handler,void * data)141 weston_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
158 // OHOS remove debugger
159 //WL_EXPORT struct weston_binding *
160 //weston_compositor_add_debug_binding(struct weston_compositor *compositor,
161 // uint32_t key,
162 // weston_key_binding_handler_t handler,
163 // void *data)
164 //{
165 // struct weston_binding *binding;
166 //
167 // binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
168 // handler, data);
169 //
170 // wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
171 //
172 // return binding;
173 //}
174
175 WL_EXPORT void
weston_binding_destroy(struct weston_binding * binding)176 weston_binding_destroy(struct weston_binding *binding)
177 {
178 wl_list_remove(&binding->link);
179 free(binding);
180 }
181
182 void
weston_binding_list_destroy_all(struct wl_list * list)183 weston_binding_list_destroy_all(struct wl_list *list)
184 {
185 struct weston_binding *binding, *tmp;
186
187 wl_list_for_each_safe(binding, tmp, list, link)
188 weston_binding_destroy(binding);
189 }
190
191 struct binding_keyboard_grab {
192 uint32_t key;
193 struct weston_keyboard_grab grab;
194 };
195
196 static void
binding_key(struct weston_keyboard_grab * grab,const struct timespec * time,uint32_t key,uint32_t state_w)197 binding_key(struct weston_keyboard_grab *grab,
198 const struct timespec *time, uint32_t key, uint32_t state_w)
199 {
200 struct binding_keyboard_grab *b =
201 container_of(grab, struct binding_keyboard_grab, grab);
202 struct wl_resource *resource;
203 enum wl_keyboard_key_state state = state_w;
204 uint32_t serial;
205 struct weston_keyboard *keyboard = grab->keyboard;
206 struct wl_display *display = keyboard->seat->compositor->wl_display;
207 uint32_t msecs;
208
209 if (key == b->key) {
210 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
211 weston_keyboard_end_grab(grab->keyboard);
212 if (keyboard->input_method_resource)
213 keyboard->grab = &keyboard->input_method_grab;
214 free(b);
215 } else {
216 /* Don't send the key press event for the binding key */
217 return;
218 }
219 }
220 if (!wl_list_empty(&keyboard->focus_resource_list)) {
221 serial = wl_display_next_serial(display);
222 msecs = timespec_to_msec(time);
223 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
224 wl_keyboard_send_key(resource,
225 serial,
226 msecs,
227 key,
228 state);
229 }
230 }
231 }
232
233 static void
binding_modifiers(struct weston_keyboard_grab * grab,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)234 binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
235 uint32_t mods_depressed, uint32_t mods_latched,
236 uint32_t mods_locked, uint32_t group)
237 {
238 struct wl_resource *resource;
239
240 wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) {
241 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
242 mods_latched, mods_locked, group);
243 }
244 }
245
246 static void
binding_cancel(struct weston_keyboard_grab * grab)247 binding_cancel(struct weston_keyboard_grab *grab)
248 {
249 struct binding_keyboard_grab *binding_grab =
250 container_of(grab, struct binding_keyboard_grab, grab);
251
252 weston_keyboard_end_grab(grab->keyboard);
253 free(binding_grab);
254 }
255
256 static const struct weston_keyboard_grab_interface binding_grab = {
257 binding_key,
258 binding_modifiers,
259 binding_cancel,
260 };
261
262 static void
install_binding_grab(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,struct weston_surface * focus)263 install_binding_grab(struct weston_keyboard *keyboard,
264 const struct timespec *time, uint32_t key,
265 struct weston_surface *focus)
266 {
267 struct binding_keyboard_grab *grab;
268
269 grab = malloc(sizeof *grab);
270 grab->key = key;
271 grab->grab.interface = &binding_grab;
272 weston_keyboard_start_grab(keyboard, &grab->grab);
273
274 /* Notify the surface which had the focus before this binding
275 * triggered that we stole a keypress from under it, by forcing
276 * a wl_keyboard leave/enter pair. The enter event will contain
277 * the pressed key in the keys array, so the client will know
278 * the exact state of the keyboard.
279 * If the old focus surface is different than the new one it
280 * means it was changed in the binding handler, so it received
281 * the enter event already. */
282 if (focus && keyboard->focus == focus) {
283 weston_keyboard_set_focus(keyboard, NULL);
284 weston_keyboard_set_focus(keyboard, focus);
285 }
286 }
287
288 void
weston_compositor_run_key_binding(struct weston_compositor * compositor,struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,enum wl_keyboard_key_state state)289 weston_compositor_run_key_binding(struct weston_compositor *compositor,
290 struct weston_keyboard *keyboard,
291 const struct timespec *time, uint32_t key,
292 enum wl_keyboard_key_state state)
293 {
294 struct weston_binding *b, *tmp;
295 struct weston_surface *focus;
296 struct weston_seat *seat = keyboard->seat;
297
298 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
299 return;
300
301 /* Invalidate all active modifier bindings. */
302 wl_list_for_each(b, &compositor->modifier_binding_list, link)
303 b->key = key;
304
305 wl_list_for_each_safe(b, tmp, &compositor->key_binding_list, link) {
306 if (b->key == key && b->modifier == seat->modifier_state) {
307 weston_key_binding_handler_t handler = b->handler;
308 focus = keyboard->focus;
309 handler(keyboard, time, key, b->data);
310
311 /* If this was a key binding and it didn't
312 * install a keyboard grab, install one now to
313 * swallow the key press. */
314 if (keyboard->grab ==
315 &keyboard->default_grab)
316 install_binding_grab(keyboard,
317 time,
318 key,
319 focus);
320 }
321 }
322 }
323
324 void
weston_compositor_run_modifier_binding(struct weston_compositor * compositor,struct weston_keyboard * keyboard,enum weston_keyboard_modifier modifier,enum wl_keyboard_key_state state)325 weston_compositor_run_modifier_binding(struct weston_compositor *compositor,
326 struct weston_keyboard *keyboard,
327 enum weston_keyboard_modifier modifier,
328 enum wl_keyboard_key_state state)
329 {
330 struct weston_binding *b, *tmp;
331
332 if (keyboard->grab != &keyboard->default_grab)
333 return;
334
335 wl_list_for_each_safe(b, tmp, &compositor->modifier_binding_list, link) {
336 weston_modifier_binding_handler_t handler = b->handler;
337
338 if (b->modifier != modifier)
339 continue;
340
341 /* Prime the modifier binding. */
342 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
343 b->key = 0;
344 continue;
345 }
346 /* Ignore the binding if a key was pressed in between. */
347 else if (b->key != 0) {
348 return;
349 }
350
351 handler(keyboard, modifier, b->data);
352 }
353 }
354
355 void
weston_compositor_run_button_binding(struct weston_compositor * compositor,struct weston_pointer * pointer,const struct timespec * time,uint32_t button,enum wl_pointer_button_state state)356 weston_compositor_run_button_binding(struct weston_compositor *compositor,
357 struct weston_pointer *pointer,
358 const struct timespec *time,
359 uint32_t button,
360 enum wl_pointer_button_state state)
361 {
362 struct weston_binding *b, *tmp;
363
364 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
365 return;
366
367 /* Invalidate all active modifier bindings. */
368 wl_list_for_each(b, &compositor->modifier_binding_list, link)
369 b->key = button;
370
371 wl_list_for_each_safe(b, tmp, &compositor->button_binding_list, link) {
372 if (b->button == button &&
373 b->modifier == pointer->seat->modifier_state) {
374 weston_button_binding_handler_t handler = b->handler;
375 handler(pointer, time, button, b->data);
376 }
377 }
378 }
379
380 void
weston_compositor_run_touch_binding(struct weston_compositor * compositor,struct weston_touch * touch,const struct timespec * time,int touch_type)381 weston_compositor_run_touch_binding(struct weston_compositor *compositor,
382 struct weston_touch *touch,
383 const struct timespec *time,
384 int touch_type)
385 {
386 struct weston_binding *b, *tmp;
387
388 if (touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN)
389 return;
390
391 wl_list_for_each_safe(b, tmp, &compositor->touch_binding_list, link) {
392 if (b->modifier == touch->seat->modifier_state) {
393 weston_touch_binding_handler_t handler = b->handler;
394 handler(touch, time, b->data);
395 }
396 }
397 }
398
399 int
weston_compositor_run_axis_binding(struct weston_compositor * compositor,struct weston_pointer * pointer,const struct timespec * time,struct weston_pointer_axis_event * event)400 weston_compositor_run_axis_binding(struct weston_compositor *compositor,
401 struct weston_pointer *pointer,
402 const struct timespec *time,
403 struct weston_pointer_axis_event *event)
404 {
405 struct weston_binding *b, *tmp;
406
407 /* Invalidate all active modifier bindings. */
408 wl_list_for_each(b, &compositor->modifier_binding_list, link)
409 b->key = event->axis;
410
411 wl_list_for_each_safe(b, tmp, &compositor->axis_binding_list, link) {
412 if (b->axis == event->axis &&
413 b->modifier == pointer->seat->modifier_state) {
414 weston_axis_binding_handler_t handler = b->handler;
415 handler(pointer, time, event, b->data);
416 return 1;
417 }
418 }
419
420 return 0;
421 }
422
423 int
weston_compositor_run_debug_binding(struct weston_compositor * compositor,struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,enum wl_keyboard_key_state state)424 weston_compositor_run_debug_binding(struct weston_compositor *compositor,
425 struct weston_keyboard *keyboard,
426 const struct timespec *time, uint32_t key,
427 enum wl_keyboard_key_state state)
428 {
429 weston_key_binding_handler_t handler;
430 struct weston_binding *binding, *tmp;
431 int count = 0;
432
433 wl_list_for_each_safe(binding, tmp, &compositor->debug_binding_list, link) {
434 if (key != binding->key)
435 continue;
436
437 count++;
438 handler = binding->handler;
439 handler(keyboard, time, key, binding->data);
440 }
441
442 return count;
443 }
444
445 struct debug_binding_grab {
446 struct weston_keyboard_grab grab;
447 struct weston_seat *seat;
448 uint32_t key[2];
449 int key_released[2];
450 };
451
452 static void
debug_binding_key(struct weston_keyboard_grab * grab,const struct timespec * time,uint32_t key,uint32_t state)453 debug_binding_key(struct weston_keyboard_grab *grab, const struct timespec *time,
454 uint32_t key, uint32_t state)
455 {
456 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
457 struct weston_compositor *ec = db->seat->compositor;
458 struct wl_display *display = ec->wl_display;
459 struct wl_resource *resource;
460 uint32_t serial;
461 int send = 0, terminate = 0;
462 int check_binding = 1;
463 int i;
464 struct wl_list *resource_list;
465 uint32_t msecs;
466
467 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
468 /* Do not run bindings on key releases */
469 check_binding = 0;
470
471 for (i = 0; i < 2; i++)
472 if (key == db->key[i])
473 db->key_released[i] = 1;
474
475 if (db->key_released[0] && db->key_released[1]) {
476 /* All key releases been swalled so end the grab */
477 terminate = 1;
478 } else if (key != db->key[0] && key != db->key[1]) {
479 /* Should not swallow release of other keys */
480 send = 1;
481 }
482 } else if (key == db->key[0] && !db->key_released[0]) {
483 /* Do not check bindings for the first press of the binding
484 * key. This allows it to be used as a debug shortcut.
485 * We still need to swallow this event. */
486 check_binding = 0;
487 } else if (db->key[1]) {
488 /* If we already ran a binding don't process another one since
489 * we can't keep track of all the binding keys that were
490 * pressed in order to swallow the release events. */
491 send = 1;
492 check_binding = 0;
493 }
494
495 if (check_binding) {
496 if (weston_compositor_run_debug_binding(ec, grab->keyboard,
497 time, key, state)) {
498 /* We ran a binding so swallow the press and keep the
499 * grab to swallow the released too. */
500 send = 0;
501 terminate = 0;
502 db->key[1] = key;
503 } else {
504 /* Terminate the grab since the key pressed is not a
505 * debug binding key. */
506 send = 1;
507 terminate = 1;
508 }
509 }
510
511 if (send) {
512 serial = wl_display_next_serial(display);
513 resource_list = &grab->keyboard->focus_resource_list;
514 msecs = timespec_to_msec(time);
515 wl_resource_for_each(resource, resource_list) {
516 wl_keyboard_send_key(resource, serial, msecs, key, state);
517 }
518 }
519
520 if (terminate) {
521 weston_keyboard_end_grab(grab->keyboard);
522 if (grab->keyboard->input_method_resource)
523 grab->keyboard->grab = &grab->keyboard->input_method_grab;
524 free(db);
525 }
526 }
527
528 static void
debug_binding_modifiers(struct weston_keyboard_grab * grab,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)529 debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
530 uint32_t mods_depressed, uint32_t mods_latched,
531 uint32_t mods_locked, uint32_t group)
532 {
533 struct wl_resource *resource;
534 struct wl_list *resource_list;
535
536 resource_list = &grab->keyboard->focus_resource_list;
537
538 wl_resource_for_each(resource, resource_list) {
539 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
540 mods_latched, mods_locked, group);
541 }
542 }
543
544 static void
debug_binding_cancel(struct weston_keyboard_grab * grab)545 debug_binding_cancel(struct weston_keyboard_grab *grab)
546 {
547 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
548
549 weston_keyboard_end_grab(grab->keyboard);
550 free(db);
551 }
552
553 struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
554 debug_binding_key,
555 debug_binding_modifiers,
556 debug_binding_cancel,
557 };
558
559 static void
debug_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)560 debug_binding(struct weston_keyboard *keyboard, const struct timespec *time,
561 uint32_t key, void *data)
562 {
563 struct debug_binding_grab *grab;
564
565 grab = calloc(1, sizeof *grab);
566 if (!grab)
567 return;
568
569 grab->seat = keyboard->seat;
570 grab->key[0] = key;
571 grab->grab.interface = &debug_binding_keyboard_grab;
572 weston_keyboard_start_grab(keyboard, &grab->grab);
573 }
574
575 /** Install the trigger binding for debug bindings.
576 *
577 * \param compositor The compositor.
578 * \param mod The modifier.
579 *
580 * This will add a key binding for modifier+SHIFT+SPACE that will trigger
581 * debug key bindings.
582 */
583 WL_EXPORT void
weston_install_debug_key_binding(struct weston_compositor * compositor,uint32_t mod)584 weston_install_debug_key_binding(struct weston_compositor *compositor,
585 uint32_t mod)
586 {
587 weston_compositor_add_key_binding(compositor, KEY_SPACE,
588 mod | MODIFIER_SHIFT,
589 debug_binding, NULL);
590 }
591