1/*
2 * Copyright © 2013 Jonas Ådahl
3 * Copyright © 2013-2015 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef LIBINPUT_PRIVATE_H
26#define LIBINPUT_PRIVATE_H
27
28#include "config.h"
29
30#include <errno.h>
31#include <math.h>
32#include <stdarg.h>
33
34#if HAVE_LIBWACOM
35#include <libwacom/libwacom.h>
36#endif
37
38#include "linux/input.h"
39
40#include "libinput.h"
41#include "libinput-private-config.h"
42#include "libinput-util.h"
43#include "libinput-version.h"
44
45struct libinput_source;
46
47/* A coordinate pair in device coordinates */
48struct device_coords {
49	int x, y;
50};
51
52/*
53 * A coordinate pair in device coordinates, capable of holding non discrete
54 * values, this is necessary e.g. when device coordinates get averaged.
55 */
56struct device_float_coords {
57	double x, y;
58};
59
60/* A dpi-normalized coordinate pair */
61struct normalized_coords {
62	double x, y;
63};
64
65/* A discrete step pair (mouse wheels) */
66struct discrete_coords {
67	int x, y;
68};
69
70/* A pair of coordinates normalized to a [0,1] or [-1, 1] range */
71struct normalized_range_coords {
72	double x, y;
73};
74
75/* A pair of angles in degrees */
76struct wheel_angle {
77	double x, y;
78};
79
80/* A pair of wheel click data for the 120-normalized range */
81struct wheel_v120 {
82	int x, y;
83};
84
85/* A pair of angles in degrees */
86struct tilt_degrees {
87	double x, y;
88};
89
90/* A threshold with an upper and lower limit */
91struct threshold {
92	int upper;
93	int lower;
94};
95
96/* A pair of coordinates in mm */
97struct phys_coords {
98	double x;
99	double y;
100};
101
102/* A rectangle in mm, x/y is the top-left corner */
103struct phys_rect {
104	double x, y;
105	double w, h;
106};
107
108/* A rectangle in device coordinates, x/y is the top-left corner */
109struct device_coord_rect {
110	int x, y;
111	int w, h;
112};
113
114/* A pair of major/minor in mm */
115struct phys_ellipsis {
116	double major;
117	double minor;
118};
119
120struct libinput_interface_backend {
121	int (*resume)(struct libinput *libinput);
122	void (*suspend)(struct libinput *libinput);
123	void (*destroy)(struct libinput *libinput);
124	int (*device_change_seat)(struct libinput_device *device,
125				  const char *seat_name);
126};
127
128struct libinput {
129	int epoll_fd;
130	struct list source_destroy_list;
131
132	struct list seat_list;
133
134	struct {
135		struct list list;
136		struct libinput_source *source;
137		int fd;
138		uint64_t next_expiry;
139
140		struct ratelimit expiry_in_past_limit;
141	} timer;
142
143	struct libinput_event **events;
144	size_t events_count;
145	size_t events_len;
146	size_t events_in;
147	size_t events_out;
148
149	struct list tool_list;
150
151	const struct libinput_interface *interface;
152	const struct libinput_interface_backend *interface_backend;
153
154	libinput_log_handler log_handler;
155	enum libinput_log_priority log_priority;
156	void *user_data;
157	int refcount;
158
159	struct list device_group_list;
160
161	uint64_t last_event_time;
162	uint64_t dispatch_time;
163
164	bool quirks_initialized;
165	struct quirks_context *quirks;
166
167#if HAVE_LIBWACOM
168	struct {
169		WacomDeviceDatabase *db;
170		size_t refcount;
171	} libwacom;
172#endif
173};
174
175typedef void (*libinput_seat_destroy_func) (struct libinput_seat *seat);
176
177struct libinput_seat {
178	struct libinput *libinput;
179	struct list link;
180	struct list devices_list;
181	void *user_data;
182	int refcount;
183	libinput_seat_destroy_func destroy;
184
185	char *physical_name;
186	char *logical_name;
187
188	uint32_t slot_map;
189
190	uint32_t button_count[KEY_CNT];
191};
192
193struct libinput_device_config_tap {
194	int (*count)(struct libinput_device *device);
195	enum libinput_config_status (*set_enabled)(struct libinput_device *device,
196						   enum libinput_config_tap_state enable);
197	enum libinput_config_tap_state (*get_enabled)(struct libinput_device *device);
198	enum libinput_config_tap_state (*get_default)(struct libinput_device *device);
199
200	enum libinput_config_status (*set_map)(struct libinput_device *device,
201						   enum libinput_config_tap_button_map map);
202	enum libinput_config_tap_button_map (*get_map)(struct libinput_device *device);
203	enum libinput_config_tap_button_map (*get_default_map)(struct libinput_device *device);
204
205	enum libinput_config_status (*set_drag_enabled)(struct libinput_device *device,
206							enum libinput_config_drag_state);
207	enum libinput_config_drag_state (*get_drag_enabled)(struct libinput_device *device);
208	enum libinput_config_drag_state (*get_default_drag_enabled)(struct libinput_device *device);
209
210	enum libinput_config_status (*set_draglock_enabled)(struct libinput_device *device,
211							    enum libinput_config_drag_lock_state);
212	enum libinput_config_drag_lock_state (*get_draglock_enabled)(struct libinput_device *device);
213	enum libinput_config_drag_lock_state (*get_default_draglock_enabled)(struct libinput_device *device);
214};
215
216struct libinput_device_config_calibration {
217	int (*has_matrix)(struct libinput_device *device);
218	enum libinput_config_status (*set_matrix)(struct libinput_device *device,
219						  const float matrix[6]);
220	int (*get_matrix)(struct libinput_device *device,
221			  float matrix[6]);
222	int (*get_default_matrix)(struct libinput_device *device,
223							  float matrix[6]);
224};
225
226struct libinput_device_config_send_events {
227	uint32_t (*get_modes)(struct libinput_device *device);
228	enum libinput_config_status (*set_mode)(struct libinput_device *device,
229						   enum libinput_config_send_events_mode mode);
230	enum libinput_config_send_events_mode (*get_mode)(struct libinput_device *device);
231	enum libinput_config_send_events_mode (*get_default_mode)(struct libinput_device *device);
232};
233
234/**
235 * Custom acceleration function min number of points
236 * At least 2 points are required for linear interpolation
237 */
238#define LIBINPUT_ACCEL_NPOINTS_MIN 2
239
240/**
241 * Custom acceleration function max number of points
242 * an arbitrary limit of sample points
243 * it should be more than enough for everyone
244 */
245#define LIBINPUT_ACCEL_NPOINTS_MAX 64
246
247/**
248 * Custom acceleration function min point value
249 */
250#define LIBINPUT_ACCEL_POINT_MIN_VALUE 0
251
252/**
253 * Custom acceleration function max point value
254 */
255#define LIBINPUT_ACCEL_POINT_MAX_VALUE 10000
256
257/**
258 * Custom acceleration function max step size
259 */
260#define LIBINPUT_ACCEL_STEP_MAX 10000
261
262struct libinput_config_accel_custom_func {
263	double step;
264	size_t npoints;
265	double points[LIBINPUT_ACCEL_NPOINTS_MAX];
266};
267
268struct libinput_config_accel {
269	enum libinput_config_accel_profile profile;
270
271	struct  {
272		struct libinput_config_accel_custom_func *fallback;
273		struct libinput_config_accel_custom_func *motion;
274		struct libinput_config_accel_custom_func *scroll;
275	} custom;
276};
277
278struct libinput_device_config_accel {
279	int (*available)(struct libinput_device *device);
280	enum libinput_config_status (*set_speed)(struct libinput_device *device,
281						 double speed);
282	double (*get_speed)(struct libinput_device *device);
283	double (*get_default_speed)(struct libinput_device *device);
284
285	uint32_t (*get_profiles)(struct libinput_device *device);
286	enum libinput_config_status (*set_profile)(struct libinput_device *device,
287						   enum libinput_config_accel_profile);
288	enum libinput_config_accel_profile (*get_profile)(struct libinput_device *device);
289	enum libinput_config_accel_profile (*get_default_profile)(struct libinput_device *device);
290	enum libinput_config_status (*set_accel_config)(struct libinput_device *device,
291						        struct libinput_config_accel *accel_config);
292};
293
294struct libinput_device_config_natural_scroll {
295	int (*has)(struct libinput_device *device);
296	enum libinput_config_status (*set_enabled)(struct libinput_device *device,
297						   int enabled);
298	int (*get_enabled)(struct libinput_device *device);
299	int (*get_default_enabled)(struct libinput_device *device);
300};
301
302struct libinput_device_config_left_handed {
303	int (*has)(struct libinput_device *device);
304	enum libinput_config_status (*set)(struct libinput_device *device, int left_handed);
305	int (*get)(struct libinput_device *device);
306	int (*get_default)(struct libinput_device *device);
307};
308
309struct libinput_device_config_scroll_method {
310	uint32_t (*get_methods)(struct libinput_device *device);
311	enum libinput_config_status (*set_method)(struct libinput_device *device,
312						  enum libinput_config_scroll_method method);
313	enum libinput_config_scroll_method (*get_method)(struct libinput_device *device);
314	enum libinput_config_scroll_method (*get_default_method)(struct libinput_device *device);
315	enum libinput_config_status (*set_button)(struct libinput_device *device,
316						  uint32_t button);
317	uint32_t (*get_button)(struct libinput_device *device);
318	uint32_t (*get_default_button)(struct libinput_device *device);
319	enum libinput_config_status (*set_button_lock)(struct libinput_device *device,
320						       enum libinput_config_scroll_button_lock_state);
321	enum libinput_config_scroll_button_lock_state (*get_button_lock)(struct libinput_device *device);
322	enum libinput_config_scroll_button_lock_state (*get_default_button_lock)(struct libinput_device *device);
323};
324
325struct libinput_device_config_click_method {
326	uint32_t (*get_methods)(struct libinput_device *device);
327	enum libinput_config_status (*set_method)(struct libinput_device *device,
328						  enum libinput_config_click_method method);
329	enum libinput_config_click_method (*get_method)(struct libinput_device *device);
330	enum libinput_config_click_method (*get_default_method)(struct libinput_device *device);
331};
332
333struct libinput_device_config_middle_emulation {
334	int (*available)(struct libinput_device *device);
335	enum libinput_config_status (*set)(
336			 struct libinput_device *device,
337			 enum libinput_config_middle_emulation_state);
338	enum libinput_config_middle_emulation_state (*get)(
339			 struct libinput_device *device);
340	enum libinput_config_middle_emulation_state (*get_default)(
341			 struct libinput_device *device);
342};
343
344struct libinput_device_config_dwt {
345	int (*is_available)(struct libinput_device *device);
346	enum libinput_config_status (*set_enabled)(
347			 struct libinput_device *device,
348			 enum libinput_config_dwt_state enable);
349	enum libinput_config_dwt_state (*get_enabled)(
350			 struct libinput_device *device);
351	enum libinput_config_dwt_state (*get_default_enabled)(
352			 struct libinput_device *device);
353};
354
355struct libinput_device_config_dwtp {
356	int (*is_available)(struct libinput_device *device);
357	enum libinput_config_status (*set_enabled)(
358			 struct libinput_device *device,
359			 enum libinput_config_dwtp_state enable);
360	enum libinput_config_dwtp_state (*get_enabled)(
361			 struct libinput_device *device);
362	enum libinput_config_dwtp_state (*get_default_enabled)(
363			 struct libinput_device *device);
364};
365
366struct libinput_device_config_rotation {
367	int (*is_available)(struct libinput_device *device);
368	enum libinput_config_status (*set_angle)(
369			 struct libinput_device *device,
370			 unsigned int degrees_cw);
371	unsigned int (*get_angle)(struct libinput_device *device);
372	unsigned int (*get_default_angle)(struct libinput_device *device);
373};
374
375struct libinput_device_config_gesture {
376	enum libinput_config_status (*set_hold_enabled)(struct libinput_device *device,
377			 enum libinput_config_hold_state enabled);
378	enum libinput_config_hold_state (*get_hold_enabled)(struct libinput_device *device);
379	enum libinput_config_hold_state (*get_hold_default)(struct libinput_device *device);
380};
381
382struct libinput_device_config {
383	struct libinput_device_config_tap *tap;
384	struct libinput_device_config_calibration *calibration;
385	struct libinput_device_config_send_events *sendevents;
386	struct libinput_device_config_accel *accel;
387	struct libinput_device_config_natural_scroll *natural_scroll;
388	struct libinput_device_config_left_handed *left_handed;
389	struct libinput_device_config_scroll_method *scroll_method;
390	struct libinput_device_config_click_method *click_method;
391	struct libinput_device_config_middle_emulation *middle_emulation;
392	struct libinput_device_config_dwt *dwt;
393	struct libinput_device_config_dwtp *dwtp;
394	struct libinput_device_config_rotation *rotation;
395	struct libinput_device_config_gesture *gesture;
396};
397
398struct libinput_device_group {
399	int refcount;
400	void *user_data;
401	char *identifier; /* unique identifier or NULL for singletons */
402
403	struct list link;
404};
405
406struct libinput_device {
407	struct libinput_seat *seat;
408	struct libinput_device_group *group;
409	struct list link;
410	struct list event_listeners;
411	void *user_data;
412	int refcount;
413	struct libinput_device_config config;
414};
415
416enum libinput_tablet_tool_axis {
417	LIBINPUT_TABLET_TOOL_AXIS_X = 1,
418	LIBINPUT_TABLET_TOOL_AXIS_Y = 2,
419	LIBINPUT_TABLET_TOOL_AXIS_DISTANCE = 3,
420	LIBINPUT_TABLET_TOOL_AXIS_PRESSURE = 4,
421	LIBINPUT_TABLET_TOOL_AXIS_TILT_X = 5,
422	LIBINPUT_TABLET_TOOL_AXIS_TILT_Y = 6,
423	LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z = 7,
424	LIBINPUT_TABLET_TOOL_AXIS_SLIDER = 8,
425	LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL = 9,
426	LIBINPUT_TABLET_TOOL_AXIS_SIZE_MAJOR = 10,
427	LIBINPUT_TABLET_TOOL_AXIS_SIZE_MINOR = 11,
428};
429
430#define LIBINPUT_TABLET_TOOL_AXIS_MAX LIBINPUT_TABLET_TOOL_AXIS_SIZE_MINOR
431
432struct tablet_axes {
433	struct device_coords point;
434	struct normalized_coords delta;
435	double distance;
436	double pressure;
437	struct tilt_degrees tilt;
438	double rotation;
439	double slider;
440	double wheel;
441	int wheel_discrete;
442	struct phys_ellipsis size;
443};
444
445enum pressure_heuristic_state {
446	PRESSURE_HEURISTIC_STATE_PROXIN1, /** First proximity in event */
447	PRESSURE_HEURISTIC_STATE_PROXIN2, /** Second proximity in event */
448	PRESSURE_HEURISTIC_STATE_DECIDE,  /** Decide on offset now */
449	PRESSURE_HEURISTIC_STATE_DONE,    /** Decision's been made, live with it */
450};
451
452struct libinput_tablet_tool {
453	struct list link;
454	uint32_t serial;
455	uint32_t tool_id;
456	enum libinput_tablet_tool_type type;
457	unsigned char axis_caps[NCHARS(LIBINPUT_TABLET_TOOL_AXIS_MAX + 1)];
458	unsigned char buttons[NCHARS(KEY_MAX) + 1];
459	int refcount;
460	void *user_data;
461
462	struct {
463		struct threshold threshold; /* in device coordinates */
464		int offset; /* in device coordinates */
465		bool has_offset;
466
467		enum pressure_heuristic_state heuristic_state;
468	} pressure;
469};
470
471struct libinput_tablet_pad_mode_group {
472	struct libinput_device *device;
473	struct list link;
474	int refcount;
475	void *user_data;
476
477	unsigned int index;
478	unsigned int num_modes;
479	unsigned int current_mode;
480
481	uint32_t button_mask;
482	uint32_t ring_mask;
483	uint32_t strip_mask;
484
485	uint32_t toggle_button_mask;
486
487	void (*destroy)(struct libinput_tablet_pad_mode_group *group);
488};
489
490struct libinput_event {
491	enum libinput_event_type type;
492	struct libinput_device *device;
493};
494
495struct libinput_event_listener {
496	struct list link;
497	void (*notify_func)(uint64_t time, struct libinput_event *ev, void *notify_func_data);
498	void *notify_func_data;
499};
500
501typedef void (*libinput_source_dispatch_t)(void *data);
502
503#define log_debug(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
504#define log_info(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
505#define log_error(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
506#define log_bug_kernel(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
507#define log_bug_libinput(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
508#define log_bug_client(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
509
510#define log_debug_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
511#define log_info_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
512#define log_error_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
513#define log_bug_kernel_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
514#define log_bug_libinput_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
515#define log_bug_client_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
516
517static inline bool
518is_logged(const struct libinput *libinput,
519	  enum libinput_log_priority priority)
520{
521       return libinput->log_handler &&
522               libinput->log_priority <= priority;
523}
524
525void
526log_msg_ratelimit(struct libinput *libinput,
527		  struct ratelimit *ratelimit,
528		  enum libinput_log_priority priority,
529		  const char *format, ...)
530	LIBINPUT_ATTRIBUTE_PRINTF(4, 5);
531
532void
533log_msg(struct libinput *libinput,
534	enum libinput_log_priority priority,
535	const char *format, ...)
536	LIBINPUT_ATTRIBUTE_PRINTF(3, 4);
537
538void
539log_msg_va(struct libinput *libinput,
540	   enum libinput_log_priority priority,
541	   const char *format,
542	   va_list args)
543	LIBINPUT_ATTRIBUTE_PRINTF(3, 0);
544
545int
546libinput_init(struct libinput *libinput,
547	      const struct libinput_interface *interface,
548	      const struct libinput_interface_backend *interface_backend,
549	      void *user_data);
550
551void
552libinput_init_quirks(struct libinput *libinput);
553
554struct libinput_source *
555libinput_add_fd(struct libinput *libinput,
556		int fd,
557		libinput_source_dispatch_t dispatch,
558		void *data);
559
560void
561libinput_remove_source(struct libinput *libinput,
562		       struct libinput_source *source);
563
564int
565open_restricted(struct libinput *libinput,
566		const char *path, int flags);
567
568void
569close_restricted(struct libinput *libinput, int fd);
570
571bool
572ignore_litest_test_suite_device(struct udev_device *device);
573
574void
575libinput_seat_init(struct libinput_seat *seat,
576		   struct libinput *libinput,
577		   const char *physical_name,
578		   const char *logical_name,
579		   libinput_seat_destroy_func destroy);
580
581void
582libinput_device_init(struct libinput_device *device,
583		     struct libinput_seat *seat);
584
585struct libinput_device_group *
586libinput_device_group_create(struct libinput *libinput,
587			     const char *identifier);
588
589struct libinput_device_group *
590libinput_device_group_find_group(struct libinput *libinput,
591				 const char *identifier);
592
593void
594libinput_device_set_device_group(struct libinput_device *device,
595				 struct libinput_device_group *group);
596
597void
598libinput_device_init_event_listener(struct libinput_event_listener *listener);
599
600void
601libinput_device_add_event_listener(struct libinput_device *device,
602				   struct libinput_event_listener *listener,
603				   void (*notify_func)(
604						uint64_t time,
605						struct libinput_event *event,
606						void *notify_func_data),
607				   void *notify_func_data);
608
609void
610libinput_device_remove_event_listener(struct libinput_event_listener *listener);
611
612void
613notify_added_device(struct libinput_device *device);
614
615void
616notify_removed_device(struct libinput_device *device);
617
618void
619keyboard_notify_key(struct libinput_device *device,
620		    uint64_t time,
621		    uint32_t key,
622		    enum libinput_key_state state);
623
624void
625pointer_notify_motion(struct libinput_device *device,
626		      uint64_t time,
627		      const struct normalized_coords *delta,
628		      const struct device_float_coords *raw);
629
630void
631pointer_notify_motion_absolute(struct libinput_device *device,
632			       uint64_t time,
633			       const struct device_coords *point);
634
635void
636pointer_notify_button(struct libinput_device *device,
637		      uint64_t time,
638		      int32_t button,
639		      enum libinput_button_state state);
640
641void
642pointer_notify_axis_finger(struct libinput_device *device,
643			   uint64_t time,
644			   uint32_t axes,
645			   const struct normalized_coords *delta);
646void
647pointer_notify_axis_continuous(struct libinput_device *device,
648			       uint64_t time,
649			       uint32_t axes,
650			       const struct normalized_coords *delta);
651
652void
653pointer_notify_axis_legacy_wheel(struct libinput_device *device,
654				 uint64_t time,
655				 uint32_t axes,
656				 const struct normalized_coords *delta,
657				 const struct discrete_coords *discrete);
658
659void
660pointer_notify_axis_wheel(struct libinput_device *device,
661			  uint64_t time,
662			  uint32_t axes,
663			  const struct normalized_coords *delta,
664			  const struct wheel_v120 *v120);
665
666void
667touch_notify_touch_down(struct libinput_device *device,
668			uint64_t time,
669			int32_t slot,
670			int32_t seat_slot,
671			const struct device_coords *point);
672
673void
674touch_notify_touch_motion(struct libinput_device *device,
675			  uint64_t time,
676			  int32_t slot,
677			  int32_t seat_slot,
678			  const struct device_coords *point);
679
680void
681touch_notify_touch_up(struct libinput_device *device,
682		      uint64_t time,
683		      int32_t slot,
684		      int32_t seat_slot);
685
686void
687touch_notify_touch_cancel(struct libinput_device *device,
688			  uint64_t time,
689			  int32_t slot,
690			  int32_t seat_slot);
691
692void
693touch_notify_frame(struct libinput_device *device,
694		   uint64_t time);
695
696void
697gesture_notify_swipe(struct libinput_device *device,
698		     uint64_t time,
699		     enum libinput_event_type type,
700		     int finger_count,
701		     const struct normalized_coords *delta,
702		     const struct normalized_coords *unaccel);
703
704void
705gesture_notify_swipe_end(struct libinput_device *device,
706			 uint64_t time,
707			 int finger_count,
708			 bool cancelled);
709
710void
711gesture_notify_pinch(struct libinput_device *device,
712		     uint64_t time,
713		     enum libinput_event_type type,
714		     int finger_count,
715		     const struct normalized_coords *delta,
716		     const struct normalized_coords *unaccel,
717		     double scale,
718		     double angle);
719
720void
721gesture_notify_pinch_end(struct libinput_device *device,
722			 uint64_t time,
723			 int finger_count,
724			 double scale,
725			 bool cancelled);
726
727void
728gesture_notify_hold(struct libinput_device *device,
729		    uint64_t time,
730		    int finger_count);
731
732void
733gesture_notify_hold_end(struct libinput_device *device,
734			uint64_t time,
735			int finger_count,
736			bool cancelled);
737
738void
739tablet_notify_axis(struct libinput_device *device,
740		   uint64_t time,
741		   struct libinput_tablet_tool *tool,
742		   enum libinput_tablet_tool_tip_state tip_state,
743		   unsigned char *changed_axes,
744		   const struct tablet_axes *axes);
745
746void
747tablet_notify_proximity(struct libinput_device *device,
748			uint64_t time,
749			struct libinput_tablet_tool *tool,
750			enum libinput_tablet_tool_proximity_state state,
751			unsigned char *changed_axes,
752			const struct tablet_axes *axes);
753
754void
755tablet_notify_tip(struct libinput_device *device,
756		  uint64_t time,
757		  struct libinput_tablet_tool *tool,
758		  enum libinput_tablet_tool_tip_state tip_state,
759		  unsigned char *changed_axes,
760		  const struct tablet_axes *axes);
761
762void
763tablet_notify_button(struct libinput_device *device,
764		     uint64_t time,
765		     struct libinput_tablet_tool *tool,
766		     enum libinput_tablet_tool_tip_state tip_state,
767		     const struct tablet_axes *axes,
768		     int32_t button,
769		     enum libinput_button_state state);
770
771void
772tablet_pad_notify_button(struct libinput_device *device,
773			 uint64_t time,
774			 int32_t button,
775			 enum libinput_button_state state,
776			 struct libinput_tablet_pad_mode_group *group);
777void
778tablet_pad_notify_ring(struct libinput_device *device,
779		       uint64_t time,
780		       unsigned int number,
781		       double value,
782		       enum libinput_tablet_pad_ring_axis_source source,
783		       struct libinput_tablet_pad_mode_group *group);
784void
785tablet_pad_notify_strip(struct libinput_device *device,
786			uint64_t time,
787			unsigned int number,
788			double value,
789			enum libinput_tablet_pad_strip_axis_source source,
790			struct libinput_tablet_pad_mode_group *group);
791void
792tablet_pad_notify_key(struct libinput_device *device,
793		      uint64_t time,
794		      int32_t key,
795		      enum libinput_key_state state);
796void
797switch_notify_toggle(struct libinput_device *device,
798		     uint64_t time,
799		     enum libinput_switch sw,
800		     enum libinput_switch_state state);
801
802static inline uint64_t
803libinput_now(struct libinput *libinput)
804{
805	struct timespec ts = { 0, 0 };
806
807	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
808		log_error(libinput, "clock_gettime failed: %s\n", strerror(errno));
809		return 0;
810	}
811
812	return s2us(ts.tv_sec) + ns2us(ts.tv_nsec);
813}
814
815static inline struct device_float_coords
816device_delta(const struct device_coords a, const struct device_coords b)
817{
818	struct device_float_coords delta;
819
820	delta.x = a.x - b.x;
821	delta.y = a.y - b.y;
822
823	return delta;
824}
825
826static inline struct device_float_coords
827device_average(const struct device_coords a, const struct device_coords b)
828{
829	struct device_float_coords average;
830
831	average.x = (a.x + b.x) / 2.0;
832	average.y = (a.y + b.y) / 2.0;
833
834	return average;
835}
836
837static inline struct device_float_coords
838device_float_delta(const struct device_float_coords a, const struct device_float_coords b)
839{
840	struct device_float_coords delta;
841
842	delta.x = a.x - b.x;
843	delta.y = a.y - b.y;
844
845	return delta;
846}
847
848static inline struct device_float_coords
849device_float_average(const struct device_float_coords a, const struct device_float_coords b)
850{
851	struct device_float_coords average;
852
853	average.x = (a.x + b.x) / 2.0;
854	average.y = (a.y + b.y) / 2.0;
855
856	return average;
857}
858
859static inline bool
860device_float_is_zero(const struct device_float_coords coords)
861{
862	return coords.x == 0.0 && coords.y == 0.0;
863}
864
865static inline double
866normalized_length(const struct normalized_coords norm)
867{
868	return hypot(norm.x, norm.y);
869}
870
871static inline bool
872normalized_is_zero(const struct normalized_coords norm)
873{
874	return norm.x == 0.0 && norm.y == 0.0;
875}
876
877static inline double
878length_in_mm(const struct phys_coords mm)
879{
880	return hypot(mm.x, mm.y);
881}
882
883enum directions {
884	N  = bit(0),
885	NE = bit(1),
886	E  = bit(2),
887	SE = bit(3),
888	S  = bit(4),
889	SW = bit(5),
890	W  = bit(6),
891	NW = bit(7),
892	UNDEFINED_DIRECTION = 0xff
893};
894
895static inline uint32_t
896xy_get_direction(double x, double y)
897{
898	uint32_t dir = UNDEFINED_DIRECTION;
899	int d1, d2;
900	double r;
901
902	if (fabs(x) < 2.0 && fabs(y) < 2.0) {
903		if (x > 0.0 && y > 0.0)
904			dir = S | SE | E;
905		else if (x > 0.0 && y < 0.0)
906			dir = N | NE | E;
907		else if (x < 0.0 && y > 0.0)
908			dir = S | SW | W;
909		else if (x < 0.0 && y < 0.0)
910			dir = N | NW | W;
911		else if (x > 0.0)
912			dir = NE | E | SE;
913		else if (x < 0.0)
914			dir = NW | W | SW;
915		else if (y > 0.0)
916			dir = SE | S | SW;
917		else if (y < 0.0)
918			dir = NE | N | NW;
919	} else {
920		/* Calculate r within the interval  [0 to 8)
921		 *
922		 * r = [0 .. 2π] where 0 is North
923		 * d_f = r / 2π  ([0 .. 1))
924		 * d_8 = 8 * d_f
925		 */
926		r = atan2(y, x);
927		r = fmod(r + 2.5*M_PI, 2*M_PI);
928		r *= 4*M_1_PI;
929
930		/* Mark one or two close enough octants */
931		d1 = (int)(r + 0.9) % 8;
932		d2 = (int)(r + 0.1) % 8;
933
934		dir = bit(d1) | bit(d2);
935	}
936
937	return dir;
938}
939
940static inline uint32_t
941phys_get_direction(const struct phys_coords mm)
942{
943	return xy_get_direction(mm.x, mm.y);
944}
945
946/**
947 * Get the direction for the given set of coordinates.
948 * assumption: coordinates are normalized to one axis resolution.
949 */
950static inline uint32_t
951device_float_get_direction(const struct device_float_coords coords)
952{
953	return xy_get_direction(coords.x, coords.y);
954}
955
956/**
957 * Returns true if the point is within the given rectangle, including the
958 * left edge but excluding the right edge.
959 */
960static inline bool
961point_in_rect(const struct device_coords *point,
962	      const struct device_coord_rect *rect)
963{
964	return (point->x >= rect->x &&
965		point->x < rect->x + rect->w &&
966		point->y >= rect->y &&
967		point->y < rect->y + rect->h);
968}
969
970#if HAVE_LIBWACOM
971WacomDeviceDatabase *
972libinput_libwacom_ref(struct libinput *li);
973void
974libinput_libwacom_unref(struct libinput *li);
975#else
976static inline void *libinput_libwacom_ref(struct libinput *li) { return NULL; }
977static inline void libinput_libwacom_unref(struct libinput *li) {}
978#endif
979
980#endif /* LIBINPUT_PRIVATE_H */
981