Debian Codegraph @

clutter/evdev/clutter-device-manager-evdev.c

raw
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Copyright (C) 2010 Intel Corp.
7 * Copyright (C) 2014 Jonas Ådahl
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * Author: Damien Lespiau <[email protected]>
23 * Author: Jonas Ådahl <[email protected]>
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <math.h>
31 #include <float.h>
32 #include <linux/input.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include <glib.h>
41 #include <libinput.h>
42
43 #include "clutter-backend.h"
44 #include "clutter-debug.h"
45 #include "clutter-device-manager.h"
46 #include "clutter-device-manager-private.h"
47 #include "clutter-event-private.h"
48 #include "clutter-input-device-evdev.h"
49 #include "clutter-main.h"
50 #include "clutter-private.h"
51 #include "clutter-stage-manager.h"
52 #include "clutter-xkb-utils.h"
53 #include "clutter-backend-private.h"
54 #include "clutter-evdev.h"
55 #include "clutter-stage-private.h"
56
57 #include "clutter-device-manager-evdev.h"
58
59 #define DISCRETE_SCROLL_STEP 10.0
60
61 #define AUTOREPEAT_VALUE 2
62
63 /* Try to keep the pointer inside the stage. Hopefully no one is using
64 * this backend with stages smaller than this. */
65 #define INITIAL_POINTER_X 16
66 #define INITIAL_POINTER_Y 16
67
68 /*
69 * Clutter makes the assumption that two core devices have ID's 2 and 3 (core
70 * pointer and core keyboard).
71 *
72 * Since the two first devices that will ever be created will be the virtual
73 * pointer and virtual keyboard of the first seat, we fulfill the made
74 * assumptions by having the first device having ID 2 and following 3.
75 */
76 #define INITIAL_DEVICE_ID 2
77
78 typedef struct _ClutterTouchState ClutterTouchState;
79 typedef struct _ClutterEventFilter ClutterEventFilter;
80
81 struct _ClutterTouchState
82 {
83 guint32 id;
84 ClutterPoint coords;
85 };
86
87 struct _ClutterSeatEvdev
88 {
89 struct libinput_seat *libinput_seat;
90 ClutterDeviceManagerEvdev *manager_evdev;
91
92 GSList *devices;
93
94 ClutterInputDevice *core_pointer;
95 ClutterInputDevice *core_keyboard;
96
97 GHashTable *touches;
98
99 struct xkb_state *xkb;
100 xkb_led_index_t caps_lock_led;
101 xkb_led_index_t num_lock_led;
102 xkb_led_index_t scroll_lock_led;
103 uint32_t button_state;
104
105 /* keyboard repeat */
106 gboolean repeat;
107 guint32 repeat_delay;
108 guint32 repeat_interval;
109 guint32 repeat_key;
110 guint32 repeat_count;
111 guint32 repeat_timer;
112 ClutterInputDevice *repeat_device;
113
114 gfloat pointer_x;
115 gfloat pointer_y;
116
117 /* Emulation of discrete scroll events out of smooth ones */
118 gfloat accum_scroll_dx;
119 gfloat accum_scroll_dy;
120 };
121
122 struct _ClutterEventFilter
123 {
124 ClutterEvdevFilterFunc func;
125 gpointer data;
126 GDestroyNotify destroy_notify;
127 };
128
129 typedef struct _ClutterEventSource ClutterEventSource;
130
131 struct _ClutterDeviceManagerEvdevPrivate
132 {
133 struct libinput *libinput;
134
135 ClutterStage *stage;
136 gboolean released;
137
138 ClutterEventSource *event_source;
139
140 GSList *devices;
141 GSList *seats;
142
143 ClutterSeatEvdev *main_seat;
144 struct xkb_keymap *keymap;
145
146 ClutterPointerConstrainCallback constrain_callback;
147 gpointer constrain_data;
148 GDestroyNotify constrain_data_notify;
149
150 ClutterStageManager *stage_manager;
151 guint stage_added_handler;
152 guint stage_removed_handler;
153
154 GSList *event_filters;
155
156 gint device_id_next;
157 GList *free_device_ids;
158 };
159
160 static void clutter_device_manager_evdev_event_extender_init (ClutterEventExtenderInterface *iface);
161
162 G_DEFINE_TYPE_WITH_CODE (ClutterDeviceManagerEvdev,
163 clutter_device_manager_evdev,
164 CLUTTER_TYPE_DEVICE_MANAGER,
165 G_ADD_PRIVATE (ClutterDeviceManagerEvdev)
166 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_EVENT_EXTENDER,
167 clutter_device_manager_evdev_event_extender_init))
168
169 static ClutterOpenDeviceCallback device_open_callback;
170 static ClutterCloseDeviceCallback device_close_callback;
171 static gpointer device_callback_data;
172
173 #ifdef CLUTTER_ENABLE_DEBUG
174 static const char *device_type_str[] = {
175 "pointer", /* CLUTTER_POINTER_DEVICE */
176 "keyboard", /* CLUTTER_KEYBOARD_DEVICE */
177 "extension", /* CLUTTER_EXTENSION_DEVICE */
178 "joystick", /* CLUTTER_JOYSTICK_DEVICE */
179 "tablet", /* CLUTTER_TABLET_DEVICE */
180 "touchpad", /* CLUTTER_TOUCHPAD_DEVICE */
181 "touchscreen", /* CLUTTER_TOUCHSCREEN_DEVICE */
182 "pen", /* CLUTTER_PEN_DEVICE */
183 "eraser", /* CLUTTER_ERASER_DEVICE */
184 "cursor", /* CLUTTER_CURSOR_DEVICE */
185 };
186 #endif /* CLUTTER_ENABLE_DEBUG */
187
188 /*
189 * ClutterEventSource management
190 *
191 * The device manager is responsible for managing the GSource when devices
192 * appear and disappear from the system.
193 */
194
195 static const char *option_xkb_layout = "us";
196 static const char *option_xkb_variant = "";
197 static const char *option_xkb_options = "";
198
199 static inline guint64
200 us (guint64 us)
201 {
202 return us;
203 }
204
205 static inline guint64
206 ms2us (guint64 ms)
207 {
208 return us (ms * 1000);
209 }
210
211 static inline guint32
212 us2ms (guint64 us)
213 {
214 return (guint32) (us / 1000);
215 }
216
217 static void
218 clutter_device_manager_evdev_copy_event_data (ClutterEventExtender *event_extender,
219 const ClutterEvent *src,
220 ClutterEvent *dest)
221 {
222 ClutterEventEvdev *event_evdev;
223
224 event_evdev = _clutter_event_get_platform_data (src);
225 if (event_evdev != NULL)
226 _clutter_event_set_platform_data (dest, _clutter_event_evdev_copy (event_evdev));
227 }
228
229 static void
230 clutter_device_manager_evdev_free_event_data (ClutterEventExtender *event_extender,
231 ClutterEvent *event)
232 {
233 ClutterEventEvdev *event_evdev;
234
235 event_evdev = _clutter_event_get_platform_data (event);
236 if (event_evdev != NULL)
237 _clutter_event_evdev_free (event_evdev);
238 }
239
240 static void
241 clutter_device_manager_evdev_event_extender_init (ClutterEventExtenderInterface *iface)
242 {
243 iface->copy_event_data = clutter_device_manager_evdev_copy_event_data;
244 iface->free_event_data = clutter_device_manager_evdev_free_event_data;
245 }
246
247 /*
248 * ClutterEventSource for reading input devices
249 */
250
251 struct _ClutterEventSource
252 {
253 GSource source;
254
255 ClutterDeviceManagerEvdev *manager_evdev;
256 GPollFD event_poll_fd;
257 };
258
259 static void
260 process_events (ClutterDeviceManagerEvdev *manager_evdev);
261
262 static gboolean
263 clutter_event_prepare (GSource *source,
264 gint *timeout)
265 {
266 gboolean retval;
267
268 _clutter_threads_acquire_lock ();
269
270 *timeout = -1;
271 retval = clutter_events_pending ();
272
273 _clutter_threads_release_lock ();
274
275 return retval;
276 }
277
278 static gboolean
279 clutter_event_check (GSource *source)
280 {
281 ClutterEventSource *event_source = (ClutterEventSource *) source;
282 gboolean retval;
283
284 _clutter_threads_acquire_lock ();
285
286 retval = ((event_source->event_poll_fd.revents & G_IO_IN) ||
287 clutter_events_pending ());
288
289 _clutter_threads_release_lock ();
290
291 return retval;
292 }
293
294 static void
295 queue_event (ClutterEvent *event)
296 {
297 _clutter_event_push (event, FALSE);
298 }
299
300 static void
301 clear_repeat_timer (ClutterSeatEvdev *seat)
302 {
303 if (seat->repeat_timer)
304 {
305 g_source_remove (seat->repeat_timer);
306 seat->repeat_timer = 0;
307 g_clear_object (&seat->repeat_device);
308 }
309 }
310
311 static gboolean
312 keyboard_repeat (gpointer data);
313
314 static void
315 clutter_seat_evdev_sync_leds (ClutterSeatEvdev *seat);
316
317 static void
318 notify_key_device (ClutterInputDevice *input_device,
319 guint64 time_us,
320 guint32 key,
321 guint32 state,
322 gboolean update_keys)
323 {
324 ClutterInputDeviceEvdev *device_evdev =
325 CLUTTER_INPUT_DEVICE_EVDEV (input_device);
326 ClutterSeatEvdev *seat = _clutter_input_device_evdev_get_seat (device_evdev);
327 ClutterStage *stage;
328 ClutterEvent *event = NULL;
329 enum xkb_state_component changed_state;
330
331 /* We can drop the event on the floor if no stage has been
332 * associated with the device yet. */
333 stage = _clutter_input_device_get_stage (input_device);
334 if (stage == NULL)
335 {
336 clear_repeat_timer (seat);
337 return;
338 }
339
340 event = _clutter_key_event_new_from_evdev (input_device,
341 seat->core_keyboard,
342 stage,
343 seat->xkb,
344 seat->button_state,
345 us2ms (time_us), key, state);
346 _clutter_evdev_event_set_event_code (event, key);
347
348 /* We must be careful and not pass multiple releases to xkb, otherwise it gets
349 confused and locks the modifiers */
350 if (state != AUTOREPEAT_VALUE)
351 {
352 changed_state = xkb_state_update_key (seat->xkb,
353 event->key.hardware_keycode,
354 state ? XKB_KEY_DOWN : XKB_KEY_UP);
355 }
356 else
357 {
358 changed_state = 0;
359 clutter_event_set_flags (event, CLUTTER_EVENT_FLAG_SYNTHETIC);
360 }
361
362 queue_event (event);
363
364 if (update_keys && (changed_state & XKB_STATE_LEDS))
365 clutter_seat_evdev_sync_leds (seat);
366
367 if (state == 0 || /* key release */
368 !seat->repeat ||
369 !xkb_keymap_key_repeats (xkb_state_get_keymap (seat->xkb), event->key.hardware_keycode))
370 {
371 clear_repeat_timer (seat);
372 return;
373 }
374
375 if (state == 1) /* key press */
376 seat->repeat_count = 0;
377
378 seat->repeat_count += 1;
379 seat->repeat_key = key;
380
381 switch (seat->repeat_count)
382 {
383 case 1:
384 case 2:
385 {
386 guint32 interval;
387
388 clear_repeat_timer (seat);
389 seat->repeat_device = g_object_ref (input_device);
390
391 if (seat->repeat_count == 1)
392 interval = seat->repeat_delay;
393 else
394 interval = seat->repeat_interval;
395
396 seat->repeat_timer =
397 clutter_threads_add_timeout_full (CLUTTER_PRIORITY_EVENTS,
398 interval,
399 keyboard_repeat,
400 seat,
401 NULL);
402 return;
403 }
404 default:
405 return;
406 }
407 }
408
409 static gboolean
410 keyboard_repeat (gpointer data)
411 {
412 ClutterSeatEvdev *seat = data;
413 GSource *source;
414 guint32 time_ms;
415
416 g_return_val_if_fail (seat->repeat_device != NULL, G_SOURCE_REMOVE);
417 source = g_main_context_find_source_by_id (NULL, seat->repeat_timer);
418 time_ms = g_source_get_time (source) / 1000;
419
420 notify_key_device (seat->repeat_device,
421 ms2us (time_ms),
422 seat->repeat_key,
423 AUTOREPEAT_VALUE,
424 FALSE);
425
426 return G_SOURCE_CONTINUE;
427 }
428
429 static ClutterEvent *
430 new_absolute_motion_event (ClutterInputDevice *input_device,
431 guint64 time_us,
432 gfloat x,
433 gfloat y)
434 {
435 gfloat stage_width, stage_height;
436 ClutterDeviceManagerEvdev *manager_evdev;
437 ClutterInputDeviceEvdev *device_evdev;
438 ClutterSeatEvdev *seat;
439 ClutterStage *stage;
440 ClutterEvent *event = NULL;
441
442 stage = _clutter_input_device_get_stage (input_device);
443 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (input_device);
444 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (input_device->device_manager);
445 seat = _clutter_input_device_evdev_get_seat (device_evdev);
446
447 stage_width = clutter_actor_get_width (CLUTTER_ACTOR (stage));
448 stage_height = clutter_actor_get_height (CLUTTER_ACTOR (stage));
449
450 event = clutter_event_new (CLUTTER_MOTION);
451
452 if (manager_evdev->priv->constrain_callback)
453 {
454 manager_evdev->priv->constrain_callback (seat->core_pointer,
455 us2ms (time_us),
456 seat->pointer_x,
457 seat->pointer_y,
458 &x, &y,
459 manager_evdev->priv->constrain_data);
460 }
461 else
462 {
463 x = CLAMP (x, 0.f, stage_width - 1);
464 y = CLAMP (y, 0.f, stage_height - 1);
465 }
466
467 _clutter_evdev_event_set_time_usec (event, time_us);
468 event->motion.time = us2ms (time_us);
469 event->motion.stage = stage;
470 event->motion.device = seat->core_pointer;
471 _clutter_xkb_translate_state (event, seat->xkb, seat->button_state);
472 event->motion.x = x;
473 event->motion.y = y;
474 clutter_event_set_device (event, seat->core_pointer);
475 clutter_event_set_source_device (event, input_device);
476
477 _clutter_input_device_set_stage (seat->core_pointer, stage);
478
479 seat->pointer_x = x;
480 seat->pointer_y = y;
481
482 return event;
483 }
484
485 static void
486 notify_absolute_motion (ClutterInputDevice *input_device,
487 guint64 time_us,
488 gfloat x,
489 gfloat y)
490 {
491 ClutterEvent *event;
492
493 event = new_absolute_motion_event (input_device, time_us, x, y);
494
495 queue_event (event);
496 }
497
498 static void
499 notify_relative_motion (ClutterInputDevice *input_device,
500 struct libinput_event_pointer *pointer_event)
501 {
502 guint64 time_us;
503 double dx;
504 double dy;
505 double dx_unaccel;
506 double dy_unaccel;
507 gfloat new_x, new_y;
508 ClutterInputDeviceEvdev *device_evdev;
509 ClutterSeatEvdev *seat;
510 ClutterEvent *event;
511
512 /* We can drop the event on the floor if no stage has been
513 * associated with the device yet. */
514 if (!_clutter_input_device_get_stage (input_device))
515 return;
516
517 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (input_device);
518 seat = _clutter_input_device_evdev_get_seat (device_evdev);
519
520 dx = libinput_event_pointer_get_dx (pointer_event);
521 dy = libinput_event_pointer_get_dy (pointer_event);
522 new_x = seat->pointer_x + dx;
523 new_y = seat->pointer_y + dy;
524
525 time_us = libinput_event_pointer_get_time_usec (pointer_event);
526 event = new_absolute_motion_event (input_device, time_us, new_x, new_y);
527
528 dx_unaccel = libinput_event_pointer_get_dx_unaccelerated (pointer_event);
529 dy_unaccel = libinput_event_pointer_get_dy_unaccelerated (pointer_event);
530 _clutter_evdev_event_set_relative_motion (event,
531 dx, dy,
532 dx_unaccel, dy_unaccel);
533
534 queue_event (event);
535 }
536
537 static ClutterScrollDirection
538 discrete_to_direction (gdouble discrete_x,
539 gdouble discrete_y)
540 {
541 if (discrete_x > 0)
542 return CLUTTER_SCROLL_RIGHT;
543 else if (discrete_x < 0)
544 return CLUTTER_SCROLL_LEFT;
545 else if (discrete_y > 0)
546 return CLUTTER_SCROLL_DOWN;
547 else if (discrete_y < 0)
548 return CLUTTER_SCROLL_UP;
549 else
550 return CLUTTER_SCROLL_SMOOTH;
551 }
552
553 static void
554 notify_discrete_scroll (ClutterInputDevice *input_device,
555 guint64 time_us,
556 ClutterScrollDirection direction,
557 ClutterScrollSource scroll_source,
558 gboolean emulated)
559 {
560 ClutterInputDeviceEvdev *device_evdev;
561 ClutterSeatEvdev *seat;
562 ClutterStage *stage;
563 ClutterEvent *event = NULL;
564
565 if (direction == CLUTTER_SCROLL_SMOOTH)
566 return;
567
568 /* We can drop the event on the floor if no stage has been
569 * associated with the device yet. */
570 stage = _clutter_input_device_get_stage (input_device);
571 if (stage == NULL)
572 return;
573
574 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (input_device);
575 seat = _clutter_input_device_evdev_get_seat (device_evdev);
576
577 event = clutter_event_new (CLUTTER_SCROLL);
578
579 _clutter_evdev_event_set_time_usec (event, time_us);
580 event->scroll.time = us2ms (time_us);
581 event->scroll.stage = CLUTTER_STAGE (stage);
582 event->scroll.device = seat->core_pointer;
583 _clutter_xkb_translate_state (event, seat->xkb, seat->button_state);
584
585 event->scroll.direction = direction;
586
587 event->scroll.x = seat->pointer_x;
588 event->scroll.y = seat->pointer_y;
589 clutter_event_set_device (event, seat->core_pointer);
590 clutter_event_set_source_device (event, input_device);
591 event->scroll.scroll_source = scroll_source;
592
593 _clutter_event_set_pointer_emulated (event, emulated);
594
595 queue_event (event);
596 }
597
598 static void
599 notify_scroll (ClutterInputDevice *input_device,
600 guint64 time_us,
601 gdouble dx,
602 gdouble dy,
603 ClutterScrollSource source,
604 ClutterScrollFinishFlags flags,
605 gboolean emulated)
606 {
607 ClutterInputDeviceEvdev *device_evdev;
608 ClutterSeatEvdev *seat;
609 ClutterStage *stage;
610 ClutterEvent *event = NULL;
611 gdouble scroll_factor;
612
613 /* We can drop the event on the floor if no stage has been
614 * associated with the device yet. */
615 stage = _clutter_input_device_get_stage (input_device);
616 if (stage == NULL)
617 return;
618
619 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (input_device);
620 seat = _clutter_input_device_evdev_get_seat (device_evdev);
621
622 event = clutter_event_new (CLUTTER_SCROLL);
623
624 _clutter_evdev_event_set_time_usec (event, time_us);
625 event->scroll.time = us2ms (time_us);
626 event->scroll.stage = CLUTTER_STAGE (stage);
627 event->scroll.device = seat->core_pointer;
628 _clutter_xkb_translate_state (event, seat->xkb, seat->button_state);
629
630 /* libinput pointer axis events are in pointer motion coordinate space.
631 * To convert to Xi2 discrete step coordinate space, multiply the factor
632 * 1/10. */
633 event->scroll.direction = CLUTTER_SCROLL_SMOOTH;
634 scroll_factor = 1.0 / DISCRETE_SCROLL_STEP;
635 clutter_event_set_scroll_delta (event,
636 scroll_factor * dx,
637 scroll_factor * dy);
638
639 event->scroll.x = seat->pointer_x;
640 event->scroll.y = seat->pointer_y;
641 clutter_event_set_device (event, seat->core_pointer);
642 clutter_event_set_source_device (event, input_device);
643 event->scroll.scroll_source = source;
644 event->scroll.finish_flags = flags;
645
646 _clutter_event_set_pointer_emulated (event, emulated);
647
648 queue_event (event);
649 }
650
651 static void
652 notify_button (ClutterInputDevice *input_device,
653 guint64 time_us,
654 guint32 button,
655 guint32 state)
656 {
657 ClutterInputDeviceEvdev *device_evdev;
658 ClutterSeatEvdev *seat;
659 ClutterStage *stage;
660 ClutterEvent *event = NULL;
661 gint button_nr;
662 static gint maskmap[8] =
663 {
664 CLUTTER_BUTTON1_MASK, CLUTTER_BUTTON3_MASK, CLUTTER_BUTTON2_MASK,
665 CLUTTER_BUTTON4_MASK, CLUTTER_BUTTON5_MASK, 0, 0, 0
666 };
667
668 /* We can drop the event on the floor if no stage has been
669 * associated with the device yet. */
670 stage = _clutter_input_device_get_stage (input_device);
671 if (stage == NULL)
672 return;
673
674 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (input_device);
675 seat = _clutter_input_device_evdev_get_seat (device_evdev);
676
677 /* The evdev button numbers don't map sequentially to clutter button
678 * numbers (the right and middle mouse buttons are in the opposite
679 * order) so we'll map them directly with a switch statement */
680 switch (button)
681 {
682 case BTN_LEFT:
683 button_nr = CLUTTER_BUTTON_PRIMARY;
684 break;
685
686 case BTN_RIGHT:
687 button_nr = CLUTTER_BUTTON_SECONDARY;
688 break;
689
690 case BTN_MIDDLE:
691 button_nr = CLUTTER_BUTTON_MIDDLE;
692 break;
693
694 default:
695 /* For compatibility reasons, all additional buttons go after the old 4-7 scroll ones */
696 button_nr = button - (BTN_LEFT - 1) + 4;
697 break;
698 }
699
700 if (button_nr < 1 || button_nr > 12)
701 {
702 g_warning ("Unhandled button event 0x%x", button);
703 return;
704 }
705
706 if (state)
707 event = clutter_event_new (CLUTTER_BUTTON_PRESS);
708 else
709 event = clutter_event_new (CLUTTER_BUTTON_RELEASE);
710
711 /* Update the modifiers */
712 if (state)
713 seat->button_state |= maskmap[button - BTN_LEFT];
714 else
715 seat->button_state &= ~maskmap[button - BTN_LEFT];
716
717 _clutter_evdev_event_set_time_usec (event, time_us);
718 event->button.time = us2ms (time_us);
719 event->button.stage = CLUTTER_STAGE (stage);
720 event->button.device = seat->core_pointer;
721 _clutter_xkb_translate_state (event, seat->xkb, seat->button_state);
722 event->button.button = button_nr;
723 event->button.x = seat->pointer_x;
724 event->button.y = seat->pointer_y;
725 clutter_event_set_device (event, seat->core_pointer);
726 clutter_event_set_source_device (event, input_device);
727
728 _clutter_evdev_event_set_event_code (event, button);
729
730 _clutter_input_device_set_stage (seat->core_pointer, stage);
731
732 queue_event (event);
733 }
734
735 static void
736 notify_touch_event (ClutterInputDevice *input_device,
737 ClutterEventType evtype,
738 guint64 time_us,
739 gint32 slot,
740 gdouble x,
741 gdouble y)
742 {
743 ClutterInputDeviceEvdev *device_evdev;
744 ClutterSeatEvdev *seat;
745 ClutterStage *stage;
746 ClutterEvent *event = NULL;
747
748 /* We can drop the event on the floor if no stage has been
749 * associated with the device yet. */
750 stage = _clutter_input_device_get_stage (input_device);
751 if (stage == NULL)
752 return;
753
754 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (input_device);
755 seat = _clutter_input_device_evdev_get_seat (device_evdev);
756
757 event = clutter_event_new (evtype);
758
759 _clutter_evdev_event_set_time_usec (event, time_us);
760 event->touch.time = us2ms (time_us);
761 event->touch.stage = CLUTTER_STAGE (stage);
762 event->touch.device = seat->core_pointer;
763 event->touch.x = x;
764 event->touch.y = y;
765 /* "NULL" sequences are special cased in clutter */
766 event->touch.sequence = GINT_TO_POINTER (slot + 1);
767 _clutter_xkb_translate_state (event, seat->xkb, seat->button_state);
768
769 if (evtype == CLUTTER_TOUCH_BEGIN ||
770 evtype == CLUTTER_TOUCH_UPDATE)
771 event->touch.modifier_state |= CLUTTER_BUTTON1_MASK;
772
773 clutter_event_set_device (event, seat->core_pointer);
774 clutter_event_set_source_device (event, input_device);
775
776 queue_event (event);
777 }
778
779 static void
780 notify_pinch_gesture_event (ClutterInputDevice *input_device,
781 ClutterTouchpadGesturePhase phase,
782 guint64 time_us,
783 gdouble dx,
784 gdouble dy,
785 gdouble angle_delta,
786 gdouble scale)
787 {
788 ClutterInputDeviceEvdev *device_evdev;
789 ClutterSeatEvdev *seat;
790 ClutterStage *stage;
791 ClutterEvent *event = NULL;
792 ClutterPoint pos;
793
794 /* We can drop the event on the floor if no stage has been
795 * associated with the device yet. */
796 stage = _clutter_input_device_get_stage (input_device);
797 if (stage == NULL)
798 return;
799
800 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (input_device);
801 seat = _clutter_input_device_evdev_get_seat (device_evdev);
802
803 event = clutter_event_new (CLUTTER_TOUCHPAD_PINCH);
804
805 clutter_input_device_get_coords (seat->core_pointer, NULL, &pos);
806
807 _clutter_evdev_event_set_time_usec (event, time_us);
808 event->touchpad_pinch.phase = phase;
809 event->touchpad_pinch.time = us2ms (time_us);
810 event->touchpad_pinch.stage = CLUTTER_STAGE (stage);
811 event->touchpad_pinch.x = pos.x;
812 event->touchpad_pinch.y = pos.y;
813 event->touchpad_pinch.dx = dx;
814 event->touchpad_pinch.dy = dy;
815 event->touchpad_pinch.angle_delta = angle_delta;
816 event->touchpad_pinch.scale = scale;
817
818 _clutter_xkb_translate_state (event, seat->xkb, seat->button_state);
819
820 clutter_event_set_device (event, seat->core_pointer);
821 clutter_event_set_source_device (event, input_device);
822
823 queue_event (event);
824 }
825
826 static void
827 notify_swipe_gesture_event (ClutterInputDevice *input_device,
828 ClutterTouchpadGesturePhase phase,
829 guint64 time_us,
830 guint n_fingers,
831 gdouble dx,
832 gdouble dy)
833 {
834 ClutterInputDeviceEvdev *device_evdev;
835 ClutterSeatEvdev *seat;
836 ClutterStage *stage;
837 ClutterEvent *event = NULL;
838 ClutterPoint pos;
839
840 /* We can drop the event on the floor if no stage has been
841 * associated with the device yet. */
842 stage = _clutter_input_device_get_stage (input_device);
843 if (stage == NULL)
844 return;
845
846 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (input_device);
847 seat = _clutter_input_device_evdev_get_seat (device_evdev);
848
849 event = clutter_event_new (CLUTTER_TOUCHPAD_SWIPE);
850
851 _clutter_evdev_event_set_time_usec (event, time_us);
852 event->touchpad_swipe.phase = phase;
853 event->touchpad_swipe.time = us2ms (time_us);
854 event->touchpad_swipe.stage = CLUTTER_STAGE (stage);
855
856 clutter_input_device_get_coords (seat->core_pointer, NULL, &pos);
857 event->touchpad_swipe.x = pos.x;
858 event->touchpad_swipe.y = pos.y;
859 event->touchpad_swipe.dx = dx;
860 event->touchpad_swipe.dy = dy;
861 event->touchpad_swipe.n_fingers = n_fingers;
862
863 _clutter_xkb_translate_state (event, seat->xkb, seat->button_state);
864
865 clutter_event_set_device (event, seat->core_pointer);
866 clutter_event_set_source_device (event, input_device);
867
868 queue_event (event);
869 }
870
871 static void
872 dispatch_libinput (ClutterDeviceManagerEvdev *manager_evdev)
873 {
874 ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv;
875
876 libinput_dispatch (priv->libinput);
877 process_events (manager_evdev);
878 }
879
880 static gboolean
881 clutter_event_dispatch (GSource *g_source,
882 GSourceFunc callback,
883 gpointer user_data)
884 {
885 ClutterEventSource *source = (ClutterEventSource *) g_source;
886 ClutterDeviceManagerEvdev *manager_evdev;
887 ClutterEvent *event;
888
889 _clutter_threads_acquire_lock ();
890
891 manager_evdev = source->manager_evdev;
892
893 /* Don't queue more events if we haven't finished handling the previous batch
894 */
895 if (clutter_events_pending ())
896 goto queue_event;
897
898 dispatch_libinput (manager_evdev);
899
900 queue_event:
901 event = clutter_event_get ();
902
903 if (event)
904 {
905 ClutterModifierType event_state;
906 ClutterInputDevice *input_device =
907 clutter_event_get_source_device (event);
908 ClutterInputDeviceEvdev *device_evdev =
909 CLUTTER_INPUT_DEVICE_EVDEV (input_device);
910 ClutterSeatEvdev *seat =
911 _clutter_input_device_evdev_get_seat (device_evdev);
912
913 /* Drop events if we don't have any stage to forward them to */
914 if (!_clutter_input_device_get_stage (input_device))
915 goto out;
916
917 /* forward the event into clutter for emission etc. */
918 _clutter_stage_queue_event (event->any.stage, event, FALSE);
919
920 /* update the device states *after* the event */
921 event_state = seat->button_state |
922 xkb_state_serialize_mods (seat->xkb, XKB_STATE_MODS_EFFECTIVE);
923 _clutter_input_device_set_state (seat->core_pointer, event_state);
924 _clutter_input_device_set_state (seat->core_keyboard, event_state);
925 }
926
927 out:
928 _clutter_threads_release_lock ();
929
930 return TRUE;
931 }
932 static GSourceFuncs event_funcs = {
933 clutter_event_prepare,
934 clutter_event_check,
935 clutter_event_dispatch,
936 NULL
937 };
938
939 static ClutterEventSource *
940 clutter_event_source_new (ClutterDeviceManagerEvdev *manager_evdev)
941 {
942 ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv;
943 GSource *source;
944 ClutterEventSource *event_source;
945 gint fd;
946
947 source = g_source_new (&event_funcs, sizeof (ClutterEventSource));
948 event_source = (ClutterEventSource *) source;
949
950 /* setup the source */
951 event_source->manager_evdev = manager_evdev;
952
953 fd = libinput_get_fd (priv->libinput);
954 event_source->event_poll_fd.fd = fd;
955 event_source->event_poll_fd.events = G_IO_IN;
956
957 /* and finally configure and attach the GSource */
958 g_source_set_priority (source, CLUTTER_PRIORITY_EVENTS);
959 g_source_add_poll (source, &event_source->event_poll_fd);
960 g_source_set_can_recurse (source, TRUE);
961 g_source_attach (source, NULL);
962
963 return event_source;
964 }
965
966 static void
967 clutter_event_source_free (ClutterEventSource *source)
968 {
969 GSource *g_source = (GSource *) source;
970
971 CLUTTER_NOTE (EVENT, "Removing GSource for evdev device manager");
972
973 /* ignore the return value of close, it's not like we can do something
974 * about it */
975 close (source->event_poll_fd.fd);
976
977 g_source_destroy (g_source);
978 g_source_unref (g_source);
979 }
980
981 static void
982 clutter_touch_state_free (ClutterTouchState *touch_state)
983 {
984 g_slice_free (ClutterTouchState, touch_state);
985 }
986
987 static void
988 clutter_seat_evdev_set_libinput_seat (ClutterSeatEvdev *seat,
989 struct libinput_seat *libinput_seat)
990 {
991 g_assert (seat->libinput_seat == NULL);
992
993 libinput_seat_ref (libinput_seat);
994 libinput_seat_set_user_data (libinput_seat, seat);
995 seat->libinput_seat = libinput_seat;
996 }
997
998 static ClutterSeatEvdev *
999 clutter_seat_evdev_new (ClutterDeviceManagerEvdev *manager_evdev)
1000 {
1001 ClutterDeviceManager *manager = CLUTTER_DEVICE_MANAGER (manager_evdev);
1002 ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv;
1003 ClutterSeatEvdev *seat;
1004 ClutterInputDevice *device;
1005 struct xkb_context *ctx;
1006 struct xkb_rule_names names;
1007 struct xkb_keymap *keymap;
1008
1009 seat = g_new0 (ClutterSeatEvdev, 1);
1010 if (!seat)
1011 return NULL;
1012
1013 device = _clutter_input_device_evdev_new_virtual (
1014 manager, seat, CLUTTER_POINTER_DEVICE);
1015 _clutter_input_device_set_stage (device, priv->stage);
1016 seat->pointer_x = INITIAL_POINTER_X;
1017 seat->pointer_y = INITIAL_POINTER_Y;
1018 _clutter_input_device_set_coords (device, NULL,
1019 seat->pointer_x, seat->pointer_y,
1020 NULL);
1021 _clutter_device_manager_add_device (manager, device);
1022 seat->core_pointer = device;
1023
1024 device = _clutter_input_device_evdev_new_virtual (
1025 manager, seat, CLUTTER_KEYBOARD_DEVICE);
1026 _clutter_input_device_set_stage (device, priv->stage);
1027 _clutter_device_manager_add_device (manager, device);
1028 seat->core_keyboard = device;
1029
1030 seat->touches = g_hash_table_new_full (NULL, NULL, NULL,
1031 (GDestroyNotify) clutter_touch_state_free);
1032
1033 ctx = xkb_context_new(0);
1034 g_assert (ctx);
1035
1036 names.rules = "evdev";
1037 names.model = "pc105";
1038 names.layout = option_xkb_layout;
1039 names.variant = option_xkb_variant;
1040 names.options = option_xkb_options;
1041
1042 keymap = xkb_keymap_new_from_names (ctx, &names, 0);
1043 xkb_context_unref(ctx);
1044 if (keymap)
1045 {
1046 seat->xkb = xkb_state_new (keymap);
1047
1048 seat->caps_lock_led =
1049 xkb_keymap_led_get_index (keymap, XKB_LED_NAME_CAPS);
1050 seat->num_lock_led =
1051 xkb_keymap_led_get_index (keymap, XKB_LED_NAME_NUM);
1052 seat->scroll_lock_led =
1053 xkb_keymap_led_get_index (keymap, XKB_LED_NAME_SCROLL);
1054
1055 priv->keymap = keymap;
1056 }
1057
1058 seat->repeat = TRUE;
1059 seat->repeat_delay = 250; /* ms */
1060 seat->repeat_interval = 33; /* ms */
1061
1062 priv->seats = g_slist_append (priv->seats, seat);
1063 return seat;
1064 }
1065
1066 static void
1067 clutter_seat_evdev_free (ClutterSeatEvdev *seat)
1068 {
1069 GSList *iter;
1070
1071 for (iter = seat->devices; iter; iter = g_slist_next (iter))
1072 {
1073 ClutterInputDevice *device = iter->data;
1074
1075 g_object_unref (device);
1076 }
1077 g_slist_free (seat->devices);
1078 g_hash_table_unref (seat->touches);
1079
1080 xkb_state_unref (seat->xkb);
1081
1082 clear_repeat_timer (seat);
1083
1084 if (seat->libinput_seat)
1085 libinput_seat_unref (seat->libinput_seat);
1086
1087 g_free (seat);
1088 }
1089
1090 static void
1091 clutter_seat_evdev_set_stage (ClutterSeatEvdev *seat, ClutterStage *stage)
1092 {
1093 GSList *l;
1094
1095 for (l = seat->devices; l; l = l->next)
1096 {
1097 ClutterInputDevice *device = l->data;
1098
1099 _clutter_input_device_set_stage (device, stage);
1100 }
1101 }
1102
1103 static void
1104 evdev_add_device (ClutterDeviceManagerEvdev *manager_evdev,
1105 struct libinput_device *libinput_device)
1106 {
1107 ClutterDeviceManager *manager = (ClutterDeviceManager *) manager_evdev;
1108 ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv;
1109 ClutterInputDeviceType type;
1110 struct libinput_seat *libinput_seat;
1111 ClutterSeatEvdev *seat;
1112 ClutterInputDevice *device;
1113
1114 libinput_seat = libinput_device_get_seat (libinput_device);
1115 seat = libinput_seat_get_user_data (libinput_seat);
1116 if (seat == NULL)
1117 {
1118 /* Clutter has the notion of global "core" pointers and keyboard devices,
1119 * which are located on the main seat. Make whatever seat comes first the
1120 * main seat. */
1121 if (priv->main_seat->libinput_seat == NULL)
1122 seat = priv->main_seat;
1123 else
1124 seat = clutter_seat_evdev_new (manager_evdev);
1125
1126 clutter_seat_evdev_set_libinput_seat (seat, libinput_seat);
1127 }
1128
1129 device = _clutter_input_device_evdev_new (manager, seat, libinput_device);
1130 _clutter_input_device_set_stage (device, manager_evdev->priv->stage);
1131
1132 _clutter_device_manager_add_device (manager, device);
1133
1134 /* Clutter assumes that device types are exclusive in the
1135 * ClutterInputDevice API */
1136 type = _clutter_input_device_evdev_determine_type (libinput_device);
1137
1138 if (type == CLUTTER_KEYBOARD_DEVICE)
1139 {
1140 _clutter_input_device_set_associated_device (device, seat->core_keyboard);
1141 _clutter_input_device_add_slave (seat->core_keyboard, device);
1142 }
1143 else if (type == CLUTTER_POINTER_DEVICE)
1144 {
1145 _clutter_input_device_set_associated_device (device, seat->core_pointer);
1146 _clutter_input_device_add_slave (seat->core_pointer, device);
1147 }
1148
1149 CLUTTER_NOTE (EVENT, "Added physical device '%s', type %s",
1150 clutter_input_device_get_device_name (device),
1151 device_type_str[type]);
1152 }
1153
1154 static void
1155 evdev_remove_device (ClutterDeviceManagerEvdev *manager_evdev,
1156 ClutterInputDeviceEvdev *device_evdev)
1157 {
1158 ClutterDeviceManager *manager = CLUTTER_DEVICE_MANAGER (manager_evdev);
1159 ClutterInputDevice *input_device = CLUTTER_INPUT_DEVICE (device_evdev);
1160
1161 _clutter_device_manager_remove_device (manager, input_device);
1162 }
1163
1164 /*
1165 * ClutterDeviceManager implementation
1166 */
1167
1168 static void
1169 clutter_device_manager_evdev_add_device (ClutterDeviceManager *manager,
1170 ClutterInputDevice *device)
1171 {
1172 ClutterDeviceManagerEvdev *manager_evdev;
1173 ClutterDeviceManagerEvdevPrivate *priv;
1174 ClutterInputDeviceEvdev *device_evdev;
1175 ClutterSeatEvdev *seat;
1176
1177 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager);
1178 priv = manager_evdev->priv;
1179 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (device);
1180 seat = _clutter_input_device_evdev_get_seat (device_evdev);
1181
1182 seat->devices = g_slist_prepend (seat->devices, device);
1183 priv->devices = g_slist_prepend (priv->devices, device);
1184 }
1185
1186 static void
1187 clutter_device_manager_evdev_remove_device (ClutterDeviceManager *manager,
1188 ClutterInputDevice *device)
1189 {
1190 ClutterDeviceManagerEvdev *manager_evdev;
1191 ClutterDeviceManagerEvdevPrivate *priv;
1192 ClutterInputDeviceEvdev *device_evdev;
1193 ClutterSeatEvdev *seat;
1194
1195 device_evdev = CLUTTER_INPUT_DEVICE_EVDEV (device);
1196 seat = _clutter_input_device_evdev_get_seat (device_evdev);
1197 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager);
1198 priv = manager_evdev->priv;
1199
1200 /* Remove the device */
1201 seat->devices = g_slist_remove (seat->devices, device);
1202 priv->devices = g_slist_remove (priv->devices, device);
1203
1204 if (seat->repeat_timer && seat->repeat_device == device)
1205 clear_repeat_timer (seat);
1206
1207 g_object_unref (device);
1208 }
1209
1210 static const GSList *
1211 clutter_device_manager_evdev_get_devices (ClutterDeviceManager *manager)
1212 {
1213 return CLUTTER_DEVICE_MANAGER_EVDEV (manager)->priv->devices;
1214 }
1215
1216 static ClutterInputDevice *
1217 clutter_device_manager_evdev_get_core_device (ClutterDeviceManager *manager,
1218 ClutterInputDeviceType type)
1219 {
1220 ClutterDeviceManagerEvdev *manager_evdev;
1221 ClutterDeviceManagerEvdevPrivate *priv;
1222
1223 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager);
1224 priv = manager_evdev->priv;
1225
1226 switch (type)
1227 {
1228 case CLUTTER_POINTER_DEVICE:
1229 return priv->main_seat->core_pointer;
1230
1231 case CLUTTER_KEYBOARD_DEVICE:
1232 return priv->main_seat->core_keyboard;
1233
1234 case CLUTTER_EXTENSION_DEVICE:
1235 default:
1236 return NULL;
1237 }
1238
1239 return NULL;
1240 }
1241
1242 static ClutterInputDevice *
1243 clutter_device_manager_evdev_get_device (ClutterDeviceManager *manager,
1244 gint id)
1245 {
1246 ClutterDeviceManagerEvdev *manager_evdev;
1247 ClutterDeviceManagerEvdevPrivate *priv;
1248 GSList *l;
1249 GSList *device_it;
1250
1251 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager);
1252 priv = manager_evdev->priv;
1253
1254 for (l = priv->seats; l; l = l->next)
1255 {
1256 ClutterSeatEvdev *seat = l->data;
1257
1258 for (device_it = seat->devices; device_it; device_it = device_it->next)
1259 {
1260 ClutterInputDevice *device = device_it->data;
1261
1262 if (clutter_input_device_get_device_id (device) == id)
1263 return device;
1264 }
1265 }
1266
1267 return NULL;
1268 }
1269
1270 static void
1271 clutter_seat_evdev_sync_leds (ClutterSeatEvdev *seat)
1272 {
1273 GSList *iter;
1274 ClutterInputDeviceEvdev *device_evdev;
1275 int caps_lock, num_lock, scroll_lock;
1276 enum libinput_led leds = 0;
1277
1278 caps_lock = xkb_state_led_index_is_active (seat->xkb, seat->caps_lock_led);
1279 num_lock = xkb_state_led_index_is_active (seat->xkb, seat->num_lock_led);
1280 scroll_lock = xkb_state_led_index_is_active (seat->xkb, seat->scroll_lock_led);
1281
1282 if (caps_lock)
1283 leds |= LIBINPUT_LED_CAPS_LOCK;
1284 if (num_lock)
1285 leds |= LIBINPUT_LED_NUM_LOCK;
1286 if (scroll_lock)
1287 leds |= LIBINPUT_LED_SCROLL_LOCK;
1288
1289 for (iter = seat->devices; iter; iter = iter->next)
1290 {
1291 device_evdev = iter->data;
1292 _clutter_input_device_evdev_update_leds (device_evdev, leds);
1293 }
1294 }
1295
1296 static void
1297 flush_event_queue (void)
1298 {
1299 ClutterEvent *event;
1300
1301 while ((event = clutter_event_get ()) != NULL)
1302 {
1303 _clutter_process_event (event);
1304 clutter_event_free (event);
1305 }
1306 }
1307
1308 static gboolean
1309 process_base_event (ClutterDeviceManagerEvdev *manager_evdev,
1310 struct libinput_event *event)
1311 {
1312 ClutterInputDevice *device;
1313 struct libinput_device *libinput_device;
1314 gboolean handled = TRUE;
1315
1316 switch (libinput_event_get_type (event))
1317 {
1318 case LIBINPUT_EVENT_DEVICE_ADDED:
1319 libinput_device = libinput_event_get_device (event);
1320
1321 evdev_add_device (manager_evdev, libinput_device);
1322 break;
1323
1324 case LIBINPUT_EVENT_DEVICE_REMOVED:
1325 /* Flush all queued events, there
1326 * might be some from this device.
1327 */
1328 flush_event_queue ();
1329
1330 libinput_device = libinput_event_get_device (event);
1331
1332 device = libinput_device_get_user_data (libinput_device);
1333 evdev_remove_device (manager_evdev,
1334 CLUTTER_INPUT_DEVICE_EVDEV (device));
1335 break;
1336
1337 default:
1338 handled = FALSE;
1339 }
1340
1341 return handled;
1342 }
1343
1344 static ClutterTouchState *
1345 _device_seat_add_touch (ClutterInputDevice *input_device,
1346 guint32 id)
1347 {
1348 ClutterInputDeviceEvdev *device_evdev =
1349 CLUTTER_INPUT_DEVICE_EVDEV (input_device);
1350 ClutterSeatEvdev *seat = _clutter_input_device_evdev_get_seat (device_evdev);
1351 ClutterTouchState *touch;
1352
1353 touch = g_slice_new0 (ClutterTouchState);
1354 touch->id = id;
1355
1356 g_hash_table_insert (seat->touches, GUINT_TO_POINTER (id), touch);
1357
1358 return touch;
1359 }
1360
1361 static void
1362 _device_seat_remove_touch (ClutterInputDevice *input_device,
1363 guint32 id)
1364 {
1365 ClutterInputDeviceEvdev *device_evdev =
1366 CLUTTER_INPUT_DEVICE_EVDEV (input_device);
1367 ClutterSeatEvdev *seat = _clutter_input_device_evdev_get_seat (device_evdev);
1368
1369 g_hash_table_remove (seat->touches, GUINT_TO_POINTER (id));
1370 }
1371
1372 static ClutterTouchState *
1373 _device_seat_get_touch (ClutterInputDevice *input_device,
1374 guint32 id)
1375 {
1376 ClutterInputDeviceEvdev *device_evdev =
1377 CLUTTER_INPUT_DEVICE_EVDEV (input_device);
1378 ClutterSeatEvdev *seat = _clutter_input_device_evdev_get_seat (device_evdev);
1379
1380 return g_hash_table_lookup (seat->touches, GUINT_TO_POINTER (id));
1381 }
1382
1383 static void
1384 check_notify_discrete_scroll (ClutterDeviceManagerEvdev *manager_evdev,
1385 ClutterInputDevice *device,
1386 guint64 time_us,
1387 ClutterScrollSource scroll_source)
1388 {
1389 ClutterInputDeviceEvdev *device_evdev =
1390 CLUTTER_INPUT_DEVICE_EVDEV (device);
1391 ClutterSeatEvdev *seat = _clutter_input_device_evdev_get_seat (device_evdev);
1392 int i, n_xscrolls, n_yscrolls;
1393
1394 n_xscrolls = floor (fabs (seat->accum_scroll_dx) / DISCRETE_SCROLL_STEP);
1395 n_yscrolls = floor (fabs (seat->accum_scroll_dy) / DISCRETE_SCROLL_STEP);
1396
1397 for (i = 0; i < n_xscrolls; i++)
1398 {
1399 notify_discrete_scroll (device, time_us,
1400 seat->accum_scroll_dx > 0 ?
1401 CLUTTER_SCROLL_RIGHT : CLUTTER_SCROLL_LEFT,
1402 scroll_source, TRUE);
1403 }
1404
1405 for (i = 0; i < n_yscrolls; i++)
1406 {
1407 notify_discrete_scroll (device, time_us,
1408 seat->accum_scroll_dy > 0 ?
1409 CLUTTER_SCROLL_DOWN : CLUTTER_SCROLL_UP,
1410 scroll_source, TRUE);
1411 }
1412
1413 seat->accum_scroll_dx = fmodf (seat->accum_scroll_dx, DISCRETE_SCROLL_STEP);
1414 seat->accum_scroll_dy = fmodf (seat->accum_scroll_dy, DISCRETE_SCROLL_STEP);
1415 }
1416
1417 static ClutterScrollSource
1418 translate_scroll_source (enum libinput_pointer_axis_source source)
1419 {
1420 switch (source)
1421 {
1422 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
1423 return CLUTTER_SCROLL_SOURCE_WHEEL;
1424 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
1425 return CLUTTER_SCROLL_SOURCE_FINGER;
1426 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
1427 return CLUTTER_SCROLL_SOURCE_CONTINUOUS;
1428 default:
1429 return CLUTTER_SCROLL_SOURCE_UNKNOWN;
1430 }
1431 }
1432
1433 static gboolean
1434 process_device_event (ClutterDeviceManagerEvdev *manager_evdev,
1435 struct libinput_event *event)
1436 {
1437 gboolean handled = TRUE;
1438 struct libinput_device *libinput_device = libinput_event_get_device(event);
1439 ClutterInputDevice *device;
1440
1441 switch (libinput_event_get_type (event))
1442 {
1443 case LIBINPUT_EVENT_KEYBOARD_KEY:
1444 {
1445 guint32 key, key_state, seat_key_count;
1446 guint64 time_us;
1447 struct libinput_event_keyboard *key_event =
1448 libinput_event_get_keyboard_event (event);
1449 device = libinput_device_get_user_data (libinput_device);
1450
1451 time_us = libinput_event_keyboard_get_time_usec (key_event);
1452 key = libinput_event_keyboard_get_key (key_event);
1453 key_state = libinput_event_keyboard_get_key_state (key_event) ==
1454 LIBINPUT_KEY_STATE_PRESSED;
1455 seat_key_count =
1456 libinput_event_keyboard_get_seat_key_count (key_event);
1457
1458 /* Ignore key events that are not seat wide state changes. */
1459 if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
1460 seat_key_count != 1) ||
1461 (key_state == LIBINPUT_KEY_STATE_RELEASED &&
1462 seat_key_count != 0))
1463 break;
1464
1465 notify_key_device (device, time_us, key, key_state, TRUE);
1466
1467 break;
1468 }
1469
1470 case LIBINPUT_EVENT_POINTER_MOTION:
1471 {
1472 struct libinput_event_pointer *motion_event =
1473 libinput_event_get_pointer_event (event);
1474 device = libinput_device_get_user_data (libinput_device);
1475
1476 notify_relative_motion (device, motion_event);
1477
1478 break;
1479 }
1480
1481 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
1482 {
1483 guint64 time_us;
1484 double x, y;
1485 gfloat stage_width, stage_height;
1486 ClutterStage *stage;
1487 struct libinput_event_pointer *motion_event =
1488 libinput_event_get_pointer_event (event);
1489 device = libinput_device_get_user_data (libinput_device);
1490
1491 stage = _clutter_input_device_get_stage (device);
1492 if (stage == NULL)
1493 break;
1494
1495 stage_width = clutter_actor_get_width (CLUTTER_ACTOR (stage));
1496 stage_height = clutter_actor_get_height (CLUTTER_ACTOR (stage));
1497
1498 time_us = libinput_event_pointer_get_time_usec (motion_event);
1499 x = libinput_event_pointer_get_absolute_x_transformed (motion_event,
1500 stage_width);
1501 y = libinput_event_pointer_get_absolute_y_transformed (motion_event,
1502 stage_height);
1503 notify_absolute_motion (device, time_us, x, y);
1504
1505 break;
1506 }
1507
1508 case LIBINPUT_EVENT_POINTER_BUTTON:
1509 {
1510 guint32 button, button_state, seat_button_count;
1511 guint64 time_us;
1512 struct libinput_event_pointer *button_event =
1513 libinput_event_get_pointer_event (event);
1514 device = libinput_device_get_user_data (libinput_device);
1515
1516 time_us = libinput_event_pointer_get_time_usec (button_event);
1517 button = libinput_event_pointer_get_button (button_event);
1518 button_state = libinput_event_pointer_get_button_state (button_event) ==
1519 LIBINPUT_BUTTON_STATE_PRESSED;
1520 seat_button_count =
1521 libinput_event_pointer_get_seat_button_count (button_event);
1522
1523 /* Ignore button events that are not seat wide state changes. */
1524 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
1525 seat_button_count != 1) ||
1526 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
1527 seat_button_count != 0))
1528 break;
1529
1530 notify_button (device, time_us, button, button_state);
1531
1532 break;
1533 }
1534
1535 case LIBINPUT_EVENT_POINTER_AXIS:
1536 {
1537 gdouble dx = 0.0, dy = 0.0;
1538 gdouble discrete_x = 0.0, discrete_y = 0.0;
1539 guint64 time_us;
1540 gboolean wheel = FALSE;
1541 enum libinput_pointer_axis axis;
1542 enum libinput_pointer_axis_source source;
1543 struct libinput_event_pointer *axis_event =
1544 libinput_event_get_pointer_event (event);
1545 ClutterSeatEvdev *seat;
1546 ClutterScrollSource scroll_source;
1547 ClutterScrollFinishFlags finish_flags = 0;
1548
1549 device = libinput_device_get_user_data (libinput_device);
1550 seat = _clutter_input_device_evdev_get_seat (CLUTTER_INPUT_DEVICE_EVDEV (device));
1551
1552 time_us = libinput_event_pointer_get_time_usec (axis_event);
1553 source = libinput_event_pointer_get_axis_source (axis_event);
1554 scroll_source = translate_scroll_source (source);
1555
1556 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
1557 the value is the angle of the click in degrees. To keep
1558 backwards-compat with existing clients, we just send multiples of
1559 the click count. */
1560
1561 if (source == LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
1562 wheel = TRUE;
1563
1564 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
1565 if (libinput_event_pointer_has_axis (axis_event, axis))
1566 {
1567 discrete_y = libinput_event_pointer_get_axis_value_discrete (axis_event, axis);
1568 dy = libinput_event_pointer_get_axis_value (axis_event, axis);
1569
1570 if (wheel)
1571 seat->accum_scroll_dy = 0;
1572 else if (fabs (dy) < DBL_EPSILON)
1573 {
1574 finish_flags |= CLUTTER_SCROLL_FINISHED_VERTICAL;
1575 seat->accum_scroll_dy = 0;
1576 }
1577 else
1578 seat->accum_scroll_dy += dy;
1579 }
1580
1581 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
1582 if (libinput_event_pointer_has_axis (axis_event, axis))
1583 {
1584 discrete_x = libinput_event_pointer_get_axis_value_discrete (axis_event, axis);
1585 dx = libinput_event_pointer_get_axis_value (axis_event, axis);
1586
1587 if (wheel)
1588 seat->accum_scroll_dx = 0;
1589 else if (fabs (dx) < DBL_EPSILON)
1590 {
1591 finish_flags |= CLUTTER_SCROLL_FINISHED_HORIZONTAL;
1592 seat->accum_scroll_dx = 0;
1593 }
1594 else
1595 seat->accum_scroll_dx += dx;
1596 }
1597
1598 if (wheel)
1599 {
1600 notify_scroll (device, time_us,
1601 discrete_x * DISCRETE_SCROLL_STEP,
1602 discrete_y * DISCRETE_SCROLL_STEP,
1603 scroll_source, finish_flags, TRUE);
1604 notify_discrete_scroll (device, time_us,
1605 discrete_to_direction (discrete_x, discrete_y),
1606 scroll_source, FALSE);
1607 }
1608 else
1609 {
1610 notify_scroll (device, time_us, dx, dy,
1611 scroll_source, finish_flags, FALSE);
1612 check_notify_discrete_scroll (manager_evdev, device,
1613 time_us, scroll_source);
1614 }
1615
1616 break;
1617
1618 }
1619
1620 case LIBINPUT_EVENT_TOUCH_DOWN:
1621 {
1622 gint32 slot;
1623 guint64 time_us;
1624 double x, y;
1625 gfloat stage_width, stage_height;
1626 ClutterStage *stage;
1627 ClutterTouchState *touch_state;
1628 struct libinput_event_touch *touch_event =
1629 libinput_event_get_touch_event (event);
1630 device = libinput_device_get_user_data (libinput_device);
1631
1632 stage = _clutter_input_device_get_stage (device);
1633 if (stage == NULL)
1634 break;
1635
1636 stage_width = clutter_actor_get_width (CLUTTER_ACTOR (stage));
1637 stage_height = clutter_actor_get_height (CLUTTER_ACTOR (stage));
1638
1639 slot = libinput_event_touch_get_slot (touch_event);
1640 time_us = libinput_event_touch_get_time_usec (touch_event);
1641 x = libinput_event_touch_get_x_transformed (touch_event,
1642 stage_width);
1643 y = libinput_event_touch_get_y_transformed (touch_event,
1644 stage_height);
1645
1646 touch_state = _device_seat_add_touch (device, slot);
1647 touch_state->coords.x = x;
1648 touch_state->coords.y = y;
1649
1650 notify_touch_event (device, CLUTTER_TOUCH_BEGIN, time_us, slot,
1651 touch_state->coords.x, touch_state->coords.y);
1652 break;
1653 }
1654
1655 case LIBINPUT_EVENT_TOUCH_UP:
1656 {
1657 gint32 slot;
1658 guint64 time_us;
1659 ClutterTouchState *touch_state;
1660 struct libinput_event_touch *touch_event =
1661 libinput_event_get_touch_event (event);
1662 device = libinput_device_get_user_data (libinput_device);
1663
1664 slot = libinput_event_touch_get_slot (touch_event);
1665 time_us = libinput_event_touch_get_time_usec (touch_event);
1666 touch_state = _device_seat_get_touch (device, slot);
1667
1668 notify_touch_event (device, CLUTTER_TOUCH_END, time_us, slot,
1669 touch_state->coords.x, touch_state->coords.y);
1670 _device_seat_remove_touch (device, slot);
1671
1672 break;
1673 }
1674
1675 case LIBINPUT_EVENT_TOUCH_MOTION:
1676 {
1677 gint32 slot;
1678 guint64 time_us;
1679 double x, y;
1680 gfloat stage_width, stage_height;
1681 ClutterStage *stage;
1682 ClutterTouchState *touch_state;
1683 struct libinput_event_touch *touch_event =
1684 libinput_event_get_touch_event (event);
1685 device = libinput_device_get_user_data (libinput_device);
1686
1687 stage = _clutter_input_device_get_stage (device);
1688 if (stage == NULL)
1689 break;
1690
1691 stage_width = clutter_actor_get_width (CLUTTER_ACTOR (stage));
1692 stage_height = clutter_actor_get_height (CLUTTER_ACTOR (stage));
1693
1694 slot = libinput_event_touch_get_slot (touch_event);
1695 time_us = libinput_event_touch_get_time_usec (touch_event);
1696 x = libinput_event_touch_get_x_transformed (touch_event,
1697 stage_width);
1698 y = libinput_event_touch_get_y_transformed (touch_event,
1699 stage_height);
1700
1701 touch_state = _device_seat_get_touch (device, slot);
1702 touch_state->coords.x = x;
1703 touch_state->coords.y = y;
1704
1705 notify_touch_event (device, CLUTTER_TOUCH_UPDATE, time_us, slot,
1706 touch_state->coords.x, touch_state->coords.y);
1707 break;
1708 }
1709 case LIBINPUT_EVENT_TOUCH_CANCEL:
1710 {
1711 ClutterTouchState *touch_state;
1712 GHashTableIter iter;
1713 guint64 time_us;
1714 struct libinput_event_touch *touch_event =
1715 libinput_event_get_touch_event (event);
1716 ClutterSeatEvdev *seat;
1717
1718 device = libinput_device_get_user_data (libinput_device);
1719 time_us = libinput_event_touch_get_time_usec (touch_event);
1720 seat = _clutter_input_device_evdev_get_seat (CLUTTER_INPUT_DEVICE_EVDEV (device));
1721 g_hash_table_iter_init (&iter, seat->touches);
1722
1723 while (g_hash_table_iter_next (&iter, NULL, (gpointer*) &touch_state))
1724 {
1725 notify_touch_event (device, CLUTTER_TOUCH_CANCEL,
1726 time_us, touch_state->id,
1727 touch_state->coords.x, touch_state->coords.y);
1728 g_hash_table_iter_remove (&iter);
1729 }
1730
1731 break;
1732 }
1733 case LIBINPUT_EVENT_GESTURE_PINCH_BEGIN:
1734 case LIBINPUT_EVENT_GESTURE_PINCH_END:
1735 {
1736 struct libinput_event_gesture *gesture_event =
1737 libinput_event_get_gesture_event (event);
1738 ClutterTouchpadGesturePhase phase;
1739 guint64 time_us;
1740
1741 if (libinput_event_get_type (event) == LIBINPUT_EVENT_GESTURE_PINCH_BEGIN)
1742 phase = CLUTTER_TOUCHPAD_GESTURE_PHASE_BEGIN;
1743 else
1744 phase = libinput_event_gesture_get_cancelled (gesture_event) ?
1745 CLUTTER_TOUCHPAD_GESTURE_PHASE_CANCEL : CLUTTER_TOUCHPAD_GESTURE_PHASE_END;
1746
1747 device = libinput_device_get_user_data (libinput_device);
1748 time_us = libinput_event_gesture_get_time_usec (gesture_event);
1749 notify_pinch_gesture_event (device, phase, time_us, 0, 0, 0, 0);
1750 break;
1751 }
1752 case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
1753 {
1754 struct libinput_event_gesture *gesture_event =
1755 libinput_event_get_gesture_event (event);
1756 gdouble angle_delta, scale, dx, dy;
1757 guint64 time_us;
1758
1759 device = libinput_device_get_user_data (libinput_device);
1760 time_us = libinput_event_gesture_get_time_usec (gesture_event);
1761 angle_delta = libinput_event_gesture_get_angle_delta (gesture_event);
1762 scale = libinput_event_gesture_get_scale (gesture_event);
1763 dx = libinput_event_gesture_get_dx (gesture_event);
1764 dy = libinput_event_gesture_get_dx (gesture_event);
1765
1766 notify_pinch_gesture_event (device,
1767 CLUTTER_TOUCHPAD_GESTURE_PHASE_UPDATE,
1768 time_us, dx, dy, angle_delta, scale);
1769 break;
1770 }
1771 case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN:
1772 case LIBINPUT_EVENT_GESTURE_SWIPE_END:
1773 {
1774 struct libinput_event_gesture *gesture_event =
1775 libinput_event_get_gesture_event (event);
1776 ClutterTouchpadGesturePhase phase;
1777 guint32 n_fingers;
1778 guint64 time_us;
1779
1780 device = libinput_device_get_user_data (libinput_device);
1781 time_us = libinput_event_gesture_get_time_usec (gesture_event);
1782 n_fingers = libinput_event_gesture_get_finger_count (gesture_event);
1783
1784 if (libinput_event_get_type (event) == LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN)
1785 phase = CLUTTER_TOUCHPAD_GESTURE_PHASE_BEGIN;
1786 else
1787 phase = libinput_event_gesture_get_cancelled (gesture_event) ?
1788 CLUTTER_TOUCHPAD_GESTURE_PHASE_CANCEL : CLUTTER_TOUCHPAD_GESTURE_PHASE_END;
1789
1790 notify_swipe_gesture_event (device, phase, time_us, n_fingers, 0, 0);
1791 break;
1792 }
1793 case LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE:
1794 {
1795 struct libinput_event_gesture *gesture_event =
1796 libinput_event_get_gesture_event (event);
1797 guint32 n_fingers;
1798 guint64 time_us;
1799 gdouble dx, dy;
1800
1801 device = libinput_device_get_user_data (libinput_device);
1802 time_us = libinput_event_gesture_get_time_usec (gesture_event);
1803 n_fingers = libinput_event_gesture_get_finger_count (gesture_event);
1804 dx = libinput_event_gesture_get_dx (gesture_event);
1805 dy = libinput_event_gesture_get_dy (gesture_event);
1806
1807 notify_swipe_gesture_event (device,
1808 CLUTTER_TOUCHPAD_GESTURE_PHASE_UPDATE,
1809 time_us, n_fingers, dx, dy);
1810 break;
1811 }
1812 default:
1813 handled = FALSE;
1814 }
1815
1816 return handled;
1817 }
1818
1819 static gboolean
1820 filter_event (ClutterDeviceManagerEvdev *manager_evdev,
1821 struct libinput_event *event)
1822 {
1823 gboolean retval = CLUTTER_EVENT_PROPAGATE;
1824 ClutterEventFilter *filter;
1825 GSList *tmp_list;
1826
1827 tmp_list = manager_evdev->priv->event_filters;
1828
1829 while (tmp_list)
1830 {
1831 filter = tmp_list->data;
1832 retval = filter->func (event, filter->data);
1833 tmp_list = tmp_list->next;
1834
1835 if (retval != CLUTTER_EVENT_PROPAGATE)
1836 break;
1837 }
1838
1839 return retval;
1840 }
1841
1842 static void
1843 process_event (ClutterDeviceManagerEvdev *manager_evdev,
1844 struct libinput_event *event)
1845 {
1846 gboolean retval;
1847
1848 retval = filter_event (manager_evdev, event);
1849
1850 if (retval != CLUTTER_EVENT_PROPAGATE)
1851 return;
1852
1853 if (process_base_event (manager_evdev, event))
1854 return;
1855 if (process_device_event (manager_evdev, event))
1856 return;
1857 }
1858
1859 static void
1860 process_events (ClutterDeviceManagerEvdev *manager_evdev)
1861 {
1862 ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv;
1863 struct libinput_event *event;
1864
1865 while ((event = libinput_get_event (priv->libinput)))
1866 {
1867 process_event(manager_evdev, event);
1868 libinput_event_destroy(event);
1869 }
1870 }
1871
1872 static int
1873 open_restricted (const char *path,
1874 int flags,
1875 void *user_data)
1876 {
1877 gint fd;
1878
1879 if (device_open_callback)
1880 {
1881 GError *error = NULL;
1882
1883 fd = device_open_callback (path, flags, device_callback_data, &error);
1884
1885 if (fd < 0)
1886 {
1887 g_warning ("Could not open device %s: %s", path, error->message);
1888 g_error_free (error);
1889 }
1890 }
1891 else
1892 {
1893 fd = open (path, O_RDWR | O_NONBLOCK);
1894 if (fd < 0)
1895 {
1896 g_warning ("Could not open device %s: %s", path, strerror (errno));
1897 }
1898 }
1899
1900 return fd;
1901 }
1902
1903 static void
1904 close_restricted (int fd,
1905 void *user_data)
1906 {
1907 if (device_close_callback)
1908 device_close_callback (fd, device_callback_data);
1909 else
1910 close (fd);
1911 }
1912
1913 static const struct libinput_interface libinput_interface = {
1914 open_restricted,
1915 close_restricted
1916 };
1917
1918 /*
1919 * GObject implementation
1920 */
1921
1922 static void
1923 clutter_device_manager_evdev_constructed (GObject *gobject)
1924 {
1925 ClutterDeviceManagerEvdev *manager_evdev;
1926 ClutterDeviceManagerEvdevPrivate *priv;
1927 ClutterEventSource *source;
1928 struct udev *udev;
1929
1930 udev = udev_new ();
1931 if (G_UNLIKELY (udev == NULL))
1932 {
1933 g_warning ("Failed to create udev object");
1934 return;
1935 }
1936
1937 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (gobject);
1938 priv = manager_evdev->priv;
1939
1940 priv->libinput = libinput_udev_create_context (&libinput_interface,
1941 manager_evdev,
1942 udev);
1943 if (priv->libinput == NULL)
1944 {
1945 g_critical ("Failed to create the libinput object.");
1946 return;
1947 }
1948
1949 if (libinput_udev_assign_seat (priv->libinput, "seat0") == -1)
1950 {
1951 g_critical ("Failed to assign a seat to the libinput object.");
1952 libinput_unref (priv->libinput);
1953 priv->libinput = NULL;
1954 return;
1955 }
1956
1957 udev_unref (udev);
1958
1959 priv->main_seat = clutter_seat_evdev_new (manager_evdev);
1960
1961 dispatch_libinput (manager_evdev);
1962
1963 source = clutter_event_source_new (manager_evdev);
1964 priv->event_source = source;
1965 }
1966
1967 static void
1968 clutter_device_manager_evdev_dispose (GObject *object)
1969 {
1970 ClutterDeviceManagerEvdev *manager_evdev;
1971 ClutterDeviceManagerEvdevPrivate *priv;
1972
1973 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (object);
1974 priv = manager_evdev->priv;
1975
1976 if (priv->stage_added_handler)
1977 {
1978 g_signal_handler_disconnect (priv->stage_manager,
1979 priv->stage_added_handler);
1980 priv->stage_added_handler = 0;
1981 }
1982
1983 if (priv->stage_removed_handler)
1984 {
1985 g_signal_handler_disconnect (priv->stage_manager,
1986 priv->stage_removed_handler);
1987 priv->stage_removed_handler = 0;
1988 }
1989
1990 if (priv->stage_manager)
1991 {
1992 g_object_unref (priv->stage_manager);
1993 priv->stage_manager = NULL;
1994 }
1995
1996 G_OBJECT_CLASS (clutter_device_manager_evdev_parent_class)->dispose (object);
1997 }
1998
1999 static void
2000 clutter_device_manager_evdev_finalize (GObject *object)
2001 {
2002 ClutterDeviceManagerEvdev *manager_evdev;
2003 ClutterDeviceManagerEvdevPrivate *priv;
2004
2005 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (object);
2006 priv = manager_evdev->priv;
2007
2008 g_slist_free_full (priv->seats, (GDestroyNotify) clutter_seat_evdev_free);
2009 g_slist_free (priv->devices);
2010
2011 if (priv->keymap)
2012 xkb_keymap_unref (priv->keymap);
2013
2014 if (priv->event_source != NULL)
2015 clutter_event_source_free (priv->event_source);
2016
2017 if (priv->constrain_data_notify != NULL)
2018 priv->constrain_data_notify (priv->constrain_data);
2019
2020 if (priv->libinput != NULL)
2021 libinput_unref (priv->libinput);
2022
2023 g_list_free (priv->free_device_ids);
2024
2025 G_OBJECT_CLASS (clutter_device_manager_evdev_parent_class)->finalize (object);
2026 }
2027
2028 static void
2029 clutter_device_manager_evdev_class_init (ClutterDeviceManagerEvdevClass *klass)
2030 {
2031 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
2032 ClutterDeviceManagerClass *manager_class;
2033
2034 gobject_class->constructed = clutter_device_manager_evdev_constructed;
2035 gobject_class->finalize = clutter_device_manager_evdev_finalize;
2036 gobject_class->dispose = clutter_device_manager_evdev_dispose;
2037
2038 manager_class = CLUTTER_DEVICE_MANAGER_CLASS (klass);
2039 manager_class->add_device = clutter_device_manager_evdev_add_device;
2040 manager_class->remove_device = clutter_device_manager_evdev_remove_device;
2041 manager_class->get_devices = clutter_device_manager_evdev_get_devices;
2042 manager_class->get_core_device = clutter_device_manager_evdev_get_core_device;
2043 manager_class->get_device = clutter_device_manager_evdev_get_device;
2044 }
2045
2046 static void
2047 clutter_device_manager_evdev_stage_added_cb (ClutterStageManager *manager,
2048 ClutterStage *stage,
2049 ClutterDeviceManagerEvdev *self)
2050 {
2051 ClutterDeviceManagerEvdevPrivate *priv = self->priv;
2052 GSList *l;
2053
2054 /* NB: Currently we can only associate a single stage with all evdev
2055 * devices.
2056 *
2057 * We save a pointer to the stage so if we release/reclaim input
2058 * devices due to switching virtual terminals then we know what
2059 * stage to re associate the devices with.
2060 */
2061 priv->stage = stage;
2062
2063 /* Set the stage of any devices that don't already have a stage */
2064 for (l = priv->seats; l; l = l->next)
2065 {
2066 ClutterSeatEvdev *seat = l->data;
2067
2068 clutter_seat_evdev_set_stage (seat, stage);
2069 }
2070
2071 /* We only want to do this once so we can catch the default
2072 stage. If the application has multiple stages then it will need
2073 to manage the stage of the input devices itself */
2074 g_signal_handler_disconnect (priv->stage_manager,
2075 priv->stage_added_handler);
2076 priv->stage_added_handler = 0;
2077 }
2078
2079 static void
2080 clutter_device_manager_evdev_stage_removed_cb (ClutterStageManager *manager,
2081 ClutterStage *stage,
2082 ClutterDeviceManagerEvdev *self)
2083 {
2084 ClutterDeviceManagerEvdevPrivate *priv = self->priv;
2085 GSList *l;
2086
2087 /* Remove the stage of any input devices that were pointing to this
2088 stage so we don't send events to invalid stages */
2089 for (l = priv->seats; l; l = l->next)
2090 {
2091 ClutterSeatEvdev *seat = l->data;
2092
2093 clutter_seat_evdev_set_stage (seat, NULL);
2094 }
2095 }
2096
2097 static void
2098 clutter_device_manager_evdev_init (ClutterDeviceManagerEvdev *self)
2099 {
2100 ClutterDeviceManagerEvdevPrivate *priv;
2101
2102 priv = self->priv = clutter_device_manager_evdev_get_instance_private (self);
2103
2104 priv->stage_manager = clutter_stage_manager_get_default ();
2105 g_object_ref (priv->stage_manager);
2106
2107 /* evdev doesn't have any way to link an event to a particular stage
2108 so we'll have to leave it up to applications to set the
2109 corresponding stage for an input device. However to make it
2110 easier for applications that are only using one fullscreen stage
2111 (which is probably the most frequent use-case for the evdev
2112 backend) we'll associate any input devices that don't have a
2113 stage with the first stage created. */
2114 priv->stage_added_handler =
2115 g_signal_connect (priv->stage_manager,
2116 "stage-added",
2117 G_CALLBACK (clutter_device_manager_evdev_stage_added_cb),
2118 self);
2119 priv->stage_removed_handler =
2120 g_signal_connect (priv->stage_manager,
2121 "stage-removed",
2122 G_CALLBACK (clutter_device_manager_evdev_stage_removed_cb),
2123 self);
2124
2125 priv->device_id_next = INITIAL_DEVICE_ID;
2126 }
2127
2128 void
2129 _clutter_events_evdev_init (ClutterBackend *backend)
2130 {
2131 CLUTTER_NOTE (EVENT, "Initializing evdev backend");
2132
2133 backend->device_manager = g_object_new (CLUTTER_TYPE_DEVICE_MANAGER_EVDEV,
2134 "backend", backend,
2135 NULL);
2136 }
2137
2138 void
2139 _clutter_events_evdev_uninit (ClutterBackend *backend)
2140 {
2141 CLUTTER_NOTE (EVENT, "Uninitializing evdev backend");
2142 }
2143
2144 gint
2145 _clutter_device_manager_evdev_acquire_device_id (ClutterDeviceManagerEvdev *manager_evdev)
2146 {
2147 ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv;
2148 GList *first;
2149 gint next_id;
2150
2151 if (priv->free_device_ids == NULL)
2152 {
2153 gint i;
2154
2155 /* We ran out of free ID's, so append 10 new ones. */
2156 for (i = 0; i < 10; i++)
2157 priv->free_device_ids =
2158 g_list_append (priv->free_device_ids,
2159 GINT_TO_POINTER (priv->device_id_next++));
2160 }
2161
2162 first = g_list_first (priv->free_device_ids);
2163 next_id = GPOINTER_TO_INT (first->data);
2164 priv->free_device_ids = g_list_remove_link (priv->free_device_ids, first);
2165
2166 return next_id;
2167 }
2168
2169 static int
2170 compare_ids (gconstpointer a,
2171 gconstpointer b)
2172 {
2173 return GPOINTER_TO_INT (a) - GPOINTER_TO_INT (b);
2174 }
2175
2176 void
2177 _clutter_device_manager_evdev_release_device_id (ClutterDeviceManagerEvdev *manager_evdev,
2178 ClutterInputDevice *device)
2179 {
2180 ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv;
2181 gint device_id;
2182
2183 device_id = clutter_input_device_get_device_id (device);
2184 priv->free_device_ids = g_list_insert_sorted (priv->free_device_ids,
2185 GINT_TO_POINTER (device_id),
2186 compare_ids);
2187 }
2188
2189 /**
2190 * clutter_evdev_release_devices:
2191 *
2192 * Releases all the evdev devices that Clutter is currently managing. This api
2193 * is typically used when switching away from the Clutter application when
2194 * switching tty. The devices can be reclaimed later with a call to
2195 * clutter_evdev_reclaim_devices().
2196 *
2197 * This function should only be called after clutter has been initialized.
2198 *
2199 * Since: 1.10
2200 * Stability: unstable
2201 */
2202 void
2203 clutter_evdev_release_devices (void)
2204 {
2205 ClutterDeviceManager *manager = clutter_device_manager_get_default ();
2206 ClutterDeviceManagerEvdev *manager_evdev;
2207 ClutterDeviceManagerEvdevPrivate *priv;
2208
2209 if (!manager)
2210 {
2211 g_warning ("clutter_evdev_release_devices shouldn't be called "
2212 "before clutter_init()");
2213 return;
2214 }
2215
2216 g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER_EVDEV (manager));
2217
2218 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager);
2219 priv = manager_evdev->priv;
2220
2221 if (priv->released)
2222 {
2223 g_warning ("clutter_evdev_release_devices() shouldn't be called "
2224 "multiple times without a corresponding call to "
2225 "clutter_evdev_reclaim_devices() first");
2226 return;
2227 }
2228
2229 libinput_suspend (priv->libinput);
2230 process_events (manager_evdev);
2231
2232 priv->released = TRUE;
2233 }
2234
2235 static void
2236 clutter_evdev_update_xkb_state (ClutterDeviceManagerEvdev *manager_evdev)
2237 {
2238 ClutterDeviceManagerEvdevPrivate *priv;
2239 GSList *iter;
2240 ClutterSeatEvdev *seat;
2241 xkb_mod_mask_t latched_mods;
2242 xkb_mod_mask_t locked_mods;
2243
2244 priv = manager_evdev->priv;
2245
2246 for (iter = priv->seats; iter; iter = iter->next)
2247 {
2248 seat = iter->data;
2249
2250 latched_mods = xkb_state_serialize_mods (seat->xkb,
2251 XKB_STATE_MODS_LATCHED);
2252 locked_mods = xkb_state_serialize_mods (seat->xkb,
2253 XKB_STATE_MODS_LOCKED);
2254 xkb_state_unref (seat->xkb);
2255 seat->xkb = xkb_state_new (priv->keymap);
2256
2257 xkb_state_update_mask (seat->xkb,
2258 0, /* depressed */
2259 latched_mods,
2260 locked_mods,
2261 0, 0, 0);
2262
2263 seat->caps_lock_led = xkb_keymap_led_get_index (priv->keymap, XKB_LED_NAME_CAPS);
2264 seat->num_lock_led = xkb_keymap_led_get_index (priv->keymap, XKB_LED_NAME_NUM);
2265 seat->scroll_lock_led = xkb_keymap_led_get_index (priv->keymap, XKB_LED_NAME_SCROLL);
2266
2267 clutter_seat_evdev_sync_leds (seat);
2268 }
2269 }
2270
2271 /**
2272 * clutter_evdev_reclaim_devices:
2273 *
2274 * This causes Clutter to re-probe for evdev devices. This is must only be
2275 * called after a corresponding call to clutter_evdev_release_devices()
2276 * was previously used to release all evdev devices. This API is typically
2277 * used when a clutter application using evdev has regained focus due to
2278 * switching ttys.
2279 *
2280 * This function should only be called after clutter has been initialized.
2281 *
2282 * Since: 1.10
2283 * Stability: unstable
2284 */
2285 void
2286 clutter_evdev_reclaim_devices (void)
2287 {
2288 ClutterDeviceManager *manager = clutter_device_manager_get_default ();
2289 ClutterDeviceManagerEvdev *manager_evdev =
2290 CLUTTER_DEVICE_MANAGER_EVDEV (manager);
2291 ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv;
2292
2293 if (!priv->released)
2294 {
2295 g_warning ("Spurious call to clutter_evdev_reclaim_devices() without "
2296 "previous call to clutter_evdev_release_devices");
2297 return;
2298 }
2299
2300 libinput_resume (priv->libinput);
2301 clutter_evdev_update_xkb_state (manager_evdev);
2302 process_events (manager_evdev);
2303
2304 priv->released = FALSE;
2305 }
2306
2307 /**
2308 * clutter_evdev_set_device_callbacks: (skip)
2309 * @open_callback: the user replacement for open()
2310 * @close_callback: the user replacement for close()
2311 * @user_data: user data for @callback
2312 *
2313 * Through this function, the application can set a custom callback
2314 * to invoked when Clutter is about to open an evdev device. It can do
2315 * so if special handling is needed, for example to circumvent permission
2316 * problems.
2317 *
2318 * Setting @callback to %NULL will reset the default behavior.
2319 *
2320 * For reliable effects, this function must be called before clutter_init().
2321 *
2322 * Since: 1.16
2323 * Stability: unstable
2324 */
2325 void
2326 clutter_evdev_set_device_callbacks (ClutterOpenDeviceCallback open_callback,
2327 ClutterCloseDeviceCallback close_callback,
2328 gpointer user_data)
2329 {
2330 device_open_callback = open_callback;
2331 device_close_callback = close_callback;
2332 device_callback_data = user_data;
2333 }
2334
2335 /**
2336 * clutter_evdev_set_keyboard_map: (skip)
2337 * @evdev: the #ClutterDeviceManager created by the evdev backend
2338 * @keymap: the new keymap
2339 *
2340 * Instructs @evdev to use the speficied keyboard map. This will cause
2341 * the backend to drop the state and create a new one with the new
2342 * map. To avoid state being lost, callers should ensure that no key
2343 * is pressed when calling this function.
2344 *
2345 * Since: 1.16
2346 * Stability: unstable
2347 */
2348 void
2349 clutter_evdev_set_keyboard_map (ClutterDeviceManager *evdev,
2350 struct xkb_keymap *keymap)
2351 {
2352 ClutterDeviceManagerEvdev *manager_evdev;
2353 ClutterDeviceManagerEvdevPrivate *priv;
2354
2355 g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER_EVDEV (evdev));
2356
2357 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (evdev);
2358 priv = manager_evdev->priv;
2359
2360 if (priv->keymap)
2361 xkb_keymap_unref (priv->keymap);
2362
2363 priv->keymap = xkb_keymap_ref (keymap);
2364 clutter_evdev_update_xkb_state (manager_evdev);
2365 }
2366
2367 /**
2368 * clutter_evdev_get_keyboard_map: (skip)
2369 * @evdev: the #ClutterDeviceManager created by the evdev backend
2370 *
2371 * Retrieves the #xkb_keymap in use by the evdev backend.
2372 *
2373 * Return value: the #xkb_keymap.
2374 *
2375 * Since: 1.18
2376 * Stability: unstable
2377 */
2378 struct xkb_keymap *
2379 clutter_evdev_get_keyboard_map (ClutterDeviceManager *evdev)
2380 {
2381 ClutterDeviceManagerEvdev *manager_evdev;
2382
2383 g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER_EVDEV (evdev), NULL);
2384
2385 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (evdev);
2386
2387 return xkb_state_get_keymap (manager_evdev->priv->main_seat->xkb);
2388 }
2389
2390 /**
2391 * clutter_evdev_set_keyboard_layout_index: (skip)
2392 * @evdev: the #ClutterDeviceManager created by the evdev backend
2393 * @idx: the xkb layout index to set
2394 *
2395 * Sets the xkb layout index on the backend's #xkb_state .
2396 *
2397 * Since: 1.20
2398 * Stability: unstable
2399 */
2400 void
2401 clutter_evdev_set_keyboard_layout_index (ClutterDeviceManager *evdev,
2402 xkb_layout_index_t idx)
2403 {
2404 ClutterDeviceManagerEvdev *manager_evdev;
2405 xkb_mod_mask_t depressed_mods;
2406 xkb_mod_mask_t latched_mods;
2407 xkb_mod_mask_t locked_mods;
2408 struct xkb_state *state;
2409
2410 g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER_EVDEV (evdev));
2411
2412 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (evdev);
2413 state = manager_evdev->priv->main_seat->xkb;
2414
2415 depressed_mods = xkb_state_serialize_mods (state, XKB_STATE_MODS_DEPRESSED);
2416 latched_mods = xkb_state_serialize_mods (state, XKB_STATE_MODS_LATCHED);
2417 locked_mods = xkb_state_serialize_mods (state, XKB_STATE_MODS_LOCKED);
2418
2419 xkb_state_update_mask (state, depressed_mods, latched_mods, locked_mods, 0, 0, idx);
2420 }
2421
2422 /**
2423 * clutter_evdev_set_pointer_constrain_callback:
2424 * @evdev: the #ClutterDeviceManager created by the evdev backend
2425 * @callback: the callback
2426 * @user_data: data to pass to the callback
2427 * @user_data_notify: function to be called when removing the callback
2428 *
2429 * Sets a callback to be invoked for every pointer motion. The callback
2430 * can then modify the new pointer coordinates to constrain movement within
2431 * a specific region.
2432 *
2433 * Since: 1.16
2434 * Stability: unstable
2435 */
2436 void
2437 clutter_evdev_set_pointer_constrain_callback (ClutterDeviceManager *evdev,
2438 ClutterPointerConstrainCallback callback,
2439 gpointer user_data,
2440 GDestroyNotify user_data_notify)
2441 {
2442 ClutterDeviceManagerEvdev *manager_evdev;
2443 ClutterDeviceManagerEvdevPrivate *priv;
2444
2445 g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER_EVDEV (evdev));
2446
2447 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (evdev);
2448 priv = manager_evdev->priv;
2449
2450 if (priv->constrain_data_notify)
2451 priv->constrain_data_notify (priv->constrain_data);
2452
2453 priv->constrain_callback = callback;
2454 priv->constrain_data = user_data;
2455 priv->constrain_data_notify = user_data_notify;
2456 }
2457
2458 /**
2459 * clutter_evdev_set_keyboard_repeat:
2460 * @evdev: the #ClutterDeviceManager created by the evdev backend
2461 * @repeat: whether to enable or disable keyboard repeat events
2462 * @delay: the delay in ms between the hardware key press event and
2463 * the first synthetic event
2464 * @interval: the period in ms between consecutive synthetic key
2465 * press events
2466 *
2467 * Enables or disables sythetic key press events, allowing for initial
2468 * delay and interval period to be specified.
2469 *
2470 * Since: 1.18
2471 * Stability: unstable
2472 */
2473 void
2474 clutter_evdev_set_keyboard_repeat (ClutterDeviceManager *evdev,
2475 gboolean repeat,
2476 guint32 delay,
2477 guint32 interval)
2478 {
2479 ClutterDeviceManagerEvdev *manager_evdev;
2480 ClutterSeatEvdev *seat;
2481
2482 g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER_EVDEV (evdev));
2483
2484 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (evdev);
2485 seat = manager_evdev->priv->main_seat;
2486
2487 seat->repeat = repeat;
2488 seat->repeat_delay = delay;
2489 seat->repeat_interval = interval;
2490 }
2491
2492 /**
2493 * clutter_evdev_add_filter: (skip)
2494 * @func: (closure data): a filter function
2495 * @data: (allow-none): user data to be passed to the filter function, or %NULL
2496 * @destroy_notify: (allow-none): function to call on @data when the filter is removed, or %NULL
2497 *
2498 * Adds an event filter function.
2499 *
2500 * Since: 1.20
2501 * Stability: unstable
2502 */
2503 void
2504 clutter_evdev_add_filter (ClutterEvdevFilterFunc func,
2505 gpointer data,
2506 GDestroyNotify destroy_notify)
2507 {
2508 ClutterDeviceManagerEvdev *manager_evdev;
2509 ClutterDeviceManager *manager;
2510 ClutterEventFilter *filter;
2511
2512 g_return_if_fail (func != NULL);
2513
2514 manager = clutter_device_manager_get_default ();
2515
2516 if (!CLUTTER_IS_DEVICE_MANAGER_EVDEV (manager))
2517 {
2518 g_critical ("The Clutter input backend is not a evdev backend");
2519 return;
2520 }
2521
2522 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager);
2523
2524 filter = g_new0 (ClutterEventFilter, 1);
2525 filter->func = func;
2526 filter->data = data;
2527 filter->destroy_notify = destroy_notify;
2528
2529 manager_evdev->priv->event_filters =
2530 g_slist_append (manager_evdev->priv->event_filters, filter);
2531 }
2532
2533 /**
2534 * clutter_evdev_remove_filter: (skip)
2535 * @func: a filter function
2536 * @data: (allow-none): user data to be passed to the filter function, or %NULL
2537 *
2538 * Removes the given filter function.
2539 *
2540 * Since: 1.20
2541 * Stability: unstable
2542 */
2543 void
2544 clutter_evdev_remove_filter (ClutterEvdevFilterFunc func,
2545 gpointer data)
2546 {
2547 ClutterDeviceManagerEvdev *manager_evdev;
2548 ClutterDeviceManager *manager;
2549 ClutterEventFilter *filter;
2550 GSList *tmp_list;
2551
2552 g_return_if_fail (func != NULL);
2553
2554 manager = clutter_device_manager_get_default ();
2555
2556 if (!CLUTTER_IS_DEVICE_MANAGER_EVDEV (manager))
2557 {
2558 g_critical ("The Clutter input backend is not a evdev backend");
2559 return;
2560 }
2561
2562 manager_evdev = CLUTTER_DEVICE_MANAGER_EVDEV (manager);
2563 tmp_list = manager_evdev->priv->event_filters;
2564
2565 while (tmp_list)
2566 {
2567 filter = tmp_list->data;
2568
2569 if (filter->func == func && filter->data == data)
2570 {
2571 if (filter->destroy_notify)
2572 filter->destroy_notify (filter->data);
2573 g_free (filter);
2574 manager_evdev->priv->event_filters =
2575 g_slist_delete_link (manager_evdev->priv->event_filters, tmp_list);
2576 return;
2577 }
2578
2579 tmp_list = tmp_list->next;
2580 }
2581 }
2582
2583 /**
2584 * clutter_evdev_warp_pointer:
2585 * @pointer_device: the pointer device to warp
2586 * @time: the timestamp for the warp event
2587 * @x: the new X position of the pointer
2588 * @y: the new Y position of the pointer
2589 *
2590 * Warps the pointer to a new location. Technically, this is
2591 * processed the same way as an absolute motion event from
2592 * libinput: it simply generates an absolute motion event that
2593 * will be processed on the next iteration of the mainloop.
2594 *
2595 * The intended use for this is for display servers that need
2596 * to warp cursor the cursor to a new location.
2597 *
2598 * Since: 1.20
2599 * Stability: unstable
2600 */
2601 void
2602 clutter_evdev_warp_pointer (ClutterInputDevice *pointer_device,
2603 guint32 time_,
2604 int x,
2605 int y)
2606 {
2607 notify_absolute_motion (pointer_device, ms2us(time_), x, y);
2608 }