evdev: hook up a generic enable/disable interface for devices
[platform/upstream/libinput.git] / src / evdev.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "linux/input.h"
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <mtdev-plumbing.h>
33 #include <assert.h>
34 #include <time.h>
35 #include <math.h>
36
37 #include "libinput.h"
38 #include "evdev.h"
39 #include "filter.h"
40 #include "libinput-private.h"
41
42 #define DEFAULT_AXIS_STEP_DISTANCE 10
43
44 enum evdev_key_type {
45         EVDEV_KEY_TYPE_NONE,
46         EVDEV_KEY_TYPE_KEY,
47         EVDEV_KEY_TYPE_BUTTON,
48 };
49
50 static void
51 hw_set_key_down(struct evdev_device *device, int code, int pressed)
52 {
53         long_set_bit_state(device->hw_key_mask, code, pressed);
54 }
55
56 static int
57 hw_is_key_down(struct evdev_device *device, int code)
58 {
59         return long_bit_is_set(device->hw_key_mask, code);
60 }
61
62 static int
63 get_key_down_count(struct evdev_device *device, int code)
64 {
65         return device->key_count[code];
66 }
67
68 static int
69 update_key_down_count(struct evdev_device *device, int code, int pressed)
70 {
71         int key_count;
72         assert(code >= 0 && code < KEY_CNT);
73
74         if (pressed) {
75                 key_count = ++device->key_count[code];
76         } else {
77                 assert(device->key_count[code] > 0);
78                 key_count = --device->key_count[code];
79         }
80
81         if (key_count > 32) {
82                 log_bug_libinput(device->base.seat->libinput,
83                                  "Key count for %s reached abnormal values\n",
84                                  libevdev_event_code_get_name(EV_KEY, code));
85         }
86
87         return key_count;
88 }
89
90 void
91 evdev_keyboard_notify_key(struct evdev_device *device,
92                           uint32_t time,
93                           int key,
94                           enum libinput_key_state state)
95 {
96         int down_count;
97
98         down_count = update_key_down_count(device, key, state);
99
100         if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
101             (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
102                 keyboard_notify_key(&device->base, time, key, state);
103 }
104
105 void
106 evdev_pointer_notify_button(struct evdev_device *device,
107                             uint32_t time,
108                             int button,
109                             enum libinput_button_state state)
110 {
111         int down_count;
112
113         down_count = update_key_down_count(device, button, state);
114
115         if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
116             (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0))
117                 pointer_notify_button(&device->base, time, button, state);
118 }
119
120 void
121 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
122 {
123         static const struct {
124                 enum libinput_led weston;
125                 int evdev;
126         } map[] = {
127                 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
128                 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
129                 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
130         };
131         struct input_event ev[ARRAY_LENGTH(map) + 1];
132         unsigned int i;
133
134         if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
135                 return;
136
137         memset(ev, 0, sizeof(ev));
138         for (i = 0; i < ARRAY_LENGTH(map); i++) {
139                 ev[i].type = EV_LED;
140                 ev[i].code = map[i].evdev;
141                 ev[i].value = !!(leds & map[i].weston);
142         }
143         ev[i].type = EV_SYN;
144         ev[i].code = SYN_REPORT;
145
146         i = write(device->fd, ev, sizeof ev);
147         (void)i; /* no, we really don't care about the return value */
148 }
149
150 static void
151 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
152 {
153         if (!device->abs.apply_calibration)
154                 return;
155
156         matrix_mult_vec(&device->abs.calibration, x, y);
157 }
158
159 static inline double
160 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
161 {
162         return (val - absinfo->minimum) * to_range /
163                 (absinfo->maximum - absinfo->minimum + 1);
164 }
165
166 double
167 evdev_device_transform_x(struct evdev_device *device,
168                          double x,
169                          uint32_t width)
170 {
171         return scale_axis(device->abs.absinfo_x, x, width);
172 }
173
174 double
175 evdev_device_transform_y(struct evdev_device *device,
176                          double y,
177                          uint32_t height)
178 {
179         return scale_axis(device->abs.absinfo_y, y, height);
180 }
181
182 static void
183 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
184 {
185         struct libinput *libinput = device->base.seat->libinput;
186         struct motion_params motion;
187         int32_t cx, cy;
188         int32_t x, y;
189         int slot;
190         int seat_slot;
191         struct libinput_device *base = &device->base;
192         struct libinput_seat *seat = base->seat;
193
194         slot = device->mt.slot;
195
196         switch (device->pending_event) {
197         case EVDEV_NONE:
198                 return;
199         case EVDEV_RELATIVE_MOTION:
200                 motion.dx = device->rel.dx;
201                 motion.dy = device->rel.dy;
202                 device->rel.dx = 0;
203                 device->rel.dy = 0;
204
205                 /* Apply pointer acceleration. */
206                 filter_dispatch(device->pointer.filter, &motion, device, time);
207
208                 if (motion.dx == 0.0 && motion.dy == 0.0)
209                         break;
210
211                 pointer_notify_motion(base, time, motion.dx, motion.dy);
212                 break;
213         case EVDEV_ABSOLUTE_MT_DOWN:
214                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
215                         break;
216
217                 if (device->mt.slots[slot].seat_slot != -1) {
218                         log_bug_kernel(libinput,
219                                        "%s: Driver sent multiple touch down for the "
220                                        "same slot", device->devnode);
221                         break;
222                 }
223
224                 seat_slot = ffs(~seat->slot_map) - 1;
225                 device->mt.slots[slot].seat_slot = seat_slot;
226
227                 if (seat_slot == -1)
228                         break;
229
230                 seat->slot_map |= 1 << seat_slot;
231                 x = device->mt.slots[slot].x;
232                 y = device->mt.slots[slot].y;
233                 transform_absolute(device, &x, &y);
234
235                 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
236                 break;
237         case EVDEV_ABSOLUTE_MT_MOTION:
238                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
239                         break;
240
241                 seat_slot = device->mt.slots[slot].seat_slot;
242                 x = device->mt.slots[slot].x;
243                 y = device->mt.slots[slot].y;
244
245                 if (seat_slot == -1)
246                         break;
247
248                 transform_absolute(device, &x, &y);
249                 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
250                 break;
251         case EVDEV_ABSOLUTE_MT_UP:
252                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
253                         break;
254
255                 seat_slot = device->mt.slots[slot].seat_slot;
256                 device->mt.slots[slot].seat_slot = -1;
257
258                 if (seat_slot == -1)
259                         break;
260
261                 seat->slot_map &= ~(1 << seat_slot);
262
263                 touch_notify_touch_up(base, time, slot, seat_slot);
264                 break;
265         case EVDEV_ABSOLUTE_TOUCH_DOWN:
266                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
267                         break;
268
269                 if (device->abs.seat_slot != -1) {
270                         log_bug_kernel(libinput,
271                                        "%s: Driver sent multiple touch down for the "
272                                        "same slot", device->devnode);
273                         break;
274                 }
275
276                 seat_slot = ffs(~seat->slot_map) - 1;
277                 device->abs.seat_slot = seat_slot;
278
279                 if (seat_slot == -1)
280                         break;
281
282                 seat->slot_map |= 1 << seat_slot;
283
284                 cx = device->abs.x;
285                 cy = device->abs.y;
286                 transform_absolute(device, &cx, &cy);
287
288                 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
289                 break;
290         case EVDEV_ABSOLUTE_MOTION:
291                 cx = device->abs.x;
292                 cy = device->abs.y;
293                 transform_absolute(device, &cx, &cy);
294                 x = cx;
295                 y = cy;
296
297                 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
298                         seat_slot = device->abs.seat_slot;
299
300                         if (seat_slot == -1)
301                                 break;
302
303                         touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
304                 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
305                         pointer_notify_motion_absolute(base, time, x, y);
306                 }
307                 break;
308         case EVDEV_ABSOLUTE_TOUCH_UP:
309                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
310                         break;
311
312                 seat_slot = device->abs.seat_slot;
313                 device->abs.seat_slot = -1;
314
315                 if (seat_slot == -1)
316                         break;
317
318                 seat->slot_map &= ~(1 << seat_slot);
319
320                 touch_notify_touch_up(base, time, -1, seat_slot);
321                 break;
322         default:
323                 assert(0 && "Unknown pending event type");
324                 break;
325         }
326
327         device->pending_event = EVDEV_NONE;
328 }
329
330 static enum evdev_key_type
331 get_key_type(uint16_t code)
332 {
333         if (code == BTN_TOUCH)
334                 return EVDEV_KEY_TYPE_NONE;
335
336         if (code >= KEY_ESC && code <= KEY_MICMUTE)
337                 return EVDEV_KEY_TYPE_KEY;
338         if (code >= BTN_MISC && code <= BTN_GEAR_UP)
339                 return EVDEV_KEY_TYPE_BUTTON;
340         if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
341                 return EVDEV_KEY_TYPE_KEY;
342         if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
343                 return EVDEV_KEY_TYPE_BUTTON;
344         return EVDEV_KEY_TYPE_NONE;
345 }
346
347 static void
348 evdev_process_touch_button(struct evdev_device *device,
349                            uint64_t time, int value)
350 {
351         if (device->pending_event != EVDEV_NONE &&
352             device->pending_event != EVDEV_ABSOLUTE_MOTION)
353                 evdev_flush_pending_event(device, time);
354
355         device->pending_event = (value ?
356                                  EVDEV_ABSOLUTE_TOUCH_DOWN :
357                                  EVDEV_ABSOLUTE_TOUCH_UP);
358 }
359
360 static inline void
361 evdev_process_key(struct evdev_device *device,
362                   struct input_event *e, uint64_t time)
363 {
364         enum evdev_key_type type;
365
366         /* ignore kernel key repeat */
367         if (e->value == 2)
368                 return;
369
370         if (e->code == BTN_TOUCH) {
371                 if (!device->is_mt)
372                         evdev_process_touch_button(device, time, e->value);
373                 return;
374         }
375
376         evdev_flush_pending_event(device, time);
377
378         type = get_key_type(e->code);
379
380         /* Ignore key release events from the kernel for keys that libinput
381          * never got a pressed event for. */
382         if (e->value == 0) {
383                 switch (type) {
384                 case EVDEV_KEY_TYPE_NONE:
385                         break;
386                 case EVDEV_KEY_TYPE_KEY:
387                 case EVDEV_KEY_TYPE_BUTTON:
388                         if (!hw_is_key_down(device, e->code))
389                                 return;
390                 }
391         }
392
393         hw_set_key_down(device, e->code, e->value);
394
395         switch (type) {
396         case EVDEV_KEY_TYPE_NONE:
397                 break;
398         case EVDEV_KEY_TYPE_KEY:
399                 evdev_keyboard_notify_key(
400                         device,
401                         time,
402                         e->code,
403                         e->value ? LIBINPUT_KEY_STATE_PRESSED :
404                                    LIBINPUT_KEY_STATE_RELEASED);
405                 break;
406         case EVDEV_KEY_TYPE_BUTTON:
407                 evdev_pointer_notify_button(
408                         device,
409                         time,
410                         e->code,
411                         e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
412                                    LIBINPUT_BUTTON_STATE_RELEASED);
413                 break;
414         }
415 }
416
417 static void
418 evdev_process_touch(struct evdev_device *device,
419                     struct input_event *e,
420                     uint64_t time)
421 {
422         switch (e->code) {
423         case ABS_MT_SLOT:
424                 evdev_flush_pending_event(device, time);
425                 device->mt.slot = e->value;
426                 break;
427         case ABS_MT_TRACKING_ID:
428                 if (device->pending_event != EVDEV_NONE &&
429                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
430                         evdev_flush_pending_event(device, time);
431                 if (e->value >= 0)
432                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
433                 else
434                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
435                 break;
436         case ABS_MT_POSITION_X:
437                 device->mt.slots[device->mt.slot].x = e->value;
438                 if (device->pending_event == EVDEV_NONE)
439                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
440                 break;
441         case ABS_MT_POSITION_Y:
442                 device->mt.slots[device->mt.slot].y = e->value;
443                 if (device->pending_event == EVDEV_NONE)
444                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
445                 break;
446         }
447 }
448
449 static inline void
450 evdev_process_absolute_motion(struct evdev_device *device,
451                               struct input_event *e)
452 {
453         switch (e->code) {
454         case ABS_X:
455                 device->abs.x = e->value;
456                 if (device->pending_event == EVDEV_NONE)
457                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
458                 break;
459         case ABS_Y:
460                 device->abs.y = e->value;
461                 if (device->pending_event == EVDEV_NONE)
462                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
463                 break;
464         }
465 }
466
467 static inline void
468 evdev_process_relative(struct evdev_device *device,
469                        struct input_event *e, uint64_t time)
470 {
471         struct libinput_device *base = &device->base;
472
473         switch (e->code) {
474         case REL_X:
475                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
476                         evdev_flush_pending_event(device, time);
477                 device->rel.dx += e->value;
478                 device->pending_event = EVDEV_RELATIVE_MOTION;
479                 break;
480         case REL_Y:
481                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
482                         evdev_flush_pending_event(device, time);
483                 device->rel.dy += e->value;
484                 device->pending_event = EVDEV_RELATIVE_MOTION;
485                 break;
486         case REL_WHEEL:
487                 evdev_flush_pending_event(device, time);
488                 pointer_notify_axis(
489                         base,
490                         time,
491                         LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
492                         -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
493                 break;
494         case REL_HWHEEL:
495                 evdev_flush_pending_event(device, time);
496                 switch (e->value) {
497                 case -1:
498                         /* Scroll left */
499                 case 1:
500                         /* Scroll right */
501                         pointer_notify_axis(
502                                 base,
503                                 time,
504                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
505                                 e->value * DEFAULT_AXIS_STEP_DISTANCE);
506                         break;
507                 default:
508                         break;
509
510                 }
511         }
512 }
513
514 static inline void
515 evdev_process_absolute(struct evdev_device *device,
516                        struct input_event *e,
517                        uint64_t time)
518 {
519         if (device->is_mt) {
520                 evdev_process_touch(device, e, time);
521         } else {
522                 evdev_process_absolute_motion(device, e);
523         }
524 }
525
526 static inline int
527 evdev_need_touch_frame(struct evdev_device *device)
528 {
529         if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
530                 return 0;
531
532         switch (device->pending_event) {
533         case EVDEV_NONE:
534         case EVDEV_RELATIVE_MOTION:
535                 break;
536         case EVDEV_ABSOLUTE_MT_DOWN:
537         case EVDEV_ABSOLUTE_MT_MOTION:
538         case EVDEV_ABSOLUTE_MT_UP:
539         case EVDEV_ABSOLUTE_TOUCH_DOWN:
540         case EVDEV_ABSOLUTE_TOUCH_UP:
541         case EVDEV_ABSOLUTE_MOTION:
542                 return 1;
543         }
544
545         return 0;
546 }
547
548 static void
549 fallback_process(struct evdev_dispatch *dispatch,
550                  struct evdev_device *device,
551                  struct input_event *event,
552                  uint64_t time)
553 {
554         int need_frame = 0;
555
556         switch (event->type) {
557         case EV_REL:
558                 evdev_process_relative(device, event, time);
559                 break;
560         case EV_ABS:
561                 evdev_process_absolute(device, event, time);
562                 break;
563         case EV_KEY:
564                 evdev_process_key(device, event, time);
565                 break;
566         case EV_SYN:
567                 need_frame = evdev_need_touch_frame(device);
568                 evdev_flush_pending_event(device, time);
569                 if (need_frame)
570                         touch_notify_frame(&device->base, time);
571                 break;
572         }
573 }
574
575 static void
576 fallback_destroy(struct evdev_dispatch *dispatch)
577 {
578         free(dispatch);
579 }
580
581 static int
582 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
583 {
584         struct evdev_device *device = (struct evdev_device*)libinput_device;
585
586         return device->abs.absinfo_x && device->abs.absinfo_y;
587 }
588
589 static enum libinput_config_status
590 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
591                              const float matrix[6])
592 {
593         struct evdev_device *device = (struct evdev_device*)libinput_device;
594
595         evdev_device_calibrate(device, matrix);
596
597         return LIBINPUT_CONFIG_STATUS_SUCCESS;
598 }
599
600 static int
601 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
602                              float matrix[6])
603 {
604         struct evdev_device *device = (struct evdev_device*)libinput_device;
605
606         matrix_to_farray6(&device->abs.usermatrix, matrix);
607
608         return !matrix_is_identity(&device->abs.usermatrix);
609 }
610
611 static int
612 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
613                                      float matrix[6])
614 {
615         struct evdev_device *device = (struct evdev_device*)libinput_device;
616
617         matrix_to_farray6(&device->abs.default_calibration, matrix);
618
619         return !matrix_is_identity(&device->abs.default_calibration);
620 }
621
622 struct evdev_dispatch_interface fallback_interface = {
623         fallback_process,
624         fallback_destroy
625 };
626
627 static uint32_t
628 evdev_sendevents_get_modes(struct libinput_device *device)
629 {
630         return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED |
631                LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
632 }
633
634 static enum libinput_config_status
635 evdev_sendevents_set_mode(struct libinput_device *device,
636                           enum libinput_config_send_events_mode mode)
637 {
638         struct evdev_device *evdev = (struct evdev_device*)device;
639         struct evdev_dispatch *dispatch = evdev->dispatch;
640
641         if (mode == dispatch->sendevents.current_mode)
642                 return LIBINPUT_CONFIG_STATUS_SUCCESS;
643
644         switch(mode) {
645         case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
646                 evdev_device_resume(evdev);
647                 break;
648         case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
649                 evdev_device_suspend(evdev);
650                 break;
651         default:
652                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
653         }
654
655         dispatch->sendevents.current_mode = mode;
656
657         return LIBINPUT_CONFIG_STATUS_SUCCESS;
658 }
659
660 static enum libinput_config_send_events_mode
661 evdev_sendevents_get_mode(struct libinput_device *device)
662 {
663         struct evdev_device *evdev = (struct evdev_device*)device;
664         struct evdev_dispatch *dispatch = evdev->dispatch;
665
666         return dispatch->sendevents.current_mode;
667 }
668
669 static enum libinput_config_send_events_mode
670 evdev_sendevents_get_default_mode(struct libinput_device *device)
671 {
672         return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
673 }
674
675 static struct evdev_dispatch *
676 fallback_dispatch_create(struct libinput_device *device)
677 {
678         struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
679         if (dispatch == NULL)
680                 return NULL;
681
682         dispatch->interface = &fallback_interface;
683
684         device->config.calibration = &dispatch->calibration;
685
686         dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
687         dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
688         dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
689         dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
690
691         device->config.sendevents = &dispatch->sendevents.config;
692
693         dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
694         dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
695         dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
696         dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
697         dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
698
699         return dispatch;
700 }
701
702 static inline void
703 evdev_process_event(struct evdev_device *device, struct input_event *e)
704 {
705         struct evdev_dispatch *dispatch = device->dispatch;
706         uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
707
708         dispatch->interface->process(dispatch, device, e, time);
709 }
710
711 static inline void
712 evdev_device_dispatch_one(struct evdev_device *device,
713                           struct input_event *ev)
714 {
715         if (!device->mtdev) {
716                 evdev_process_event(device, ev);
717         } else {
718                 mtdev_put_event(device->mtdev, ev);
719                 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
720                         while (!mtdev_empty(device->mtdev)) {
721                                 struct input_event e;
722                                 mtdev_get_event(device->mtdev, &e);
723                                 evdev_process_event(device, &e);
724                         }
725                 }
726         }
727 }
728
729 static int
730 evdev_sync_device(struct evdev_device *device)
731 {
732         struct input_event ev;
733         int rc;
734
735         do {
736                 rc = libevdev_next_event(device->evdev,
737                                          LIBEVDEV_READ_FLAG_SYNC, &ev);
738                 if (rc < 0)
739                         break;
740                 evdev_device_dispatch_one(device, &ev);
741         } while (rc == LIBEVDEV_READ_STATUS_SYNC);
742
743         return rc == -EAGAIN ? 0 : rc;
744 }
745
746 static void
747 evdev_device_dispatch(void *data)
748 {
749         struct evdev_device *device = data;
750         struct libinput *libinput = device->base.seat->libinput;
751         struct input_event ev;
752         int rc;
753
754         /* If the compositor is repainting, this function is called only once
755          * per frame and we have to process all the events available on the
756          * fd, otherwise there will be input lag. */
757         do {
758                 rc = libevdev_next_event(device->evdev,
759                                          LIBEVDEV_READ_FLAG_NORMAL, &ev);
760                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
761                         /* send one more sync event so we handle all
762                            currently pending events before we sync up
763                            to the current state */
764                         ev.code = SYN_REPORT;
765                         evdev_device_dispatch_one(device, &ev);
766
767                         rc = evdev_sync_device(device);
768                         if (rc == 0)
769                                 rc = LIBEVDEV_READ_STATUS_SUCCESS;
770                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
771                         evdev_device_dispatch_one(device, &ev);
772                 }
773         } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
774
775         if (rc != -EAGAIN && rc != -EINTR) {
776                 libinput_remove_source(libinput, device->source);
777                 device->source = NULL;
778         }
779 }
780
781 static int
782 configure_pointer_acceleration(struct evdev_device *device)
783 {
784         device->pointer.filter =
785                 create_pointer_accelator_filter(
786                         pointer_accel_profile_smooth_simple);
787         if (!device->pointer.filter)
788                 return -1;
789
790         return 0;
791 }
792
793 static inline int
794 evdev_need_mtdev(struct evdev_device *device)
795 {
796         struct libevdev *evdev = device->evdev;
797
798         return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
799                 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
800                 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
801 }
802
803 static int
804 evdev_configure_device(struct evdev_device *device)
805 {
806         struct libinput *libinput = device->base.seat->libinput;
807         struct libevdev *evdev = device->evdev;
808         const struct input_absinfo *absinfo;
809         struct input_absinfo fixed;
810         int has_abs, has_rel, has_mt;
811         int has_button, has_keyboard, has_touch;
812         struct mt_slot *slots;
813         int num_slots;
814         int active_slot;
815         int slot;
816         unsigned int i;
817
818         has_rel = 0;
819         has_abs = 0;
820         has_mt = 0;
821         has_button = 0;
822         has_keyboard = 0;
823         has_touch = 0;
824
825         if (libevdev_has_event_type(evdev, EV_ABS)) {
826
827                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
828                         if (absinfo->resolution == 0) {
829                                 fixed = *absinfo;
830                                 fixed.resolution = 1;
831                                 libevdev_set_abs_info(evdev, ABS_X, &fixed);
832                                 device->abs.fake_resolution = 1;
833                         }
834                         device->abs.absinfo_x = absinfo;
835                         has_abs = 1;
836                 }
837                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
838                         if (absinfo->resolution == 0) {
839                                 fixed = *absinfo;
840                                 fixed.resolution = 1;
841                                 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
842                                 device->abs.fake_resolution = 1;
843                         }
844                         device->abs.absinfo_y = absinfo;
845                         has_abs = 1;
846                 }
847                 /* We only handle the slotted Protocol B in weston.
848                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
849                    require mtdev for conversion. */
850                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
851                     libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
852                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
853                         if (absinfo->resolution == 0) {
854                                 fixed = *absinfo;
855                                 fixed.resolution = 1;
856                                 libevdev_set_abs_info(evdev,
857                                                       ABS_MT_POSITION_X,
858                                                       &fixed);
859                                 device->abs.fake_resolution = 1;
860                         }
861                         device->abs.absinfo_x = absinfo;
862                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
863                         if (absinfo->resolution == 0) {
864                                 fixed = *absinfo;
865                                 fixed.resolution = 1;
866                                 libevdev_set_abs_info(evdev,
867                                                       ABS_MT_POSITION_Y,
868                                                       &fixed);
869                                 device->abs.fake_resolution = 1;
870                         }
871                         device->abs.absinfo_y = absinfo;
872                         device->is_mt = 1;
873                         has_touch = 1;
874                         has_mt = 1;
875
876                         if (evdev_need_mtdev(device)) {
877                                 device->mtdev = mtdev_new_open(device->fd);
878                                 if (!device->mtdev)
879                                         return -1;
880
881                                 num_slots = device->mtdev->caps.slot.maximum;
882                                 if (device->mtdev->caps.slot.minimum < 0 ||
883                                     num_slots <= 0)
884                                         return -1;
885                                 active_slot = device->mtdev->caps.slot.value;
886                         } else {
887                                 num_slots = libevdev_get_num_slots(device->evdev);
888                                 active_slot = libevdev_get_current_slot(evdev);
889                         }
890
891                         slots = calloc(num_slots, sizeof(struct mt_slot));
892                         if (!slots)
893                                 return -1;
894
895                         for (slot = 0; slot < num_slots; ++slot) {
896                                 slots[slot].seat_slot = -1;
897                                 slots[slot].x = 0;
898                                 slots[slot].y = 0;
899                         }
900                         device->mt.slots = slots;
901                         device->mt.slots_len = num_slots;
902                         device->mt.slot = active_slot;
903                 }
904         }
905         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
906             libevdev_has_event_code(evdev, EV_REL, REL_Y))
907                 has_rel = 1;
908
909         if (libevdev_has_event_type(evdev, EV_KEY)) {
910                 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
911                     libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
912                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
913                     (has_abs || has_mt)) {
914                         device->dispatch = evdev_mt_touchpad_create(device);
915                         log_info(libinput,
916                                  "input device '%s', %s is a touchpad\n",
917                                  device->devname, device->devnode);
918                         return device->dispatch == NULL ? -1 : 0;
919                 }
920
921                 for (i = 0; i < KEY_MAX; i++) {
922                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
923                                 switch (get_key_type(i)) {
924                                 case EVDEV_KEY_TYPE_NONE:
925                                         break;
926                                 case EVDEV_KEY_TYPE_KEY:
927                                         has_keyboard = 1;
928                                         break;
929                                 case EVDEV_KEY_TYPE_BUTTON:
930                                         has_button = 1;
931                                         break;
932                                 }
933                         }
934                 }
935
936                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
937                         has_touch = 1;
938         }
939         if (libevdev_has_event_type(evdev, EV_LED))
940                 has_keyboard = 1;
941
942         if ((has_abs || has_rel) && has_button) {
943                 if (configure_pointer_acceleration(device) == -1)
944                         return -1;
945
946                 device->seat_caps |= EVDEV_DEVICE_POINTER;
947
948                 log_info(libinput,
949                          "input device '%s', %s is a pointer caps =%s%s%s\n",
950                          device->devname, device->devnode,
951                          has_abs ? " absolute-motion" : "",
952                          has_rel ? " relative-motion": "",
953                          has_button ? " button" : "");
954         }
955         if (has_keyboard) {
956                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
957                 log_info(libinput,
958                          "input device '%s', %s is a keyboard\n",
959                          device->devname, device->devnode);
960         }
961         if (has_touch && !has_button) {
962                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
963                 log_info(libinput,
964                          "input device '%s', %s is a touch device\n",
965                          device->devname, device->devnode);
966         }
967
968         return 0;
969 }
970
971 struct evdev_device *
972 evdev_device_create(struct libinput_seat *seat,
973                     const char *devnode,
974                     const char *sysname)
975 {
976         struct libinput *libinput = seat->libinput;
977         struct evdev_device *device;
978         int rc;
979         int fd;
980         int unhandled_device = 0;
981
982         /* Use non-blocking mode so that we can loop on read on
983          * evdev_device_data() until all events on the fd are
984          * read.  mtdev_get() also expects this. */
985         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
986         if (fd < 0) {
987                 log_info(libinput,
988                          "opening input device '%s' failed (%s).\n",
989                          devnode, strerror(-fd));
990                 return NULL;
991         }
992
993         device = zalloc(sizeof *device);
994         if (device == NULL)
995                 return NULL;
996
997         libinput_device_init(&device->base, seat);
998         libinput_seat_ref(seat);
999
1000         rc = libevdev_new_from_fd(fd, &device->evdev);
1001         if (rc != 0)
1002                 goto err;
1003
1004         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1005
1006         device->seat_caps = 0;
1007         device->is_mt = 0;
1008         device->mtdev = NULL;
1009         device->devnode = strdup(devnode);
1010         device->sysname = strdup(sysname);
1011         device->rel.dx = 0;
1012         device->rel.dy = 0;
1013         device->abs.seat_slot = -1;
1014         device->dispatch = NULL;
1015         device->fd = fd;
1016         device->pending_event = EVDEV_NONE;
1017         device->devname = libevdev_get_name(device->evdev);
1018
1019         matrix_init_identity(&device->abs.calibration);
1020         matrix_init_identity(&device->abs.usermatrix);
1021         matrix_init_identity(&device->abs.default_calibration);
1022
1023         if (evdev_configure_device(device) == -1)
1024                 goto err;
1025
1026         if (device->seat_caps == 0) {
1027                 unhandled_device = 1;
1028                 goto err;
1029         }
1030
1031         /* If the dispatch was not set up use the fallback. */
1032         if (device->dispatch == NULL)
1033                 device->dispatch = fallback_dispatch_create(&device->base);
1034         if (device->dispatch == NULL)
1035                 goto err;
1036
1037         device->source =
1038                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1039         if (!device->source)
1040                 goto err;
1041
1042         list_insert(seat->devices_list.prev, &device->base.link);
1043         notify_added_device(&device->base);
1044
1045         return device;
1046
1047 err:
1048         if (fd >= 0)
1049                 close_restricted(libinput, fd);
1050         evdev_device_destroy(device);
1051
1052         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
1053 }
1054
1055 int
1056 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1057 {
1058         memset(keys, 0, size);
1059         return 0;
1060 }
1061
1062 const char *
1063 evdev_device_get_output(struct evdev_device *device)
1064 {
1065         return device->output_name;
1066 }
1067
1068 const char *
1069 evdev_device_get_sysname(struct evdev_device *device)
1070 {
1071         return device->sysname;
1072 }
1073
1074 const char *
1075 evdev_device_get_name(struct evdev_device *device)
1076 {
1077         return device->devname;
1078 }
1079
1080 unsigned int
1081 evdev_device_get_id_product(struct evdev_device *device)
1082 {
1083         return libevdev_get_id_product(device->evdev);
1084 }
1085
1086 unsigned int
1087 evdev_device_get_id_vendor(struct evdev_device *device)
1088 {
1089         return libevdev_get_id_vendor(device->evdev);
1090 }
1091
1092 void
1093 evdev_device_set_default_calibration(struct evdev_device *device,
1094                                      const float calibration[6])
1095 {
1096         matrix_from_farray6(&device->abs.default_calibration, calibration);
1097         evdev_device_calibrate(device, calibration);
1098 }
1099
1100 void
1101 evdev_device_calibrate(struct evdev_device *device,
1102                        const float calibration[6])
1103 {
1104         struct matrix scale,
1105                       translate,
1106                       transform;
1107         double sx, sy;
1108
1109         matrix_from_farray6(&transform, calibration);
1110         device->abs.apply_calibration = !matrix_is_identity(&transform);
1111
1112         if (!device->abs.apply_calibration) {
1113                 matrix_init_identity(&device->abs.calibration);
1114                 return;
1115         }
1116
1117         sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1118         sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1119
1120         /* The transformation matrix is in the form:
1121          *  [ a b c ]
1122          *  [ d e f ]
1123          *  [ 0 0 1 ]
1124          * Where a, e are the scale components, a, b, d, e are the rotation
1125          * component (combined with scale) and c and f are the translation
1126          * component. The translation component in the input matrix must be
1127          * normalized to multiples of the device width and height,
1128          * respectively. e.g. c == 1 shifts one device-width to the right.
1129          *
1130          * We pre-calculate a single matrix to apply to event coordinates:
1131          *     M = Un-Normalize * Calibration * Normalize
1132          *
1133          * Normalize: scales the device coordinates to [0,1]
1134          * Calibration: user-supplied matrix
1135          * Un-Normalize: scales back up to device coordinates
1136          * Matrix maths requires the normalize/un-normalize in reverse
1137          * order.
1138          */
1139
1140         /* back up the user matrix so we can return it on request */
1141         matrix_from_farray6(&device->abs.usermatrix, calibration);
1142
1143         /* Un-Normalize */
1144         matrix_init_translate(&translate,
1145                               device->abs.absinfo_x->minimum,
1146                               device->abs.absinfo_y->minimum);
1147         matrix_init_scale(&scale, sx, sy);
1148         matrix_mult(&scale, &translate, &scale);
1149
1150         /* Calibration */
1151         matrix_mult(&transform, &scale, &transform);
1152
1153         /* Normalize */
1154         matrix_init_translate(&translate,
1155                               -device->abs.absinfo_x->minimum/sx,
1156                               -device->abs.absinfo_y->minimum/sy);
1157         matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1158         matrix_mult(&scale, &translate, &scale);
1159
1160         /* store final matrix in device */
1161         matrix_mult(&device->abs.calibration, &transform, &scale);
1162 }
1163
1164 int
1165 evdev_device_has_capability(struct evdev_device *device,
1166                             enum libinput_device_capability capability)
1167 {
1168         switch (capability) {
1169         case LIBINPUT_DEVICE_CAP_POINTER:
1170                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1171         case LIBINPUT_DEVICE_CAP_KEYBOARD:
1172                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1173         case LIBINPUT_DEVICE_CAP_TOUCH:
1174                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1175         default:
1176                 return 0;
1177         }
1178 }
1179
1180 int
1181 evdev_device_get_size(struct evdev_device *device,
1182                       double *width,
1183                       double *height)
1184 {
1185         const struct input_absinfo *x, *y;
1186
1187         x = libevdev_get_abs_info(device->evdev, ABS_X);
1188         y = libevdev_get_abs_info(device->evdev, ABS_Y);
1189
1190         if (!x || !y || device->abs.fake_resolution ||
1191             !x->resolution || !y->resolution)
1192                 return -1;
1193
1194         *width = evdev_convert_to_mm(x, x->maximum);
1195         *height = evdev_convert_to_mm(y, y->maximum);
1196
1197         return 0;
1198 }
1199
1200 static void
1201 release_pressed_keys(struct evdev_device *device)
1202 {
1203         struct libinput *libinput = device->base.seat->libinput;
1204         uint64_t time;
1205         int code;
1206
1207         if ((time = libinput_now(libinput)) == 0)
1208                 return;
1209
1210         for (code = 0; code < KEY_CNT; code++) {
1211                 if (get_key_down_count(device, code) > 0) {
1212                         switch (get_key_type(code)) {
1213                         case EVDEV_KEY_TYPE_NONE:
1214                                 break;
1215                         case EVDEV_KEY_TYPE_KEY:
1216                                 keyboard_notify_key(
1217                                         &device->base,
1218                                         time,
1219                                         code,
1220                                         LIBINPUT_KEY_STATE_RELEASED);
1221                                 break;
1222                         case EVDEV_KEY_TYPE_BUTTON:
1223                                 pointer_notify_button(
1224                                         &device->base,
1225                                         time,
1226                                         code,
1227                                         LIBINPUT_BUTTON_STATE_RELEASED);
1228                                 break;
1229                         }
1230                 }
1231         }
1232 }
1233
1234 int
1235 evdev_device_suspend(struct evdev_device *device)
1236 {
1237         if (device->source) {
1238                 libinput_remove_source(device->base.seat->libinput,
1239                                        device->source);
1240                 device->source = NULL;
1241         }
1242
1243         release_pressed_keys(device);
1244
1245         if (device->mtdev) {
1246                 mtdev_close_delete(device->mtdev);
1247                 device->mtdev = NULL;
1248         }
1249
1250         if (device->fd != -1) {
1251                 close_restricted(device->base.seat->libinput, device->fd);
1252                 device->fd = -1;
1253         }
1254
1255         return 0;
1256 }
1257
1258 int
1259 evdev_device_resume(struct evdev_device *device)
1260 {
1261         struct libinput *libinput = device->base.seat->libinput;
1262         int fd;
1263
1264         if (device->fd != -1)
1265                 return 0;
1266
1267         fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1268
1269         if (fd < 0)
1270                 return -errno;
1271
1272         device->fd = fd;
1273
1274         if (evdev_need_mtdev(device)) {
1275                 device->mtdev = mtdev_new_open(device->fd);
1276                 if (!device->mtdev)
1277                         return -ENODEV;
1278         }
1279
1280         device->source =
1281                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1282         if (!device->source) {
1283                 mtdev_close_delete(device->mtdev);
1284                 return -ENOMEM;
1285         }
1286
1287         memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1288
1289         return 0;
1290 }
1291
1292 void
1293 evdev_device_remove(struct evdev_device *device)
1294 {
1295         evdev_device_suspend(device);
1296
1297         list_remove(&device->base.link);
1298
1299         notify_removed_device(&device->base);
1300         libinput_device_unref(&device->base);
1301 }
1302
1303 void
1304 evdev_device_destroy(struct evdev_device *device)
1305 {
1306         struct evdev_dispatch *dispatch;
1307
1308         dispatch = device->dispatch;
1309         if (dispatch)
1310                 dispatch->interface->destroy(dispatch);
1311
1312         filter_destroy(device->pointer.filter);
1313         libinput_seat_unref(device->base.seat);
1314         libevdev_free(device->evdev);
1315         free(device->mt.slots);
1316         free(device->devnode);
1317         free(device->sysname);
1318         free(device);
1319 }