Add libinput_next_event_type()
[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_SEAT,
128         LIBINPUT_EVENT_REMOVED_SEAT,
129         LIBINPUT_EVENT_ADDED_DEVICE,
130         LIBINPUT_EVENT_REMOVED_DEVICE,
131
132         LIBINPUT_EVENT_KEYBOARD_KEY = 300,
133
134         LIBINPUT_EVENT_POINTER_MOTION = 400,
135         LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
136         LIBINPUT_EVENT_POINTER_BUTTON,
137         LIBINPUT_EVENT_POINTER_AXIS,
138
139         LIBINPUT_EVENT_TOUCH_TOUCH = 500,
140 };
141
142 struct libinput;
143 struct libinput_device;
144 struct libinput_seat;
145
146 union libinput_event_target {
147         struct libinput *libinput;
148         struct libinput_seat *seat;
149         struct libinput_device *device;
150 };
151
152 struct libinput_event;
153 struct libinput_event_added_seat;
154 struct libinput_event_removed_seat;
155 struct libinput_event_added_device;
156 struct libinput_event_removed_device;
157 struct libinput_event_keyboard_key;
158 struct libinput_event_pointer_motion;
159 struct libinput_event_pointer_motion_absolute;
160 struct libinput_event_pointer_button;
161 struct libinput_event_pointer_axis;
162 struct libinput_event_touch_touch;
163
164 /**
165  * @defgroup fixed_point Fixed point utilities
166  */
167
168 /**
169  * @ingroup fixed_point
170  *
171  * Convert li_fixed_t to a double
172  *
173  * @param f fixed point number
174  * @return Converted double
175  */
176 static inline double
177 li_fixed_to_double (li_fixed_t f)
178 {
179         union {
180                 double d;
181                 int64_t i;
182         } u;
183
184         u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
185
186         return u.d - (3LL << 43);
187 }
188
189 /**
190  * @ingroup fixed_point
191  *
192  * Convert li_fixed_t to a int. The fraction part is discarded.
193  *
194  * @param f fixed point number
195  * @return Converted int
196  */
197 static inline int
198 li_fixed_to_int(li_fixed_t f)
199 {
200         return f / 256;
201 }
202
203 /**
204  * @defgroup event Acessing and destruction of events
205  */
206
207 /**
208  * @ingroup event
209  *
210  * Destroy the event.
211  *
212  * @param event An event retrieved by libinput_get_event().
213  */
214 void
215 libinput_event_destroy(struct libinput_event *event);
216
217 /**
218  * @ingroup event
219  *
220  * Get the type of the event.
221  *
222  * @param event An event retrieved by libinput_get_event().
223  */
224 enum libinput_event_type
225 libinput_event_get_type(struct libinput_event *event);
226
227 /**
228  * @ingroup event
229  *
230  * Get get the target union of the event.
231  *
232  * The valid union member depends on the event type. For global events not
233  * related to some seat or device, the target is a libinput struct pointer.
234  * For events associated with a seat, the target is a libinput_seat pointer
235  * and for events associated with a device, the target is a libinput_device
236  * pointer.
237  *
238  * @param event An event retrieved by libinput_get_event().
239  */
240 union libinput_event_target
241 libinput_event_get_target(struct libinput_event *event);
242
243 /**
244  * @defgroup event_added_seat Added seat event
245  */
246
247 struct libinput_seat *
248 libinput_event_added_seat_get_seat(struct libinput_event_added_seat *event);
249
250 /**
251  * @defgroup event_removed_seat Removed seat event
252  */
253
254 struct libinput_seat *
255 libinput_event_removed_seat_get_seat(struct libinput_event_removed_seat *event);
256
257 /**
258  * @defgroup event_added_device Added device event
259  */
260
261 struct libinput_device *
262 libinput_event_added_device_get_device(
263         struct libinput_event_added_device *event);
264
265 /**
266  * @defgroup event_removed_device Removed device event
267  */
268
269 struct libinput_device *
270 libinput_event_removed_device_get_device(
271         struct libinput_event_removed_device *event);
272
273 /**
274  * @defgroup event_keyboard_key Keyboard key event
275  */
276
277 uint32_t
278 libinput_event_keyboard_key_get_time(
279         struct libinput_event_keyboard_key *event);
280
281 uint32_t
282 libinput_event_keyboard_key_get_key(
283         struct libinput_event_keyboard_key *event);
284
285 enum libinput_keyboard_key_state
286 libinput_event_keyboard_key_get_state(
287         struct libinput_event_keyboard_key *event);
288
289 /**
290  * @defgroup event_pointer_motion Pointer motion event
291  */
292
293 uint32_t
294 libinput_event_pointer_motion_get_time(
295         struct libinput_event_pointer_motion *event);
296
297 li_fixed_t
298 libinput_event_pointer_motion_get_dx(
299         struct libinput_event_pointer_motion *event);
300
301 li_fixed_t
302 libinput_event_pointer_motion_get_dy(
303         struct libinput_event_pointer_motion *event);
304
305 /**
306  * @defgroup event_pointer_motion_absolute Absolute pointer motion event
307  */
308
309 uint32_t
310 libinput_event_pointer_motion_absolute_get_time(
311         struct libinput_event_pointer_motion_absolute *event);
312
313 li_fixed_t
314 libinput_event_pointer_motion_absolute_get_x(
315         struct libinput_event_pointer_motion_absolute *event);
316
317 li_fixed_t
318 libinput_event_pointer_motion_absolute_get_y(
319         struct libinput_event_pointer_motion_absolute *event);
320
321 /**
322  * @defgroup event_pointer_button Pointer button event
323  */
324
325 uint32_t
326 libinput_event_pointer_button_get_time(
327         struct libinput_event_pointer_button *event);
328
329 uint32_t
330 libinput_event_pointer_button_get_button(
331         struct libinput_event_pointer_button *event);
332
333 enum libinput_pointer_button_state
334 libinput_event_pointer_button_get_state(
335         struct libinput_event_pointer_button *event);
336
337 /**
338  * @defgroup event_pointer_axis Pointer axis event
339  */
340
341 uint32_t
342 libinput_event_pointer_axis_get_time(
343         struct libinput_event_pointer_axis *event);
344
345 enum libinput_pointer_axis
346 libinput_event_pointer_axis_get_axis(
347         struct libinput_event_pointer_axis *event);
348
349 li_fixed_t
350 libinput_event_pointer_axis_get_value(
351         struct libinput_event_pointer_axis *event);
352
353 /**
354  * @defgroup event_pointer_button Pointer button event
355  */
356
357 uint32_t
358 libinput_event_touch_touch_get_time(
359         struct libinput_event_touch_touch *event);
360
361 uint32_t
362 libinput_event_touch_touch_get_slot(
363         struct libinput_event_touch_touch *event);
364
365 li_fixed_t
366 libinput_event_touch_touch_get_x(
367         struct libinput_event_touch_touch *event);
368
369 li_fixed_t
370 libinput_event_touch_touch_get_y(
371         struct libinput_event_touch_touch *event);
372
373 enum libinput_touch_type
374 libinput_event_touch_touch_get_touch_type(
375         struct libinput_event_touch_touch *event);
376
377 /**
378  * @defgroup base Initialization and manipulation of libinput contexts
379  */
380
381 struct libinput_interface {
382         /**
383          * Open the device at the given path with the flags provided and
384          * return the fd.
385          *
386          * @param path The device path to open
387          * @param flags Flags as defined by open(2)
388          * @param user_data The user_data provided in
389          * libinput_create_from_udev()
390          *
391          * @return the file descriptor, or a negative errno on failure.
392          */
393         int (*open_restricted)(const char *path, int flags, void *user_data);
394         /**
395          * Close the file descriptor.
396          *
397          * @param fd The file descriptor to close
398          * @param user_data The user_data provided in
399          * libinput_create_from_udev()
400          */
401         void (*close_restricted)(int fd, void *user_data);
402
403         void (*get_current_screen_dimensions)(struct libinput_device *device,
404                                               int *width,
405                                               int *height,
406                                               void *user_data);
407 };
408
409 /**
410  * @ingroup base
411  *
412  * Create a new libinput context from udev, for input devices matching
413  * the given seat ID. New devices or devices removed will appear as events
414  * during libinput_dispatch.
415  *
416  * @param interface The callback interface
417  * @param user_data Caller-specific data passed to the various callback
418  * interfaces.
419  * @param udev An already initialized udev context
420  * @param seat_id A seat identifier. This string must not be NULL.
421  *
422  * @return An initialized libinput context, ready to handle events or NULL on
423  * error.
424  */
425 struct libinput *
426 libinput_create_from_udev(const struct libinput_interface *interface,
427                           void *user_data,
428                           struct udev *udev,
429                           const char *seat_id);
430 /**
431  * @ingroup base
432  *
433  * Create a new libinput context from the given path. This context
434  * represents one single device only, it will not respond to new devices
435  * being added and reading from the device after it was removed will fail.
436  *
437  * @param interface The callback interface
438  * @param user_data Caller-specific data passed to the various callback
439  * interfaces.
440  * @param path Path to an input device
441  *
442  * @return An initialized libinput context, ready to handle events or NULL on
443  * error.
444  */
445 struct libinput *
446 libinput_create_from_path(const struct libinput_interface *interface,
447                           void *user_data,
448                           const char *path);
449
450 /**
451  * @ingroup base
452  *
453  * libinput keeps a single file descriptor for all events. Call into
454  * libinput_dispatch() if any events become available on this fd.
455  *
456  * @return the file descriptor used to notify of pending events.
457  */
458 int
459 libinput_get_fd(struct libinput *libinput);
460
461 /**
462  * @ingroup base
463  *
464  * Main event dispatchment function. Reads events of the file descriptors
465  * and processes them internally. Use libinput_get_event() to retrieve the
466  * events.
467  *
468  * Dispatching does not necessarily queue libinput events.
469  *
470  * @param libinput A previously initialized libinput context
471  *
472  * @return 0 on success, or a negative errno on failure
473  */
474 int
475 libinput_dispatch(struct libinput *libinput);
476
477 /**
478  * @ingroup base
479  *
480  * Retrieve the next event from libinput's internal event queue.
481  *
482  * After handling the retrieved event, the caller must destroy it using
483  * libinput_event_destroy().
484  *
485  * @param libinput A previously initialized libinput context
486  * @return The next available event, or NULL if no event is available.
487  */
488 struct libinput_event *
489 libinput_get_event(struct libinput *libinput);
490
491 /**
492  * @ingroup base
493  *
494  * Return the type of the next event in the internal queue. This function
495  * does not pop the event off the queue and the next call to
496  * libinput_get_event() returns that event.
497  *
498  * @param libinput A previously initialized libinput context
499  * @return The event type of the next available event or LIBINPUT_EVENT_NONE
500  * if no event is availble.
501  */
502 enum libinput_event_type
503 libinput_next_event_type(struct libinput *libinput);
504
505 /**
506  * @ingroup base
507  *
508  * @param libinput A previously initialized libinput context
509  * @return the caller-specific data previously assigned in
510  * libinput_create_udev().
511  */
512 void *
513 libinput_get_user_data(struct libinput *libinput);
514
515 /**
516  * @ingroup base
517  *
518  * Resume a suspended libinput context. This re-enables device
519  * monitoring and adds existing devices.
520  *
521  * @param libinput A previously initialized libinput context
522  * @see libinput_suspend
523  *
524  * @return 0 on success or -1 on failure
525  */
526 int
527 libinput_resume(struct libinput *libinput);
528
529 /**
530  * @ingroup base
531  *
532  * Suspend monitoring for new devices and close existing devices.
533  * This all but terminates libinput but does keep the context
534  * valid to be resumed with libinput_resume().
535  *
536  * @param libinput A previously initialized libinput context
537  */
538 void
539 libinput_suspend(struct libinput *libinput);
540
541 /**
542  * @ingroup base
543  *
544  * Destroy the libinput context. After this, object references associated with
545  * the destroyed context are invalid and may not be interacted with.
546  *
547  * @param libinput A previously initialized libinput context
548  */
549 void
550 libinput_destroy(struct libinput *libinput);
551
552 /**
553  * @defgroup seat Initialization and manipulation of seats
554  */
555
556 /**
557  * @ingroup seat
558  *
559  * Increase the refcount of the seat. A seat will be freed whenever the
560  * refcount reaches 0. This may happen during dispatch if the
561  * seat was removed from the system. A caller must ensure to reference
562  * the seat correctly to avoid dangling pointers.
563  *
564  * @param seat A previously obtained seat
565  */
566 void
567 libinput_seat_ref(struct libinput_seat *seat);
568
569 /**
570  * @ingroup seat
571  *
572  * Decrease the refcount of the seat. A seat will be freed whenever the
573  * refcount reaches 0. This may happen during dispatch if the
574  * seat was removed from the system. A caller must ensure to reference
575  * the seat correctly to avoid dangling pointers.
576  *
577  * @param seat A previously obtained seat
578  */
579 void
580 libinput_seat_unref(struct libinput_seat *seat);
581
582 /**
583  * @ingroup seat
584  *
585  * Set caller-specific data associated with this seat. libinput does
586  * not manage, look at, or modify this data. The caller must ensure the
587  * data is valid.
588  *
589  * @param seat A previously obtained seat
590  * @param user_data Caller-specific data pointer
591  * @see libinput_seat_get_user_data
592  */
593 void
594 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data);
595
596 /**
597  * @ingroup seat
598  *
599  * Get the caller-specific data associated with this seat, if any.
600  *
601  * @param seat A previously obtained seat
602  * @return Caller-specific data pointer or NULL if none was set
603  * @see libinput_seat_set_user_data
604  */
605 void *
606 libinput_seat_get_user_data(struct libinput_seat *seat);
607
608 /**
609  * @ingroup seat
610  *
611  * @param seat A previously obtained seat
612  * @return the name of this seat
613  */
614 const char *
615 libinput_seat_get_name(struct libinput_seat *seat);
616
617 /**
618  * @defgroup device Initialization and manipulation of input devices
619  */
620
621 /**
622  * @ingroup device
623  *
624  * Increase the refcount of the input device. An input device will be freed
625  * whenever the refcount reaches 0. This may happen during dispatch if the
626  * device was removed from the system. A caller must ensure to reference
627  * the device correctly to avoid dangling pointers.
628  *
629  * @param device A previously obtained device
630  */
631 void
632 libinput_device_ref(struct libinput_device *device);
633
634 /**
635  * @ingroup device
636  *
637  * Decrease the refcount of the input device. An input device will be freed
638  * whenever the refcount reaches 0. This may happen during dispatch if the
639  * device was removed from the system. A caller must ensure to reference
640  * the device correctly to avoid dangling pointers.
641  *
642  * @param device A previously obtained device
643  */
644 void
645 libinput_device_unref(struct libinput_device *device);
646
647 /**
648  * @ingroup device
649  *
650  * Set caller-specific data associated with this input device. libinput does
651  * not manage, look at, or modify this data. The caller must ensure the
652  * data is valid.
653  *
654  * @param device A previously obtained device
655  * @param user_data Caller-specific data pointer
656  * @see libinput_device_get_user_data
657  */
658 void
659 libinput_device_set_user_data(struct libinput_device *device, void *user_data);
660
661 /**
662  * @ingroup device
663  *
664  * Get the caller-specific data associated with this input device, if any.
665  *
666  * @param device A previously obtained device
667  * @return Caller-specific data pointer or NULL if none was set
668  * @see libinput_device_set_user_data
669  */
670 void *
671 libinput_device_get_user_data(struct libinput_device *device);
672
673 /**
674  * @ingroup device
675  *
676  * Get the system name of the device.
677  *
678  * @param device A previously obtained device
679  * @return System name of the device
680  */
681 const char *
682 libinput_device_get_sysname(struct libinput_device *device);
683
684 /**
685  * @ingroup device
686  *
687  * A device may be mapped to a single output, or all available outputs. If a
688  * device is mapped to a single output only, a relative device may not move
689  * beyond the boundaries of this output. An absolute device has its input
690  * coordinates mapped to the extents of this output.
691  *
692  * @return the name of the output this device is mapped to, or NULL if no
693  * output is set
694  */
695 const char *
696 libinput_device_get_output_name(struct libinput_device *device);
697
698 /**
699  * @ingroup device
700  *
701  * Get the seat associated with this input device.
702  *
703  * @param device A previously obtained device
704  * @return The seat this input device belongs to
705  */
706 struct libinput_seat *
707 libinput_device_get_seat(struct libinput_device *device);
708
709 /**
710  * @ingroup device
711  *
712  * Update the LEDs on the device, if any. If the device does not have
713  * LEDs, or does not have one or more of the LEDs given in the mask, this
714  * function does nothing.
715  *
716  * @param device A previously obtained device
717  * @param leds A mask of the LEDs to set, or unset.
718  */
719 void
720 libinput_device_led_update(struct libinput_device *device,
721                            enum libinput_led leds);
722
723 /**
724  * @ingroup device
725  *
726  * Set the bitmask in keys to the bitmask of the keys present on the device
727  * (see linux/input.h), up to size characters.
728  *
729  * @param device A current input device
730  * @param keys An array filled with the bitmask for the keys
731  * @param size Size of the keys array
732  */
733 int
734 libinput_device_get_keys(struct libinput_device *device,
735                          char *keys, size_t size);
736
737 /**
738  * @ingroup device
739  *
740  * Apply the 3x3 transformation matrix to absolute device coordinates. This
741  * matrix has no effect on relative events.
742  *
743  * Given a 6-element array [a, b, c, d, e, f], the matrix is applied as
744  * @code
745  * [ a  b  c ]   [ x ]
746  * [ d  e  f ] * [ y ]
747  * [ 0  0  1 ]   [ 1 ]
748  * @endcode
749  */
750 void
751 libinput_device_calibrate(struct libinput_device *device,
752                           float calibration[6]);
753
754 /**
755  * @ingroup device
756  *
757  * Check if the given device has the specified capability
758  *
759  * @return 1 if the given device has the capability or 0 if not
760  */
761 int
762 libinput_device_has_capability(struct libinput_device *device,
763                                enum libinput_device_capability capability);
764
765 #endif /* LIBINPUT_H */