Rename pid/vid to product-id and vendor-id
[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 <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdarg.h>
29
30 #include "libevdev.h"
31 #include "libevdev-int.h"
32
33 #define MAXEVENTS 64
34
35 static inline int
36 bit_is_set(const unsigned long *array, int bit)
37 {
38     return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
39 }
40
41 static inline void
42 set_bit(unsigned long *array, int bit)
43 {
44     array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
45 }
46
47 static inline void
48 clear_bit(unsigned long *array, int bit)
49 {
50     array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS));
51 }
52
53 static inline void
54 set_bit_state(unsigned long *array, int bit, int state)
55 {
56         if (state)
57                 set_bit(array, bit);
58         else
59                 clear_bit(array, bit);
60 }
61
62 static unsigned int
63 type_to_mask_const(const struct libevdev *dev, unsigned int type, const unsigned long **mask)
64 {
65         unsigned int max;
66
67         switch(type) {
68                 case EV_ABS:
69                         *mask = dev->abs_bits;
70                         max = ABS_MAX;
71                         break;
72                 case EV_REL:
73                         *mask = dev->rel_bits;
74                         max = REL_MAX;
75                         break;
76                 case EV_KEY:
77                         *mask = dev->key_bits;
78                         max = KEY_MAX;
79                         break;
80                 case EV_LED:
81                         *mask = dev->led_bits;
82                         max = LED_MAX;
83                         break;
84                 default:
85                      return 0;
86         }
87
88         return max;
89 }
90
91 static unsigned int
92 type_to_mask(struct libevdev *dev, unsigned int type, unsigned long **mask)
93 {
94         unsigned int max;
95
96         switch(type) {
97                 case EV_ABS:
98                         *mask = dev->abs_bits;
99                         max = ABS_MAX;
100                         break;
101                 case EV_REL:
102                         *mask = dev->rel_bits;
103                         max = REL_MAX;
104                         break;
105                 case EV_KEY:
106                         *mask = dev->key_bits;
107                         max = KEY_MAX;
108                         break;
109                 case EV_LED:
110                         *mask = dev->led_bits;
111                         max = LED_MAX;
112                         break;
113                 default:
114                      return 0;
115         }
116
117         return max;
118 }
119
120 static int
121 init_event_queue(struct libevdev *dev)
122 {
123         /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
124            EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
125
126         const int QUEUE_SIZE = 256;
127
128         return queue_alloc(dev, QUEUE_SIZE);
129 }
130
131 static void
132 _libevdev_log(struct libevdev *dev, const char *format, ...)
133 {
134         va_list args;
135
136         va_start(args, format);
137         dev->log(format, args);
138         va_end(args);
139 }
140
141 static void
142 libevdev_noop_log_func(const char *format, va_list args)
143 {
144 }
145
146 struct libevdev*
147 libevdev_new(void)
148 {
149         struct libevdev *dev;
150
151         dev = calloc(1, sizeof(*dev));
152         if (!dev)
153                 return NULL;
154         dev->fd = -1;
155         dev->num_slots = -1;
156         dev->current_slot = -1;
157         dev->log = libevdev_noop_log_func;
158
159         return dev;
160 }
161
162 int
163 libevdev_new_from_fd(int fd, struct libevdev **dev)
164 {
165         struct libevdev *d;
166         int rc;
167
168         d = libevdev_new();
169         if (!d)
170                 return -ENOSPC;
171
172         rc = libevdev_set_fd(d, fd);
173         if (rc < 0)
174                 libevdev_free(d);
175         else
176                 *dev = d;
177         return rc;
178 }
179
180 void
181 libevdev_free(struct libevdev *dev)
182 {
183         queue_free(dev);
184         free(dev);
185 }
186
187 void
188 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
189 {
190         dev->log = logfunc ? logfunc : libevdev_noop_log_func;
191 }
192
193 int
194 libevdev_change_fd(struct libevdev *dev, int fd)
195 {
196         if (dev->fd == -1)
197                 return -1;
198         dev->fd = fd;
199         return 0;
200 }
201
202 int
203 libevdev_set_fd(struct libevdev* dev, int fd)
204 {
205         int rc;
206         int i;
207
208         if (dev->fd != -1)
209                 return -EBADF;
210
211         rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
212         if (rc < 0)
213                 goto out;
214
215         rc = ioctl(fd, EVIOCGNAME(sizeof(dev->name) - 1), dev->name);
216         if (rc < 0)
217                 goto out;
218
219         rc = ioctl(fd, EVIOCGID, &dev->ids);
220         if (rc < 0)
221                 goto out;
222
223         rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
224         if (rc < 0)
225                 goto out;
226
227         rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
228         if (rc < 0)
229                 goto out;
230
231         rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
232         if (rc < 0)
233                 goto out;
234
235         rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
236         if (rc < 0)
237                 goto out;
238
239         rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
240         if (rc < 0)
241                 goto out;
242
243         for (i = ABS_X; i <= ABS_MAX; i++) {
244                 if (bit_is_set(dev->abs_bits, i)) {
245                         struct input_absinfo abs_info;
246                         rc = ioctl(fd, EVIOCGABS(i), &dev->abs_info[i]);
247                         if (rc < 0)
248                                 goto out;
249
250                         if (i == ABS_MT_SLOT) {
251                                 dev->num_slots = abs_info.maximum + 1; /* FIXME: non-zero min? */
252                                 dev->current_slot = abs_info.value;
253                         }
254
255                 }
256         }
257
258         rc = init_event_queue(dev);
259         if (rc < 0)
260                 return -rc;
261
262         /* not copying key state because we won't know when we'll start to
263          * use this fd and key's are likely to change state by then.
264          * Same with the valuators, really, but they may not change.
265          */
266
267         dev->fd = fd;
268
269 out:
270         return rc ? -errno : 0;
271 }
272
273 int
274 libevdev_get_fd(const struct libevdev* dev)
275 {
276         return dev->fd;
277 }
278
279 static inline void
280 init_event(struct input_event *ev, int type, int code, int value)
281 {
282         ev->time.tv_sec = 0; /* FIXME: blah! */
283         ev->time.tv_usec = 0; /* FIXME: blah! */
284         ev->type = type;
285         ev->code = code;
286         ev->value = value;
287 }
288
289 static int
290 sync_key_state(struct libevdev *dev)
291 {
292         int rc;
293         int i;
294         unsigned long keystate[NLONGS(KEY_MAX)];
295
296         rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
297         if (rc < 0)
298                 goto out;
299
300         for (i = 0; i < KEY_MAX; i++) {
301                 int old, new;
302                 old = bit_is_set(dev->key_values, i);
303                 new = bit_is_set(keystate, i);
304                 if (old ^ new) {
305                         struct input_event *ev = queue_push(dev);
306                         init_event(ev, EV_KEY, i, new ? 1 : 0);
307                 }
308                 set_bit_state(dev->key_values, i, new);
309         }
310
311         rc = 0;
312 out:
313         return rc ? -errno : 0;
314 }
315
316 static int
317 sync_abs_state(struct libevdev *dev)
318 {
319         int rc;
320         int i;
321
322         for (i = ABS_X; i <= ABS_MAX; i++) {
323                 struct input_absinfo abs_info;
324
325                 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
326                         continue;
327
328                 if (!bit_is_set(dev->abs_bits, i))
329                         continue;
330
331                 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
332                 if (rc < 0)
333                         goto out;
334
335                 if (dev->abs_info[i].value != abs_info.value) {
336                         struct input_event *ev = queue_push(dev);
337
338                         init_event(ev, EV_ABS, i, abs_info.value);
339                         dev->abs_info[i].value = abs_info.value;
340                 }
341         }
342
343         rc = 0;
344 out:
345         return rc ? -errno : 0;
346 }
347
348 static int
349 sync_mt_state(struct libevdev *dev)
350 {
351         int rc;
352         int i;
353         struct mt_state {
354                 int code;
355                 int val[MAX_SLOTS];
356         } mt_state[ABS_MT_CNT];
357
358         for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
359                 int idx;
360                 if (i == ABS_MT_SLOT)
361                         continue;
362
363                 idx = i - ABS_MT_MIN;
364                 mt_state[idx].code = i;
365                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
366                 if (rc < 0)
367                         goto out;
368         }
369
370         for (i = 0; i < dev->num_slots; i++) {
371                 int j;
372                 struct input_event *ev;
373
374                 ev = queue_push(dev);
375                 init_event(ev, EV_ABS, ABS_MT_SLOT, i);
376                 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
377                         int jdx = j - ABS_MT_MIN;
378
379                         if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
380                                 continue;
381
382                         ev = queue_push(dev);
383                         init_event(ev, EV_ABS, j, mt_state[jdx].val[i]);
384                         dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
385                 }
386         }
387
388         rc = 0;
389 out:
390         return rc ? -errno : 0;
391 }
392
393 static int
394 sync_state(struct libevdev *dev)
395 {
396         int i;
397         int rc = 0;
398         struct input_event *ev;
399
400         /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
401            queue[0]) we need to shift this backwards. Except that chances are that the
402            queue may be either full or too full to prepend all the events needed for
403            syncing.
404
405            so we search for the last sync event in the queue and drop everything before
406            including that event and rely on the kernel to tell us the right value for that
407            bitfield during the sync process.
408          */
409
410         for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
411                 struct input_event e;
412                 queue_peek(dev, i, &e);
413                 if (e.type == EV_SYN)
414                         break;
415         }
416
417         if (i > 0)
418                 queue_shift_multiple(dev, i + 1, NULL);
419
420         if (libevdev_has_event_type(dev, EV_KEY))
421                 rc = sync_key_state(dev);
422         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
423                 rc = sync_abs_state(dev);
424         if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
425                 rc = sync_mt_state(dev);
426
427         ev = queue_push(dev);
428         init_event(ev, EV_SYN, SYN_REPORT, 0);
429
430         dev->queue_nsync = queue_num_elements(dev);
431         dev->need_sync = 0;
432
433         return rc;
434 }
435
436 static int
437 update_key_state(struct libevdev *dev, const struct input_event *e)
438 {
439         if (!libevdev_has_event_type(dev, EV_KEY))
440                 return 1;
441
442         if (e->code > KEY_MAX)
443                 return 1;
444
445         if (e->value == 0)
446                 clear_bit(dev->key_values, e->code);
447         else
448                 set_bit(dev->key_values, e->code);
449
450         return 0;
451 }
452
453 static int
454 update_mt_state(struct libevdev *dev, const struct input_event *e)
455 {
456         if (e->code == ABS_MT_SLOT) {
457                 dev->current_slot = e->value;
458                 return 0;
459         } else if (dev->current_slot == -1)
460                 return 1;
461
462         dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
463
464         return 0;
465 }
466
467 static int
468 update_abs_state(struct libevdev *dev, const struct input_event *e)
469 {
470         if (!libevdev_has_event_type(dev, EV_ABS))
471                 return 1;
472
473         if (e->code > ABS_MAX)
474                 return 1;
475
476         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
477                 return update_mt_state(dev, e);
478
479         dev->abs_info[e->code].value = e->value;
480
481         return 0;
482 }
483
484 static int
485 update_state(struct libevdev *dev, const struct input_event *e)
486 {
487         int rc = 0;
488
489         switch(e->type) {
490                 case EV_SYN:
491                 case EV_REL:
492                         break;
493                 case EV_KEY:
494                         rc = update_key_state(dev, e);
495                         break;
496                 case EV_ABS:
497                         rc = update_abs_state(dev, e);
498                         break;
499         }
500
501         return rc;
502 }
503
504 static int
505 read_more_events(struct libevdev *dev)
506 {
507         int free_elem;
508         int len;
509         struct input_event *next;
510
511         free_elem = queue_num_free_elements(dev);
512         if (free_elem <= 0)
513                 return 0;
514
515         next = queue_next_element(dev);
516         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
517         if (len < 0) {
518                 return -errno;
519         } else if (len > 0 && len % sizeof(struct input_event) != 0)
520                 return -EINVAL;
521         else if (len > 0) {
522                 int nev = len/sizeof(struct input_event);
523                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
524         }
525
526         return 0;
527 }
528
529 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
530 {
531         int rc = 0;
532
533         if (dev->fd < 0)
534                 return -ENODEV;
535
536         if (flags & LIBEVDEV_READ_SYNC) {
537                 if (!dev->need_sync && dev->queue_nsync == 0)
538                         return -EAGAIN;
539                 else if (dev->need_sync) {
540                         rc = sync_state(dev);
541                         if (rc != 0)
542                                 return rc;
543                 }
544         } else if (dev->need_sync) {
545                 /* FIXME: still need to call update_state for all events
546                  * here, otherwise the library has the wrong view of the
547                  * device too */
548                 queue_shift_multiple(dev, dev->queue_nsync, NULL);
549         }
550
551         /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
552          * event in the queue from the previous read.
553          */
554
555         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
556            worst case we don't read fast enough and end up with SYN_DROPPED anyway */
557         rc = read_more_events(dev);
558         if (rc < 0 && rc != -EAGAIN)
559                 goto out;
560
561         if (queue_shift(dev, ev) != 0)
562                 return -EAGAIN;
563
564         update_state(dev, ev);
565
566         rc = 0;
567         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
568                 dev->need_sync = 1;
569                 rc = 1;
570         }
571
572         if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
573                 dev->queue_nsync--;
574                 rc = 1;
575         }
576
577 out:
578         return rc;
579 }
580
581 const char *
582 libevdev_get_name(const struct libevdev *dev)
583 {
584         return dev->name;
585 }
586
587 int libevdev_get_product_id(const struct libevdev *dev)
588 {
589         return dev->ids.product;
590 }
591
592 int libevdev_get_vendor_id(const struct libevdev *dev)
593 {
594         return dev->ids.vendor;
595 }
596
597 int libevdev_get_bustype(const struct libevdev *dev)
598 {
599         return dev->ids.bustype;
600 }
601
602 int
603 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
604 {
605         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
606 }
607
608 int
609 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
610 {
611         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
612 }
613
614 int
615 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
616 {
617         const unsigned long *mask;
618         unsigned int max;
619
620         if (!libevdev_has_event_type(dev, type))
621                 return 0;
622
623         if (type == EV_SYN)
624                 return 1;
625
626         max = type_to_mask_const(dev, type, &mask);
627
628         if (code > max)
629                 return 0;
630
631         return bit_is_set(mask, code);
632 }
633
634 int
635 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
636 {
637         int value;
638
639         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
640                 return 0;
641
642         switch (type) {
643                 case EV_ABS: value = dev->abs_info[code].value; break;
644                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
645                 default:
646                         value = 0;
647                         break;
648         }
649
650         return value;
651 }
652
653 int
654 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
655 {
656         if (libevdev_has_event_type(dev, type) &&
657             libevdev_has_event_code(dev, type, code)) {
658                 *value = libevdev_get_event_value(dev, type, code);
659                 return 1;
660         } else
661                 return 0;
662 }
663
664 int
665 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
666 {
667         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
668                 return 0;
669
670         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
671                 return 0;
672
673         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
674                 return 0;
675
676         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
677 }
678
679 int
680 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
681 {
682         if (libevdev_has_event_type(dev, EV_ABS) &&
683             libevdev_has_event_code(dev, EV_ABS, code) &&
684             slot < dev->num_slots && slot < MAX_SLOTS) {
685                 *value = libevdev_get_slot_value(dev, slot, code);
686                 return 1;
687         } else
688                 return 0;
689 }
690
691 int
692 libevdev_get_num_slots(const struct libevdev *dev)
693 {
694         return dev->num_slots;
695 }
696
697 int
698 libevdev_get_current_slot(const struct libevdev *dev)
699 {
700         return dev->current_slot;
701 }
702
703 const struct input_absinfo*
704 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
705 {
706         if (!libevdev_has_event_type(dev, EV_ABS) ||
707             !libevdev_has_event_code(dev, EV_ABS, code))
708                 return NULL;
709
710         return &dev->abs_info[code];
711 }
712
713 int
714 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
715 {
716         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
717
718         return absinfo ? absinfo->minimum : 0;
719 }
720
721 int
722 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
723 {
724         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
725
726         return absinfo ? absinfo->maximum : 0;
727 }
728
729 int
730 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
731 {
732         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
733
734         return absinfo ? absinfo->fuzz : 0;
735 }
736
737 int
738 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
739 {
740         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
741
742         return absinfo ? absinfo->flat : 0;
743 }
744
745 int
746 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
747 {
748         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
749
750         return absinfo ? absinfo->resolution : 0;
751 }
752
753 int
754 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
755 {
756         if (type > EV_MAX)
757                 return 1;
758
759         set_bit(dev->bits, type);
760
761         /* FIXME: pass through to kernel */
762
763         return 0;
764 }
765
766 int
767 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
768 {
769         if (type > EV_MAX)
770                 return 1;
771
772         clear_bit(dev->bits, type);
773
774         /* FIXME: pass through to kernel */
775
776         return 0;
777 }
778
779 int
780 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
781                            unsigned int code, const void *data)
782 {
783         unsigned int max;
784         unsigned long *mask;
785
786         if (libevdev_enable_event_type(dev, type))
787                 return 1;
788
789         max = type_to_mask(dev, type, &mask);
790
791         if (code > max)
792                 return 1;
793
794         set_bit(mask, code);
795
796         if (type == EV_ABS) {
797                 const struct input_absinfo *abs = data;
798                 dev->abs_info[code] = *abs;
799         }
800
801         /* FIXME: pass through to kernel */
802
803         return 0;
804 }
805
806 int
807 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
808 {
809         unsigned int max;
810         unsigned long *mask;
811
812         if (type > EV_MAX)
813                 return 1;
814
815         max = type_to_mask(dev, type, &mask);
816
817         if (code > max)
818                 return 1;
819
820         clear_bit(mask, code);
821
822         /* FIXME: pass through to kernel */
823
824         return 0;
825 }
826
827 int
828 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
829 {
830         int rc;
831
832         if (code > ABS_MAX)
833                 return -EINVAL;
834
835         rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
836         if (rc < 0)
837                 rc = -errno;
838         else
839                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
840
841         return rc;
842 }
843
844 int
845 libevdev_grab(struct libevdev *dev, int grab)
846 {
847         int rc = 0;
848
849         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
850                 return -EINVAL;
851
852         if (grab == dev->grabbed)
853                 return 0;
854
855         if (grab == LIBEVDEV_GRAB)
856                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
857         else if (grab == LIBEVDEV_UNGRAB)
858                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
859
860         if (rc == 0)
861                 dev->grabbed = grab;
862
863         return rc < 0 ? -errno : 0;
864 }