Debian Codegraph @

clutter/wayland/clutter-input-device-wayland.c

raw
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Copyright (C) 2010 Intel Corporation.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20
21 * Authors:
22 * Matthew Allum
23 * Robert Bragg
24 * Kristian Høgsberg
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <linux/input.h>
34 #include <sys/mman.h>
35 #include <wayland-util.h>
36 #include <wayland-client.h>
37
38 #include "clutter-debug.h"
39 #include "clutter-device-manager-private.h"
40 #include "clutter-event-private.h"
41 #include "clutter-private.h"
42 #include "clutter-keysyms.h"
43 #include "evdev/clutter-xkb-utils.h"
44 #include "clutter-input-device-wayland.h"
45 #include "clutter-backend-wayland.h"
46 #include "clutter-backend-wayland-priv.h"
47 #include "clutter-stage-private.h"
48 #include "clutter-stage-wayland.h"
49 #include "clutter-wayland.h"
50
51 #include "cogl/clutter-stage-cogl.h"
52
53 typedef struct _ClutterInputDeviceClass ClutterInputDeviceWaylandClass;
54
55 #define clutter_input_device_wayland_get_type \
56 _clutter_input_device_wayland_get_type
57
58 G_DEFINE_TYPE (ClutterInputDeviceWayland,
59 clutter_input_device_wayland,
60 CLUTTER_TYPE_INPUT_DEVICE);
61
62 /* This gives us a fake time source for higher level abstractions to have an
63 * understanding of when an event happens. All that matters are that this is a
64 * monotonic increasing millisecond accurate time for events to be compared with.
65 */
66 static guint32
67 _clutter_wayland_get_time (void)
68 {
69 return g_get_monotonic_time () / 1000;
70 }
71
72 static void
73 clutter_wayland_handle_motion (void *data,
74 struct wl_pointer *pointer,
75 uint32_t _time,
76 wl_fixed_t x, wl_fixed_t y)
77 {
78 ClutterInputDeviceWayland *device = data;
79 ClutterStageCogl *stage_cogl;
80 ClutterEvent *event;
81
82 if (!device->pointer_focus)
83 return;
84
85 stage_cogl = device->pointer_focus;
86
87 event = clutter_event_new (CLUTTER_MOTION);
88 event->motion.stage = stage_cogl->wrapper;
89 event->motion.device = CLUTTER_INPUT_DEVICE (device);
90 event->motion.time = _clutter_wayland_get_time ();
91 event->motion.modifier_state = 0;
92 event->motion.x = wl_fixed_to_double (x);
93 event->motion.y = wl_fixed_to_double (y);
94
95 device->x = event->motion.x;
96 device->y = event->motion.y;
97
98 _clutter_event_push (event, FALSE);
99 }
100
101 static void
102 clutter_wayland_handle_button (void *data,
103 struct wl_pointer *pointer,
104 uint32_t serial, uint32_t _time,
105 uint32_t button, uint32_t state)
106 {
107 ClutterInputDeviceWayland *device = data;
108 ClutterStageCogl *stage_cogl;
109 ClutterEvent *event;
110 ClutterEventType type;
111 ClutterModifierType modifier_mask = 0;
112
113 if (!device->pointer_focus)
114 return;
115
116 stage_cogl = device->pointer_focus;
117
118 if (state)
119 type = CLUTTER_BUTTON_PRESS;
120 else
121 type = CLUTTER_BUTTON_RELEASE;
122
123 event = clutter_event_new (type);
124 event->button.stage = stage_cogl->wrapper;
125 event->button.device = CLUTTER_INPUT_DEVICE (device);
126 event->button.time = _clutter_wayland_get_time ();
127 event->button.x = device->x;
128 event->button.y = device->y;
129 _clutter_xkb_translate_state (event, device->xkb, 0);
130
131 /* evdev button codes */
132 switch (button) {
133 case BTN_LEFT:
134 event->button.button = CLUTTER_BUTTON_PRIMARY;
135 modifier_mask = CLUTTER_BUTTON1_MASK;
136 break;
137 case BTN_RIGHT:
138 event->button.button = CLUTTER_BUTTON_SECONDARY;
139 modifier_mask = CLUTTER_BUTTON3_MASK;
140 break;
141 case BTN_MIDDLE:
142 event->button.button = CLUTTER_BUTTON_MIDDLE;
143 modifier_mask = CLUTTER_BUTTON2_MASK;
144 break;
145 }
146
147 if (modifier_mask)
148 {
149 if (state)
150 device->button_modifier_state |= modifier_mask;
151 else
152 device->button_modifier_state &= ~modifier_mask;
153 }
154
155 event->button.modifier_state = device->button_modifier_state;
156
157 _clutter_event_push (event, FALSE);
158 }
159
160 static void
161 clutter_wayland_handle_axis (void *data,
162 struct wl_pointer *pointer,
163 uint32_t time,
164 uint32_t axis,
165 wl_fixed_t value)
166 {
167 ClutterInputDeviceWayland *device = data;
168 ClutterStageCogl *stage_cogl;
169 ClutterEvent *event;
170 gdouble delta_x, delta_y;
171 gdouble delta_factor;
172
173 if (!device->pointer_focus)
174 return;
175
176 stage_cogl = device->pointer_focus;
177 event = clutter_event_new (CLUTTER_SCROLL);
178 event->scroll.time = _clutter_wayland_get_time ();
179 event->scroll.stage = stage_cogl->wrapper;
180 event->scroll.direction = CLUTTER_SCROLL_SMOOTH;
181 event->scroll.x = device->x;
182 event->scroll.y = device->y;
183
184 /* Wayland pointer axis events are in pointer motion coordinate space.
185 * To convert to Xi2 discrete step coordinate space, multiply the factor
186 * 1/10. */
187 delta_factor = 1.0 / 10.0;
188 if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL)
189 {
190 delta_x = wl_fixed_to_double (value) * delta_factor;
191 delta_y = 0;
192 }
193 else
194 {
195 delta_x = 0;
196 delta_y = wl_fixed_to_double (value) * delta_factor;
197 }
198 clutter_event_set_scroll_delta (event, delta_x, delta_y);
199
200 _clutter_xkb_translate_state (event, device->xkb, 0);
201
202 _clutter_event_push (event, FALSE);
203 }
204
205 static void
206 clutter_wayland_handle_keymap (void *data,
207 struct wl_keyboard *keyboard,
208 uint32_t format,
209 int32_t fd,
210 uint32_t size)
211 {
212 ClutterInputDeviceWayland *device = data;
213 struct xkb_context *ctx;
214 struct xkb_keymap *keymap;
215 char *map_str;
216
217 if (device->xkb)
218 {
219 xkb_state_unref (device->xkb);
220 device->xkb = NULL;
221 }
222
223 ctx = xkb_context_new (0);
224 if (!ctx)
225 {
226 close (fd);
227 return;
228 }
229
230 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
231 {
232 close (fd);
233 return;
234 }
235
236 map_str = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
237 if (map_str == MAP_FAILED)
238 {
239 close (fd);
240 return;
241 }
242
243 keymap = xkb_map_new_from_string (ctx,
244 map_str,
245 XKB_KEYMAP_FORMAT_TEXT_V1,
246 0);
247 xkb_context_unref (ctx);
248 munmap (map_str, size);
249 close (fd);
250
251 if (!keymap)
252 {
253 g_warning ("failed to compile keymap\n");
254 return;
255 }
256
257 device->xkb = xkb_state_new (keymap);
258 xkb_map_unref (keymap);
259 if (!device->xkb)
260 {
261 g_warning ("failed to create XKB state object\n");
262 return;
263 }
264 }
265
266 /* XXX: Need a wl_keyboard event to harmonise these across clients. */
267 #define KEY_REPEAT_DELAY 660
268 #define KEY_REPEAT_INTERVAL 40
269
270 static gboolean
271 clutter_wayland_repeat_key (void *data)
272 {
273 ClutterInputDeviceWayland *device = data;
274 ClutterStageCogl *stage_cogl = device->keyboard_focus;
275 ClutterEvent *event;
276
277 event = _clutter_key_event_new_from_evdev ((ClutterInputDevice *) device,
278 (ClutterInputDevice*) device,
279 stage_cogl->wrapper,
280 device->xkb, 0,
281 device->repeat_time,
282 device->repeat_key,
283 1);
284 device->repeat_time += KEY_REPEAT_INTERVAL;
285 _clutter_event_push (event, FALSE);
286
287 if (!device->is_initial_repeat)
288 return TRUE;
289
290 g_source_remove (device->repeat_source);
291 device->repeat_source = g_timeout_add (KEY_REPEAT_INTERVAL,
292 clutter_wayland_repeat_key,
293 device);
294 device->is_initial_repeat = FALSE;
295
296 return FALSE;
297 }
298
299 static void
300 clutter_wayland_handle_key (void *data,
301 struct wl_keyboard *keyboard,
302 uint32_t serial, uint32_t _time,
303 uint32_t key, uint32_t state)
304 {
305 ClutterInputDeviceWayland *device = data;
306 ClutterStageCogl *stage_cogl = device->keyboard_focus;
307 ClutterEvent *event;
308
309 if (!device->keyboard_focus)
310 return;
311 if (!device->xkb)
312 return;
313
314 event = _clutter_key_event_new_from_evdev ((ClutterInputDevice *) device,
315 (ClutterInputDevice *) device,
316 stage_cogl->wrapper,
317 device->xkb, 0,
318 _clutter_wayland_get_time (),
319 key, state);
320
321 _clutter_event_push (event, FALSE);
322
323 if (!xkb_key_repeats (xkb_state_get_map (device->xkb), key))
324 return;
325
326 if (state)
327 {
328 if (device->repeat_key != XKB_KEYCODE_INVALID)
329 g_source_remove (device->repeat_source);
330 device->repeat_key = key;
331 device->repeat_time = _time + KEY_REPEAT_DELAY;
332 device->repeat_source = g_timeout_add (KEY_REPEAT_DELAY,
333 clutter_wayland_repeat_key,
334 device);
335 device->is_initial_repeat = TRUE;
336 }
337 else if (device->repeat_key == key)
338 {
339 g_source_remove (device->repeat_source);
340 device->repeat_key = XKB_KEYCODE_INVALID;
341 }
342 }
343
344 static void
345 clutter_wayland_handle_modifiers (void *data,
346 struct wl_keyboard *keyboard,
347 uint32_t serial,
348 uint32_t mods_depressed,
349 uint32_t mods_latched,
350 uint32_t mods_locked,
351 uint32_t group)
352 {
353 ClutterInputDeviceWayland *device = data;
354
355 if (!device->xkb)
356 return;
357
358 xkb_state_update_mask (device->xkb,
359 mods_depressed,
360 mods_latched,
361 mods_locked,
362 0,
363 0,
364 group);
365 }
366
367 static void
368 clutter_wayland_handle_pointer_enter (void *data,
369 struct wl_pointer *pointer,
370 uint32_t serial,
371 struct wl_surface *surface,
372 wl_fixed_t x, wl_fixed_t y)
373 {
374 ClutterInputDeviceWayland *device = data;
375 ClutterStageWayland *stage_wayland;
376 ClutterStageCogl *stage_cogl;
377 ClutterEvent *event;
378 ClutterBackend *backend;
379 ClutterBackendWayland *backend_wayland;
380
381 stage_wayland = wl_surface_get_user_data (surface);
382
383 if (!CLUTTER_IS_STAGE_COGL (stage_wayland))
384 return;
385 stage_cogl = CLUTTER_STAGE_COGL (stage_wayland);
386
387 device->pointer_focus = stage_cogl;
388 _clutter_input_device_set_stage (CLUTTER_INPUT_DEVICE (device),
389 stage_cogl->wrapper);
390
391 event = clutter_event_new (CLUTTER_ENTER);
392 event->crossing.stage = stage_cogl->wrapper;
393 event->crossing.time = _clutter_wayland_get_time ();
394 event->crossing.x = wl_fixed_to_double (x);
395 event->crossing.y = wl_fixed_to_double (y);
396 event->crossing.source = CLUTTER_ACTOR (stage_cogl->wrapper);
397 event->crossing.device = CLUTTER_INPUT_DEVICE (device);
398
399 device->x = event->crossing.x;
400 device->y = event->crossing.y;
401
402 _clutter_event_push (event, FALSE);
403
404 if (stage_wayland->cursor_visible)
405 {
406 /* Set the cursor to the cursor loaded at backend initialisation */
407 backend = clutter_get_default_backend ();
408 backend_wayland = CLUTTER_BACKEND_WAYLAND (backend);
409
410 _clutter_backend_wayland_ensure_cursor (backend_wayland);
411
412 wl_pointer_set_cursor (pointer,
413 serial,
414 backend_wayland->cursor_surface,
415 backend_wayland->cursor_x,
416 backend_wayland->cursor_y);
417 wl_surface_attach (backend_wayland->cursor_surface,
418 backend_wayland->cursor_buffer,
419 0,
420 0);
421 wl_surface_damage (backend_wayland->cursor_surface,
422 0,
423 0,
424 32, /* XXX: FFS */
425 32);
426
427 wl_surface_commit (backend_wayland->cursor_surface);
428 }
429 else
430 {
431 wl_pointer_set_cursor (pointer, serial, NULL, 0, 0);
432 }
433 }
434
435 static void
436 clutter_wayland_handle_pointer_leave (void *data,
437 struct wl_pointer *pointer,
438 uint32_t serial,
439 struct wl_surface *surface)
440 {
441 ClutterInputDeviceWayland *device = data;
442 ClutterStageCogl *stage_cogl;
443 ClutterEvent *event;
444
445 if (surface == NULL)
446 return;
447
448 if (!CLUTTER_IS_STAGE_COGL (wl_surface_get_user_data (surface)))
449 return;
450
451 stage_cogl = wl_surface_get_user_data (surface);
452 g_assert (device->pointer_focus == stage_cogl);
453
454 event = clutter_event_new (CLUTTER_LEAVE);
455 event->crossing.stage = stage_cogl->wrapper;
456 event->crossing.time = _clutter_wayland_get_time ();
457 event->crossing.x = device->x;
458 event->crossing.y = device->y;
459 event->crossing.source = CLUTTER_ACTOR (stage_cogl->wrapper);
460 event->crossing.device = CLUTTER_INPUT_DEVICE (device);
461
462 _clutter_event_push (event, FALSE);
463
464 device->pointer_focus = NULL;
465 _clutter_input_device_set_stage (CLUTTER_INPUT_DEVICE (device), NULL);
466 }
467
468 static void
469 clutter_wayland_handle_keyboard_enter (void *data,
470 struct wl_keyboard *keyboard,
471 uint32_t serial,
472 struct wl_surface *surface,
473 struct wl_array *keys)
474 {
475 ClutterInputDeviceWayland *device = data;
476 ClutterStageCogl *stage_cogl;
477
478 if (surface == NULL)
479 return;
480
481 if (!CLUTTER_IS_STAGE_COGL (wl_surface_get_user_data (surface)))
482 return;
483
484 stage_cogl = wl_surface_get_user_data (surface);
485 g_assert (device->keyboard_focus == NULL);
486 device->keyboard_focus = stage_cogl;
487
488 _clutter_stage_update_state (stage_cogl->wrapper,
489 0,
490 CLUTTER_STAGE_STATE_ACTIVATED);
491 }
492
493 static void
494 clutter_wayland_handle_keyboard_leave (void *data,
495 struct wl_keyboard *keyboard,
496 uint32_t serial,
497 struct wl_surface *surface)
498 {
499 ClutterInputDeviceWayland *device = data;
500 ClutterStageCogl *stage_cogl;
501
502 if (surface == NULL)
503 return;
504
505 if (!CLUTTER_IS_STAGE_COGL (wl_surface_get_user_data (surface)))
506 return;
507
508 stage_cogl = wl_surface_get_user_data (surface);
509 g_assert (device->keyboard_focus == stage_cogl);
510
511 _clutter_stage_update_state (stage_cogl->wrapper,
512 CLUTTER_STAGE_STATE_ACTIVATED,
513 0);
514
515 if (device->repeat_key != XKB_KEYCODE_INVALID)
516 {
517 g_source_remove (device->repeat_source);
518 device->repeat_key = XKB_KEYCODE_INVALID;
519 }
520
521 device->keyboard_focus = NULL;
522 }
523
524 static const struct wl_keyboard_listener _clutter_keyboard_wayland_listener = {
525 clutter_wayland_handle_keymap,
526 clutter_wayland_handle_keyboard_enter,
527 clutter_wayland_handle_keyboard_leave,
528 clutter_wayland_handle_key,
529 clutter_wayland_handle_modifiers,
530 };
531
532 static const struct wl_pointer_listener _clutter_pointer_wayland_listener = {
533 clutter_wayland_handle_pointer_enter,
534 clutter_wayland_handle_pointer_leave,
535 clutter_wayland_handle_motion,
536 clutter_wayland_handle_button,
537 clutter_wayland_handle_axis,
538 };
539
540 static void
541 clutter_wayland_handle_seat (void *data,
542 struct wl_seat *seat,
543 uint32_t capabilities)
544 {
545 ClutterInputDeviceWayland *device = data;
546
547 /* XXX: Needs to handle removals too. */
548
549 if ((capabilities & WL_SEAT_CAPABILITY_POINTER) && !device->has_pointer)
550 {
551 struct wl_pointer *pointer;
552
553 pointer = wl_seat_get_pointer (seat);
554 if (pointer)
555 {
556 wl_pointer_add_listener (pointer,
557 &_clutter_pointer_wayland_listener,
558 device);
559 wl_pointer_set_user_data (pointer, device);
560 device->has_pointer = 1;
561 }
562 }
563
564 if ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && !device->has_keyboard)
565 {
566 struct wl_keyboard *keyboard;
567
568 keyboard = wl_seat_get_keyboard (seat);
569 if (keyboard)
570 {
571 wl_keyboard_add_listener (keyboard,
572 &_clutter_keyboard_wayland_listener,
573 device);
574 wl_keyboard_set_user_data (keyboard, device);
575 device->has_keyboard = 1;
576 }
577 }
578 }
579
580 const struct wl_seat_listener _clutter_seat_wayland_listener = {
581 clutter_wayland_handle_seat,
582 };
583
584 static gboolean
585 clutter_input_device_wayland_keycode_to_evdev (ClutterInputDevice *device,
586 guint hardware_keycode,
587 guint *evdev_keycode)
588 {
589 /* The hardware keycodes from the wayland backend are already evdev
590 keycodes */
591 *evdev_keycode = hardware_keycode;
592 return TRUE;
593 }
594
595 static void
596 clutter_input_device_wayland_class_init (ClutterInputDeviceWaylandClass *klass)
597 {
598 klass->keycode_to_evdev = clutter_input_device_wayland_keycode_to_evdev;
599 }
600
601 static void
602 clutter_input_device_wayland_init (ClutterInputDeviceWayland *self)
603 {
604 self->repeat_key = XKB_KEYCODE_INVALID;
605 }
606
607 /**
608 * clutter_wayland_input_device_get_wl_seat: (skip)
609 * @device: a #ClutterInputDevice
610 *
611 * Access the underlying data structure representing the Wayland device that is
612 * backing this #ClutterInputDevice.
613 *
614 * Note: this function can only be called when running on the Wayland platform.
615 * Calling this function at any other time will return %NULL.
616 *
617 * Returns: (transfer none): the Wayland input device associated with the
618 * @device
619 *
620 * Since: 1.10
621 */
622 struct wl_seat *
623 clutter_wayland_input_device_get_wl_seat (ClutterInputDevice *device)
624 {
625 ClutterInputDeviceWayland *wayland_device;
626
627 if (!CLUTTER_INPUT_DEVICE_WAYLAND (device))
628 return NULL;
629
630 wayland_device = CLUTTER_INPUT_DEVICE_WAYLAND (device);
631
632 return wayland_device->input_device;
633 }