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 
45 struct libinput_source;
46 
47 /* A coordinate pair in device coordinates */
48 struct 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  */
56 struct device_float_coords {
57 	double x, y;
58 };
59 
60 /* A dpi-normalized coordinate pair */
61 struct normalized_coords {
62 	double x, y;
63 };
64 
65 /* A discrete step pair (mouse wheels) */
66 struct discrete_coords {
67 	int x, y;
68 };
69 
70 /* A pair of coordinates normalized to a [0,1] or [-1, 1] range */
71 struct normalized_range_coords {
72 	double x, y;
73 };
74 
75 /* A pair of angles in degrees */
76 struct wheel_angle {
77 	double x, y;
78 };
79 
80 /* A pair of wheel click data for the 120-normalized range */
81 struct wheel_v120 {
82 	int x, y;
83 };
84 
85 /* A pair of angles in degrees */
86 struct tilt_degrees {
87 	double x, y;
88 };
89 
90 /* A threshold with an upper and lower limit */
91 struct threshold {
92 	int upper;
93 	int lower;
94 };
95 
96 /* A pair of coordinates in mm */
97 struct phys_coords {
98 	double x;
99 	double y;
100 };
101 
102 /* A rectangle in mm, x/y is the top-left corner */
103 struct 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 */
109 struct device_coord_rect {
110 	int x, y;
111 	int w, h;
112 };
113 
114 /* A pair of major/minor in mm */
115 struct phys_ellipsis {
116 	double major;
117 	double minor;
118 };
119 
120 struct 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 
128 struct 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 
175 typedef void (*libinput_seat_destroy_func) (struct libinput_seat *seat);
176 
177 struct 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 
193 struct 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 
216 struct 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 
226 struct 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 
262 struct libinput_config_accel_custom_func {
263 	double step;
264 	size_t npoints;
265 	double points[LIBINPUT_ACCEL_NPOINTS_MAX];
266 };
267 
268 struct 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 
278 struct 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 
294 struct 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 
302 struct 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 
309 struct 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 
325 struct 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 
333 struct 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 
344 struct 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 
355 struct 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 
366 struct 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 
375 struct 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 
382 struct 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 
398 struct 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 
406 struct 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 
416 enum 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 
432 struct 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 
445 enum 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 
452 struct 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 
471 struct 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 
490 struct libinput_event {
491 	enum libinput_event_type type;
492 	struct libinput_device *device;
493 };
494 
495 struct 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 
501 typedef 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 
517 static inline bool
is_logged(const struct libinput *libinput, enum libinput_log_priority priority)518 is_logged(const struct libinput *libinput,
519 	  enum libinput_log_priority priority)
520 {
521        return libinput->log_handler &&
522                libinput->log_priority <= priority;
523 }
524 
525 void
526 log_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 
532 void
533 log_msg(struct libinput *libinput,
534 	enum libinput_log_priority priority,
535 	const char *format, ...)
536 	LIBINPUT_ATTRIBUTE_PRINTF(3, 4);
537 
538 void
539 log_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 
545 int
546 libinput_init(struct libinput *libinput,
547 	      const struct libinput_interface *interface,
548 	      const struct libinput_interface_backend *interface_backend,
549 	      void *user_data);
550 
551 void
552 libinput_init_quirks(struct libinput *libinput);
553 
554 struct libinput_source *
555 libinput_add_fd(struct libinput *libinput,
556 		int fd,
557 		libinput_source_dispatch_t dispatch,
558 		void *data);
559 
560 void
561 libinput_remove_source(struct libinput *libinput,
562 		       struct libinput_source *source);
563 
564 int
565 open_restricted(struct libinput *libinput,
566 		const char *path, int flags);
567 
568 void
569 close_restricted(struct libinput *libinput, int fd);
570 
571 bool
572 ignore_litest_test_suite_device(struct udev_device *device);
573 
574 void
575 libinput_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 
581 void
582 libinput_device_init(struct libinput_device *device,
583 		     struct libinput_seat *seat);
584 
585 struct libinput_device_group *
586 libinput_device_group_create(struct libinput *libinput,
587 			     const char *identifier);
588 
589 struct libinput_device_group *
590 libinput_device_group_find_group(struct libinput *libinput,
591 				 const char *identifier);
592 
593 void
594 libinput_device_set_device_group(struct libinput_device *device,
595 				 struct libinput_device_group *group);
596 
597 void
598 libinput_device_init_event_listener(struct libinput_event_listener *listener);
599 
600 void
601 libinput_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 
609 void
610 libinput_device_remove_event_listener(struct libinput_event_listener *listener);
611 
612 void
613 notify_added_device(struct libinput_device *device);
614 
615 void
616 notify_removed_device(struct libinput_device *device);
617 
618 void
619 keyboard_notify_key(struct libinput_device *device,
620 		    uint64_t time,
621 		    uint32_t key,
622 		    enum libinput_key_state state);
623 
624 void
625 pointer_notify_motion(struct libinput_device *device,
626 		      uint64_t time,
627 		      const struct normalized_coords *delta,
628 		      const struct device_float_coords *raw);
629 
630 void
631 pointer_notify_motion_absolute(struct libinput_device *device,
632 			       uint64_t time,
633 			       const struct device_coords *point);
634 
635 void
636 pointer_notify_button(struct libinput_device *device,
637 		      uint64_t time,
638 		      int32_t button,
639 		      enum libinput_button_state state);
640 
641 void
642 pointer_notify_axis_finger(struct libinput_device *device,
643 			   uint64_t time,
644 			   uint32_t axes,
645 			   const struct normalized_coords *delta);
646 void
647 pointer_notify_axis_continuous(struct libinput_device *device,
648 			       uint64_t time,
649 			       uint32_t axes,
650 			       const struct normalized_coords *delta);
651 
652 void
653 pointer_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 
659 void
660 pointer_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 
666 void
667 touch_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 
673 void
674 touch_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 
680 void
681 touch_notify_touch_up(struct libinput_device *device,
682 		      uint64_t time,
683 		      int32_t slot,
684 		      int32_t seat_slot);
685 
686 void
687 touch_notify_touch_cancel(struct libinput_device *device,
688 			  uint64_t time,
689 			  int32_t slot,
690 			  int32_t seat_slot);
691 
692 void
693 touch_notify_frame(struct libinput_device *device,
694 		   uint64_t time);
695 
696 void
697 gesture_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 
704 void
705 gesture_notify_swipe_end(struct libinput_device *device,
706 			 uint64_t time,
707 			 int finger_count,
708 			 bool cancelled);
709 
710 void
711 gesture_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 
720 void
721 gesture_notify_pinch_end(struct libinput_device *device,
722 			 uint64_t time,
723 			 int finger_count,
724 			 double scale,
725 			 bool cancelled);
726 
727 void
728 gesture_notify_hold(struct libinput_device *device,
729 		    uint64_t time,
730 		    int finger_count);
731 
732 void
733 gesture_notify_hold_end(struct libinput_device *device,
734 			uint64_t time,
735 			int finger_count,
736 			bool cancelled);
737 
738 void
739 tablet_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 
746 void
747 tablet_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 
754 void
755 tablet_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 
762 void
763 tablet_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 
771 void
772 tablet_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);
777 void
778 tablet_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);
784 void
785 tablet_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);
791 void
792 tablet_pad_notify_key(struct libinput_device *device,
793 		      uint64_t time,
794 		      int32_t key,
795 		      enum libinput_key_state state);
796 void
797 switch_notify_toggle(struct libinput_device *device,
798 		     uint64_t time,
799 		     enum libinput_switch sw,
800 		     enum libinput_switch_state state);
801 
802 static inline uint64_t
libinput_now(struct libinput *libinput)803 libinput_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 
815 static inline struct device_float_coords
device_delta(const struct device_coords a, const struct device_coords b)816 device_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 
826 static inline struct device_float_coords
device_average(const struct device_coords a, const struct device_coords b)827 device_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 
837 static inline struct device_float_coords
device_float_delta(const struct device_float_coords a, const struct device_float_coords b)838 device_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 
848 static inline struct device_float_coords
device_float_average(const struct device_float_coords a, const struct device_float_coords b)849 device_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 
859 static inline bool
device_float_is_zero(const struct device_float_coords coords)860 device_float_is_zero(const struct device_float_coords coords)
861 {
862 	return coords.x == 0.0 && coords.y == 0.0;
863 }
864 
865 static inline double
normalized_length(const struct normalized_coords norm)866 normalized_length(const struct normalized_coords norm)
867 {
868 	return hypot(norm.x, norm.y);
869 }
870 
871 static inline bool
normalized_is_zero(const struct normalized_coords norm)872 normalized_is_zero(const struct normalized_coords norm)
873 {
874 	return norm.x == 0.0 && norm.y == 0.0;
875 }
876 
877 static inline double
length_in_mm(const struct phys_coords mm)878 length_in_mm(const struct phys_coords mm)
879 {
880 	return hypot(mm.x, mm.y);
881 }
882 
883 enum 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 
895 static inline uint32_t
xy_get_direction(double x, double y)896 xy_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 
940 static inline uint32_t
phys_get_direction(const struct phys_coords mm)941 phys_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  */
950 static inline uint32_t
device_float_get_direction(const struct device_float_coords coords)951 device_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  */
960 static inline bool
point_in_rect(const struct device_coords *point, const struct device_coord_rect *rect)961 point_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
971 WacomDeviceDatabase *
972 libinput_libwacom_ref(struct libinput *li);
973 void
974 libinput_libwacom_unref(struct libinput *li);
975 #else
libinput_libwacom_ref(struct libinput *li)976 static inline void *libinput_libwacom_ref(struct libinput *li) { return NULL; }
libinput_libwacom_unref(struct libinput *li)977 static inline void libinput_libwacom_unref(struct libinput *li) {}
978 #endif
979 
980 #endif /* LIBINPUT_PRIVATE_H */
981