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