Disallow disabling EV_SYN event codes
[platform/upstream/libevdev.git] / libevdev / libevdev.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24 #include <errno.h>
25 #include <poll.h>
26 #include <stdlib.h>
27 #include <string.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 /* DEPRECATED */
40 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_SYNC = LIBEVDEV_READ_FLAG_SYNC;
41 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_NORMAL = LIBEVDEV_READ_FLAG_NORMAL;
42 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_FORCE_SYNC = LIBEVDEV_READ_FLAG_FORCE_SYNC;
43 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_BLOCKING = LIBEVDEV_READ_FLAG_BLOCKING;
44
45 static int sync_mt_state(struct libevdev *dev, int create_events);
46
47 static int
48 init_event_queue(struct libevdev *dev)
49 {
50         /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
51            EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
52
53         const int QUEUE_SIZE = 256;
54
55         return queue_alloc(dev, QUEUE_SIZE);
56 }
57
58 static void
59 libevdev_dflt_log_func(enum libevdev_log_priority priority,
60                        void *data,
61                        const char *file, int line, const char *func,
62                        const char *format, va_list args)
63 {
64         const char *prefix;
65         switch(priority) {
66                 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
67                 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
68                 case LIBEVDEV_LOG_DEBUG:
69                                         prefix = "libevdev debug";
70                                         break;
71                 default:
72                                         prefix = "libevdev INVALID LOG PRIORITY";
73                                         break;
74         }
75         /* default logging format:
76            libevev error in libevdev_some_func: blah blah
77            libevev info in libevdev_some_func: blah blah
78            libevev debug in file.c:123:libevdev_some_func: blah blah
79          */
80
81         fprintf(stderr, "%s in ", prefix);
82         if (priority == LIBEVDEV_LOG_DEBUG)
83                 fprintf(stderr, "%s:%d:", file, line);
84         fprintf(stderr, "%s: ", func);
85         vfprintf(stderr, format, args);
86 }
87
88 /*
89  * Global logging settings.
90  */
91 struct logdata log_data = {
92         LIBEVDEV_LOG_INFO,
93         libevdev_dflt_log_func,
94         NULL,
95 };
96
97 void
98 log_msg(enum libevdev_log_priority priority,
99         void *data,
100         const char *file, int line, const char *func,
101         const char *format, ...)
102 {
103         va_list args;
104
105         if (!log_data.handler)
106                 return;
107
108         va_start(args, format);
109         log_data.handler(priority, data, file, line, func, format, args);
110         va_end(args);
111 }
112
113 static void
114 libevdev_reset(struct libevdev *dev)
115 {
116         memset(dev, 0, sizeof(*dev));
117         dev->fd = -1;
118         dev->initialized = false;
119         dev->num_slots = -1;
120         dev->current_slot = -1;
121         dev->grabbed = LIBEVDEV_UNGRAB;
122         dev->sync_state = SYNC_NONE;
123 }
124
125 LIBEVDEV_EXPORT struct libevdev*
126 libevdev_new(void)
127 {
128         struct libevdev *dev;
129
130         dev = malloc(sizeof(*dev));
131         if (!dev)
132                 return NULL;
133
134         libevdev_reset(dev);
135
136         return dev;
137 }
138
139 LIBEVDEV_EXPORT int
140 libevdev_new_from_fd(int fd, struct libevdev **dev)
141 {
142         struct libevdev *d;
143         int rc;
144
145         d = libevdev_new();
146         if (!d)
147                 return -ENOMEM;
148
149         rc = libevdev_set_fd(d, fd);
150         if (rc < 0)
151                 libevdev_free(d);
152         else
153                 *dev = d;
154         return rc;
155 }
156
157 LIBEVDEV_EXPORT void
158 libevdev_free(struct libevdev *dev)
159 {
160         if (!dev)
161                 return;
162
163         free(dev->name);
164         free(dev->phys);
165         free(dev->uniq);
166         queue_free(dev);
167         free(dev);
168 }
169
170 /* DEPRECATED */
171 LIBEVDEV_EXPORT void
172 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
173 {
174         /* Can't be backwards compatible to this yet, so don't even try */
175         fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
176 }
177
178 LIBEVDEV_EXPORT void
179 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
180 {
181         log_data.handler = logfunc;
182         log_data.userdata = data;
183 }
184
185 LIBEVDEV_EXPORT void
186 libevdev_set_log_priority(enum libevdev_log_priority priority)
187 {
188         if (priority > LIBEVDEV_LOG_DEBUG)
189                 priority = LIBEVDEV_LOG_DEBUG;
190         log_data.priority = priority;
191 }
192
193 LIBEVDEV_EXPORT enum libevdev_log_priority
194 libevdev_get_log_priority(void)
195 {
196         return log_data.priority;
197 }
198
199 LIBEVDEV_EXPORT int
200 libevdev_change_fd(struct libevdev *dev, int fd)
201 {
202         if (!dev->initialized) {
203                 log_bug("device not initialized. call libevdev_set_fd() first\n");
204                 return -1;
205         }
206         dev->fd = fd;
207         return 0;
208 }
209
210 LIBEVDEV_EXPORT int
211 libevdev_set_fd(struct libevdev* dev, int fd)
212 {
213         int rc;
214         int i;
215         char buf[256];
216
217         if (dev->initialized) {
218                 log_bug("device already initialized.\n");
219                 return -EBADF;
220         } else if (fd < 0)
221                 return -EBADF;
222
223         libevdev_reset(dev);
224
225         rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
226         if (rc < 0)
227                 goto out;
228
229         memset(buf, 0, sizeof(buf));
230         rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
231         if (rc < 0)
232                 goto out;
233
234         free(dev->name);
235         dev->name = strdup(buf);
236         if (!dev->name) {
237                 errno = ENOMEM;
238                 goto out;
239         }
240
241         free(dev->phys);
242         dev->phys = NULL;
243         memset(buf, 0, sizeof(buf));
244         rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
245         if (rc < 0) {
246                 /* uinput has no phys */
247                 if (errno != ENOENT)
248                         goto out;
249         } else {
250                 dev->phys = strdup(buf);
251                 if (!dev->phys) {
252                         errno = ENOMEM;
253                         goto out;
254                 }
255         }
256
257         free(dev->uniq);
258         dev->uniq = NULL;
259         memset(buf, 0, sizeof(buf));
260         rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
261         if (rc < 0) {
262                 if (errno != ENOENT)
263                         goto out;
264         } else  {
265                 dev->uniq = strdup(buf);
266                 if (!dev->uniq) {
267                         errno = ENOMEM;
268                         goto out;
269                 }
270         }
271
272         rc = ioctl(fd, EVIOCGID, &dev->ids);
273         if (rc < 0)
274                 goto out;
275
276         rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
277         if (rc < 0)
278                 goto out;
279
280         /* Built on a kernel with props, running against a kernel without property
281            support. This should not be a fatal case, we'll be missing properties but other
282            than that everything is as expected.
283          */
284         rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
285         if (rc < 0 && errno != EINVAL)
286                 goto out;
287
288         rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
289         if (rc < 0)
290                 goto out;
291
292         rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
293         if (rc < 0)
294                 goto out;
295
296         rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
297         if (rc < 0)
298                 goto out;
299
300         rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
301         if (rc < 0)
302                 goto out;
303
304         rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
305         if (rc < 0)
306                 goto out;
307
308         rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
309         if (rc < 0)
310                 goto out;
311
312         rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
313         if (rc < 0)
314                 goto out;
315
316         rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
317         if (rc < 0)
318                 goto out;
319
320         rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
321         if (rc < 0)
322                 goto out;
323
324         rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
325         if (rc < 0)
326                 goto out;
327
328         rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
329         if (rc < 0)
330                 goto out;
331
332         /* rep is a special case, always set it to 1 for both values if EV_REP is set */
333         if (bit_is_set(dev->bits, EV_REP)) {
334                 for (i = 0; i < REP_CNT; i++)
335                         set_bit(dev->rep_bits, i);
336                 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
337                 if (rc < 0)
338                         goto out;
339         }
340
341         for (i = ABS_X; i <= ABS_MAX; i++) {
342                 if (bit_is_set(dev->abs_bits, i)) {
343                         struct input_absinfo abs_info;
344                         rc = ioctl(fd, EVIOCGABS(i), &abs_info);
345                         if (rc < 0)
346                                 goto out;
347
348                         dev->abs_info[i] = abs_info;
349                         if (i == ABS_MT_SLOT) {
350                                 dev->num_slots = abs_info.maximum + 1;
351                                 dev->current_slot = abs_info.value;
352                         }
353
354                 }
355         }
356
357         dev->fd = fd;
358         sync_mt_state(dev, 0);
359
360         rc = init_event_queue(dev);
361         if (rc < 0) {
362                 dev->fd = -1;
363                 return -rc;
364         }
365
366         /* not copying key state because we won't know when we'll start to
367          * use this fd and key's are likely to change state by then.
368          * Same with the valuators, really, but they may not change.
369          */
370
371         dev->initialized = true;
372 out:
373         if (rc)
374                 libevdev_reset(dev);
375         return rc ? -errno : 0;
376 }
377
378 LIBEVDEV_EXPORT int
379 libevdev_get_fd(const struct libevdev* dev)
380 {
381         return dev->fd;
382 }
383
384 static inline void
385 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
386 {
387         ev->time = dev->last_event_time;
388         ev->type = type;
389         ev->code = code;
390         ev->value = value;
391 }
392
393 static int
394 sync_key_state(struct libevdev *dev)
395 {
396         int rc;
397         int i;
398         unsigned long keystate[NLONGS(KEY_CNT)] = {0};
399
400         rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
401         if (rc < 0)
402                 goto out;
403
404         for (i = 0; i < KEY_CNT; i++) {
405                 int old, new;
406                 old = bit_is_set(dev->key_values, i);
407                 new = bit_is_set(keystate, i);
408                 if (old ^ new) {
409                         struct input_event *ev = queue_push(dev);
410                         init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
411                 }
412         }
413
414         memcpy(dev->key_values, keystate, rc);
415
416         rc = 0;
417 out:
418         return rc ? -errno : 0;
419 }
420
421 static int
422 sync_sw_state(struct libevdev *dev)
423 {
424         int rc;
425         int i;
426         unsigned long swstate[NLONGS(SW_CNT)] = {0};
427
428         rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
429         if (rc < 0)
430                 goto out;
431
432         for (i = 0; i < SW_CNT; i++) {
433                 int old, new;
434                 old = bit_is_set(dev->sw_values, i);
435                 new = bit_is_set(swstate, i);
436                 if (old ^ new) {
437                         struct input_event *ev = queue_push(dev);
438                         init_event(dev, ev, EV_SW, i, new ? 1 : 0);
439                 }
440         }
441
442         memcpy(dev->sw_values, swstate, rc);
443
444         rc = 0;
445 out:
446         return rc ? -errno : 0;
447 }
448
449 static int
450 sync_led_state(struct libevdev *dev)
451 {
452         int rc;
453         int i;
454         unsigned long ledstate[NLONGS(LED_CNT)] = {0};
455
456         rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
457         if (rc < 0)
458                 goto out;
459
460         for (i = 0; i < LED_CNT; i++) {
461                 int old, new;
462                 old = bit_is_set(dev->led_values, i);
463                 new = bit_is_set(ledstate, i);
464                 if (old ^ new) {
465                         struct input_event *ev = queue_push(dev);
466                         init_event(dev, ev, EV_LED, i, new ? 1 : 0);
467                 }
468         }
469
470         memcpy(dev->led_values, ledstate, rc);
471
472         rc = 0;
473 out:
474         return rc ? -errno : 0;
475 }
476 static int
477 sync_abs_state(struct libevdev *dev)
478 {
479         int rc;
480         int i;
481
482         for (i = ABS_X; i < ABS_CNT; i++) {
483                 struct input_absinfo abs_info;
484
485                 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
486                         continue;
487
488                 if (!bit_is_set(dev->abs_bits, i))
489                         continue;
490
491                 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
492                 if (rc < 0)
493                         goto out;
494
495                 if (dev->abs_info[i].value != abs_info.value) {
496                         struct input_event *ev = queue_push(dev);
497
498                         init_event(dev, ev, EV_ABS, i, abs_info.value);
499                         dev->abs_info[i].value = abs_info.value;
500                 }
501         }
502
503         rc = 0;
504 out:
505         return rc ? -errno : 0;
506 }
507
508 static int
509 sync_mt_state(struct libevdev *dev, int create_events)
510 {
511         int rc;
512         int i;
513         int ioctl_success = 0;
514         struct mt_state {
515                 int code;
516                 int val[MAX_SLOTS];
517         } mt_state[ABS_MT_CNT];
518
519         memset(&mt_state, 0, sizeof(mt_state));
520
521         for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
522                 int idx;
523                 if (i == ABS_MT_SLOT)
524                         continue;
525
526                 if (!libevdev_has_event_code(dev, EV_ABS, i))
527                         continue;
528
529                 idx = i - ABS_MT_MIN;
530                 mt_state[idx].code = i;
531                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
532                 if (rc < 0) {
533                         /* if the first ioctl fails with -EINVAL, chances are the kernel
534                            doesn't support the ioctl. Simply continue */
535                         if (errno == -EINVAL && !ioctl_success) {
536                                 rc = 0;
537                         } else /* if the second, ... ioctl fails, really fail */
538                                 goto out;
539                 } else if (ioctl_success == 0)
540                         ioctl_success = 1;
541         }
542
543         for (i = 0; i < dev->num_slots; i++) {
544                 int j;
545                 struct input_event *ev;
546
547                 if (create_events) {
548                         ev = queue_push(dev);
549                         init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
550                 }
551
552                 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
553                         int jdx = j - ABS_MT_MIN;
554
555                         if (j == ABS_MT_SLOT)
556                                 continue;
557
558                         if (!libevdev_has_event_code(dev, EV_ABS, j))
559                                 continue;
560
561                         if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
562                                 continue;
563
564                         if (create_events) {
565                                 ev = queue_push(dev);
566                                 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
567                         }
568                         dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
569                 }
570         }
571
572         rc = 0;
573 out:
574         return rc ? -errno : 0;
575 }
576
577 static int
578 sync_state(struct libevdev *dev)
579 {
580         int i;
581         int rc = 0;
582         struct input_event *ev;
583
584         /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
585            queue[0]) we need to shift this backwards. Except that chances are that the
586            queue may be either full or too full to prepend all the events needed for
587            SYNC_IN_PROGRESS.
588
589            so we search for the last sync event in the queue and drop everything before
590            including that event and rely on the kernel to tell us the right value for that
591            bitfield during the sync process.
592          */
593
594         for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
595                 struct input_event e = {{0,0}, 0, 0, 0};
596                 queue_peek(dev, i, &e);
597                 if (e.type == EV_SYN)
598                         break;
599         }
600
601         if (i > 0)
602                 queue_shift_multiple(dev, i + 1, NULL);
603
604         if (libevdev_has_event_type(dev, EV_KEY))
605                 rc = sync_key_state(dev);
606         if (libevdev_has_event_type(dev, EV_LED))
607                 rc = sync_led_state(dev);
608         if (libevdev_has_event_type(dev, EV_SW))
609                 rc = sync_sw_state(dev);
610         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
611                 rc = sync_abs_state(dev);
612         if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
613                 rc = sync_mt_state(dev, 1);
614
615         dev->queue_nsync = queue_num_elements(dev);
616
617         if (dev->queue_nsync > 0) {
618                 ev = queue_push(dev);
619                 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
620                 dev->queue_nsync++;
621         }
622
623         return rc;
624 }
625
626 static int
627 update_key_state(struct libevdev *dev, const struct input_event *e)
628 {
629         if (!libevdev_has_event_type(dev, EV_KEY))
630                 return 1;
631
632         if (e->code > KEY_MAX)
633                 return 1;
634
635         set_bit_state(dev->key_values, e->code, e->value != 0);
636
637         return 0;
638 }
639
640 static int
641 update_mt_state(struct libevdev *dev, const struct input_event *e)
642 {
643         if (e->code == ABS_MT_SLOT) {
644                 int i;
645                 dev->current_slot = e->value;
646                 /* sync abs_info with the current slot values */
647                 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
648                         if (libevdev_has_event_code(dev, EV_ABS, i))
649                                 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
650                 }
651
652                 return 0;
653         } else if (dev->current_slot == -1)
654                 return 1;
655
656         dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
657
658         return 0;
659 }
660
661 static int
662 update_abs_state(struct libevdev *dev, const struct input_event *e)
663 {
664         if (!libevdev_has_event_type(dev, EV_ABS))
665                 return 1;
666
667         if (e->code > ABS_MAX)
668                 return 1;
669
670         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
671                 update_mt_state(dev, e);
672
673         dev->abs_info[e->code].value = e->value;
674
675         return 0;
676 }
677
678 static int
679 update_led_state(struct libevdev *dev, const struct input_event *e)
680 {
681         if (!libevdev_has_event_type(dev, EV_LED))
682                 return 1;
683
684         if (e->code > LED_MAX)
685                 return 1;
686
687         set_bit_state(dev->led_values, e->code, e->value != 0);
688
689         return 0;
690 }
691
692 static int
693 update_sw_state(struct libevdev *dev, const struct input_event *e)
694 {
695         if (!libevdev_has_event_type(dev, EV_SW))
696                 return 1;
697
698         if (e->code > SW_MAX)
699                 return 1;
700
701         set_bit_state(dev->sw_values, e->code, e->value != 0);
702
703         return 0;
704 }
705
706 static int
707 update_state(struct libevdev *dev, const struct input_event *e)
708 {
709         int rc = 0;
710
711         switch(e->type) {
712                 case EV_SYN:
713                 case EV_REL:
714                         break;
715                 case EV_KEY:
716                         rc = update_key_state(dev, e);
717                         break;
718                 case EV_ABS:
719                         rc = update_abs_state(dev, e);
720                         break;
721                 case EV_LED:
722                         rc = update_led_state(dev, e);
723                         break;
724                 case EV_SW:
725                         rc = update_sw_state(dev, e);
726                         break;
727         }
728
729         dev->last_event_time = e->time;
730
731         return rc;
732 }
733
734 static int
735 read_more_events(struct libevdev *dev)
736 {
737         int free_elem;
738         int len;
739         struct input_event *next;
740
741         free_elem = queue_num_free_elements(dev);
742         if (free_elem <= 0)
743                 return 0;
744
745         next = queue_next_element(dev);
746         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
747         if (len < 0) {
748                 return -errno;
749         } else if (len > 0 && len % sizeof(struct input_event) != 0)
750                 return -EINVAL;
751         else if (len > 0) {
752                 int nev = len/sizeof(struct input_event);
753                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
754         }
755
756         return 0;
757 }
758
759 LIBEVDEV_EXPORT int
760 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
761 {
762         int rc = LIBEVDEV_READ_STATUS_SUCCESS;
763
764         if (!dev->initialized) {
765                 log_bug("device not initialized. call libevdev_set_fd() first\n");
766                 return -EBADF;
767         } else if (dev->fd < 0)
768                 return -EBADF;
769
770         if (!(flags & (LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_SYNC|LIBEVDEV_READ_FLAG_FORCE_SYNC))) {
771                 log_bug("invalid flags %#x\n.\n", flags);
772                 return -EINVAL;
773         }
774
775         if (flags & LIBEVDEV_READ_FLAG_SYNC) {
776                 if (dev->sync_state == SYNC_NEEDED) {
777                         rc = sync_state(dev);
778                         if (rc != 0)
779                                 return rc;
780                         dev->sync_state = SYNC_IN_PROGRESS;
781                 }
782
783                 if (dev->queue_nsync == 0) {
784                         dev->sync_state = SYNC_NONE;
785                         return -EAGAIN;
786                 }
787
788         } else if (dev->sync_state != SYNC_NONE) {
789                 struct input_event e;
790
791                 /* call update_state for all events here, otherwise the library has the wrong view
792                    of the device too */
793                 while (queue_shift(dev, &e) == 0) {
794                         dev->queue_nsync--;
795                         update_state(dev, &e);
796                 }
797
798                 dev->sync_state = SYNC_NONE;
799         }
800
801         /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
802
803         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
804            worst case we don't read fast enough and end up with SYN_DROPPED anyway.
805
806            Except if the fd is in blocking mode and we still have events from the last read, don't
807            read in any more.
808          */
809         do {
810                 if (!(flags & LIBEVDEV_READ_FLAG_BLOCKING) ||
811                     queue_num_elements(dev) == 0) {
812                         rc = read_more_events(dev);
813                         if (rc < 0 && rc != -EAGAIN)
814                                 goto out;
815                 }
816
817                 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
818                         dev->sync_state = SYNC_NEEDED;
819                         rc = LIBEVDEV_READ_STATUS_SYNC;
820                         goto out;
821                 }
822
823
824                 if (queue_shift(dev, ev) != 0)
825                         return -EAGAIN;
826
827                 update_state(dev, ev);
828
829         /* if we disabled a code, get the next event instead */
830         } while(!libevdev_has_event_code(dev, ev->type, ev->code));
831
832         rc = 0;
833         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
834                 dev->sync_state = SYNC_NEEDED;
835                 rc = LIBEVDEV_READ_STATUS_SYNC;
836         }
837
838         if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
839                 dev->queue_nsync--;
840                 rc = LIBEVDEV_READ_STATUS_SYNC;
841                 if (dev->queue_nsync == 0)
842                         dev->sync_state = SYNC_NONE;
843         }
844
845 out:
846         return rc;
847 }
848
849 LIBEVDEV_EXPORT int
850 libevdev_has_event_pending(struct libevdev *dev)
851 {
852         struct pollfd fds = { dev->fd, POLLIN, 0 };
853         int rc;
854
855         if (!dev->initialized) {
856                 log_bug("device not initialized. call libevdev_set_fd() first\n");
857                 return -EBADF;
858         } else if (dev->fd < 0)
859                 return -EBADF;
860
861         if (queue_num_elements(dev) != 0)
862                 return 1;
863
864         rc = poll(&fds, 1, 0);
865         return (rc >= 0) ? rc : -errno;
866 }
867
868 LIBEVDEV_EXPORT const char *
869 libevdev_get_name(const struct libevdev *dev)
870 {
871         return dev->name ? dev->name : "";
872 }
873
874 LIBEVDEV_EXPORT const char *
875 libevdev_get_phys(const struct libevdev *dev)
876 {
877         return dev->phys;
878 }
879
880 LIBEVDEV_EXPORT const char *
881 libevdev_get_uniq(const struct libevdev *dev)
882 {
883         return dev->uniq;
884 }
885
886 #define STRING_SETTER(field) \
887 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
888 { \
889         if (field == NULL) \
890                 return; \
891         free(dev->field); \
892         dev->field = strdup(field); \
893 }
894
895 STRING_SETTER(name);
896 STRING_SETTER(phys);
897 STRING_SETTER(uniq);
898
899
900 #define PRODUCT_GETTER(name) \
901 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
902 { \
903         return dev->ids.name; \
904 }
905
906 PRODUCT_GETTER(product);
907 PRODUCT_GETTER(vendor);
908 PRODUCT_GETTER(bustype);
909 PRODUCT_GETTER(version);
910
911 #define PRODUCT_SETTER(field) \
912 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
913 { \
914         dev->ids.field = field;\
915 }
916
917 PRODUCT_SETTER(product);
918 PRODUCT_SETTER(vendor);
919 PRODUCT_SETTER(bustype);
920 PRODUCT_SETTER(version);
921
922 LIBEVDEV_EXPORT int
923 libevdev_get_driver_version(const struct libevdev *dev)
924 {
925         return dev->driver_version;
926 }
927
928 LIBEVDEV_EXPORT int
929 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
930 {
931         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
932 }
933
934 LIBEVDEV_EXPORT int
935 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
936 {
937         if (prop > INPUT_PROP_MAX)
938                 return -1;
939
940         set_bit(dev->props, prop);
941         return 0;
942 }
943
944 LIBEVDEV_EXPORT int
945 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
946 {
947         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
948 }
949
950 LIBEVDEV_EXPORT int
951 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
952 {
953         const unsigned long *mask;
954         int max;
955
956         if (!libevdev_has_event_type(dev, type))
957                 return 0;
958
959         if (type == EV_SYN)
960                 return 1;
961
962         max = type_to_mask_const(dev, type, &mask);
963
964         if (max == -1 || code > (unsigned int)max)
965                 return 0;
966
967         return bit_is_set(mask, code);
968 }
969
970 LIBEVDEV_EXPORT int
971 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
972 {
973         int value;
974
975         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
976                 return 0;
977
978         switch (type) {
979                 case EV_ABS: value = dev->abs_info[code].value; break;
980                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
981                 case EV_LED: value = bit_is_set(dev->led_values, code); break;
982                 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
983                 default:
984                         value = 0;
985                         break;
986         }
987
988         return value;
989 }
990
991 LIBEVDEV_EXPORT int
992 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
993 {
994         int rc = 0;
995         struct input_event e;
996
997         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
998                 return -1;
999
1000         e.type = type;
1001         e.code = code;
1002         e.value = value;
1003
1004         switch(type) {
1005                 case EV_ABS: rc = update_abs_state(dev, &e); break;
1006                 case EV_KEY: rc = update_key_state(dev, &e); break;
1007                 case EV_LED: rc = update_led_state(dev, &e); break;
1008                 case EV_SW: rc = update_sw_state(dev, &e); break;
1009                 default:
1010                              rc = -1;
1011                              break;
1012         }
1013
1014         return rc;
1015 }
1016
1017 LIBEVDEV_EXPORT int
1018 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1019 {
1020         if (libevdev_has_event_type(dev, type) &&
1021             libevdev_has_event_code(dev, type, code)) {
1022                 *value = libevdev_get_event_value(dev, type, code);
1023                 return 1;
1024         } else
1025                 return 0;
1026 }
1027
1028 LIBEVDEV_EXPORT int
1029 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1030 {
1031         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1032                 return 0;
1033
1034         if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1035                 return 0;
1036
1037         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1038                 return 0;
1039
1040         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1041 }
1042
1043 LIBEVDEV_EXPORT int
1044 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1045 {
1046         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1047                 return -1;
1048
1049         if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1050                 return -1;
1051
1052         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1053                 return -1;
1054
1055         if (code == ABS_MT_SLOT) {
1056                 if (value < 0 || value >= libevdev_get_num_slots(dev))
1057                         return -1;
1058                 dev->current_slot = value;
1059         }
1060
1061         dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1062
1063
1064         return 0;
1065 }
1066
1067 LIBEVDEV_EXPORT int
1068 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1069 {
1070         if (libevdev_has_event_type(dev, EV_ABS) &&
1071             libevdev_has_event_code(dev, EV_ABS, code) &&
1072             dev->num_slots >= 0 &&
1073             slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1074                 *value = libevdev_get_slot_value(dev, slot, code);
1075                 return 1;
1076         } else
1077                 return 0;
1078 }
1079
1080 LIBEVDEV_EXPORT int
1081 libevdev_get_num_slots(const struct libevdev *dev)
1082 {
1083         return dev->num_slots;
1084 }
1085
1086 LIBEVDEV_EXPORT int
1087 libevdev_get_current_slot(const struct libevdev *dev)
1088 {
1089         return dev->current_slot;
1090 }
1091
1092 LIBEVDEV_EXPORT const struct input_absinfo*
1093 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1094 {
1095         if (!libevdev_has_event_type(dev, EV_ABS) ||
1096             !libevdev_has_event_code(dev, EV_ABS, code))
1097                 return NULL;
1098
1099         return &dev->abs_info[code];
1100 }
1101
1102 #define ABS_GETTER(name) \
1103 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1104 { \
1105         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1106         return absinfo ? absinfo->name : 0; \
1107 }
1108
1109 ABS_GETTER(maximum);
1110 ABS_GETTER(minimum);
1111 ABS_GETTER(fuzz);
1112 ABS_GETTER(flat);
1113 ABS_GETTER(resolution);
1114
1115 #define ABS_SETTER(field) \
1116 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1117 { \
1118         if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1119                 return; \
1120         dev->abs_info[code].field = val; \
1121 }
1122
1123 ABS_SETTER(maximum)
1124 ABS_SETTER(minimum)
1125 ABS_SETTER(fuzz)
1126 ABS_SETTER(flat)
1127 ABS_SETTER(resolution)
1128
1129 LIBEVDEV_EXPORT void
1130 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1131 {
1132         if (!libevdev_has_event_code(dev, EV_ABS, code))
1133                 return;
1134
1135         dev->abs_info[code] = *abs;
1136 }
1137
1138 LIBEVDEV_EXPORT int
1139 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1140 {
1141         if (type > EV_MAX)
1142                 return -1;
1143
1144         if (libevdev_has_event_type(dev, type))
1145                 return 0;
1146
1147         set_bit(dev->bits, type);
1148
1149         if (type == EV_REP) {
1150                 int delay = 0, period = 0;
1151                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1152                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1153         }
1154         return 0;
1155 }
1156
1157 LIBEVDEV_EXPORT int
1158 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1159 {
1160         if (type > EV_MAX || type == EV_SYN)
1161                 return -1;
1162
1163         clear_bit(dev->bits, type);
1164
1165         return 0;
1166 }
1167
1168 LIBEVDEV_EXPORT int
1169 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1170                            unsigned int code, const void *data)
1171 {
1172         unsigned int max;
1173         unsigned long *mask = NULL;
1174
1175         if (libevdev_enable_event_type(dev, type))
1176                 return -1;
1177
1178         switch(type) {
1179                 case EV_SYN:
1180                         return 0;
1181                 case EV_ABS:
1182                 case EV_REP:
1183                         if (data == NULL)
1184                                 return -1;
1185                         break;
1186                 default:
1187                         if (data != NULL)
1188                                 return -1;
1189                         break;
1190         }
1191
1192         max = type_to_mask(dev, type, &mask);
1193
1194         if (code > max)
1195                 return -1;
1196
1197         set_bit(mask, code);
1198
1199         if (type == EV_ABS) {
1200                 const struct input_absinfo *abs = data;
1201                 dev->abs_info[code] = *abs;
1202         } else if (type == EV_REP) {
1203                 const int *value = data;
1204                 dev->rep_values[code] = *value;
1205         }
1206
1207         return 0;
1208 }
1209
1210 LIBEVDEV_EXPORT int
1211 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1212 {
1213         unsigned int max;
1214         unsigned long *mask = NULL;
1215
1216         if (type > EV_MAX || type == EV_SYN)
1217                 return -1;
1218
1219         max = type_to_mask(dev, type, &mask);
1220
1221         if (code > max)
1222                 return -1;
1223
1224         clear_bit(mask, code);
1225
1226         return 0;
1227 }
1228
1229 LIBEVDEV_EXPORT int
1230 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1231 {
1232         return libevdev_kernel_set_abs_info(dev, code, abs);
1233 }
1234
1235 LIBEVDEV_EXPORT int
1236 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1237 {
1238         int rc;
1239
1240         if (!dev->initialized) {
1241                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1242                 return -EBADF;
1243         } else if (dev->fd < 0)
1244                 return -EBADF;
1245
1246         if (code > ABS_MAX)
1247                 return -EINVAL;
1248
1249         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1250         if (rc < 0)
1251                 rc = -errno;
1252         else
1253                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1254
1255         return rc;
1256 }
1257
1258 LIBEVDEV_EXPORT int
1259 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1260 {
1261         int rc = 0;
1262
1263         if (!dev->initialized) {
1264                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1265                 return -EBADF;
1266         } else if (dev->fd < 0)
1267                 return -EBADF;
1268
1269         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1270                 log_bug("invalid grab parameter %#x\n", grab);
1271                 return -EINVAL;
1272         }
1273
1274         if (grab == dev->grabbed)
1275                 return 0;
1276
1277         if (grab == LIBEVDEV_GRAB)
1278                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1279         else if (grab == LIBEVDEV_UNGRAB)
1280                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1281
1282         if (rc == 0)
1283                 dev->grabbed = grab;
1284
1285         return rc < 0 ? -errno : 0;
1286 }
1287
1288 /* DEPRECATED */
1289 LIBEVDEV_EXPORT int
1290 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1291 ALIAS(libevdev_event_is_type);
1292
1293 LIBEVDEV_EXPORT int
1294 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1295 {
1296         return type < EV_CNT && ev->type == type;
1297 }
1298
1299 /* DEPRECATED */
1300 LIBEVDEV_EXPORT int
1301 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1302 ALIAS(libevdev_event_is_code);
1303
1304 LIBEVDEV_EXPORT int
1305 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1306 {
1307         int max;
1308
1309         if (!libevdev_event_is_type(ev, type))
1310                 return 0;
1311
1312         max = libevdev_event_type_get_max(type);
1313         return (max > -1 && code <= (unsigned int)max && ev->code == code);
1314 }
1315
1316 /* DEPRECATED */
1317 LIBEVDEV_EXPORT const char*
1318 libevdev_get_event_type_name(unsigned int type)
1319 ALIAS(libevdev_event_type_get_name);
1320
1321 LIBEVDEV_EXPORT const char*
1322 libevdev_event_type_get_name(unsigned int type)
1323 {
1324         if (type > EV_MAX)
1325                 return NULL;
1326
1327         return ev_map[type];
1328 }
1329
1330 /* DEPRECATED */
1331 LIBEVDEV_EXPORT const char*
1332 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1333 ALIAS(libevdev_event_code_get_name);
1334
1335 LIBEVDEV_EXPORT const char*
1336 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1337 {
1338         int max = libevdev_event_type_get_max(type);
1339
1340         if (max == -1 || code > (unsigned int)max)
1341                 return NULL;
1342
1343         return event_type_map[type][code];
1344 }
1345
1346 /* DEPRECATED */
1347 LIBEVDEV_EXPORT const char*
1348 libevdev_get_input_prop_name(unsigned int prop)
1349 ALIAS(libevdev_property_get_name);
1350
1351 /* DEPRECATED */
1352 LIBEVDEV_EXPORT const char*
1353 libevdev_get_property_name(unsigned int prop)
1354 ALIAS(libevdev_property_get_name);
1355
1356 LIBEVDEV_EXPORT const char*
1357 libevdev_property_get_name(unsigned int prop)
1358 {
1359         if (prop > INPUT_PROP_MAX)
1360                 return NULL;
1361
1362         return input_prop_map[prop];
1363 }
1364
1365 /* DEPRECATED */
1366 LIBEVDEV_EXPORT int
1367 libevdev_get_event_type_max(unsigned int type)
1368 ALIAS(libevdev_event_type_get_max);
1369
1370 LIBEVDEV_EXPORT int
1371 libevdev_event_type_get_max(unsigned int type)
1372 {
1373         if (type > EV_MAX)
1374                 return -1;
1375
1376         return ev_max[type];
1377 }
1378
1379 LIBEVDEV_EXPORT int
1380 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1381 {
1382         if (!libevdev_has_event_type(dev, EV_REP))
1383                 return -1;
1384
1385         if (delay != NULL)
1386                 *delay = dev->rep_values[REP_DELAY];
1387         if (period != NULL)
1388                 *period = dev->rep_values[REP_PERIOD];
1389
1390         return 0;
1391 }
1392
1393 LIBEVDEV_EXPORT int
1394 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1395 {
1396         return libevdev_kernel_set_led_values(dev, code, value, -1);
1397 }
1398
1399 LIBEVDEV_EXPORT int
1400 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1401 {
1402         struct input_event ev[LED_MAX + 1];
1403         enum libevdev_led_value val;
1404         va_list args;
1405         int code;
1406         int rc = 0;
1407         size_t nleds = 0;
1408
1409         if (!dev->initialized) {
1410                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1411                 return -EBADF;
1412         } else if (dev->fd < 0)
1413                 return -EBADF;
1414
1415         memset(ev, 0, sizeof(ev));
1416
1417         va_start(args, dev);
1418         code = va_arg(args, unsigned int);
1419         while (code != -1) {
1420                 if (code > LED_MAX) {
1421                         rc = -EINVAL;
1422                         break;
1423                 }
1424                 val = va_arg(args, enum libevdev_led_value);
1425                 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1426                         rc = -EINVAL;
1427                         break;
1428                 }
1429
1430                 if (libevdev_has_event_code(dev, EV_LED, code)) {
1431                         struct input_event *e = ev;
1432
1433                         while (e->type > 0 && e->code != code)
1434                                 e++;
1435
1436                         if (e->type == 0)
1437                                 nleds++;
1438                         e->type = EV_LED;
1439                         e->code = code;
1440                         e->value = (val == LIBEVDEV_LED_ON);
1441                 }
1442                 code = va_arg(args, unsigned int);
1443         }
1444         va_end(args);
1445
1446         if (rc == 0 && nleds > 0) {
1447                 ev[nleds].type = EV_SYN;
1448                 ev[nleds++].code = SYN_REPORT;
1449
1450                 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1451                 if (rc > 0) {
1452                         nleds--; /* last is EV_SYN */
1453                         while (nleds--)
1454                                 update_led_state(dev, &ev[nleds]);
1455                 }
1456                 rc = (rc != -1) ? 0 : -errno;
1457         }
1458
1459         return rc;
1460 }
1461
1462 LIBEVDEV_EXPORT int
1463 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1464 {
1465         if (!dev->initialized) {
1466                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1467                 return -EBADF;
1468         } else if (dev->fd < 0)
1469                 return -EBADF;
1470
1471         return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;
1472 }