• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	} timer;
140 
141 	struct libinput_event **events;
142 	size_t events_count;
143 	size_t events_len;
144 	size_t events_in;
145 	size_t events_out;
146 
147 	struct list tool_list;
148 
149 	const struct libinput_interface *interface;
150 	const struct libinput_interface_backend *interface_backend;
151 
152 	libinput_log_handler log_handler;
153 	enum libinput_log_priority log_priority;
154 	void *user_data;
155 	int refcount;
156 
157 	struct list device_group_list;
158 
159 	uint64_t last_event_time;
160 	uint64_t dispatch_time;
161 
162 	bool quirks_initialized;
163 	struct quirks_context *quirks;
164 
165 #if HAVE_LIBWACOM
166 	struct {
167 		WacomDeviceDatabase *db;
168 		size_t refcount;
169 	} libwacom;
170 #endif
171 };
172 
173 typedef void (*libinput_seat_destroy_func) (struct libinput_seat *seat);
174 
175 struct libinput_seat {
176 	struct libinput *libinput;
177 	struct list link;
178 	struct list devices_list;
179 	void *user_data;
180 	int refcount;
181 	libinput_seat_destroy_func destroy;
182 
183 	char *physical_name;
184 	char *logical_name;
185 
186 	uint32_t slot_map;
187 
188 	uint32_t button_count[KEY_CNT];
189 };
190 
191 struct libinput_device_config_tap {
192 	int (*count)(struct libinput_device *device);
193 	enum libinput_config_status (*set_enabled)(struct libinput_device *device,
194 						   enum libinput_config_tap_state enable);
195 	enum libinput_config_tap_state (*get_enabled)(struct libinput_device *device);
196 	enum libinput_config_tap_state (*get_default)(struct libinput_device *device);
197 
198 	enum libinput_config_status (*set_map)(struct libinput_device *device,
199 						   enum libinput_config_tap_button_map map);
200 	enum libinput_config_tap_button_map (*get_map)(struct libinput_device *device);
201 	enum libinput_config_tap_button_map (*get_default_map)(struct libinput_device *device);
202 
203 	enum libinput_config_status (*set_drag_enabled)(struct libinput_device *device,
204 							enum libinput_config_drag_state);
205 	enum libinput_config_drag_state (*get_drag_enabled)(struct libinput_device *device);
206 	enum libinput_config_drag_state (*get_default_drag_enabled)(struct libinput_device *device);
207 
208 	enum libinput_config_status (*set_draglock_enabled)(struct libinput_device *device,
209 							    enum libinput_config_drag_lock_state);
210 	enum libinput_config_drag_lock_state (*get_draglock_enabled)(struct libinput_device *device);
211 	enum libinput_config_drag_lock_state (*get_default_draglock_enabled)(struct libinput_device *device);
212 };
213 
214 struct libinput_device_config_calibration {
215 	int (*has_matrix)(struct libinput_device *device);
216 	enum libinput_config_status (*set_matrix)(struct libinput_device *device,
217 						  const float matrix[6]);
218 	int (*get_matrix)(struct libinput_device *device,
219 			  float matrix[6]);
220 	int (*get_default_matrix)(struct libinput_device *device,
221 							  float matrix[6]);
222 };
223 
224 struct libinput_device_config_send_events {
225 	uint32_t (*get_modes)(struct libinput_device *device);
226 	enum libinput_config_status (*set_mode)(struct libinput_device *device,
227 						   enum libinput_config_send_events_mode mode);
228 	enum libinput_config_send_events_mode (*get_mode)(struct libinput_device *device);
229 	enum libinput_config_send_events_mode (*get_default_mode)(struct libinput_device *device);
230 };
231 
232 struct libinput_device_config_accel {
233 	int (*available)(struct libinput_device *device);
234 	enum libinput_config_status (*set_speed)(struct libinput_device *device,
235 						 double speed);
236 	double (*get_speed)(struct libinput_device *device);
237 	double (*get_default_speed)(struct libinput_device *device);
238 
239 	uint32_t (*get_profiles)(struct libinput_device *device);
240 	enum libinput_config_status (*set_profile)(struct libinput_device *device,
241 						   enum libinput_config_accel_profile);
242 	enum libinput_config_accel_profile (*get_profile)(struct libinput_device *device);
243 	enum libinput_config_accel_profile (*get_default_profile)(struct libinput_device *device);
244 };
245 
246 struct libinput_device_config_natural_scroll {
247 	int (*has)(struct libinput_device *device);
248 	enum libinput_config_status (*set_enabled)(struct libinput_device *device,
249 						   int enabled);
250 	int (*get_enabled)(struct libinput_device *device);
251 	int (*get_default_enabled)(struct libinput_device *device);
252 };
253 
254 struct libinput_device_config_left_handed {
255 	int (*has)(struct libinput_device *device);
256 	enum libinput_config_status (*set)(struct libinput_device *device, int left_handed);
257 	int (*get)(struct libinput_device *device);
258 	int (*get_default)(struct libinput_device *device);
259 };
260 
261 struct libinput_device_config_scroll_method {
262 	uint32_t (*get_methods)(struct libinput_device *device);
263 	enum libinput_config_status (*set_method)(struct libinput_device *device,
264 						  enum libinput_config_scroll_method method);
265 	enum libinput_config_scroll_method (*get_method)(struct libinput_device *device);
266 	enum libinput_config_scroll_method (*get_default_method)(struct libinput_device *device);
267 	enum libinput_config_status (*set_button)(struct libinput_device *device,
268 						  uint32_t button);
269 	uint32_t (*get_button)(struct libinput_device *device);
270 	uint32_t (*get_default_button)(struct libinput_device *device);
271 	enum libinput_config_status (*set_button_lock)(struct libinput_device *device,
272 						       enum libinput_config_scroll_button_lock_state);
273 	enum libinput_config_scroll_button_lock_state (*get_button_lock)(struct libinput_device *device);
274 	enum libinput_config_scroll_button_lock_state (*get_default_button_lock)(struct libinput_device *device);
275 };
276 
277 struct libinput_device_config_click_method {
278 	uint32_t (*get_methods)(struct libinput_device *device);
279 	enum libinput_config_status (*set_method)(struct libinput_device *device,
280 						  enum libinput_config_click_method method);
281 	enum libinput_config_click_method (*get_method)(struct libinput_device *device);
282 	enum libinput_config_click_method (*get_default_method)(struct libinput_device *device);
283 };
284 
285 struct libinput_device_config_middle_emulation {
286 	int (*available)(struct libinput_device *device);
287 	enum libinput_config_status (*set)(
288 			 struct libinput_device *device,
289 			 enum libinput_config_middle_emulation_state);
290 	enum libinput_config_middle_emulation_state (*get)(
291 			 struct libinput_device *device);
292 	enum libinput_config_middle_emulation_state (*get_default)(
293 			 struct libinput_device *device);
294 };
295 
296 struct libinput_device_config_dwt {
297 	int (*is_available)(struct libinput_device *device);
298 	enum libinput_config_status (*set_enabled)(
299 			 struct libinput_device *device,
300 			 enum libinput_config_dwt_state enable);
301 	enum libinput_config_dwt_state (*get_enabled)(
302 			 struct libinput_device *device);
303 	enum libinput_config_dwt_state (*get_default_enabled)(
304 			 struct libinput_device *device);
305 };
306 
307 struct libinput_device_config_rotation {
308 	int (*is_available)(struct libinput_device *device);
309 	enum libinput_config_status (*set_angle)(
310 			 struct libinput_device *device,
311 			 unsigned int degrees_cw);
312 	unsigned int (*get_angle)(struct libinput_device *device);
313 	unsigned int (*get_default_angle)(struct libinput_device *device);
314 };
315 
316 struct libinput_device_config_gesture {
317 	enum libinput_config_status (*set_hold_enabled)(struct libinput_device *device,
318 			 enum libinput_config_hold_state enabled);
319 	enum libinput_config_hold_state (*get_hold_enabled)(struct libinput_device *device);
320 	enum libinput_config_hold_state (*get_hold_default)(struct libinput_device *device);
321 };
322 
323 struct libinput_device_config {
324 	struct libinput_device_config_tap *tap;
325 	struct libinput_device_config_calibration *calibration;
326 	struct libinput_device_config_send_events *sendevents;
327 	struct libinput_device_config_accel *accel;
328 	struct libinput_device_config_natural_scroll *natural_scroll;
329 	struct libinput_device_config_left_handed *left_handed;
330 	struct libinput_device_config_scroll_method *scroll_method;
331 	struct libinput_device_config_click_method *click_method;
332 	struct libinput_device_config_middle_emulation *middle_emulation;
333 	struct libinput_device_config_dwt *dwt;
334 	struct libinput_device_config_rotation *rotation;
335 	struct libinput_device_config_gesture *gesture;
336 };
337 
338 struct libinput_device_group {
339 	int refcount;
340 	void *user_data;
341 	char *identifier; /* unique identifier or NULL for singletons */
342 
343 	struct list link;
344 };
345 
346 struct libinput_device {
347 	struct libinput_seat *seat;
348 	struct libinput_device_group *group;
349 	struct list link;
350 	struct list event_listeners;
351 	void *user_data;
352 	int refcount;
353 	struct libinput_device_config config;
354 };
355 
356 enum libinput_tablet_tool_axis {
357 	LIBINPUT_TABLET_TOOL_AXIS_X = 1,
358 	LIBINPUT_TABLET_TOOL_AXIS_Y = 2,
359 	LIBINPUT_TABLET_TOOL_AXIS_DISTANCE = 3,
360 	LIBINPUT_TABLET_TOOL_AXIS_PRESSURE = 4,
361 	LIBINPUT_TABLET_TOOL_AXIS_TILT_X = 5,
362 	LIBINPUT_TABLET_TOOL_AXIS_TILT_Y = 6,
363 	LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z = 7,
364 	LIBINPUT_TABLET_TOOL_AXIS_SLIDER = 8,
365 	LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL = 9,
366 	LIBINPUT_TABLET_TOOL_AXIS_SIZE_MAJOR = 10,
367 	LIBINPUT_TABLET_TOOL_AXIS_SIZE_MINOR = 11,
368 };
369 
370 #define LIBINPUT_TABLET_TOOL_AXIS_MAX LIBINPUT_TABLET_TOOL_AXIS_SIZE_MINOR
371 
372 struct tablet_axes {
373 	struct device_coords point;
374 	struct normalized_coords delta;
375 	double distance;
376 	double pressure;
377 	struct tilt_degrees tilt;
378 	double rotation;
379 	double slider;
380 	double wheel;
381 	int wheel_discrete;
382 	struct phys_ellipsis size;
383 };
384 
385 struct libinput_tablet_tool {
386 	struct list link;
387 	uint32_t serial;
388 	uint32_t tool_id;
389 	enum libinput_tablet_tool_type type;
390 	unsigned char axis_caps[NCHARS(LIBINPUT_TABLET_TOOL_AXIS_MAX + 1)];
391 	unsigned char buttons[NCHARS(KEY_MAX) + 1];
392 	int refcount;
393 	void *user_data;
394 
395 	struct {
396 		/* The pressure threshold assumes a pressure_offset of 0 */
397 		struct threshold threshold;
398 		/* pressure_offset includes axis->minimum */
399 		int offset;
400 		bool has_offset;
401 	} pressure;
402 };
403 
404 struct libinput_tablet_pad_mode_group {
405 	struct libinput_device *device;
406 	struct list link;
407 	int refcount;
408 	void *user_data;
409 
410 	unsigned int index;
411 	unsigned int num_modes;
412 	unsigned int current_mode;
413 
414 	uint32_t button_mask;
415 	uint32_t ring_mask;
416 	uint32_t strip_mask;
417 
418 	uint32_t toggle_button_mask;
419 
420 	void (*destroy)(struct libinput_tablet_pad_mode_group *group);
421 };
422 
423 struct libinput_event {
424 	enum libinput_event_type type;
425 	struct libinput_device *device;
426 };
427 
428 struct libinput_event_listener {
429 	struct list link;
430 	void (*notify_func)(uint64_t time, struct libinput_event *ev, void *notify_func_data);
431 	void *notify_func_data;
432 };
433 
434 typedef void (*libinput_source_dispatch_t)(void *data);
435 
436 #define log_debug(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
437 #define log_info(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
438 #define log_error(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
439 #define log_bug_kernel(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
440 #define log_bug_libinput(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
441 #define log_bug_client(li_, ...) log_msg((li_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
442 
443 #define log_debug_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
444 #define log_info_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
445 #define log_error_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
446 #define log_bug_kernel_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
447 #define log_bug_libinput_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
448 #define log_bug_client_ratelimit(li_, r_, ...) log_msg_ratelimit((li_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
449 
450 static inline bool
is_logged(const struct libinput * libinput,enum libinput_log_priority priority)451 is_logged(const struct libinput *libinput,
452 	  enum libinput_log_priority priority)
453 {
454        return libinput->log_handler &&
455                libinput->log_priority <= priority;
456 }
457 
458 
459 void
460 log_msg_ratelimit(struct libinput *libinput,
461 		  struct ratelimit *ratelimit,
462 		  enum libinput_log_priority priority,
463 		  const char *format, ...)
464 	LIBINPUT_ATTRIBUTE_PRINTF(4, 5);
465 
466 void
467 log_msg(struct libinput *libinput,
468 	enum libinput_log_priority priority,
469 	const char *format, ...)
470 	LIBINPUT_ATTRIBUTE_PRINTF(3, 4);
471 
472 void
473 log_msg_va(struct libinput *libinput,
474 	   enum libinput_log_priority priority,
475 	   const char *format,
476 	   va_list args)
477 	LIBINPUT_ATTRIBUTE_PRINTF(3, 0);
478 
479 int
480 libinput_init(struct libinput *libinput,
481 	      const struct libinput_interface *interface,
482 	      const struct libinput_interface_backend *interface_backend,
483 	      void *user_data);
484 
485 void
486 libinput_init_quirks(struct libinput *libinput);
487 
488 struct libinput_source *
489 libinput_add_fd(struct libinput *libinput,
490 		int fd,
491 		libinput_source_dispatch_t dispatch,
492 		void *data);
493 
494 void
495 libinput_remove_source(struct libinput *libinput,
496 		       struct libinput_source *source);
497 
498 int
499 open_restricted(struct libinput *libinput,
500 		const char *path, int flags);
501 
502 void
503 close_restricted(struct libinput *libinput, int fd);
504 
505 bool
506 ignore_litest_test_suite_device(struct udev_device *device);
507 
508 void
509 libinput_seat_init(struct libinput_seat *seat,
510 		   struct libinput *libinput,
511 		   const char *physical_name,
512 		   const char *logical_name,
513 		   libinput_seat_destroy_func destroy);
514 
515 void
516 libinput_device_init(struct libinput_device *device,
517 		     struct libinput_seat *seat);
518 
519 struct libinput_device_group *
520 libinput_device_group_create(struct libinput *libinput,
521 			     const char *identifier);
522 
523 struct libinput_device_group *
524 libinput_device_group_find_group(struct libinput *libinput,
525 				 const char *identifier);
526 
527 void
528 libinput_device_set_device_group(struct libinput_device *device,
529 				 struct libinput_device_group *group);
530 
531 void
532 libinput_device_init_event_listener(struct libinput_event_listener *listener);
533 
534 void
535 libinput_device_add_event_listener(struct libinput_device *device,
536 				   struct libinput_event_listener *listener,
537 				   void (*notify_func)(
538 						uint64_t time,
539 						struct libinput_event *event,
540 						void *notify_func_data),
541 				   void *notify_func_data);
542 
543 void
544 libinput_device_remove_event_listener(struct libinput_event_listener *listener);
545 
546 void
547 notify_added_device(struct libinput_device *device);
548 
549 void
550 notify_removed_device(struct libinput_device *device);
551 
552 void
553 keyboard_notify_key(struct libinput_device *device,
554 		    uint64_t time,
555 		    uint32_t key,
556 		    enum libinput_key_state state);
557 
558 void
559 pointer_notify_motion(struct libinput_device *device,
560 		      uint64_t time,
561 		      const struct normalized_coords *delta,
562 		      const struct device_float_coords *raw);
563 
564 void
565 pointer_notify_motion_absolute(struct libinput_device *device,
566 			       uint64_t time,
567 			       const struct device_coords *point);
568 
569 void
570 pointer_notify_button(struct libinput_device *device,
571 		      uint64_t time,
572 		      int32_t button,
573 		      enum libinput_button_state state);
574 
575 void
576 pointer_notify_axis_finger(struct libinput_device *device,
577 			   uint64_t time,
578 			   uint32_t axes,
579 			   const struct normalized_coords *delta);
580 void
581 pointer_notify_axis_continuous(struct libinput_device *device,
582 			       uint64_t time,
583 			       uint32_t axes,
584 			       const struct normalized_coords *delta);
585 
586 void
587 pointer_notify_axis_legacy_wheel(struct libinput_device *device,
588 				 uint64_t time,
589 				 uint32_t axes,
590 				 const struct normalized_coords *delta,
591 				 const struct discrete_coords *discrete);
592 
593 void
594 pointer_notify_axis_wheel(struct libinput_device *device,
595 			  uint64_t time,
596 			  uint32_t axes,
597 			  const struct normalized_coords *delta,
598 			  const struct wheel_v120 *v120);
599 
600 void
601 touch_notify_touch_down(struct libinput_device *device,
602 			uint64_t time,
603 			int32_t slot,
604 			int32_t seat_slot,
605 			const struct device_coords *point);
606 
607 void
608 touch_notify_touch_motion(struct libinput_device *device,
609 			  uint64_t time,
610 			  int32_t slot,
611 			  int32_t seat_slot,
612 			  const struct device_coords *point);
613 
614 void
615 touch_notify_touch_up(struct libinput_device *device,
616 		      uint64_t time,
617 		      int32_t slot,
618 		      int32_t seat_slot);
619 
620 void
621 touch_notify_touch_cancel(struct libinput_device *device,
622 			  uint64_t time,
623 			  int32_t slot,
624 			  int32_t seat_slot);
625 
626 void
627 touch_notify_frame(struct libinput_device *device,
628 		   uint64_t time);
629 
630 void
631 gesture_notify_swipe(struct libinput_device *device,
632 		     uint64_t time,
633 		     enum libinput_event_type type,
634 		     int finger_count,
635 		     const struct normalized_coords *delta,
636 		     const struct normalized_coords *unaccel);
637 
638 void
639 gesture_notify_swipe_end(struct libinput_device *device,
640 			 uint64_t time,
641 			 int finger_count,
642 			 bool cancelled);
643 
644 void
645 gesture_notify_pinch(struct libinput_device *device,
646 		     uint64_t time,
647 		     enum libinput_event_type type,
648 		     int finger_count,
649 		     const struct normalized_coords *delta,
650 		     const struct normalized_coords *unaccel,
651 		     double scale,
652 		     double angle);
653 
654 void
655 gesture_notify_pinch_end(struct libinput_device *device,
656 			 uint64_t time,
657 			 int finger_count,
658 			 double scale,
659 			 bool cancelled);
660 
661 void
662 gesture_notify_hold(struct libinput_device *device,
663 		    uint64_t time,
664 		    int finger_count);
665 
666 void
667 gesture_notify_hold_end(struct libinput_device *device,
668 			uint64_t time,
669 			int finger_count,
670 			bool cancelled);
671 
672 void
673 tablet_notify_axis(struct libinput_device *device,
674 		   uint64_t time,
675 		   struct libinput_tablet_tool *tool,
676 		   enum libinput_tablet_tool_tip_state tip_state,
677 		   unsigned char *changed_axes,
678 		   const struct tablet_axes *axes);
679 
680 void
681 tablet_notify_proximity(struct libinput_device *device,
682 			uint64_t time,
683 			struct libinput_tablet_tool *tool,
684 			enum libinput_tablet_tool_proximity_state state,
685 			unsigned char *changed_axes,
686 			const struct tablet_axes *axes);
687 
688 void
689 tablet_notify_tip(struct libinput_device *device,
690 		  uint64_t time,
691 		  struct libinput_tablet_tool *tool,
692 		  enum libinput_tablet_tool_tip_state tip_state,
693 		  unsigned char *changed_axes,
694 		  const struct tablet_axes *axes);
695 
696 void
697 tablet_notify_button(struct libinput_device *device,
698 		     uint64_t time,
699 		     struct libinput_tablet_tool *tool,
700 		     enum libinput_tablet_tool_tip_state tip_state,
701 		     const struct tablet_axes *axes,
702 		     int32_t button,
703 		     enum libinput_button_state state);
704 
705 void
706 tablet_pad_notify_button(struct libinput_device *device,
707 			 uint64_t time,
708 			 int32_t button,
709 			 enum libinput_button_state state,
710 			 struct libinput_tablet_pad_mode_group *group);
711 void
712 tablet_pad_notify_ring(struct libinput_device *device,
713 		       uint64_t time,
714 		       unsigned int number,
715 		       double value,
716 		       enum libinput_tablet_pad_ring_axis_source source,
717 		       struct libinput_tablet_pad_mode_group *group);
718 void
719 tablet_pad_notify_strip(struct libinput_device *device,
720 			uint64_t time,
721 			unsigned int number,
722 			double value,
723 			enum libinput_tablet_pad_strip_axis_source source,
724 			struct libinput_tablet_pad_mode_group *group);
725 void
726 tablet_pad_notify_key(struct libinput_device *device,
727 		      uint64_t time,
728 		      int32_t key,
729 		      enum libinput_key_state state);
730 void
731 switch_notify_toggle(struct libinput_device *device,
732 		     uint64_t time,
733 		     enum libinput_switch sw,
734 		     enum libinput_switch_state state);
735 
736 static inline uint64_t
libinput_now(struct libinput * libinput)737 libinput_now(struct libinput *libinput)
738 {
739 	struct timespec ts = { 0, 0 };
740 
741 	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
742 		log_error(libinput, "clock_gettime failed: %s\n", strerror(errno));
743 		return 0;
744 	}
745 
746 	return s2us(ts.tv_sec) + ns2us(ts.tv_nsec);
747 }
748 
749 static inline struct device_float_coords
device_delta(const struct device_coords a,const struct device_coords b)750 device_delta(const struct device_coords a, const struct device_coords b)
751 {
752 	struct device_float_coords delta;
753 
754 	delta.x = a.x - b.x;
755 	delta.y = a.y - b.y;
756 
757 	return delta;
758 }
759 
760 static inline struct device_float_coords
device_average(const struct device_coords a,const struct device_coords b)761 device_average(const struct device_coords a, const struct device_coords b)
762 {
763 	struct device_float_coords average;
764 
765 	average.x = (a.x + b.x) / 2.0;
766 	average.y = (a.y + b.y) / 2.0;
767 
768 	return average;
769 }
770 
771 static inline struct device_float_coords
device_float_delta(const struct device_float_coords a,const struct device_float_coords b)772 device_float_delta(const struct device_float_coords a, const struct device_float_coords b)
773 {
774 	struct device_float_coords delta;
775 
776 	delta.x = a.x - b.x;
777 	delta.y = a.y - b.y;
778 
779 	return delta;
780 }
781 
782 static inline struct device_float_coords
device_float_average(const struct device_float_coords a,const struct device_float_coords b)783 device_float_average(const struct device_float_coords a, const struct device_float_coords b)
784 {
785 	struct device_float_coords average;
786 
787 	average.x = (a.x + b.x) / 2.0;
788 	average.y = (a.y + b.y) / 2.0;
789 
790 	return average;
791 }
792 
793 static inline bool
device_float_is_zero(const struct device_float_coords coords)794 device_float_is_zero(const struct device_float_coords coords)
795 {
796 	return coords.x == 0.0 && coords.y == 0.0;
797 }
798 
799 static inline double
normalized_length(const struct normalized_coords norm)800 normalized_length(const struct normalized_coords norm)
801 {
802 	return hypot(norm.x, norm.y);
803 }
804 
805 static inline bool
normalized_is_zero(const struct normalized_coords norm)806 normalized_is_zero(const struct normalized_coords norm)
807 {
808 	return norm.x == 0.0 && norm.y == 0.0;
809 }
810 
811 static inline double
length_in_mm(const struct phys_coords mm)812 length_in_mm(const struct phys_coords mm)
813 {
814 	return hypot(mm.x, mm.y);
815 }
816 
817 enum directions {
818 	N  = bit(0),
819 	NE = bit(1),
820 	E  = bit(2),
821 	SE = bit(3),
822 	S  = bit(4),
823 	SW = bit(5),
824 	W  = bit(6),
825 	NW = bit(7),
826 	UNDEFINED_DIRECTION = 0xff
827 };
828 
829 static inline uint32_t
xy_get_direction(double x,double y)830 xy_get_direction(double x, double y)
831 {
832 	uint32_t dir = UNDEFINED_DIRECTION;
833 	int d1, d2;
834 	double r;
835 
836 	if (fabs(x) < 2.0 && fabs(y) < 2.0) {
837 		if (x > 0.0 && y > 0.0)
838 			dir = S | SE | E;
839 		else if (x > 0.0 && y < 0.0)
840 			dir = N | NE | E;
841 		else if (x < 0.0 && y > 0.0)
842 			dir = S | SW | W;
843 		else if (x < 0.0 && y < 0.0)
844 			dir = N | NW | W;
845 		else if (x > 0.0)
846 			dir = NE | E | SE;
847 		else if (x < 0.0)
848 			dir = NW | W | SW;
849 		else if (y > 0.0)
850 			dir = SE | S | SW;
851 		else if (y < 0.0)
852 			dir = NE | N | NW;
853 	} else {
854 		/* Calculate r within the interval  [0 to 8)
855 		 *
856 		 * r = [0 .. 2π] where 0 is North
857 		 * d_f = r / 2π  ([0 .. 1))
858 		 * d_8 = 8 * d_f
859 		 */
860 		r = atan2(y, x);
861 		r = fmod(r + 2.5*M_PI, 2*M_PI);
862 		r *= 4*M_1_PI;
863 
864 		/* Mark one or two close enough octants */
865 		d1 = (int)(r + 0.9) % 8;
866 		d2 = (int)(r + 0.1) % 8;
867 
868 		dir = (1 << d1) | (1 << d2);
869 	}
870 
871 	return dir;
872 }
873 
874 static inline uint32_t
phys_get_direction(const struct phys_coords mm)875 phys_get_direction(const struct phys_coords mm)
876 {
877 	return xy_get_direction(mm.x, mm.y);
878 }
879 
880 /**
881  * Get the direction for the given set of coordinates.
882  * assumption: coordinates are normalized to one axis resolution.
883  */
884 static inline uint32_t
device_float_get_direction(const struct device_float_coords coords)885 device_float_get_direction(const struct device_float_coords coords)
886 {
887 	return xy_get_direction(coords.x, coords.y);
888 }
889 
890 /**
891  * Returns true if the point is within the given rectangle, including the
892  * left edge but excluding the right edge.
893  */
894 static inline bool
point_in_rect(const struct device_coords * point,const struct device_coord_rect * rect)895 point_in_rect(const struct device_coords *point,
896 	      const struct device_coord_rect *rect)
897 {
898 	return (point->x >= rect->x &&
899 		point->x < rect->x + rect->w &&
900 		point->y >= rect->y &&
901 		point->y < rect->y + rect->h);
902 }
903 
904 #if HAVE_LIBWACOM
905 WacomDeviceDatabase *
906 libinput_libwacom_ref(struct libinput *li);
907 void
908 libinput_libwacom_unref(struct libinput *li);
909 #else
libinput_libwacom_ref(struct libinput * li)910 static inline void *libinput_libwacom_ref(struct libinput *li) { return NULL; }
libinput_libwacom_unref(struct libinput * li)911 static inline void libinput_libwacom_unref(struct libinput *li) {}
912 #endif
913 
914 
915 #endif /* LIBINPUT_PRIVATE_H */
916