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