blob: 8d4c1333ed113a01675f7fea83f9ce93c5ba63c5 [file] [log] [blame]
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020025#include <stdlib.h>
26
27#include "compositor.h"
28
29struct weston_binding {
30 uint32_t key;
31 uint32_t button;
32 uint32_t axis;
33 uint32_t modifier;
34 void *handler;
35 void *data;
36 struct wl_list link;
37};
38
39static struct weston_binding *
40weston_compositor_add_binding(struct weston_compositor *compositor,
41 uint32_t key, uint32_t button, uint32_t axis,
42 uint32_t modifier, void *handler, void *data)
43{
44 struct weston_binding *binding;
45
46 binding = malloc(sizeof *binding);
47 if (binding == NULL)
48 return NULL;
49
50 binding->key = key;
51 binding->button = button;
52 binding->axis = axis;
53 binding->modifier = modifier;
54 binding->handler = handler;
55 binding->data = data;
56
57 return binding;
58}
59
60WL_EXPORT struct weston_binding *
61weston_compositor_add_key_binding(struct weston_compositor *compositor,
62 uint32_t key, uint32_t modifier,
63 weston_key_binding_handler_t handler,
64 void *data)
65{
66 struct weston_binding *binding;
67
68 binding = weston_compositor_add_binding(compositor, key, 0, 0,
69 modifier, handler, data);
70 if (binding == NULL)
71 return NULL;
72
73 wl_list_insert(compositor->key_binding_list.prev, &binding->link);
74
75 return binding;
76}
77
78WL_EXPORT struct weston_binding *
Daniel Stone96d47c02013-11-19 11:37:12 +010079weston_compositor_add_modifier_binding(struct weston_compositor *compositor,
80 uint32_t modifier,
81 weston_modifier_binding_handler_t handler,
82 void *data)
83{
84 struct weston_binding *binding;
85
86 binding = weston_compositor_add_binding(compositor, 0, 0, 0,
87 modifier, handler, data);
88 if (binding == NULL)
89 return NULL;
90
91 wl_list_insert(compositor->modifier_binding_list.prev, &binding->link);
92
93 return binding;
94}
95
96WL_EXPORT struct weston_binding *
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +020097weston_compositor_add_button_binding(struct weston_compositor *compositor,
98 uint32_t button, uint32_t modifier,
99 weston_button_binding_handler_t handler,
100 void *data)
101{
102 struct weston_binding *binding;
103
104 binding = weston_compositor_add_binding(compositor, 0, button, 0,
105 modifier, handler, data);
106 if (binding == NULL)
107 return NULL;
108
109 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
110
111 return binding;
112}
113
114WL_EXPORT struct weston_binding *
Neil Robertsa28c6932013-10-03 16:43:04 +0100115weston_compositor_add_touch_binding(struct weston_compositor *compositor,
116 uint32_t modifier,
117 weston_touch_binding_handler_t handler,
118 void *data)
119{
120 struct weston_binding *binding;
121
122 binding = weston_compositor_add_binding(compositor, 0, 0, 0,
123 modifier, handler, data);
124 if (binding == NULL)
125 return NULL;
126
127 wl_list_insert(compositor->touch_binding_list.prev, &binding->link);
128
129 return binding;
130}
131
132WL_EXPORT struct weston_binding *
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200133weston_compositor_add_axis_binding(struct weston_compositor *compositor,
134 uint32_t axis, uint32_t modifier,
135 weston_axis_binding_handler_t handler,
136 void *data)
137{
138 struct weston_binding *binding;
139
140 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
141 modifier, handler, data);
142 if (binding == NULL)
143 return NULL;
144
145 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
146
147 return binding;
148}
149
150WL_EXPORT struct weston_binding *
151weston_compositor_add_debug_binding(struct weston_compositor *compositor,
152 uint32_t key,
153 weston_key_binding_handler_t handler,
154 void *data)
155{
156 struct weston_binding *binding;
157
158 binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
159 handler, data);
160
161 wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
162
163 return binding;
164}
165
166WL_EXPORT void
167weston_binding_destroy(struct weston_binding *binding)
168{
169 wl_list_remove(&binding->link);
170 free(binding);
171}
172
173WL_EXPORT void
174weston_binding_list_destroy_all(struct wl_list *list)
175{
176 struct weston_binding *binding, *tmp;
177
178 wl_list_for_each_safe(binding, tmp, list, link)
179 weston_binding_destroy(binding);
180}
181
182struct binding_keyboard_grab {
183 uint32_t key;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400184 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200185};
186
187static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400188binding_key(struct weston_keyboard_grab *grab,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200189 uint32_t time, uint32_t key, uint32_t state_w)
190{
191 struct binding_keyboard_grab *b =
192 container_of(grab, struct binding_keyboard_grab, grab);
193 struct wl_resource *resource;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200194 enum wl_keyboard_key_state state = state_w;
195 uint32_t serial;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400196 struct weston_keyboard *keyboard = grab->keyboard;
Rob Bradford880ebc72013-07-22 17:31:38 +0100197 struct wl_display *display = keyboard->seat->compositor->wl_display;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200198
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200199 if (key == b->key) {
200 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400201 weston_keyboard_end_grab(grab->keyboard);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200202 if (keyboard->input_method_resource)
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400203 keyboard->grab = &keyboard->input_method_grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200204 free(b);
Giulio Camuffo943b46e2014-12-05 18:02:58 +0200205 } else {
206 /* Don't send the key press event for the binding key */
207 return;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200208 }
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200209 }
210 if (!wl_list_empty(&keyboard->focus_resource_list)) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200211 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100212 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
213 wl_keyboard_send_key(resource,
214 serial,
215 time,
216 key,
217 state);
218 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200219 }
220}
221
222static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400223binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200224 uint32_t mods_depressed, uint32_t mods_latched,
225 uint32_t mods_locked, uint32_t group)
226{
227 struct wl_resource *resource;
228
Neil Roberts96d790e2013-09-19 17:32:00 +0100229 wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) {
230 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
231 mods_latched, mods_locked, group);
232 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200233}
234
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200235static void
236binding_cancel(struct weston_keyboard_grab *grab)
237{
238 struct binding_keyboard_grab *binding_grab =
239 container_of(grab, struct binding_keyboard_grab, grab);
240
241 weston_keyboard_end_grab(grab->keyboard);
242 free(binding_grab);
243}
244
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400245static const struct weston_keyboard_grab_interface binding_grab = {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200246 binding_key,
247 binding_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200248 binding_cancel,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200249};
250
251static void
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200252install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key,
253 struct weston_surface *focus)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200254{
255 struct binding_keyboard_grab *grab;
256
257 grab = malloc(sizeof *grab);
258 grab->key = key;
259 grab->grab.interface = &binding_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400260 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200261
262 /* Notify the surface which had the focus before this binding
263 * triggered that we stole a keypress from under it, by forcing
264 * a wl_keyboard leave/enter pair. The enter event will contain
265 * the pressed key in the keys array, so the client will know
266 * the exact state of the keyboard.
267 * If the old focus surface is different than the new one it
268 * means it was changed in the binding handler, so it received
269 * the enter event already. */
270 if (focus && seat->keyboard->focus == focus) {
271 weston_keyboard_set_focus(seat->keyboard, NULL);
272 weston_keyboard_set_focus(seat->keyboard, focus);
273 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200274}
275
Pekka Paalanen86b53962014-11-19 13:43:32 +0200276WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200277weston_compositor_run_key_binding(struct weston_compositor *compositor,
278 struct weston_seat *seat,
279 uint32_t time, uint32_t key,
280 enum wl_keyboard_key_state state)
281{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300282 struct weston_binding *b, *tmp;
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200283 struct weston_surface *focus;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200284
285 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Pekka Paalanen86b53962014-11-19 13:43:32 +0200286 return;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200287
Daniel Stone96d47c02013-11-19 11:37:12 +0100288 /* Invalidate all active modifier bindings. */
289 wl_list_for_each(b, &compositor->modifier_binding_list, link)
290 b->key = key;
291
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300292 wl_list_for_each_safe(b, tmp, &compositor->key_binding_list, link) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200293 if (b->key == key && b->modifier == seat->modifier_state) {
294 weston_key_binding_handler_t handler = b->handler;
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200295 focus = seat->keyboard->focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400296 handler(seat, time, key, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200297
298 /* If this was a key binding and it didn't
299 * install a keyboard grab, install one now to
Giulio Camuffo943b46e2014-12-05 18:02:58 +0200300 * swallow the key press. */
Kristian Høgsberge3148752013-05-06 23:19:49 -0400301 if (seat->keyboard->grab ==
302 &seat->keyboard->default_grab)
Giulio Camuffoa20ca812014-11-22 11:16:56 +0200303 install_binding_grab(seat, time, key, focus);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200304 }
305 }
306}
307
308WL_EXPORT void
Daniel Stone96d47c02013-11-19 11:37:12 +0100309weston_compositor_run_modifier_binding(struct weston_compositor *compositor,
310 struct weston_seat *seat,
311 enum weston_keyboard_modifier modifier,
312 enum wl_keyboard_key_state state)
313{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300314 struct weston_binding *b, *tmp;
Daniel Stone96d47c02013-11-19 11:37:12 +0100315
Emilio Pozuelo Monfort1539ea22013-11-27 10:34:32 +0100316 if (seat->keyboard->grab != &seat->keyboard->default_grab)
317 return;
318
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300319 wl_list_for_each_safe(b, tmp, &compositor->modifier_binding_list, link) {
Daniel Stone96d47c02013-11-19 11:37:12 +0100320 weston_modifier_binding_handler_t handler = b->handler;
321
322 if (b->modifier != modifier)
323 continue;
324
325 /* Prime the modifier binding. */
326 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
327 b->key = 0;
328 continue;
329 }
330 /* Ignore the binding if a key was pressed in between. */
331 else if (b->key != 0) {
332 return;
333 }
334
335 handler(seat, modifier, b->data);
336 }
337}
338
339WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200340weston_compositor_run_button_binding(struct weston_compositor *compositor,
341 struct weston_seat *seat,
342 uint32_t time, uint32_t button,
343 enum wl_pointer_button_state state)
344{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300345 struct weston_binding *b, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200346
347 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
348 return;
349
Daniel Stone96d47c02013-11-19 11:37:12 +0100350 /* Invalidate all active modifier bindings. */
351 wl_list_for_each(b, &compositor->modifier_binding_list, link)
352 b->key = button;
353
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300354 wl_list_for_each_safe(b, tmp, &compositor->button_binding_list, link) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200355 if (b->button == button && b->modifier == seat->modifier_state) {
356 weston_button_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400357 handler(seat, time, button, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200358 }
359 }
360}
361
Neil Robertsa28c6932013-10-03 16:43:04 +0100362WL_EXPORT void
363weston_compositor_run_touch_binding(struct weston_compositor *compositor,
364 struct weston_seat *seat, uint32_t time,
365 int touch_type)
366{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300367 struct weston_binding *b, *tmp;
Neil Robertsa28c6932013-10-03 16:43:04 +0100368
Jonas Ådahl9484b692013-12-02 22:05:03 +0100369 if (seat->touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN)
Neil Robertsa28c6932013-10-03 16:43:04 +0100370 return;
371
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300372 wl_list_for_each_safe(b, tmp, &compositor->touch_binding_list, link) {
Neil Robertsa28c6932013-10-03 16:43:04 +0100373 if (b->modifier == seat->modifier_state) {
374 weston_touch_binding_handler_t handler = b->handler;
375 handler(seat, time, b->data);
376 }
377 }
378}
379
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100380WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200381weston_compositor_run_axis_binding(struct weston_compositor *compositor,
382 struct weston_seat *seat,
383 uint32_t time, uint32_t axis,
384 wl_fixed_t value)
385{
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300386 struct weston_binding *b, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200387
Daniel Stone96d47c02013-11-19 11:37:12 +0100388 /* Invalidate all active modifier bindings. */
389 wl_list_for_each(b, &compositor->modifier_binding_list, link)
390 b->key = axis;
391
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300392 wl_list_for_each_safe(b, tmp, &compositor->axis_binding_list, link) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200393 if (b->axis == axis && b->modifier == seat->modifier_state) {
394 weston_axis_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400395 handler(seat, time, axis, value, b->data);
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100396 return 1;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200397 }
398 }
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100399
400 return 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200401}
402
403WL_EXPORT int
404weston_compositor_run_debug_binding(struct weston_compositor *compositor,
405 struct weston_seat *seat,
406 uint32_t time, uint32_t key,
407 enum wl_keyboard_key_state state)
408{
409 weston_key_binding_handler_t handler;
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300410 struct weston_binding *binding, *tmp;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200411 int count = 0;
412
Giulio Camuffo24b98d02014-10-03 23:36:34 +0300413 wl_list_for_each_safe(binding, tmp, &compositor->debug_binding_list, link) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200414 if (key != binding->key)
415 continue;
416
417 count++;
418 handler = binding->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400419 handler(seat, time, key, binding->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200420 }
421
422 return count;
423}