2 * Copyright © 2011 Intel Corporation
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.
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.
27 #include "compositor.h"
29 struct weston_binding {
39 static struct weston_binding *
40 weston_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)
44 struct weston_binding *binding;
46 binding = malloc(sizeof *binding);
51 binding->button = button;
53 binding->modifier = modifier;
54 binding->handler = handler;
60 WL_EXPORT struct weston_binding *
61 weston_compositor_add_key_binding(struct weston_compositor *compositor,
62 uint32_t key, uint32_t modifier,
63 weston_key_binding_handler_t handler,
66 struct weston_binding *binding;
68 binding = weston_compositor_add_binding(compositor, key, 0, 0,
69 modifier, handler, data);
73 wl_list_insert(compositor->key_binding_list.prev, &binding->link);
78 WL_EXPORT struct weston_binding *
79 weston_compositor_add_modifier_binding(struct weston_compositor *compositor,
81 weston_modifier_binding_handler_t handler,
84 struct weston_binding *binding;
86 binding = weston_compositor_add_binding(compositor, 0, 0, 0,
87 modifier, handler, data);
91 wl_list_insert(compositor->modifier_binding_list.prev, &binding->link);
96 WL_EXPORT struct weston_binding *
97 weston_compositor_add_button_binding(struct weston_compositor *compositor,
98 uint32_t button, uint32_t modifier,
99 weston_button_binding_handler_t handler,
102 struct weston_binding *binding;
104 binding = weston_compositor_add_binding(compositor, 0, button, 0,
105 modifier, handler, data);
109 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
114 WL_EXPORT struct weston_binding *
115 weston_compositor_add_touch_binding(struct weston_compositor *compositor,
117 weston_touch_binding_handler_t handler,
120 struct weston_binding *binding;
122 binding = weston_compositor_add_binding(compositor, 0, 0, 0,
123 modifier, handler, data);
127 wl_list_insert(compositor->touch_binding_list.prev, &binding->link);
132 WL_EXPORT struct weston_binding *
133 weston_compositor_add_axis_binding(struct weston_compositor *compositor,
134 uint32_t axis, uint32_t modifier,
135 weston_axis_binding_handler_t handler,
138 struct weston_binding *binding;
140 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
141 modifier, handler, data);
145 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
150 WL_EXPORT struct weston_binding *
151 weston_compositor_add_debug_binding(struct weston_compositor *compositor,
153 weston_key_binding_handler_t handler,
156 struct weston_binding *binding;
158 binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
161 wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
167 weston_binding_destroy(struct weston_binding *binding)
169 wl_list_remove(&binding->link);
174 weston_binding_list_destroy_all(struct wl_list *list)
176 struct weston_binding *binding, *tmp;
178 wl_list_for_each_safe(binding, tmp, list, link)
179 weston_binding_destroy(binding);
182 struct binding_keyboard_grab {
184 struct weston_keyboard_grab grab;
188 binding_key(struct weston_keyboard_grab *grab,
189 uint32_t time, uint32_t key, uint32_t state_w)
191 struct binding_keyboard_grab *b =
192 container_of(grab, struct binding_keyboard_grab, grab);
193 struct wl_resource *resource;
194 enum wl_keyboard_key_state state = state_w;
196 struct weston_keyboard *keyboard = grab->keyboard;
197 struct wl_display *display = keyboard->seat->compositor->wl_display;
200 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
201 weston_keyboard_end_grab(grab->keyboard);
202 if (keyboard->input_method_resource)
203 keyboard->grab = &keyboard->input_method_grab;
206 } else if (!wl_list_empty(&keyboard->focus_resource_list)) {
207 serial = wl_display_next_serial(display);
208 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
209 wl_keyboard_send_key(resource,
219 binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
220 uint32_t mods_depressed, uint32_t mods_latched,
221 uint32_t mods_locked, uint32_t group)
223 struct wl_resource *resource;
225 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);
232 binding_cancel(struct weston_keyboard_grab *grab)
234 struct binding_keyboard_grab *binding_grab =
235 container_of(grab, struct binding_keyboard_grab, grab);
237 weston_keyboard_end_grab(grab->keyboard);
241 static const struct weston_keyboard_grab_interface binding_grab = {
248 install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key)
250 struct binding_keyboard_grab *grab;
252 grab = malloc(sizeof *grab);
254 grab->grab.interface = &binding_grab;
255 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
259 weston_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)
264 struct weston_binding *b;
266 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
269 /* Invalidate all active modifier bindings. */
270 wl_list_for_each(b, &compositor->modifier_binding_list, link)
273 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;
276 handler(seat, time, key, b->data);
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. */
281 if (seat->keyboard->grab ==
282 &seat->keyboard->default_grab)
283 install_binding_grab(seat, time, key);
289 weston_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)
294 struct weston_binding *b;
296 if (seat->keyboard->grab != &seat->keyboard->default_grab)
299 wl_list_for_each(b, &compositor->modifier_binding_list, link) {
300 weston_modifier_binding_handler_t handler = b->handler;
302 if (b->modifier != modifier)
305 /* Prime the modifier binding. */
306 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
310 /* Ignore the binding if a key was pressed in between. */
311 else if (b->key != 0) {
315 handler(seat, modifier, b->data);
320 weston_compositor_run_button_binding(struct weston_compositor *compositor,
321 struct weston_seat *seat,
322 uint32_t time, uint32_t button,
323 enum wl_pointer_button_state state)
325 struct weston_binding *b;
327 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
330 /* Invalidate all active modifier bindings. */
331 wl_list_for_each(b, &compositor->modifier_binding_list, link)
334 wl_list_for_each(b, &compositor->button_binding_list, link) {
335 if (b->button == button && b->modifier == seat->modifier_state) {
336 weston_button_binding_handler_t handler = b->handler;
337 handler(seat, time, button, b->data);
343 weston_compositor_run_touch_binding(struct weston_compositor *compositor,
344 struct weston_seat *seat, uint32_t time,
347 struct weston_binding *b;
349 if (seat->touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN)
352 wl_list_for_each(b, &compositor->touch_binding_list, link) {
353 if (b->modifier == seat->modifier_state) {
354 weston_touch_binding_handler_t handler = b->handler;
355 handler(seat, time, b->data);
361 weston_compositor_run_axis_binding(struct weston_compositor *compositor,
362 struct weston_seat *seat,
363 uint32_t time, uint32_t axis,
366 struct weston_binding *b;
368 /* Invalidate all active modifier bindings. */
369 wl_list_for_each(b, &compositor->modifier_binding_list, link)
372 wl_list_for_each(b, &compositor->axis_binding_list, link) {
373 if (b->axis == axis && b->modifier == seat->modifier_state) {
374 weston_axis_binding_handler_t handler = b->handler;
375 handler(seat, time, axis, value, b->data);
384 weston_compositor_run_debug_binding(struct weston_compositor *compositor,
385 struct weston_seat *seat,
386 uint32_t time, uint32_t key,
387 enum wl_keyboard_key_state state)
389 weston_key_binding_handler_t handler;
390 struct weston_binding *binding;
393 wl_list_for_each(binding, &compositor->debug_binding_list, link) {
394 if (key != binding->key)
398 handler = binding->handler;
399 handler(seat, time, key, binding->data);