blob: fb758d1280927734ca380fcc0e2ff48c72b35c41 [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);
205 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100206 } else if (!wl_list_empty(&keyboard->focus_resource_list)) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200207 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100208 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
209 wl_keyboard_send_key(resource,
210 serial,
211 time,
212 key,
213 state);
214 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200215 }
216}
217
218static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400219binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200220 uint32_t mods_depressed, uint32_t mods_latched,
221 uint32_t mods_locked, uint32_t group)
222{
223 struct wl_resource *resource;
224
Neil Roberts96d790e2013-09-19 17:32:00 +0100225 wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) {
226 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
227 mods_latched, mods_locked, group);
228 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200229}
230
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200231static void
232binding_cancel(struct weston_keyboard_grab *grab)
233{
234 struct binding_keyboard_grab *binding_grab =
235 container_of(grab, struct binding_keyboard_grab, grab);
236
237 weston_keyboard_end_grab(grab->keyboard);
238 free(binding_grab);
239}
240
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400241static const struct weston_keyboard_grab_interface binding_grab = {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200242 binding_key,
243 binding_modifiers,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +0200244 binding_cancel,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200245};
246
247static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400248install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200249{
250 struct binding_keyboard_grab *grab;
251
252 grab = malloc(sizeof *grab);
253 grab->key = key;
254 grab->grab.interface = &binding_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400255 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200256}
257
258WL_EXPORT void
259weston_compositor_run_key_binding(struct weston_compositor *compositor,
260 struct weston_seat *seat,
261 uint32_t time, uint32_t key,
262 enum wl_keyboard_key_state state)
263{
264 struct weston_binding *b;
265
266 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
267 return;
268
Daniel Stone96d47c02013-11-19 11:37:12 +0100269 /* Invalidate all active modifier bindings. */
270 wl_list_for_each(b, &compositor->modifier_binding_list, link)
271 b->key = key;
272
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200273 wl_list_for_each(b, &compositor->key_binding_list, link) {
274 if (b->key == key && b->modifier == seat->modifier_state) {
275 weston_key_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400276 handler(seat, time, key, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200277
278 /* If this was a key binding and it didn't
279 * install a keyboard grab, install one now to
280 * swallow the key release. */
Kristian Høgsberge3148752013-05-06 23:19:49 -0400281 if (seat->keyboard->grab ==
282 &seat->keyboard->default_grab)
283 install_binding_grab(seat, time, key);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200284 }
285 }
286}
287
288WL_EXPORT void
Daniel Stone96d47c02013-11-19 11:37:12 +0100289weston_compositor_run_modifier_binding(struct weston_compositor *compositor,
290 struct weston_seat *seat,
291 enum weston_keyboard_modifier modifier,
292 enum wl_keyboard_key_state state)
293{
294 struct weston_binding *b;
295
296 wl_list_for_each(b, &compositor->modifier_binding_list, link) {
297 weston_modifier_binding_handler_t handler = b->handler;
298
299 if (b->modifier != modifier)
300 continue;
301
302 /* Prime the modifier binding. */
303 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
304 b->key = 0;
305 continue;
306 }
307 /* Ignore the binding if a key was pressed in between. */
308 else if (b->key != 0) {
309 return;
310 }
311
312 handler(seat, modifier, b->data);
313 }
314}
315
316WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200317weston_compositor_run_button_binding(struct weston_compositor *compositor,
318 struct weston_seat *seat,
319 uint32_t time, uint32_t button,
320 enum wl_pointer_button_state state)
321{
322 struct weston_binding *b;
323
324 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
325 return;
326
Daniel Stone96d47c02013-11-19 11:37:12 +0100327 /* Invalidate all active modifier bindings. */
328 wl_list_for_each(b, &compositor->modifier_binding_list, link)
329 b->key = button;
330
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200331 wl_list_for_each(b, &compositor->button_binding_list, link) {
332 if (b->button == button && b->modifier == seat->modifier_state) {
333 weston_button_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400334 handler(seat, time, button, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200335 }
336 }
337}
338
Neil Robertsa28c6932013-10-03 16:43:04 +0100339WL_EXPORT void
340weston_compositor_run_touch_binding(struct weston_compositor *compositor,
341 struct weston_seat *seat, uint32_t time,
342 int touch_type)
343{
344 struct weston_binding *b;
345
346 if (seat->num_tp != 1 || touch_type != WL_TOUCH_DOWN)
347 return;
348
349 wl_list_for_each(b, &compositor->touch_binding_list, link) {
350 if (b->modifier == seat->modifier_state) {
351 weston_touch_binding_handler_t handler = b->handler;
352 handler(seat, time, b->data);
353 }
354 }
355}
356
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100357WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200358weston_compositor_run_axis_binding(struct weston_compositor *compositor,
359 struct weston_seat *seat,
360 uint32_t time, uint32_t axis,
361 wl_fixed_t value)
362{
363 struct weston_binding *b;
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 = axis;
368
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200369 wl_list_for_each(b, &compositor->axis_binding_list, link) {
370 if (b->axis == axis && b->modifier == seat->modifier_state) {
371 weston_axis_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400372 handler(seat, time, axis, value, b->data);
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100373 return 1;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200374 }
375 }
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100376
377 return 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200378}
379
380WL_EXPORT int
381weston_compositor_run_debug_binding(struct weston_compositor *compositor,
382 struct weston_seat *seat,
383 uint32_t time, uint32_t key,
384 enum wl_keyboard_key_state state)
385{
386 weston_key_binding_handler_t handler;
387 struct weston_binding *binding;
388 int count = 0;
389
390 wl_list_for_each(binding, &compositor->debug_binding_list, link) {
391 if (key != binding->key)
392 continue;
393
394 count++;
395 handler = binding->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400396 handler(seat, time, key, binding->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200397 }
398
399 return count;
400}