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