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