blob: e548ff1393b9bfd240f94935e5b26a6e6eaa8861 [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;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400146 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200147};
148
149static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400150binding_key(struct weston_keyboard_grab *grab,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200151 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;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400159 struct weston_keyboard *keyboard = grab->keyboard;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200160
161 resource = grab->keyboard->focus_resource;
162 if (key == b->key) {
163 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400164 weston_keyboard_end_grab(grab->keyboard);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200165 if (keyboard->input_method_resource)
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400166 keyboard->grab = &keyboard->input_method_grab;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200167 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
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400177binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200178 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
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400191static const struct weston_keyboard_grab_interface binding_grab = {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200192 binding_key,
193 binding_modifiers,
194};
195
196static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400197install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200198{
199 struct binding_keyboard_grab *grab;
200
201 grab = malloc(sizeof *grab);
202 grab->key = key;
203 grab->grab.interface = &binding_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400204 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200205}
206
207WL_EXPORT void
208weston_compositor_run_key_binding(struct weston_compositor *compositor,
209 struct weston_seat *seat,
210 uint32_t time, uint32_t key,
211 enum wl_keyboard_key_state state)
212{
213 struct weston_binding *b;
214
215 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
216 return;
217
218 wl_list_for_each(b, &compositor->key_binding_list, link) {
219 if (b->key == key && b->modifier == seat->modifier_state) {
220 weston_key_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400221 handler(seat, time, key, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200222
223 /* If this was a key binding and it didn't
224 * install a keyboard grab, install one now to
225 * swallow the key release. */
Kristian Høgsberge3148752013-05-06 23:19:49 -0400226 if (seat->keyboard->grab ==
227 &seat->keyboard->default_grab)
228 install_binding_grab(seat, time, key);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200229 }
230 }
231}
232
233WL_EXPORT void
234weston_compositor_run_button_binding(struct weston_compositor *compositor,
235 struct weston_seat *seat,
236 uint32_t time, uint32_t button,
237 enum wl_pointer_button_state state)
238{
239 struct weston_binding *b;
240
241 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
242 return;
243
244 wl_list_for_each(b, &compositor->button_binding_list, link) {
245 if (b->button == button && b->modifier == seat->modifier_state) {
246 weston_button_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400247 handler(seat, time, button, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200248 }
249 }
250}
251
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100252WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200253weston_compositor_run_axis_binding(struct weston_compositor *compositor,
254 struct weston_seat *seat,
255 uint32_t time, uint32_t axis,
256 wl_fixed_t value)
257{
258 struct weston_binding *b;
259
260 wl_list_for_each(b, &compositor->axis_binding_list, link) {
261 if (b->axis == axis && b->modifier == seat->modifier_state) {
262 weston_axis_binding_handler_t handler = b->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400263 handler(seat, time, axis, value, b->data);
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100264 return 1;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200265 }
266 }
Rune K. Svendsen14b2fe72013-03-07 21:50:00 +0100267
268 return 0;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200269}
270
271WL_EXPORT int
272weston_compositor_run_debug_binding(struct weston_compositor *compositor,
273 struct weston_seat *seat,
274 uint32_t time, uint32_t key,
275 enum wl_keyboard_key_state state)
276{
277 weston_key_binding_handler_t handler;
278 struct weston_binding *binding;
279 int count = 0;
280
281 wl_list_for_each(binding, &compositor->debug_binding_list, link) {
282 if (key != binding->key)
283 continue;
284
285 count++;
286 handler = binding->handler;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400287 handler(seat, time, key, binding->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200288 }
289
290 return count;
291}