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