s/libinput_pointer_button_state/libinput_button_state/
[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 void
45 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
46 {
47         static const struct {
48                 enum libinput_led weston;
49                 int evdev;
50         } map[] = {
51                 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
52                 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
53                 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
54         };
55         struct input_event ev[ARRAY_LENGTH(map) + 1];
56         unsigned int i;
57
58         if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
59                 return;
60
61         memset(ev, 0, sizeof(ev));
62         for (i = 0; i < ARRAY_LENGTH(map); i++) {
63                 ev[i].type = EV_LED;
64                 ev[i].code = map[i].evdev;
65                 ev[i].value = !!(leds & map[i].weston);
66         }
67         ev[i].type = EV_SYN;
68         ev[i].code = SYN_REPORT;
69
70         i = write(device->fd, ev, sizeof ev);
71         (void)i; /* no, we really don't care about the return value */
72 }
73
74 static void
75 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
76 {
77         if (!device->abs.apply_calibration) {
78                 *x = device->abs.x;
79                 *y = device->abs.y;
80                 return;
81         } else {
82                 *x = device->abs.x * device->abs.calibration[0] +
83                         device->abs.y * device->abs.calibration[1] +
84                         device->abs.calibration[2];
85
86                 *y = device->abs.x * device->abs.calibration[3] +
87                         device->abs.y * device->abs.calibration[4] +
88                         device->abs.calibration[5];
89         }
90 }
91
92 double
93 evdev_device_transform_x(struct evdev_device *device,
94                          double x,
95                          uint32_t width)
96 {
97         return (x - device->abs.min_x) * width /
98                 (device->abs.max_x - device->abs.min_x + 1);
99 }
100
101 double
102 evdev_device_transform_y(struct evdev_device *device,
103                          double y,
104                          uint32_t height)
105 {
106         return (y - device->abs.min_y) * height /
107                 (device->abs.max_y - device->abs.min_y + 1);
108 }
109
110 static void
111 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
112 {
113         struct motion_params motion;
114         int32_t cx, cy;
115         double x, y;
116         int slot;
117         int seat_slot;
118         struct libinput_device *base = &device->base;
119         struct libinput_seat *seat = base->seat;
120
121         slot = device->mt.slot;
122
123         switch (device->pending_event) {
124         case EVDEV_NONE:
125                 return;
126         case EVDEV_RELATIVE_MOTION:
127                 motion.dx = device->rel.dx;
128                 motion.dy = device->rel.dy;
129                 device->rel.dx = 0;
130                 device->rel.dy = 0;
131
132                 /* Apply pointer acceleration. */
133                 filter_dispatch(device->pointer.filter, &motion, device, time);
134
135                 if (motion.dx == 0.0 && motion.dy == 0.0)
136                         break;
137
138                 pointer_notify_motion(base, time, motion.dx, motion.dy);
139                 break;
140         case EVDEV_ABSOLUTE_MT_DOWN:
141                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
142                         break;
143
144                 if (device->mt.slots[slot].seat_slot != -1) {
145                         log_bug_kernel("%s: Driver sent multiple touch down for the "
146                                        "same slot", device->devnode);
147                         break;
148                 }
149
150                 seat_slot = ffs(~seat->slot_map) - 1;
151                 device->mt.slots[slot].seat_slot = seat_slot;
152
153                 if (seat_slot == -1)
154                         break;
155
156                 seat->slot_map |= 1 << seat_slot;
157                 x = device->mt.slots[slot].x;
158                 y = device->mt.slots[slot].y;
159
160                 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
161                 break;
162         case EVDEV_ABSOLUTE_MT_MOTION:
163                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
164                         break;
165
166                 seat_slot = device->mt.slots[slot].seat_slot;
167                 x = device->mt.slots[slot].x;
168                 y = device->mt.slots[slot].y;
169
170                 if (seat_slot == -1)
171                         break;
172
173                 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
174                 break;
175         case EVDEV_ABSOLUTE_MT_UP:
176                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
177                         break;
178
179                 seat_slot = device->mt.slots[slot].seat_slot;
180                 device->mt.slots[slot].seat_slot = -1;
181
182                 if (seat_slot == -1)
183                         break;
184
185                 seat->slot_map &= ~(1 << seat_slot);
186
187                 touch_notify_touch_up(base, time, slot, seat_slot);
188                 break;
189         case EVDEV_ABSOLUTE_TOUCH_DOWN:
190                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
191                         break;
192
193                 if (device->abs.seat_slot != -1) {
194                         log_bug_kernel("%s: Driver sent multiple touch down for the "
195                                        "same slot", device->devnode);
196                         break;
197                 }
198
199                 seat_slot = ffs(~seat->slot_map) - 1;
200                 device->abs.seat_slot = seat_slot;
201
202                 if (seat_slot == -1)
203                         break;
204
205                 seat->slot_map |= 1 << seat_slot;
206
207                 transform_absolute(device, &cx, &cy);
208
209                 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
210                 break;
211         case EVDEV_ABSOLUTE_MOTION:
212                 transform_absolute(device, &cx, &cy);
213                 x = cx;
214                 y = cy;
215
216                 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
217                         seat_slot = device->abs.seat_slot;
218
219                         if (seat_slot == -1)
220                                 break;
221
222                         touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
223                 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
224                         pointer_notify_motion_absolute(base, time, x, y);
225                 }
226                 break;
227         case EVDEV_ABSOLUTE_TOUCH_UP:
228                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
229                         break;
230
231                 seat_slot = device->abs.seat_slot;
232                 device->abs.seat_slot = -1;
233
234                 if (seat_slot == -1)
235                         break;
236
237                 seat->slot_map &= ~(1 << seat_slot);
238
239                 touch_notify_touch_up(base, time, -1, seat_slot);
240                 break;
241         default:
242                 assert(0 && "Unknown pending event type");
243                 break;
244         }
245
246         device->pending_event = EVDEV_NONE;
247 }
248
249 static void
250 evdev_process_touch_button(struct evdev_device *device, int time, int value)
251 {
252         if (device->pending_event != EVDEV_NONE &&
253             device->pending_event != EVDEV_ABSOLUTE_MOTION)
254                 evdev_flush_pending_event(device, time);
255
256         device->pending_event = (value ?
257                                  EVDEV_ABSOLUTE_TOUCH_DOWN :
258                                  EVDEV_ABSOLUTE_TOUCH_UP);
259 }
260
261 static inline void
262 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
263 {
264         /* ignore kernel key repeat */
265         if (e->value == 2)
266                 return;
267
268         if (e->code > KEY_MAX)
269                 return;
270
271         if (e->code == BTN_TOUCH) {
272                 if (!device->is_mt)
273                         evdev_process_touch_button(device, time, e->value);
274                 return;
275         }
276
277         evdev_flush_pending_event(device, time);
278
279         switch (e->code) {
280         case BTN_LEFT:
281         case BTN_RIGHT:
282         case BTN_MIDDLE:
283         case BTN_SIDE:
284         case BTN_EXTRA:
285         case BTN_FORWARD:
286         case BTN_BACK:
287         case BTN_TASK:
288                 pointer_notify_button(
289                         &device->base,
290                         time,
291                         e->code,
292                         e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
293                                    LIBINPUT_BUTTON_STATE_RELEASED);
294                 break;
295
296         default:
297                 /* Only let KEY_* codes pass through. */
298                 if (!(e->code <= KEY_MICMUTE ||
299                       (e->code >= KEY_OK && e->code <= KEY_LIGHTS_TOGGLE)))
300                         break;
301
302                 keyboard_notify_key(
303                         &device->base,
304                         time,
305                         e->code,
306                         e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
307                                    LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
308                 break;
309         }
310 }
311
312 static void
313 evdev_process_touch(struct evdev_device *device,
314                     struct input_event *e,
315                     uint64_t time)
316 {
317         switch (e->code) {
318         case ABS_MT_SLOT:
319                 evdev_flush_pending_event(device, time);
320                 device->mt.slot = e->value;
321                 break;
322         case ABS_MT_TRACKING_ID:
323                 if (device->pending_event != EVDEV_NONE &&
324                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
325                         evdev_flush_pending_event(device, time);
326                 if (e->value >= 0)
327                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
328                 else
329                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
330                 break;
331         case ABS_MT_POSITION_X:
332                 device->mt.slots[device->mt.slot].x = e->value;
333                 if (device->pending_event == EVDEV_NONE)
334                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
335                 break;
336         case ABS_MT_POSITION_Y:
337                 device->mt.slots[device->mt.slot].y = e->value;
338                 if (device->pending_event == EVDEV_NONE)
339                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
340                 break;
341         }
342 }
343
344 static inline void
345 evdev_process_absolute_motion(struct evdev_device *device,
346                               struct input_event *e)
347 {
348         switch (e->code) {
349         case ABS_X:
350                 device->abs.x = e->value;
351                 if (device->pending_event == EVDEV_NONE)
352                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
353                 break;
354         case ABS_Y:
355                 device->abs.y = e->value;
356                 if (device->pending_event == EVDEV_NONE)
357                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
358                 break;
359         }
360 }
361
362 static inline void
363 evdev_process_relative(struct evdev_device *device,
364                        struct input_event *e, uint64_t time)
365 {
366         struct libinput_device *base = &device->base;
367
368         switch (e->code) {
369         case REL_X:
370                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
371                         evdev_flush_pending_event(device, time);
372                 device->rel.dx += e->value;
373                 device->pending_event = EVDEV_RELATIVE_MOTION;
374                 break;
375         case REL_Y:
376                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
377                         evdev_flush_pending_event(device, time);
378                 device->rel.dy += e->value;
379                 device->pending_event = EVDEV_RELATIVE_MOTION;
380                 break;
381         case REL_WHEEL:
382                 evdev_flush_pending_event(device, time);
383                 pointer_notify_axis(
384                         base,
385                         time,
386                         LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
387                         -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
388                 break;
389         case REL_HWHEEL:
390                 evdev_flush_pending_event(device, time);
391                 switch (e->value) {
392                 case -1:
393                         /* Scroll left */
394                 case 1:
395                         /* Scroll right */
396                         pointer_notify_axis(
397                                 base,
398                                 time,
399                                 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
400                                 e->value * DEFAULT_AXIS_STEP_DISTANCE);
401                         break;
402                 default:
403                         break;
404
405                 }
406         }
407 }
408
409 static inline void
410 evdev_process_absolute(struct evdev_device *device,
411                        struct input_event *e,
412                        uint64_t time)
413 {
414         if (device->is_mt) {
415                 evdev_process_touch(device, e, time);
416         } else {
417                 evdev_process_absolute_motion(device, e);
418         }
419 }
420
421 static inline int
422 evdev_need_touch_frame(struct evdev_device *device)
423 {
424         if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
425                 return 0;
426
427         switch (device->pending_event) {
428         case EVDEV_NONE:
429         case EVDEV_RELATIVE_MOTION:
430                 break;
431         case EVDEV_ABSOLUTE_MT_DOWN:
432         case EVDEV_ABSOLUTE_MT_MOTION:
433         case EVDEV_ABSOLUTE_MT_UP:
434         case EVDEV_ABSOLUTE_TOUCH_DOWN:
435         case EVDEV_ABSOLUTE_TOUCH_UP:
436         case EVDEV_ABSOLUTE_MOTION:
437                 return 1;
438         }
439
440         return 0;
441 }
442
443 static void
444 fallback_process(struct evdev_dispatch *dispatch,
445                  struct evdev_device *device,
446                  struct input_event *event,
447                  uint64_t time)
448 {
449         int need_frame = 0;
450
451         switch (event->type) {
452         case EV_REL:
453                 evdev_process_relative(device, event, time);
454                 break;
455         case EV_ABS:
456                 evdev_process_absolute(device, event, time);
457                 break;
458         case EV_KEY:
459                 evdev_process_key(device, event, time);
460                 break;
461         case EV_SYN:
462                 need_frame = evdev_need_touch_frame(device);
463                 evdev_flush_pending_event(device, time);
464                 if (need_frame)
465                         touch_notify_frame(&device->base, time);
466                 break;
467         }
468 }
469
470 static void
471 fallback_destroy(struct evdev_dispatch *dispatch)
472 {
473         free(dispatch);
474 }
475
476 struct evdev_dispatch_interface fallback_interface = {
477         fallback_process,
478         fallback_destroy
479 };
480
481 static struct evdev_dispatch *
482 fallback_dispatch_create(void)
483 {
484         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
485         if (dispatch == NULL)
486                 return NULL;
487
488         dispatch->interface = &fallback_interface;
489
490         return dispatch;
491 }
492
493 static inline void
494 evdev_process_event(struct evdev_device *device, struct input_event *e)
495 {
496         struct evdev_dispatch *dispatch = device->dispatch;
497         uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
498
499         dispatch->interface->process(dispatch, device, e, time);
500 }
501
502 static inline void
503 evdev_device_dispatch_one(struct evdev_device *device,
504                           struct input_event *ev)
505 {
506         if (!device->mtdev) {
507                 evdev_process_event(device, ev);
508         } else {
509                 mtdev_put_event(device->mtdev, ev);
510                 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
511                         while (!mtdev_empty(device->mtdev)) {
512                                 struct input_event e;
513                                 mtdev_get_event(device->mtdev, &e);
514                                 evdev_process_event(device, &e);
515                         }
516                 }
517         }
518 }
519
520 static int
521 evdev_sync_device(struct evdev_device *device)
522 {
523         struct input_event ev;
524         int rc;
525
526         do {
527                 rc = libevdev_next_event(device->evdev,
528                                          LIBEVDEV_READ_FLAG_SYNC, &ev);
529                 if (rc < 0)
530                         break;
531                 evdev_device_dispatch_one(device, &ev);
532         } while (rc == LIBEVDEV_READ_STATUS_SYNC);
533
534         return rc == -EAGAIN ? 0 : rc;
535 }
536
537 static void
538 evdev_device_dispatch(void *data)
539 {
540         struct evdev_device *device = data;
541         struct libinput *libinput = device->base.seat->libinput;
542         struct input_event ev;
543         int rc;
544
545         /* If the compositor is repainting, this function is called only once
546          * per frame and we have to process all the events available on the
547          * fd, otherwise there will be input lag. */
548         do {
549                 rc = libevdev_next_event(device->evdev,
550                                          LIBEVDEV_READ_FLAG_NORMAL, &ev);
551                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
552                         /* send one more sync event so we handle all
553                            currently pending events before we sync up
554                            to the current state */
555                         ev.code = SYN_REPORT;
556                         evdev_device_dispatch_one(device, &ev);
557
558                         rc = evdev_sync_device(device);
559                         if (rc == 0)
560                                 rc = LIBEVDEV_READ_STATUS_SUCCESS;
561                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
562                         evdev_device_dispatch_one(device, &ev);
563                 }
564         } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
565
566         if (rc != -EAGAIN && rc != -EINTR) {
567                 libinput_remove_source(libinput, device->source);
568                 device->source = NULL;
569         }
570 }
571
572 static int
573 configure_pointer_acceleration(struct evdev_device *device)
574 {
575         device->pointer.filter =
576                 create_pointer_accelator_filter(
577                         pointer_accel_profile_smooth_simple);
578         if (!device->pointer.filter)
579                 return -1;
580
581         return 0;
582 }
583
584 static int
585 evdev_configure_device(struct evdev_device *device)
586 {
587         struct libevdev *evdev = device->evdev;
588         const struct input_absinfo *absinfo;
589         int has_abs, has_rel, has_mt;
590         int has_button, has_keyboard, has_touch;
591         struct mt_slot *slots;
592         int num_slots;
593         int active_slot;
594         int slot;
595         unsigned int i;
596
597         has_rel = 0;
598         has_abs = 0;
599         has_mt = 0;
600         has_button = 0;
601         has_keyboard = 0;
602         has_touch = 0;
603
604         if (libevdev_has_event_type(evdev, EV_ABS)) {
605
606                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
607                         device->abs.min_x = absinfo->minimum;
608                         device->abs.max_x = absinfo->maximum;
609                         has_abs = 1;
610                 }
611                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
612                         device->abs.min_y = absinfo->minimum;
613                         device->abs.max_y = absinfo->maximum;
614                         has_abs = 1;
615                 }
616                 /* We only handle the slotted Protocol B in weston.
617                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
618                    require mtdev for conversion. */
619                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
620                     libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
621                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
622                         device->abs.min_x = absinfo->minimum;
623                         device->abs.max_x = absinfo->maximum;
624                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
625                         device->abs.min_y = absinfo->minimum;
626                         device->abs.max_y = absinfo->maximum;
627                         device->is_mt = 1;
628                         has_touch = 1;
629                         has_mt = 1;
630
631                         if (!libevdev_has_event_code(evdev,
632                                                      EV_ABS, ABS_MT_SLOT)) {
633                                 device->mtdev = mtdev_new_open(device->fd);
634                                 if (!device->mtdev)
635                                         return -1;
636
637                                 num_slots = device->mtdev->caps.slot.maximum;
638                                 if (device->mtdev->caps.slot.minimum < 0 ||
639                                     num_slots <= 0)
640                                         return -1;
641                                 active_slot = device->mtdev->caps.slot.value;
642                         } else {
643                                 num_slots = libevdev_get_num_slots(device->evdev);
644                                 active_slot = libevdev_get_current_slot(evdev);
645                         }
646
647                         slots = calloc(num_slots, sizeof(struct mt_slot));
648                         if (!slots)
649                                 return -1;
650
651                         for (slot = 0; slot < num_slots; ++slot) {
652                                 slots[slot].seat_slot = -1;
653                                 slots[slot].x = 0;
654                                 slots[slot].y = 0;
655                         }
656                         device->mt.slots = slots;
657                         device->mt.slots_len = num_slots;
658                         device->mt.slot = active_slot;
659                 }
660         }
661         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
662             libevdev_has_event_code(evdev, EV_REL, REL_Y))
663                 has_rel = 1;
664
665         if (libevdev_has_event_type(evdev, EV_KEY)) {
666                 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
667                     libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
668                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
669                     (has_abs || has_mt)) {
670                         device->dispatch = evdev_mt_touchpad_create(device);
671                         log_info("input device '%s', %s is a touchpad\n",
672                                  device->devname, device->devnode);
673                 }
674                 for (i = KEY_ESC; i < KEY_MAX; i++) {
675                         if (i >= BTN_MISC && i < KEY_OK)
676                                 continue;
677                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
678                                 has_keyboard = 1;
679                                 break;
680                         }
681                 }
682                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
683                         has_touch = 1;
684                 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
685                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
686                                 has_button = 1;
687                                 break;
688                         }
689                 }
690         }
691         if (libevdev_has_event_type(evdev, EV_LED))
692                 has_keyboard = 1;
693
694         if ((has_abs || has_rel) && has_button) {
695                 if (configure_pointer_acceleration(device) == -1)
696                         return -1;
697
698                 device->seat_caps |= EVDEV_DEVICE_POINTER;
699
700                 log_info("input device '%s', %s is a pointer caps =%s%s%s\n",
701                          device->devname, device->devnode,
702                          has_abs ? " absolute-motion" : "",
703                          has_rel ? " relative-motion": "",
704                          has_button ? " button" : "");
705         }
706         if (has_keyboard) {
707                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
708                 log_info("input device '%s', %s is a keyboard\n",
709                          device->devname, device->devnode);
710         }
711         if (has_touch && !has_button) {
712                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
713                 log_info("input device '%s', %s is a touch device\n",
714                          device->devname, device->devnode);
715         }
716
717         return 0;
718 }
719
720 struct evdev_device *
721 evdev_device_create(struct libinput_seat *seat,
722                     const char *devnode,
723                     const char *sysname)
724 {
725         struct libinput *libinput = seat->libinput;
726         struct evdev_device *device;
727         int rc;
728         int fd;
729         int unhandled_device = 0;
730
731         /* Use non-blocking mode so that we can loop on read on
732          * evdev_device_data() until all events on the fd are
733          * read.  mtdev_get() also expects this. */
734         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
735         if (fd < 0) {
736                 log_info("opening input device '%s' failed (%s).\n",
737                          devnode, strerror(-fd));
738                 return NULL;
739         }
740
741         device = zalloc(sizeof *device);
742         if (device == NULL)
743                 return NULL;
744
745         libinput_device_init(&device->base, seat);
746
747         rc = libevdev_new_from_fd(fd, &device->evdev);
748         if (rc != 0)
749                 return NULL;
750
751         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
752
753         device->seat_caps = 0;
754         device->is_mt = 0;
755         device->mtdev = NULL;
756         device->devnode = strdup(devnode);
757         device->sysname = strdup(sysname);
758         device->rel.dx = 0;
759         device->rel.dy = 0;
760         device->abs.seat_slot = -1;
761         device->dispatch = NULL;
762         device->fd = fd;
763         device->pending_event = EVDEV_NONE;
764         device->devname = libevdev_get_name(device->evdev);
765
766         libinput_seat_ref(seat);
767
768         if (evdev_configure_device(device) == -1)
769                 goto err;
770
771         if (device->seat_caps == 0) {
772                 unhandled_device = 1;
773                 goto err;
774         }
775
776         /* If the dispatch was not set up use the fallback. */
777         if (device->dispatch == NULL)
778                 device->dispatch = fallback_dispatch_create();
779         if (device->dispatch == NULL)
780                 goto err;
781
782         device->source =
783                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
784         if (!device->source)
785                 goto err;
786
787         list_insert(seat->devices_list.prev, &device->base.link);
788         notify_added_device(&device->base);
789
790         return device;
791
792 err:
793         if (fd >= 0)
794                 close_restricted(libinput, fd);
795         evdev_device_destroy(device);
796
797         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
798 }
799
800 int
801 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
802 {
803         int len;
804
805         memset(keys, 0, size);
806         len = ioctl(device->fd, EVIOCGKEY(size), keys);
807
808         return (len == -1) ? -errno : len;
809 }
810
811 const char *
812 evdev_device_get_output(struct evdev_device *device)
813 {
814         return device->output_name;
815 }
816
817 const char *
818 evdev_device_get_sysname(struct evdev_device *device)
819 {
820         return device->sysname;
821 }
822
823 void
824 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
825 {
826         device->abs.apply_calibration = 1;
827         memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
828 }
829
830 int
831 evdev_device_has_capability(struct evdev_device *device,
832                             enum libinput_device_capability capability)
833 {
834         switch (capability) {
835         case LIBINPUT_DEVICE_CAP_POINTER:
836                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
837         case LIBINPUT_DEVICE_CAP_KEYBOARD:
838                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
839         case LIBINPUT_DEVICE_CAP_TOUCH:
840                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
841         default:
842                 return 0;
843         }
844 }
845
846 void
847 evdev_device_remove(struct evdev_device *device)
848 {
849         if (device->source)
850                 libinput_remove_source(device->base.seat->libinput,
851                                        device->source);
852
853         if (device->mtdev)
854                 mtdev_close_delete(device->mtdev);
855         close_restricted(device->base.seat->libinput, device->fd);
856         device->fd = -1;
857         list_remove(&device->base.link);
858
859         notify_removed_device(&device->base);
860         libinput_device_unref(&device->base);
861 }
862
863 void
864 evdev_device_destroy(struct evdev_device *device)
865 {
866         struct evdev_dispatch *dispatch;
867
868         dispatch = device->dispatch;
869         if (dispatch)
870                 dispatch->interface->destroy(dispatch);
871
872         motion_filter_destroy(device->pointer.filter);
873         libinput_seat_unref(device->base.seat);
874         libevdev_free(device->evdev);
875         free(device->mt.slots);
876         free(device->devnode);
877         free(device->sysname);
878         free(device);
879 }