compositor-drm: Work around page flip not setting tiling mode on BYT
[platform/upstream/weston.git] / src / bindings.c
1 /*
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 "config.h"
24
25 #include <stdlib.h>
26
27 #include "compositor.h"
28
29 struct 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
39 static struct weston_binding *
40 weston_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
60 WL_EXPORT struct weston_binding *
61 weston_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
78 WL_EXPORT struct weston_binding *
79 weston_compositor_add_modifier_binding(struct weston_compositor *compositor,
80                                        uint32_t modifier,
81                                        weston_modifier_binding_handler_t handler,
82                                        void *data)
83 {
84         struct weston_binding *binding;
85
86         binding = weston_compositor_add_binding(compositor, 0, 0, 0,
87                                                 modifier, handler, data);
88         if (binding == NULL)
89                 return NULL;
90
91         wl_list_insert(compositor->modifier_binding_list.prev, &binding->link);
92
93         return binding;
94 }
95
96 WL_EXPORT struct weston_binding *
97 weston_compositor_add_button_binding(struct weston_compositor *compositor,
98                                      uint32_t button, uint32_t modifier,
99                                      weston_button_binding_handler_t handler,
100                                      void *data)
101 {
102         struct weston_binding *binding;
103
104         binding = weston_compositor_add_binding(compositor, 0, button, 0,
105                                                 modifier, handler, data);
106         if (binding == NULL)
107                 return NULL;
108
109         wl_list_insert(compositor->button_binding_list.prev, &binding->link);
110
111         return binding;
112 }
113
114 WL_EXPORT struct weston_binding *
115 weston_compositor_add_touch_binding(struct weston_compositor *compositor,
116                                     uint32_t modifier,
117                                     weston_touch_binding_handler_t handler,
118                                     void *data)
119 {
120         struct weston_binding *binding;
121
122         binding = weston_compositor_add_binding(compositor, 0, 0, 0,
123                                                 modifier, handler, data);
124         if (binding == NULL)
125                 return NULL;
126
127         wl_list_insert(compositor->touch_binding_list.prev, &binding->link);
128
129         return binding;
130 }
131
132 WL_EXPORT struct weston_binding *
133 weston_compositor_add_axis_binding(struct weston_compositor *compositor,
134                                    uint32_t axis, uint32_t modifier,
135                                    weston_axis_binding_handler_t handler,
136                                    void *data)
137 {
138         struct weston_binding *binding;
139
140         binding = weston_compositor_add_binding(compositor, 0, 0, axis,
141                                                 modifier, handler, data);
142         if (binding == NULL)
143                 return NULL;
144
145         wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
146
147         return binding;
148 }
149
150 WL_EXPORT struct weston_binding *
151 weston_compositor_add_debug_binding(struct weston_compositor *compositor,
152                                     uint32_t key,
153                                     weston_key_binding_handler_t handler,
154                                     void *data)
155 {
156         struct weston_binding *binding;
157
158         binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
159                                                 handler, data);
160
161         wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
162
163         return binding;
164 }
165
166 WL_EXPORT void
167 weston_binding_destroy(struct weston_binding *binding)
168 {
169         wl_list_remove(&binding->link);
170         free(binding);
171 }
172
173 WL_EXPORT void
174 weston_binding_list_destroy_all(struct wl_list *list)
175 {
176         struct weston_binding *binding, *tmp;
177
178         wl_list_for_each_safe(binding, tmp, list, link)
179                 weston_binding_destroy(binding);
180 }
181
182 struct binding_keyboard_grab {
183         uint32_t key;
184         struct weston_keyboard_grab grab;
185 };
186
187 static void
188 binding_key(struct weston_keyboard_grab *grab,
189             uint32_t time, uint32_t key, uint32_t state_w)
190 {
191         struct binding_keyboard_grab *b =
192                 container_of(grab, struct binding_keyboard_grab, grab);
193         struct wl_resource *resource;
194         enum wl_keyboard_key_state state = state_w;
195         uint32_t serial;
196         struct weston_keyboard *keyboard = grab->keyboard;
197         struct wl_display *display = keyboard->seat->compositor->wl_display;
198
199         if (key == b->key) {
200                 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
201                         weston_keyboard_end_grab(grab->keyboard);
202                         if (keyboard->input_method_resource)
203                                 keyboard->grab = &keyboard->input_method_grab;
204                         free(b);
205                 }
206         } else if (!wl_list_empty(&keyboard->focus_resource_list)) {
207                 serial = wl_display_next_serial(display);
208                 wl_resource_for_each(resource, &keyboard->focus_resource_list) {
209                         wl_keyboard_send_key(resource,
210                                              serial,
211                                              time,
212                                              key,
213                                              state);
214                 }
215         }
216 }
217
218 static void
219 binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
220                   uint32_t mods_depressed, uint32_t mods_latched,
221                   uint32_t mods_locked, uint32_t group)
222 {
223         struct wl_resource *resource;
224
225         wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) {
226                 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
227                                            mods_latched, mods_locked, group);
228         }
229 }
230
231 static void
232 binding_cancel(struct weston_keyboard_grab *grab)
233 {
234         struct binding_keyboard_grab *binding_grab =
235                 container_of(grab, struct binding_keyboard_grab, grab);
236
237         weston_keyboard_end_grab(grab->keyboard);
238         free(binding_grab);
239 }
240
241 static const struct weston_keyboard_grab_interface binding_grab = {
242         binding_key,
243         binding_modifiers,
244         binding_cancel,
245 };
246
247 static void
248 install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key)
249 {
250         struct binding_keyboard_grab *grab;
251
252         grab = malloc(sizeof *grab);
253         grab->key = key;
254         grab->grab.interface = &binding_grab;
255         weston_keyboard_start_grab(seat->keyboard, &grab->grab);
256 }
257
258 WL_EXPORT void
259 weston_compositor_run_key_binding(struct weston_compositor *compositor,
260                                   struct weston_seat *seat,
261                                   uint32_t time, uint32_t key,
262                                   enum wl_keyboard_key_state state)
263 {
264         struct weston_binding *b;
265
266         if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
267                 return;
268
269         /* Invalidate all active modifier bindings. */
270         wl_list_for_each(b, &compositor->modifier_binding_list, link)
271                 b->key = key;
272
273         wl_list_for_each(b, &compositor->key_binding_list, link) {
274                 if (b->key == key && b->modifier == seat->modifier_state) {
275                         weston_key_binding_handler_t handler = b->handler;
276                         handler(seat, time, key, b->data);
277
278                         /* If this was a key binding and it didn't
279                          * install a keyboard grab, install one now to
280                          * swallow the key release. */
281                         if (seat->keyboard->grab ==
282                             &seat->keyboard->default_grab)
283                                 install_binding_grab(seat, time, key);
284                 }
285         }
286 }
287
288 WL_EXPORT void
289 weston_compositor_run_modifier_binding(struct weston_compositor *compositor,
290                                        struct weston_seat *seat,
291                                        enum weston_keyboard_modifier modifier,
292                                        enum wl_keyboard_key_state state)
293 {
294         struct weston_binding *b;
295
296         if (seat->keyboard->grab != &seat->keyboard->default_grab)
297                 return;
298
299         wl_list_for_each(b, &compositor->modifier_binding_list, link) {
300                 weston_modifier_binding_handler_t handler = b->handler;
301
302                 if (b->modifier != modifier)
303                         continue;
304
305                 /* Prime the modifier binding. */
306                 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
307                         b->key = 0;
308                         continue;
309                 }
310                 /* Ignore the binding if a key was pressed in between. */
311                 else if (b->key != 0) {
312                         return;
313                 }
314
315                 handler(seat, modifier, b->data);
316         }
317 }
318
319 WL_EXPORT void
320 weston_compositor_run_button_binding(struct weston_compositor *compositor,
321                                      struct weston_seat *seat,
322                                      uint32_t time, uint32_t button,
323                                      enum wl_pointer_button_state state)
324 {
325         struct weston_binding *b;
326
327         if (state == WL_POINTER_BUTTON_STATE_RELEASED)
328                 return;
329
330         /* Invalidate all active modifier bindings. */
331         wl_list_for_each(b, &compositor->modifier_binding_list, link)
332                 b->key = button;
333
334         wl_list_for_each(b, &compositor->button_binding_list, link) {
335                 if (b->button == button && b->modifier == seat->modifier_state) {
336                         weston_button_binding_handler_t handler = b->handler;
337                         handler(seat, time, button, b->data);
338                 }
339         }
340 }
341
342 WL_EXPORT void
343 weston_compositor_run_touch_binding(struct weston_compositor *compositor,
344                                     struct weston_seat *seat, uint32_t time,
345                                     int touch_type)
346 {
347         struct weston_binding *b;
348
349         if (seat->touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN)
350                 return;
351
352         wl_list_for_each(b, &compositor->touch_binding_list, link) {
353                 if (b->modifier == seat->modifier_state) {
354                         weston_touch_binding_handler_t handler = b->handler;
355                         handler(seat, time, b->data);
356                 }
357         }
358 }
359
360 WL_EXPORT int
361 weston_compositor_run_axis_binding(struct weston_compositor *compositor,
362                                    struct weston_seat *seat,
363                                    uint32_t time, uint32_t axis,
364                                    wl_fixed_t value)
365 {
366         struct weston_binding *b;
367
368         /* Invalidate all active modifier bindings. */
369         wl_list_for_each(b, &compositor->modifier_binding_list, link)
370                 b->key = axis;
371
372         wl_list_for_each(b, &compositor->axis_binding_list, link) {
373                 if (b->axis == axis && b->modifier == seat->modifier_state) {
374                         weston_axis_binding_handler_t handler = b->handler;
375                         handler(seat, time, axis, value, b->data);
376                         return 1;
377                 }
378         }
379
380         return 0;
381 }
382
383 WL_EXPORT int
384 weston_compositor_run_debug_binding(struct weston_compositor *compositor,
385                                     struct weston_seat *seat,
386                                     uint32_t time, uint32_t key,
387                                     enum wl_keyboard_key_state state)
388 {
389         weston_key_binding_handler_t handler;
390         struct weston_binding *binding;
391         int count = 0;
392
393         wl_list_for_each(binding, &compositor->debug_binding_list, link) {
394                 if (key != binding->key)
395                         continue;
396
397                 count++;
398                 handler = binding->handler;
399                 handler(seat, time, key, binding->data);
400         }
401
402         return count;
403 }