1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
/*
* Copyright © 2011-2012 Intel Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. The copyright holders make
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <stdlib.h>
#include <linux/input.h>
#include "compositor.h"
struct weston_binding {
uint32_t key;
uint32_t button;
uint32_t axis;
uint32_t modifier;
void *handler;
void *data;
struct wl_list link;
};
static struct weston_binding *
weston_compositor_add_binding(struct weston_compositor *compositor,
uint32_t key, uint32_t button, uint32_t axis,
uint32_t modifier, void *handler, void *data)
{
struct weston_binding *binding;
binding = malloc(sizeof *binding);
if (binding == NULL)
return NULL;
binding->key = key;
binding->button = button;
binding->axis = axis;
binding->modifier = modifier;
binding->handler = handler;
binding->data = data;
return binding;
}
WL_EXPORT struct weston_binding *
weston_compositor_add_key_binding(struct weston_compositor *compositor,
uint32_t key, uint32_t modifier,
weston_key_binding_handler_t handler,
void *data)
{
struct weston_binding *binding;
binding = weston_compositor_add_binding(compositor, key, 0, 0,
modifier, handler, data);
if (binding == NULL)
return NULL;
wl_list_insert(compositor->key_binding_list.prev, &binding->link);
return binding;
}
WL_EXPORT struct weston_binding *
weston_compositor_add_modifier_binding(struct weston_compositor *compositor,
uint32_t modifier,
weston_modifier_binding_handler_t handler,
void *data)
{
struct weston_binding *binding;
binding = weston_compositor_add_binding(compositor, 0, 0, 0,
modifier, handler, data);
if (binding == NULL)
return NULL;
wl_list_insert(compositor->modifier_binding_list.prev, &binding->link);
return binding;
}
WL_EXPORT struct weston_binding *
weston_compositor_add_button_binding(struct weston_compositor *compositor,
uint32_t button, uint32_t modifier,
weston_button_binding_handler_t handler,
void *data)
{
struct weston_binding *binding;
binding = weston_compositor_add_binding(compositor, 0, button, 0,
modifier, handler, data);
if (binding == NULL)
return NULL;
wl_list_insert(compositor->button_binding_list.prev, &binding->link);
return binding;
}
WL_EXPORT struct weston_binding *
weston_compositor_add_touch_binding(struct weston_compositor *compositor,
uint32_t modifier,
weston_touch_binding_handler_t handler,
void *data)
{
struct weston_binding *binding;
binding = weston_compositor_add_binding(compositor, 0, 0, 0,
modifier, handler, data);
if (binding == NULL)
return NULL;
wl_list_insert(compositor->touch_binding_list.prev, &binding->link);
return binding;
}
WL_EXPORT struct weston_binding *
weston_compositor_add_axis_binding(struct weston_compositor *compositor,
uint32_t axis, uint32_t modifier,
weston_axis_binding_handler_t handler,
void *data)
{
struct weston_binding *binding;
binding = weston_compositor_add_binding(compositor, 0, 0, axis,
modifier, handler, data);
if (binding == NULL)
return NULL;
wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
return binding;
}
WL_EXPORT struct weston_binding *
weston_compositor_add_debug_binding(struct weston_compositor *compositor,
uint32_t key,
weston_key_binding_handler_t handler,
void *data)
{
struct weston_binding *binding;
binding = weston_compositor_add_binding(compositor, key, 0, 0, 0,
handler, data);
wl_list_insert(compositor->debug_binding_list.prev, &binding->link);
return binding;
}
WL_EXPORT void
weston_binding_destroy(struct weston_binding *binding)
{
wl_list_remove(&binding->link);
free(binding);
}
WL_EXPORT void
weston_binding_list_destroy_all(struct wl_list *list)
{
struct weston_binding *binding, *tmp;
wl_list_for_each_safe(binding, tmp, list, link)
weston_binding_destroy(binding);
}
struct binding_keyboard_grab {
uint32_t key;
struct weston_keyboard_grab grab;
};
static void
binding_key(struct weston_keyboard_grab *grab,
uint32_t time, uint32_t key, uint32_t state_w)
{
struct binding_keyboard_grab *b =
container_of(grab, struct binding_keyboard_grab, grab);
struct wl_resource *resource;
enum wl_keyboard_key_state state = state_w;
uint32_t serial;
struct weston_keyboard *keyboard = grab->keyboard;
struct wl_display *display = keyboard->seat->compositor->wl_display;
if (key == b->key) {
if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
weston_keyboard_end_grab(grab->keyboard);
if (keyboard->input_method_resource)
keyboard->grab = &keyboard->input_method_grab;
free(b);
} else {
/* Don't send the key press event for the binding key */
return;
}
}
if (!wl_list_empty(&keyboard->focus_resource_list)) {
serial = wl_display_next_serial(display);
wl_resource_for_each(resource, &keyboard->focus_resource_list) {
wl_keyboard_send_key(resource,
serial,
time,
key,
state);
}
}
}
static void
binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group)
{
struct wl_resource *resource;
wl_resource_for_each(resource, &grab->keyboard->focus_resource_list) {
wl_keyboard_send_modifiers(resource, serial, mods_depressed,
mods_latched, mods_locked, group);
}
}
static void
binding_cancel(struct weston_keyboard_grab *grab)
{
struct binding_keyboard_grab *binding_grab =
container_of(grab, struct binding_keyboard_grab, grab);
weston_keyboard_end_grab(grab->keyboard);
free(binding_grab);
}
static const struct weston_keyboard_grab_interface binding_grab = {
binding_key,
binding_modifiers,
binding_cancel,
};
static void
install_binding_grab(struct weston_seat *seat, uint32_t time, uint32_t key,
struct weston_surface *focus)
{
struct binding_keyboard_grab *grab;
grab = malloc(sizeof *grab);
grab->key = key;
grab->grab.interface = &binding_grab;
weston_keyboard_start_grab(seat->keyboard, &grab->grab);
/* Notify the surface which had the focus before this binding
* triggered that we stole a keypress from under it, by forcing
* a wl_keyboard leave/enter pair. The enter event will contain
* the pressed key in the keys array, so the client will know
* the exact state of the keyboard.
* If the old focus surface is different than the new one it
* means it was changed in the binding handler, so it received
* the enter event already. */
if (focus && seat->keyboard->focus == focus) {
weston_keyboard_set_focus(seat->keyboard, NULL);
weston_keyboard_set_focus(seat->keyboard, focus);
}
}
WL_EXPORT void
weston_compositor_run_key_binding(struct weston_compositor *compositor,
struct weston_seat *seat,
uint32_t time, uint32_t key,
enum wl_keyboard_key_state state)
{
struct weston_binding *b, *tmp;
struct weston_surface *focus;
if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
return;
/* Invalidate all active modifier bindings. */
wl_list_for_each(b, &compositor->modifier_binding_list, link)
b->key = key;
wl_list_for_each_safe(b, tmp, &compositor->key_binding_list, link) {
if (b->key == key && b->modifier == seat->modifier_state) {
weston_key_binding_handler_t handler = b->handler;
focus = seat->keyboard->focus;
handler(seat, time, key, b->data);
/* If this was a key binding and it didn't
* install a keyboard grab, install one now to
* swallow the key press. */
if (seat->keyboard->grab ==
&seat->keyboard->default_grab)
install_binding_grab(seat, time, key, focus);
}
}
}
WL_EXPORT void
weston_compositor_run_modifier_binding(struct weston_compositor *compositor,
struct weston_seat *seat,
enum weston_keyboard_modifier modifier,
enum wl_keyboard_key_state state)
{
struct weston_binding *b, *tmp;
if (seat->keyboard->grab != &seat->keyboard->default_grab)
return;
wl_list_for_each_safe(b, tmp, &compositor->modifier_binding_list, link) {
weston_modifier_binding_handler_t handler = b->handler;
if (b->modifier != modifier)
continue;
/* Prime the modifier binding. */
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
b->key = 0;
continue;
}
/* Ignore the binding if a key was pressed in between. */
else if (b->key != 0) {
return;
}
handler(seat, modifier, b->data);
}
}
WL_EXPORT void
weston_compositor_run_button_binding(struct weston_compositor *compositor,
struct weston_seat *seat,
uint32_t time, uint32_t button,
enum wl_pointer_button_state state)
{
struct weston_binding *b, *tmp;
if (state == WL_POINTER_BUTTON_STATE_RELEASED)
return;
/* Invalidate all active modifier bindings. */
wl_list_for_each(b, &compositor->modifier_binding_list, link)
b->key = button;
wl_list_for_each_safe(b, tmp, &compositor->button_binding_list, link) {
if (b->button == button && b->modifier == seat->modifier_state) {
weston_button_binding_handler_t handler = b->handler;
handler(seat, time, button, b->data);
}
}
}
WL_EXPORT void
weston_compositor_run_touch_binding(struct weston_compositor *compositor,
struct weston_seat *seat, uint32_t time,
int touch_type)
{
struct weston_binding *b, *tmp;
if (seat->touch->num_tp != 1 || touch_type != WL_TOUCH_DOWN)
return;
wl_list_for_each_safe(b, tmp, &compositor->touch_binding_list, link) {
if (b->modifier == seat->modifier_state) {
weston_touch_binding_handler_t handler = b->handler;
handler(seat, time, b->data);
}
}
}
WL_EXPORT int
weston_compositor_run_axis_binding(struct weston_compositor *compositor,
struct weston_seat *seat,
uint32_t time, uint32_t axis,
wl_fixed_t value)
{
struct weston_binding *b, *tmp;
/* Invalidate all active modifier bindings. */
wl_list_for_each(b, &compositor->modifier_binding_list, link)
b->key = axis;
wl_list_for_each_safe(b, tmp, &compositor->axis_binding_list, link) {
if (b->axis == axis && b->modifier == seat->modifier_state) {
weston_axis_binding_handler_t handler = b->handler;
handler(seat, time, axis, value, b->data);
return 1;
}
}
return 0;
}
WL_EXPORT int
weston_compositor_run_debug_binding(struct weston_compositor *compositor,
struct weston_seat *seat,
uint32_t time, uint32_t key,
enum wl_keyboard_key_state state)
{
weston_key_binding_handler_t handler;
struct weston_binding *binding, *tmp;
int count = 0;
wl_list_for_each_safe(binding, tmp, &compositor->debug_binding_list, link) {
if (key != binding->key)
continue;
count++;
handler = binding->handler;
handler(seat, time, key, binding->data);
}
return count;
}
struct debug_binding_grab {
struct weston_keyboard_grab grab;
struct weston_seat *seat;
uint32_t key[2];
int key_released[2];
};
static void
debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
uint32_t key, uint32_t state)
{
struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
struct weston_compositor *ec = db->seat->compositor;
struct wl_display *display = ec->wl_display;
struct wl_resource *resource;
uint32_t serial;
int send = 0, terminate = 0;
int check_binding = 1;
int i;
struct wl_list *resource_list;
if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
/* Do not run bindings on key releases */
check_binding = 0;
for (i = 0; i < 2; i++)
if (key == db->key[i])
db->key_released[i] = 1;
if (db->key_released[0] && db->key_released[1]) {
/* All key releases been swalled so end the grab */
terminate = 1;
} else if (key != db->key[0] && key != db->key[1]) {
/* Should not swallow release of other keys */
send = 1;
}
} else if (key == db->key[0] && !db->key_released[0]) {
/* Do not check bindings for the first press of the binding
* key. This allows it to be used as a debug shortcut.
* We still need to swallow this event. */
check_binding = 0;
} else if (db->key[1]) {
/* If we already ran a binding don't process another one since
* we can't keep track of all the binding keys that were
* pressed in order to swallow the release events. */
send = 1;
check_binding = 0;
}
if (check_binding) {
if (weston_compositor_run_debug_binding(ec, db->seat, time,
key, state)) {
/* We ran a binding so swallow the press and keep the
* grab to swallow the released too. */
send = 0;
terminate = 0;
db->key[1] = key;
} else {
/* Terminate the grab since the key pressed is not a
* debug binding key. */
send = 1;
terminate = 1;
}
}
if (send) {
serial = wl_display_next_serial(display);
resource_list = &grab->keyboard->focus_resource_list;
wl_resource_for_each(resource, resource_list) {
wl_keyboard_send_key(resource, serial, time, key, state);
}
}
if (terminate) {
weston_keyboard_end_grab(grab->keyboard);
if (grab->keyboard->input_method_resource)
grab->keyboard->grab = &grab->keyboard->input_method_grab;
free(db);
}
}
static void
debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group)
{
struct wl_resource *resource;
struct wl_list *resource_list;
resource_list = &grab->keyboard->focus_resource_list;
wl_resource_for_each(resource, resource_list) {
wl_keyboard_send_modifiers(resource, serial, mods_depressed,
mods_latched, mods_locked, group);
}
}
static void
debug_binding_cancel(struct weston_keyboard_grab *grab)
{
struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
weston_keyboard_end_grab(grab->keyboard);
free(db);
}
struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
debug_binding_key,
debug_binding_modifiers,
debug_binding_cancel,
};
static void
debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
{
struct debug_binding_grab *grab;
grab = calloc(1, sizeof *grab);
if (!grab)
return;
grab->seat = seat;
grab->key[0] = key;
grab->grab.interface = &debug_binding_keyboard_grab;
weston_keyboard_start_grab(seat->keyboard, &grab->grab);
}
/** Install the trigger binding for debug bindings.
*
* \param compositor The compositor.
* \param mod The modifier.
*
* This will add a key binding for modifier+SHIFT+SPACE that will trigger
* debug key bindings.
*/
WL_EXPORT void
weston_install_debug_key_binding(struct weston_compositor *compositor,
uint32_t mod)
{
weston_compositor_add_key_binding(compositor, KEY_SPACE,
mod | MODIFIER_SHIFT,
debug_binding, NULL);
}
|