• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2013 Red Hat, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  */
25 
26 #ifndef LIBEVDEV_H
27 #define LIBEVDEV_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <linux/input.h>
34 #include <stdarg.h>
35 
36 #define LIBEVDEV_ATTRIBUTE_PRINTF(_format, _args) __attribute__ ((format (printf, _format, _args)))
37 
38 /**
39  * @mainpage
40  *
41  * **libevdev** is a library for handling evdev kernel devices. It abstracts
42  * the \ref ioctls through type-safe interfaces and provides functions to change
43  * the appearance of the device.
44  *
45  * Development
46  * ===========
47  * The git repository is available here:
48  *
49  *   - https://gitlab.freedesktop.org/libevdev/libevdev
50  *
51  * Development of libevdev is discussed on
52  * [input-tools@lists.freedesktop.org](http://lists.freedesktop.org/mailman/listinfo/input-tools).
53  * Please submit patches, questions or general comments there.
54  *
55  * Handling events and SYN_DROPPED
56  * ===============================
57  *
58  * libevdev provides an interface for handling events, including most notably
59  * `SYN_DROPPED` events. `SYN_DROPPED` events are sent by the kernel when the
60  * process does not read events fast enough and the kernel is forced to drop
61  * some events. This causes the device to get out of sync with the process'
62  * view of it. libevdev handles this by telling the caller that a * `SYN_DROPPED`
63  * has been received and that the state of the device is different to what is
64  * to be expected. It then provides the delta between the previous state and
65  * the actual state of the device as a set of events. See
66  * libevdev_next_event() and @ref syn_dropped for more information on how
67  * `SYN_DROPPED` is handled.
68  *
69  * Signal safety
70  * =============
71  *
72  * libevdev is signal-safe for the majority of its operations, i.e. many of
73  * its functions are safe to be called from within a signal handler.
74  * Check the API documentation to make sure, unless explicitly stated a call
75  * is <b>not</b> signal safe.
76  *
77  * Device handling
78  * ===============
79  *
80  * A libevdev context is valid for a given file descriptor and its
81  * duration. Closing the file descriptor will not destroy the libevdev device
82  * but libevdev will not be able to read further events.
83  *
84  * libevdev does not attempt duplicate detection. Initializing two libevdev
85  * devices for the same fd is valid and behaves the same as for two different
86  * devices.
87  *
88  * libevdev does not handle the file descriptors directly, it merely uses
89  * them. The caller is responsible for opening the file descriptors, setting
90  * them to `O_NONBLOCK` and handling permissions. A caller should drain any
91  * events pending on the file descriptor before passing it to libevdev.
92  *
93  * Where does libevdev sit?
94  * ========================
95  *
96  * libevdev is essentially a `read(2)` on steroids for `/dev/input/eventX`
97  * devices. It sits below the process that handles input events, in between
98  * the kernel and that process. In the simplest case, e.g. an evtest-like tool
99  * the stack would look like this:
100  *
101  *      kernel → libevdev → evtest
102  *
103  * For X.Org input modules, the stack would look like this:
104  *
105  *      kernel → libevdev → xf86-input-evdev → X server → X client
106  *
107  * For anything using libinput (e.g. most Wayland compositors), the stack
108  * the stack would look like this:
109  *
110  *      kernel → libevdev → libinput → Compositor → Wayland client
111  *
112  * libevdev does **not** have knowledge of X clients or Wayland clients, it is
113  * too low in the stack.
114  *
115  * Example
116  * =======
117  * Below is a simple example that shows how libevdev could be used. This example
118  * opens a device, checks for relative axes and a left mouse button and if it
119  * finds them monitors the device to print the event.
120  *
121  * @code
122  *      struct libevdev *dev = NULL;
123  *      int fd;
124  *      int rc = 1;
125  *
126  *      fd = open("/dev/input/event0", O_RDONLY|O_NONBLOCK);
127  *      rc = libevdev_new_from_fd(fd, &dev);
128  *      if (rc < 0) {
129  *              fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
130  *              exit(1);
131  *      }
132  *      printf("Input device name: \"%s\"\n", libevdev_get_name(dev));
133  *      printf("Input device ID: bus %#x vendor %#x product %#x\n",
134  *             libevdev_get_id_bustype(dev),
135  *             libevdev_get_id_vendor(dev),
136  *             libevdev_get_id_product(dev));
137  *      if (!libevdev_has_event_type(dev, EV_REL) ||
138  *          !libevdev_has_event_code(dev, EV_KEY, BTN_LEFT)) {
139  *              printf("This device does not look like a mouse\n");
140  *              exit(1);
141  *      }
142  *
143  *      do {
144  *              struct input_event ev;
145  *              rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
146  *              if (rc == 0)
147  *                      printf("Event: %s %s %d\n",
148  *                             libevdev_event_type_get_name(ev.type),
149  *                             libevdev_event_code_get_name(ev.type, ev.code),
150  *                             ev.value);
151  *      } while (rc == 1 || rc == 0 || rc == -EAGAIN);
152  * @endcode
153  *
154  * A more complete example is available with the libevdev-events tool here:
155  * https://gitlab.freedesktop.org/libevdev/libevdev/blob/master/tools/libevdev-events.c
156  *
157  * Backwards compatibility with older kernel
158  * =========================================
159  * libevdev attempts to build and run correctly on a number of kernel versions.
160  * If features required are not available, libevdev attempts to work around them
161  * in the most reasonable way. For more details see \ref backwardscompatibility.
162  *
163  * License information
164  * ===================
165  * libevdev is licensed under the
166  * [X11 license](http://cgit.freedesktop.org/libevdev/tree/COPYING).
167  *
168  * Bindings
169  * ===================
170  * - Python: https://gitlab.freedesktop.org/libevdev/python-libevdev
171  * - Haskell: http://hackage.haskell.org/package/evdev
172  * - Rust: https://crates.io/crates/evdev-rs
173  *
174  * Reporting bugs
175  * ==============
176  * Please report bugs in the freedesktop.org GitLab instance:
177  * https://gitlab.freedesktop.org/libevdev/libevdev/issues/
178  */
179 
180 /**
181  * @page syn_dropped SYN_DROPPED handling
182  *
183  * This page describes how libevdev handles `SYN_DROPPED` events.
184  *
185  * Receiving `SYN_DROPPED` events
186  * ==============================
187  *
188  * The kernel sends evdev events separated by an event of type `EV_SYN` and
189  * code `SYN_REPORT`. Such an event marks the end of a frame of hardware
190  * events. The number of events between `SYN_REPORT` events is arbitrary and
191  * depends on the hardware. An example event sequence may look like this:
192  * @code
193  *  EV_ABS   ABS_X        9
194  *  EV_ABS   ABS_Y        8
195  *  EV_SYN   SYN_REPORT   0
196  *  ------------------------
197  *  EV_ABS   ABS_X        10
198  *  EV_ABS   ABS_Y        10
199  *  EV_KEY   BTN_TOUCH    1
200  *  EV_SYN   SYN_REPORT   0
201  *  ------------------------
202  *  EV_ABS   ABS_X        11
203  *  EV_SYN   SYN_REPORT   0
204  * @endcode
205  *
206  * Events are handed to the client buffer as they appear, the kernel adjusts
207  * the buffer size to handle at least one full event. In the normal case,
208  * the client reads the event and the kernel can place the next event in the
209  * buffer. If the client is not fast enough, the kernel places an event of
210  * type `EV_SYN` and code `SYN_DROPPED` into the buffer, effectively notifying
211  * the client that some events were lost. The above example event sequence
212  * may look like this (note the missing/repeated events):
213  * @code
214  *  EV_ABS   ABS_X        9
215  *  EV_ABS   ABS_Y        8
216  *  EV_SYN   SYN_REPORT   0
217  *  ------------------------
218  *  EV_ABS   ABS_X        10
219  *  EV_ABS   ABS_Y        10
220  *  EV_SYN   SYN_DROPPED  0
221  *  EV_ABS   ABS_Y        15
222  *  EV_SYN   SYN_REPORT   0
223  *  ------------------------
224  *  EV_ABS   ABS_X        11
225  *  EV_KEY   BTN_TOUCH    0
226  *  EV_SYN   SYN_REPORT   0
227  * @endcode
228  *
229  * A `SYN_DROPPED` event may be recieved at any time in the event sequence.
230  * When a `SYN_DROPPED` event is received, the client must:
231  * * discard all events since the last `SYN_REPORT`
232  * * discard all events until including the next `SYN_REPORT`
233  * These event are part of incomplete event frames.
234  *
235  * Synchronizing the state of the device
236  * =====================================
237  *
238  * The handling of the device after a `SYN_DROPPED` depends on the available
239  * event codes. For all event codes of type `EV_REL`, no handling is
240  * necessary, there is no state attached. For all event codes of type
241  * `EV_KEY`, `EV_SW`, `EV_LED` and `EV_SND`, the matching @ref ioctls retrieve the
242  * current state. The caller must then compare the last-known state to the
243  * retrieved state and handle the deltas accordingly.
244  * libevdev simplifies this approach: if the state of the device has
245  * changed, libevdev generates an event for each code with the new value and
246  * passes it to the caller during libevdev_next_event() if
247  * @ref LIBEVDEV_READ_FLAG_SYNC is set.
248  *
249  * For events of type `EV_ABS` and an event code less than `ABS_MT_SLOT`, the
250  * handling of state changes is as described above. For events between
251  * `ABS_MT_SLOT` and `ABS_MAX`, the event handling differs.
252  * Slots are the vehicles to transport information for multiple simultaneous
253  * touchpoints on a device. Slots are re-used once a touchpoint has ended.
254  * The kernel sends an `ABS_MT_SLOT` event whenever the current slot
255  * changes; any event in the above axis range applies only to the currently
256  * active slot.
257  * Thus, an event sequence from a slot-capable device may look like this:
258  * @code
259  *  EV_ABS   ABS_MT_POSITION_Y   10
260  *  EV_ABS   ABS_MT_SLOT         1
261  *  EV_ABS   ABS_MT_POSITION_X   100
262  *  EV_ABS   ABS_MT_POSITION_Y   80
263  *  EV_SYN   SYN_REPORT          0
264  * @endcode
265  * Note the lack of `ABS_MT_SLOT`: the first `ABS_MT_POSITION_Y` applies to
266  * a slot opened previously, and is the only axis that changed for that
267  * slot. The touchpoint in slot 1 now has position `100/80`.
268  * The kernel does not provide events if a value does not change, and does
269  * not send `ABS_MT_SLOT` events if the slot does not change, or none of the
270  * values within a slot changes. A client must thus keep the state for each
271  * slot.
272  *
273  * If a `SYN_DROPPED` is received,  the client must sync all slots
274  * individually and update its internal state. libevdev simplifies this by
275  * generating multiple events:
276  * * for each slot on the device, libevdev generates an
277  *   `ABS_MT_SLOT` event with the value set to the slot number
278  * * for each event code between `ABS_MT_SLOT + 1` and `ABS_MAX` that changed
279  *   state for this slot, libevdev generates an event for the new state
280  * * libevdev sends a final `ABS_MT_SLOT` event for the current slot as
281  *   seen by the kernel
282  * * libevdev terminates this sequence with an `EV_SYN SYN_REPORT` event
283  *
284  * An example event sequence for such a sync may look like this:
285  * @code
286  *  EV_ABS   ABS_MT_SLOT         0
287  *  EV_ABS   ABS_MT_POSITION_Y   10
288  *  EV_ABS   ABS_MT_SLOT         1
289  *  EV_ABS   ABS_MT_POSITION_X   100
290  *  EV_ABS   ABS_MT_POSITION_Y   80
291  *  EV_ABS   ABS_MT_SLOT         2
292  *  EV_ABS   ABS_MT_POSITION_Y   8
293  *  EV_ABS   ABS_MT_PRESSURE     12
294  *  EV_ABS   ABS_MT_SLOT         1
295  *  EV_SYN   SYN_REPORT          0
296  * @endcode
297  * Note the terminating `ABS_MT_SLOT` event, this indicates that the kernel
298  * currently has slot 1 active.
299  *
300  * Synchronizing ABS_MT_TRACKING_ID
301  * ================================
302  *
303  * The event code `ABS_MT_TRACKING_ID` is used to denote the start and end of
304  * a touch point within a slot. An `ABS_MT_TRACKING_ID` of zero or greater
305  * denotes the start of a touchpoint, an `ABS_MT_TRACKING_ID` of -1 denotes
306  * the end of a touchpoint within this slot. During `SYN_DROPPED`, a touch
307  * point may have ended and re-started within a slot - a client must check
308  * the `ABS_MT_TRACKING_ID`. libevdev simplifies this by emulating extra
309  * events if the `ABS_MT_TRACKING_ID` has changed:
310  * * if the `ABS_MT_TRACKING_ID` was valid and is -1, libevdev enqueues an
311  *   `ABS_MT_TRACKING_ID` event with value -1.
312  * * if the `ABS_MT_TRACKING_ID` was -1 and is now a valid ID, libevdev
313  *   enqueues an `ABS_MT_TRACKING_ID` event with the current value.
314  * * if the `ABS_MT_TRACKING_ID` was a valid ID and is now a different valid
315  *   ID, libevev enqueues an `ABS_MT_TRACKING_ID` event with value -1 and
316  *   another `ABS_MT_TRACKING_ID` event with the new value.
317  *
318  * An example event sequence for such a sync may look like this:
319  * @code
320  *  EV_ABS   ABS_MT_SLOT         0
321  *  EV_ABS   ABS_MT_TRACKING_ID  -1
322  *  EV_ABS   ABS_MT_SLOT         2
323  *  EV_ABS   ABS_MT_TRACKING_ID  -1
324  *  EV_SYN   SYN_REPORT          0
325  *  ------------------------
326  *  EV_ABS   ABS_MT_SLOT         1
327  *  EV_ABS   ABS_MT_POSITION_X   100
328  *  EV_ABS   ABS_MT_POSITION_Y   80
329  *  EV_ABS   ABS_MT_SLOT         2
330  *  EV_ABS   ABS_MT_TRACKING_ID  45
331  *  EV_ABS   ABS_MT_POSITION_Y   8
332  *  EV_ABS   ABS_MT_PRESSURE     12
333  *  EV_ABS   ABS_MT_SLOT         1
334  *  EV_SYN   SYN_REPORT          0
335  * @endcode
336  * Note how the touchpoint in slot 0 was terminated, the touchpoint in slot
337  * 2 was terminated and then started with a new `ABS_MT_TRACKING_ID`. The touchpoint
338  * in slot 1 maintained the same `ABS_MT_TRACKING_ID` and only updated the
339  * coordinates. Slot 1 is the currently active slot.
340  *
341  * In the case of a `SYN_DROPPED` event, a touch point may be invisible to a
342  * client if it started after `SYN_DROPPED` and finished before the client
343  * handles events again. The below example shows an example event sequence
344  * and what libevdev sees in the case of a `SYN_DROPPED` event:
345  * @code
346  *
347  *            kernel                  |              userspace
348  *                                    |
349  *  EV_ABS   ABS_MT_SLOT         0    |    EV_ABS   ABS_MT_SLOT         0
350  *  EV_ABS   ABS_MT_TRACKING_ID  -1   |    EV_ABS   ABS_MT_TRACKING_ID  -1
351  *  EV_SYN   SYN_REPORT          0    |    EV_SYN   SYN_REPORT          0
352  *  ------------------------          |    ------------------------
353  *  EV_ABS   ABS_MT_TRACKING_ID  30   |
354  *  EV_ABS   ABS_MT_POSITION_X   100  |
355  *  EV_ABS   ABS_MT_POSITION_Y   80   |
356  *  EV_SYN   SYN_REPORT          0    |           SYN_DROPPED
357  *  ------------------------          |
358  *  EV_ABS   ABS_MT_TRACKING_ID  -1   |
359  *  EV_SYN   SYN_REPORT          0    |
360  *  ------------------------          |    ------------------------
361  *  EV_ABS   ABS_MT_SLOT         1    |    EV_ABS   ABS_MT_SLOT         1
362  *  EV_ABS   ABS_MT_POSITION_X   90   |    EV_ABS   ABS_MT_POSITION_X   90
363  *  EV_ABS   ABS_MT_POSITION_Y   10   |    EV_ABS   ABS_MT_POSITION_Y   10
364  *  EV_SYN   SYN_REPORT          0    |    EV_SYN   SYN_REPORT          0
365  * @endcode
366  * If such an event sequence occurs, libevdev will send all updated axes
367  * during the sync process. Axis events may thus be generated for devices
368  * without a currently valid `ABS_MT_TRACKING_ID`. Specifically for the above
369  * example, the client would receive the following event sequence:
370  * @code
371  *  EV_ABS   ABS_MT_SLOT         0       ← LIBEVDEV_READ_FLAG_NORMAL
372  *  EV_ABS   ABS_MT_TRACKING_ID  -1
373  *  EV_SYN   SYN_REPORT          0
374  *  ------------------------
375  *  EV_SYN   SYN_DROPPED         0       → LIBEVDEV_READ_STATUS_SYNC
376  *  ------------------------
377  *  EV_ABS   ABS_MT_POSITION_X   100     ← LIBEVDEV_READ_FLAG_SYNC
378  *  EV_ABS   ABS_MT_POSITION_Y   80
379  *  EV_SYN   SYN_REPORT          0
380  *  -----------------------------        → -EGAIN
381  *  EV_ABS   ABS_MT_SLOT         1       ← LIBEVDEV_READ_FLAG_NORMAL
382  *  EV_ABS   ABS_MT_POSITION_X   90
383  *  EV_ABS   ABS_MT_POSITION_Y   10
384  *  EV_SYN   SYN_REPORT          0
385  *  -------------------
386  * @endcode
387  * The axis events do not reflect the position of a current touch point, a
388  * client must take care not to generate a new touch point based on those
389  * updates.
390  *
391  * Discarding events before synchronizing
392  * =====================================
393  *
394  * The kernel implements the client buffer as a ring buffer. `SYN_DROPPED`
395  * events are handled when the buffer is full and a new event is received
396  * from a device. All existing events are discarded, a `SYN_DROPPED` is added
397  * to the buffer followed by the actual device event. Further events will be
398  * appended to the buffer until it is either read by the client, or filled
399  * again, at which point the sequence repeats.
400  *
401  * When the client reads the buffer, the buffer will thus always consist of
402  * exactly one `SYN_DROPPED` event followed by an unspecified number of real
403  * events. The data the ioctls return is the current state of the device,
404  * i.e. the state after all these events have been processed. For example,
405  * assume the buffer contains the following sequence:
406  *
407  * @code
408  *  EV_SYN   SYN_DROPPED
409  *  EV_ABS   ABS_X               1
410  *  EV_SYN   SYN_REPORT          0
411  *  EV_ABS   ABS_X               2
412  *  EV_SYN   SYN_REPORT          0
413  *  EV_ABS   ABS_X               3
414  *  EV_SYN   SYN_REPORT          0
415  *  EV_ABS   ABS_X               4
416  *  EV_SYN   SYN_REPORT          0
417  *  EV_ABS   ABS_X               5
418  *  EV_SYN   SYN_REPORT          0
419  *  EV_ABS   ABS_X               6
420  *  EV_SYN   SYN_REPORT          0
421  * @endcode
422  * An ioctl at any time in this sequence will return a value of 6 for ABS_X.
423  *
424  * libevdev discards all events after a `SYN_DROPPED` to ensure the events
425  * during @ref LIBEVDEV_READ_FLAG_SYNC represent the last known state of the
426  * device. This loses some granularity of the events especially as the time
427  * between the `SYN_DROPPED` and the sync process increases. It does however
428  * avoid spurious cursor movements. In the above example, the event sequence
429  * by libevdev is:
430  * @code
431  *  EV_SYN   SYN_DROPPED
432  *  EV_ABS   ABS_X               6
433  *  EV_SYN   SYN_REPORT          0
434  *  @endcode
435  */
436 
437 /**
438  * @page backwardscompatibility Compatibility and Behavior across kernel versions
439  *
440  * This page describes libevdev's behavior when the build-time kernel and the
441  * run-time kernel differ in their feature set.
442  *
443  * With the exception of event names, libevdev defines features that may be
444  * missing on older kernels and building on such kernels will not disable
445  * features. Running libevdev on a kernel that is missing some feature will
446  * change libevdev's behavior. In most cases, the new behavior should be
447  * obvious, but it is spelled out below in detail.
448  *
449  * Minimum requirements
450  * ====================
451  * libevdev requires a 2.6.36 kernel as minimum. Specifically, it requires
452  * kernel-support for `ABS_MT_SLOT`.
453  *
454  * Event and input property names
455  * ==============================
456  * Event names and input property names are defined at build-time by the
457  * linux/input.h shipped with libevdev.
458  * The list of event names is compiled at build-time, any events not defined
459  * at build time will not resolve. Specifically,
460  * libevdev_event_code_get_name() for an undefined type or code will
461  * always return `NULL`. Likewise, libevdev_property_get_name() will return NULL
462  * for properties undefined at build-time.
463  *
464  * Input properties
465  * ================
466  * If the kernel does not support input properties, specifically the
467  * `EVIOCGPROPS` ioctl, libevdev does not expose input properties to the caller.
468  * Specifically, libevdev_has_property() will always return 0 unless the
469  * property has been manually set with libevdev_enable_property().
470  *
471  * This also applies to the libevdev-uinput code. If uinput does not honor
472  * `UI_SET_PROPBIT`, libevdev will continue without setting the properties on
473  * the device.
474  *
475  * MT slot behavior
476  * =================
477  * If the kernel does not support the `EVIOCGMTSLOTS` ioctl, libevdev
478  * assumes all values in all slots are 0 and continues without an error.
479  *
480  * SYN_DROPPED behavior
481  * ====================
482  * A kernel without `SYN_DROPPED` won't send such an event. libevdev_next_event()
483  * will never require the switch to sync mode.
484  */
485 
486 /**
487  * @page ioctls evdev ioctls
488  *
489  * This page lists the status of the evdev-specific ioctls in libevdev.
490  *
491  * <dl>
492  * <dt>EVIOCGVERSION:</dt>
493  * <dd>supported, see libevdev_get_driver_version()</dd>
494  * <dt>EVIOCGID:</dt>
495  * <dd>supported, see libevdev_get_id_product(), libevdev_get_id_vendor(),
496  * libevdev_get_id_bustype(), libevdev_get_id_version()</dd>
497  * <dt>EVIOCGREP:</dt>
498  * <dd>supported, see libevdev_get_event_value())</dd>
499  * <dt>EVIOCSREP:</dt>
500  * <dd>supported, see libevdev_enable_event_code()</dd>
501  * <dt>EVIOCGKEYCODE:</dt>
502  * <dd>currently not supported</dd>
503  * <dt>EVIOCGKEYCODE:</dt>
504  * <dd>currently not supported</dd>
505  * <dt>EVIOCSKEYCODE:</dt>
506  * <dd>currently not supported</dd>
507  * <dt>EVIOCSKEYCODE:</dt>
508  * <dd>currently not supported</dd>
509  * <dt>EVIOCGNAME:</dt>
510  * <dd>supported, see libevdev_get_name()</dd>
511  * <dt>EVIOCGPHYS:</dt>
512  * <dd>supported, see libevdev_get_phys()</dd>
513  * <dt>EVIOCGUNIQ:</dt>
514  * <dd>supported, see libevdev_get_uniq()</dd>
515  * <dt>EVIOCGPROP:</dt>
516  * <dd>supported, see libevdev_has_property()</dd>
517  * <dt>EVIOCGMTSLOTS:</dt>
518  * <dd>supported, see libevdev_get_num_slots(), libevdev_get_slot_value()</dd>
519  * <dt>EVIOCGKEY:</dt>
520  * <dd>supported, see libevdev_has_event_code(), libevdev_get_event_value()</dd>
521  * <dt>EVIOCGLED:</dt>
522  * <dd>supported, see libevdev_has_event_code(), libevdev_get_event_value()</dd>
523  * <dt>EVIOCGSND:</dt>
524  * <dd>currently not supported</dd>
525  * <dt>EVIOCGSW:</dt>
526  * <dd>supported, see libevdev_has_event_code(), libevdev_get_event_value()</dd>
527  * <dt>EVIOCGBIT:</dt>
528  * <dd>supported, see libevdev_has_event_code(), libevdev_get_event_value()</dd>
529  * <dt>EVIOCGABS:</dt>
530  * <dd>supported, see libevdev_has_event_code(), libevdev_get_event_value(),
531  * libevdev_get_abs_info()</dd>
532  * <dt>EVIOCSABS:</dt>
533  * <dd>supported, see libevdev_kernel_set_abs_info()</dd>
534  * <dt>EVIOCSFF:</dt>
535  * <dd>currently not supported</dd>
536  * <dt>EVIOCRMFF:</dt>
537  * <dd>currently not supported</dd>
538  * <dt>EVIOCGEFFECTS:</dt>
539  * <dd>currently not supported</dd>
540  * <dt>EVIOCGRAB:</dt>
541  * <dd>supported, see libevdev_grab()</dd>
542  * <dt>EVIOCSCLOCKID:</dt>
543  * <dd>supported, see libevdev_set_clock_id()</dd>
544  * <dt>EVIOCREVOKE:</dt>
545  * <dd>currently not supported, see
546  * http://lists.freedesktop.org/archives/input-tools/2014-January/000688.html</dd>
547  * </dl>
548  *
549  */
550 
551 /**
552  * @page kernel_header Kernel header
553  *
554  * libevdev provides its own copy of the Linux kernel header file and
555  * compiles against the definitions define here. Event type and event code
556  * names, etc. are taken from the file below:
557  * @include linux/input.h
558  */
559 
560 /**
561  * @page static_linking Statically linking libevdev
562  *
563  * Statically linking libevdev.a is not recommended. Symbol visibility is
564  * difficult to control in a static library, so extra care must be taken to
565  * only use symbols that are explicitly exported. libevdev's API stability
566  * guarantee only applies to those symbols.
567  *
568  * If you do link libevdev statically, note that in addition to the exported
569  * symbols, libevdev reserves the <b>_libevdev_*</b> namespace. Do not use
570  * or create symbols with that prefix, they are subject to change at any
571  * time.
572  */
573 
574 /**
575  * @page testing libevdev-internal test suite
576  *
577  * libevdev's internal test suite uses the
578  * [Check unit testing framework](http://check.sourceforge.net/). Tests are
579  * divided into test suites and test cases. Most tests create a uinput device,
580  * so you'll need to run as root, and your kernel must have
581  * `CONFIG_INPUT_UINPUT` enabled.
582  *
583  * To run a specific suite only:
584  *
585  *     export CK_RUN_SUITE="suite name"
586  *
587  * To run a specific test case only:
588  *
589  *     export CK_RUN_TEST="test case name"
590  *
591  * To get a list of all suites or tests:
592  *
593  *     git grep "suite_create"
594  *     git grep "tcase_create"
595  *
596  * By default, Check forks, making debugging harder. The test suite tries to detect
597  * if it is running inside gdb and disable forking. If that doesn't work for
598  * some reason, run gdb as below to avoid forking.
599  *
600  *     sudo CK_FORK=no CK_RUN_TEST="test case name" gdb ./test/test-libevdev
601  *
602  * A special target `make gcov-report.txt` exists that runs gcov and leaves a
603  * `libevdev.c.gcov` file. Check that for test coverage.
604  *
605  * `make check` is hooked up to run the test and gcov (again, needs root).
606  *
607  * The test suite creates a lot of devices, very quickly. Add the following
608  * xorg.conf.d snippet to avoid the devices being added as X devices (at the
609  * time of writing, mutter can't handle these devices and exits after getting
610  * a BadDevice error).
611  *
612  *     $ cat /etc/X11/xorg.conf.d/99-ignore-libevdev-devices.conf
613  *     Section "InputClass"
614  *             Identifier "Ignore libevdev test devices"
615  *             MatchProduct "libevdev test device"
616  *             Option "Ignore" "on"
617  *     EndSection
618  *
619  */
620 
621 /**
622  * @defgroup init Initialization and setup
623  *
624  * Initialization, initial setup and file descriptor handling.
625  * These functions are the main entry points for users of libevdev, usually a
626  * caller will use this series of calls:
627  *
628  * @code
629  * struct libevdev *dev;
630  * int err;
631  *
632  * dev = libevdev_new();
633  * if (!dev)
634  *         return ENOMEM;
635  *
636  * err = libevdev_set_fd(dev, fd);
637  * if (err < 0)
638  *         printf("Failed (errno %d): %s\n", -err, strerror(-err));
639  *
640  * libevdev_free(dev);
641  * @endcode
642  *
643  * libevdev_set_fd() is the central call and initializes the internal structs
644  * for the device at the given fd. libevdev functions will fail if called
645  * before libevdev_set_fd() unless documented otherwise.
646  */
647 
648 /**
649  * @defgroup logging Library logging facilities
650  *
651  * libevdev provides two methods of logging library-internal messages. The
652  * old method is to provide a global log handler in
653  * libevdev_set_log_function(). The new method is to provide a per-context
654  * log handler in libevdev_set_device_log_function(). Developers are encouraged
655  * to use the per-context logging facilities over the global log handler as
656  * it provides access to the libevdev instance that caused a message, and is
657  * more flexible when libevdev is used from within a shared library.
658  *
659  * If a caller sets both the global log handler and a per-context log
660  * handler, each device with a per-context log handler will only invoke that
661  * log handler.
662  *
663  * @note To set a context-specific log handler, a context is needed.
664  * Thus developers are discouraged from using libevdev_new_from_fd() as
665  * important messages from the device initialization process may get lost.
666  *
667  * @note A context-specific handler cannot be used for libevdev's uinput
668  * devices. @ref uinput must use the global log handler.
669  */
670 
671 /**
672  * @defgroup bits Querying device capabilities
673  *
674  * Abstraction functions to handle device capabilities, specifically
675  * device properties such as the name of the device and the bits
676  * representing the events supported by this device.
677  *
678  * The logical state returned may lag behind the physical state of the device.
679  * libevdev queries the device state on libevdev_set_fd() and then relies on
680  * the caller to parse events through libevdev_next_event(). If a caller does not
681  * use libevdev_next_event(), libevdev will not update the internal state of the
682  * device and thus returns outdated values.
683  */
684 
685 /**
686  * @defgroup mt Multi-touch related functions
687  * Functions for querying multi-touch-related capabilities. MT devices
688  * following the kernel protocol B (using `ABS_MT_SLOT`) provide multiple touch
689  * points through so-called slots on the same axis. The slots are enumerated,
690  * a client reading from the device will first get an ABS_MT_SLOT event, then
691  * the values of axes changed in this slot. Multiple slots may be provided in
692  * before an `EV_SYN` event.
693  *
694  * As with @ref bits, the logical state of the device as seen by the library
695  * depends on the caller using libevdev_next_event().
696  *
697  * The Linux kernel requires all axes on a device to have a semantic
698  * meaning, matching the axis names in linux/input.h. Some devices merely
699  * export a number of axes beyond the available axis list. For those
700  * devices, the multitouch information is invalid. Specifically, if a device
701  * provides the `ABS_MT_SLOT` axis AND also the `ABS_RESERVED` axis, the
702  * device is not treated as multitouch device. No slot information is
703  * available and the `ABS_MT` axis range for these devices is treated as all
704  * other `EV_ABS` axes.
705  *
706  * Note that because of limitations in the kernel API, such fake multitouch
707  * devices can not be reliably synced after a `SYN_DROPPED` event. libevdev
708  * ignores all `ABS_MT` axis values during the sync process and instead
709  * relies on the device to send the current axis value with the first event
710  * after `SYN_DROPPED`.
711  */
712 
713 /**
714  * @defgroup kernel Modifying the appearance or capabilities of the device
715  *
716  * Modifying the set of events reported by this device. By default, the
717  * libevdev device mirrors the kernel device, enabling only those bits
718  * exported by the kernel. This set of functions enable or disable bits as
719  * seen from the caller.
720  *
721  * Enabling an event type or code does not affect event reporting - a
722  * software-enabled event will not be generated by the physical hardware.
723  * Disabling an event will prevent libevdev from routing such events to the
724  * caller. Enabling and disabling event types and codes is at the library
725  * level and thus only affects the caller.
726  *
727  * If an event type or code is enabled at kernel-level, future users of this
728  * device will see this event enabled. Currently there is no option of
729  * disabling an event type or code at kernel-level.
730  */
731 
732 /**
733  * @defgroup misc Miscellaneous helper functions
734  *
735  * Functions for printing or querying event ranges. The list of names is
736  * compiled into libevdev and is independent of the run-time kernel.
737  * Likewise, the max for each event type is compiled in and does not check
738  * the kernel at run-time.
739  */
740 
741 /**
742  * @defgroup events Event handling
743  *
744  * Functions to handle events and fetch the current state of the event.
745  * libevdev updates its internal state as the event is processed and forwarded
746  * to the caller. Thus, the libevdev state of the device should always be identical
747  * to the caller's state. It may however lag behind the actual state of the device.
748  */
749 
750 /**
751  * @ingroup init
752  *
753  * Opaque struct representing an evdev device.
754  */
755 struct libevdev;
756 
757 /**
758  * @ingroup events
759  */
760 enum libevdev_read_flag {
761 	LIBEVDEV_READ_FLAG_SYNC		= 1, /**< Process data in sync mode */
762 	LIBEVDEV_READ_FLAG_NORMAL	= 2, /**< Process data in normal mode */
763 	LIBEVDEV_READ_FLAG_FORCE_SYNC	= 4, /**< Pretend the next event is a SYN_DROPPED and
764 					          require the caller to sync */
765 	LIBEVDEV_READ_FLAG_BLOCKING	= 8  /**< The fd is not in O_NONBLOCK and a read may block */
766 };
767 
768 /**
769  * @ingroup init
770  *
771  * Initialize a new libevdev device. This function only allocates the
772  * required memory and initializes the struct to sane default values.
773  * To actually hook up the device to a kernel device, use
774  * libevdev_set_fd().
775  *
776  * Memory allocated through libevdev_new() must be released by the
777  * caller with libevdev_free().
778  *
779  * @see libevdev_set_fd
780  * @see libevdev_free
781  */
782 struct libevdev* libevdev_new(void);
783 
784 /**
785  * @ingroup init
786  *
787  * Initialize a new libevdev device from the given fd.
788  *
789  * This is a shortcut for
790  *
791  * @code
792  * int err;
793  * struct libevdev *dev = libevdev_new();
794  * err = libevdev_set_fd(dev, fd);
795  * @endcode
796  *
797  * @param fd A file descriptor to the device in O_RDWR or O_RDONLY mode.
798  * @param[out] dev The newly initialized evdev device.
799  *
800  * @return On success, 0 is returned and dev is set to the newly
801  * allocated struct. On failure, a negative errno is returned and the value
802  * of dev is undefined.
803  *
804  * @see libevdev_free
805  */
806 int libevdev_new_from_fd(int fd, struct libevdev **dev);
807 
808 /**
809  * @ingroup init
810  *
811  * Clean up and free the libevdev struct. After completion, the <code>struct
812  * libevdev</code> is invalid and must not be used.
813  *
814  * Note that calling libevdev_free() does not close the file descriptor
815  * currently associated with this instance.
816  *
817  * @param dev The evdev device
818  *
819  * @note This function may be called before libevdev_set_fd().
820  */
821 void libevdev_free(struct libevdev *dev);
822 
823 /**
824  * @ingroup logging
825  */
826 enum libevdev_log_priority {
827 	LIBEVDEV_LOG_ERROR = 10,	/**< critical errors and application bugs */
828 	LIBEVDEV_LOG_INFO  = 20,	/**< informational messages */
829 	LIBEVDEV_LOG_DEBUG = 30		/**< debug information */
830 };
831 
832 /**
833  * @ingroup logging
834  *
835  * Logging function called by library-internal logging.
836  * This function is expected to treat its input like printf would.
837  *
838  * @param priority Log priority of this message
839  * @param data User-supplied data pointer (see libevdev_set_log_function())
840  * @param file libevdev source code file generating this message
841  * @param line libevdev source code line generating this message
842  * @param func libevdev source code function generating this message
843  * @param format printf-style format string
844  * @param args List of arguments
845  *
846  * @see libevdev_set_log_function
847  */
848 typedef void (*libevdev_log_func_t)(enum libevdev_log_priority priority,
849 				    void *data,
850 				    const char *file, int line,
851 				    const char *func,
852 				    const char *format, va_list args)
853 	LIBEVDEV_ATTRIBUTE_PRINTF(6, 0);
854 
855 /**
856  * @ingroup logging
857  *
858  * Set a printf-style logging handler for library-internal logging. The default
859  * logging function is to stdout.
860  *
861  * @note The global log handler is only called if no context-specific log
862  * handler has been set with libevdev_set_device_log_function().
863  *
864  * @param logfunc The logging function for this device. If NULL, the current
865  * logging function is unset and no logging is performed.
866  * @param data User-specific data passed to the log handler.
867  *
868  * @note This function may be called before libevdev_set_fd().
869  *
870  * @deprecated Use per-context logging instead, see
871  * libevdev_set_device_log_function().
872  */
873 void libevdev_set_log_function(libevdev_log_func_t logfunc, void *data);
874 
875 /**
876  * @ingroup logging
877  *
878  * Define the minimum level to be printed to the log handler.
879  * Messages higher than this level are printed, others are discarded. This
880  * is a global setting and applies to any future logging messages.
881  *
882  * @param priority Minimum priority to be printed to the log.
883  *
884  * @deprecated Use per-context logging instead, see
885  * libevdev_set_device_log_function().
886  */
887 void libevdev_set_log_priority(enum libevdev_log_priority priority);
888 
889 /**
890  * @ingroup logging
891  *
892  * Return the current log priority level. Messages higher than this level
893  * are printed, others are discarded. This is a global setting.
894  *
895  * @return the current log level
896  *
897  * @deprecated Use per-context logging instead, see
898  * libevdev_set_device_log_function().
899  */
900 enum libevdev_log_priority libevdev_get_log_priority(void);
901 
902 /**
903  * @ingroup logging
904  *
905  * Logging function called by library-internal logging for a specific
906  * libevdev context. This function is expected to treat its input like
907  * printf would.
908  *
909  * @param dev The evdev device
910  * @param priority Log priority of this message
911  * @param data User-supplied data pointer (see libevdev_set_log_function())
912  * @param file libevdev source code file generating this message
913  * @param line libevdev source code line generating this message
914  * @param func libevdev source code function generating this message
915  * @param format printf-style format string
916  * @param args List of arguments
917  *
918  * @see libevdev_set_log_function
919  * @since 1.3
920  */
921 typedef void (*libevdev_device_log_func_t)(const struct libevdev *dev,
922 					   enum libevdev_log_priority priority,
923 					   void *data,
924 					   const char *file, int line,
925 					   const char *func,
926 					   const char *format, va_list args)
927 	LIBEVDEV_ATTRIBUTE_PRINTF(7, 0);
928 
929 /**
930  * @ingroup logging
931  *
932  * Set a printf-style logging handler for library-internal logging for this
933  * device context. The default logging function is NULL, i.e. the global log
934  * handler is invoked. If a context-specific log handler is set, the global
935  * log handler is not invoked for this device.
936  *
937  * @note This log function applies for this device context only, even if
938  * another context exists for the same fd.
939  *
940  * @param dev The evdev device
941  * @param logfunc The logging function for this device. If NULL, the current
942  * logging function is unset and logging falls back to the global log
943  * handler, if any.
944  * @param priority Minimum priority to be printed to the log.
945  * @param data User-specific data passed to the log handler.
946  *
947  * @note This function may be called before libevdev_set_fd().
948  * @since 1.3
949  */
950 void libevdev_set_device_log_function(struct libevdev *dev,
951 				      libevdev_device_log_func_t logfunc,
952 				      enum libevdev_log_priority priority,
953 				      void *data);
954 
955 /**
956  * @ingroup init
957  */
958 enum libevdev_grab_mode {
959 	LIBEVDEV_GRAB = 3,	/**< Grab the device if not currently grabbed */
960 	LIBEVDEV_UNGRAB = 4	/**< Ungrab the device if currently grabbed */
961 };
962 
963 /**
964  * @ingroup init
965  *
966  * Grab or ungrab the device through a kernel EVIOCGRAB. This prevents other
967  * clients (including kernel-internal ones such as rfkill) from receiving
968  * events from this device.
969  *
970  * This is generally a bad idea. Don't do this.
971  *
972  * Grabbing an already grabbed device, or ungrabbing an ungrabbed device is
973  * a noop and always succeeds.
974  *
975  * A grab is an operation tied to a file descriptor, not a device. If a
976  * client changes the file descriptor with libevdev_change_fd(), it must
977  * also re-issue a grab with libevdev_grab().
978  *
979  * @param dev The evdev device, already initialized with libevdev_set_fd()
980  * @param grab If true, grab the device. Otherwise ungrab the device.
981  *
982  * @return 0 if the device was successfully grabbed or ungrabbed, or a
983  * negative errno in case of failure.
984  */
985 int libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab);
986 
987 /**
988  * @ingroup init
989  *
990  * Set the fd for this struct and initialize internal data.
991  * The fd must be in O_RDONLY or O_RDWR mode.
992  *
993  * This function may only be called once per device. If the device changed and
994  * you need to re-read a device, use libevdev_free() and libevdev_new(). If
995  * you need to change the fd after closing and re-opening the same device, use
996  * libevdev_change_fd().
997  *
998  * A caller should ensure that any events currently pending on the fd are
999  * drained before the file descriptor is passed to libevdev for
1000  * initialization. Due to how the kernel's ioctl handling works, the initial
1001  * device state will reflect the current device state *after* applying all
1002  * events currently pending on the fd. Thus, if the fd is not drained, the
1003  * state visible to the caller will be inconsistent with the events
1004  * immediately available on the device. This does not affect state-less
1005  * events like EV_REL.
1006  *
1007  * Unless otherwise specified, libevdev function behavior is undefined until
1008  * a successful call to libevdev_set_fd().
1009  *
1010  * @param dev The evdev device
1011  * @param fd The file descriptor for the device
1012  *
1013  * @return 0 on success, or a negative errno on failure
1014  *
1015  * @see libevdev_change_fd
1016  * @see libevdev_new
1017  * @see libevdev_free
1018  */
1019 int libevdev_set_fd(struct libevdev* dev, int fd);
1020 
1021 /**
1022  * @ingroup init
1023  *
1024  * Change the fd for this device, without re-reading the actual device. If the fd
1025  * changes after initializing the device, for example after a VT-switch in the
1026  * X.org X server, this function updates the internal fd to the newly opened.
1027  * No check is made that new fd points to the same device. If the device has
1028  * changed, libevdev's behavior is undefined.
1029  *
1030  * libevdev does not sync itself after changing the fd and keeps the current
1031  * device state. Use libevdev_next_event with the
1032  * @ref LIBEVDEV_READ_FLAG_FORCE_SYNC flag to force a re-sync.
1033  *
1034  * The example code below illustrates how to force a re-sync of the
1035  * library-internal state. Note that this code doesn't handle the events in
1036  * the caller, it merely forces an update of the internal library state.
1037  * @code
1038  *     struct input_event ev;
1039  *     libevdev_change_fd(dev, new_fd);
1040  *     libevdev_next_event(dev, LIBEVDEV_READ_FLAG_FORCE_SYNC, &ev);
1041  *     while (libevdev_next_event(dev, LIBEVDEV_READ_FLAG_SYNC, &ev) == LIBEVDEV_READ_STATUS_SYNC)
1042  *                             ; // noop
1043  * @endcode
1044  *
1045  * The fd may be open in O_RDONLY or O_RDWR.
1046  *
1047  * After changing the fd, the device is assumed ungrabbed and a caller must
1048  * call libevdev_grab() again.
1049  *
1050  * It is an error to call this function before calling libevdev_set_fd().
1051  *
1052  * @param dev The evdev device, already initialized with libevdev_set_fd()
1053  * @param fd The new fd
1054  *
1055  * @return 0 on success, or -1 on failure.
1056  *
1057  * @see libevdev_set_fd
1058  */
1059 int libevdev_change_fd(struct libevdev* dev, int fd);
1060 
1061 /**
1062  * @ingroup init
1063  *
1064  * @param dev The evdev device
1065  *
1066  * @return The previously set fd, or -1 if none had been set previously.
1067  * @note This function may be called before libevdev_set_fd().
1068  */
1069 int libevdev_get_fd(const struct libevdev* dev);
1070 
1071 /**
1072  * @ingroup events
1073  */
1074 enum libevdev_read_status {
1075 	/**
1076 	 * libevdev_next_event() has finished without an error
1077 	 * and an event is available for processing.
1078 	 *
1079 	 * @see libevdev_next_event
1080 	 */
1081 	LIBEVDEV_READ_STATUS_SUCCESS = 0,
1082 	/**
1083 	 * Depending on the libevdev_next_event() read flag:
1084 	 * * libevdev received a SYN_DROPPED from the device, and the caller should
1085 	 * now resync the device, or,
1086 	 * * an event has been read in sync mode.
1087 	 *
1088 	 * @see libevdev_next_event
1089 	 */
1090 	LIBEVDEV_READ_STATUS_SYNC = 1
1091 };
1092 
1093 /**
1094  * @ingroup events
1095  *
1096  * Get the next event from the device. This function operates in two different
1097  * modes: normal mode or sync mode.
1098  *
1099  * In normal mode (when flags has @ref LIBEVDEV_READ_FLAG_NORMAL set), this
1100  * function returns @ref LIBEVDEV_READ_STATUS_SUCCESS and returns the event
1101  * in the argument @p ev. If no events are available at this
1102  * time, it returns -EAGAIN and ev is undefined.
1103  *
1104  * If the current event is an EV_SYN SYN_DROPPED event, this function returns
1105  * @ref LIBEVDEV_READ_STATUS_SYNC and ev is set to the EV_SYN event.
1106  * The caller should now call this function with the
1107  * @ref LIBEVDEV_READ_FLAG_SYNC flag set, to get the set of events that make up the
1108  * device state delta. This function returns @ref LIBEVDEV_READ_STATUS_SYNC for
1109  * each event part of that delta, until it returns -EAGAIN once all events
1110  * have been synced. For more details on what libevdev does to sync after a
1111  * SYN_DROPPED event, see @ref syn_dropped.
1112  *
1113  * If a device needs to be synced by the caller but the caller does not call
1114  * with the @ref LIBEVDEV_READ_FLAG_SYNC flag set, all events from the diff are
1115  * dropped after libevdev updates its internal state and event processing
1116  * continues as normal. Note that the current slot and the state of touch
1117  * points may have updated during the SYN_DROPPED event, it is strongly
1118  * recommended that a caller ignoring all sync events calls
1119  * libevdev_get_current_slot() and checks the ABS_MT_TRACKING_ID values for
1120  * all slots.
1121  *
1122  * If a device has changed state without events being enqueued in libevdev,
1123  * e.g. after changing the file descriptor, use the @ref
1124  * LIBEVDEV_READ_FLAG_FORCE_SYNC flag. This triggers an internal sync of the
1125  * device and libevdev_next_event() returns @ref LIBEVDEV_READ_STATUS_SYNC.
1126  * Any state changes are available as events as described above. If
1127  * @ref LIBEVDEV_READ_FLAG_FORCE_SYNC is set, the value of ev is undefined.
1128  *
1129  * @param dev The evdev device, already initialized with libevdev_set_fd()
1130  * @param flags Set of flags to determine behaviour. If @ref LIBEVDEV_READ_FLAG_NORMAL
1131  * is set, the next event is read in normal mode. If @ref LIBEVDEV_READ_FLAG_SYNC is
1132  * set, the next event is read in sync mode.
1133  * @param ev On success, set to the current event.
1134  * @return On failure, a negative errno is returned.
1135  * @retval LIBEVDEV_READ_STATUS_SUCCESS One or more events were read of the
1136  * device and ev points to the next event in the queue
1137  * @retval -EAGAIN No events are currently available on the device
1138  * @retval LIBEVDEV_READ_STATUS_SYNC A SYN_DROPPED event was received, or a
1139  * synced event was returned and ev points to the SYN_DROPPED event
1140  *
1141  * @note This function is signal-safe.
1142  */
1143 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev);
1144 
1145 /**
1146  * @ingroup events
1147  *
1148  * Check if there are events waiting for us. This function does not read an
1149  * event off the fd and may not access the fd at all. If there are events
1150  * queued internally this function will return non-zero. If the internal
1151  * queue is empty, this function will poll the file descriptor for data.
1152  *
1153  * This is a convenience function for simple processes, most complex programs
1154  * are expected to use select(2) or poll(2) on the file descriptor. The kernel
1155  * guarantees that if data is available, it is a multiple of sizeof(struct
1156  * input_event), and thus calling libevdev_next_event() when select(2) or
1157  * poll(2) return is safe. You do not need libevdev_has_event_pending() if
1158  * you're using select(2) or poll(2).
1159  *
1160  * @param dev The evdev device, already initialized with libevdev_set_fd()
1161  * @return On failure, a negative errno is returned.
1162  * @retval 0 No event is currently available
1163  * @retval 1 One or more events are available on the fd
1164  *
1165  * @note This function is signal-safe.
1166  */
1167 int libevdev_has_event_pending(struct libevdev *dev);
1168 
1169 /**
1170  * @ingroup bits
1171  *
1172  * Retrieve the device's name, either as set by the caller or as read from
1173  * the kernel. The string returned is valid until libevdev_free() or until
1174  * libevdev_set_name(), whichever comes earlier.
1175  *
1176  * @param dev The evdev device, already initialized with libevdev_set_fd()
1177  *
1178  * @return The device name as read off the kernel device. The name is never
1179  * NULL but it may be the empty string.
1180  *
1181  * @note This function is signal-safe.
1182  */
1183 const char* libevdev_get_name(const struct libevdev *dev);
1184 
1185 /**
1186  * @ingroup kernel
1187  *
1188  * Change the device's name as returned by libevdev_get_name(). This
1189  * function destroys the string previously returned by libevdev_get_name(),
1190  * a caller must take care that no references are kept.
1191  *
1192  * @param dev The evdev device
1193  * @param name The new, non-NULL, name to assign to this device.
1194  *
1195  * @note This function may be called before libevdev_set_fd(). A call to
1196  * libevdev_set_fd() will overwrite any previously set value.
1197  */
1198 void libevdev_set_name(struct libevdev *dev, const char *name);
1199 
1200 /**
1201  * @ingroup bits
1202  *
1203  * Retrieve the device's physical location, either as set by the caller or
1204  * as read from the kernel. The string returned is valid until
1205  * libevdev_free() or until libevdev_set_phys(), whichever comes earlier.
1206  *
1207  * Virtual devices such as uinput devices have no phys location.
1208  *
1209  * @param dev The evdev device, already initialized with libevdev_set_fd()
1210  *
1211  * @return The physical location of this device, or NULL if there is none
1212  *
1213  * @note This function is signal safe.
1214  */
1215 const char * libevdev_get_phys(const struct libevdev *dev);
1216 
1217 /**
1218  * @ingroup kernel
1219  *
1220  * Change the device's physical location as returned by libevdev_get_phys().
1221  * This function destroys the string previously returned by
1222  * libevdev_get_phys(), a caller must take care that no references are kept.
1223  *
1224  * @param dev The evdev device
1225  * @param phys The new phys to assign to this device.
1226  *
1227  * @note This function may be called before libevdev_set_fd(). A call to
1228  * libevdev_set_fd() will overwrite any previously set value.
1229  */
1230 void libevdev_set_phys(struct libevdev *dev, const char *phys);
1231 
1232 /**
1233  * @ingroup bits
1234  *
1235  * Retrieve the device's unique identifier, either as set by the caller or
1236  * as read from the kernel. The string returned is valid until
1237  * libevdev_free() or until libevdev_set_uniq(), whichever comes earlier.
1238  *
1239  * @param dev The evdev device, already initialized with libevdev_set_fd()
1240  *
1241  * @return The unique identifier for this device, or NULL if there is none
1242  *
1243  * @note This function is signal safe.
1244  */
1245 const char * libevdev_get_uniq(const struct libevdev *dev);
1246 
1247 /**
1248  * @ingroup kernel
1249  *
1250  * Change the device's unique identifier as returned by libevdev_get_uniq().
1251  * This function destroys the string previously returned by
1252  * libevdev_get_uniq(), a caller must take care that no references are kept.
1253  *
1254  * @param dev The evdev device
1255  * @param uniq The new uniq to assign to this device.
1256  *
1257  * @note This function may be called before libevdev_set_fd(). A call to
1258  * libevdev_set_fd() will overwrite any previously set value.
1259  */
1260 void libevdev_set_uniq(struct libevdev *dev, const char *uniq);
1261 
1262 /**
1263  * @ingroup bits
1264  *
1265  * @param dev The evdev device, already initialized with libevdev_set_fd()
1266  *
1267  * @return The device's product ID
1268  *
1269  * @note This function is signal-safe.
1270  */
1271 int libevdev_get_id_product(const struct libevdev *dev);
1272 
1273 /**
1274  * @ingroup kernel
1275  *
1276  * @param dev The evdev device
1277  * @param product_id The product ID to assign to this device
1278  *
1279  * @note This function may be called before libevdev_set_fd(). A call to
1280  * libevdev_set_fd() will overwrite any previously set value. Even though
1281  * the function accepts an int for product_id the value is truncated at 16
1282  * bits.
1283  */
1284 void libevdev_set_id_product(struct libevdev *dev, int product_id);
1285 
1286 /**
1287  * @ingroup bits
1288  *
1289  * @param dev The evdev device, already initialized with libevdev_set_fd()
1290  *
1291  * @return The device's vendor ID
1292  *
1293  * @note This function is signal-safe.
1294  */
1295 int libevdev_get_id_vendor(const struct libevdev *dev);
1296 
1297 /**
1298  * @ingroup kernel
1299  *
1300  * @param dev The evdev device
1301  * @param vendor_id The vendor ID to assign to this device
1302  *
1303  * @note This function may be called before libevdev_set_fd(). A call to
1304  * libevdev_set_fd() will overwrite any previously set value. Even though
1305  * the function accepts an int for vendor_id the value is truncated at 16
1306  * bits.
1307  */
1308 void libevdev_set_id_vendor(struct libevdev *dev, int vendor_id);
1309 
1310 /**
1311  * @ingroup bits
1312  *
1313  * @param dev The evdev device, already initialized with libevdev_set_fd()
1314  *
1315  * @return The device's bus type
1316  *
1317  * @note This function is signal-safe.
1318  */
1319 int libevdev_get_id_bustype(const struct libevdev *dev);
1320 
1321 /**
1322  * @ingroup kernel
1323  *
1324  * @param dev The evdev device
1325  * @param bustype The bustype to assign to this device
1326  *
1327  * @note This function may be called before libevdev_set_fd(). A call to
1328  * libevdev_set_fd() will overwrite any previously set value. Even though
1329  * the function accepts an int for bustype the value is truncated at 16
1330  * bits.
1331  */
1332 void libevdev_set_id_bustype(struct libevdev *dev, int bustype);
1333 
1334 /**
1335  * @ingroup bits
1336  *
1337  * @param dev The evdev device, already initialized with libevdev_set_fd()
1338  *
1339  * @return The device's firmware version
1340  *
1341  * @note This function is signal-safe.
1342  */
1343 int libevdev_get_id_version(const struct libevdev *dev);
1344 
1345 /**
1346  * @ingroup kernel
1347  *
1348  * @param dev The evdev device
1349  * @param version The version to assign to this device
1350  *
1351  * @note This function may be called before libevdev_set_fd(). A call to
1352  * libevdev_set_fd() will overwrite any previously set value. Even though
1353  * the function accepts an int for version the value is truncated at 16
1354  * bits.
1355  */
1356 void libevdev_set_id_version(struct libevdev *dev, int version);
1357 
1358 /**
1359  * @ingroup bits
1360  *
1361  * @param dev The evdev device, already initialized with libevdev_set_fd()
1362  *
1363  * @return The driver version for this device
1364  *
1365  * @note This function is signal-safe.
1366  */
1367 int libevdev_get_driver_version(const struct libevdev *dev);
1368 
1369 /**
1370  * @ingroup bits
1371  *
1372  * @param dev The evdev device, already initialized with libevdev_set_fd()
1373  * @param prop The input property to query for, one of INPUT_PROP_...
1374  *
1375  * @return 1 if the device provides this input property, or 0 otherwise.
1376  *
1377  * @note This function is signal-safe
1378  */
1379 int libevdev_has_property(const struct libevdev *dev, unsigned int prop);
1380 
1381 /**
1382  * @ingroup kernel
1383  *
1384  * @param dev The evdev device
1385  * @param prop The input property to enable, one of INPUT_PROP_...
1386  *
1387  * @return 0 on success or -1 on failure
1388  *
1389  * @note This function may be called before libevdev_set_fd(). A call to
1390  * libevdev_set_fd() will overwrite any previously set value.
1391  */
1392 int libevdev_enable_property(struct libevdev *dev, unsigned int prop);
1393 
1394 /**
1395  * @ingroup kernel
1396  *
1397  * @param dev The evdev device
1398  * @param prop The input property to disable, one of INPUT_PROP_...
1399  *
1400  * @return 0 on success or -1 on failure
1401  */
1402 int libevdev_disable_property(struct libevdev *dev, unsigned int prop);
1403 
1404 /**
1405  * @ingroup bits
1406  *
1407  * @param dev The evdev device, already initialized with libevdev_set_fd()
1408  * @param type The event type to query for, one of EV_SYN, EV_REL, etc.
1409  *
1410  * @return 1 if the device supports this event type, or 0 otherwise.
1411  *
1412  * @note This function is signal-safe.
1413  */
1414 int libevdev_has_event_type(const struct libevdev *dev, unsigned int type);
1415 
1416 /**
1417  * @ingroup bits
1418  *
1419  * @param dev The evdev device, already initialized with libevdev_set_fd()
1420  * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
1421  * @param code The event code to query for, one of ABS_X, REL_X, etc.
1422  *
1423  * @return 1 if the device supports this event type and code, or 0 otherwise.
1424  *
1425  * @note This function is signal-safe.
1426  */
1427 int libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code);
1428 
1429 /**
1430  * @ingroup bits
1431  *
1432  * Get the minimum axis value for the given axis, as advertised by the kernel.
1433  *
1434  * @param dev The evdev device, already initialized with libevdev_set_fd()
1435  * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
1436  *
1437  * @return axis minimum for the given axis or 0 if the axis is invalid
1438  *
1439  * @note This function is signal-safe.
1440  */
1441 int libevdev_get_abs_minimum(const struct libevdev *dev, unsigned int code);
1442 
1443 /**
1444  * @ingroup bits
1445  *
1446  * Get the maximum axis value for the given axis, as advertised by the kernel.
1447  *
1448  * @param dev The evdev device, already initialized with libevdev_set_fd()
1449  * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
1450  *
1451  * @return axis maximum for the given axis or 0 if the axis is invalid
1452  *
1453  * @note This function is signal-safe.
1454  */
1455 int libevdev_get_abs_maximum(const struct libevdev *dev, unsigned int code);
1456 
1457 /**
1458  * @ingroup bits
1459  *
1460  * Get the axis fuzz for the given axis, as advertised by the kernel.
1461  *
1462  * @param dev The evdev device, already initialized with libevdev_set_fd()
1463  * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
1464  *
1465  * @return axis fuzz for the given axis or 0 if the axis is invalid
1466  *
1467  * @note This function is signal-safe.
1468  */
1469 int libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code);
1470 
1471 /**
1472  * @ingroup bits
1473  *
1474  * Get the axis flat for the given axis, as advertised by the kernel.
1475  *
1476  * @param dev The evdev device, already initialized with libevdev_set_fd()
1477  * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
1478  *
1479  * @return axis flat for the given axis or 0 if the axis is invalid
1480  *
1481  * @note This function is signal-safe.
1482  */
1483 int libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code);
1484 
1485 /**
1486  * @ingroup bits
1487  *
1488  * Get the axis resolution for the given axis, as advertised by the kernel.
1489  *
1490  * @param dev The evdev device, already initialized with libevdev_set_fd()
1491  * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
1492  *
1493  * @return axis resolution for the given axis or 0 if the axis is invalid
1494  *
1495  * @note This function is signal-safe.
1496  */
1497 int libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code);
1498 
1499 /**
1500  * @ingroup bits
1501  *
1502  * Get the axis info for the given axis, as advertised by the kernel.
1503  *
1504  * @param dev The evdev device, already initialized with libevdev_set_fd()
1505  * @param code The EV_ABS event code to query for, one of ABS_X, ABS_Y, etc.
1506  *
1507  * @return The input_absinfo for the given code, or NULL if the device does
1508  * not support this event code.
1509  *
1510  * @note This function is signal-safe.
1511  */
1512 const struct input_absinfo* libevdev_get_abs_info(const struct libevdev *dev, unsigned int code);
1513 
1514 /**
1515  * @ingroup bits
1516  *
1517  * Behaviour of this function is undefined if the device does not provide
1518  * the event.
1519  *
1520  * If the device supports ABS_MT_SLOT, the value returned for any ABS_MT_*
1521  * event code is undefined. Use libevdev_get_slot_value() instead.
1522  *
1523  * @param dev The evdev device, already initialized with libevdev_set_fd()
1524  * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
1525  * @param code The event code to query for, one of ABS_X, REL_X, etc.
1526  *
1527  * @return The current value of the event.
1528  *
1529  * @note This function is signal-safe.
1530  * @note The value for ABS_MT_ events is undefined, use
1531  * libevdev_get_slot_value() instead
1532  *
1533  * @see libevdev_get_slot_value
1534  */
1535 int libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code);
1536 
1537 /**
1538  * @ingroup kernel
1539  *
1540  * Set the value for a given event type and code. This only makes sense for
1541  * some event types, e.g. setting the value for EV_REL is pointless.
1542  *
1543  * This is a local modification only affecting only this representation of
1544  * this device. A future call to libevdev_get_event_value() will return this
1545  * value, unless the value was overwritten by an event.
1546  *
1547  * If the device supports ABS_MT_SLOT, the value set for any ABS_MT_*
1548  * event code is the value of the currently active slot. You should use
1549  * libevdev_set_slot_value() instead.
1550  *
1551  * If the device supports ABS_MT_SLOT and the type is EV_ABS and the code is
1552  * ABS_MT_SLOT, the value must be a positive number less then the number of
1553  * slots on the device. Otherwise, libevdev_set_event_value() returns -1.
1554  *
1555  * @param dev The evdev device, already initialized with libevdev_set_fd()
1556  * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
1557  * @param code The event code to set the value for, one of ABS_X, LED_NUML, etc.
1558  * @param value The new value to set
1559  *
1560  * @return 0 on success, or -1 on failure.
1561  * @retval -1
1562  * - the device does not have the event type or
1563  * - code enabled, or the code is outside the, or
1564  * - the code is outside the allowed limits for the given type, or
1565  * - the type cannot be set, or
1566  * - the value is not permitted for the given code.
1567  *
1568  * @see libevdev_set_slot_value
1569  * @see libevdev_get_event_value
1570  */
1571 int libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value);
1572 
1573 /**
1574  * @ingroup bits
1575  *
1576  * Fetch the current value of the event type. This is a shortcut for
1577  *
1578  * @code
1579  *   if (libevdev_has_event_type(dev, t) && libevdev_has_event_code(dev, t, c))
1580  *        val = libevdev_get_event_value(dev, t, c);
1581  * @endcode
1582  *
1583  * @param dev The evdev device, already initialized with libevdev_set_fd()
1584  * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
1585  * @param code The event code to query for, one of ABS_X, REL_X, etc.
1586  * @param[out] value The current value of this axis returned.
1587  *
1588  * @return If the device supports this event type and code, the return value is
1589  * non-zero and value is set to the current value of this axis. Otherwise,
1590  * 0 is returned and value is unmodified.
1591  *
1592  * @note This function is signal-safe.
1593  * @note The value for ABS_MT_ events is undefined, use
1594  * libevdev_fetch_slot_value() instead
1595  *
1596  * @see libevdev_fetch_slot_value
1597  */
1598 int libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value);
1599 
1600 /**
1601  * @ingroup mt
1602  *
1603  * Return the current value of the code for the given slot.
1604  *
1605  * The return value is undefined for a slot exceeding the available slots on
1606  * the device, for a code that is not in the permitted ABS_MT range or for a
1607  * device that does not have slots.
1608  *
1609  * @param dev The evdev device, already initialized with libevdev_set_fd()
1610  * @param slot The numerical slot number, must be smaller than the total number
1611  * of slots on this device
1612  * @param code The event code to query for, one of ABS_MT_POSITION_X, etc.
1613  *
1614  * @note This function is signal-safe.
1615  * @note The value for events other than ABS_MT_ is undefined, use
1616  * libevdev_fetch_value() instead
1617  *
1618  * @see libevdev_get_event_value
1619  */
1620 int libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code);
1621 
1622 /**
1623  * @ingroup kernel
1624  *
1625  * Set the value for a given code for the given slot.
1626  *
1627  * This is a local modification only affecting only this representation of
1628  * this device. A future call to libevdev_get_slot_value() will return this
1629  * value, unless the value was overwritten by an event.
1630  *
1631  * This function does not set event values for axes outside the ABS_MT range,
1632  * use libevdev_set_event_value() instead.
1633  *
1634  * @param dev The evdev device, already initialized with libevdev_set_fd()
1635  * @param slot The numerical slot number, must be smaller than the total number
1636  * of slots on this device
1637  * @param code The event code to set the value for, one of ABS_MT_POSITION_X, etc.
1638  * @param value The new value to set
1639  *
1640  * @return 0 on success, or -1 on failure.
1641  * @retval -1
1642  * - the device does not have the event code enabled, or
1643  * - the code is outside the allowed limits for multitouch events, or
1644  * - the slot number is outside the limits for this device, or
1645  * - the device does not support multitouch events.
1646  *
1647  * @see libevdev_set_event_value
1648  * @see libevdev_get_slot_value
1649  */
1650 int libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value);
1651 
1652 /**
1653  * @ingroup mt
1654  *
1655  * Fetch the current value of the code for the given slot. This is a shortcut for
1656  *
1657  * @code
1658  *   if (libevdev_has_event_type(dev, EV_ABS) &&
1659  *       libevdev_has_event_code(dev, EV_ABS, c) &&
1660  *       slot < device->number_of_slots)
1661  *       val = libevdev_get_slot_value(dev, slot, c);
1662  * @endcode
1663  *
1664  * @param dev The evdev device, already initialized with libevdev_set_fd()
1665  * @param slot The numerical slot number, must be smaller than the total number
1666  * of slots on this * device
1667  * @param[out] value The current value of this axis returned.
1668  *
1669  * @param code The event code to query for, one of ABS_MT_POSITION_X, etc.
1670  * @return If the device supports this event code, the return value is
1671  * non-zero and value is set to the current value of this axis. Otherwise, or
1672  * if the event code is not an ABS_MT_* event code, 0 is returned and value
1673  * is unmodified.
1674  *
1675  * @note This function is signal-safe.
1676  */
1677 int libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value);
1678 
1679 /**
1680  * @ingroup mt
1681  *
1682  * Get the number of slots supported by this device.
1683  *
1684  * @param dev The evdev device, already initialized with libevdev_set_fd()
1685  *
1686  * @return The number of slots supported, or -1 if the device does not provide
1687  * any slots
1688  *
1689  * @note A device may provide ABS_MT_SLOT but a total number of 0 slots. Hence
1690  * the return value of -1 for "device does not provide slots at all"
1691  */
1692 int libevdev_get_num_slots(const struct libevdev *dev);
1693 
1694 /**
1695  * @ingroup mt
1696  *
1697  * Get the currently active slot. This may differ from the value
1698  * an ioctl may return at this time as events may have been read off the fd
1699  * since changing the slot value but those events are still in the buffer
1700  * waiting to be processed. The returned value is the value a caller would
1701  * see if it were to process events manually one-by-one.
1702  *
1703  * @param dev The evdev device, already initialized with libevdev_set_fd()
1704  *
1705  * @return the currently active slot (logically)
1706  *
1707  * @note This function is signal-safe.
1708  */
1709 int libevdev_get_current_slot(const struct libevdev *dev);
1710 
1711 /**
1712  * @ingroup kernel
1713  *
1714  * Change the minimum for the given EV_ABS event code, if the code exists.
1715  * This function has no effect if libevdev_has_event_code() returns false for
1716  * this code.
1717  *
1718  * @param dev The evdev device, already initialized with libevdev_set_fd()
1719  * @param code One of ABS_X, ABS_Y, ...
1720  * @param val The new minimum for this axis
1721  */
1722 void libevdev_set_abs_minimum(struct libevdev *dev, unsigned int code, int val);
1723 
1724 /**
1725  * @ingroup kernel
1726  *
1727  * Change the maximum for the given EV_ABS event code, if the code exists.
1728  * This function has no effect if libevdev_has_event_code() returns false for
1729  * this code.
1730  *
1731  * @param dev The evdev device, already initialized with libevdev_set_fd()
1732  * @param code One of ABS_X, ABS_Y, ...
1733  * @param val The new maxium for this axis
1734  */
1735 void libevdev_set_abs_maximum(struct libevdev *dev, unsigned int code, int val);
1736 
1737 /**
1738  * @ingroup kernel
1739  *
1740  * Change the fuzz for the given EV_ABS event code, if the code exists.
1741  * This function has no effect if libevdev_has_event_code() returns false for
1742  * this code.
1743  *
1744  * @param dev The evdev device, already initialized with libevdev_set_fd()
1745  * @param code One of ABS_X, ABS_Y, ...
1746  * @param val The new fuzz for this axis
1747  */
1748 void libevdev_set_abs_fuzz(struct libevdev *dev, unsigned int code, int val);
1749 
1750 /**
1751  * @ingroup kernel
1752  *
1753  * Change the flat for the given EV_ABS event code, if the code exists.
1754  * This function has no effect if libevdev_has_event_code() returns false for
1755  * this code.
1756  *
1757  * @param dev The evdev device, already initialized with libevdev_set_fd()
1758  * @param code One of ABS_X, ABS_Y, ...
1759  * @param val The new flat for this axis
1760  */
1761 void libevdev_set_abs_flat(struct libevdev *dev, unsigned int code, int val);
1762 
1763 /**
1764  * @ingroup kernel
1765  *
1766  * Change the resolution for the given EV_ABS event code, if the code exists.
1767  * This function has no effect if libevdev_has_event_code() returns false for
1768  * this code.
1769  *
1770  * @param dev The evdev device, already initialized with libevdev_set_fd()
1771  * @param code One of ABS_X, ABS_Y, ...
1772  * @param val The new axis resolution
1773  */
1774 void libevdev_set_abs_resolution(struct libevdev *dev, unsigned int code, int val);
1775 
1776 /**
1777  * @ingroup kernel
1778  *
1779  * Change the abs info for the given EV_ABS event code, if the code exists.
1780  * This function has no effect if libevdev_has_event_code() returns false for
1781  * this code.
1782  *
1783  * @param dev The evdev device, already initialized with libevdev_set_fd()
1784  * @param code One of ABS_X, ABS_Y, ...
1785  * @param abs The new absolute axis data (min, max, fuzz, flat, resolution)
1786  */
1787 void libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs);
1788 
1789 /**
1790  * @ingroup kernel
1791  *
1792  * Forcibly enable an event type on this device, even if the underlying
1793  * device does not support it. While this cannot make the device actually
1794  * report such events, it will now return true for libevdev_has_event_type().
1795  *
1796  * This is a local modification only affecting only this representation of
1797  * this device.
1798  *
1799  * @param dev The evdev device, already initialized with libevdev_set_fd()
1800  * @param type The event type to enable (EV_ABS, EV_KEY, ...)
1801  *
1802  * @return 0 on success or -1 otherwise
1803  *
1804  * @see libevdev_has_event_type
1805  */
1806 int libevdev_enable_event_type(struct libevdev *dev, unsigned int type);
1807 
1808 /**
1809  * @ingroup kernel
1810  *
1811  * Forcibly disable an event type on this device, even if the underlying
1812  * device provides it. This effectively mutes the respective set of
1813  * events. libevdev will filter any events matching this type and none will
1814  * reach the caller. libevdev_has_event_type() will return false for this
1815  * type.
1816  *
1817  * In most cases, a caller likely only wants to disable a single code, not
1818  * the whole type. Use libevdev_disable_event_code() for that.
1819  *
1820  * Disabling EV_SYN will not work. Don't shoot yourself in the foot.
1821  * It hurts.
1822  *
1823  * This is a local modification only affecting only this representation of
1824  * this device.
1825  *
1826  * @param dev The evdev device, already initialized with libevdev_set_fd()
1827  * @param type The event type to disable (EV_ABS, EV_KEY, ...)
1828  *
1829  * @return 0 on success or -1 otherwise
1830  *
1831  * @see libevdev_has_event_type
1832  * @see libevdev_disable_event_type
1833  */
1834 int libevdev_disable_event_type(struct libevdev *dev, unsigned int type);
1835 
1836 /**
1837  * @ingroup kernel
1838  *
1839  * Forcibly enable an event code on this device, even if the underlying
1840  * device does not support it. While this cannot make the device actually
1841  * report such events, it will now return true for libevdev_has_event_code().
1842  *
1843  * The last argument depends on the type and code:
1844  * - If type is EV_ABS, data must be a pointer to a struct input_absinfo
1845  *   containing the data for this axis.
1846  * - If type is EV_REP, data must be a pointer to a int containing the data
1847  *   for this axis
1848  * - For all other types, the argument must be NULL.
1849  *
1850  * This function calls libevdev_enable_event_type() if necessary.
1851  *
1852  * This is a local modification only affecting only this representation of
1853  * this device.
1854  *
1855  * If this function is called with a type of EV_ABS and EV_REP on a device
1856  * that already has the given event code enabled, the values in data
1857  * overwrite the previous values.
1858  *
1859  * @param dev The evdev device, already initialized with libevdev_set_fd()
1860  * @param type The event type to enable (EV_ABS, EV_KEY, ...)
1861  * @param code The event code to enable (ABS_X, REL_X, etc.)
1862  * @param data If type is EV_ABS, data points to a struct input_absinfo. If type is EV_REP, data
1863  * points to an integer. Otherwise, data must be NULL.
1864  *
1865  * @return 0 on success or -1 otherwise
1866  *
1867  * @see libevdev_enable_event_type
1868  */
1869 int libevdev_enable_event_code(struct libevdev *dev, unsigned int type, unsigned int code, const void *data);
1870 
1871 /**
1872  * @ingroup kernel
1873  *
1874  * Forcibly disable an event code on this device, even if the underlying
1875  * device provides it. This effectively mutes the respective set of
1876  * events. libevdev will filter any events matching this type and code and
1877  * none will reach the caller. libevdev_has_event_code() will return false for
1878  * this code.
1879  *
1880  * Disabling all event codes for a given type will not disable the event
1881  * type. Use libevdev_disable_event_type() for that.
1882  *
1883  * This is a local modification only affecting only this representation of
1884  * this device.
1885  *
1886  * Disabling codes of type EV_SYN will not work. Don't shoot yourself in the
1887  * foot. It hurts.
1888  *
1889  * @param dev The evdev device, already initialized with libevdev_set_fd()
1890  * @param type The event type to disable (EV_ABS, EV_KEY, ...)
1891  * @param code The event code to disable (ABS_X, REL_X, etc.)
1892  *
1893  * @return 0 on success or -1 otherwise
1894  *
1895  * @see libevdev_has_event_code
1896  * @see libevdev_disable_event_type
1897  */
1898 int libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code);
1899 
1900 /**
1901  * @ingroup kernel
1902  *
1903  * Set the device's EV_ABS axis to the value defined in the abs
1904  * parameter. This will be written to the kernel.
1905  *
1906  * @param dev The evdev device, already initialized with libevdev_set_fd()
1907  * @param code The EV_ABS event code to modify, one of ABS_X, ABS_Y, etc.
1908  * @param abs Axis info to set the kernel axis to
1909  *
1910  * @return 0 on success, or a negative errno on failure
1911  *
1912  * @see libevdev_enable_event_code
1913  */
1914 int libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs);
1915 
1916 /**
1917  * @ingroup kernel
1918  */
1919 enum libevdev_led_value {
1920 	LIBEVDEV_LED_ON = 3, /**< Turn the LED on */
1921 	LIBEVDEV_LED_OFF = 4 /**< Turn the LED off */
1922 };
1923 
1924 /**
1925  * @ingroup kernel
1926  *
1927  * Turn an LED on or off. Convenience function, if you need to modify multiple
1928  * LEDs simultaneously, use libevdev_kernel_set_led_values() instead.
1929  *
1930  * @note enabling an LED requires write permissions on the device's file descriptor.
1931  *
1932  * @param dev The evdev device, already initialized with libevdev_set_fd()
1933  * @param code The EV_LED event code to modify, one of LED_NUML, LED_CAPSL, ...
1934  * @param value Specifies whether to turn the LED on or off
1935  * @return 0 on success, or a negative errno on failure
1936  */
1937 int libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value);
1938 
1939 /**
1940  * @ingroup kernel
1941  *
1942  * Turn multiple LEDs on or off simultaneously. This function expects a pair
1943  * of LED codes and values to set them to, terminated by a -1. For example, to
1944  * switch the NumLock LED on but the CapsLock LED off, use:
1945  *
1946  * @code
1947  *     libevdev_kernel_set_led_values(dev, LED_NUML, LIBEVDEV_LED_ON,
1948  *                                         LED_CAPSL, LIBEVDEV_LED_OFF,
1949  *                                         -1);
1950  * @endcode
1951  *
1952  * If any LED code or value is invalid, this function returns -EINVAL and no
1953  * LEDs are modified.
1954  *
1955  * @note enabling an LED requires write permissions on the device's file descriptor.
1956  *
1957  * @param dev The evdev device, already initialized with libevdev_set_fd()
1958  * @param ... A pair of LED_* event codes and libevdev_led_value_t, followed by
1959  * -1 to terminate the list.
1960  * @return 0 on success, or a negative errno on failure
1961  */
1962 int libevdev_kernel_set_led_values(struct libevdev *dev, ...);
1963 
1964 /**
1965  * @ingroup kernel
1966  *
1967  * Set the clock ID to be used for timestamps. Further events from this device
1968  * will report an event time based on the given clock.
1969  *
1970  * This is a modification only affecting this representation of
1971  * this device.
1972  *
1973  * @param dev The evdev device, already initialized with libevdev_set_fd()
1974  * @param clockid The clock to use for future events. Permitted values
1975  * are CLOCK_MONOTONIC and CLOCK_REALTIME (the default).
1976  * @return 0 on success, or a negative errno on failure
1977  */
1978 int libevdev_set_clock_id(struct libevdev *dev, int clockid);
1979 
1980 /**
1981  * @ingroup misc
1982  *
1983  * Helper function to check if an event is of a specific type. This is
1984  * virtually the same as:
1985  *
1986  *      ev->type == type
1987  *
1988  * with the exception that some sanity checks are performed to ensure type
1989  * is valid.
1990  *
1991  * @note The ranges for types are compiled into libevdev. If the kernel
1992  * changes the max value, libevdev will not automatically pick these up.
1993  *
1994  * @param ev The input event to check
1995  * @param type Input event type to compare the event against (EV_REL, EV_ABS,
1996  * etc.)
1997  *
1998  * @return 1 if the event type matches the given type, 0 otherwise (or if
1999  * type is invalid)
2000  */
2001 int libevdev_event_is_type(const struct input_event *ev, unsigned int type);
2002 
2003 /**
2004  * @ingroup misc
2005  *
2006  * Helper function to check if an event is of a specific type and code. This
2007  * is virtually the same as:
2008  *
2009  *      ev->type == type && ev->code == code
2010  *
2011  * with the exception that some sanity checks are performed to ensure type and
2012  * code are valid.
2013  *
2014  * @note The ranges for types and codes are compiled into libevdev. If the kernel
2015  * changes the max value, libevdev will not automatically pick these up.
2016  *
2017  * @param ev The input event to check
2018  * @param type Input event type to compare the event against (EV_REL, EV_ABS,
2019  * etc.)
2020  * @param code Input event code to compare the event against (ABS_X, REL_X,
2021  * etc.)
2022  *
2023  * @return 1 if the event type matches the given type and code, 0 otherwise
2024  * (or if type/code are invalid)
2025  */
2026 int libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code);
2027 
2028 /**
2029  * @ingroup misc
2030  *
2031  * @param type The event type to return the name for.
2032  *
2033  * @return The name of the given event type (e.g. EV_ABS) or NULL for an
2034  * invalid type
2035  *
2036  * @note The list of names is compiled into libevdev. If the kernel adds new
2037  * defines for new event types, libevdev will not automatically pick these up.
2038  */
2039 const char * libevdev_event_type_get_name(unsigned int type);
2040 /**
2041  * @ingroup misc
2042  *
2043  * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
2044  * @param code The event code to return the name for (e.g. ABS_X)
2045  *
2046  * @return The name of the given event code (e.g. ABS_X) or NULL for an
2047  * invalid type or code
2048  *
2049  * @note The list of names is compiled into libevdev. If the kernel adds new
2050  * defines for new event codes, libevdev will not automatically pick these up.
2051  */
2052 const char * libevdev_event_code_get_name(unsigned int type, unsigned int code);
2053 
2054 /**
2055  * @ingroup misc
2056  *
2057  * This function resolves the event value for a code.
2058  *
2059  * For almost all event codes this will return NULL as the value is just a
2060  * numerical value. As of kernel 4.17, the only event code that will return
2061  * a non-NULL value is EV_ABS/ABS_MT_TOOL_TYPE.
2062  *
2063  * @param type The event type for the value to query (EV_ABS, etc.)
2064  * @param code The event code for the value to query (e.g. ABS_MT_TOOL_TYPE)
2065  * @param value The event value to return the name for (e.g. MT_TOOL_PALM)
2066  *
2067  * @return The name of the given event value (e.g. MT_TOOL_PALM) or NULL for
2068  * an invalid type or code or NULL for an axis that has numerical values
2069  * only.
2070  *
2071  * @note The list of names is compiled into libevdev. If the kernel adds new
2072  * defines for new event values, libevdev will not automatically pick these up.
2073  */
2074 const char * libevdev_event_value_get_name(unsigned int type,
2075 					   unsigned int code,
2076 					   int value);
2077 /**
2078  * @ingroup misc
2079  *
2080  * @param prop The input prop to return the name for (e.g. INPUT_PROP_BUTTONPAD)
2081  *
2082  * @return The name of the given input prop (e.g. INPUT_PROP_BUTTONPAD) or NULL for an
2083  * invalid property
2084  *
2085  * @note The list of names is compiled into libevdev. If the kernel adds new
2086  * defines for new properties libevdev will not automatically pick these up.
2087  * @note On older kernels input properties may not be defined and
2088  * libevdev_property_get_name() will always return NULL
2089  */
2090 const char* libevdev_property_get_name(unsigned int prop);
2091 
2092 /**
2093  * @ingroup misc
2094  *
2095  * @param type The event type to return the maximum for (EV_ABS, EV_REL, etc.). No max is defined for
2096  * EV_SYN.
2097  *
2098  * @return The max value defined for the given event type, e.g. ABS_MAX for a type of EV_ABS, or -1
2099  * for an invalid type.
2100  *
2101  * @note The max value is compiled into libevdev. If the kernel changes the
2102  * max value, libevdev will not automatically pick these up.
2103  */
2104 int libevdev_event_type_get_max(unsigned int type);
2105 
2106 /**
2107  * @ingroup misc
2108  *
2109  * Look up an event-type by its name. Event-types start with "EV_" followed by
2110  * the name (eg., "EV_ABS"). The "EV_" prefix must be included in the name. It
2111  * returns the constant assigned to the event-type or -1 if not found.
2112  *
2113  * @param name A non-NULL string describing an input-event type ("EV_KEY",
2114  * "EV_ABS", ...), zero-terminated.
2115  *
2116  * @return The given type constant for the passed name or -1 if not found.
2117  *
2118  * @note EV_MAX is also recognized.
2119  */
2120 int libevdev_event_type_from_name(const char *name);
2121 
2122 /**
2123  * @ingroup misc
2124  *
2125  * Look up an event-type by its name. Event-types start with "EV_" followed by
2126  * the name (eg., "EV_ABS"). The "EV_" prefix must be included in the name. It
2127  * returns the constant assigned to the event-type or -1 if not found.
2128  *
2129  * @param name A non-NULL string describing an input-event type ("EV_KEY",
2130  * "EV_ABS", ...).
2131  * @param len The length of the passed string excluding any terminating 0
2132  * character.
2133  *
2134  * @return The given type constant for the passed name or -1 if not found.
2135  *
2136  * @note EV_MAX is also recognized.
2137  */
2138 int libevdev_event_type_from_name_n(const char *name, size_t len);
2139 
2140 /**
2141  * @ingroup misc
2142  *
2143  * Look up an event code by its type and name. Event codes start with a fixed
2144  * prefix followed by their name (eg., "ABS_X"). The prefix must be included in
2145  * the name. It returns the constant assigned to the event code or -1 if not
2146  * found.
2147  *
2148  * You have to pass the event type where to look for the name. For instance, to
2149  * resolve "ABS_X" you need to pass EV_ABS as type and "ABS_X" as string.
2150  * Supported event codes are codes starting with SYN_, KEY_, BTN_, REL_, ABS_,
2151  * MSC_, SND_, SW_, LED_, REP_, FF_.
2152  *
2153  * @param type The event type (EV_* constant) where to look for the name.
2154  * @param name A non-NULL string describing an input-event code ("KEY_A",
2155  * "ABS_X", "BTN_Y", ...), zero-terminated.
2156  *
2157  * @return The given code constant for the passed name or -1 if not found.
2158  */
2159 int libevdev_event_code_from_name(unsigned int type, const char *name);
2160 
2161 /**
2162  * @ingroup misc
2163  *
2164  * Look up an event code by its type and name. Event codes start with a fixed
2165  * prefix followed by their name (eg., "ABS_X"). The prefix must be included in
2166  * the name. It returns the constant assigned to the event code or -1 if not
2167  * found.
2168  *
2169  * You have to pass the event type where to look for the name. For instance, to
2170  * resolve "ABS_X" you need to pass EV_ABS as type and "ABS_X" as string.
2171  * Supported event codes are codes starting with SYN_, KEY_, BTN_, REL_, ABS_,
2172  * MSC_, SND_, SW_, LED_, REP_, FF_.
2173  *
2174  * @param type The event type (EV_* constant) where to look for the name.
2175  * @param name A non-NULL string describing an input-event code ("KEY_A",
2176  * "ABS_X", "BTN_Y", ...).
2177  * @param len The length of the string in @p name excluding any terminating 0
2178  * character.
2179  *
2180  * @return The given code constant for the name or -1 if not found.
2181  */
2182 int libevdev_event_code_from_name_n(unsigned int type, const char *name,
2183 				    size_t len);
2184 
2185 /**
2186  * @ingroup misc
2187  *
2188  * Look up an event value by its type, code and name. Event values start
2189  * with a fixed prefix followed by their name (eg., "MT_TOOL_PALM"). The
2190  * prefix must be included in the name. It returns the constant assigned
2191  * to the event code or -1 if not found.
2192  *
2193  * You have to pass the event type and code where to look for the name. For
2194  * instance, to resolve "MT_TOOL_PALM" you need to pass EV_ABS as type,
2195  * ABS_MT_TOOL_TYPE as code and "MT_TOOL_PALM" as string.
2196  *
2197  * As of kernel 4.17, only EV_ABS/ABS_MT_TOOL_TYPE support name resolution.
2198  *
2199  * @param type The event type (EV_* constant) where to look for the name.
2200  * @param code The event code (ABS_* constant) where to look for the name.
2201  * @param name A non-NULL string describing an input-event value
2202  * ("MT_TOOL_TYPE", ...)
2203  *
2204  * @return The given value constant for the name or -1 if not found.
2205  */
2206 int libevdev_event_value_from_name(unsigned int type, unsigned int code,
2207 				   const char *name);
2208 
2209 /**
2210  * @ingroup misc
2211  *
2212  * Look up an event type for a  event code name. For example, the name
2213  * "ABS_Y" returns EV_ABS. For the lookup to succeed, the name must be
2214  * unique, which is the case for all defines as of kernel 5.0 and likely to
2215  * be the case in the future.
2216  *
2217  * This is equivalent to libevdev_event_type_from_name() but takes the code
2218  * name instead of the type name.
2219  *
2220  * @param name A non-NULL string describing an input-event value
2221  * ("ABS_X", "REL_Y", "KEY_A", ...)
2222  *
2223  * @return The given event code for the name or -1 if not found.
2224  */
2225 int
2226 libevdev_event_type_from_code_name(const char *name);
2227 
2228 /**
2229  * @ingroup misc
2230  *
2231  * Look up an event type for a  event code name. For example, the name
2232  * "ABS_Y" returns EV_ABS. For the lookup to succeed, the name must be
2233  * unique, which is the case for all defines as of kernel 5.0 and likely to
2234  * be the case in the future.
2235  *
2236  * This is equivalent to libevdev_event_type_from_name_n() but takes the code
2237  * name instead of the type name.
2238  *
2239  * @param name A non-NULL string describing an input-event value
2240  * ("ABS_X", "REL_Y", "KEY_A", ...)
2241  * @param len The length of the passed string excluding any terminating 0
2242  * character.
2243  *
2244  * @return The given event code for the name or -1 if not found.
2245  */
2246 int
2247 libevdev_event_type_from_code_name_n(const char *name, size_t len);
2248 
2249 /**
2250  * @ingroup misc
2251  *
2252  * Look up an event code by its name. For example, the name "ABS_Y"
2253  * returns 1. For the lookup to succeed, the name must be unique, which is
2254  * the case for all defines as of kernel 5.0 and likely to be the case in
2255  * the future.
2256  *
2257  * This is equivalent to libevdev_event_code_from_name() without the need
2258  * for knowing the event type.
2259  *
2260  * @param name A non-NULL string describing an input-event value
2261  * ("ABS_X", "REL_Y", "KEY_A", ...)
2262  *
2263  * @return The given event code for the name or -1 if not found.
2264  */
2265 int
2266 libevdev_event_code_from_code_name(const char *name);
2267 
2268 /**
2269  * @ingroup misc
2270  *
2271  * Look up an event code by its name. For example, the name "ABS_Y"
2272  * returns 1. For the lookup to succeed, the name must be unique, which is
2273  * the case for all defines as of kernel 5.0 and likely to be the case in
2274  * the future.
2275  *
2276  * This is equivalent to libevdev_event_code_from_name_n() without the need
2277  * for knowing the event type.
2278  *
2279  * @param name A non-NULL string describing an input-event value
2280  * ("ABS_X", "REL_Y", "KEY_A", ...)
2281  * @param len The length of the passed string excluding any terminating 0
2282  * character.
2283  *
2284  * @return The given event code for the name or -1 if not found.
2285  */
2286 int
2287 libevdev_event_code_from_code_name_n(const char *name, size_t len);
2288 
2289 /**
2290  * @ingroup misc
2291  *
2292  * Look up an event value by its type, code and name. Event values start
2293  * with a fixed prefix followed by their name (eg., "MT_TOOL_PALM"). The
2294  * prefix must be included in the name. It returns the constant assigned
2295  * to the event code or -1 if not found.
2296  *
2297  * You have to pass the event type and code where to look for the name. For
2298  * instance, to resolve "MT_TOOL_PALM" you need to pass EV_ABS as type,
2299  * ABS_MT_TOOL_TYPE as code and "MT_TOOL_PALM" as string.
2300  *
2301  * As of kernel 4.17, only EV_ABS/ABS_MT_TOOL_TYPE support name resolution.
2302  *
2303  * @param type The event type (EV_* constant) where to look for the name.
2304  * @param code The event code (ABS_* constant) where to look for the name.
2305  * @param name A non-NULL string describing an input-event value
2306  * ("MT_TOOL_TYPE", ...)
2307  * @param len The length of the string in @p name excluding any terminating 0
2308  * character.
2309  *
2310  * @return The given value constant for the name or -1 if not found.
2311  */
2312 int libevdev_event_value_from_name_n(unsigned int type, unsigned int code,
2313 				     const char *name, size_t len);
2314 
2315 /**
2316  * @ingroup misc
2317  *
2318  * Look up an input property by its name. Properties start with the fixed
2319  * prefix "INPUT_PROP_" followed by their name (eg., "INPUT_PROP_POINTER").
2320  * The prefix must be included in the name. It returns the constant assigned
2321  * to the property or -1 if not found.
2322  *
2323  * @param name A non-NULL string describing an input property
2324  *
2325  * @return The given code constant for the name or -1 if not found.
2326  */
2327 int libevdev_property_from_name(const char *name);
2328 
2329 /**
2330  * @ingroup misc
2331  *
2332  * Look up an input property by its name. Properties start with the fixed
2333  * prefix "INPUT_PROP_" followed by their name (eg., "INPUT_PROP_POINTER").
2334  * The prefix must be included in the name. It returns the constant assigned
2335  * to the property or -1 if not found.
2336  *
2337  * @param name A non-NULL string describing an input property
2338  * @param len The length of the string in @p name excluding any terminating 0
2339  * character.
2340  *
2341  * @return The given code constant for the name or -1 if not found.
2342  */
2343 int libevdev_property_from_name_n(const char *name, size_t len);
2344 
2345 /**
2346  * @ingroup bits
2347  *
2348  * Get the repeat delay and repeat period values for this device. This
2349  * function is a convenience function only, EV_REP is supported by
2350  * libevdev_get_event_value().
2351  *
2352  * @param dev The evdev device, already initialized with libevdev_set_fd()
2353  * @param delay If not null, set to the repeat delay value
2354  * @param period If not null, set to the repeat period value
2355  *
2356  * @return 0 on success, -1 if this device does not have repeat settings.
2357  *
2358  * @note This function is signal-safe
2359  *
2360  * @see libevdev_get_event_value
2361  */
2362 int libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period);
2363 
2364 /********* DEPRECATED SECTION *********/
2365 #if defined(__GNUC__) && __GNUC__ >= 4
2366 #define LIBEVDEV_DEPRECATED __attribute__ ((deprecated))
2367 #else
2368 #define LIBEVDEV_DEPRECATED
2369 #endif
2370 
2371 #ifdef __cplusplus
2372 }
2373 #endif
2374 
2375 #endif /* LIBEVDEV_H */
2376