blob: a871c26dcc2982182a136294716272238f8165d3 [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
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) {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200172 serial = wl_display_next_serial(display);
173 wl_keyboard_send_key(resource, serial, time, key, state);
174 }
175}
176
177static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400178binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200179 uint32_t mods_depressed, uint32_t mods_latched,
180 uint32_t mods_locked, uint32_t group)
181{
182 struct wl_resource *resource;
183
184 resource = grab->keyboard->focus_resource;
185 if (!resource)
186 return;
187
188 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
189 mods_latched, mods_locked, group);
190}
191
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400192static const struct weston_keyboard_grab_interface binding_grab = {
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200193 binding_key,
194 binding_modifiers,
195};
196
197static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400198install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key)
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200199{
200 struct binding_keyboard_grab *grab;
201
202 grab = malloc(sizeof *grab);
203 grab->key = key;
204 grab->grab.interface = &binding_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -0400205 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200206}
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;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400222 handler(seat, time, key, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200223
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. */
Kristian Høgsberge3148752013-05-06 23:19:49 -0400227 if (seat->keyboard->grab ==
228 &seat->keyboard->default_grab)
229 install_binding_grab(seat, time, key);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200230 }
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;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400248 handler(seat, time, button, b->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200249 }
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;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400264 handler(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;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400288 handler(seat, time, key, binding->data);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200289 }
290
291 return count;
292}