blob: f6ec9ea01f41b6e234aadc107c9c3b7aa019ec8c [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 *
79weston_compositor_add_button_binding(struct weston_compositor *compositor,
80 uint32_t button, uint32_t modifier,
81 weston_button_binding_handler_t handler,
82 void *data)
83{
84 struct weston_binding *binding;
85
86 binding = weston_compositor_add_binding(compositor, 0, button, 0,
87 modifier, handler, data);
88 if (binding == NULL)
89 return NULL;
90
91 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
92
93 return binding;
94}
95
96WL_EXPORT struct weston_binding *
97weston_compositor_add_axis_binding(struct weston_compositor *compositor,
98 uint32_t axis, uint32_t modifier,
99 weston_axis_binding_handler_t handler,
100 void *data)
101{
102 struct weston_binding *binding;
103
104 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
105 modifier, handler, data);
106 if (binding == NULL)
107 return NULL;
108
109 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
110
111 return binding;
112}
113
114WL_EXPORT struct weston_binding *
115weston_compositor_add_debug_binding(struct weston_compositor *compositor,
116 uint32_t key,
117 weston_key_binding_handler_t handler,
118 void *data)
119{
120 struct weston_binding *binding;
121
122 binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
123 handler, data);
124
125 wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
126
127 return binding;
128}
129
130WL_EXPORT void
131weston_binding_destroy(struct weston_binding *binding)
132{
133 wl_list_remove(&binding->link);
134 free(binding);
135}
136
137WL_EXPORT void
138weston_binding_list_destroy_all(struct wl_list *list)
139{
140 struct weston_binding *binding, *tmp;
141
142 wl_list_for_each_safe(binding, tmp, list, link)
143 weston_binding_destroy(binding);
144}
145
146struct binding_keyboard_grab {
147 uint32_t key;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400148 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200149};
150
151static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400152binding_key(struct weston_keyboard_grab *grab,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200153 uint32_t time, uint32_t key, uint32_t state_w)
154{
155 struct binding_keyboard_grab *b =
156 container_of(grab, struct binding_keyboard_grab, grab);
157 struct wl_resource *resource;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200158 enum wl_keyboard_key_state state = state_w;
159 uint32_t serial;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400160 struct weston_keyboard *keyboard = grab->keyboard;
Rob Bradford880ebc72013-07-22 17:31:38 +0100161 struct wl_display *display = keyboard->seat->compositor->wl_display;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200162
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200163 if (key == b->key) {
164 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400165 weston_keyboard_end_grab(grab->keyboard);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200166 if (keyboard->input_method_resource)
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400167 keyboard->grab = &keyboard->input_method_grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200168 free(b);
169 }
Neil Roberts96d790e2013-09-19 17:32:00 +0100170 } else if (!wl_list_empty(&keyboard->focus_resource_list)) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200171 serial = wl_display_next_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +0100172 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
173 wl_keyboard_send_key(resource,
174 serial,
175 time,
176 key,
177 state);
178 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200179 }
180}
181
182static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400183binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200184 uint32_t mods_depressed, uint32_t mods_latched,
185 uint32_t mods_locked, uint32_t group)
186{
187 struct wl_resource *resource;
188
Neil Roberts96d790e2013-09-19 17:32:00 +0100189 wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) {
190 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
191 mods_latched, mods_locked, group);
192 }
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200193}
194
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400195static const struct weston_keyboard_grab_interface binding_grab = {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200196 binding_key,
197 binding_modifiers,
198};
199
200static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400201install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200202{
203 struct binding_keyboard_grab *grab;
204
205 grab = malloc(sizeof *grab);
206 grab->key = key;
207 grab->grab.interface = &binding_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400208 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200209}
210
211WL_EXPORT void
212weston_compositor_run_key_binding(struct weston_compositor *compositor,
213 struct weston_seat *seat,
214 uint32_t time, uint32_t key,
215 enum wl_keyboard_key_state state)
216{
217 struct weston_binding *b;
218
219 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
220 return;
221
222 wl_list_for_each(b, &compositor->key_binding_list, link) {
223 if (b->key == key && b->modifier == seat->modifier_state) {
224 weston_key_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400225 handler(seat, time, key, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200226
227 /* If this was a key binding and it didn't
228 * install a keyboard grab, install one now to
229 * swallow the key release. */
Kristian Høgsberge3148752013-05-06 23:19:49 -0400230 if (seat->keyboard->grab ==
231 &seat->keyboard->default_grab)
232 install_binding_grab(seat, time, key);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200233 }
234 }
235}
236
237WL_EXPORT void
238weston_compositor_run_button_binding(struct weston_compositor *compositor,
239 struct weston_seat *seat,
240 uint32_t time, uint32_t button,
241 enum wl_pointer_button_state state)
242{
243 struct weston_binding *b;
244
245 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
246 return;
247
248 wl_list_for_each(b, &compositor->button_binding_list, link) {
249 if (b->button == button && b->modifier == seat->modifier_state) {
250 weston_button_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400251 handler(seat, time, button, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200252 }
253 }
254}
255
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100256WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200257weston_compositor_run_axis_binding(struct weston_compositor *compositor,
258 struct weston_seat *seat,
259 uint32_t time, uint32_t axis,
260 wl_fixed_t value)
261{
262 struct weston_binding *b;
263
264 wl_list_for_each(b, &compositor->axis_binding_list, link) {
265 if (b->axis == axis && b->modifier == seat->modifier_state) {
266 weston_axis_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400267 handler(seat, time, axis, value, b->data);
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100268 return 1;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200269 }
270 }
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100271
272 return 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200273}
274
275WL_EXPORT int
276weston_compositor_run_debug_binding(struct weston_compositor *compositor,
277 struct weston_seat *seat,
278 uint32_t time, uint32_t key,
279 enum wl_keyboard_key_state state)
280{
281 weston_key_binding_handler_t handler;
282 struct weston_binding *binding;
283 int count = 0;
284
285 wl_list_for_each(binding, &compositor->debug_binding_list, link) {
286 if (key != binding->key)
287 continue;
288
289 count++;
290 handler = binding->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400291 handler(seat, time, key, binding->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200292 }
293
294 return count;
295}