1 /*
2 * Copyright © 2013 Red Hat, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23 #include "config.h"
24 #include <errno.h>
25 #include <limits.h>
26 #include <poll.h>
27 #include <stdarg.h>
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include "libevdev-int.h"
35 #include "libevdev-util.h"
36 #include "libevdev.h"
37
38 #include "event-names.h"
39
40 #define MAXEVENTS 64
41
42 enum event_filter_status {
43 EVENT_FILTER_NONE, /**< Event untouched by filters */
44 EVENT_FILTER_MODIFIED, /**< Event was modified */
45 EVENT_FILTER_DISCARD, /**< Discard current event */
46 };
47
48 /* Keeps a record of touches during SYN_DROPPED */
49 enum touch_state {
50 TOUCH_OFF,
51 TOUCH_STARTED, /* Started during SYN_DROPPED */
52 TOUCH_STOPPED, /* Stopped during SYN_DROPPED */
53 TOUCH_ONGOING, /* Existed before, still have same tracking ID */
54 TOUCH_CHANGED, /* Existed before but have new tracking ID now, so
55 stopped + started in that slot */
56 };
57
58 struct slot_change_state {
59 enum touch_state state;
60 unsigned long axes[NLONGS(ABS_CNT)]; /* bitmask for updated axes */
61 };
62
63 static int sync_mt_state(struct libevdev *dev,
64 struct slot_change_state *changes_out);
65 static int
66 update_key_state(struct libevdev *dev, const struct input_event *e);
67
68 static inline int*
slot_value(const struct libevdev * dev,int slot,int axis)69 slot_value(const struct libevdev *dev, int slot, int axis)
70 {
71 if (unlikely(slot > dev->num_slots)) {
72 log_bug(dev, "Slot %d exceeds number of slots (%d)\n", slot, dev->num_slots);
73 slot = 0;
74 }
75 if (unlikely(axis < ABS_MT_MIN || axis > ABS_MT_MAX)) {
76 log_bug(dev, "MT axis %d is outside the valid range [%d,%d]\n",
77 axis, ABS_MT_MIN, ABS_MT_MAX);
78 axis = ABS_MT_MIN;
79 }
80 return &dev->mt_slot_vals[slot * ABS_MT_CNT + axis - ABS_MT_MIN];
81 }
82
83 static int
init_event_queue(struct libevdev * dev)84 init_event_queue(struct libevdev *dev)
85 {
86 const int MIN_QUEUE_SIZE = 256;
87 int nevents = 1; /* terminating SYN_REPORT */
88 int nslots;
89 unsigned int type, code;
90
91 /* count the number of axes, keys, etc. to get a better idea at how
92 many events per EV_SYN we could possibly get. That's the max we
93 may get during SYN_DROPPED too. Use double that, just so we have
94 room for events while syncing a device.
95 */
96 for (type = EV_KEY; type < EV_MAX; type++) {
97 int max = libevdev_event_type_get_max(type);
98 for (code = 0; max > 0 && code < (unsigned int) max; code++) {
99 if (libevdev_has_event_code(dev, type, code))
100 nevents++;
101 }
102 }
103
104 nslots = libevdev_get_num_slots(dev);
105 if (nslots > 1) {
106 int num_mt_axes = 0;
107
108 for (code = ABS_MT_SLOT; code <= ABS_MAX; code++) {
109 if (libevdev_has_event_code(dev, EV_ABS, code))
110 num_mt_axes++;
111 }
112
113 /* We already counted the first slot in the initial count */
114 nevents += num_mt_axes * (nslots - 1);
115 }
116
117 return queue_alloc(dev, max(MIN_QUEUE_SIZE, nevents * 2));
118 }
119
120 static void
libevdev_dflt_log_func(enum libevdev_log_priority priority,void * data,const char * file,int line,const char * func,const char * format,va_list args)121 libevdev_dflt_log_func(enum libevdev_log_priority priority,
122 void *data,
123 const char *file, int line, const char *func,
124 const char *format, va_list args)
125 {
126 const char *prefix;
127 switch(priority) {
128 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
129 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
130 case LIBEVDEV_LOG_DEBUG:
131 prefix = "libevdev debug";
132 break;
133 default:
134 prefix = "libevdev INVALID LOG PRIORITY";
135 break;
136 }
137 /* default logging format:
138 libevev error in libevdev_some_func: blah blah
139 libevev info in libevdev_some_func: blah blah
140 libevev debug in file.c:123:libevdev_some_func: blah blah
141 */
142
143 fprintf(stderr, "%s in ", prefix);
144 if (priority == LIBEVDEV_LOG_DEBUG)
145 fprintf(stderr, "%s:%d:", file, line);
146 fprintf(stderr, "%s: ", func);
147 vfprintf(stderr, format, args);
148 }
149
150 static void
fix_invalid_absinfo(const struct libevdev * dev,int axis,struct input_absinfo * abs_info)151 fix_invalid_absinfo(const struct libevdev *dev,
152 int axis,
153 struct input_absinfo* abs_info)
154 {
155 /*
156 * The reported absinfo for ABS_MT_TRACKING_ID is sometimes
157 * uninitialized for certain mtk-soc, due to init code mangling
158 * in the vendor kernel.
159 */
160 if (axis == ABS_MT_TRACKING_ID &&
161 abs_info->maximum == abs_info->minimum) {
162 abs_info->minimum = -1;
163 abs_info->maximum = 0xFFFF;
164 log_bug(dev,
165 "Device \"%s\" has invalid ABS_MT_TRACKING_ID range",
166 dev->name);
167 }
168 }
169
170 /*
171 * Global logging settings.
172 */
173 static struct logdata log_data = {
174 .priority = LIBEVDEV_LOG_INFO,
175 .global_handler = libevdev_dflt_log_func,
176 .userdata = NULL,
177 };
178
179 void
_libevdev_log_msg(const struct libevdev * dev,enum libevdev_log_priority priority,const char * file,int line,const char * func,const char * format,...)180 _libevdev_log_msg(const struct libevdev *dev,
181 enum libevdev_log_priority priority,
182 const char *file, int line, const char *func,
183 const char *format, ...)
184 {
185 va_list args;
186
187 if (dev && dev->log.device_handler) {
188 /**
189 * if both global handler and device handler are set
190 * we've set up the handlers wrong. And that means we'll
191 * likely get the printf args wrong and cause all sorts of
192 * mayhem. Seppuku is called for.
193 */
194 if (unlikely(dev->log.global_handler))
195 abort();
196
197 if (priority > dev->log.priority)
198 return;
199 } else if (!log_data.global_handler || priority > log_data.priority) {
200 return;
201 } else if (unlikely(log_data.device_handler)) {
202 abort(); /* Seppuku, see above */
203 }
204
205 va_start(args, format);
206 if (dev && dev->log.device_handler)
207 dev->log.device_handler(dev, priority, dev->log.userdata, file, line, func, format, args);
208 else
209 log_data.global_handler(priority, log_data.userdata, file, line, func, format, args);
210 va_end(args);
211 }
212
213 static void
libevdev_reset(struct libevdev * dev)214 libevdev_reset(struct libevdev *dev)
215 {
216 enum libevdev_log_priority pri = dev->log.priority;
217 libevdev_device_log_func_t handler = dev->log.device_handler;
218
219 free(dev->name);
220 free(dev->phys);
221 free(dev->uniq);
222 free(dev->mt_slot_vals);
223 memset(dev, 0, sizeof(*dev));
224 dev->fd = -1;
225 dev->initialized = false;
226 dev->num_slots = -1;
227 dev->current_slot = -1;
228 dev->grabbed = LIBEVDEV_UNGRAB;
229 dev->sync_state = SYNC_NONE;
230 dev->log.priority = pri;
231 dev->log.device_handler = handler;
232 libevdev_enable_event_type(dev, EV_SYN);
233 }
234
235 LIBEVDEV_EXPORT struct libevdev*
libevdev_new(void)236 libevdev_new(void)
237 {
238 struct libevdev *dev;
239
240 dev = calloc(1, sizeof(*dev));
241 if (!dev)
242 return NULL;
243
244 libevdev_reset(dev);
245
246 return dev;
247 }
248
249 LIBEVDEV_EXPORT int
libevdev_new_from_fd(int fd,struct libevdev ** dev)250 libevdev_new_from_fd(int fd, struct libevdev **dev)
251 {
252 struct libevdev *d;
253 int rc;
254
255 d = libevdev_new();
256 if (!d)
257 return -ENOMEM;
258
259 rc = libevdev_set_fd(d, fd);
260 if (rc < 0)
261 libevdev_free(d);
262 else
263 *dev = d;
264 return rc;
265 }
266
267 LIBEVDEV_EXPORT void
libevdev_free(struct libevdev * dev)268 libevdev_free(struct libevdev *dev)
269 {
270 if (!dev)
271 return;
272
273 queue_free(dev);
274 libevdev_reset(dev);
275 free(dev);
276 }
277
278 LIBEVDEV_EXPORT void
libevdev_set_log_function(libevdev_log_func_t logfunc,void * data)279 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
280 {
281 log_data.global_handler = logfunc;
282 log_data.userdata = data;
283 }
284
285 LIBEVDEV_EXPORT void
libevdev_set_log_priority(enum libevdev_log_priority priority)286 libevdev_set_log_priority(enum libevdev_log_priority priority)
287 {
288 if (priority > LIBEVDEV_LOG_DEBUG)
289 priority = LIBEVDEV_LOG_DEBUG;
290 log_data.priority = priority;
291 }
292
293 LIBEVDEV_EXPORT enum libevdev_log_priority
libevdev_get_log_priority(void)294 libevdev_get_log_priority(void)
295 {
296 return log_data.priority;
297 }
298
299 LIBEVDEV_EXPORT void
libevdev_set_device_log_function(struct libevdev * dev,libevdev_device_log_func_t logfunc,enum libevdev_log_priority priority,void * data)300 libevdev_set_device_log_function(struct libevdev *dev,
301 libevdev_device_log_func_t logfunc,
302 enum libevdev_log_priority priority,
303 void *data)
304 {
305 if (!dev) {
306 log_bug(NULL, "device must not be NULL\n");
307 return;
308 }
309
310 dev->log.priority = priority;
311 dev->log.device_handler = logfunc;
312 dev->log.userdata = data;
313 }
314
315 enum libevdev_log_priority
_libevdev_log_priority(const struct libevdev * dev)316 _libevdev_log_priority(const struct libevdev *dev)
317 {
318 if (dev && dev->log.device_handler)
319 return dev->log.priority;
320 return libevdev_get_log_priority();
321 }
322
323 LIBEVDEV_EXPORT int
libevdev_change_fd(struct libevdev * dev,int fd)324 libevdev_change_fd(struct libevdev *dev, int fd)
325 {
326 if (!dev->initialized) {
327 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
328 return -1;
329 }
330 dev->fd = fd;
331 dev->grabbed = LIBEVDEV_UNGRAB;
332 return 0;
333 }
334
335 static void
reset_tracking_ids(struct libevdev * dev)336 reset_tracking_ids(struct libevdev *dev)
337 {
338 if (dev->num_slots == -1 ||
339 !libevdev_has_event_code(dev, EV_ABS, ABS_MT_TRACKING_ID))
340 return;
341
342 for (int slot = 0; slot < dev->num_slots; slot++)
343 libevdev_set_slot_value(dev, slot, ABS_MT_TRACKING_ID, -1);
344 }
345
346 static inline void
free_slots(struct libevdev * dev)347 free_slots(struct libevdev *dev)
348 {
349 dev->num_slots = -1;
350 free(dev->mt_slot_vals);
351 dev->mt_slot_vals = NULL;
352 }
353
354 static int
init_slots(struct libevdev * dev)355 init_slots(struct libevdev *dev)
356 {
357 const struct input_absinfo *abs_info;
358 int rc = 0;
359
360 free(dev->mt_slot_vals);
361 dev->mt_slot_vals = NULL;
362
363 /* devices with ABS_RESERVED aren't MT devices,
364 see the documentation for multitouch-related
365 functions for more details */
366 if (libevdev_has_event_code(dev, EV_ABS, ABS_RESERVED) ||
367 !libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT)) {
368 if (dev->num_slots != -1) {
369 free_slots(dev);
370 }
371 return rc;
372 }
373
374 abs_info = libevdev_get_abs_info(dev, ABS_MT_SLOT);
375
376 free_slots(dev);
377 dev->num_slots = abs_info->maximum + 1;
378 dev->mt_slot_vals = calloc(dev->num_slots * ABS_MT_CNT, sizeof(int));
379 if (!dev->mt_slot_vals) {
380 rc = -ENOMEM;
381 goto out;
382 }
383 dev->current_slot = abs_info->value;
384
385 reset_tracking_ids(dev);
386 out:
387 return rc;
388 }
389
390 LIBEVDEV_EXPORT int
libevdev_set_fd(struct libevdev * dev,int fd)391 libevdev_set_fd(struct libevdev* dev, int fd)
392 {
393 int rc;
394 int i;
395 char buf[256];
396
397 if (dev->initialized) {
398 log_bug(dev, "device already initialized.\n");
399 return -EBADF;
400 }
401
402 if (fd < 0) {
403 return -EBADF;
404 }
405
406 libevdev_reset(dev);
407
408 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
409 if (rc < 0)
410 goto out;
411
412 memset(buf, 0, sizeof(buf));
413 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
414 if (rc < 0)
415 goto out;
416
417 free(dev->name);
418 dev->name = strdup(buf);
419 if (!dev->name) {
420 errno = ENOMEM;
421 goto out;
422 }
423
424 free(dev->phys);
425 dev->phys = NULL;
426 memset(buf, 0, sizeof(buf));
427 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
428 if (rc < 0) {
429 /* uinput has no phys */
430 if (errno != ENOENT)
431 goto out;
432 } else {
433 dev->phys = strdup(buf);
434 if (!dev->phys) {
435 errno = ENOMEM;
436 goto out;
437 }
438 }
439
440 free(dev->uniq);
441 dev->uniq = NULL;
442 memset(buf, 0, sizeof(buf));
443 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
444 if (rc < 0) {
445 if (errno != ENOENT)
446 goto out;
447 } else {
448 dev->uniq = strdup(buf);
449 if (!dev->uniq) {
450 errno = ENOMEM;
451 goto out;
452 }
453 }
454
455 rc = ioctl(fd, EVIOCGID, &dev->ids);
456 if (rc < 0)
457 goto out;
458
459 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
460 if (rc < 0)
461 goto out;
462
463 /* Built on a kernel with props, running against a kernel without property
464 support. This should not be a fatal case, we'll be missing properties but other
465 than that everything is as expected.
466 */
467 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
468 if (rc < 0 && errno != EINVAL)
469 goto out;
470
471 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
472 if (rc < 0)
473 goto out;
474
475 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
476 if (rc < 0)
477 goto out;
478
479 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
480 if (rc < 0)
481 goto out;
482
483 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
484 if (rc < 0)
485 goto out;
486
487 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
488 if (rc < 0)
489 goto out;
490
491 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
492 if (rc < 0)
493 goto out;
494
495 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
496 if (rc < 0)
497 goto out;
498
499 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
500 if (rc < 0)
501 goto out;
502
503 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
504 if (rc < 0)
505 goto out;
506
507 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
508 if (rc < 0)
509 goto out;
510
511 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
512 if (rc < 0)
513 goto out;
514
515 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
516 if (bit_is_set(dev->bits, EV_REP)) {
517 for (i = 0; i < REP_CNT; i++)
518 set_bit(dev->rep_bits, i);
519 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
520 if (rc < 0)
521 goto out;
522 }
523
524 for (i = ABS_X; i <= ABS_MAX; i++) {
525 if (bit_is_set(dev->abs_bits, i)) {
526 struct input_absinfo abs_info;
527 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
528 if (rc < 0)
529 goto out;
530
531 fix_invalid_absinfo(dev, i, &abs_info);
532
533 dev->abs_info[i] = abs_info;
534 }
535 }
536
537 dev->fd = fd;
538
539 rc = init_slots(dev);
540 if (rc != 0)
541 goto out;
542
543 if (dev->num_slots != -1) {
544 struct slot_change_state unused[dev->num_slots];
545 sync_mt_state(dev, unused);
546 }
547
548 rc = init_event_queue(dev);
549 if (rc < 0) {
550 dev->fd = -1;
551 return -rc;
552 }
553
554 /* not copying key state because we won't know when we'll start to
555 * use this fd and key's are likely to change state by then.
556 * Same with the valuators, really, but they may not change.
557 */
558
559 dev->initialized = true;
560 out:
561 if (rc)
562 libevdev_reset(dev);
563 return rc ? -errno : 0;
564 }
565
566 LIBEVDEV_EXPORT int
libevdev_get_fd(const struct libevdev * dev)567 libevdev_get_fd(const struct libevdev* dev)
568 {
569 return dev->fd;
570 }
571
572 static int
sync_key_state(struct libevdev * dev)573 sync_key_state(struct libevdev *dev)
574 {
575 int rc;
576 int i;
577 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
578
579 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
580 if (rc < 0)
581 goto out;
582
583 for (i = 0; i < KEY_CNT; i++) {
584 int old, new;
585 old = bit_is_set(dev->key_values, i);
586 new = bit_is_set(keystate, i);
587 if (old ^ new)
588 queue_push_event(dev, EV_KEY, i, new ? 1 : 0);
589 }
590
591 memcpy(dev->key_values, keystate, rc);
592
593 rc = 0;
594 out:
595 return rc ? -errno : 0;
596 }
597
598 static int
sync_sw_state(struct libevdev * dev)599 sync_sw_state(struct libevdev *dev)
600 {
601 int rc;
602 int i;
603 unsigned long swstate[NLONGS(SW_CNT)] = {0};
604
605 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
606 if (rc < 0)
607 goto out;
608
609 for (i = 0; i < SW_CNT; i++) {
610 int old, new;
611 old = bit_is_set(dev->sw_values, i);
612 new = bit_is_set(swstate, i);
613 if (old ^ new)
614 queue_push_event(dev, EV_SW, i, new ? 1 : 0);
615 }
616
617 memcpy(dev->sw_values, swstate, rc);
618
619 rc = 0;
620 out:
621 return rc ? -errno : 0;
622 }
623
624 static int
sync_led_state(struct libevdev * dev)625 sync_led_state(struct libevdev *dev)
626 {
627 int rc;
628 int i;
629 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
630
631 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
632 if (rc < 0)
633 goto out;
634
635 for (i = 0; i < LED_CNT; i++) {
636 int old, new;
637 old = bit_is_set(dev->led_values, i);
638 new = bit_is_set(ledstate, i);
639 if (old ^ new) {
640 queue_push_event(dev, EV_LED, i, new ? 1 : 0);
641 }
642 }
643
644 memcpy(dev->led_values, ledstate, rc);
645
646 rc = 0;
647 out:
648 return rc ? -errno : 0;
649 }
650 static int
sync_abs_state(struct libevdev * dev)651 sync_abs_state(struct libevdev *dev)
652 {
653 int rc;
654 int i;
655
656 for (i = ABS_X; i < ABS_CNT; i++) {
657 struct input_absinfo abs_info;
658
659 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
660 continue;
661
662 if (!bit_is_set(dev->abs_bits, i))
663 continue;
664
665 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
666 if (rc < 0)
667 goto out;
668
669 if (dev->abs_info[i].value != abs_info.value) {
670 queue_push_event(dev, EV_ABS, i, abs_info.value);
671 dev->abs_info[i].value = abs_info.value;
672 }
673 }
674
675 rc = 0;
676 out:
677 return rc ? -errno : 0;
678 }
679
680 static int
sync_mt_state(struct libevdev * dev,struct slot_change_state changes_out[dev->num_slots])681 sync_mt_state(struct libevdev *dev,
682 struct slot_change_state changes_out[dev->num_slots])
683 {
684 #define MAX_SLOTS 256
685 int rc = 0;
686 struct slot_change_state changes[MAX_SLOTS] = {0};
687 unsigned int nslots = min(MAX_SLOTS, dev->num_slots);
688
689 for (int axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
690 /* EVIOCGMTSLOTS required format */
691 struct mt_sync_state {
692 uint32_t code;
693 int32_t val[MAX_SLOTS];
694 } mt_state;
695
696 if (axis == ABS_MT_SLOT ||
697 !libevdev_has_event_code(dev, EV_ABS, axis))
698 continue;
699
700 mt_state.code = axis;
701 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(mt_state)), &mt_state);
702 if (rc < 0)
703 goto out;
704
705 for (unsigned int slot = 0; slot < nslots; slot++) {
706 int val_before = *slot_value(dev, slot, axis),
707 val_after = mt_state.val[slot];
708
709 if (axis == ABS_MT_TRACKING_ID) {
710 if (val_before == -1 && val_after != -1) {
711 changes[slot].state = TOUCH_STARTED;
712 } else if (val_before != -1 && val_after == -1) {
713 changes[slot].state = TOUCH_STOPPED;
714 } else if (val_before != -1 && val_after != -1 &&
715 val_before == val_after) {
716 changes[slot].state = TOUCH_ONGOING;
717 } else if (val_before != -1 && val_after != -1 &&
718 val_before != val_after) {
719 changes[slot].state = TOUCH_CHANGED;
720 } else {
721 changes[slot].state = TOUCH_OFF;
722 }
723 }
724
725 if (val_before == val_after)
726 continue;
727
728 *slot_value(dev, slot, axis) = val_after;
729
730 set_bit(changes[slot].axes, axis);
731 /* note that this slot has updates */
732 set_bit(changes[slot].axes, ABS_MT_SLOT);
733 }
734 }
735
736 if (dev->num_slots > MAX_SLOTS)
737 memset(changes_out, 0, sizeof(*changes) * dev->num_slots);
738
739 memcpy(changes_out, changes, sizeof(*changes) * nslots);
740 out:
741 return rc;
742 }
743
744 static void
terminate_slots(struct libevdev * dev,const struct slot_change_state changes[dev->num_slots],int * last_reported_slot)745 terminate_slots(struct libevdev *dev,
746 const struct slot_change_state changes[dev->num_slots],
747 int *last_reported_slot)
748 {
749 const unsigned int map[] = {BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP,
750 BTN_TOOL_TRIPLETAP, BTN_TOOL_QUADTAP,
751 BTN_TOOL_QUINTTAP};
752 bool touches_stopped = false;
753 int ntouches_before = 0, ntouches_after = 0;
754
755 /* For BTN_TOOL_* emulation, we need to know how many touches we had
756 * before and how many we have left once we terminate all the ones
757 * that changed and all the ones that stopped.
758 */
759 for (int slot = 0; slot < dev->num_slots; slot++) {
760 switch(changes[slot].state) {
761 case TOUCH_OFF:
762 break;
763 case TOUCH_CHANGED:
764 case TOUCH_STOPPED:
765 queue_push_event(dev, EV_ABS, ABS_MT_SLOT, slot);
766 queue_push_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
767
768 *last_reported_slot = slot;
769 touches_stopped = true;
770 ntouches_before++;
771 break;
772 case TOUCH_ONGOING:
773 ntouches_before++;
774 ntouches_after++;
775 break;
776 case TOUCH_STARTED:
777 break;
778 }
779 }
780
781 /* If any of the touches stopped, we need to split the sync state
782 into two frames - one with all the stopped touches, one with the
783 new touches starting (if any) */
784 if (touches_stopped) {
785 /* Send through the required BTN_TOOL_ 0 and 1 events for
786 * the previous and current number of fingers. And update
787 * our own key state accordingly, so that during the second
788 * sync event frame sync_key_state() sets everything correctly
789 * for the *real* number of touches.
790 */
791 if (ntouches_before > 0 && ntouches_before <= 5) {
792 struct input_event ev = {
793 .type = EV_KEY,
794 .code = map[ntouches_before - 1],
795 .value = 0,
796 };
797 queue_push_event(dev, ev.type, ev.code, ev.value);
798 update_key_state(dev, &ev);
799 }
800
801 if (ntouches_after > 0 && ntouches_after <= 5) {
802 struct input_event ev = {
803 .type = EV_KEY,
804 .code = map[ntouches_after - 1],
805 .value = 1,
806 };
807 queue_push_event(dev, ev.type, ev.code, ev.value);
808 update_key_state(dev, &ev);
809 }
810
811 queue_push_event(dev, EV_SYN, SYN_REPORT, 0);
812 }
813 }
814
815 static int
push_mt_sync_events(struct libevdev * dev,const struct slot_change_state changes[dev->num_slots],int last_reported_slot)816 push_mt_sync_events(struct libevdev *dev,
817 const struct slot_change_state changes[dev->num_slots],
818 int last_reported_slot)
819 {
820 struct input_absinfo abs_info;
821 int rc;
822
823 for (int slot = 0; slot < dev->num_slots; slot++) {
824 /* stopped touches were already terminated in
825 * terminate_slots */
826 if (changes[slot].state == TOUCH_STOPPED ||
827 !bit_is_set(changes[slot].axes, ABS_MT_SLOT))
828 continue;
829
830 queue_push_event(dev, EV_ABS, ABS_MT_SLOT, slot);
831 last_reported_slot = slot;
832
833 for (int axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
834 if (axis == ABS_MT_SLOT ||
835 !libevdev_has_event_code(dev, EV_ABS, axis))
836 continue;
837
838 if (bit_is_set(changes[slot].axes, axis))
839 queue_push_event(dev, EV_ABS, axis,
840 *slot_value(dev, slot, axis));
841 }
842 }
843
844 /* add one last slot event to make sure the client is on the same
845 slot as the kernel */
846
847 rc = ioctl(dev->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
848 if (rc < 0)
849 goto out;
850
851 dev->current_slot = abs_info.value;
852
853 if (dev->current_slot != last_reported_slot)
854 queue_push_event(dev, EV_ABS, ABS_MT_SLOT, dev->current_slot);
855
856 rc = 0;
857 out:
858 return rc ? -errno : 0;
859 }
860
861 static int
read_more_events(struct libevdev * dev)862 read_more_events(struct libevdev *dev)
863 {
864 int free_elem;
865 int len;
866 struct input_event *next;
867
868 free_elem = queue_num_free_elements(dev);
869 if (free_elem <= 0)
870 return 0;
871
872 next = queue_next_element(dev);
873 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
874 if (len < 0)
875 return -errno;
876
877 if (len > 0 && len % sizeof(struct input_event) != 0)
878 return -EINVAL;
879
880 if (len > 0) {
881 int nev = len/sizeof(struct input_event);
882 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
883 }
884
885 return 0;
886 }
887
888 static inline void
drain_events(struct libevdev * dev)889 drain_events(struct libevdev *dev)
890 {
891 int rc;
892 size_t nelem;
893 int iterations = 0;
894 const int max_iterations = 8; /* EVDEV_BUF_PACKETS in
895 kernel/drivers/input/evedev.c */
896
897 queue_shift_multiple(dev, queue_num_elements(dev), NULL);
898
899 do {
900 rc = read_more_events(dev);
901 if (rc == -EAGAIN)
902 return;
903
904 if (rc < 0) {
905 log_error(dev, "Failed to drain events before sync.\n");
906 return;
907 }
908
909 nelem = queue_num_elements(dev);
910 queue_shift_multiple(dev, nelem, NULL);
911 } while (iterations++ < max_iterations && nelem >= queue_size(dev));
912
913 /* Our buffer should be roughly the same or bigger than the kernel
914 buffer in most cases, so we usually don't expect to recurse. If
915 we do, make sure we stop after max_iterations and proceed with
916 what we have. This could happen if events queue up faster than
917 we can drain them.
918 */
919 if (iterations >= max_iterations)
920 log_info(dev, "Unable to drain events, buffer size mismatch.\n");
921 }
922
923 static int
sync_state(struct libevdev * dev)924 sync_state(struct libevdev *dev)
925 {
926 int rc = 0;
927 bool want_mt_sync = false;
928 int last_reported_slot = 0;
929 struct slot_change_state changes[dev->num_slots > 0 ? dev->num_slots : 1];
930 memset(changes, 0, sizeof(changes));
931
932 /* see section "Discarding events before synchronizing" in
933 * libevdev/libevdev.h */
934 drain_events(dev);
935
936 /* We generate one or two event frames during sync.
937 * The first one (if it exists) terminates all slots that have
938 * either terminated during SYN_DROPPED or changed their tracking
939 * ID.
940 *
941 * The second frame syncs everything up to the current state of the
942 * device - including re-starting those slots that have a changed
943 * tracking id.
944 */
945 if (dev->num_slots > -1 &&
946 libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT)) {
947 want_mt_sync = true;
948 rc = sync_mt_state(dev, changes);
949 if (rc == 0)
950 terminate_slots(dev, changes, &last_reported_slot);
951 else
952 want_mt_sync = false;
953 }
954
955 if (libevdev_has_event_type(dev, EV_KEY))
956 rc = sync_key_state(dev);
957 if (libevdev_has_event_type(dev, EV_LED))
958 rc = sync_led_state(dev);
959 if (libevdev_has_event_type(dev, EV_SW))
960 rc = sync_sw_state(dev);
961 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
962 rc = sync_abs_state(dev);
963 if (rc == 0 && want_mt_sync)
964 push_mt_sync_events(dev, changes, last_reported_slot);
965
966 dev->queue_nsync = queue_num_elements(dev);
967
968 if (dev->queue_nsync > 0) {
969 queue_push_event(dev, EV_SYN, SYN_REPORT, 0);
970 dev->queue_nsync++;
971 }
972
973 return rc;
974 }
975
976 static int
update_key_state(struct libevdev * dev,const struct input_event * e)977 update_key_state(struct libevdev *dev, const struct input_event *e)
978 {
979 if (!libevdev_has_event_type(dev, EV_KEY))
980 return 1;
981
982 if (e->code > KEY_MAX)
983 return 1;
984
985 set_bit_state(dev->key_values, e->code, e->value != 0);
986
987 return 0;
988 }
989
990 static int
update_mt_state(struct libevdev * dev,const struct input_event * e)991 update_mt_state(struct libevdev *dev, const struct input_event *e)
992 {
993 if (e->code == ABS_MT_SLOT && dev->num_slots > -1) {
994 int i;
995 dev->current_slot = e->value;
996 /* sync abs_info with the current slot values */
997 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
998 if (libevdev_has_event_code(dev, EV_ABS, i))
999 dev->abs_info[i].value = *slot_value(dev, dev->current_slot, i);
1000 }
1001
1002 return 0;
1003 }
1004
1005 if (dev->current_slot == -1)
1006 return 1;
1007
1008 *slot_value(dev, dev->current_slot, e->code) = e->value;
1009
1010 return 0;
1011 }
1012
1013 static int
update_abs_state(struct libevdev * dev,const struct input_event * e)1014 update_abs_state(struct libevdev *dev, const struct input_event *e)
1015 {
1016 if (!libevdev_has_event_type(dev, EV_ABS))
1017 return 1;
1018
1019 if (e->code > ABS_MAX)
1020 return 1;
1021
1022 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
1023 update_mt_state(dev, e);
1024
1025 dev->abs_info[e->code].value = e->value;
1026
1027 return 0;
1028 }
1029
1030 static int
update_led_state(struct libevdev * dev,const struct input_event * e)1031 update_led_state(struct libevdev *dev, const struct input_event *e)
1032 {
1033 if (!libevdev_has_event_type(dev, EV_LED))
1034 return 1;
1035
1036 if (e->code > LED_MAX)
1037 return 1;
1038
1039 set_bit_state(dev->led_values, e->code, e->value != 0);
1040
1041 return 0;
1042 }
1043
1044 static int
update_sw_state(struct libevdev * dev,const struct input_event * e)1045 update_sw_state(struct libevdev *dev, const struct input_event *e)
1046 {
1047 if (!libevdev_has_event_type(dev, EV_SW))
1048 return 1;
1049
1050 if (e->code > SW_MAX)
1051 return 1;
1052
1053 set_bit_state(dev->sw_values, e->code, e->value != 0);
1054
1055 return 0;
1056 }
1057
1058 static int
update_state(struct libevdev * dev,const struct input_event * e)1059 update_state(struct libevdev *dev, const struct input_event *e)
1060 {
1061 int rc = 0;
1062
1063 switch(e->type) {
1064 case EV_SYN:
1065 case EV_REL:
1066 break;
1067 case EV_KEY:
1068 rc = update_key_state(dev, e);
1069 break;
1070 case EV_ABS:
1071 rc = update_abs_state(dev, e);
1072 break;
1073 case EV_LED:
1074 rc = update_led_state(dev, e);
1075 break;
1076 case EV_SW:
1077 rc = update_sw_state(dev, e);
1078 break;
1079 }
1080
1081 dev->last_event_time.tv_sec = e->input_event_sec;
1082 dev->last_event_time.tv_usec = e->input_event_usec;
1083
1084 return rc;
1085 }
1086
1087 /**
1088 * Sanitize/modify events where needed.
1089 */
1090 static inline enum event_filter_status
sanitize_event(const struct libevdev * dev,struct input_event * ev,enum SyncState sync_state)1091 sanitize_event(const struct libevdev *dev,
1092 struct input_event *ev,
1093 enum SyncState sync_state)
1094 {
1095 if (!libevdev_has_event_code(dev, ev->type, ev->code))
1096 return EVENT_FILTER_DISCARD;
1097
1098 if (unlikely(dev->num_slots > -1 &&
1099 libevdev_event_is_code(ev, EV_ABS, ABS_MT_SLOT) &&
1100 (ev->value < 0 || ev->value >= dev->num_slots))) {
1101 log_bug(dev, "Device \"%s\" received an invalid slot index %d."
1102 "Capping to announced max slot number %d.\n",
1103 dev->name, ev->value, dev->num_slots - 1);
1104 ev->value = dev->num_slots - 1;
1105 return EVENT_FILTER_MODIFIED;
1106
1107 /* Drop any invalid tracking IDs, they are only supposed to go from
1108 N to -1 or from -1 to N. Never from -1 to -1, or N to M. Very
1109 unlikely to ever happen from a real device.
1110 */
1111 }
1112
1113 if (unlikely(sync_state == SYNC_NONE &&
1114 dev->num_slots > -1 &&
1115 libevdev_event_is_code(ev, EV_ABS, ABS_MT_TRACKING_ID) &&
1116 ((ev->value == -1 &&
1117 *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) == -1) ||
1118 (ev->value != -1 &&
1119 *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) != -1)))) {
1120 log_bug(dev, "Device \"%s\" received a double tracking ID %d in slot %d.\n",
1121 dev->name, ev->value, dev->current_slot);
1122 return EVENT_FILTER_DISCARD;
1123 }
1124
1125 return EVENT_FILTER_NONE;
1126 }
1127
1128 LIBEVDEV_EXPORT int
libevdev_next_event(struct libevdev * dev,unsigned int flags,struct input_event * ev)1129 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
1130 {
1131 int rc = LIBEVDEV_READ_STATUS_SUCCESS;
1132 enum event_filter_status filter_status;
1133 const unsigned int valid_flags = LIBEVDEV_READ_FLAG_NORMAL |
1134 LIBEVDEV_READ_FLAG_SYNC |
1135 LIBEVDEV_READ_FLAG_FORCE_SYNC |
1136 LIBEVDEV_READ_FLAG_BLOCKING;
1137
1138 if (!dev->initialized) {
1139 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1140 return -EBADF;
1141 }
1142
1143 if (dev->fd < 0)
1144 return -EBADF;
1145
1146 if ((flags & valid_flags) == 0) {
1147 log_bug(dev, "invalid flags %#x.\n", flags);
1148 return -EINVAL;
1149 }
1150
1151 if (flags & LIBEVDEV_READ_FLAG_SYNC) {
1152 if (dev->sync_state == SYNC_NEEDED) {
1153 rc = sync_state(dev);
1154 if (rc != 0)
1155 return rc;
1156 dev->sync_state = SYNC_IN_PROGRESS;
1157 }
1158
1159 if (dev->queue_nsync == 0) {
1160 dev->sync_state = SYNC_NONE;
1161 return -EAGAIN;
1162 }
1163
1164 } else if (dev->sync_state != SYNC_NONE) {
1165 struct input_event e;
1166
1167 /* call update_state for all events here, otherwise the library has the wrong view
1168 of the device too */
1169 while (queue_shift(dev, &e) == 0) {
1170 dev->queue_nsync--;
1171 if (sanitize_event(dev, &e, dev->sync_state) != EVENT_FILTER_DISCARD)
1172 update_state(dev, &e);
1173 }
1174
1175 dev->sync_state = SYNC_NONE;
1176 }
1177
1178 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
1179 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
1180
1181 Except if the fd is in blocking mode and we still have events from the last read, don't
1182 read in any more.
1183 */
1184 do {
1185 if (queue_num_elements(dev) == 0) {
1186 rc = read_more_events(dev);
1187 if (rc < 0 && rc != -EAGAIN)
1188 goto out;
1189 }
1190
1191 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
1192 dev->sync_state = SYNC_NEEDED;
1193 rc = LIBEVDEV_READ_STATUS_SYNC;
1194 goto out;
1195 }
1196
1197 if (queue_shift(dev, ev) != 0)
1198 return -EAGAIN;
1199
1200 filter_status = sanitize_event(dev, ev, dev->sync_state);
1201 if (filter_status != EVENT_FILTER_DISCARD)
1202 update_state(dev, ev);
1203
1204 /* if we disabled a code, get the next event instead */
1205 } while(filter_status == EVENT_FILTER_DISCARD ||
1206 !libevdev_has_event_code(dev, ev->type, ev->code));
1207
1208 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1209 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
1210 dev->sync_state = SYNC_NEEDED;
1211 rc = LIBEVDEV_READ_STATUS_SYNC;
1212 }
1213
1214 if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
1215 dev->queue_nsync--;
1216 rc = LIBEVDEV_READ_STATUS_SYNC;
1217 if (dev->queue_nsync == 0)
1218 dev->sync_state = SYNC_NONE;
1219 }
1220
1221 out:
1222 return rc;
1223 }
1224
1225 LIBEVDEV_EXPORT int
libevdev_has_event_pending(struct libevdev * dev)1226 libevdev_has_event_pending(struct libevdev *dev)
1227 {
1228 struct pollfd fds = { dev->fd, POLLIN, 0 };
1229 int rc;
1230
1231 if (!dev->initialized) {
1232 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1233 return -EBADF;
1234 }
1235
1236 if (dev->fd < 0)
1237 return -EBADF;
1238
1239 if (queue_num_elements(dev) != 0)
1240 return 1;
1241
1242 rc = poll(&fds, 1, 0);
1243 return (rc >= 0) ? rc : -errno;
1244 }
1245
1246 LIBEVDEV_EXPORT const char *
libevdev_get_name(const struct libevdev * dev)1247 libevdev_get_name(const struct libevdev *dev)
1248 {
1249 return dev->name ? dev->name : "";
1250 }
1251
1252 LIBEVDEV_EXPORT const char *
libevdev_get_phys(const struct libevdev * dev)1253 libevdev_get_phys(const struct libevdev *dev)
1254 {
1255 return dev->phys;
1256 }
1257
1258 LIBEVDEV_EXPORT const char *
libevdev_get_uniq(const struct libevdev * dev)1259 libevdev_get_uniq(const struct libevdev *dev)
1260 {
1261 return dev->uniq;
1262 }
1263
1264 #define STRING_SETTER(field) \
1265 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
1266 { \
1267 if (field == NULL) \
1268 return; \
1269 free(dev->field); \
1270 dev->field = strdup(field); \
1271 }
1272
1273 STRING_SETTER(name)
STRING_SETTER(phys)1274 STRING_SETTER(phys)
1275 STRING_SETTER(uniq)
1276
1277 #define PRODUCT_GETTER(name) \
1278 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
1279 { \
1280 return dev->ids.name; \
1281 }
1282
1283 PRODUCT_GETTER(product)
1284 PRODUCT_GETTER(vendor)
1285 PRODUCT_GETTER(bustype)
1286 PRODUCT_GETTER(version)
1287
1288 #define PRODUCT_SETTER(field) \
1289 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
1290 { \
1291 dev->ids.field = field;\
1292 }
1293
1294 PRODUCT_SETTER(product)
1295 PRODUCT_SETTER(vendor)
1296 PRODUCT_SETTER(bustype)
1297 PRODUCT_SETTER(version)
1298
1299 LIBEVDEV_EXPORT int
1300 libevdev_get_driver_version(const struct libevdev *dev)
1301 {
1302 return dev->driver_version;
1303 }
1304
1305 LIBEVDEV_EXPORT int
libevdev_has_property(const struct libevdev * dev,unsigned int prop)1306 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
1307 {
1308 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
1309 }
1310
1311 LIBEVDEV_EXPORT int
libevdev_enable_property(struct libevdev * dev,unsigned int prop)1312 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
1313 {
1314 if (prop > INPUT_PROP_MAX)
1315 return -1;
1316
1317 set_bit(dev->props, prop);
1318 return 0;
1319 }
1320
1321 LIBEVDEV_EXPORT int
libevdev_disable_property(struct libevdev * dev,unsigned int prop)1322 libevdev_disable_property(struct libevdev *dev, unsigned int prop)
1323 {
1324 if (prop > INPUT_PROP_MAX)
1325 return -1;
1326
1327 clear_bit(dev->props, prop);
1328 return 0;
1329 }
1330
1331 LIBEVDEV_EXPORT int
libevdev_has_event_type(const struct libevdev * dev,unsigned int type)1332 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
1333 {
1334 return type == EV_SYN ||(type <= EV_MAX && bit_is_set(dev->bits, type));
1335 }
1336
1337 LIBEVDEV_EXPORT int
libevdev_has_event_code(const struct libevdev * dev,unsigned int type,unsigned int code)1338 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
1339 {
1340 const unsigned long *mask = NULL;
1341 int max;
1342
1343 if (!libevdev_has_event_type(dev, type))
1344 return 0;
1345
1346 if (type == EV_SYN)
1347 return 1;
1348
1349 max = type_to_mask_const(dev, type, &mask);
1350
1351 if (max == -1 || code > (unsigned int)max)
1352 return 0;
1353
1354 return bit_is_set(mask, code);
1355 }
1356
1357 LIBEVDEV_EXPORT int
libevdev_get_event_value(const struct libevdev * dev,unsigned int type,unsigned int code)1358 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
1359 {
1360 int value = 0;
1361
1362 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1363 return 0;
1364
1365 switch (type) {
1366 case EV_ABS: value = dev->abs_info[code].value; break;
1367 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
1368 case EV_LED: value = bit_is_set(dev->led_values, code); break;
1369 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
1370 case EV_REP:
1371 switch(code) {
1372 case REP_DELAY:
1373 libevdev_get_repeat(dev, &value, NULL);
1374 break;
1375 case REP_PERIOD:
1376 libevdev_get_repeat(dev, NULL, &value);
1377 break;
1378 default:
1379 value = 0;
1380 break;
1381 }
1382 break;
1383 default:
1384 value = 0;
1385 break;
1386 }
1387
1388 return value;
1389 }
1390
1391 LIBEVDEV_EXPORT int
libevdev_set_event_value(struct libevdev * dev,unsigned int type,unsigned int code,int value)1392 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
1393 {
1394 int rc = 0;
1395 struct input_event e;
1396
1397 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1398 return -1;
1399
1400 e.type = type;
1401 e.code = code;
1402 e.value = value;
1403
1404 if (sanitize_event(dev, &e, SYNC_NONE) != EVENT_FILTER_NONE)
1405 return -1;
1406
1407 switch(type) {
1408 case EV_ABS: rc = update_abs_state(dev, &e); break;
1409 case EV_KEY: rc = update_key_state(dev, &e); break;
1410 case EV_LED: rc = update_led_state(dev, &e); break;
1411 case EV_SW: rc = update_sw_state(dev, &e); break;
1412 default:
1413 rc = -1;
1414 break;
1415 }
1416
1417 return rc;
1418 }
1419
1420 LIBEVDEV_EXPORT int
libevdev_fetch_event_value(const struct libevdev * dev,unsigned int type,unsigned int code,int * value)1421 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1422 {
1423 if (libevdev_has_event_type(dev, type) &&
1424 libevdev_has_event_code(dev, type, code)) {
1425 *value = libevdev_get_event_value(dev, type, code);
1426 return 1;
1427 }
1428
1429 return 0;
1430 }
1431
1432 LIBEVDEV_EXPORT int
libevdev_get_slot_value(const struct libevdev * dev,unsigned int slot,unsigned int code)1433 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1434 {
1435 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1436 return 0;
1437
1438 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots)
1439 return 0;
1440
1441 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1442 return 0;
1443
1444 return *slot_value(dev, slot, code);
1445 }
1446
1447 LIBEVDEV_EXPORT int
libevdev_set_slot_value(struct libevdev * dev,unsigned int slot,unsigned int code,int value)1448 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1449 {
1450 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1451 return -1;
1452
1453 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots)
1454 return -1;
1455
1456 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1457 return -1;
1458
1459 if (code == ABS_MT_SLOT) {
1460 if (value < 0 || value >= libevdev_get_num_slots(dev))
1461 return -1;
1462 dev->current_slot = value;
1463 }
1464
1465 *slot_value(dev, slot, code) = value;
1466
1467 return 0;
1468 }
1469
1470 LIBEVDEV_EXPORT int
libevdev_fetch_slot_value(const struct libevdev * dev,unsigned int slot,unsigned int code,int * value)1471 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1472 {
1473 if (libevdev_has_event_type(dev, EV_ABS) &&
1474 libevdev_has_event_code(dev, EV_ABS, code) &&
1475 dev->num_slots >= 0 &&
1476 slot < (unsigned int)dev->num_slots) {
1477 *value = libevdev_get_slot_value(dev, slot, code);
1478 return 1;
1479 }
1480
1481 return 0;
1482 }
1483
1484 LIBEVDEV_EXPORT int
libevdev_get_num_slots(const struct libevdev * dev)1485 libevdev_get_num_slots(const struct libevdev *dev)
1486 {
1487 return dev->num_slots;
1488 }
1489
1490 LIBEVDEV_EXPORT int
libevdev_get_current_slot(const struct libevdev * dev)1491 libevdev_get_current_slot(const struct libevdev *dev)
1492 {
1493 return dev->current_slot;
1494 }
1495
1496 LIBEVDEV_EXPORT const struct input_absinfo*
libevdev_get_abs_info(const struct libevdev * dev,unsigned int code)1497 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1498 {
1499 if (!libevdev_has_event_type(dev, EV_ABS) ||
1500 !libevdev_has_event_code(dev, EV_ABS, code))
1501 return NULL;
1502
1503 return &dev->abs_info[code];
1504 }
1505
1506 #define ABS_GETTER(name) \
1507 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1508 { \
1509 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1510 return absinfo ? absinfo->name : 0; \
1511 }
1512
1513 ABS_GETTER(maximum)
ABS_GETTER(minimum)1514 ABS_GETTER(minimum)
1515 ABS_GETTER(fuzz)
1516 ABS_GETTER(flat)
1517 ABS_GETTER(resolution)
1518
1519 #define ABS_SETTER(field) \
1520 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1521 { \
1522 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1523 return; \
1524 dev->abs_info[code].field = val; \
1525 }
1526
1527 ABS_SETTER(maximum)
1528 ABS_SETTER(minimum)
1529 ABS_SETTER(fuzz)
1530 ABS_SETTER(flat)
1531 ABS_SETTER(resolution)
1532
1533 LIBEVDEV_EXPORT void
1534 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1535 {
1536 if (!libevdev_has_event_code(dev, EV_ABS, code))
1537 return;
1538
1539 dev->abs_info[code] = *abs;
1540 }
1541
1542 LIBEVDEV_EXPORT int
libevdev_enable_event_type(struct libevdev * dev,unsigned int type)1543 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1544 {
1545 int max;
1546
1547 if (type > EV_MAX)
1548 return -1;
1549
1550 if (libevdev_has_event_type(dev, type))
1551 return 0;
1552
1553 max = libevdev_event_type_get_max(type);
1554 if (max == -1)
1555 return -1;
1556
1557 set_bit(dev->bits, type);
1558
1559 if (type == EV_REP) {
1560 int delay = 0, period = 0;
1561 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1562 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1563 }
1564 return 0;
1565 }
1566
1567 LIBEVDEV_EXPORT int
libevdev_disable_event_type(struct libevdev * dev,unsigned int type)1568 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1569 {
1570 int max;
1571
1572 if (type > EV_MAX || type == EV_SYN)
1573 return -1;
1574
1575 max = libevdev_event_type_get_max(type);
1576 if (max == -1)
1577 return -1;
1578
1579 clear_bit(dev->bits, type);
1580
1581 return 0;
1582 }
1583
1584 LIBEVDEV_EXPORT int
libevdev_enable_event_code(struct libevdev * dev,unsigned int type,unsigned int code,const void * data)1585 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1586 unsigned int code, const void *data)
1587 {
1588 unsigned int max;
1589 unsigned long *mask = NULL;
1590
1591 if (libevdev_enable_event_type(dev, type))
1592 return -1;
1593
1594 switch(type) {
1595 case EV_SYN:
1596 return 0;
1597 case EV_ABS:
1598 case EV_REP:
1599 if (data == NULL)
1600 return -1;
1601 break;
1602 default:
1603 if (data != NULL)
1604 return -1;
1605 break;
1606 }
1607
1608 max = type_to_mask(dev, type, &mask);
1609
1610 if (code > max || (int)max == -1)
1611 return -1;
1612
1613 set_bit(mask, code);
1614
1615 if (type == EV_ABS) {
1616 const struct input_absinfo *abs = data;
1617 dev->abs_info[code] = *abs;
1618 if (code == ABS_MT_SLOT) {
1619 if (init_slots(dev) != 0)
1620 return -1;
1621 } else if (code == ABS_MT_TRACKING_ID) {
1622 reset_tracking_ids(dev);
1623 }
1624 } else if (type == EV_REP) {
1625 const int *value = data;
1626 dev->rep_values[code] = *value;
1627 }
1628
1629 return 0;
1630 }
1631
1632 LIBEVDEV_EXPORT int
libevdev_disable_event_code(struct libevdev * dev,unsigned int type,unsigned int code)1633 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1634 {
1635 unsigned int max;
1636 unsigned long *mask = NULL;
1637
1638 if (type > EV_MAX || type == EV_SYN)
1639 return -1;
1640
1641 max = type_to_mask(dev, type, &mask);
1642
1643 if (code > max || (int)max == -1)
1644 return -1;
1645
1646 clear_bit(mask, code);
1647
1648 if (type == EV_ABS) {
1649 if (code == ABS_MT_SLOT) {
1650 if (init_slots(dev) != 0)
1651 return -1;
1652 } else if (code == ABS_MT_TRACKING_ID) {
1653 reset_tracking_ids(dev);
1654 }
1655 }
1656
1657 return 0;
1658 }
1659
1660 LIBEVDEV_EXPORT int
libevdev_kernel_set_abs_info(struct libevdev * dev,unsigned int code,const struct input_absinfo * abs)1661 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1662 {
1663 int rc;
1664
1665 if (!dev->initialized) {
1666 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1667 return -EBADF;
1668 }
1669
1670 if (dev->fd < 0)
1671 return -EBADF;
1672
1673 if (code > ABS_MAX)
1674 return -EINVAL;
1675
1676 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1677 if (rc < 0)
1678 rc = -errno;
1679 else
1680 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1681
1682 return rc;
1683 }
1684
1685 LIBEVDEV_EXPORT int
libevdev_grab(struct libevdev * dev,enum libevdev_grab_mode grab)1686 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1687 {
1688 int rc = 0;
1689
1690 if (!dev->initialized) {
1691 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1692 return -EBADF;
1693 }
1694
1695 if (dev->fd < 0)
1696 return -EBADF;
1697
1698 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1699 log_bug(dev, "invalid grab parameter %#x\n", grab);
1700 return -EINVAL;
1701 }
1702
1703 if (grab == dev->grabbed)
1704 return 0;
1705
1706 if (grab == LIBEVDEV_GRAB)
1707 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1708 else if (grab == LIBEVDEV_UNGRAB)
1709 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1710
1711 if (rc == 0)
1712 dev->grabbed = grab;
1713
1714 return rc < 0 ? -errno : 0;
1715 }
1716
1717 LIBEVDEV_EXPORT int
libevdev_event_is_type(const struct input_event * ev,unsigned int type)1718 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1719 {
1720 return type < EV_CNT && ev->type == type;
1721 }
1722
1723 LIBEVDEV_EXPORT int
libevdev_event_is_code(const struct input_event * ev,unsigned int type,unsigned int code)1724 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1725 {
1726 int max;
1727
1728 if (!libevdev_event_is_type(ev, type))
1729 return 0;
1730
1731 max = libevdev_event_type_get_max(type);
1732 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1733 }
1734
1735 LIBEVDEV_EXPORT const char*
libevdev_event_type_get_name(unsigned int type)1736 libevdev_event_type_get_name(unsigned int type)
1737 {
1738 if (type > EV_MAX)
1739 return NULL;
1740
1741 return ev_map[type];
1742 }
1743
1744 LIBEVDEV_EXPORT const char*
libevdev_event_code_get_name(unsigned int type,unsigned int code)1745 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1746 {
1747 int max = libevdev_event_type_get_max(type);
1748
1749 if (max == -1 || code > (unsigned int)max)
1750 return NULL;
1751
1752 return event_type_map[type][code];
1753 }
1754
1755 LIBEVDEV_EXPORT const char *
libevdev_event_value_get_name(unsigned int type,unsigned int code,int value)1756 libevdev_event_value_get_name(unsigned int type,
1757 unsigned int code,
1758 int value)
1759 {
1760 /* This is a simplified version because nothing else
1761 is an enum like ABS_MT_TOOL_TYPE so we don't need
1762 a generic lookup */
1763 if (type != EV_ABS || code != ABS_MT_TOOL_TYPE)
1764 return NULL;
1765
1766 if (value < 0 || value > MT_TOOL_MAX)
1767 return NULL;
1768
1769 return mt_tool_map[value];
1770 }
1771
1772 LIBEVDEV_EXPORT const char*
libevdev_property_get_name(unsigned int prop)1773 libevdev_property_get_name(unsigned int prop)
1774 {
1775 if (prop > INPUT_PROP_MAX)
1776 return NULL;
1777
1778 return input_prop_map[prop];
1779 }
1780
1781 LIBEVDEV_EXPORT int
libevdev_event_type_get_max(unsigned int type)1782 libevdev_event_type_get_max(unsigned int type)
1783 {
1784 if (type > EV_MAX)
1785 return -1;
1786
1787 return ev_max[type];
1788 }
1789
1790 LIBEVDEV_EXPORT int
libevdev_get_repeat(const struct libevdev * dev,int * delay,int * period)1791 libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
1792 {
1793 if (!libevdev_has_event_type(dev, EV_REP))
1794 return -1;
1795
1796 if (delay != NULL)
1797 *delay = dev->rep_values[REP_DELAY];
1798 if (period != NULL)
1799 *period = dev->rep_values[REP_PERIOD];
1800
1801 return 0;
1802 }
1803
1804 LIBEVDEV_EXPORT int
libevdev_kernel_set_led_value(struct libevdev * dev,unsigned int code,enum libevdev_led_value value)1805 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1806 {
1807 return libevdev_kernel_set_led_values(dev, code, value, -1);
1808 }
1809
1810 LIBEVDEV_EXPORT int
libevdev_kernel_set_led_values(struct libevdev * dev,...)1811 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1812 {
1813 struct input_event ev[LED_MAX + 1];
1814 enum libevdev_led_value val;
1815 va_list args;
1816 int code;
1817 int rc = 0;
1818 size_t nleds = 0;
1819
1820 if (!dev->initialized) {
1821 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1822 return -EBADF;
1823 }
1824
1825 if (dev->fd < 0)
1826 return -EBADF;
1827
1828 memset(ev, 0, sizeof(ev));
1829
1830 va_start(args, dev);
1831 code = va_arg(args, unsigned int);
1832 while (code != -1) {
1833 if (code > LED_MAX) {
1834 rc = -EINVAL;
1835 break;
1836 }
1837 val = va_arg(args, enum libevdev_led_value);
1838 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1839 rc = -EINVAL;
1840 break;
1841 }
1842
1843 if (libevdev_has_event_code(dev, EV_LED, code)) {
1844 struct input_event *e = ev;
1845
1846 while (e->type > 0 && e->code != code)
1847 e++;
1848
1849 if (e->type == 0)
1850 nleds++;
1851 e->type = EV_LED;
1852 e->code = code;
1853 e->value = (val == LIBEVDEV_LED_ON);
1854 }
1855 code = va_arg(args, unsigned int);
1856 }
1857 va_end(args);
1858
1859 if (rc == 0 && nleds > 0) {
1860 ev[nleds].type = EV_SYN;
1861 ev[nleds++].code = SYN_REPORT;
1862
1863 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1864 if (rc > 0) {
1865 nleds--; /* last is EV_SYN */
1866 while (nleds--)
1867 update_led_state(dev, &ev[nleds]);
1868 }
1869 rc = (rc != -1) ? 0 : -errno;
1870 }
1871
1872 return rc;
1873 }
1874
1875 LIBEVDEV_EXPORT int
libevdev_set_clock_id(struct libevdev * dev,int clockid)1876 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1877 {
1878 if (!dev->initialized) {
1879 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1880 return -EBADF;
1881 }
1882
1883 if (dev->fd < 0)
1884 return -EBADF;
1885
1886 return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;
1887 }
1888