2 * Copyright © 2013 Red Hat, Inc.
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.
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
30 #include <linux/input.h>
36 * **libevdev** is a library for handling evdev kernel devices. It abstracts
37 * the ioctls through type-safe interfaces and provides functions to change
38 * the appearance of the device.
40 * Development of libevdev is discussed on
41 * [input-tools@lists.freedesktop.org](http://lists.freedesktop.org/mailman/listinfo/input-tools)
42 * Please submit patches, questions or general comments there.
44 * Handling events and SYN_DROPPED
45 * ===============================
47 * libevdev provides an interface for handling events, including most notably
48 * SYN_DROPPED events. SYN_DROPPED events are sent by the kernel when the
49 * process does not read events fast enough and the kernel is forced to drop
50 * some events. This causes the device to get out of sync with the process'
51 * view of it. libevdev handles this by telling the caller that a SYN_DROPPED
52 * has been received and that the state of the device is different to what is
53 * to be expected. It then provides the delta between the previous state and
54 * the actual state of the device as a set of events. See
55 * libevdev_next_event() for more information.
60 * libevdev is signal-safe for the majority of its operations. Check the API
61 * documentation to make sure, unless explicitly stated a call is <b>not</b>
67 * libevdev does not attempt duplicate detection. Initializing two libevdev
68 * devices for the same fd is valid and behaves the same as for two different
71 * libevdev does not handle the file descriptors directly, it merely uses
72 * them. The caller is responsible for opening the file descriptors, setting
73 * them to O_NONBLOCK and handling permissions.
75 * Where does libevdev sit?
76 * ========================
78 * libevdev is essentially a `read(2)` on steroids for `/dev/input/eventX
79 * devices. It sits below the process that handles input events, in between
80 * the kernel and that process. In the simplest case, e.g. an evtest-like tool
81 * the stack would look like this:
83 * kernel → libevdev → evtest
85 * For X.Org input modules, the stack would look like this:
87 * kernel → libevdev → xf86-input-evdev → X server → X client
89 * For Weston/Wayland, the stack would look like this:
91 * kernel → libevdev → Weston → Wayland client
93 * libevdev does **not** have knowledge of X clients or Wayland clients, it is
94 * too low in the stack.
98 * Below is a simple example that shows how libevdev could be used. This example
99 * opens a device, checks for relative axes and a left mouse button and if it
100 * finds them monitors the device to print the event.
103 * struct libevdev *dev = NULL;
107 * fd = open("/dev/input/event0", O_RDONLY|O_NONBLOCK);
108 * rc = libevdev_new_from_fd(fd, &dev);
110 * fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
113 * printf("Input device name: \"%s\"\n", libevdev_get_name(dev));
114 * printf("Input device ID: bus %#x vendor %#x product %#x\n",
115 * libevdev_get_bustype(dev),
116 * libevdev_get_vendor_id(dev),
117 * libevdev_get_product_id(dev));
118 * if (!libevdev_has_event_type(dev, EV_REL) ||
119 * !libevdev_has_event_code(dev, EV_KEY, BTN_LEFT)) {
120 * printf("This device does not look like a mouse\n");
125 * struct input_event ev;
126 * rc = libevdev_next_event(dev, LIBEVDEV_READ_NORMAL, &ev);
128 * printf("Event: %s %s %d\n",
129 * libevdev_get_event_type_name(ev.type),
130 * libevdev_get_event_code_name(ev.type, ev.code),
132 * } while (rc == 1 || rc == 0 || rc == -EAGAIN);
135 * A more complete example is available with the libevdev-events tool here:
136 * http://cgit.freedesktop.org/libevdev/tree/tools/libevdev-events.c
138 * libevdev internal test suite
139 * ============================
141 * libevdev's internal test suite uses the
142 * [Check unit testing framework](http://check.sourceforge.net/). Tests are
143 * divided into test suites and test cases. Most tests create a uinput device,
144 * so you'll need to run as root.
146 * To run a specific suite only:
148 * export CK_RUN_SUITE="suite name"
150 * To run a specific test case only:
152 * export CK_RUN_TEST="test case name"
154 * To get a list of all suites or tests:
156 * git grep "suite_create"
157 * git grep "tcase_create"
159 * By default, check forks, making debugging harder. Run gdb as below to avoid
162 * sudo CK_FORK=no CK_RUN_TEST="test case name" gdb ./test/test-libevdev
164 * A special target `make gcov-report.txt` exists that runs gcov and leaves a
165 * `libevdev.c.gcov` file. Check that for test coverage.
167 * `make check` is hooked up to run the test and gcov (again, needs root).
169 * The test suite creates a lot of devices, very quickly. Add the following
170 * xorg.conf.d snippet to avoid the devices being added as X devices (at the
171 * time of writing, mutter can't handle these devices and exits after getting
172 * a BadDevice error).
174 * $ cat /etc/X11/xorg.conf.d/99-ignore-libevdev-devices.conf
175 * Section "InputClass"
176 * Identifier "Ignore libevdev test devices"
177 * MatchProduct "libevdev test device"
178 * Option "Ignore" "on"
181 * License information
182 * ===================
183 * libevdev is licensed under the
184 * [X11 license](http://cgit.freedesktop.org/libevdev/tree/COPYING).
188 * Please report bugs in the freedesktop.org bugzilla under the libevdev product:
189 * https://bugs.freedesktop.org/enter_bug.cgi?product=libevdev
193 * @defgroup init Initialization and setup
195 * Initialization, initial setup and file descriptor handling.
196 * These functions are the main entry points for users of libevdev, usually a
197 * caller will use this series of calls:
200 * struct libevdev *dev;
203 * dev = libevdev_new();
207 * err = libevdev_set_fd(dev, fd);
209 * printf("Failed (errno %d): %s\n", -err, strerror(-err));
211 * libevdev_free(dev);
214 * libevdev_set_fd() is the central call and initializes the internal structs
215 * for the device at the given fd. libevdev functions will fail if called
216 * before libevdev_set_fd() unless documented otherwise.
220 * @defgroup bits Querying device capabilities
222 * Abstraction functions to handle device capabilities, specificially
223 * device propeties such as the name of the device and the bits
224 * representing the events suppported by this device.
226 * The logical state returned may lag behind the physical state of the device.
227 * libevdev queries the device state on libevdev_set_fd() and then relies on
228 * the caller to parse events through libevdev_next_fd(). If a caller does not
229 * use libevdev_next_fd(), libevdev will not update the internal state of the
230 * device and thus returns outdated values.
234 * @defgroup mt Multi-touch related functions
235 * Functions for querying multi-touch-related capabilities. MT devices
236 * following the kernel protocol B (using ABS_MT_SLOT) provide multiple touch
237 * points through so-called slots on the same axis. The slots are enumerated,
238 * a client reading from the device will first get an ABS_MT_SLOT event, then
239 * the values of axes changed in this slot. Multiple slots may be provided in
240 * before an EV_SYN event.
242 * As with @ref bits, the logical state of the device as seen by the library
243 * depends on the caller using libevdev_next_event().
247 * @defgroup kernel Modifying the appearance or capabilities of the device
249 * Modifying the set of events reported by this device. By default, the
250 * libevdev device mirrors the kernel device, enabling only those bits
251 * exported by the kernel. This set of functions enable or disable bits as
252 * seen from the caller.
254 * Enabling an event type or code does not affect event reporting - a
255 * software-enabled event will not be generated by the physical hardware.
256 * Disabling an event will prevent libevdev from routing such events to the
257 * caller. Enabling and disabling event types and codes is at the library
258 * level and thus only affects the caller.
260 * If an event type or code is enabled at kernel-level, future users of this
261 * device will see this event enabled. Currently there is no option of
262 * disabling an event type or code at kernel-level.
266 * @defgroup misc Miscellaneous helper functions
268 * Functions for printing or querying event ranges. The list of names is
269 * compiled into libevdev and will not change when the kernel changes. Adding
270 * or removing names requires a re-compilation of libevdev. Likewise, the max
271 * for each event type is compiled in and does not check the underlying
276 * @defgroup events Event handling
278 * Functions to handle events and fetch the current state of the event. Generally,
279 * libevdev updates its internal state as the event is processed and forwarded
280 * to the caller. Thus, the libevdev state of the device should always be identical
281 * to the caller's state. It may however lag behind the actual state of the device.
287 * Opaque struct representing an evdev device.
291 enum libevdev_read_flag {
292 LIBEVDEV_READ_SYNC = 1, /**< Process data in sync mode */
293 LIBEVDEV_READ_NORMAL = 2, /**< Process data in normal mode */
294 LIBEVDEV_FORCE_SYNC = 4, /**< Pretend the next event is a SYN_DROPPED. There is
295 no reason to ever use this except for
296 automated tests, so don't. */
297 LIBEVDEV_READ_BLOCKING = 8, /**< The fd is not in O_NONBLOCK and a read may block */
303 * Initialize a new libevdev device. This function only allocates the
304 * required memory and initializes the struct to sane default values.
305 * To actually hook up the device to a kernel device, use
308 * Memory allocated through libevdev_new() must be released by the
309 * caller with libevdev_free().
311 * @see libevdev_set_fd
314 struct libevdev* libevdev_new(void);
319 * Initialize a new libevdev device from the given fd.
321 * This is a shortcut for
325 * struct libevdev *dev = libevdev_new();
326 * err = libevdev_set_fd(dev, fd);
329 * @param fd A file descriptor to the device in O_RDWR or O_RDONLY mode.
330 * @param[out] dev The newly initialized evdev device.
332 * @return On success, zero is returned and dev is set to the newly
333 * allocated struct. On failure, a negative errno is returned and the value
334 * of dev is undefined.
338 int libevdev_new_from_fd(int fd, struct libevdev **dev);
343 * Clean up and free the libevdev struct. After completion, the <code>struct
344 * libevdev</code> is invalid and must not be used.
346 * @param dev The evdev device
348 * @note This function may be called before libevdev_set_fd().
350 void libevdev_free(struct libevdev *dev);
353 * Logging function called by library-internal logging.
354 * This function is expected to treat it's input like printf would.
356 * @param format printf-style format string
357 * @param args List of arguments
359 * @see libevdev_set_log_handler
361 typedef void (*libevdev_log_func_t)(const char *format, va_list args);
364 * Set a printf-style logging handler for library-internal logging. The default
365 * logging function is a noop.
367 * @param dev The evdev device
368 * @param logfunc The logging function for this device. If NULL, the current
369 * logging function is unset.
371 * @note This function may be called before libevdev_set_fd().
373 void libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc);
376 enum libevdev_grab_mode {
382 * Grab or ungrab the device through a kernel EVIOCGRAB. This prevents other
383 * clients (including kernel-internal ones such as rfkill) from receiving
384 * events from this device.
386 * This is generally a bad idea. Don't do this.
388 * Grabbing an already grabbed device, or ungrabbing an ungrabbed device is
389 * a noop and always succeeds.
391 * @param dev The evdev device, already initialized with libevdev_set_fd()
392 * @param grab If true, grab the device. Otherwise ungrab the device.
394 * @return 0 if the device was successfull grabbed or ungrabbed, or a
395 * negative errno in case of failure.
397 int libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab);
402 * Set the fd for this struct and initialize internal data.
403 * The fd must be in O_RDONLY or O_RDWR mode.
405 * This function may only be called once per device. If the device changed and
406 * you need to re-read a device, use libevdev_free() and libevdev_new(). If
407 * you need to change the fd after closing and re-opening the same device, use
408 * libevdev_change_fd().
410 * Unless otherwise specified, libevdev function behavior is undefined until
411 * a successfull call to libevdev_set_fd().
413 * @param dev The evdev device
414 * @param fd The file descriptor for the device
416 * @return 0 on success, or a negative error code on failure
418 * @see libevdev_change_fd
422 int libevdev_set_fd(struct libevdev* dev, int fd);
427 * Change the fd for this device, without re-reading the actual device. If the fd
428 * changes after initializing the device, for example after a VT-switch in the
429 * X.org X server, this function updates the internal fd to the newly opened.
430 * No check is made that new fd points to the same device. If the device has
431 * changed, libevdev's behavior is undefined.
433 * The fd may be open in O_RDONLY or O_RDWR.
435 * It is an error to call this function before calling libevdev_set_fd().
437 * @param dev The evdev device, already initialized with libevdev_set_fd()
438 * @param fd The new fd
440 * @return 0 on success, or -1 on failure.
442 * @see libevdev_set_fd
444 int libevdev_change_fd(struct libevdev* dev, int fd);
447 * @param dev The evdev device
449 * @return The previously set fd, or -1 if none had been set previously.
450 * @note This function may be called before libevdev_set_fd().
452 int libevdev_get_fd(const struct libevdev* dev);
457 * Get the next event from the device. This function operates in two different
458 * modes: normal mode or sync mode.
460 * In normal mode, this function returns 0 and returns the event in the
461 * parameter ev. If no events are available at this time, it returns -EAGAIN
462 * and ev is undefined.
464 * If a SYN_DROPPED is read from the device, this function returns 1. The
465 * caller should now call this function with the LIBEVDEV_READ_SYNC flag set,
466 * to get the set of events that make up the device state delta. This function
467 * returns 1 for each event part of that delta, until it returns -EAGAIN once
468 * all events have been synced.
470 * If a device needs to be synced by the caller but the caller does not call
471 * with the LIBEVDEV_READ_SYNC flag set, all events from the diff are dropped
472 * and event processing continues as normal.
474 * @param dev The evdev device, already initialized with libevdev_set_fd()
475 * @param flags Set of flags to determine behaviour. If LIBEVDEV_READ_NORMAL
476 * is set, the next event is read in normal mode. If LIBEVDEV_READ_SYNC is
477 * set, the next event is read in sync mode.
478 * @param ev On success, set to the current event.
479 * @return On failure, a negative errno is returned.
480 * @retval 0 One or more events where read of the device
481 * @retval -EAGAIN No events are currently available on the device
482 * @retval 1 A SYN_DROPPED event was received, or a synced event was
485 * @note This function is signal-safe.
487 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev);
492 * Check if there are events waiting for us. This does not read an event off
493 * the fd and may not access the fd at all. If there are events queued
494 * internally this function will return non-zero. If the internal queue is empty,
495 * this function will poll the file descriptor for data.
497 * This is a convenience function for simple processes, most complex programs
498 * are expected to use select(2) or poll(2) on the file descriptor. The kernel
499 * guarantees that if data is available, it is a multiple of sizeof(struct
500 * input_event), and thus calling libevdev_next_event() when select(2) or
501 * poll(2) return is safe. You do not need libevdev_has_event_pending() if
502 * you're using select(2) or poll(2).
504 * @param dev The evdev device, already initialized with libevdev_set_fd()
505 * @return On failure, a negative errno is returned.
506 * @retval 0 No event is currently available
507 * @retval 1 One or more events are available on the fd
509 * @note This function is signal-safe.
511 int libevdev_has_event_pending(struct libevdev *dev);
516 * @param dev The evdev device, already initialized with libevdev_set_fd()
518 * @return The device name as read off the kernel device. The name is never
519 * NULL but it may be the empty string.
521 * @note This function is signal-safe.
523 const char* libevdev_get_name(const struct libevdev *dev);
528 * @param dev The evdev device
529 * @param name The new, non-NULL, name to assign to this device.
531 * @note This function may be called before libevdev_set_fd(). A call to
532 * libevdev_set_fd() will overwrite any previously set value.
534 void libevdev_set_name(struct libevdev *dev, const char *name);
539 * Virtual devices such as uinput devices have no phys location.
541 * @param dev The evdev device, already initialized with libevdev_set_fd()
543 * @return The physical location of this device, or NULL if there is none
545 * @note This function is signal safe.
547 const char * libevdev_get_phys(const struct libevdev *dev);
552 * @param dev The evdev device
553 * @param phys The new, non-NULL, phys to assign to this device.
555 * @note This function may be called before libevdev_set_fd(). A call to
556 * libevdev_set_fd() will overwrite any previously set value.
558 void libevdev_set_phys(struct libevdev *dev, const char *phys);
563 * @param dev The evdev device, already initialized with libevdev_set_fd()
565 * @return The unique identifier for this device, or NULL if there is none
567 * @note This function is signal safe.
569 const char * libevdev_get_uniq(const struct libevdev *dev);
574 * @param dev The evdev device
575 * @param uniq The new, non-NULL, uniq to assign to this device.
577 * @note This function may be called before libevdev_set_fd(). A call to
578 * libevdev_set_fd() will overwrite any previously set value.
580 void libevdev_set_uniq(struct libevdev *dev, const char *uniq);
585 * @param dev The evdev device, already initialized with libevdev_set_fd()
587 * @return The device's product ID
589 * @note This function is signal-safe.
591 int libevdev_get_id_product(const struct libevdev *dev);
596 * @param dev The evdev device
597 * @param product_id The product ID to assign to this device
599 * @note This function may be called before libevdev_set_fd(). A call to
600 * libevdev_set_fd() will overwrite any previously set value.
602 void libevdev_set_id_product(struct libevdev *dev, int product_id);
607 * @param dev The evdev device, already initialized with libevdev_set_fd()
609 * @return The device's vendor ID
611 * @note This function is signal-safe.
613 int libevdev_get_id_vendor(const struct libevdev *dev);
618 * @param dev The evdev device
619 * @param vendor_id The vendor ID to assign to this device
621 * @note This function may be called before libevdev_set_fd(). A call to
622 * libevdev_set_fd() will overwrite any previously set value.
624 void libevdev_set_id_vendor(struct libevdev *dev, int vendor_id);
629 * @param dev The evdev device, already initialized with libevdev_set_fd()
631 * @return The device's bus type
633 * @note This function is signal-safe.
635 int libevdev_get_id_bustype(const struct libevdev *dev);
640 * @param dev The evdev device
641 * @param bustype The bustype to assign to this device
643 * @note This function may be called before libevdev_set_fd(). A call to
644 * libevdev_set_fd() will overwrite any previously set value.
646 void libevdev_set_id_bustype(struct libevdev *dev, int bustype);
651 * @param dev The evdev device, already initialized with libevdev_set_fd()
653 * @return The device's firmware version
655 * @note This function is signal-safe.
657 int libevdev_get_id_version(const struct libevdev *dev);
662 * @param dev The evdev device
663 * @param version The version to assign to this device
665 * @note This function may be called before libevdev_set_fd(). A call to
666 * libevdev_set_fd() will overwrite any previously set value.
668 void libevdev_set_id_version(struct libevdev *dev, int version);
673 * @param dev The evdev device, already initialized with libevdev_set_fd()
675 * @return The driver version for this device
677 * @note This function is signal-safe.
679 int libevdev_get_driver_version(const struct libevdev *dev);
684 * @param dev The evdev device, already initialized with libevdev_set_fd()
685 * @param prop The input property to query for, one of INPUT_PROP_...
687 * @return 1 if the device provides this input property, or 0 otherwise.
689 * @note This function is signal-safe
691 int libevdev_has_property(const struct libevdev *dev, unsigned int prop);
696 * @param dev The evdev device
697 * @param prop The input property to enable, one of INPUT_PROP_...
699 * @return 0 on success or -1 on failure
701 * @note This function may be called before libevdev_set_fd(). A call to
702 * libevdev_set_fd() will overwrite any previously set value.
704 int libevdev_enable_property(struct libevdev *dev, unsigned int prop);
709 * @param dev The evdev device, already initialized with libevdev_set_fd()
710 * @param type The event type to query for, one of EV_SYN, EV_REL, etc.
712 * @return 1 if the device supports this event type, or 0 otherwise.
714 * @note This function is signal-safe.
716 int libevdev_has_event_type(const struct libevdev *dev, unsigned int type);
721 * @param dev The evdev device, already initialized with libevdev_set_fd()
722 * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
723 * @param code The event code to query for, one of ABS_X, REL_X, etc.
725 * @return 1 if the device supports this event type and code, or 0 otherwise.
727 * @note This function is signal-safe.
729 int libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code);
734 * Get the minimum axis value for the given axis, as advertised by the kernel.
736 * @param dev The evdev device, already initialized with libevdev_set_fd()
737 * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
739 * @return axis minimum for the given axis or 0 if the axis is invalid
741 int libevdev_get_abs_minimum(const struct libevdev *dev, unsigned int code);
745 * Get the maximum axis value for the given axis, as advertised by the kernel.
747 * @param dev The evdev device, already initialized with libevdev_set_fd()
748 * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
750 * @return axis maximum for the given axis or 0 if the axis is invalid
752 int libevdev_get_abs_maximum(const struct libevdev *dev, unsigned int code);
756 * Get the axis fuzz for the given axis, as advertised by the kernel.
758 * @param dev The evdev device, already initialized with libevdev_set_fd()
759 * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
761 * @return axis fuzz for the given axis or 0 if the axis is invalid
763 int libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code);
767 * Get the axis flat for the given axis, as advertised by the kernel.
769 * @param dev The evdev device, already initialized with libevdev_set_fd()
770 * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
772 * @return axis flat for the given axis or 0 if the axis is invalid
774 int libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code);
778 * Get the axis resolution for the given axis, as advertised by the kernel.
780 * @param dev The evdev device, already initialized with libevdev_set_fd()
781 * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
783 * @return axis resolution for the given axis or 0 if the axis is invalid
785 int libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code);
790 * Get the axis info for the given axis, as advertised by the kernel.
792 * @param dev The evdev device, already initialized with libevdev_set_fd()
793 * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
795 * @return The input_absinfo for the given code, or NULL if the device does
796 * not support this event code.
798 const struct input_absinfo* libevdev_get_abs_info(const struct libevdev *dev, unsigned int code);
803 * Behaviour of this function is undefined if the device does not provide
806 * If the device supports ABS_MT_SLOT, the value returned for any ABS_MT_*
807 * event code is the value of the currently active slot. You should use
808 * libevdev_get_slot_value() instead.
810 * @param dev The evdev device, already initialized with libevdev_set_fd()
811 * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
812 * @param code The event code to query for, one of ABS_X, REL_X, etc.
814 * @return The current value of the event.
816 * @note This function is signal-safe.
817 * @note The value for ABS_MT_ events is undefined, use
818 * libevdev_get_slot_value() instead
820 * @see libevdev_get_slot_value
822 int libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code);
827 * Set the value for a given event type and code. This only makes sense for
828 * some event types, e.g. setting the value for EV_REL is pointless.
830 * This is a local modification only affecting only this representation of
831 * this device. A future call to libevdev_get_event_value() will return this
832 * value, unless the value was overwritten by an event.
834 * If the device supports ABS_MT_SLOT, the value set for any ABS_MT_*
835 * event code is the value of the currently active slot. You should use
836 * libevdev_set_slot_value() instead.
838 * @param dev The evdev device, already initialized with libevdev_set_fd()
839 * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
840 * @param code The event code to set the value for, one of ABS_X, LED_NUML, etc.
841 * @param value The new value to set
843 * @return 0 on success, or -1 on failure.
844 * @retval -1 the device does not have the event type or code enabled, or the code is outside the
845 * allowed limits for the given type, or the type cannot be set.
847 * @see libevdev_set_slot_value
848 * @see libevdev_get_event_value
850 int libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value);
855 * Fetch the current value of the event type. This is a shortcut for
858 * if (libevdev_has_event_type(dev, t) && libevdev_has_event_code(dev, t, c))
859 * val = libevdev_get_event_value(dev, t, c);
862 * @param dev The evdev device, already initialized with libevdev_set_fd()
863 * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
864 * @param code The event code to query for, one of ABS_X, REL_X, etc.
865 * @param[out] value The current value of this axis returned.
867 * @return If the device supports this event type and code, the return value is
868 * non-zero and value is set to the current value of this axis. Otherwise,
869 * zero is returned and value is unmodified.
871 * @note This function is signal-safe.
872 * @note The value for ABS_MT_ events is undefined, use
873 * libevdev_fetch_slot_value() instead
875 * @see libevdev_fetch_slot_value
877 int libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value);
882 * Return the current value of the code for the given slot.
884 * The return value is undefined for a slot exceeding the available slots on
885 * the device, for a code that is not in the permitted ABS_MT range or for a
886 * device that does not have slots.
888 * @param dev The evdev device, already initialized with libevdev_set_fd()
889 * @param slot The numerical slot number, must be smaller than the total number
890 * of slots on this device
891 * @param code The event code to query for, one of ABS_MT_POSITION_X, etc.
893 * @note This function is signal-safe.
894 * @note The value for events other than ABS_MT_ is undefined, use
895 * libevdev_fetch_value() instead
897 * @see libevdev_get_value
899 int libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code);
904 * Set the value for a given code for the given slot.
906 * This is a local modification only affecting only this representation of
907 * this device. A future call to libevdev_get_slot_value() will return this
908 * value, unless the value was overwritten by an event.
910 * This function does not set event values for axes outside the ABS_MT range,
911 * use libevdev_set_event_value() instead.
913 * @param dev The evdev device, already initialized with libevdev_set_fd()
914 * @param slot The numerical slot number, must be smaller than the total number
915 * of slots on this device
916 * @param code The event code to set the value for, one of ABS_MT_POSITION_X, etc.
917 * @param value The new value to set
919 * @return 0 on success, or -1 on failure.
920 * @retval -1 the device does not have the event code enabled, or the code is
921 * outside the allowed limits for multitouch events, or the slot number is outside
922 * the limits for this device, or the device does not support multitouch events.
924 * @see libevdev_set_event_value
925 * @see libevdev_get_slot_value
927 int libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value);
932 * Fetch the current value of the code for the given slot. This is a shortcut for
935 * if (libevdev_has_event_type(dev, EV_ABS) &&
936 * libevdev_has_event_code(dev, EV_ABS, c) &&
937 * slot < device->number_of_slots)
938 * val = libevdev_get_slot_value(dev, slot, c);
941 * @param dev The evdev device, already initialized with libevdev_set_fd()
942 * @param slot The numerical slot number, must be smaller than the total number
943 * of slots on this * device
944 * @param[out] value The current value of this axis returned.
946 * @param code The event code to query for, one of ABS_MT_POSITION_X, etc.
947 * @return If the device supports this event code, the return value is
948 * non-zero and value is set to the current value of this axis. Otherwise, or
949 * if the event code is not an ABS_MT_* event code, zero is returned and value
952 * @note This function is signal-safe.
954 int libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value);
959 * Get the number of slots supported by this device.
961 * @param dev The evdev device, already initialized with libevdev_set_fd()
963 * @return The number of slots supported, or -1 if the device does not provide
966 * @note A device may provide ABS_MT_SLOT but a total number of 0 slots. Hence
967 * the return value of -1 for "device does not provide slots at all"
969 int libevdev_get_num_slots(const struct libevdev *dev);
974 * Get the currently active slot. This may differ from the value
975 * an ioctl may return at this time as events may have been read off the fd
976 * since changing the slot value but those events are still in the buffer
977 * waiting to be processed. The returned value is the value a caller would
978 * see if it were to process events manually one-by-one.
980 * @param dev The evdev device, already initialized with libevdev_set_fd()
982 * @return the currently active slot (logically)
984 int libevdev_get_current_slot(const struct libevdev *dev);
989 * Change the minimum for the given EV_ABS event code, if the code exists.
990 * This function has no effect if libevdev_has_event_code() returns false for
993 void libevdev_set_abs_minimum(struct libevdev *dev, unsigned int code, int min);
998 * Change the maximum for the given EV_ABS event code, if the code exists.
999 * This function has no effect if libevdev_has_event_code() returns false for
1002 void libevdev_set_abs_maximum(struct libevdev *dev, unsigned int code, int max);
1007 * Change the fuzz for the given EV_ABS event code, if the code exists.
1008 * This function has no effect if libevdev_has_event_code() returns false for
1011 void libevdev_set_abs_fuzz(struct libevdev *dev, unsigned int code, int fuzz);
1016 * Change the flat for the given EV_ABS event code, if the code exists.
1017 * This function has no effect if libevdev_has_event_code() returns false for
1020 void libevdev_set_abs_flat(struct libevdev *dev, unsigned int code, int flat);
1025 * Change the resolution for the given EV_ABS event code, if the code exists.
1026 * This function has no effect if libevdev_has_event_code() returns false for
1029 void libevdev_set_abs_resolution(struct libevdev *dev, unsigned int code, int resolution);
1034 * Change the abs info for the given EV_ABS event code, if the code exists.
1035 * This function has no effect if libevdev_has_event_code() returns false for
1038 void libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs);
1043 * Forcibly enable an event type on this device, even if the underlying
1044 * device does not support it. While this cannot make the device actually
1045 * report such events, it will now return true for libevdev_has_event_type().
1047 * This is a local modification only affecting only this representation of
1050 * @param dev The evdev device, already initialized with libevdev_set_fd()
1051 * @param type The event type to enable (EV_ABS, EV_KEY, ...)
1053 * @return 0 on success or -1 otherwise
1055 * @see libevdev_has_event_type
1057 int libevdev_enable_event_type(struct libevdev *dev, unsigned int type);
1062 * Forcibly disable an event type on this device, even if the underlying
1063 * device provides it. This effectively mutes the respective set of
1064 * events. libevdev will filter any events matching this type and none will
1065 * reach the caller. libevdev_has_event_type() will return false for this
1068 * In most cases, a caller likely only wants to disable a single code, not
1069 * the whole type. Use libevdev_disable_event_code() for that.
1071 * Disabling EV_SYN will not work. Don't shoot yourself in the foot.
1074 * This is a local modification only affecting only this representation of
1077 * @param dev The evdev device, already initialized with libevdev_set_fd()
1078 * @param type The event type to disable (EV_ABS, EV_KEY, ...)
1080 * @return 0 on success or -1 otherwise
1082 * @see libevdev_has_event_type
1083 * @see libevdev_disable_event_type
1085 int libevdev_disable_event_type(struct libevdev *dev, unsigned int type);
1090 * Forcibly enable an event type on this device, even if the underlying
1091 * device does not support it. While this cannot make the device actually
1092 * report such events, it will now return true for libevdev_has_event_code().
1094 * The last argument depends on the type and code:
1095 * - If type is EV_ABS, data must be a pointer to a struct input_absinfo
1096 * containing the data for this axis.
1097 * - For all other types, the argument must be NULL.
1099 * This function calls libevdev_enable_event_type() if necessary.
1101 * This is a local modification only affecting only this representation of
1104 * @param dev The evdev device, already initialized with libevdev_set_fd()
1105 * @param type The event type to enable (EV_ABS, EV_KEY, ...)
1106 * @param code The event code to enable (ABS_X, REL_X, etc.)
1107 * @param data If type is EV_ABS, data points to a struct input_absinfo. If type is EV_REP, data
1108 * points to an integer. Otherwise, data must be NULL.
1110 * @return 0 on success or -1 otherwise
1112 * @see libevdev_enable_event_type
1114 int libevdev_enable_event_code(struct libevdev *dev, unsigned int type, unsigned int code, const void *data);
1119 * Forcibly disable an event code on this device, even if the underlying
1120 * device provides it. This effectively mutes the respective set of
1121 * events. libevdev will filter any events matching this type and code and
1122 * none will reach the caller. libevdev_has_event_code() will return false for
1125 * Disabling all event codes for a given type will not disable the event
1126 * type. Use libevdev_disable_event_type() for that.
1128 * This is a local modification only affecting only this representation of
1131 * Disabling EV_SYN will not work. Don't shoot yourself in the foot.
1134 * @param dev The evdev device, already initialized with libevdev_set_fd()
1135 * @param type The event type to disable (EV_ABS, EV_KEY, ...)
1136 * @param code The event code to disable (ABS_X, REL_X, etc.)
1138 * @return 0 on success or -1 otherwise
1140 * @see libevdev_has_event_code
1141 * @see libevdev_disable_event_type
1143 int libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code);
1148 * Set the device's EV_ABS axis to the value defined in the abs
1149 * parameter. This will be written to the kernel.
1151 * @param dev The evdev device, already initialized with libevdev_set_fd()
1152 * @param code The EV_ABS event code to modify, one of ABS_X, ABS_Y, etc.
1153 * @param abs Axis info to set the kernel axis to
1155 * @return zero on success, or a negative errno on failure
1157 * @see libevdev_enable_event_code
1159 int libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs);
1162 enum libevdev_led_value {
1163 LIBEVDEV_LED_ON = 3,
1164 LIBEVDEV_LED_OFF = 4,
1170 * Turn an LED on or off. Convenience function, if you need to modify multiple
1171 * LEDs simultaneously, use libevdev_kernel_set_led_values() instead.
1173 * @note enabling an LED requires write permissions on the device's file descriptor.
1175 * @param dev The evdev device, already initialized with libevdev_set_fd()
1176 * @param code The EV_LED event code to modify, one of LED_NUML, LED_CAPSL, ...
1177 * @param value Specifies whether to turn the LED on or off
1178 * @return zero on success, or a negative errno on failure
1180 int libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value);
1185 * Turn multiple LEDs on or off simultaneously. This function expects a pair
1186 * of LED codes and values to set them to, terminated by a -1. For example, to
1187 * switch the NumLock LED on but the CapsLock LED off, use:
1190 * libevdev_kernel_set_led_values(dev, LED_NUML, LIBEVDEV_LED_ON,
1191 * LED_CAPSL, LIBEVDEV_LED_OFF,
1195 * If any LED code or value is invalid, this function returns -EINVAL and no
1196 * LEDs are modified.
1198 * @note enabling an LED requires write permissions on the device's file descriptor.
1200 * @param dev The evdev device, already initialized with libevdev_set_fd()
1201 * @param ... A pair of LED_* event codes and libevdev_led_value_t, followed by
1202 * -1 to terminate the list.
1203 * @return zero on success, or a negative errno on failure
1205 int libevdev_kernel_set_led_values(struct libevdev *dev, ...);
1210 * Helper function to check if an event is of a specific type. This is
1211 * virtually the same as:
1215 * with the exception that some sanity checks are performed to ensure type
1218 * @note The ranges for types are compiled into libevdev. If the kernel
1219 * changes the max value, libevdev will not automatically pick these up.
1221 * @param ev The input event to check
1222 * @param type Input event type to compare the event against (EV_REL, EV_ABS,
1225 * @return 1 if the event type matches the given type, 0 otherwise (or if
1228 int libevdev_is_event_type(const struct input_event *ev, unsigned int type);
1233 * Helper function to check if an event is of a specific type and code. This
1234 * is virtually the same as:
1236 * ev->type == type && ev->code == code
1238 * with the exception that some sanity checks are performed to ensure type and
1241 * @note The ranges for types and codes are compiled into libevdev. If the kernel
1242 * changes the max value, libevdev will not automatically pick these up.
1244 * @param ev The input event to check
1245 * @param type Input event type to compare the event against (EV_REL, EV_ABS,
1247 * @param code Input event code to compare the event against (ABS_X, REL_X,
1250 * @return 1 if the event type matches the given type and code, 0 otherwise
1251 * (or if type/code are invalid)
1253 int libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code);
1258 * @param type The event type to return the name for.
1260 * @return The name of the given event type (e.g. EV_ABS) or NULL for an
1263 * @note The list of names is compiled into libevdev. If the kernel adds new
1264 * defines for new properties libevdev will not automatically pick these up.
1266 const char * libevdev_get_event_type_name(unsigned int type);
1270 * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
1271 * @param code The event code to return the name for (e.g. ABS_X)
1273 * @return The name of the given event code (e.g. ABS_X) or NULL for an
1274 * invalid type or code
1276 * @note The list of names is compiled into libevdev. If the kernel adds new
1277 * defines for new properties libevdev will not automatically pick these up.
1279 const char * libevdev_get_event_code_name(unsigned int type, unsigned int code);
1284 * @param prop The input prop to return the name for (e.g. INPUT_PROP_BUTTONPAD)
1286 * @return The name of the given input prop (e.g. INPUT_PROP_BUTTONPAD) or NULL for an
1289 * @note The list of names is compiled into libevdev. If the kernel adds new
1290 * defines for new properties libevdev will not automatically pick these up.
1291 * @note On older kernels input properties may not be defined and
1292 * libevdev_get_input_prop_name() will always return NULL
1294 const char* libevdev_get_property_name(unsigned int prop);
1299 * @param type The event type to return the maximum for (EV_ABS, EV_REL, etc.). No max is defined for
1302 * @return The max value defined for the given event type, e.g. ABS_MAX for a type of EV_ABS, or -1
1303 * for an invalid type.
1305 * @note The max value is compiled into libevdev. If the kernel changes the
1306 * max value, libevdev will not automatically pick these up.
1308 int libevdev_get_event_type_max(unsigned int type);
1313 * Get the repeat delay and repeat period values for this device.
1315 * @param dev The evdev device, already initialized with libevdev_set_fd()
1316 * @param delay If not null, set to the repeat delay value
1317 * @param period If not null, set to the repeat period value
1319 * @return 0 on success, -1 if this device does not have repeat settings.
1321 * @note This function is signal-safe
1323 int libevdev_get_repeat(struct libevdev *dev, int *delay, int *period);
1326 /********* DEPRECATED SECTION *********/
1327 #if defined(__GNUC__) && __GNUC__ >= 4
1328 #define LIBEVDEV_DEPRECATED __attribute__ ((deprecated))
1330 #define LIBEVDEV_DEPRECATED
1333 /* replacement: libevdev_kernel_set_abs_info */
1334 int libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs) LIBEVDEV_DEPRECATED;
1336 /**************************************/
1342 #endif /* LIBEVDEV_H */