blob: 322736d8bfe30a7f652f1dea6bd2a617ba80f61b [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
23#include <stdlib.h>
24
25#include "compositor.h"
26
27struct weston_binding {
28 uint32_t key;
29 uint32_t button;
30 uint32_t axis;
31 uint32_t modifier;
32 void *handler;
33 void *data;
34 struct wl_list link;
35};
36
37static struct weston_binding *
38weston_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)
41{
42 struct weston_binding *binding;
43
44 binding = malloc(sizeof *binding);
45 if (binding == NULL)
46 return NULL;
47
48 binding->key = key;
49 binding->button = button;
50 binding->axis = axis;
51 binding->modifier = modifier;
52 binding->handler = handler;
53 binding->data = data;
54
55 return binding;
56}
57
58WL_EXPORT struct weston_binding *
59weston_compositor_add_key_binding(struct weston_compositor *compositor,
60 uint32_t key, uint32_t modifier,
61 weston_key_binding_handler_t handler,
62 void *data)
63{
64 struct weston_binding *binding;
65
66 binding = weston_compositor_add_binding(compositor, key, 0, 0,
67 modifier, handler, data);
68 if (binding == NULL)
69 return NULL;
70
71 wl_list_insert(compositor->key_binding_list.prev, &binding->link);
72
73 return binding;
74}
75
76WL_EXPORT struct weston_binding *
77weston_compositor_add_button_binding(struct weston_compositor *compositor,
78 uint32_t button, uint32_t modifier,
79 weston_button_binding_handler_t handler,
80 void *data)
81{
82 struct weston_binding *binding;
83
84 binding = weston_compositor_add_binding(compositor, 0, button, 0,
85 modifier, handler, data);
86 if (binding == NULL)
87 return NULL;
88
89 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
90
91 return binding;
92}
93
94WL_EXPORT struct weston_binding *
95weston_compositor_add_axis_binding(struct weston_compositor *compositor,
96 uint32_t axis, uint32_t modifier,
97 weston_axis_binding_handler_t handler,
98 void *data)
99{
100 struct weston_binding *binding;
101
102 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
103 modifier, handler, data);
104 if (binding == NULL)
105 return NULL;
106
107 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
108
109 return binding;
110}
111
112WL_EXPORT struct weston_binding *
113weston_compositor_add_debug_binding(struct weston_compositor *compositor,
114 uint32_t key,
115 weston_key_binding_handler_t handler,
116 void *data)
117{
118 struct weston_binding *binding;
119
120 binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
121 handler, data);
122
123 wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
124
125 return binding;
126}
127
128WL_EXPORT void
129weston_binding_destroy(struct weston_binding *binding)
130{
131 wl_list_remove(&binding->link);
132 free(binding);
133}
134
135WL_EXPORT void
136weston_binding_list_destroy_all(struct wl_list *list)
137{
138 struct weston_binding *binding, *tmp;
139
140 wl_list_for_each_safe(binding, tmp, list, link)
141 weston_binding_destroy(binding);
142}
143
144struct binding_keyboard_grab {
145 uint32_t key;
146 struct wl_keyboard_grab grab;
147};
148
149static void
150binding_key(struct wl_keyboard_grab *grab,
151 uint32_t time, uint32_t key, uint32_t state_w)
152{
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;
158 uint32_t serial;
159 struct weston_keyboard *keyboard = (struct weston_keyboard *)grab->keyboard;
160
161 resource = grab->keyboard->focus_resource;
162 if (key == b->key) {
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;
167 free(b);
168 }
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);
173 }
174}
175
176static void
177binding_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)
180{
181 struct wl_resource *resource;
182
183 resource = grab->keyboard->focus_resource;
184 if (!resource)
185 return;
186
187 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
188 mods_latched, mods_locked, group);
189}
190
191static const struct wl_keyboard_grab_interface binding_grab = {
192 binding_key,
193 binding_modifiers,
194};
195
196static void
197install_binding_grab(struct wl_seat *seat,
198 uint32_t time, uint32_t key)
199{
200 struct binding_keyboard_grab *grab;
201
202 grab = malloc(sizeof *grab);
203 grab->key = key;
204 grab->grab.interface = &binding_grab;
205 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
206}
207
208WL_EXPORT void
209weston_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)
213{
214 struct weston_binding *b;
215
216 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
217 return;
218
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);
223
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);
230 }
231 }
232}
233
234WL_EXPORT void
235weston_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)
239{
240 struct weston_binding *b;
241
242 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
243 return;
244
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);
249 }
250 }
251}
252
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100253WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200254weston_compositor_run_axis_binding(struct weston_compositor *compositor,
255 struct weston_seat *seat,
256 uint32_t time, uint32_t axis,
257 wl_fixed_t value)
258{
259 struct weston_binding *b;
260
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);
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100265 return 1;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200266 }
267 }
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100268
269 return 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200270}
271
272WL_EXPORT int
273weston_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)
277{
278 weston_key_binding_handler_t handler;
279 struct weston_binding *binding;
280 int count = 0;
281
282 wl_list_for_each(binding, &compositor->debug_binding_list, link) {
283 if (key != binding->key)
284 continue;
285
286 count++;
287 handler = binding->handler;
288 handler(&seat->seat, time, key, binding->data);
289 }
290
291 return count;
292}