blob: 558208ce0cb66f57c735e8139901cd39c2bd05a0 [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;
158 struct wl_display *display;
159 enum wl_keyboard_key_state state = state_w;
160 uint32_t serial;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400161 struct weston_keyboard *keyboard = grab->keyboard;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200162
163 resource = grab->keyboard->focus_resource;
164 if (key == b->key) {
165 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400166 weston_keyboard_end_grab(grab->keyboard);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200167 if (keyboard->input_method_resource)
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400168 keyboard->grab = &keyboard->input_method_grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200169 free(b);
170 }
171 } else if (resource) {
172 display = wl_client_get_display(resource->client);
173 serial = wl_display_next_serial(display);
174 wl_keyboard_send_key(resource, serial, time, key, state);
175 }
176}
177
178static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400179binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200180 uint32_t mods_depressed, uint32_t mods_latched,
181 uint32_t mods_locked, uint32_t group)
182{
183 struct wl_resource *resource;
184
185 resource = grab->keyboard->focus_resource;
186 if (!resource)
187 return;
188
189 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
190 mods_latched, mods_locked, group);
191}
192
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400193static const struct weston_keyboard_grab_interface binding_grab = {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200194 binding_key,
195 binding_modifiers,
196};
197
198static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400199install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200200{
201 struct binding_keyboard_grab *grab;
202
203 grab = malloc(sizeof *grab);
204 grab->key = key;
205 grab->grab.interface = &binding_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400206 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200207}
208
209WL_EXPORT void
210weston_compositor_run_key_binding(struct weston_compositor *compositor,
211 struct weston_seat *seat,
212 uint32_t time, uint32_t key,
213 enum wl_keyboard_key_state state)
214{
215 struct weston_binding *b;
216
217 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
218 return;
219
220 wl_list_for_each(b, &compositor->key_binding_list, link) {
221 if (b->key == key && b->modifier == seat->modifier_state) {
222 weston_key_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400223 handler(seat, time, key, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200224
225 /* If this was a key binding and it didn't
226 * install a keyboard grab, install one now to
227 * swallow the key release. */
Kristian Høgsberge3148752013-05-06 23:19:49 -0400228 if (seat->keyboard->grab ==
229 &seat->keyboard->default_grab)
230 install_binding_grab(seat, time, key);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200231 }
232 }
233}
234
235WL_EXPORT void
236weston_compositor_run_button_binding(struct weston_compositor *compositor,
237 struct weston_seat *seat,
238 uint32_t time, uint32_t button,
239 enum wl_pointer_button_state state)
240{
241 struct weston_binding *b;
242
243 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
244 return;
245
246 wl_list_for_each(b, &compositor->button_binding_list, link) {
247 if (b->button == button && b->modifier == seat->modifier_state) {
248 weston_button_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400249 handler(seat, time, button, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200250 }
251 }
252}
253
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100254WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200255weston_compositor_run_axis_binding(struct weston_compositor *compositor,
256 struct weston_seat *seat,
257 uint32_t time, uint32_t axis,
258 wl_fixed_t value)
259{
260 struct weston_binding *b;
261
262 wl_list_for_each(b, &compositor->axis_binding_list, link) {
263 if (b->axis == axis && b->modifier == seat->modifier_state) {
264 weston_axis_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400265 handler(seat, time, axis, value, b->data);
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100266 return 1;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200267 }
268 }
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100269
270 return 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200271}
272
273WL_EXPORT int
274weston_compositor_run_debug_binding(struct weston_compositor *compositor,
275 struct weston_seat *seat,
276 uint32_t time, uint32_t key,
277 enum wl_keyboard_key_state state)
278{
279 weston_key_binding_handler_t handler;
280 struct weston_binding *binding;
281 int count = 0;
282
283 wl_list_for_each(binding, &compositor->debug_binding_list, link) {
284 if (key != binding->key)
285 continue;
286
287 count++;
288 handler = binding->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400289 handler(seat, time, key, binding->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200290 }
291
292 return count;
293}