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.
25 #include "compositor.h"
27 struct weston_binding {
37 static struct weston_binding *
38 weston_compositor_add_binding(struct weston_compositor *compositor,
39 uint32_t key, uint32_t button, uint32_t axis,
40 uint32_t modifier, void *handler, void *data)
42 struct weston_binding *binding;
44 binding = malloc(sizeof *binding);
49 binding->button = button;
51 binding->modifier = modifier;
52 binding->handler = handler;
58 WL_EXPORT struct weston_binding *
59 weston_compositor_add_key_binding(struct weston_compositor *compositor,
60 uint32_t key, uint32_t modifier,
61 weston_key_binding_handler_t handler,
64 struct weston_binding *binding;
66 binding = weston_compositor_add_binding(compositor, key, 0, 0,
67 modifier, handler, data);
71 wl_list_insert(compositor->key_binding_list.prev, &binding->link);
76 WL_EXPORT struct weston_binding *
77 weston_compositor_add_button_binding(struct weston_compositor *compositor,
78 uint32_t button, uint32_t modifier,
79 weston_button_binding_handler_t handler,
82 struct weston_binding *binding;
84 binding = weston_compositor_add_binding(compositor, 0, button, 0,
85 modifier, handler, data);
89 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
94 WL_EXPORT struct weston_binding *
95 weston_compositor_add_axis_binding(struct weston_compositor *compositor,
96 uint32_t axis, uint32_t modifier,
97 weston_axis_binding_handler_t handler,
100 struct weston_binding *binding;
102 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
103 modifier, handler, data);
107 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
112 WL_EXPORT struct weston_binding *
113 weston_compositor_add_debug_binding(struct weston_compositor *compositor,
115 weston_key_binding_handler_t handler,
118 struct weston_binding *binding;
120 binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
123 wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
129 weston_binding_destroy(struct weston_binding *binding)
131 wl_list_remove(&binding->link);
136 weston_binding_list_destroy_all(struct wl_list *list)
138 struct weston_binding *binding, *tmp;
140 wl_list_for_each_safe(binding, tmp, list, link)
141 weston_binding_destroy(binding);
144 struct binding_keyboard_grab {
146 struct wl_keyboard_grab grab;
150 binding_key(struct wl_keyboard_grab *grab,
151 uint32_t time, uint32_t key, uint32_t state_w)
153 struct binding_keyboard_grab *b =
154 container_of(grab, struct binding_keyboard_grab, grab);
155 struct wl_resource *resource;
156 struct wl_display *display;
157 enum wl_keyboard_key_state state = state_w;
159 struct weston_keyboard *keyboard = (struct weston_keyboard *)grab->keyboard;
161 resource = grab->keyboard->focus_resource;
163 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
164 wl_keyboard_end_grab(grab->keyboard);
165 if (keyboard->input_method_resource)
166 keyboard->keyboard.grab = &keyboard->input_method_grab;
169 } else if (resource) {
170 display = wl_client_get_display(resource->client);
171 serial = wl_display_next_serial(display);
172 wl_keyboard_send_key(resource, serial, time, key, state);
177 binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
178 uint32_t mods_depressed, uint32_t mods_latched,
179 uint32_t mods_locked, uint32_t group)
181 struct wl_resource *resource;
183 resource = grab->keyboard->focus_resource;
187 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
188 mods_latched, mods_locked, group);
191 static const struct wl_keyboard_grab_interface binding_grab = {
197 install_binding_grab(struct wl_seat *seat,
198 uint32_t time, uint32_t key)
200 struct binding_keyboard_grab *grab;
202 grab = malloc(sizeof *grab);
204 grab->grab.interface = &binding_grab;
205 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
209 weston_compositor_run_key_binding(struct weston_compositor *compositor,
210 struct weston_seat *seat,
211 uint32_t time, uint32_t key,
212 enum wl_keyboard_key_state state)
214 struct weston_binding *b;
216 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
219 wl_list_for_each(b, &compositor->key_binding_list, link) {
220 if (b->key == key && b->modifier == seat->modifier_state) {
221 weston_key_binding_handler_t handler = b->handler;
222 handler(&seat->seat, time, key, b->data);
224 /* If this was a key binding and it didn't
225 * install a keyboard grab, install one now to
226 * swallow the key release. */
227 if (seat->seat.keyboard->grab ==
228 &seat->seat.keyboard->default_grab)
229 install_binding_grab(&seat->seat, time, key);
235 weston_compositor_run_button_binding(struct weston_compositor *compositor,
236 struct weston_seat *seat,
237 uint32_t time, uint32_t button,
238 enum wl_pointer_button_state state)
240 struct weston_binding *b;
242 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
245 wl_list_for_each(b, &compositor->button_binding_list, link) {
246 if (b->button == button && b->modifier == seat->modifier_state) {
247 weston_button_binding_handler_t handler = b->handler;
248 handler(&seat->seat, time, button, b->data);
254 weston_compositor_run_axis_binding(struct weston_compositor *compositor,
255 struct weston_seat *seat,
256 uint32_t time, uint32_t axis,
259 struct weston_binding *b;
261 wl_list_for_each(b, &compositor->axis_binding_list, link) {
262 if (b->axis == axis && b->modifier == seat->modifier_state) {
263 weston_axis_binding_handler_t handler = b->handler;
264 handler(&seat->seat, time, axis, value, b->data);
273 weston_compositor_run_debug_binding(struct weston_compositor *compositor,
274 struct weston_seat *seat,
275 uint32_t time, uint32_t key,
276 enum wl_keyboard_key_state state)
278 weston_key_binding_handler_t handler;
279 struct weston_binding *binding;
282 wl_list_for_each(binding, &compositor->debug_binding_list, link) {
283 if (key != binding->key)
287 handler = binding->handler;
288 handler(&seat->seat, time, key, binding->data);