Merge the device added/removed events into a single device notify event
[platform/upstream/libinput.git] / src / libinput.h
1 /*
2  * Copyright © 2013 Jonas Ådahl
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #ifndef LIBINPUT_H
24 #define LIBINPUT_H
25
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <libudev.h>
29
30 /**
31  * @mainpage
32  * libinput is a generic input device handling library. It abstracts
33  * commonly-used concepts such as keyboard, pointer and touchpad handling
34  * behind an API.
35  */
36
37 /**
38  * @ingroup fixed_point
39  *
40  * libinput 24.8 fixed point real number.
41  */
42 typedef int32_t li_fixed_t;
43
44 /**
45  * @ingroup device
46  *
47  * Capabilities on a device. A device may have one or more capabilities
48  * at a time, and capabilities may appear or disappear during the
49  * lifteime of the device.
50  */
51 enum libinput_device_capability {
52         LIBINPUT_DEVICE_CAP_KEYBOARD = 0,
53         LIBINPUT_DEVICE_CAP_POINTER = 1,
54         LIBINPUT_DEVICE_CAP_TOUCH = 2
55 };
56
57 /**
58  * @ingroup device
59  *
60  * Logical state of a key. Note that the logical state may not represent
61  * the physical state of the key.
62  */
63 enum libinput_keyboard_key_state {
64         LIBINPUT_KEYBOARD_KEY_STATE_RELEASED = 0,
65         LIBINPUT_KEYBOARD_KEY_STATE_PRESSED = 1
66 };
67
68 /**
69  * @ingroup device
70  *
71  * Mask reflecting LEDs on a device.
72  */
73 enum libinput_led {
74         LIBINPUT_LED_NUM_LOCK = (1 << 0),
75         LIBINPUT_LED_CAPS_LOCK = (1 << 1),
76         LIBINPUT_LED_SCROLL_LOCK = (1 << 2)
77 };
78
79 /**
80  * @ingroup device
81  *
82  * Logical state of a physical button. Note that the logical state may not
83  * represent the physical state of the button.
84  */
85 enum libinput_pointer_button_state {
86         LIBINPUT_POINTER_BUTTON_STATE_RELEASED = 0,
87         LIBINPUT_POINTER_BUTTON_STATE_PRESSED = 1
88 };
89
90
91 /**
92  * @ingroup device
93  *
94  * Axes on a device that are not x or y coordinates.
95  */
96 enum libinput_pointer_axis {
97         LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL = 0,
98         LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL = 1
99 };
100
101 /**
102  * @ingroup device
103  *
104  * Logical touch state of a touch point. A touch point usually follows the
105  * sequence down, motion, up, with the number of motion events being zero or
106  * greater. If a touch point was used for gesture interpretation internally
107  * and will not generate any further events, the touchpoint is cancelled.
108  *
109  * A frame event is set after a set of touchpoints that constitute one
110  * logical set of points at a sampling point.
111  */
112 enum libinput_touch_type {
113         LIBINPUT_TOUCH_TYPE_DOWN = 0,
114         LIBINPUT_TOUCH_TYPE_UP = 1,
115         LIBINPUT_TOUCH_TYPE_MOTION = 2,
116         LIBINPUT_TOUCH_TYPE_FRAME = 3,
117         LIBINPUT_TOUCH_TYPE_CANCEL = 4
118 };
119
120 /**
121  * @ingroup base
122  *
123  * Event type for events returned by libinput_get_event().
124  */
125 enum libinput_event_type {
126         LIBINPUT_EVENT_NONE = 0,
127         LIBINPUT_EVENT_ADDED_DEVICE,
128         LIBINPUT_EVENT_REMOVED_DEVICE,
129
130         LIBINPUT_EVENT_KEYBOARD_KEY = 300,
131
132         LIBINPUT_EVENT_POINTER_MOTION = 400,
133         LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
134         LIBINPUT_EVENT_POINTER_BUTTON,
135         LIBINPUT_EVENT_POINTER_AXIS,
136
137         LIBINPUT_EVENT_TOUCH_TOUCH = 500
138 };
139
140 struct libinput;
141 struct libinput_device;
142 struct libinput_seat;
143
144 struct libinput_event;
145 struct libinput_event_device_notify;
146 struct libinput_event_keyboard_key;
147 struct libinput_event_pointer_motion;
148 struct libinput_event_pointer_motion_absolute;
149 struct libinput_event_pointer_button;
150 struct libinput_event_pointer_axis;
151 struct libinput_event_touch_touch;
152
153 /**
154  * @defgroup fixed_point Fixed point utilities
155  */
156
157 /**
158  * @ingroup fixed_point
159  *
160  * Convert li_fixed_t to a double
161  *
162  * @param f fixed point number
163  * @return Converted double
164  */
165 static inline double
166 li_fixed_to_double (li_fixed_t f)
167 {
168         union {
169                 double d;
170                 int64_t i;
171         } u;
172
173         u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
174
175         return u.d - (3LL << 43);
176 }
177
178 /**
179  * @ingroup fixed_point
180  *
181  * Convert li_fixed_t to a int. The fraction part is discarded.
182  *
183  * @param f fixed point number
184  * @return Converted int
185  */
186 static inline int
187 li_fixed_to_int(li_fixed_t f)
188 {
189         return f / 256;
190 }
191
192 /**
193  * @defgroup event Acessing and destruction of events
194  */
195
196 /**
197  * @ingroup event
198  *
199  * Destroy the event.
200  *
201  * @param event An event retrieved by libinput_get_event().
202  */
203 void
204 libinput_event_destroy(struct libinput_event *event);
205
206 /**
207  * @ingroup event
208  *
209  * Get the type of the event.
210  *
211  * @param event An event retrieved by libinput_get_event().
212  */
213 enum libinput_event_type
214 libinput_event_get_type(struct libinput_event *event);
215
216 /**
217  * @ingroup event
218  *
219  * Get the libinput context from the event.
220  *
221  * @param event The libinput event
222  * @return The libinput context for this event.
223  */
224 struct libinput*
225 libinput_event_get_context(struct libinput_event *event);
226
227 /**
228  * @ingroup event
229  *
230  * Return the device associated with this event, if applicable. For device
231  * added/removed events this is the device added or removed. For all other
232  * device events, this is the device that generated the event.
233  *
234  * This device is not refcounted and its lifetime is that of the event. Use
235  * libinput_device_ref() before using the device outside of this scope.
236  *
237  * @return The device associated with this event
238  */
239
240 struct libinput_device*
241 libinput_event_get_device(struct libinput_event *event);
242
243 /**
244  * @defgroup event_keyboard_key Keyboard key event
245  */
246
247 uint32_t
248 libinput_event_keyboard_key_get_time(
249         struct libinput_event_keyboard_key *event);
250
251 uint32_t
252 libinput_event_keyboard_key_get_key(
253         struct libinput_event_keyboard_key *event);
254
255 enum libinput_keyboard_key_state
256 libinput_event_keyboard_key_get_state(
257         struct libinput_event_keyboard_key *event);
258
259 /**
260  * @defgroup event_pointer_motion Pointer motion event
261  */
262
263 uint32_t
264 libinput_event_pointer_motion_get_time(
265         struct libinput_event_pointer_motion *event);
266
267 li_fixed_t
268 libinput_event_pointer_motion_get_dx(
269         struct libinput_event_pointer_motion *event);
270
271 li_fixed_t
272 libinput_event_pointer_motion_get_dy(
273         struct libinput_event_pointer_motion *event);
274
275 /**
276  * @defgroup event_pointer_motion_absolute Absolute pointer motion event
277  */
278
279 uint32_t
280 libinput_event_pointer_motion_absolute_get_time(
281         struct libinput_event_pointer_motion_absolute *event);
282
283 li_fixed_t
284 libinput_event_pointer_motion_absolute_get_x(
285         struct libinput_event_pointer_motion_absolute *event);
286
287 li_fixed_t
288 libinput_event_pointer_motion_absolute_get_y(
289         struct libinput_event_pointer_motion_absolute *event);
290
291 /**
292  * @defgroup event_pointer_button Pointer button event
293  */
294
295 uint32_t
296 libinput_event_pointer_button_get_time(
297         struct libinput_event_pointer_button *event);
298
299 uint32_t
300 libinput_event_pointer_button_get_button(
301         struct libinput_event_pointer_button *event);
302
303 enum libinput_pointer_button_state
304 libinput_event_pointer_button_get_state(
305         struct libinput_event_pointer_button *event);
306
307 /**
308  * @defgroup event_pointer_axis Pointer axis event
309  */
310
311 uint32_t
312 libinput_event_pointer_axis_get_time(
313         struct libinput_event_pointer_axis *event);
314
315 enum libinput_pointer_axis
316 libinput_event_pointer_axis_get_axis(
317         struct libinput_event_pointer_axis *event);
318
319 li_fixed_t
320 libinput_event_pointer_axis_get_value(
321         struct libinput_event_pointer_axis *event);
322
323 /**
324  * @defgroup event_pointer_button Pointer button event
325  */
326
327 uint32_t
328 libinput_event_touch_touch_get_time(
329         struct libinput_event_touch_touch *event);
330
331 uint32_t
332 libinput_event_touch_touch_get_slot(
333         struct libinput_event_touch_touch *event);
334
335 li_fixed_t
336 libinput_event_touch_touch_get_x(
337         struct libinput_event_touch_touch *event);
338
339 li_fixed_t
340 libinput_event_touch_touch_get_y(
341         struct libinput_event_touch_touch *event);
342
343 enum libinput_touch_type
344 libinput_event_touch_touch_get_touch_type(
345         struct libinput_event_touch_touch *event);
346
347 /**
348  * @defgroup base Initialization and manipulation of libinput contexts
349  */
350
351 struct libinput_interface {
352         /**
353          * Open the device at the given path with the flags provided and
354          * return the fd.
355          *
356          * @param path The device path to open
357          * @param flags Flags as defined by open(2)
358          * @param user_data The user_data provided in
359          * libinput_create_from_udev()
360          *
361          * @return the file descriptor, or a negative errno on failure.
362          */
363         int (*open_restricted)(const char *path, int flags, void *user_data);
364         /**
365          * Close the file descriptor.
366          *
367          * @param fd The file descriptor to close
368          * @param user_data The user_data provided in
369          * libinput_create_from_udev()
370          */
371         void (*close_restricted)(int fd, void *user_data);
372
373         void (*get_current_screen_dimensions)(struct libinput_device *device,
374                                               int *width,
375                                               int *height,
376                                               void *user_data);
377 };
378
379 /**
380  * @ingroup base
381  *
382  * Create a new libinput context from udev, for input devices matching
383  * the given seat ID. New devices or devices removed will appear as events
384  * during libinput_dispatch.
385  *
386  * @param interface The callback interface
387  * @param user_data Caller-specific data passed to the various callback
388  * interfaces.
389  * @param udev An already initialized udev context
390  * @param seat_id A seat identifier. This string must not be NULL.
391  *
392  * @return An initialized libinput context, ready to handle events or NULL on
393  * error.
394  */
395 struct libinput *
396 libinput_create_from_udev(const struct libinput_interface *interface,
397                           void *user_data,
398                           struct udev *udev,
399                           const char *seat_id);
400 /**
401  * @ingroup base
402  *
403  * Create a new libinput context from the given path. This context
404  * represents one single device only, it will not respond to new devices
405  * being added and reading from the device after it was removed will fail.
406  *
407  * @param interface The callback interface
408  * @param user_data Caller-specific data passed to the various callback
409  * interfaces.
410  * @param path Path to an input device
411  *
412  * @return An initialized libinput context, ready to handle events or NULL on
413  * error.
414  */
415 struct libinput *
416 libinput_create_from_path(const struct libinput_interface *interface,
417                           void *user_data,
418                           const char *path);
419
420 /**
421  * @ingroup base
422  *
423  * libinput keeps a single file descriptor for all events. Call into
424  * libinput_dispatch() if any events become available on this fd.
425  *
426  * @return the file descriptor used to notify of pending events.
427  */
428 int
429 libinput_get_fd(struct libinput *libinput);
430
431 /**
432  * @ingroup base
433  *
434  * Main event dispatchment function. Reads events of the file descriptors
435  * and processes them internally. Use libinput_get_event() to retrieve the
436  * events.
437  *
438  * Dispatching does not necessarily queue libinput events.
439  *
440  * @param libinput A previously initialized libinput context
441  *
442  * @return 0 on success, or a negative errno on failure
443  */
444 int
445 libinput_dispatch(struct libinput *libinput);
446
447 /**
448  * @ingroup base
449  *
450  * Retrieve the next event from libinput's internal event queue.
451  *
452  * After handling the retrieved event, the caller must destroy it using
453  * libinput_event_destroy().
454  *
455  * @param libinput A previously initialized libinput context
456  * @return The next available event, or NULL if no event is available.
457  */
458 struct libinput_event *
459 libinput_get_event(struct libinput *libinput);
460
461 /**
462  * @ingroup base
463  *
464  * Return the type of the next event in the internal queue. This function
465  * does not pop the event off the queue and the next call to
466  * libinput_get_event() returns that event.
467  *
468  * @param libinput A previously initialized libinput context
469  * @return The event type of the next available event or LIBINPUT_EVENT_NONE
470  * if no event is availble.
471  */
472 enum libinput_event_type
473 libinput_next_event_type(struct libinput *libinput);
474
475 /**
476  * @ingroup base
477  *
478  * @param libinput A previously initialized libinput context
479  * @return the caller-specific data previously assigned in
480  * libinput_create_udev().
481  */
482 void *
483 libinput_get_user_data(struct libinput *libinput);
484
485 /**
486  * @ingroup base
487  *
488  * Resume a suspended libinput context. This re-enables device
489  * monitoring and adds existing devices.
490  *
491  * @param libinput A previously initialized libinput context
492  * @see libinput_suspend
493  *
494  * @return 0 on success or -1 on failure
495  */
496 int
497 libinput_resume(struct libinput *libinput);
498
499 /**
500  * @ingroup base
501  *
502  * Suspend monitoring for new devices and close existing devices.
503  * This all but terminates libinput but does keep the context
504  * valid to be resumed with libinput_resume().
505  *
506  * @param libinput A previously initialized libinput context
507  */
508 void
509 libinput_suspend(struct libinput *libinput);
510
511 /**
512  * @ingroup base
513  *
514  * Destroy the libinput context. After this, object references associated with
515  * the destroyed context are invalid and may not be interacted with.
516  *
517  * @param libinput A previously initialized libinput context
518  */
519 void
520 libinput_destroy(struct libinput *libinput);
521
522 /**
523  * @defgroup seat Initialization and manipulation of seats
524  *
525  * A seat has two identifiers, the physical name and the logical name. The
526  * physical name is summarized as the list of devices a process on the same
527  * physical seat has access to.
528  *
529  * The logical seat name is the seat name for a logical group of devices. A
530  * compositor may use that to create additonal seats as independent device
531  * sets. Alternatively, a compositor may limit itself to a single logical
532  * seat, leaving a second compositor to manage devices on the other logical
533  * seats.
534  *
535  * @code
536  * +---+--------+------------+------------------------+------------+
537  * |   | event0 |            |                        | log seat A |
538  * | K +--------+            |                        +------------+
539  * | e | event1 | phys seat0 |    libinput context 1  |            |
540  * | r +--------+            |                        | log seat B |
541  * | n | event2 |            |                        |            |
542  * | e +--------+------------+------------------------+------------+
543  * | l | event3 | phys seat1 |    libinput context 2  | log seat C |
544  * +---+--------+------------+------------------------+------------+
545  * @endcode
546  */
547
548 /**
549  * @ingroup seat
550  *
551  * Increase the refcount of the seat. A seat will be freed whenever the
552  * refcount reaches 0. This may happen during dispatch if the
553  * seat was removed from the system. A caller must ensure to reference
554  * the seat correctly to avoid dangling pointers.
555  *
556  * @param seat A previously obtained seat
557  */
558 void
559 libinput_seat_ref(struct libinput_seat *seat);
560
561 /**
562  * @ingroup seat
563  *
564  * Decrease the refcount of the seat. A seat will be freed whenever the
565  * refcount reaches 0. This may happen during dispatch if the
566  * seat was removed from the system. A caller must ensure to reference
567  * the seat correctly to avoid dangling pointers.
568  *
569  * @param seat A previously obtained seat
570  */
571 void
572 libinput_seat_unref(struct libinput_seat *seat);
573
574 /**
575  * @ingroup seat
576  *
577  * Set caller-specific data associated with this seat. libinput does
578  * not manage, look at, or modify this data. The caller must ensure the
579  * data is valid.
580  *
581  * @param seat A previously obtained seat
582  * @param user_data Caller-specific data pointer
583  * @see libinput_seat_get_user_data
584  */
585 void
586 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data);
587
588 /**
589  * @ingroup seat
590  *
591  * Get the caller-specific data associated with this seat, if any.
592  *
593  * @param seat A previously obtained seat
594  * @return Caller-specific data pointer or NULL if none was set
595  * @see libinput_seat_set_user_data
596  */
597 void *
598 libinput_seat_get_user_data(struct libinput_seat *seat);
599
600 /**
601  * @ingroup seat
602  *
603  * Return the physical name of the seat. For libinput contexts created from
604  * udev, this is always the same value as passed into
605  * libinput_create_from_udev() and all seats from that context will have the
606  * same physical name.
607  *
608  * The physical name of the seat is one that is usually set by the system or
609  * lower levels of the stack. In most cases, this is the base filter for
610  * devices - devices assigned to seats outside the current seat will not
611  * be available to the caller.
612  *
613  * @param seat A previously obtained seat
614  * @return the physical name of this seat
615  */
616 const char *
617 libinput_seat_get_physical_name(struct libinput_seat *seat);
618
619 /**
620  * @ingroup seat
621  *
622  * Return the logical name of the seat. This is an identifier to group sets
623  * of devices within the compositor.
624  *
625  * @param seat A previously obtained seat
626  * @return the logical name of this seat
627  */
628 const char *
629 libinput_seat_get_logical_name(struct libinput_seat *seat);
630
631 /**
632  * @defgroup device Initialization and manipulation of input devices
633  */
634
635 /**
636  * @ingroup device
637  *
638  * Increase the refcount of the input device. An input device will be freed
639  * whenever the refcount reaches 0. This may happen during dispatch if the
640  * device was removed from the system. A caller must ensure to reference
641  * the device correctly to avoid dangling pointers.
642  *
643  * @param device A previously obtained device
644  */
645 void
646 libinput_device_ref(struct libinput_device *device);
647
648 /**
649  * @ingroup device
650  *
651  * Decrease the refcount of the input device. An input device will be freed
652  * whenever the refcount reaches 0. This may happen during dispatch if the
653  * device was removed from the system. A caller must ensure to reference
654  * the device correctly to avoid dangling pointers.
655  *
656  * @param device A previously obtained device
657  */
658 void
659 libinput_device_unref(struct libinput_device *device);
660
661 /**
662  * @ingroup device
663  *
664  * Set caller-specific data associated with this input device. libinput does
665  * not manage, look at, or modify this data. The caller must ensure the
666  * data is valid.
667  *
668  * @param device A previously obtained device
669  * @param user_data Caller-specific data pointer
670  * @see libinput_device_get_user_data
671  */
672 void
673 libinput_device_set_user_data(struct libinput_device *device, void *user_data);
674
675 /**
676  * @ingroup device
677  *
678  * Get the caller-specific data associated with this input device, if any.
679  *
680  * @param device A previously obtained device
681  * @return Caller-specific data pointer or NULL if none was set
682  * @see libinput_device_set_user_data
683  */
684 void *
685 libinput_device_get_user_data(struct libinput_device *device);
686
687 /**
688  * @ingroup device
689  *
690  * Get the system name of the device.
691  *
692  * @param device A previously obtained device
693  * @return System name of the device
694  */
695 const char *
696 libinput_device_get_sysname(struct libinput_device *device);
697
698 /**
699  * @ingroup device
700  *
701  * A device may be mapped to a single output, or all available outputs. If a
702  * device is mapped to a single output only, a relative device may not move
703  * beyond the boundaries of this output. An absolute device has its input
704  * coordinates mapped to the extents of this output.
705  *
706  * @return the name of the output this device is mapped to, or NULL if no
707  * output is set
708  */
709 const char *
710 libinput_device_get_output_name(struct libinput_device *device);
711
712 /**
713  * @ingroup device
714  *
715  * Get the seat associated with this input device.
716  *
717  * @param device A previously obtained device
718  * @return The seat this input device belongs to
719  */
720 struct libinput_seat *
721 libinput_device_get_seat(struct libinput_device *device);
722
723 /**
724  * @ingroup device
725  *
726  * Update the LEDs on the device, if any. If the device does not have
727  * LEDs, or does not have one or more of the LEDs given in the mask, this
728  * function does nothing.
729  *
730  * @param device A previously obtained device
731  * @param leds A mask of the LEDs to set, or unset.
732  */
733 void
734 libinput_device_led_update(struct libinput_device *device,
735                            enum libinput_led leds);
736
737 /**
738  * @ingroup device
739  *
740  * Set the bitmask in keys to the bitmask of the keys present on the device
741  * (see linux/input.h), up to size characters.
742  *
743  * @param device A current input device
744  * @param keys An array filled with the bitmask for the keys
745  * @param size Size of the keys array
746  */
747 int
748 libinput_device_get_keys(struct libinput_device *device,
749                          char *keys, size_t size);
750
751 /**
752  * @ingroup device
753  *
754  * Apply the 3x3 transformation matrix to absolute device coordinates. This
755  * matrix has no effect on relative events.
756  *
757  * Given a 6-element array [a, b, c, d, e, f], the matrix is applied as
758  * @code
759  * [ a  b  c ]   [ x ]
760  * [ d  e  f ] * [ y ]
761  * [ 0  0  1 ]   [ 1 ]
762  * @endcode
763  */
764 void
765 libinput_device_calibrate(struct libinput_device *device,
766                           float calibration[6]);
767
768 /**
769  * @ingroup device
770  *
771  * Check if the given device has the specified capability
772  *
773  * @return 1 if the given device has the capability or 0 if not
774  */
775 int
776 libinput_device_has_capability(struct libinput_device *device,
777                                enum libinput_device_capability capability);
778
779 #endif /* LIBINPUT_H */