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