• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013 Jonas Ådahl
3  * Copyright © 2013-2018 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 #include "config.h"
26 
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <sys/epoll.h>
34 #include <unistd.h>
35 #include <assert.h>
36 
37 #include "libinput.h"
38 #include "libinput-private.h"
39 #include "evdev.h"
40 #include "timer.h"
41 #include "quirks.h"
42 
43 #define require_event_type(li_, type_, retval_, ...)	\
44 	if (type_ == LIBINPUT_EVENT_NONE) abort(); \
45 	if (!check_event_type(li_, __func__, type_, __VA_ARGS__, -1)) \
46 		return retval_; \
47 
48 #define ASSERT_INT_SIZE(type_) \
49 	static_assert(sizeof(type_) == sizeof(unsigned int), \
50 		      "sizeof("  #type_ ") must be sizeof(uint)")
51 
52 ASSERT_INT_SIZE(enum libinput_log_priority);
53 ASSERT_INT_SIZE(enum libinput_device_capability);
54 ASSERT_INT_SIZE(enum libinput_key_state);
55 ASSERT_INT_SIZE(enum libinput_led);
56 ASSERT_INT_SIZE(enum libinput_button_state);
57 ASSERT_INT_SIZE(enum libinput_pointer_axis);
58 ASSERT_INT_SIZE(enum libinput_pointer_axis_source);
59 ASSERT_INT_SIZE(enum libinput_tablet_pad_ring_axis_source);
60 ASSERT_INT_SIZE(enum libinput_tablet_pad_strip_axis_source);
61 ASSERT_INT_SIZE(enum libinput_tablet_tool_type);
62 ASSERT_INT_SIZE(enum libinput_tablet_tool_proximity_state);
63 ASSERT_INT_SIZE(enum libinput_tablet_tool_tip_state);
64 ASSERT_INT_SIZE(enum libinput_switch_state);
65 ASSERT_INT_SIZE(enum libinput_switch);
66 ASSERT_INT_SIZE(enum libinput_event_type);
67 ASSERT_INT_SIZE(enum libinput_config_status);
68 ASSERT_INT_SIZE(enum libinput_config_tap_state);
69 ASSERT_INT_SIZE(enum libinput_config_tap_button_map);
70 ASSERT_INT_SIZE(enum libinput_config_drag_state);
71 ASSERT_INT_SIZE(enum libinput_config_drag_lock_state);
72 ASSERT_INT_SIZE(enum libinput_config_send_events_mode);
73 ASSERT_INT_SIZE(enum libinput_config_accel_profile);
74 ASSERT_INT_SIZE(enum libinput_config_click_method);
75 ASSERT_INT_SIZE(enum libinput_config_middle_emulation_state);
76 ASSERT_INT_SIZE(enum libinput_config_scroll_method);
77 ASSERT_INT_SIZE(enum libinput_config_dwt_state);
78 
79 static inline bool
check_event_type(struct libinput * libinput,const char * function_name,unsigned int type_in,...)80 check_event_type(struct libinput *libinput,
81 		 const char *function_name,
82 		 unsigned int type_in,
83 		 ...)
84 {
85 	bool rc = false;
86 	va_list args;
87 	unsigned int type_permitted;
88 
89 	va_start(args, type_in);
90 	type_permitted = va_arg(args, unsigned int);
91 
92 	while (type_permitted != (unsigned int)-1) {
93 		if (type_permitted == type_in) {
94 			rc = true;
95 			break;
96 		}
97 		type_permitted = va_arg(args, unsigned int);
98 	}
99 
100 	va_end(args);
101 
102 	if (!rc)
103 		log_bug_client(libinput,
104 			       "Invalid event type %d passed to %s()\n",
105 			       type_in, function_name);
106 
107 	return rc;
108 }
109 
110 static inline const char *
event_type_to_str(enum libinput_event_type type)111 event_type_to_str(enum libinput_event_type type)
112 {
113 	switch(type) {
114 	CASE_RETURN_STRING(LIBINPUT_EVENT_DEVICE_ADDED);
115 	CASE_RETURN_STRING(LIBINPUT_EVENT_DEVICE_REMOVED);
116 	CASE_RETURN_STRING(LIBINPUT_EVENT_KEYBOARD_KEY);
117 	CASE_RETURN_STRING(LIBINPUT_EVENT_POINTER_MOTION);
118 	CASE_RETURN_STRING(LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE);
119 	CASE_RETURN_STRING(LIBINPUT_EVENT_POINTER_BUTTON);
120 	CASE_RETURN_STRING(LIBINPUT_EVENT_POINTER_AXIS);
121 	CASE_RETURN_STRING(LIBINPUT_EVENT_TOUCH_DOWN);
122 	CASE_RETURN_STRING(LIBINPUT_EVENT_TOUCH_UP);
123 	CASE_RETURN_STRING(LIBINPUT_EVENT_TOUCH_MOTION);
124 	CASE_RETURN_STRING(LIBINPUT_EVENT_TOUCH_CANCEL);
125 	CASE_RETURN_STRING(LIBINPUT_EVENT_TOUCH_FRAME);
126 	CASE_RETURN_STRING(LIBINPUT_EVENT_TABLET_TOOL_AXIS);
127 	CASE_RETURN_STRING(LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
128 	CASE_RETURN_STRING(LIBINPUT_EVENT_TABLET_TOOL_TIP);
129 	CASE_RETURN_STRING(LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
130 	CASE_RETURN_STRING(LIBINPUT_EVENT_TABLET_PAD_BUTTON);
131 	CASE_RETURN_STRING(LIBINPUT_EVENT_TABLET_PAD_RING);
132 	CASE_RETURN_STRING(LIBINPUT_EVENT_TABLET_PAD_STRIP);
133 	CASE_RETURN_STRING(LIBINPUT_EVENT_TABLET_PAD_KEY);
134 	CASE_RETURN_STRING(LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN);
135 	CASE_RETURN_STRING(LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE);
136 	CASE_RETURN_STRING(LIBINPUT_EVENT_GESTURE_SWIPE_END);
137 	CASE_RETURN_STRING(LIBINPUT_EVENT_GESTURE_PINCH_BEGIN);
138 	CASE_RETURN_STRING(LIBINPUT_EVENT_GESTURE_PINCH_UPDATE);
139 	CASE_RETURN_STRING(LIBINPUT_EVENT_GESTURE_PINCH_END);
140 	CASE_RETURN_STRING(LIBINPUT_EVENT_SWITCH_TOGGLE);
141 	case LIBINPUT_EVENT_NONE:
142 		abort();
143 	}
144 
145 	return NULL;
146 }
147 
148 struct libinput_source {
149 	libinput_source_dispatch_t dispatch;
150 	void *user_data;
151 	int fd;
152 	struct list link;
153 };
154 
155 struct libinput_event_device_notify {
156 	struct libinput_event base;
157 };
158 
159 struct libinput_event_keyboard {
160 	struct libinput_event base;
161 	uint64_t time;
162 	uint32_t key;
163 	uint32_t seat_key_count;
164 	enum libinput_key_state state;
165 };
166 
167 struct libinput_event_pointer {
168 	struct libinput_event base;
169 	uint64_t time;
170 	struct normalized_coords delta;
171 	struct device_float_coords delta_raw;
172 	struct device_coords absolute;
173 	struct discrete_coords discrete;
174 	uint32_t button;
175 	uint32_t seat_button_count;
176 	enum libinput_button_state state;
177 	enum libinput_pointer_axis_source source;
178 	uint32_t axes;
179 };
180 
181 struct libinput_event_touch {
182 	struct libinput_event base;
183 	uint64_t time;
184 	int32_t slot;
185 	int32_t seat_slot;
186 	struct device_coords point;
187 };
188 
189 struct libinput_event_gesture {
190 	struct libinput_event base;
191 	uint64_t time;
192 	int finger_count;
193 	int cancelled;
194 	struct normalized_coords delta;
195 	struct normalized_coords delta_unaccel;
196 	double scale;
197 	double angle;
198 };
199 
200 struct libinput_event_tablet_tool {
201 	struct libinput_event base;
202 	uint32_t button;
203 	enum libinput_button_state state;
204 	uint32_t seat_button_count;
205 	uint64_t time;
206 	struct tablet_axes axes;
207 	unsigned char changed_axes[NCHARS(LIBINPUT_TABLET_TOOL_AXIS_MAX + 1)];
208 	struct libinput_tablet_tool *tool;
209 	enum libinput_tablet_tool_proximity_state proximity_state;
210 	enum libinput_tablet_tool_tip_state tip_state;
211 };
212 
213 struct libinput_event_tablet_pad {
214 	struct libinput_event base;
215 	unsigned int mode;
216 	struct libinput_tablet_pad_mode_group *mode_group;
217 	uint64_t time;
218 	struct {
219 		uint32_t number;
220 		enum libinput_button_state state;
221 	} button;
222 	struct {
223 		uint32_t code;
224 		enum libinput_key_state state;
225 	} key;
226 	struct {
227 		enum libinput_tablet_pad_ring_axis_source source;
228 		double position;
229 		int number;
230 	} ring;
231 	struct {
232 		enum libinput_tablet_pad_strip_axis_source source;
233 		double position;
234 		int number;
235 	} strip;
236 };
237 
238 struct libinput_event_switch {
239 	struct libinput_event base;
240 	uint64_t time;
241 	enum libinput_switch sw;
242 	enum libinput_switch_state state;
243 };
244 
245 LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
246 static void
libinput_default_log_func(struct libinput * libinput,enum libinput_log_priority priority,const char * format,va_list args)247 libinput_default_log_func(struct libinput *libinput,
248 			  enum libinput_log_priority priority,
249 			  const char *format, va_list args)
250 {
251 	const char *prefix;
252 
253 	switch(priority) {
254 	case LIBINPUT_LOG_PRIORITY_DEBUG: prefix = "debug"; break;
255 	case LIBINPUT_LOG_PRIORITY_INFO: prefix = "info"; break;
256 	case LIBINPUT_LOG_PRIORITY_ERROR: prefix = "error"; break;
257 	default: prefix="<invalid priority>"; break;
258 	}
259 
260 	fprintf(stderr, "libinput %s: ", prefix);
261 	vfprintf(stderr, format, args);
262 }
263 
264 void
log_msg_va(struct libinput * libinput,enum libinput_log_priority priority,const char * format,va_list args)265 log_msg_va(struct libinput *libinput,
266 	   enum libinput_log_priority priority,
267 	   const char *format,
268 	   va_list args)
269 {
270 	if (is_logged(libinput, priority))
271 		libinput->log_handler(libinput, priority, format, args);
272 }
273 
274 void
log_msg(struct libinput * libinput,enum libinput_log_priority priority,const char * format,...)275 log_msg(struct libinput *libinput,
276 	enum libinput_log_priority priority,
277 	const char *format, ...)
278 {
279 	va_list args;
280 
281 	va_start(args, format);
282 	log_msg_va(libinput, priority, format, args);
283 	va_end(args);
284 }
285 
286 void
log_msg_ratelimit(struct libinput * libinput,struct ratelimit * ratelimit,enum libinput_log_priority priority,const char * format,...)287 log_msg_ratelimit(struct libinput *libinput,
288 		  struct ratelimit *ratelimit,
289 		  enum libinput_log_priority priority,
290 		  const char *format, ...)
291 {
292 	va_list args;
293 	enum ratelimit_state state;
294 
295 	state = ratelimit_test(ratelimit);
296 	if (state == RATELIMIT_EXCEEDED)
297 		return;
298 
299 	va_start(args, format);
300 	log_msg_va(libinput, priority, format, args);
301 	va_end(args);
302 
303 	if (state == RATELIMIT_THRESHOLD)
304 		log_msg(libinput,
305 			priority,
306 			"WARNING: log rate limit exceeded (%d msgs per %dms). Discarding future messages.\n",
307 			ratelimit->burst,
308 			us2ms(ratelimit->interval));
309 }
310 
311 LIBINPUT_EXPORT void
libinput_log_set_priority(struct libinput * libinput,enum libinput_log_priority priority)312 libinput_log_set_priority(struct libinput *libinput,
313 			  enum libinput_log_priority priority)
314 {
315 	libinput->log_priority = priority;
316 }
317 
318 LIBINPUT_EXPORT enum libinput_log_priority
libinput_log_get_priority(const struct libinput * libinput)319 libinput_log_get_priority(const struct libinput *libinput)
320 {
321 	return libinput->log_priority;
322 }
323 
324 LIBINPUT_EXPORT void
libinput_log_set_handler(struct libinput * libinput,libinput_log_handler log_handler)325 libinput_log_set_handler(struct libinput *libinput,
326 			 libinput_log_handler log_handler)
327 {
328 	libinput->log_handler = log_handler;
329 }
330 
331 static void
332 libinput_device_group_destroy(struct libinput_device_group *group);
333 
334 static void
335 libinput_post_event(struct libinput *libinput,
336 		    struct libinput_event *event);
337 
338 LIBINPUT_EXPORT enum libinput_event_type
libinput_event_get_type(struct libinput_event * event)339 libinput_event_get_type(struct libinput_event *event)
340 {
341 	return event->type;
342 }
343 
344 LIBINPUT_EXPORT struct libinput *
libinput_event_get_context(struct libinput_event * event)345 libinput_event_get_context(struct libinput_event *event)
346 {
347 	return event->device->seat->libinput;
348 }
349 
350 LIBINPUT_EXPORT struct libinput_device *
libinput_event_get_device(struct libinput_event * event)351 libinput_event_get_device(struct libinput_event *event)
352 {
353 	return event->device;
354 }
355 
356 LIBINPUT_EXPORT struct libinput_event_pointer *
libinput_event_get_pointer_event(struct libinput_event * event)357 libinput_event_get_pointer_event(struct libinput_event *event)
358 {
359 	require_event_type(libinput_event_get_context(event),
360 			   event->type,
361 			   NULL,
362 			   LIBINPUT_EVENT_POINTER_MOTION,
363 			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
364 			   LIBINPUT_EVENT_POINTER_BUTTON,
365 			   LIBINPUT_EVENT_POINTER_AXIS);
366 
367 	return (struct libinput_event_pointer *) event;
368 }
369 
370 LIBINPUT_EXPORT struct libinput_event_keyboard *
libinput_event_get_keyboard_event(struct libinput_event * event)371 libinput_event_get_keyboard_event(struct libinput_event *event)
372 {
373 	require_event_type(libinput_event_get_context(event),
374 			   event->type,
375 			   NULL,
376 			   LIBINPUT_EVENT_KEYBOARD_KEY);
377 
378 	return (struct libinput_event_keyboard *) event;
379 }
380 
381 LIBINPUT_EXPORT struct libinput_event_touch *
libinput_event_get_touch_event(struct libinput_event * event)382 libinput_event_get_touch_event(struct libinput_event *event)
383 {
384 	require_event_type(libinput_event_get_context(event),
385 			   event->type,
386 			   NULL,
387 			   LIBINPUT_EVENT_TOUCH_DOWN,
388 			   LIBINPUT_EVENT_TOUCH_UP,
389 			   LIBINPUT_EVENT_TOUCH_MOTION,
390 			   LIBINPUT_EVENT_TOUCH_CANCEL,
391 			   LIBINPUT_EVENT_TOUCH_FRAME);
392 	return (struct libinput_event_touch *) event;
393 }
394 
395 LIBINPUT_EXPORT struct libinput_event_gesture *
libinput_event_get_gesture_event(struct libinput_event * event)396 libinput_event_get_gesture_event(struct libinput_event *event)
397 {
398 	require_event_type(libinput_event_get_context(event),
399 			   event->type,
400 			   NULL,
401 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
402 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
403 			   LIBINPUT_EVENT_GESTURE_SWIPE_END,
404 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
405 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
406 			   LIBINPUT_EVENT_GESTURE_PINCH_END);
407 
408 	return (struct libinput_event_gesture *) event;
409 }
410 
411 LIBINPUT_EXPORT struct libinput_event_tablet_tool *
libinput_event_get_tablet_tool_event(struct libinput_event * event)412 libinput_event_get_tablet_tool_event(struct libinput_event *event)
413 {
414 	require_event_type(libinput_event_get_context(event),
415 			   event->type,
416 			   NULL,
417 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
418 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY,
419 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
420 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
421 
422 	return (struct libinput_event_tablet_tool *) event;
423 }
424 
425 LIBINPUT_EXPORT struct libinput_event_tablet_pad *
libinput_event_get_tablet_pad_event(struct libinput_event * event)426 libinput_event_get_tablet_pad_event(struct libinput_event *event)
427 {
428 	require_event_type(libinput_event_get_context(event),
429 			   event->type,
430 			   NULL,
431 			   LIBINPUT_EVENT_TABLET_PAD_RING,
432 			   LIBINPUT_EVENT_TABLET_PAD_STRIP,
433 			   LIBINPUT_EVENT_TABLET_PAD_BUTTON,
434 			   LIBINPUT_EVENT_TABLET_PAD_KEY);
435 
436 	return (struct libinput_event_tablet_pad *) event;
437 }
438 
439 LIBINPUT_EXPORT struct libinput_event_device_notify *
libinput_event_get_device_notify_event(struct libinput_event * event)440 libinput_event_get_device_notify_event(struct libinput_event *event)
441 {
442 	require_event_type(libinput_event_get_context(event),
443 			   event->type,
444 			   NULL,
445 			   LIBINPUT_EVENT_DEVICE_ADDED,
446 			   LIBINPUT_EVENT_DEVICE_REMOVED);
447 
448 	return (struct libinput_event_device_notify *) event;
449 }
450 
451 LIBINPUT_EXPORT struct libinput_event_switch *
libinput_event_get_switch_event(struct libinput_event * event)452 libinput_event_get_switch_event(struct libinput_event *event)
453 {
454 	require_event_type(libinput_event_get_context(event),
455 			   event->type,
456 			   NULL,
457 			   LIBINPUT_EVENT_SWITCH_TOGGLE);
458 
459 	return (struct libinput_event_switch *) event;
460 }
461 
462 LIBINPUT_EXPORT uint32_t
libinput_event_keyboard_get_time(struct libinput_event_keyboard * event)463 libinput_event_keyboard_get_time(struct libinput_event_keyboard *event)
464 {
465 	require_event_type(libinput_event_get_context(&event->base),
466 			   event->base.type,
467 			   0,
468 			   LIBINPUT_EVENT_KEYBOARD_KEY);
469 
470 	return us2ms(event->time);
471 }
472 
473 LIBINPUT_EXPORT uint64_t
libinput_event_keyboard_get_time_usec(struct libinput_event_keyboard * event)474 libinput_event_keyboard_get_time_usec(struct libinput_event_keyboard *event)
475 {
476 	require_event_type(libinput_event_get_context(&event->base),
477 			   event->base.type,
478 			   0,
479 			   LIBINPUT_EVENT_KEYBOARD_KEY);
480 
481 	return event->time;
482 }
483 
484 LIBINPUT_EXPORT uint32_t
libinput_event_keyboard_get_key(struct libinput_event_keyboard * event)485 libinput_event_keyboard_get_key(struct libinput_event_keyboard *event)
486 {
487 	require_event_type(libinput_event_get_context(&event->base),
488 			   event->base.type,
489 			   0,
490 			   LIBINPUT_EVENT_KEYBOARD_KEY);
491 
492 	return event->key;
493 }
494 
495 LIBINPUT_EXPORT enum libinput_key_state
libinput_event_keyboard_get_key_state(struct libinput_event_keyboard * event)496 libinput_event_keyboard_get_key_state(struct libinput_event_keyboard *event)
497 {
498 	require_event_type(libinput_event_get_context(&event->base),
499 			   event->base.type,
500 			   0,
501 			   LIBINPUT_EVENT_KEYBOARD_KEY);
502 
503 	return event->state;
504 }
505 
506 LIBINPUT_EXPORT uint32_t
libinput_event_keyboard_get_seat_key_count(struct libinput_event_keyboard * event)507 libinput_event_keyboard_get_seat_key_count(
508 	struct libinput_event_keyboard *event)
509 {
510 	require_event_type(libinput_event_get_context(&event->base),
511 			   event->base.type,
512 			   0,
513 			   LIBINPUT_EVENT_KEYBOARD_KEY);
514 
515 	return event->seat_key_count;
516 }
517 
518 LIBINPUT_EXPORT uint32_t
libinput_event_pointer_get_time(struct libinput_event_pointer * event)519 libinput_event_pointer_get_time(struct libinput_event_pointer *event)
520 {
521 	require_event_type(libinput_event_get_context(&event->base),
522 			   event->base.type,
523 			   0,
524 			   LIBINPUT_EVENT_POINTER_MOTION,
525 			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
526 			   LIBINPUT_EVENT_POINTER_BUTTON,
527 			   LIBINPUT_EVENT_POINTER_AXIS);
528 
529 	return us2ms(event->time);
530 }
531 
532 LIBINPUT_EXPORT uint64_t
libinput_event_pointer_get_time_usec(struct libinput_event_pointer * event)533 libinput_event_pointer_get_time_usec(struct libinput_event_pointer *event)
534 {
535 	require_event_type(libinput_event_get_context(&event->base),
536 			   event->base.type,
537 			   0,
538 			   LIBINPUT_EVENT_POINTER_MOTION,
539 			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
540 			   LIBINPUT_EVENT_POINTER_BUTTON,
541 			   LIBINPUT_EVENT_POINTER_AXIS);
542 
543 	return event->time;
544 }
545 
546 LIBINPUT_EXPORT double
libinput_event_pointer_get_dx(struct libinput_event_pointer * event)547 libinput_event_pointer_get_dx(struct libinput_event_pointer *event)
548 {
549 	require_event_type(libinput_event_get_context(&event->base),
550 			   event->base.type,
551 			   0,
552 			   LIBINPUT_EVENT_POINTER_MOTION);
553 
554 	return event->delta.x;
555 }
556 
557 LIBINPUT_EXPORT double
libinput_event_pointer_get_dy(struct libinput_event_pointer * event)558 libinput_event_pointer_get_dy(struct libinput_event_pointer *event)
559 {
560 	require_event_type(libinput_event_get_context(&event->base),
561 			   event->base.type,
562 			   0,
563 			   LIBINPUT_EVENT_POINTER_MOTION);
564 
565 	return event->delta.y;
566 }
567 
568 LIBINPUT_EXPORT double
libinput_event_pointer_get_dx_unaccelerated(struct libinput_event_pointer * event)569 libinput_event_pointer_get_dx_unaccelerated(
570 	struct libinput_event_pointer *event)
571 {
572 	require_event_type(libinput_event_get_context(&event->base),
573 			   event->base.type,
574 			   0,
575 			   LIBINPUT_EVENT_POINTER_MOTION);
576 
577 	return event->delta_raw.x;
578 }
579 
580 LIBINPUT_EXPORT double
libinput_event_pointer_get_dy_unaccelerated(struct libinput_event_pointer * event)581 libinput_event_pointer_get_dy_unaccelerated(
582 	struct libinput_event_pointer *event)
583 {
584 	require_event_type(libinput_event_get_context(&event->base),
585 			   event->base.type,
586 			   0,
587 			   LIBINPUT_EVENT_POINTER_MOTION);
588 
589 	return event->delta_raw.y;
590 }
591 
592 LIBINPUT_EXPORT double
libinput_event_pointer_get_absolute_x(struct libinput_event_pointer * event)593 libinput_event_pointer_get_absolute_x(struct libinput_event_pointer *event)
594 {
595 	struct evdev_device *device = evdev_device(event->base.device);
596 
597 	require_event_type(libinput_event_get_context(&event->base),
598 			   event->base.type,
599 			   0,
600 			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE);
601 
602 	return evdev_convert_to_mm(device->abs.absinfo_x, event->absolute.x);
603 }
604 
605 LIBINPUT_EXPORT double
libinput_event_pointer_get_absolute_y(struct libinput_event_pointer * event)606 libinput_event_pointer_get_absolute_y(struct libinput_event_pointer *event)
607 {
608 	struct evdev_device *device = evdev_device(event->base.device);
609 
610 	require_event_type(libinput_event_get_context(&event->base),
611 			   event->base.type,
612 			   0,
613 			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE);
614 
615 	return evdev_convert_to_mm(device->abs.absinfo_y, event->absolute.y);
616 }
617 
618 LIBINPUT_EXPORT double
libinput_event_pointer_get_absolute_x_transformed(struct libinput_event_pointer * event,uint32_t width)619 libinput_event_pointer_get_absolute_x_transformed(
620 	struct libinput_event_pointer *event,
621 	uint32_t width)
622 {
623 	struct evdev_device *device = evdev_device(event->base.device);
624 
625 	require_event_type(libinput_event_get_context(&event->base),
626 			   event->base.type,
627 			   0,
628 			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE);
629 
630 	return evdev_device_transform_x(device, event->absolute.x, width);
631 }
632 
633 LIBINPUT_EXPORT double
libinput_event_pointer_get_absolute_y_transformed(struct libinput_event_pointer * event,uint32_t height)634 libinput_event_pointer_get_absolute_y_transformed(
635 	struct libinput_event_pointer *event,
636 	uint32_t height)
637 {
638 	struct evdev_device *device = evdev_device(event->base.device);
639 
640 	require_event_type(libinput_event_get_context(&event->base),
641 			   event->base.type,
642 			   0,
643 			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE);
644 
645 	return evdev_device_transform_y(device, event->absolute.y, height);
646 }
647 
648 LIBINPUT_EXPORT uint32_t
libinput_event_pointer_get_button(struct libinput_event_pointer * event)649 libinput_event_pointer_get_button(struct libinput_event_pointer *event)
650 {
651 	require_event_type(libinput_event_get_context(&event->base),
652 			   event->base.type,
653 			   0,
654 			   LIBINPUT_EVENT_POINTER_BUTTON);
655 
656 	return event->button;
657 }
658 
659 LIBINPUT_EXPORT enum libinput_button_state
libinput_event_pointer_get_button_state(struct libinput_event_pointer * event)660 libinput_event_pointer_get_button_state(struct libinput_event_pointer *event)
661 {
662 	require_event_type(libinput_event_get_context(&event->base),
663 			   event->base.type,
664 			   0,
665 			   LIBINPUT_EVENT_POINTER_BUTTON);
666 
667 	return event->state;
668 }
669 
670 LIBINPUT_EXPORT uint32_t
libinput_event_pointer_get_seat_button_count(struct libinput_event_pointer * event)671 libinput_event_pointer_get_seat_button_count(
672 	struct libinput_event_pointer *event)
673 {
674 	require_event_type(libinput_event_get_context(&event->base),
675 			   event->base.type,
676 			   0,
677 			   LIBINPUT_EVENT_POINTER_BUTTON);
678 
679 	return event->seat_button_count;
680 }
681 
682 LIBINPUT_EXPORT int
libinput_event_pointer_has_axis(struct libinput_event_pointer * event,enum libinput_pointer_axis axis)683 libinput_event_pointer_has_axis(struct libinput_event_pointer *event,
684 				enum libinput_pointer_axis axis)
685 {
686 	require_event_type(libinput_event_get_context(&event->base),
687 			   event->base.type,
688 			   0,
689 			   LIBINPUT_EVENT_POINTER_AXIS);
690 
691 	switch (axis) {
692 	case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL:
693 	case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL:
694 		return !!(event->axes & bit(axis));
695 	}
696 
697 	return 0;
698 }
699 
700 LIBINPUT_EXPORT double
libinput_event_pointer_get_axis_value(struct libinput_event_pointer * event,enum libinput_pointer_axis axis)701 libinput_event_pointer_get_axis_value(struct libinput_event_pointer *event,
702 				      enum libinput_pointer_axis axis)
703 {
704 	struct libinput *libinput = event->base.device->seat->libinput;
705 	double value = 0;
706 
707 	require_event_type(libinput_event_get_context(&event->base),
708 			   event->base.type,
709 			   0.0,
710 			   LIBINPUT_EVENT_POINTER_AXIS);
711 
712 	if (!libinput_event_pointer_has_axis(event, axis)) {
713 		log_bug_client(libinput, "value requested for unset axis\n");
714 	} else {
715 		switch (axis) {
716 		case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL:
717 			value = event->delta.x;
718 			break;
719 		case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL:
720 			value = event->delta.y;
721 			break;
722 		}
723 	}
724 
725 	return value;
726 }
727 
728 LIBINPUT_EXPORT double
libinput_event_pointer_get_axis_value_discrete(struct libinput_event_pointer * event,enum libinput_pointer_axis axis)729 libinput_event_pointer_get_axis_value_discrete(struct libinput_event_pointer *event,
730 					       enum libinput_pointer_axis axis)
731 {
732 	struct libinput *libinput = event->base.device->seat->libinput;
733 	double value = 0;
734 
735 	require_event_type(libinput_event_get_context(&event->base),
736 			   event->base.type,
737 			   0.0,
738 			   LIBINPUT_EVENT_POINTER_AXIS);
739 
740 	if (!libinput_event_pointer_has_axis(event, axis)) {
741 		log_bug_client(libinput, "value requested for unset axis\n");
742 	} else {
743 		switch (axis) {
744 		case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL:
745 			value = event->discrete.x;
746 			break;
747 		case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL:
748 			value = event->discrete.y;
749 			break;
750 		}
751 	}
752 	return value;
753 }
754 
755 LIBINPUT_EXPORT enum libinput_pointer_axis_source
libinput_event_pointer_get_axis_source(struct libinput_event_pointer * event)756 libinput_event_pointer_get_axis_source(struct libinput_event_pointer *event)
757 {
758 	require_event_type(libinput_event_get_context(&event->base),
759 			   event->base.type,
760 			   0,
761 			   LIBINPUT_EVENT_POINTER_AXIS);
762 
763 	return event->source;
764 }
765 
766 LIBINPUT_EXPORT uint32_t
libinput_event_touch_get_time(struct libinput_event_touch * event)767 libinput_event_touch_get_time(struct libinput_event_touch *event)
768 {
769 	require_event_type(libinput_event_get_context(&event->base),
770 			   event->base.type,
771 			   0,
772 			   LIBINPUT_EVENT_TOUCH_DOWN,
773 			   LIBINPUT_EVENT_TOUCH_UP,
774 			   LIBINPUT_EVENT_TOUCH_MOTION,
775 			   LIBINPUT_EVENT_TOUCH_CANCEL,
776 			   LIBINPUT_EVENT_TOUCH_FRAME);
777 
778 	return us2ms(event->time);
779 }
780 
781 LIBINPUT_EXPORT uint64_t
libinput_event_touch_get_time_usec(struct libinput_event_touch * event)782 libinput_event_touch_get_time_usec(struct libinput_event_touch *event)
783 {
784 	require_event_type(libinput_event_get_context(&event->base),
785 			   event->base.type,
786 			   0,
787 			   LIBINPUT_EVENT_TOUCH_DOWN,
788 			   LIBINPUT_EVENT_TOUCH_UP,
789 			   LIBINPUT_EVENT_TOUCH_MOTION,
790 			   LIBINPUT_EVENT_TOUCH_CANCEL,
791 			   LIBINPUT_EVENT_TOUCH_FRAME);
792 
793 	return event->time;
794 }
795 
796 LIBINPUT_EXPORT int32_t
libinput_event_touch_get_slot(struct libinput_event_touch * event)797 libinput_event_touch_get_slot(struct libinput_event_touch *event)
798 {
799 	require_event_type(libinput_event_get_context(&event->base),
800 			   event->base.type,
801 			   0,
802 			   LIBINPUT_EVENT_TOUCH_DOWN,
803 			   LIBINPUT_EVENT_TOUCH_UP,
804 			   LIBINPUT_EVENT_TOUCH_MOTION,
805 			   LIBINPUT_EVENT_TOUCH_CANCEL);
806 
807 	return event->slot;
808 }
809 
810 LIBINPUT_EXPORT int32_t
libinput_event_touch_get_seat_slot(struct libinput_event_touch * event)811 libinput_event_touch_get_seat_slot(struct libinput_event_touch *event)
812 {
813 	require_event_type(libinput_event_get_context(&event->base),
814 			   event->base.type,
815 			   0,
816 			   LIBINPUT_EVENT_TOUCH_DOWN,
817 			   LIBINPUT_EVENT_TOUCH_UP,
818 			   LIBINPUT_EVENT_TOUCH_MOTION,
819 			   LIBINPUT_EVENT_TOUCH_CANCEL);
820 
821 	return event->seat_slot;
822 }
823 
824 LIBINPUT_EXPORT double
libinput_event_touch_get_x(struct libinput_event_touch * event)825 libinput_event_touch_get_x(struct libinput_event_touch *event)
826 {
827 	struct evdev_device *device = evdev_device(event->base.device);
828 
829 	require_event_type(libinput_event_get_context(&event->base),
830 			   event->base.type,
831 			   0,
832 			   LIBINPUT_EVENT_TOUCH_DOWN,
833 			   LIBINPUT_EVENT_TOUCH_MOTION);
834 
835 	return evdev_convert_to_mm(device->abs.absinfo_x, event->point.x);
836 }
837 
838 LIBINPUT_EXPORT double
libinput_event_touch_get_x_transformed(struct libinput_event_touch * event,uint32_t width)839 libinput_event_touch_get_x_transformed(struct libinput_event_touch *event,
840 				       uint32_t width)
841 {
842 	struct evdev_device *device = evdev_device(event->base.device);
843 
844 	require_event_type(libinput_event_get_context(&event->base),
845 			   event->base.type,
846 			   0,
847 			   LIBINPUT_EVENT_TOUCH_DOWN,
848 			   LIBINPUT_EVENT_TOUCH_MOTION);
849 
850 	return evdev_device_transform_x(device, event->point.x, width);
851 }
852 
853 LIBINPUT_EXPORT double
libinput_event_touch_get_y_transformed(struct libinput_event_touch * event,uint32_t height)854 libinput_event_touch_get_y_transformed(struct libinput_event_touch *event,
855 				       uint32_t height)
856 {
857 	struct evdev_device *device = evdev_device(event->base.device);
858 
859 	require_event_type(libinput_event_get_context(&event->base),
860 			   event->base.type,
861 			   0,
862 			   LIBINPUT_EVENT_TOUCH_DOWN,
863 			   LIBINPUT_EVENT_TOUCH_MOTION);
864 
865 	return evdev_device_transform_y(device, event->point.y, height);
866 }
867 
868 LIBINPUT_EXPORT double
libinput_event_touch_get_y(struct libinput_event_touch * event)869 libinput_event_touch_get_y(struct libinput_event_touch *event)
870 {
871 	struct evdev_device *device = evdev_device(event->base.device);
872 
873 	require_event_type(libinput_event_get_context(&event->base),
874 			   event->base.type,
875 			   0,
876 			   LIBINPUT_EVENT_TOUCH_DOWN,
877 			   LIBINPUT_EVENT_TOUCH_MOTION);
878 
879 	return evdev_convert_to_mm(device->abs.absinfo_y, event->point.y);
880 }
881 
882 LIBINPUT_EXPORT uint32_t
libinput_event_gesture_get_time(struct libinput_event_gesture * event)883 libinput_event_gesture_get_time(struct libinput_event_gesture *event)
884 {
885 	require_event_type(libinput_event_get_context(&event->base),
886 			   event->base.type,
887 			   0,
888 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
889 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
890 			   LIBINPUT_EVENT_GESTURE_PINCH_END,
891 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
892 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
893 			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
894 
895 	return us2ms(event->time);
896 }
897 
898 LIBINPUT_EXPORT uint64_t
libinput_event_gesture_get_time_usec(struct libinput_event_gesture * event)899 libinput_event_gesture_get_time_usec(struct libinput_event_gesture *event)
900 {
901 	require_event_type(libinput_event_get_context(&event->base),
902 			   event->base.type,
903 			   0,
904 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
905 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
906 			   LIBINPUT_EVENT_GESTURE_PINCH_END,
907 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
908 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
909 			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
910 
911 	return event->time;
912 }
913 
914 LIBINPUT_EXPORT int
libinput_event_gesture_get_finger_count(struct libinput_event_gesture * event)915 libinput_event_gesture_get_finger_count(struct libinput_event_gesture *event)
916 {
917 	require_event_type(libinput_event_get_context(&event->base),
918 			   event->base.type,
919 			   0,
920 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
921 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
922 			   LIBINPUT_EVENT_GESTURE_PINCH_END,
923 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
924 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
925 			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
926 
927 	return event->finger_count;
928 }
929 
930 LIBINPUT_EXPORT int
libinput_event_gesture_get_cancelled(struct libinput_event_gesture * event)931 libinput_event_gesture_get_cancelled(struct libinput_event_gesture *event)
932 {
933 	require_event_type(libinput_event_get_context(&event->base),
934 			   event->base.type,
935 			   0,
936 			   LIBINPUT_EVENT_GESTURE_PINCH_END,
937 			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
938 
939 	return event->cancelled;
940 }
941 
942 LIBINPUT_EXPORT double
libinput_event_gesture_get_dx(struct libinput_event_gesture * event)943 libinput_event_gesture_get_dx(struct libinput_event_gesture *event)
944 {
945 	require_event_type(libinput_event_get_context(&event->base),
946 			   event->base.type,
947 			   0.0,
948 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
949 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
950 			   LIBINPUT_EVENT_GESTURE_PINCH_END,
951 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
952 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
953 			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
954 
955 	return event->delta.x;
956 }
957 
958 LIBINPUT_EXPORT double
libinput_event_gesture_get_dy(struct libinput_event_gesture * event)959 libinput_event_gesture_get_dy(struct libinput_event_gesture *event)
960 {
961 	require_event_type(libinput_event_get_context(&event->base),
962 			   event->base.type,
963 			   0.0,
964 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
965 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
966 			   LIBINPUT_EVENT_GESTURE_PINCH_END,
967 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
968 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
969 			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
970 
971 	return event->delta.y;
972 }
973 
974 LIBINPUT_EXPORT double
libinput_event_gesture_get_dx_unaccelerated(struct libinput_event_gesture * event)975 libinput_event_gesture_get_dx_unaccelerated(
976 	struct libinput_event_gesture *event)
977 {
978 	require_event_type(libinput_event_get_context(&event->base),
979 			   event->base.type,
980 			   0.0,
981 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
982 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
983 			   LIBINPUT_EVENT_GESTURE_PINCH_END,
984 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
985 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
986 			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
987 
988 	return event->delta_unaccel.x;
989 }
990 
991 LIBINPUT_EXPORT double
libinput_event_gesture_get_dy_unaccelerated(struct libinput_event_gesture * event)992 libinput_event_gesture_get_dy_unaccelerated(
993 	struct libinput_event_gesture *event)
994 {
995 	require_event_type(libinput_event_get_context(&event->base),
996 			   event->base.type,
997 			   0.0,
998 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
999 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
1000 			   LIBINPUT_EVENT_GESTURE_PINCH_END,
1001 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
1002 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
1003 			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
1004 
1005 	return event->delta_unaccel.y;
1006 }
1007 
1008 LIBINPUT_EXPORT double
libinput_event_gesture_get_scale(struct libinput_event_gesture * event)1009 libinput_event_gesture_get_scale(struct libinput_event_gesture *event)
1010 {
1011 	require_event_type(libinput_event_get_context(&event->base),
1012 			   event->base.type,
1013 			   0.0,
1014 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
1015 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
1016 			   LIBINPUT_EVENT_GESTURE_PINCH_END);
1017 
1018 	return event->scale;
1019 }
1020 
1021 LIBINPUT_EXPORT double
libinput_event_gesture_get_angle_delta(struct libinput_event_gesture * event)1022 libinput_event_gesture_get_angle_delta(struct libinput_event_gesture *event)
1023 {
1024 	require_event_type(libinput_event_get_context(&event->base),
1025 			   event->base.type,
1026 			   0.0,
1027 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
1028 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
1029 			   LIBINPUT_EVENT_GESTURE_PINCH_END);
1030 
1031 	return event->angle;
1032 }
1033 
1034 LIBINPUT_EXPORT int
libinput_event_tablet_tool_x_has_changed(struct libinput_event_tablet_tool * event)1035 libinput_event_tablet_tool_x_has_changed(
1036 				struct libinput_event_tablet_tool *event)
1037 {
1038 	require_event_type(libinput_event_get_context(&event->base),
1039 			   event->base.type,
1040 			   0,
1041 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1042 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1043 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1044 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1045 
1046 	return bit_is_set(event->changed_axes,
1047 			  LIBINPUT_TABLET_TOOL_AXIS_X);
1048 }
1049 
1050 LIBINPUT_EXPORT int
libinput_event_tablet_tool_y_has_changed(struct libinput_event_tablet_tool * event)1051 libinput_event_tablet_tool_y_has_changed(
1052 				struct libinput_event_tablet_tool *event)
1053 {
1054 	require_event_type(libinput_event_get_context(&event->base),
1055 			   event->base.type,
1056 			   0,
1057 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1058 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1059 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1060 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1061 
1062 	return bit_is_set(event->changed_axes,
1063 			  LIBINPUT_TABLET_TOOL_AXIS_Y);
1064 }
1065 
1066 LIBINPUT_EXPORT int
libinput_event_tablet_tool_pressure_has_changed(struct libinput_event_tablet_tool * event)1067 libinput_event_tablet_tool_pressure_has_changed(
1068 				struct libinput_event_tablet_tool *event)
1069 {
1070 	require_event_type(libinput_event_get_context(&event->base),
1071 			   event->base.type,
1072 			   0,
1073 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1074 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1075 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1076 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1077 
1078 	return bit_is_set(event->changed_axes,
1079 			  LIBINPUT_TABLET_TOOL_AXIS_PRESSURE);
1080 }
1081 
1082 LIBINPUT_EXPORT int
libinput_event_tablet_tool_distance_has_changed(struct libinput_event_tablet_tool * event)1083 libinput_event_tablet_tool_distance_has_changed(
1084 				struct libinput_event_tablet_tool *event)
1085 {
1086 	require_event_type(libinput_event_get_context(&event->base),
1087 			   event->base.type,
1088 			   0,
1089 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1090 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1091 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1092 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1093 
1094 	return bit_is_set(event->changed_axes,
1095 			  LIBINPUT_TABLET_TOOL_AXIS_DISTANCE);
1096 }
1097 
1098 LIBINPUT_EXPORT int
libinput_event_tablet_tool_tilt_x_has_changed(struct libinput_event_tablet_tool * event)1099 libinput_event_tablet_tool_tilt_x_has_changed(
1100 				struct libinput_event_tablet_tool *event)
1101 {
1102 	require_event_type(libinput_event_get_context(&event->base),
1103 			   event->base.type,
1104 			   0,
1105 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1106 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1107 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1108 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1109 
1110 	return bit_is_set(event->changed_axes,
1111 			  LIBINPUT_TABLET_TOOL_AXIS_TILT_X);
1112 }
1113 
1114 LIBINPUT_EXPORT int
libinput_event_tablet_tool_tilt_y_has_changed(struct libinput_event_tablet_tool * event)1115 libinput_event_tablet_tool_tilt_y_has_changed(
1116 				struct libinput_event_tablet_tool *event)
1117 {
1118 	require_event_type(libinput_event_get_context(&event->base),
1119 			   event->base.type,
1120 			   0,
1121 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1122 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1123 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1124 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1125 
1126 	return bit_is_set(event->changed_axes,
1127 			  LIBINPUT_TABLET_TOOL_AXIS_TILT_Y);
1128 }
1129 
1130 LIBINPUT_EXPORT int
libinput_event_tablet_tool_rotation_has_changed(struct libinput_event_tablet_tool * event)1131 libinput_event_tablet_tool_rotation_has_changed(
1132 				struct libinput_event_tablet_tool *event)
1133 {
1134 	require_event_type(libinput_event_get_context(&event->base),
1135 			   event->base.type,
1136 			   0,
1137 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1138 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1139 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1140 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1141 
1142 	return bit_is_set(event->changed_axes,
1143 			  LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z);
1144 }
1145 
1146 LIBINPUT_EXPORT int
libinput_event_tablet_tool_slider_has_changed(struct libinput_event_tablet_tool * event)1147 libinput_event_tablet_tool_slider_has_changed(
1148 				struct libinput_event_tablet_tool *event)
1149 {
1150 	require_event_type(libinput_event_get_context(&event->base),
1151 			   event->base.type,
1152 			   0,
1153 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1154 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1155 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1156 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1157 
1158 	return bit_is_set(event->changed_axes,
1159 			  LIBINPUT_TABLET_TOOL_AXIS_SLIDER);
1160 }
1161 
1162 LIBINPUT_EXPORT int
libinput_event_tablet_tool_size_major_has_changed(struct libinput_event_tablet_tool * event)1163 libinput_event_tablet_tool_size_major_has_changed(
1164 				struct libinput_event_tablet_tool *event)
1165 {
1166 	require_event_type(libinput_event_get_context(&event->base),
1167 			   event->base.type,
1168 			   0,
1169 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1170 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1171 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1172 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1173 
1174 	return bit_is_set(event->changed_axes,
1175 			  LIBINPUT_TABLET_TOOL_AXIS_SIZE_MAJOR);
1176 }
1177 
1178 LIBINPUT_EXPORT int
libinput_event_tablet_tool_size_minor_has_changed(struct libinput_event_tablet_tool * event)1179 libinput_event_tablet_tool_size_minor_has_changed(
1180 				struct libinput_event_tablet_tool *event)
1181 {
1182 	require_event_type(libinput_event_get_context(&event->base),
1183 			   event->base.type,
1184 			   0,
1185 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1186 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1187 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1188 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1189 
1190 	return bit_is_set(event->changed_axes,
1191 			  LIBINPUT_TABLET_TOOL_AXIS_SIZE_MINOR);
1192 }
1193 
1194 LIBINPUT_EXPORT int
libinput_event_tablet_tool_wheel_has_changed(struct libinput_event_tablet_tool * event)1195 libinput_event_tablet_tool_wheel_has_changed(
1196 				struct libinput_event_tablet_tool *event)
1197 {
1198 	require_event_type(libinput_event_get_context(&event->base),
1199 			   event->base.type,
1200 			   0,
1201 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1202 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1203 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1204 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1205 
1206 	return bit_is_set(event->changed_axes,
1207 			  LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL);
1208 }
1209 
1210 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_x(struct libinput_event_tablet_tool * event)1211 libinput_event_tablet_tool_get_x(struct libinput_event_tablet_tool *event)
1212 {
1213 	struct evdev_device *device = evdev_device(event->base.device);
1214 
1215 	require_event_type(libinput_event_get_context(&event->base),
1216 			   event->base.type,
1217 			   0,
1218 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1219 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1220 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1221 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1222 
1223 	return evdev_convert_to_mm(device->abs.absinfo_x,
1224 				   event->axes.point.x);
1225 }
1226 
1227 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_y(struct libinput_event_tablet_tool * event)1228 libinput_event_tablet_tool_get_y(struct libinput_event_tablet_tool *event)
1229 {
1230 	struct evdev_device *device = evdev_device(event->base.device);
1231 
1232 	require_event_type(libinput_event_get_context(&event->base),
1233 			   event->base.type,
1234 			   0,
1235 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1236 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1237 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1238 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1239 
1240 	return evdev_convert_to_mm(device->abs.absinfo_y,
1241 				   event->axes.point.y);
1242 }
1243 
1244 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_dx(struct libinput_event_tablet_tool * event)1245 libinput_event_tablet_tool_get_dx(struct libinput_event_tablet_tool *event)
1246 {
1247 	require_event_type(libinput_event_get_context(&event->base),
1248 			   event->base.type,
1249 			   0,
1250 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1251 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1252 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1253 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1254 
1255 	return event->axes.delta.x;
1256 }
1257 
1258 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_dy(struct libinput_event_tablet_tool * event)1259 libinput_event_tablet_tool_get_dy(struct libinput_event_tablet_tool *event)
1260 {
1261 	require_event_type(libinput_event_get_context(&event->base),
1262 			   event->base.type,
1263 			   0,
1264 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1265 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1266 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1267 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1268 
1269 	return event->axes.delta.y;
1270 }
1271 
1272 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_pressure(struct libinput_event_tablet_tool * event)1273 libinput_event_tablet_tool_get_pressure(struct libinput_event_tablet_tool *event)
1274 {
1275 	require_event_type(libinput_event_get_context(&event->base),
1276 			   event->base.type,
1277 			   0,
1278 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1279 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1280 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1281 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1282 
1283 	return event->axes.pressure;
1284 }
1285 
1286 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_distance(struct libinput_event_tablet_tool * event)1287 libinput_event_tablet_tool_get_distance(struct libinput_event_tablet_tool *event)
1288 {
1289 	require_event_type(libinput_event_get_context(&event->base),
1290 			   event->base.type,
1291 			   0,
1292 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1293 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1294 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1295 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1296 
1297 	return event->axes.distance;
1298 }
1299 
1300 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_tilt_x(struct libinput_event_tablet_tool * event)1301 libinput_event_tablet_tool_get_tilt_x(struct libinput_event_tablet_tool *event)
1302 {
1303 	require_event_type(libinput_event_get_context(&event->base),
1304 			   event->base.type,
1305 			   0,
1306 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1307 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1308 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1309 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1310 
1311 	return event->axes.tilt.x;
1312 }
1313 
1314 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_tilt_y(struct libinput_event_tablet_tool * event)1315 libinput_event_tablet_tool_get_tilt_y(struct libinput_event_tablet_tool *event)
1316 {
1317 	require_event_type(libinput_event_get_context(&event->base),
1318 			   event->base.type,
1319 			   0,
1320 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1321 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1322 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1323 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1324 
1325 	return event->axes.tilt.y;
1326 }
1327 
1328 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_rotation(struct libinput_event_tablet_tool * event)1329 libinput_event_tablet_tool_get_rotation(struct libinput_event_tablet_tool *event)
1330 {
1331 	require_event_type(libinput_event_get_context(&event->base),
1332 			   event->base.type,
1333 			   0,
1334 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1335 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1336 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1337 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1338 
1339 	return event->axes.rotation;
1340 }
1341 
1342 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_slider_position(struct libinput_event_tablet_tool * event)1343 libinput_event_tablet_tool_get_slider_position(struct libinput_event_tablet_tool *event)
1344 {
1345 	require_event_type(libinput_event_get_context(&event->base),
1346 			   event->base.type,
1347 			   0,
1348 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1349 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1350 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1351 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1352 
1353 	return event->axes.slider;
1354 }
1355 
1356 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_size_major(struct libinput_event_tablet_tool * event)1357 libinput_event_tablet_tool_get_size_major(struct libinput_event_tablet_tool *event)
1358 {
1359 	require_event_type(libinput_event_get_context(&event->base),
1360 			   event->base.type,
1361 			   0,
1362 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1363 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1364 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1365 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1366 
1367 	return event->axes.size.major;
1368 }
1369 
1370 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_size_minor(struct libinput_event_tablet_tool * event)1371 libinput_event_tablet_tool_get_size_minor(struct libinput_event_tablet_tool *event)
1372 {
1373 	require_event_type(libinput_event_get_context(&event->base),
1374 			   event->base.type,
1375 			   0,
1376 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1377 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1378 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1379 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1380 
1381 	return event->axes.size.minor;
1382 }
1383 
1384 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_wheel_delta(struct libinput_event_tablet_tool * event)1385 libinput_event_tablet_tool_get_wheel_delta(struct libinput_event_tablet_tool *event)
1386 {
1387 	require_event_type(libinput_event_get_context(&event->base),
1388 			   event->base.type,
1389 			   0,
1390 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1391 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1392 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1393 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1394 
1395 	return event->axes.wheel;
1396 }
1397 
1398 LIBINPUT_EXPORT int
libinput_event_tablet_tool_get_wheel_delta_discrete(struct libinput_event_tablet_tool * event)1399 libinput_event_tablet_tool_get_wheel_delta_discrete(
1400 				      struct libinput_event_tablet_tool *event)
1401 {
1402 	require_event_type(libinput_event_get_context(&event->base),
1403 			   event->base.type,
1404 			   0,
1405 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1406 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1407 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1408 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1409 
1410 	return event->axes.wheel_discrete;
1411 }
1412 
1413 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_x_transformed(struct libinput_event_tablet_tool * event,uint32_t width)1414 libinput_event_tablet_tool_get_x_transformed(struct libinput_event_tablet_tool *event,
1415 					uint32_t width)
1416 {
1417 	struct evdev_device *device = evdev_device(event->base.device);
1418 
1419 	require_event_type(libinput_event_get_context(&event->base),
1420 			   event->base.type,
1421 			   0,
1422 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1423 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1424 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1425 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1426 
1427 	return evdev_device_transform_x(device,
1428 					event->axes.point.x,
1429 					width);
1430 }
1431 
1432 LIBINPUT_EXPORT double
libinput_event_tablet_tool_get_y_transformed(struct libinput_event_tablet_tool * event,uint32_t height)1433 libinput_event_tablet_tool_get_y_transformed(struct libinput_event_tablet_tool *event,
1434 					uint32_t height)
1435 {
1436 	struct evdev_device *device = evdev_device(event->base.device);
1437 
1438 	require_event_type(libinput_event_get_context(&event->base),
1439 			   event->base.type,
1440 			   0,
1441 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1442 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1443 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1444 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1445 
1446 	return evdev_device_transform_y(device,
1447 					event->axes.point.y,
1448 					height);
1449 }
1450 
1451 LIBINPUT_EXPORT struct libinput_tablet_tool *
libinput_event_tablet_tool_get_tool(struct libinput_event_tablet_tool * event)1452 libinput_event_tablet_tool_get_tool(struct libinput_event_tablet_tool *event)
1453 {
1454 	require_event_type(libinput_event_get_context(&event->base),
1455 			   event->base.type,
1456 			   0,
1457 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1458 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1459 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1460 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1461 
1462 	return event->tool;
1463 }
1464 
1465 LIBINPUT_EXPORT enum libinput_tablet_tool_proximity_state
libinput_event_tablet_tool_get_proximity_state(struct libinput_event_tablet_tool * event)1466 libinput_event_tablet_tool_get_proximity_state(struct libinput_event_tablet_tool *event)
1467 {
1468 	require_event_type(libinput_event_get_context(&event->base),
1469 			   event->base.type,
1470 			   0,
1471 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1472 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1473 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1474 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1475 
1476 	return event->proximity_state;
1477 }
1478 
1479 LIBINPUT_EXPORT enum libinput_tablet_tool_tip_state
libinput_event_tablet_tool_get_tip_state(struct libinput_event_tablet_tool * event)1480 libinput_event_tablet_tool_get_tip_state(struct libinput_event_tablet_tool *event)
1481 {
1482 	require_event_type(libinput_event_get_context(&event->base),
1483 			   event->base.type,
1484 			   0,
1485 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1486 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1487 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1488 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1489 
1490 	return event->tip_state;
1491 }
1492 
1493 LIBINPUT_EXPORT uint32_t
libinput_event_tablet_tool_get_time(struct libinput_event_tablet_tool * event)1494 libinput_event_tablet_tool_get_time(struct libinput_event_tablet_tool *event)
1495 {
1496 	require_event_type(libinput_event_get_context(&event->base),
1497 			   event->base.type,
1498 			   0,
1499 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1500 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1501 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1502 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1503 
1504 	return us2ms(event->time);
1505 }
1506 
1507 LIBINPUT_EXPORT uint64_t
libinput_event_tablet_tool_get_time_usec(struct libinput_event_tablet_tool * event)1508 libinput_event_tablet_tool_get_time_usec(struct libinput_event_tablet_tool *event)
1509 {
1510 	require_event_type(libinput_event_get_context(&event->base),
1511 			   event->base.type,
1512 			   0,
1513 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
1514 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
1515 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
1516 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY);
1517 
1518 	return event->time;
1519 }
1520 
1521 LIBINPUT_EXPORT uint32_t
libinput_event_tablet_tool_get_button(struct libinput_event_tablet_tool * event)1522 libinput_event_tablet_tool_get_button(struct libinput_event_tablet_tool *event)
1523 {
1524 	require_event_type(libinput_event_get_context(&event->base),
1525 			   event->base.type,
1526 			   0,
1527 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
1528 
1529 	return event->button;
1530 }
1531 
1532 LIBINPUT_EXPORT enum libinput_button_state
libinput_event_tablet_tool_get_button_state(struct libinput_event_tablet_tool * event)1533 libinput_event_tablet_tool_get_button_state(struct libinput_event_tablet_tool *event)
1534 {
1535 	require_event_type(libinput_event_get_context(&event->base),
1536 			   event->base.type,
1537 			   0,
1538 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
1539 
1540 	return event->state;
1541 }
1542 
1543 LIBINPUT_EXPORT uint32_t
libinput_event_tablet_tool_get_seat_button_count(struct libinput_event_tablet_tool * event)1544 libinput_event_tablet_tool_get_seat_button_count(struct libinput_event_tablet_tool *event)
1545 {
1546 	require_event_type(libinput_event_get_context(&event->base),
1547 			   event->base.type,
1548 			   0,
1549 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
1550 
1551 	return event->seat_button_count;
1552 }
1553 
1554 LIBINPUT_EXPORT enum libinput_tablet_tool_type
libinput_tablet_tool_get_type(struct libinput_tablet_tool * tool)1555 libinput_tablet_tool_get_type(struct libinput_tablet_tool *tool)
1556 {
1557 	return tool->type;
1558 }
1559 
1560 LIBINPUT_EXPORT uint64_t
libinput_tablet_tool_get_tool_id(struct libinput_tablet_tool * tool)1561 libinput_tablet_tool_get_tool_id(struct libinput_tablet_tool *tool)
1562 {
1563 	return tool->tool_id;
1564 }
1565 
1566 LIBINPUT_EXPORT int
libinput_tablet_tool_is_unique(struct libinput_tablet_tool * tool)1567 libinput_tablet_tool_is_unique(struct libinput_tablet_tool *tool)
1568 {
1569 	return tool->serial != 0;
1570 }
1571 
1572 LIBINPUT_EXPORT uint64_t
libinput_tablet_tool_get_serial(struct libinput_tablet_tool * tool)1573 libinput_tablet_tool_get_serial(struct libinput_tablet_tool *tool)
1574 {
1575 	return tool->serial;
1576 }
1577 
1578 LIBINPUT_EXPORT int
libinput_tablet_tool_has_pressure(struct libinput_tablet_tool * tool)1579 libinput_tablet_tool_has_pressure(struct libinput_tablet_tool *tool)
1580 {
1581 	return bit_is_set(tool->axis_caps,
1582 			  LIBINPUT_TABLET_TOOL_AXIS_PRESSURE);
1583 }
1584 
1585 LIBINPUT_EXPORT int
libinput_tablet_tool_has_distance(struct libinput_tablet_tool * tool)1586 libinput_tablet_tool_has_distance(struct libinput_tablet_tool *tool)
1587 {
1588 	return bit_is_set(tool->axis_caps,
1589 			  LIBINPUT_TABLET_TOOL_AXIS_DISTANCE);
1590 }
1591 
1592 LIBINPUT_EXPORT int
libinput_tablet_tool_has_tilt(struct libinput_tablet_tool * tool)1593 libinput_tablet_tool_has_tilt(struct libinput_tablet_tool *tool)
1594 {
1595 	return bit_is_set(tool->axis_caps,
1596 			  LIBINPUT_TABLET_TOOL_AXIS_TILT_X);
1597 }
1598 
1599 LIBINPUT_EXPORT int
libinput_tablet_tool_has_rotation(struct libinput_tablet_tool * tool)1600 libinput_tablet_tool_has_rotation(struct libinput_tablet_tool *tool)
1601 {
1602 	return bit_is_set(tool->axis_caps,
1603 			  LIBINPUT_TABLET_TOOL_AXIS_ROTATION_Z);
1604 }
1605 
1606 LIBINPUT_EXPORT int
libinput_tablet_tool_has_slider(struct libinput_tablet_tool * tool)1607 libinput_tablet_tool_has_slider(struct libinput_tablet_tool *tool)
1608 {
1609 	return bit_is_set(tool->axis_caps,
1610 			  LIBINPUT_TABLET_TOOL_AXIS_SLIDER);
1611 }
1612 
1613 LIBINPUT_EXPORT int
libinput_tablet_tool_has_wheel(struct libinput_tablet_tool * tool)1614 libinput_tablet_tool_has_wheel(struct libinput_tablet_tool *tool)
1615 {
1616 	return bit_is_set(tool->axis_caps,
1617 			  LIBINPUT_TABLET_TOOL_AXIS_REL_WHEEL);
1618 }
1619 
1620 LIBINPUT_EXPORT int
libinput_tablet_tool_has_size(struct libinput_tablet_tool * tool)1621 libinput_tablet_tool_has_size(struct libinput_tablet_tool *tool)
1622 {
1623 	return bit_is_set(tool->axis_caps,
1624 			  LIBINPUT_TABLET_TOOL_AXIS_SIZE_MAJOR);
1625 }
1626 
1627 LIBINPUT_EXPORT int
libinput_tablet_tool_has_button(struct libinput_tablet_tool * tool,uint32_t code)1628 libinput_tablet_tool_has_button(struct libinput_tablet_tool *tool,
1629 				uint32_t code)
1630 {
1631 	if (NCHARS(code) > sizeof(tool->buttons))
1632 		return 0;
1633 
1634 	return bit_is_set(tool->buttons, code);
1635 }
1636 
1637 LIBINPUT_EXPORT void
libinput_tablet_tool_set_user_data(struct libinput_tablet_tool * tool,void * user_data)1638 libinput_tablet_tool_set_user_data(struct libinput_tablet_tool *tool,
1639 				   void *user_data)
1640 {
1641 	tool->user_data = user_data;
1642 }
1643 
1644 LIBINPUT_EXPORT void *
libinput_tablet_tool_get_user_data(struct libinput_tablet_tool * tool)1645 libinput_tablet_tool_get_user_data(struct libinput_tablet_tool *tool)
1646 {
1647 	return tool->user_data;
1648 }
1649 
1650 LIBINPUT_EXPORT struct libinput_tablet_tool *
libinput_tablet_tool_ref(struct libinput_tablet_tool * tool)1651 libinput_tablet_tool_ref(struct libinput_tablet_tool *tool)
1652 {
1653 	tool->refcount++;
1654 	return tool;
1655 }
1656 
1657 LIBINPUT_EXPORT struct libinput_tablet_tool *
libinput_tablet_tool_unref(struct libinput_tablet_tool * tool)1658 libinput_tablet_tool_unref(struct libinput_tablet_tool *tool)
1659 {
1660 	assert(tool->refcount > 0);
1661 
1662 	tool->refcount--;
1663 	if (tool->refcount > 0)
1664 		return tool;
1665 
1666 	list_remove(&tool->link);
1667 	free(tool);
1668 	return NULL;
1669 }
1670 
1671 LIBINPUT_EXPORT struct libinput_event *
libinput_event_switch_get_base_event(struct libinput_event_switch * event)1672 libinput_event_switch_get_base_event(struct libinput_event_switch *event)
1673 {
1674 	require_event_type(libinput_event_get_context(&event->base),
1675 			   event->base.type,
1676 			   NULL,
1677 			   LIBINPUT_EVENT_SWITCH_TOGGLE);
1678 
1679 	return &event->base;
1680 }
1681 
1682 LIBINPUT_EXPORT enum libinput_switch
libinput_event_switch_get_switch(struct libinput_event_switch * event)1683 libinput_event_switch_get_switch(struct libinput_event_switch *event)
1684 {
1685 	require_event_type(libinput_event_get_context(&event->base),
1686 			   event->base.type,
1687 			   0,
1688 			   LIBINPUT_EVENT_SWITCH_TOGGLE);
1689 
1690 	return event->sw;
1691 }
1692 
1693 LIBINPUT_EXPORT enum libinput_switch_state
libinput_event_switch_get_switch_state(struct libinput_event_switch * event)1694 libinput_event_switch_get_switch_state(struct libinput_event_switch *event)
1695 {
1696 	require_event_type(libinput_event_get_context(&event->base),
1697 			   event->base.type,
1698 			   0,
1699 			   LIBINPUT_EVENT_SWITCH_TOGGLE);
1700 
1701 	return event->state;
1702 }
1703 
1704 LIBINPUT_EXPORT uint32_t
libinput_event_switch_get_time(struct libinput_event_switch * event)1705 libinput_event_switch_get_time(struct libinput_event_switch *event)
1706 {
1707 	require_event_type(libinput_event_get_context(&event->base),
1708 			   event->base.type,
1709 			   0,
1710 			   LIBINPUT_EVENT_SWITCH_TOGGLE);
1711 
1712 	return us2ms(event->time);
1713 }
1714 
1715 LIBINPUT_EXPORT uint64_t
libinput_event_switch_get_time_usec(struct libinput_event_switch * event)1716 libinput_event_switch_get_time_usec(struct libinput_event_switch *event)
1717 {
1718 	require_event_type(libinput_event_get_context(&event->base),
1719 			   event->base.type,
1720 			   0,
1721 			   LIBINPUT_EVENT_SWITCH_TOGGLE);
1722 
1723 	return event->time;
1724 }
1725 
1726 struct libinput_source *
libinput_add_fd(struct libinput * libinput,int fd,libinput_source_dispatch_t dispatch,void * user_data)1727 libinput_add_fd(struct libinput *libinput,
1728 		int fd,
1729 		libinput_source_dispatch_t dispatch,
1730 		void *user_data)
1731 {
1732 	struct libinput_source *source;
1733 	struct epoll_event ep;
1734 
1735 	source = zalloc(sizeof *source);
1736 	source->dispatch = dispatch;
1737 	source->user_data = user_data;
1738 	source->fd = fd;
1739 
1740 	memset(&ep, 0, sizeof ep);
1741 	ep.events = EPOLLIN;
1742 	ep.data.ptr = source;
1743 
1744 	if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
1745 		free(source);
1746 		return NULL;
1747 	}
1748 
1749 	return source;
1750 }
1751 
1752 void
libinput_remove_source(struct libinput * libinput,struct libinput_source * source)1753 libinput_remove_source(struct libinput *libinput,
1754 		       struct libinput_source *source)
1755 {
1756 	epoll_ctl(libinput->epoll_fd, EPOLL_CTL_DEL, source->fd, NULL);
1757 	source->fd = -1;
1758 	list_insert(&libinput->source_destroy_list, &source->link);
1759 }
1760 
1761 int
libinput_init(struct libinput * libinput,const struct libinput_interface * interface,const struct libinput_interface_backend * interface_backend,void * user_data)1762 libinput_init(struct libinput *libinput,
1763 	      const struct libinput_interface *interface,
1764 	      const struct libinput_interface_backend *interface_backend,
1765 	      void *user_data)
1766 {
1767 	assert(interface->open_restricted != NULL);
1768 	assert(interface->close_restricted != NULL);
1769 
1770 	libinput->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
1771 	if (libinput->epoll_fd < 0)
1772 		return -1;
1773 
1774 	libinput->events_len = 4;
1775 	libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
1776 	libinput->log_handler = libinput_default_log_func;
1777 	libinput->log_priority = LIBINPUT_LOG_PRIORITY_ERROR;
1778 	libinput->interface = interface;
1779 	libinput->interface_backend = interface_backend;
1780 	libinput->user_data = user_data;
1781 	libinput->refcount = 1;
1782 	list_init(&libinput->source_destroy_list);
1783 	list_init(&libinput->seat_list);
1784 	list_init(&libinput->device_group_list);
1785 	list_init(&libinput->tool_list);
1786 
1787 	if (libinput_timer_subsys_init(libinput) != 0) {
1788 		free(libinput->events);
1789 		close(libinput->epoll_fd);
1790 		return -1;
1791 	}
1792 
1793 	return 0;
1794 }
1795 
1796 void
libinput_init_quirks(struct libinput * libinput)1797 libinput_init_quirks(struct libinput *libinput)
1798 {
1799 	const char *data_path,
1800 	           *override_file = NULL;
1801 	struct quirks_context *quirks;
1802 
1803 	if (libinput->quirks_initialized)
1804 		return;
1805 
1806 	/* If we fail, we'll fail next time too */
1807 	libinput->quirks_initialized = true;
1808 
1809 	data_path = getenv("LIBINPUT_QUIRKS_DIR");
1810 	if (!data_path) {
1811 		data_path = LIBINPUT_QUIRKS_DIR;
1812 		override_file = LIBINPUT_QUIRKS_OVERRIDE_FILE;
1813 	}
1814 
1815 	quirks = quirks_init_subsystem(data_path,
1816 				       override_file,
1817 				       log_msg_va,
1818 				       libinput,
1819 				       QLOG_LIBINPUT_LOGGING);
1820 	if (!quirks) {
1821 		log_error(libinput,
1822 			  "Failed to load the device quirks from %s%s%s. "
1823 			  "This will negatively affect device behavior. "
1824 			  "See %sdevice-quirks.html for details.\n",
1825 			  data_path,
1826 			  override_file ? " and " : "",
1827 			  override_file ? override_file : "",
1828 			  HTTP_DOC_LINK
1829 			  );
1830 		return;
1831 	}
1832 
1833 	libinput->quirks = quirks;
1834 }
1835 
1836 static void
1837 libinput_device_destroy(struct libinput_device *device);
1838 
1839 static void
1840 libinput_seat_destroy(struct libinput_seat *seat);
1841 
1842 static void
libinput_drop_destroyed_sources(struct libinput * libinput)1843 libinput_drop_destroyed_sources(struct libinput *libinput)
1844 {
1845 	struct libinput_source *source, *next;
1846 
1847 	list_for_each_safe(source, next, &libinput->source_destroy_list, link)
1848 		free(source);
1849 	list_init(&libinput->source_destroy_list);
1850 }
1851 
1852 LIBINPUT_EXPORT struct libinput *
libinput_ref(struct libinput * libinput)1853 libinput_ref(struct libinput *libinput)
1854 {
1855 	libinput->refcount++;
1856 	return libinput;
1857 }
1858 
1859 LIBINPUT_EXPORT struct libinput *
libinput_unref(struct libinput * libinput)1860 libinput_unref(struct libinput *libinput)
1861 {
1862 	struct libinput_event *event;
1863 	struct libinput_device *device, *next_device;
1864 	struct libinput_seat *seat, *next_seat;
1865 	struct libinput_tablet_tool *tool, *next_tool;
1866 	struct libinput_device_group *group, *next_group;
1867 
1868 	if (libinput == NULL)
1869 		return NULL;
1870 
1871 	assert(libinput->refcount > 0);
1872 	libinput->refcount--;
1873 	if (libinput->refcount > 0)
1874 		return libinput;
1875 
1876 	libinput_suspend(libinput);
1877 
1878 	libinput->interface_backend->destroy(libinput);
1879 
1880 	while ((event = libinput_get_event(libinput)))
1881 	       libinput_event_destroy(event);
1882 
1883 	free(libinput->events);
1884 
1885 	list_for_each_safe(seat, next_seat, &libinput->seat_list, link) {
1886 		list_for_each_safe(device, next_device,
1887 				   &seat->devices_list,
1888 				   link)
1889 			libinput_device_destroy(device);
1890 
1891 		libinput_seat_destroy(seat);
1892 	}
1893 
1894 	list_for_each_safe(group,
1895 			   next_group,
1896 			   &libinput->device_group_list,
1897 			   link) {
1898 		libinput_device_group_destroy(group);
1899 	}
1900 
1901 	list_for_each_safe(tool, next_tool, &libinput->tool_list, link) {
1902 		libinput_tablet_tool_unref(tool);
1903 	}
1904 
1905 	libinput_timer_subsys_destroy(libinput);
1906 	libinput_drop_destroyed_sources(libinput);
1907 	quirks_context_unref(libinput->quirks);
1908 	close(libinput->epoll_fd);
1909 	free(libinput);
1910 
1911 	return NULL;
1912 }
1913 
1914 static void
libinput_event_tablet_tool_destroy(struct libinput_event_tablet_tool * event)1915 libinput_event_tablet_tool_destroy(struct libinput_event_tablet_tool *event)
1916 {
1917 	libinput_tablet_tool_unref(event->tool);
1918 }
1919 
1920 static void
libinput_event_tablet_pad_destroy(struct libinput_event_tablet_pad * event)1921 libinput_event_tablet_pad_destroy(struct libinput_event_tablet_pad *event)
1922 {
1923 	if (event->base.type != LIBINPUT_EVENT_TABLET_PAD_KEY)
1924 		libinput_tablet_pad_mode_group_unref(event->mode_group);
1925 }
1926 
1927 LIBINPUT_EXPORT void
libinput_event_destroy(struct libinput_event * event)1928 libinput_event_destroy(struct libinput_event *event)
1929 {
1930 	if (event == NULL)
1931 		return;
1932 
1933 	switch(event->type) {
1934 	case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
1935 	case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
1936 	case LIBINPUT_EVENT_TABLET_TOOL_TIP:
1937 	case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
1938 		libinput_event_tablet_tool_destroy(
1939 		   libinput_event_get_tablet_tool_event(event));
1940 		break;
1941 	case LIBINPUT_EVENT_TABLET_PAD_RING:
1942 	case LIBINPUT_EVENT_TABLET_PAD_STRIP:
1943 	case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
1944 	case LIBINPUT_EVENT_TABLET_PAD_KEY:
1945 		libinput_event_tablet_pad_destroy(
1946 		   libinput_event_get_tablet_pad_event(event));
1947 		break;
1948 	default:
1949 		break;
1950 	}
1951 
1952 	if (event->device)
1953 		libinput_device_unref(event->device);
1954 
1955 	free(event);
1956 }
1957 
1958 int
open_restricted(struct libinput * libinput,const char * path,int flags)1959 open_restricted(struct libinput *libinput,
1960 		const char *path, int flags)
1961 {
1962 	return libinput->interface->open_restricted(path,
1963 						    flags,
1964 						    libinput->user_data);
1965 }
1966 
1967 void
close_restricted(struct libinput * libinput,int fd)1968 close_restricted(struct libinput *libinput, int fd)
1969 {
1970 	libinput->interface->close_restricted(fd, libinput->user_data);
1971 }
1972 
1973 bool
ignore_litest_test_suite_device(struct udev_device * device)1974 ignore_litest_test_suite_device(struct udev_device *device)
1975 {
1976 	if (!getenv("LIBINPUT_RUNNING_TEST_SUITE") &&
1977 	    udev_device_get_property_value(device, "LIBINPUT_TEST_DEVICE"))
1978 		return true;
1979 
1980 	return false;
1981 }
1982 
1983 void
libinput_seat_init(struct libinput_seat * seat,struct libinput * libinput,const char * physical_name,const char * logical_name,libinput_seat_destroy_func destroy)1984 libinput_seat_init(struct libinput_seat *seat,
1985 		   struct libinput *libinput,
1986 		   const char *physical_name,
1987 		   const char *logical_name,
1988 		   libinput_seat_destroy_func destroy)
1989 {
1990 	seat->refcount = 1;
1991 	seat->libinput = libinput;
1992 	seat->physical_name = safe_strdup(physical_name);
1993 	seat->logical_name = safe_strdup(logical_name);
1994 	seat->destroy = destroy;
1995 	list_init(&seat->devices_list);
1996 	list_insert(&libinput->seat_list, &seat->link);
1997 }
1998 
1999 LIBINPUT_EXPORT struct libinput_seat *
libinput_seat_ref(struct libinput_seat * seat)2000 libinput_seat_ref(struct libinput_seat *seat)
2001 {
2002 	seat->refcount++;
2003 	return seat;
2004 }
2005 
2006 static void
libinput_seat_destroy(struct libinput_seat * seat)2007 libinput_seat_destroy(struct libinput_seat *seat)
2008 {
2009 	list_remove(&seat->link);
2010 	free(seat->logical_name);
2011 	free(seat->physical_name);
2012 	seat->destroy(seat);
2013 }
2014 
2015 LIBINPUT_EXPORT struct libinput_seat *
libinput_seat_unref(struct libinput_seat * seat)2016 libinput_seat_unref(struct libinput_seat *seat)
2017 {
2018 	assert(seat->refcount > 0);
2019 	seat->refcount--;
2020 	if (seat->refcount == 0) {
2021 		libinput_seat_destroy(seat);
2022 		return NULL;
2023 	} else {
2024 		return seat;
2025 	}
2026 }
2027 
2028 LIBINPUT_EXPORT void
libinput_seat_set_user_data(struct libinput_seat * seat,void * user_data)2029 libinput_seat_set_user_data(struct libinput_seat *seat, void *user_data)
2030 {
2031 	seat->user_data = user_data;
2032 }
2033 
2034 LIBINPUT_EXPORT void *
libinput_seat_get_user_data(struct libinput_seat * seat)2035 libinput_seat_get_user_data(struct libinput_seat *seat)
2036 {
2037 	return seat->user_data;
2038 }
2039 
2040 LIBINPUT_EXPORT struct libinput *
libinput_seat_get_context(struct libinput_seat * seat)2041 libinput_seat_get_context(struct libinput_seat *seat)
2042 {
2043 	return seat->libinput;
2044 }
2045 
2046 LIBINPUT_EXPORT const char *
libinput_seat_get_physical_name(struct libinput_seat * seat)2047 libinput_seat_get_physical_name(struct libinput_seat *seat)
2048 {
2049 	return seat->physical_name;
2050 }
2051 
2052 LIBINPUT_EXPORT const char *
libinput_seat_get_logical_name(struct libinput_seat * seat)2053 libinput_seat_get_logical_name(struct libinput_seat *seat)
2054 {
2055 	return seat->logical_name;
2056 }
2057 
2058 void
libinput_device_init(struct libinput_device * device,struct libinput_seat * seat)2059 libinput_device_init(struct libinput_device *device,
2060 		     struct libinput_seat *seat)
2061 {
2062 	device->seat = seat;
2063 	device->refcount = 1;
2064 	list_init(&device->event_listeners);
2065 }
2066 
2067 LIBINPUT_EXPORT struct libinput_device *
libinput_device_ref(struct libinput_device * device)2068 libinput_device_ref(struct libinput_device *device)
2069 {
2070 	device->refcount++;
2071 	return device;
2072 }
2073 
2074 static void
libinput_device_destroy(struct libinput_device * device)2075 libinput_device_destroy(struct libinput_device *device)
2076 {
2077 	assert(list_empty(&device->event_listeners));
2078 	evdev_device_destroy(evdev_device(device));
2079 }
2080 
2081 LIBINPUT_EXPORT struct libinput_device *
libinput_device_unref(struct libinput_device * device)2082 libinput_device_unref(struct libinput_device *device)
2083 {
2084 	assert(device->refcount > 0);
2085 	device->refcount--;
2086 	if (device->refcount == 0) {
2087 		libinput_device_destroy(device);
2088 		return NULL;
2089 	} else {
2090 		return device;
2091 	}
2092 }
2093 
2094 LIBINPUT_EXPORT int
libinput_get_fd(struct libinput * libinput)2095 libinput_get_fd(struct libinput *libinput)
2096 {
2097 	return libinput->epoll_fd;
2098 }
2099 
2100 LIBINPUT_EXPORT int
libinput_dispatch(struct libinput * libinput)2101 libinput_dispatch(struct libinput *libinput)
2102 {
2103 	static uint8_t take_time_snapshot;
2104 	struct libinput_source *source;
2105 	struct epoll_event ep[32];
2106 	int i, count;
2107 
2108 	/* Every 10 calls to libinput_dispatch() we take the current time so
2109 	 * we can check the delay between our current time and the event
2110 	 * timestamps */
2111 	if ((++take_time_snapshot % 10) == 0)
2112 		libinput->dispatch_time = libinput_now(libinput);
2113 	else if (libinput->dispatch_time)
2114 		libinput->dispatch_time = 0;
2115 
2116 	count = epoll_wait(libinput->epoll_fd, ep, ARRAY_LENGTH(ep), 0);
2117 	if (count < 0)
2118 		return -errno;
2119 
2120 	for (i = 0; i < count; ++i) {
2121 		source = ep[i].data.ptr;
2122 		if (source->fd == -1)
2123 			continue;
2124 
2125 		source->dispatch(source->user_data);
2126 	}
2127 
2128 	libinput_drop_destroyed_sources(libinput);
2129 
2130 	return 0;
2131 }
2132 
2133 void
libinput_device_init_event_listener(struct libinput_event_listener * listener)2134 libinput_device_init_event_listener(struct libinput_event_listener *listener)
2135 {
2136 	list_init(&listener->link);
2137 }
2138 
2139 void
libinput_device_add_event_listener(struct libinput_device * device,struct libinput_event_listener * listener,void (* notify_func)(uint64_t time,struct libinput_event * event,void * notify_func_data),void * notify_func_data)2140 libinput_device_add_event_listener(struct libinput_device *device,
2141 				   struct libinput_event_listener *listener,
2142 				   void (*notify_func)(
2143 						uint64_t time,
2144 						struct libinput_event *event,
2145 						void *notify_func_data),
2146 				   void *notify_func_data)
2147 {
2148 	listener->notify_func = notify_func;
2149 	listener->notify_func_data = notify_func_data;
2150 	list_insert(&device->event_listeners, &listener->link);
2151 }
2152 
2153 void
libinput_device_remove_event_listener(struct libinput_event_listener * listener)2154 libinput_device_remove_event_listener(struct libinput_event_listener *listener)
2155 {
2156 	list_remove(&listener->link);
2157 }
2158 
2159 static uint32_t
update_seat_key_count(struct libinput_seat * seat,int32_t key,enum libinput_key_state state)2160 update_seat_key_count(struct libinput_seat *seat,
2161 		      int32_t key,
2162 		      enum libinput_key_state state)
2163 {
2164 	assert(key >= 0 && key <= KEY_MAX);
2165 
2166 	switch (state) {
2167 	case LIBINPUT_KEY_STATE_PRESSED:
2168 		return ++seat->button_count[key];
2169 	case LIBINPUT_KEY_STATE_RELEASED:
2170 		/* We might not have received the first PRESSED event. */
2171 		if (seat->button_count[key] == 0)
2172 			return 0;
2173 
2174 		return --seat->button_count[key];
2175 	}
2176 
2177 	return 0;
2178 }
2179 
2180 static uint32_t
update_seat_button_count(struct libinput_seat * seat,int32_t button,enum libinput_button_state state)2181 update_seat_button_count(struct libinput_seat *seat,
2182 			 int32_t button,
2183 			 enum libinput_button_state state)
2184 {
2185 	assert(button >= 0 && button <= KEY_MAX);
2186 
2187 	switch (state) {
2188 	case LIBINPUT_BUTTON_STATE_PRESSED:
2189 		return ++seat->button_count[button];
2190 	case LIBINPUT_BUTTON_STATE_RELEASED:
2191 		/* We might not have received the first PRESSED event. */
2192 		if (seat->button_count[button] == 0)
2193 			return 0;
2194 
2195 		return --seat->button_count[button];
2196 	}
2197 
2198 	return 0;
2199 }
2200 
2201 static void
init_event_base(struct libinput_event * event,struct libinput_device * device,enum libinput_event_type type)2202 init_event_base(struct libinput_event *event,
2203 		struct libinput_device *device,
2204 		enum libinput_event_type type)
2205 {
2206 	event->type = type;
2207 	event->device = device;
2208 }
2209 
2210 static void
post_base_event(struct libinput_device * device,enum libinput_event_type type,struct libinput_event * event)2211 post_base_event(struct libinput_device *device,
2212 		enum libinput_event_type type,
2213 		struct libinput_event *event)
2214 {
2215 	struct libinput *libinput = device->seat->libinput;
2216 	init_event_base(event, device, type);
2217 	libinput_post_event(libinput, event);
2218 }
2219 
2220 static void
post_device_event(struct libinput_device * device,uint64_t time,enum libinput_event_type type,struct libinput_event * event)2221 post_device_event(struct libinput_device *device,
2222 		  uint64_t time,
2223 		  enum libinput_event_type type,
2224 		  struct libinput_event *event)
2225 {
2226 	struct libinput_event_listener *listener, *tmp;
2227 #if 0
2228 	struct libinput *libinput = device->seat->libinput;
2229 
2230 	if (libinput->last_event_time > time) {
2231 		log_bug_libinput(device->seat->libinput,
2232 				 "out-of-order timestamps for %s time %" PRIu64 "\n",
2233 				 event_type_to_str(type),
2234 				 time);
2235 	}
2236 	libinput->last_event_time = time;
2237 #endif
2238 
2239 	init_event_base(event, device, type);
2240 
2241 	list_for_each_safe(listener, tmp, &device->event_listeners, link)
2242 		listener->notify_func(time, event, listener->notify_func_data);
2243 
2244 	libinput_post_event(device->seat->libinput, event);
2245 }
2246 
2247 void
notify_added_device(struct libinput_device * device)2248 notify_added_device(struct libinput_device *device)
2249 {
2250 	struct libinput_event_device_notify *added_device_event;
2251 
2252 	added_device_event = zalloc(sizeof *added_device_event);
2253 
2254 	post_base_event(device,
2255 			LIBINPUT_EVENT_DEVICE_ADDED,
2256 			&added_device_event->base);
2257 
2258 #ifdef __clang_analyzer__
2259 	/* clang doesn't realize we're not leaking the event here, so
2260 	 * pretend to free it  */
2261 	free(added_device_event);
2262 #endif
2263 }
2264 
2265 void
notify_removed_device(struct libinput_device * device)2266 notify_removed_device(struct libinput_device *device)
2267 {
2268 	struct libinput_event_device_notify *removed_device_event;
2269 
2270 	removed_device_event = zalloc(sizeof *removed_device_event);
2271 
2272 	post_base_event(device,
2273 			LIBINPUT_EVENT_DEVICE_REMOVED,
2274 			&removed_device_event->base);
2275 
2276 #ifdef __clang_analyzer__
2277 	/* clang doesn't realize we're not leaking the event here, so
2278 	 * pretend to free it  */
2279 	free(removed_device_event);
2280 #endif
2281 }
2282 
2283 static inline bool
device_has_cap(struct libinput_device * device,enum libinput_device_capability cap)2284 device_has_cap(struct libinput_device *device,
2285 	       enum libinput_device_capability cap)
2286 {
2287 	const char *capability;
2288 
2289 	if (libinput_device_has_capability(device, cap))
2290 		return true;
2291 
2292 	switch (cap) {
2293 	case LIBINPUT_DEVICE_CAP_POINTER:
2294 		capability = "CAP_POINTER";
2295 		break;
2296 	case LIBINPUT_DEVICE_CAP_KEYBOARD:
2297 		capability = "CAP_KEYBOARD";
2298 		break;
2299 	case LIBINPUT_DEVICE_CAP_TOUCH:
2300 		capability = "CAP_TOUCH";
2301 		break;
2302 	case LIBINPUT_DEVICE_CAP_GESTURE:
2303 		capability = "CAP_GESTURE";
2304 		break;
2305 	case LIBINPUT_DEVICE_CAP_TABLET_TOOL:
2306 		capability = "CAP_TABLET_TOOL";
2307 		break;
2308 	case LIBINPUT_DEVICE_CAP_TABLET_PAD:
2309 		capability = "CAP_TABLET_PAD";
2310 		break;
2311 	case LIBINPUT_DEVICE_CAP_SWITCH:
2312 		capability = "CAP_SWITCH";
2313 		break;
2314 	}
2315 
2316 	log_bug_libinput(device->seat->libinput,
2317 			 "Event for missing capability %s on device \"%s\"\n",
2318 			 capability,
2319 			 libinput_device_get_name(device));
2320 
2321 	return false;
2322 }
2323 
2324 void
keyboard_notify_key(struct libinput_device * device,uint64_t time,uint32_t key,enum libinput_key_state state)2325 keyboard_notify_key(struct libinput_device *device,
2326 		    uint64_t time,
2327 		    uint32_t key,
2328 		    enum libinput_key_state state)
2329 {
2330 	struct libinput_event_keyboard *key_event;
2331 	uint32_t seat_key_count;
2332 
2333 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_KEYBOARD))
2334 		return;
2335 
2336 	key_event = zalloc(sizeof *key_event);
2337 
2338 	seat_key_count = update_seat_key_count(device->seat, key, state);
2339 
2340 	*key_event = (struct libinput_event_keyboard) {
2341 		.time = time,
2342 		.key = key,
2343 		.state = state,
2344 		.seat_key_count = seat_key_count,
2345 	};
2346 
2347 	post_device_event(device, time,
2348 			  LIBINPUT_EVENT_KEYBOARD_KEY,
2349 			  &key_event->base);
2350 }
2351 
2352 void
pointer_notify_motion(struct libinput_device * device,uint64_t time,const struct normalized_coords * delta,const struct device_float_coords * raw)2353 pointer_notify_motion(struct libinput_device *device,
2354 		      uint64_t time,
2355 		      const struct normalized_coords *delta,
2356 		      const struct device_float_coords *raw)
2357 {
2358 	struct libinput_event_pointer *motion_event;
2359 
2360 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_POINTER))
2361 		return;
2362 
2363 	motion_event = zalloc(sizeof *motion_event);
2364 
2365 	*motion_event = (struct libinput_event_pointer) {
2366 		.time = time,
2367 		.delta = *delta,
2368 		.delta_raw = *raw,
2369 	};
2370 
2371 	post_device_event(device, time,
2372 			  LIBINPUT_EVENT_POINTER_MOTION,
2373 			  &motion_event->base);
2374 }
2375 
2376 void
pointer_notify_motion_absolute(struct libinput_device * device,uint64_t time,const struct device_coords * point)2377 pointer_notify_motion_absolute(struct libinput_device *device,
2378 			       uint64_t time,
2379 			       const struct device_coords *point)
2380 {
2381 	struct libinput_event_pointer *motion_absolute_event;
2382 
2383 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_POINTER))
2384 		return;
2385 
2386 	motion_absolute_event = zalloc(sizeof *motion_absolute_event);
2387 
2388 	*motion_absolute_event = (struct libinput_event_pointer) {
2389 		.time = time,
2390 		.absolute = *point,
2391 	};
2392 
2393 	post_device_event(device, time,
2394 			  LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
2395 			  &motion_absolute_event->base);
2396 }
2397 
2398 void
pointer_notify_button(struct libinput_device * device,uint64_t time,int32_t button,enum libinput_button_state state)2399 pointer_notify_button(struct libinput_device *device,
2400 		      uint64_t time,
2401 		      int32_t button,
2402 		      enum libinput_button_state state)
2403 {
2404 	struct libinput_event_pointer *button_event;
2405 	int32_t seat_button_count;
2406 
2407 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_POINTER))
2408 		return;
2409 
2410 	button_event = zalloc(sizeof *button_event);
2411 
2412 	seat_button_count = update_seat_button_count(device->seat,
2413 						     button,
2414 						     state);
2415 
2416 	*button_event = (struct libinput_event_pointer) {
2417 		.time = time,
2418 		.button = button,
2419 		.state = state,
2420 		.seat_button_count = seat_button_count,
2421 	};
2422 
2423 	post_device_event(device, time,
2424 			  LIBINPUT_EVENT_POINTER_BUTTON,
2425 			  &button_event->base);
2426 }
2427 
2428 void
pointer_notify_axis(struct libinput_device * device,uint64_t time,uint32_t axes,enum libinput_pointer_axis_source source,const struct normalized_coords * delta,const struct discrete_coords * discrete)2429 pointer_notify_axis(struct libinput_device *device,
2430 		    uint64_t time,
2431 		    uint32_t axes,
2432 		    enum libinput_pointer_axis_source source,
2433 		    const struct normalized_coords *delta,
2434 		    const struct discrete_coords *discrete)
2435 {
2436 	struct libinput_event_pointer *axis_event;
2437 
2438 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_POINTER))
2439 		return;
2440 
2441 	axis_event = zalloc(sizeof *axis_event);
2442 
2443 	*axis_event = (struct libinput_event_pointer) {
2444 		.time = time,
2445 		.delta = *delta,
2446 		.source = source,
2447 		.axes = axes,
2448 		.discrete = *discrete,
2449 	};
2450 
2451 	post_device_event(device, time,
2452 			  LIBINPUT_EVENT_POINTER_AXIS,
2453 			  &axis_event->base);
2454 }
2455 
2456 void
touch_notify_touch_down(struct libinput_device * device,uint64_t time,int32_t slot,int32_t seat_slot,const struct device_coords * point)2457 touch_notify_touch_down(struct libinput_device *device,
2458 			uint64_t time,
2459 			int32_t slot,
2460 			int32_t seat_slot,
2461 			const struct device_coords *point)
2462 {
2463 	struct libinput_event_touch *touch_event;
2464 
2465 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_TOUCH))
2466 		return;
2467 
2468 	touch_event = zalloc(sizeof *touch_event);
2469 
2470 	*touch_event = (struct libinput_event_touch) {
2471 		.time = time,
2472 		.slot = slot,
2473 		.seat_slot = seat_slot,
2474 		.point = *point,
2475 	};
2476 
2477 	post_device_event(device, time,
2478 			  LIBINPUT_EVENT_TOUCH_DOWN,
2479 			  &touch_event->base);
2480 }
2481 
2482 void
touch_notify_touch_motion(struct libinput_device * device,uint64_t time,int32_t slot,int32_t seat_slot,const struct device_coords * point)2483 touch_notify_touch_motion(struct libinput_device *device,
2484 			  uint64_t time,
2485 			  int32_t slot,
2486 			  int32_t seat_slot,
2487 			  const struct device_coords *point)
2488 {
2489 	struct libinput_event_touch *touch_event;
2490 
2491 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_TOUCH))
2492 		return;
2493 
2494 	touch_event = zalloc(sizeof *touch_event);
2495 
2496 	*touch_event = (struct libinput_event_touch) {
2497 		.time = time,
2498 		.slot = slot,
2499 		.seat_slot = seat_slot,
2500 		.point = *point,
2501 	};
2502 
2503 	post_device_event(device, time,
2504 			  LIBINPUT_EVENT_TOUCH_MOTION,
2505 			  &touch_event->base);
2506 }
2507 
2508 void
touch_notify_touch_up(struct libinput_device * device,uint64_t time,int32_t slot,int32_t seat_slot)2509 touch_notify_touch_up(struct libinput_device *device,
2510 		      uint64_t time,
2511 		      int32_t slot,
2512 		      int32_t seat_slot)
2513 {
2514 	struct libinput_event_touch *touch_event;
2515 
2516 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_TOUCH))
2517 		return;
2518 
2519 	touch_event = zalloc(sizeof *touch_event);
2520 
2521 	*touch_event = (struct libinput_event_touch) {
2522 		.time = time,
2523 		.slot = slot,
2524 		.seat_slot = seat_slot,
2525 	};
2526 
2527 	post_device_event(device, time,
2528 			  LIBINPUT_EVENT_TOUCH_UP,
2529 			  &touch_event->base);
2530 }
2531 
2532 void
touch_notify_touch_cancel(struct libinput_device * device,uint64_t time,int32_t slot,int32_t seat_slot)2533 touch_notify_touch_cancel(struct libinput_device *device,
2534 			  uint64_t time,
2535 			  int32_t slot,
2536 			  int32_t seat_slot)
2537 {
2538 	struct libinput_event_touch *touch_event;
2539 
2540 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_TOUCH))
2541 		return;
2542 
2543 	touch_event = zalloc(sizeof *touch_event);
2544 
2545 	*touch_event = (struct libinput_event_touch) {
2546 		.time = time,
2547 		.slot = slot,
2548 		.seat_slot = seat_slot,
2549 	};
2550 
2551 	post_device_event(device, time,
2552 			  LIBINPUT_EVENT_TOUCH_CANCEL,
2553 			  &touch_event->base);
2554 }
2555 
2556 void
touch_notify_frame(struct libinput_device * device,uint64_t time)2557 touch_notify_frame(struct libinput_device *device,
2558 		   uint64_t time)
2559 {
2560 	struct libinput_event_touch *touch_event;
2561 
2562 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_TOUCH))
2563 		return;
2564 
2565 	touch_event = zalloc(sizeof *touch_event);
2566 
2567 	*touch_event = (struct libinput_event_touch) {
2568 		.time = time,
2569 	};
2570 
2571 	post_device_event(device, time,
2572 			  LIBINPUT_EVENT_TOUCH_FRAME,
2573 			  &touch_event->base);
2574 }
2575 
2576 void
tablet_notify_axis(struct libinput_device * device,uint64_t time,struct libinput_tablet_tool * tool,enum libinput_tablet_tool_tip_state tip_state,unsigned char * changed_axes,const struct tablet_axes * axes)2577 tablet_notify_axis(struct libinput_device *device,
2578 		   uint64_t time,
2579 		   struct libinput_tablet_tool *tool,
2580 		   enum libinput_tablet_tool_tip_state tip_state,
2581 		   unsigned char *changed_axes,
2582 		   const struct tablet_axes *axes)
2583 {
2584 	struct libinput_event_tablet_tool *axis_event;
2585 
2586 	axis_event = zalloc(sizeof *axis_event);
2587 
2588 	*axis_event = (struct libinput_event_tablet_tool) {
2589 		.time = time,
2590 		.tool = libinput_tablet_tool_ref(tool),
2591 		.proximity_state = LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN,
2592 		.tip_state = tip_state,
2593 		.axes = *axes,
2594 	};
2595 
2596 	memcpy(axis_event->changed_axes,
2597 	       changed_axes,
2598 	       sizeof(axis_event->changed_axes));
2599 
2600 	post_device_event(device,
2601 			  time,
2602 			  LIBINPUT_EVENT_TABLET_TOOL_AXIS,
2603 			  &axis_event->base);
2604 }
2605 
2606 void
tablet_notify_proximity(struct libinput_device * device,uint64_t time,struct libinput_tablet_tool * tool,enum libinput_tablet_tool_proximity_state proximity_state,unsigned char * changed_axes,const struct tablet_axes * axes)2607 tablet_notify_proximity(struct libinput_device *device,
2608 			uint64_t time,
2609 			struct libinput_tablet_tool *tool,
2610 			enum libinput_tablet_tool_proximity_state proximity_state,
2611 			unsigned char *changed_axes,
2612 			const struct tablet_axes *axes)
2613 {
2614 	struct libinput_event_tablet_tool *proximity_event;
2615 
2616 	proximity_event = zalloc(sizeof *proximity_event);
2617 
2618 	*proximity_event = (struct libinput_event_tablet_tool) {
2619 		.time = time,
2620 		.tool = libinput_tablet_tool_ref(tool),
2621 		.tip_state = LIBINPUT_TABLET_TOOL_TIP_UP,
2622 		.proximity_state = proximity_state,
2623 		.axes = *axes,
2624 	};
2625 	memcpy(proximity_event->changed_axes,
2626 	       changed_axes,
2627 	       sizeof(proximity_event->changed_axes));
2628 
2629 	post_device_event(device,
2630 			  time,
2631 			  LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY,
2632 			  &proximity_event->base);
2633 }
2634 
2635 void
tablet_notify_tip(struct libinput_device * device,uint64_t time,struct libinput_tablet_tool * tool,enum libinput_tablet_tool_tip_state tip_state,unsigned char * changed_axes,const struct tablet_axes * axes)2636 tablet_notify_tip(struct libinput_device *device,
2637 		  uint64_t time,
2638 		  struct libinput_tablet_tool *tool,
2639 		  enum libinput_tablet_tool_tip_state tip_state,
2640 		  unsigned char *changed_axes,
2641 		  const struct tablet_axes *axes)
2642 {
2643 	struct libinput_event_tablet_tool *tip_event;
2644 
2645 	tip_event = zalloc(sizeof *tip_event);
2646 
2647 	*tip_event = (struct libinput_event_tablet_tool) {
2648 		.time = time,
2649 		.tool = libinput_tablet_tool_ref(tool),
2650 		.tip_state = tip_state,
2651 		.proximity_state = LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN,
2652 		.axes = *axes,
2653 	};
2654 	memcpy(tip_event->changed_axes,
2655 	       changed_axes,
2656 	       sizeof(tip_event->changed_axes));
2657 
2658 	post_device_event(device,
2659 			  time,
2660 			  LIBINPUT_EVENT_TABLET_TOOL_TIP,
2661 			  &tip_event->base);
2662 }
2663 
2664 void
tablet_notify_button(struct libinput_device * device,uint64_t time,struct libinput_tablet_tool * tool,enum libinput_tablet_tool_tip_state tip_state,const struct tablet_axes * axes,int32_t button,enum libinput_button_state state)2665 tablet_notify_button(struct libinput_device *device,
2666 		     uint64_t time,
2667 		     struct libinput_tablet_tool *tool,
2668 		     enum libinput_tablet_tool_tip_state tip_state,
2669 		     const struct tablet_axes *axes,
2670 		     int32_t button,
2671 		     enum libinput_button_state state)
2672 {
2673 	struct libinput_event_tablet_tool *button_event;
2674 	int32_t seat_button_count;
2675 
2676 	button_event = zalloc(sizeof *button_event);
2677 
2678 	seat_button_count = update_seat_button_count(device->seat,
2679 						     button,
2680 						     state);
2681 
2682 	*button_event = (struct libinput_event_tablet_tool) {
2683 		.time = time,
2684 		.tool = libinput_tablet_tool_ref(tool),
2685 		.button = button,
2686 		.state = state,
2687 		.seat_button_count = seat_button_count,
2688 		.proximity_state = LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN,
2689 		.tip_state = tip_state,
2690 		.axes = *axes,
2691 	};
2692 
2693 	post_device_event(device,
2694 			  time,
2695 			  LIBINPUT_EVENT_TABLET_TOOL_BUTTON,
2696 			  &button_event->base);
2697 }
2698 
2699 void
tablet_pad_notify_button(struct libinput_device * device,uint64_t time,int32_t button,enum libinput_button_state state,struct libinput_tablet_pad_mode_group * group)2700 tablet_pad_notify_button(struct libinput_device *device,
2701 			 uint64_t time,
2702 			 int32_t button,
2703 			 enum libinput_button_state state,
2704 			 struct libinput_tablet_pad_mode_group *group)
2705 {
2706 	struct libinput_event_tablet_pad *button_event;
2707 	unsigned int mode;
2708 
2709 	button_event = zalloc(sizeof *button_event);
2710 
2711 	mode = libinput_tablet_pad_mode_group_get_mode(group);
2712 
2713 	*button_event = (struct libinput_event_tablet_pad) {
2714 		.time = time,
2715 		.button.number = button,
2716 		.button.state = state,
2717 		.mode_group = libinput_tablet_pad_mode_group_ref(group),
2718 		.mode = mode,
2719 	};
2720 
2721 	post_device_event(device,
2722 			  time,
2723 			  LIBINPUT_EVENT_TABLET_PAD_BUTTON,
2724 			  &button_event->base);
2725 }
2726 
2727 void
tablet_pad_notify_ring(struct libinput_device * device,uint64_t time,unsigned int number,double value,enum libinput_tablet_pad_ring_axis_source source,struct libinput_tablet_pad_mode_group * group)2728 tablet_pad_notify_ring(struct libinput_device *device,
2729 		       uint64_t time,
2730 		       unsigned int number,
2731 		       double value,
2732 		       enum libinput_tablet_pad_ring_axis_source source,
2733 		       struct libinput_tablet_pad_mode_group *group)
2734 {
2735 	struct libinput_event_tablet_pad *ring_event;
2736 	unsigned int mode;
2737 
2738 	ring_event = zalloc(sizeof *ring_event);
2739 
2740 	mode = libinput_tablet_pad_mode_group_get_mode(group);
2741 
2742 	*ring_event = (struct libinput_event_tablet_pad) {
2743 		.time = time,
2744 		.ring.number = number,
2745 		.ring.position = value,
2746 		.ring.source = source,
2747 		.mode_group = libinput_tablet_pad_mode_group_ref(group),
2748 		.mode = mode,
2749 	};
2750 
2751 	post_device_event(device,
2752 			  time,
2753 			  LIBINPUT_EVENT_TABLET_PAD_RING,
2754 			  &ring_event->base);
2755 }
2756 
2757 void
tablet_pad_notify_strip(struct libinput_device * device,uint64_t time,unsigned int number,double value,enum libinput_tablet_pad_strip_axis_source source,struct libinput_tablet_pad_mode_group * group)2758 tablet_pad_notify_strip(struct libinput_device *device,
2759 			uint64_t time,
2760 			unsigned int number,
2761 			double value,
2762 			enum libinput_tablet_pad_strip_axis_source source,
2763 			struct libinput_tablet_pad_mode_group *group)
2764 {
2765 	struct libinput_event_tablet_pad *strip_event;
2766 	unsigned int mode;
2767 
2768 	strip_event = zalloc(sizeof *strip_event);
2769 
2770 	mode = libinput_tablet_pad_mode_group_get_mode(group);
2771 
2772 	*strip_event = (struct libinput_event_tablet_pad) {
2773 		.time = time,
2774 		.strip.number = number,
2775 		.strip.position = value,
2776 		.strip.source = source,
2777 		.mode_group = libinput_tablet_pad_mode_group_ref(group),
2778 		.mode = mode,
2779 	};
2780 
2781 	post_device_event(device,
2782 			  time,
2783 			  LIBINPUT_EVENT_TABLET_PAD_STRIP,
2784 			  &strip_event->base);
2785 }
2786 
2787 void
tablet_pad_notify_key(struct libinput_device * device,uint64_t time,int32_t key,enum libinput_key_state state)2788 tablet_pad_notify_key(struct libinput_device *device,
2789 		      uint64_t time,
2790 		      int32_t key,
2791 		      enum libinput_key_state state)
2792 {
2793 	struct libinput_event_tablet_pad *key_event;
2794 
2795 	key_event = zalloc(sizeof *key_event);
2796 
2797 	*key_event = (struct libinput_event_tablet_pad) {
2798 		.time = time,
2799 		.key.code = key,
2800 		.key.state = state,
2801 	};
2802 
2803 	post_device_event(device,
2804 			  time,
2805 			  LIBINPUT_EVENT_TABLET_PAD_KEY,
2806 			  &key_event->base);
2807 }
2808 
2809 static void
gesture_notify(struct libinput_device * device,uint64_t time,enum libinput_event_type type,int finger_count,int cancelled,const struct normalized_coords * delta,const struct normalized_coords * unaccel,double scale,double angle)2810 gesture_notify(struct libinput_device *device,
2811 	       uint64_t time,
2812 	       enum libinput_event_type type,
2813 	       int finger_count,
2814 	       int cancelled,
2815 	       const struct normalized_coords *delta,
2816 	       const struct normalized_coords *unaccel,
2817 	       double scale,
2818 	       double angle)
2819 {
2820 	struct libinput_event_gesture *gesture_event;
2821 
2822 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_GESTURE))
2823 		return;
2824 
2825 	gesture_event = zalloc(sizeof *gesture_event);
2826 
2827 	*gesture_event = (struct libinput_event_gesture) {
2828 		.time = time,
2829 		.finger_count = finger_count,
2830 		.cancelled = cancelled,
2831 		.delta = *delta,
2832 		.delta_unaccel = *unaccel,
2833 		.scale = scale,
2834 		.angle = angle,
2835 	};
2836 
2837 	post_device_event(device, time, type,
2838 			  &gesture_event->base);
2839 }
2840 
2841 void
gesture_notify_swipe(struct libinput_device * device,uint64_t time,enum libinput_event_type type,int finger_count,const struct normalized_coords * delta,const struct normalized_coords * unaccel)2842 gesture_notify_swipe(struct libinput_device *device,
2843 		     uint64_t time,
2844 		     enum libinput_event_type type,
2845 		     int finger_count,
2846 		     const struct normalized_coords *delta,
2847 		     const struct normalized_coords *unaccel)
2848 {
2849 	gesture_notify(device, time, type, finger_count, 0, delta, unaccel,
2850 		       0.0, 0.0);
2851 }
2852 
2853 void
gesture_notify_swipe_end(struct libinput_device * device,uint64_t time,int finger_count,int cancelled)2854 gesture_notify_swipe_end(struct libinput_device *device,
2855 			 uint64_t time,
2856 			 int finger_count,
2857 			 int cancelled)
2858 {
2859 	const struct normalized_coords zero = { 0.0, 0.0 };
2860 
2861 	gesture_notify(device, time, LIBINPUT_EVENT_GESTURE_SWIPE_END,
2862 		       finger_count, cancelled, &zero, &zero, 0.0, 0.0);
2863 }
2864 
2865 void
gesture_notify_pinch(struct libinput_device * device,uint64_t time,enum libinput_event_type type,int finger_count,const struct normalized_coords * delta,const struct normalized_coords * unaccel,double scale,double angle)2866 gesture_notify_pinch(struct libinput_device *device,
2867 		     uint64_t time,
2868 		     enum libinput_event_type type,
2869 		     int finger_count,
2870 		     const struct normalized_coords *delta,
2871 		     const struct normalized_coords *unaccel,
2872 		     double scale,
2873 		     double angle)
2874 {
2875 	gesture_notify(device, time, type, finger_count, 0,
2876 		       delta, unaccel, scale, angle);
2877 }
2878 
2879 void
gesture_notify_pinch_end(struct libinput_device * device,uint64_t time,int finger_count,double scale,int cancelled)2880 gesture_notify_pinch_end(struct libinput_device *device,
2881 			 uint64_t time,
2882 			 int finger_count,
2883 			 double scale,
2884 			 int cancelled)
2885 {
2886 	const struct normalized_coords zero = { 0.0, 0.0 };
2887 
2888 	gesture_notify(device, time, LIBINPUT_EVENT_GESTURE_PINCH_END,
2889 		       finger_count, cancelled, &zero, &zero, scale, 0.0);
2890 }
2891 
2892 void
switch_notify_toggle(struct libinput_device * device,uint64_t time,enum libinput_switch sw,enum libinput_switch_state state)2893 switch_notify_toggle(struct libinput_device *device,
2894 		     uint64_t time,
2895 		     enum libinput_switch sw,
2896 		     enum libinput_switch_state state)
2897 {
2898 	struct libinput_event_switch *switch_event;
2899 
2900 	if (!device_has_cap(device, LIBINPUT_DEVICE_CAP_SWITCH))
2901 		return;
2902 
2903 	switch_event = zalloc(sizeof *switch_event);
2904 
2905 	*switch_event = (struct libinput_event_switch) {
2906 		.time = time,
2907 		.sw = sw,
2908 		.state = state,
2909 	};
2910 
2911 	post_device_event(device, time,
2912 			  LIBINPUT_EVENT_SWITCH_TOGGLE,
2913 			  &switch_event->base);
2914 
2915 #ifdef __clang_analyzer__
2916 	/* clang doesn't realize we're not leaking the event here, so
2917 	 * pretend to free it  */
2918 	free(switch_event);
2919 #endif
2920 }
2921 
2922 static void
libinput_post_event(struct libinput * libinput,struct libinput_event * event)2923 libinput_post_event(struct libinput *libinput,
2924 		    struct libinput_event *event)
2925 {
2926 	struct libinput_event **events = libinput->events;
2927 	size_t events_len = libinput->events_len;
2928 	size_t events_count = libinput->events_count;
2929 	size_t move_len;
2930 	size_t new_out;
2931 
2932 #if 0
2933 	log_debug(libinput, "Queuing %s\n", event_type_to_str(event->type));
2934 #endif
2935 
2936 	events_count++;
2937 	if (events_count > events_len) {
2938 		void *tmp;
2939 
2940 		events_len *= 2;
2941 		tmp = realloc(events, events_len * sizeof *events);
2942 		if (!tmp) {
2943 			log_error(libinput,
2944 				  "Failed to reallocate event ring buffer. "
2945 				  "Events may be discarded\n");
2946 			return;
2947 		}
2948 
2949 		events = tmp;
2950 
2951 		if (libinput->events_count > 0 && libinput->events_in == 0) {
2952 			libinput->events_in = libinput->events_len;
2953 		} else if (libinput->events_count > 0 &&
2954 			   libinput->events_out >= libinput->events_in) {
2955 			move_len = libinput->events_len - libinput->events_out;
2956 			new_out = events_len - move_len;
2957 			memmove(events + new_out,
2958 				events + libinput->events_out,
2959 				move_len * sizeof *events);
2960 			libinput->events_out = new_out;
2961 		}
2962 
2963 		libinput->events = events;
2964 		libinput->events_len = events_len;
2965 	}
2966 
2967 	if (event->device)
2968 		libinput_device_ref(event->device);
2969 
2970 	libinput->events_count = events_count;
2971 	events[libinput->events_in] = event;
2972 	libinput->events_in = (libinput->events_in + 1) % libinput->events_len;
2973 }
2974 
2975 LIBINPUT_EXPORT struct libinput_event *
libinput_get_event(struct libinput * libinput)2976 libinput_get_event(struct libinput *libinput)
2977 {
2978 	struct libinput_event *event;
2979 
2980 	if (libinput->events_count == 0)
2981 		return NULL;
2982 
2983 	event = libinput->events[libinput->events_out];
2984 	libinput->events_out =
2985 		(libinput->events_out + 1) % libinput->events_len;
2986 	libinput->events_count--;
2987 
2988 	return event;
2989 }
2990 
2991 LIBINPUT_EXPORT enum libinput_event_type
libinput_next_event_type(struct libinput * libinput)2992 libinput_next_event_type(struct libinput *libinput)
2993 {
2994 	struct libinput_event *event;
2995 
2996 	if (libinput->events_count == 0)
2997 		return LIBINPUT_EVENT_NONE;
2998 
2999 	event = libinput->events[libinput->events_out];
3000 	return event->type;
3001 }
3002 
3003 LIBINPUT_EXPORT void
libinput_set_user_data(struct libinput * libinput,void * user_data)3004 libinput_set_user_data(struct libinput *libinput,
3005 		       void *user_data)
3006 {
3007 	libinput->user_data = user_data;
3008 }
3009 
3010 LIBINPUT_EXPORT void *
libinput_get_user_data(struct libinput * libinput)3011 libinput_get_user_data(struct libinput *libinput)
3012 {
3013 	return libinput->user_data;
3014 }
3015 
3016 LIBINPUT_EXPORT int
libinput_resume(struct libinput * libinput)3017 libinput_resume(struct libinput *libinput)
3018 {
3019 	return libinput->interface_backend->resume(libinput);
3020 }
3021 
3022 LIBINPUT_EXPORT void
libinput_suspend(struct libinput * libinput)3023 libinput_suspend(struct libinput *libinput)
3024 {
3025 	libinput->interface_backend->suspend(libinput);
3026 }
3027 
3028 LIBINPUT_EXPORT void
libinput_device_set_user_data(struct libinput_device * device,void * user_data)3029 libinput_device_set_user_data(struct libinput_device *device, void *user_data)
3030 {
3031 	device->user_data = user_data;
3032 }
3033 
3034 LIBINPUT_EXPORT void *
libinput_device_get_user_data(struct libinput_device * device)3035 libinput_device_get_user_data(struct libinput_device *device)
3036 {
3037 	return device->user_data;
3038 }
3039 
3040 LIBINPUT_EXPORT struct libinput *
libinput_device_get_context(struct libinput_device * device)3041 libinput_device_get_context(struct libinput_device *device)
3042 {
3043 	return libinput_seat_get_context(device->seat);
3044 }
3045 
3046 LIBINPUT_EXPORT struct libinput_device_group *
libinput_device_get_device_group(struct libinput_device * device)3047 libinput_device_get_device_group(struct libinput_device *device)
3048 {
3049 	return device->group;
3050 }
3051 
3052 LIBINPUT_EXPORT const char *
libinput_device_get_sysname(struct libinput_device * device)3053 libinput_device_get_sysname(struct libinput_device *device)
3054 {
3055 	return evdev_device_get_sysname((struct evdev_device *) device);
3056 }
3057 
3058 LIBINPUT_EXPORT const char *
libinput_device_get_name(struct libinput_device * device)3059 libinput_device_get_name(struct libinput_device *device)
3060 {
3061 	return evdev_device_get_name((struct evdev_device *) device);
3062 }
3063 
3064 LIBINPUT_EXPORT unsigned int
libinput_device_get_id_product(struct libinput_device * device)3065 libinput_device_get_id_product(struct libinput_device *device)
3066 {
3067 	return evdev_device_get_id_product((struct evdev_device *) device);
3068 }
3069 
3070 LIBINPUT_EXPORT unsigned int
libinput_device_get_id_vendor(struct libinput_device * device)3071 libinput_device_get_id_vendor(struct libinput_device *device)
3072 {
3073 	return evdev_device_get_id_vendor((struct evdev_device *) device);
3074 }
3075 
3076 LIBINPUT_EXPORT const char *
libinput_device_get_output_name(struct libinput_device * device)3077 libinput_device_get_output_name(struct libinput_device *device)
3078 {
3079 	return evdev_device_get_output((struct evdev_device *) device);
3080 }
3081 
3082 LIBINPUT_EXPORT struct libinput_seat *
libinput_device_get_seat(struct libinput_device * device)3083 libinput_device_get_seat(struct libinput_device *device)
3084 {
3085 	return device->seat;
3086 }
3087 
3088 LIBINPUT_EXPORT int
libinput_device_set_seat_logical_name(struct libinput_device * device,const char * name)3089 libinput_device_set_seat_logical_name(struct libinput_device *device,
3090 				      const char *name)
3091 {
3092 	struct libinput *libinput = device->seat->libinput;
3093 
3094 	if (name == NULL)
3095 		return -1;
3096 
3097 	return libinput->interface_backend->device_change_seat(device,
3098 							       name);
3099 }
3100 
3101 LIBINPUT_EXPORT struct udev_device *
libinput_device_get_udev_device(struct libinput_device * device)3102 libinput_device_get_udev_device(struct libinput_device *device)
3103 {
3104 	return evdev_device_get_udev_device((struct evdev_device *)device);
3105 }
3106 
3107 LIBINPUT_EXPORT void
libinput_device_led_update(struct libinput_device * device,enum libinput_led leds)3108 libinput_device_led_update(struct libinput_device *device,
3109 			   enum libinput_led leds)
3110 {
3111 	evdev_device_led_update((struct evdev_device *) device, leds);
3112 }
3113 
3114 LIBINPUT_EXPORT int
libinput_device_has_capability(struct libinput_device * device,enum libinput_device_capability capability)3115 libinput_device_has_capability(struct libinput_device *device,
3116 			       enum libinput_device_capability capability)
3117 {
3118 	return evdev_device_has_capability((struct evdev_device *) device,
3119 					   capability);
3120 }
3121 
3122 LIBINPUT_EXPORT int
libinput_device_get_size(struct libinput_device * device,double * width,double * height)3123 libinput_device_get_size(struct libinput_device *device,
3124 			 double *width,
3125 			 double *height)
3126 {
3127 	return evdev_device_get_size((struct evdev_device *)device,
3128 				     width,
3129 				     height);
3130 }
3131 
3132 LIBINPUT_EXPORT int
libinput_device_pointer_has_button(struct libinput_device * device,uint32_t code)3133 libinput_device_pointer_has_button(struct libinput_device *device, uint32_t code)
3134 {
3135 	return evdev_device_has_button((struct evdev_device *)device, code);
3136 }
3137 
3138 LIBINPUT_EXPORT int
libinput_device_keyboard_has_key(struct libinput_device * device,uint32_t code)3139 libinput_device_keyboard_has_key(struct libinput_device *device, uint32_t code)
3140 {
3141 	return evdev_device_has_key((struct evdev_device *)device, code);
3142 }
3143 
3144 LIBINPUT_EXPORT int
libinput_device_touch_get_touch_count(struct libinput_device * device)3145 libinput_device_touch_get_touch_count(struct libinput_device *device)
3146 {
3147 	return evdev_device_get_touch_count((struct evdev_device *)device);
3148 }
3149 
3150 LIBINPUT_EXPORT int
libinput_device_switch_has_switch(struct libinput_device * device,enum libinput_switch sw)3151 libinput_device_switch_has_switch(struct libinput_device *device,
3152 				  enum libinput_switch sw)
3153 {
3154 	return evdev_device_has_switch((struct evdev_device *)device, sw);
3155 }
3156 
3157 LIBINPUT_EXPORT int
libinput_device_tablet_pad_has_key(struct libinput_device * device,uint32_t code)3158 libinput_device_tablet_pad_has_key(struct libinput_device *device, uint32_t code)
3159 {
3160 	return evdev_device_tablet_pad_has_key((struct evdev_device *)device,
3161 					       code);
3162 }
3163 
3164 LIBINPUT_EXPORT int
libinput_device_tablet_pad_get_num_buttons(struct libinput_device * device)3165 libinput_device_tablet_pad_get_num_buttons(struct libinput_device *device)
3166 {
3167 	return evdev_device_tablet_pad_get_num_buttons((struct evdev_device *)device);
3168 }
3169 
3170 LIBINPUT_EXPORT int
libinput_device_tablet_pad_get_num_rings(struct libinput_device * device)3171 libinput_device_tablet_pad_get_num_rings(struct libinput_device *device)
3172 {
3173 	return evdev_device_tablet_pad_get_num_rings((struct evdev_device *)device);
3174 }
3175 
3176 LIBINPUT_EXPORT int
libinput_device_tablet_pad_get_num_strips(struct libinput_device * device)3177 libinput_device_tablet_pad_get_num_strips(struct libinput_device *device)
3178 {
3179 	return evdev_device_tablet_pad_get_num_strips((struct evdev_device *)device);
3180 }
3181 
3182 LIBINPUT_EXPORT int
libinput_device_tablet_pad_get_num_mode_groups(struct libinput_device * device)3183 libinput_device_tablet_pad_get_num_mode_groups(struct libinput_device *device)
3184 {
3185 	return evdev_device_tablet_pad_get_num_mode_groups((struct evdev_device *)device);
3186 }
3187 
3188 LIBINPUT_EXPORT struct libinput_tablet_pad_mode_group*
libinput_device_tablet_pad_get_mode_group(struct libinput_device * device,unsigned int index)3189 libinput_device_tablet_pad_get_mode_group(struct libinput_device *device,
3190 					  unsigned int index)
3191 {
3192 	return evdev_device_tablet_pad_get_mode_group((struct evdev_device *)device,
3193 						      index);
3194 }
3195 
3196 LIBINPUT_EXPORT unsigned int
libinput_tablet_pad_mode_group_get_num_modes(struct libinput_tablet_pad_mode_group * group)3197 libinput_tablet_pad_mode_group_get_num_modes(
3198 				     struct libinput_tablet_pad_mode_group *group)
3199 {
3200 	return group->num_modes;
3201 }
3202 
3203 LIBINPUT_EXPORT unsigned int
libinput_tablet_pad_mode_group_get_mode(struct libinput_tablet_pad_mode_group * group)3204 libinput_tablet_pad_mode_group_get_mode(struct libinput_tablet_pad_mode_group *group)
3205 {
3206 	return group->current_mode;
3207 }
3208 
3209 LIBINPUT_EXPORT unsigned int
libinput_tablet_pad_mode_group_get_index(struct libinput_tablet_pad_mode_group * group)3210 libinput_tablet_pad_mode_group_get_index(struct libinput_tablet_pad_mode_group *group)
3211 {
3212 	return group->index;
3213 }
3214 
3215 LIBINPUT_EXPORT int
libinput_tablet_pad_mode_group_has_button(struct libinput_tablet_pad_mode_group * group,unsigned int button)3216 libinput_tablet_pad_mode_group_has_button(struct libinput_tablet_pad_mode_group *group,
3217 					  unsigned int button)
3218 {
3219 	if ((int)button >=
3220 	    libinput_device_tablet_pad_get_num_buttons(group->device))
3221 		return 0;
3222 
3223 	return !!(group->button_mask & (1 << button));
3224 }
3225 
3226 LIBINPUT_EXPORT int
libinput_tablet_pad_mode_group_has_ring(struct libinput_tablet_pad_mode_group * group,unsigned int ring)3227 libinput_tablet_pad_mode_group_has_ring(struct libinput_tablet_pad_mode_group *group,
3228 					unsigned int ring)
3229 {
3230 	if ((int)ring >=
3231 	    libinput_device_tablet_pad_get_num_rings(group->device))
3232 		return 0;
3233 
3234 	return !!(group->ring_mask & (1 << ring));
3235 }
3236 
3237 LIBINPUT_EXPORT int
libinput_tablet_pad_mode_group_has_strip(struct libinput_tablet_pad_mode_group * group,unsigned int strip)3238 libinput_tablet_pad_mode_group_has_strip(struct libinput_tablet_pad_mode_group *group,
3239 					 unsigned int strip)
3240 {
3241 	if ((int)strip >=
3242 	    libinput_device_tablet_pad_get_num_strips(group->device))
3243 		return 0;
3244 
3245 	return !!(group->strip_mask & (1 << strip));
3246 }
3247 
3248 LIBINPUT_EXPORT int
libinput_tablet_pad_mode_group_button_is_toggle(struct libinput_tablet_pad_mode_group * group,unsigned int button)3249 libinput_tablet_pad_mode_group_button_is_toggle(struct libinput_tablet_pad_mode_group *group,
3250 						unsigned int button)
3251 {
3252 	if ((int)button >=
3253 	    libinput_device_tablet_pad_get_num_buttons(group->device))
3254 		return 0;
3255 
3256 	return !!(group->toggle_button_mask & (1 << button));
3257 }
3258 
3259 LIBINPUT_EXPORT struct libinput_tablet_pad_mode_group *
libinput_tablet_pad_mode_group_ref(struct libinput_tablet_pad_mode_group * group)3260 libinput_tablet_pad_mode_group_ref(
3261 			struct libinput_tablet_pad_mode_group *group)
3262 {
3263 	group->refcount++;
3264 	return group;
3265 }
3266 
3267 LIBINPUT_EXPORT struct libinput_tablet_pad_mode_group *
libinput_tablet_pad_mode_group_unref(struct libinput_tablet_pad_mode_group * group)3268 libinput_tablet_pad_mode_group_unref(
3269 			struct libinput_tablet_pad_mode_group *group)
3270 {
3271 	assert(group->refcount > 0);
3272 
3273 	group->refcount--;
3274 	if (group->refcount > 0)
3275 		return group;
3276 
3277 	list_remove(&group->link);
3278 	group->destroy(group);
3279 	return NULL;
3280 }
3281 
3282 LIBINPUT_EXPORT void
libinput_tablet_pad_mode_group_set_user_data(struct libinput_tablet_pad_mode_group * group,void * user_data)3283 libinput_tablet_pad_mode_group_set_user_data(
3284 			struct libinput_tablet_pad_mode_group *group,
3285 			void *user_data)
3286 {
3287 	group->user_data = user_data;
3288 }
3289 
3290 LIBINPUT_EXPORT void *
libinput_tablet_pad_mode_group_get_user_data(struct libinput_tablet_pad_mode_group * group)3291 libinput_tablet_pad_mode_group_get_user_data(
3292 			struct libinput_tablet_pad_mode_group *group)
3293 {
3294 	return group->user_data;
3295 }
3296 
3297 LIBINPUT_EXPORT struct libinput_event *
libinput_event_device_notify_get_base_event(struct libinput_event_device_notify * event)3298 libinput_event_device_notify_get_base_event(struct libinput_event_device_notify *event)
3299 {
3300 	require_event_type(libinput_event_get_context(&event->base),
3301 			   event->base.type,
3302 			   NULL,
3303 			   LIBINPUT_EVENT_DEVICE_ADDED,
3304 			   LIBINPUT_EVENT_DEVICE_REMOVED);
3305 
3306 	return &event->base;
3307 }
3308 
3309 LIBINPUT_EXPORT struct libinput_event *
libinput_event_keyboard_get_base_event(struct libinput_event_keyboard * event)3310 libinput_event_keyboard_get_base_event(struct libinput_event_keyboard *event)
3311 {
3312 	require_event_type(libinput_event_get_context(&event->base),
3313 			   event->base.type,
3314 			   NULL,
3315 			   LIBINPUT_EVENT_KEYBOARD_KEY);
3316 
3317 	return &event->base;
3318 }
3319 
3320 LIBINPUT_EXPORT struct libinput_event *
libinput_event_pointer_get_base_event(struct libinput_event_pointer * event)3321 libinput_event_pointer_get_base_event(struct libinput_event_pointer *event)
3322 {
3323 	require_event_type(libinput_event_get_context(&event->base),
3324 			   event->base.type,
3325 			   NULL,
3326 			   LIBINPUT_EVENT_POINTER_MOTION,
3327 			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
3328 			   LIBINPUT_EVENT_POINTER_BUTTON,
3329 			   LIBINPUT_EVENT_POINTER_AXIS);
3330 
3331 	return &event->base;
3332 }
3333 
3334 LIBINPUT_EXPORT struct libinput_event *
libinput_event_touch_get_base_event(struct libinput_event_touch * event)3335 libinput_event_touch_get_base_event(struct libinput_event_touch *event)
3336 {
3337 	require_event_type(libinput_event_get_context(&event->base),
3338 			   event->base.type,
3339 			   NULL,
3340 			   LIBINPUT_EVENT_TOUCH_DOWN,
3341 			   LIBINPUT_EVENT_TOUCH_UP,
3342 			   LIBINPUT_EVENT_TOUCH_MOTION,
3343 			   LIBINPUT_EVENT_TOUCH_CANCEL,
3344 			   LIBINPUT_EVENT_TOUCH_FRAME);
3345 
3346 	return &event->base;
3347 }
3348 
3349 LIBINPUT_EXPORT struct libinput_event *
libinput_event_gesture_get_base_event(struct libinput_event_gesture * event)3350 libinput_event_gesture_get_base_event(struct libinput_event_gesture *event)
3351 {
3352 	require_event_type(libinput_event_get_context(&event->base),
3353 			   event->base.type,
3354 			   NULL,
3355 			   LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
3356 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
3357 			   LIBINPUT_EVENT_GESTURE_SWIPE_END,
3358 			   LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
3359 			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
3360 			   LIBINPUT_EVENT_GESTURE_PINCH_END);
3361 
3362 	return &event->base;
3363 }
3364 
3365 LIBINPUT_EXPORT struct libinput_event *
libinput_event_tablet_tool_get_base_event(struct libinput_event_tablet_tool * event)3366 libinput_event_tablet_tool_get_base_event(struct libinput_event_tablet_tool *event)
3367 {
3368 	require_event_type(libinput_event_get_context(&event->base),
3369 			   event->base.type,
3370 			   NULL,
3371 			   LIBINPUT_EVENT_TABLET_TOOL_AXIS,
3372 			   LIBINPUT_EVENT_TABLET_TOOL_TIP,
3373 			   LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY,
3374 			   LIBINPUT_EVENT_TABLET_TOOL_BUTTON);
3375 
3376 	return &event->base;
3377 }
3378 
3379 LIBINPUT_EXPORT double
libinput_event_tablet_pad_get_ring_position(struct libinput_event_tablet_pad * event)3380 libinput_event_tablet_pad_get_ring_position(struct libinput_event_tablet_pad *event)
3381 {
3382 	require_event_type(libinput_event_get_context(&event->base),
3383 			   event->base.type,
3384 			   0.0,
3385 			   LIBINPUT_EVENT_TABLET_PAD_RING);
3386 
3387 	return event->ring.position;
3388 }
3389 
3390 LIBINPUT_EXPORT unsigned int
libinput_event_tablet_pad_get_ring_number(struct libinput_event_tablet_pad * event)3391 libinput_event_tablet_pad_get_ring_number(struct libinput_event_tablet_pad *event)
3392 {
3393 	require_event_type(libinput_event_get_context(&event->base),
3394 			   event->base.type,
3395 			   0,
3396 			   LIBINPUT_EVENT_TABLET_PAD_RING);
3397 
3398 	return event->ring.number;
3399 }
3400 
3401 LIBINPUT_EXPORT enum libinput_tablet_pad_ring_axis_source
libinput_event_tablet_pad_get_ring_source(struct libinput_event_tablet_pad * event)3402 libinput_event_tablet_pad_get_ring_source(struct libinput_event_tablet_pad *event)
3403 {
3404 	require_event_type(libinput_event_get_context(&event->base),
3405 			   event->base.type,
3406 			   LIBINPUT_TABLET_PAD_RING_SOURCE_UNKNOWN,
3407 			   LIBINPUT_EVENT_TABLET_PAD_RING);
3408 
3409 	return event->ring.source;
3410 }
3411 
3412 LIBINPUT_EXPORT double
libinput_event_tablet_pad_get_strip_position(struct libinput_event_tablet_pad * event)3413 libinput_event_tablet_pad_get_strip_position(struct libinput_event_tablet_pad *event)
3414 {
3415 	require_event_type(libinput_event_get_context(&event->base),
3416 			   event->base.type,
3417 			   0.0,
3418 			   LIBINPUT_EVENT_TABLET_PAD_STRIP);
3419 
3420 	return event->strip.position;
3421 }
3422 
3423 LIBINPUT_EXPORT unsigned int
libinput_event_tablet_pad_get_strip_number(struct libinput_event_tablet_pad * event)3424 libinput_event_tablet_pad_get_strip_number(struct libinput_event_tablet_pad *event)
3425 {
3426 	require_event_type(libinput_event_get_context(&event->base),
3427 			   event->base.type,
3428 			   0,
3429 			   LIBINPUT_EVENT_TABLET_PAD_STRIP);
3430 
3431 	return event->strip.number;
3432 }
3433 
3434 LIBINPUT_EXPORT enum libinput_tablet_pad_strip_axis_source
libinput_event_tablet_pad_get_strip_source(struct libinput_event_tablet_pad * event)3435 libinput_event_tablet_pad_get_strip_source(struct libinput_event_tablet_pad *event)
3436 {
3437 	require_event_type(libinput_event_get_context(&event->base),
3438 			   event->base.type,
3439 			   LIBINPUT_TABLET_PAD_STRIP_SOURCE_UNKNOWN,
3440 			   LIBINPUT_EVENT_TABLET_PAD_STRIP);
3441 
3442 	return event->strip.source;
3443 }
3444 
3445 LIBINPUT_EXPORT uint32_t
libinput_event_tablet_pad_get_button_number(struct libinput_event_tablet_pad * event)3446 libinput_event_tablet_pad_get_button_number(struct libinput_event_tablet_pad *event)
3447 {
3448 	require_event_type(libinput_event_get_context(&event->base),
3449 			   event->base.type,
3450 			   0,
3451 			   LIBINPUT_EVENT_TABLET_PAD_BUTTON);
3452 
3453 	return event->button.number;
3454 }
3455 
3456 LIBINPUT_EXPORT enum libinput_button_state
libinput_event_tablet_pad_get_button_state(struct libinput_event_tablet_pad * event)3457 libinput_event_tablet_pad_get_button_state(struct libinput_event_tablet_pad *event)
3458 {
3459 	require_event_type(libinput_event_get_context(&event->base),
3460 			   event->base.type,
3461 			   LIBINPUT_BUTTON_STATE_RELEASED,
3462 			   LIBINPUT_EVENT_TABLET_PAD_BUTTON);
3463 
3464 	return event->button.state;
3465 }
3466 
3467 LIBINPUT_EXPORT uint32_t
libinput_event_tablet_pad_get_key(struct libinput_event_tablet_pad * event)3468 libinput_event_tablet_pad_get_key(struct libinput_event_tablet_pad *event)
3469 {
3470 	require_event_type(libinput_event_get_context(&event->base),
3471 			   event->base.type,
3472 			   0,
3473 			   LIBINPUT_EVENT_TABLET_PAD_KEY);
3474 
3475 	return event->key.code;
3476 }
3477 
3478 LIBINPUT_EXPORT enum libinput_key_state
libinput_event_tablet_pad_get_key_state(struct libinput_event_tablet_pad * event)3479 libinput_event_tablet_pad_get_key_state(struct libinput_event_tablet_pad *event)
3480 {
3481 	require_event_type(libinput_event_get_context(&event->base),
3482 			   event->base.type,
3483 			   LIBINPUT_KEY_STATE_RELEASED,
3484 			   LIBINPUT_EVENT_TABLET_PAD_KEY);
3485 
3486 	return event->key.state;
3487 }
3488 
3489 LIBINPUT_EXPORT unsigned int
libinput_event_tablet_pad_get_mode(struct libinput_event_tablet_pad * event)3490 libinput_event_tablet_pad_get_mode(struct libinput_event_tablet_pad *event)
3491 {
3492 	require_event_type(libinput_event_get_context(&event->base),
3493 			   event->base.type,
3494 			   0,
3495 			   LIBINPUT_EVENT_TABLET_PAD_RING,
3496 			   LIBINPUT_EVENT_TABLET_PAD_STRIP,
3497 			   LIBINPUT_EVENT_TABLET_PAD_BUTTON);
3498 
3499 	return event->mode;
3500 }
3501 
3502 LIBINPUT_EXPORT struct libinput_tablet_pad_mode_group *
libinput_event_tablet_pad_get_mode_group(struct libinput_event_tablet_pad * event)3503 libinput_event_tablet_pad_get_mode_group(struct libinput_event_tablet_pad *event)
3504 {
3505 	require_event_type(libinput_event_get_context(&event->base),
3506 			   event->base.type,
3507 			   NULL,
3508 			   LIBINPUT_EVENT_TABLET_PAD_RING,
3509 			   LIBINPUT_EVENT_TABLET_PAD_STRIP,
3510 			   LIBINPUT_EVENT_TABLET_PAD_BUTTON);
3511 
3512 	return event->mode_group;
3513 }
3514 
3515 LIBINPUT_EXPORT uint32_t
libinput_event_tablet_pad_get_time(struct libinput_event_tablet_pad * event)3516 libinput_event_tablet_pad_get_time(struct libinput_event_tablet_pad *event)
3517 {
3518 	require_event_type(libinput_event_get_context(&event->base),
3519 			   event->base.type,
3520 			   0,
3521 			   LIBINPUT_EVENT_TABLET_PAD_RING,
3522 			   LIBINPUT_EVENT_TABLET_PAD_STRIP,
3523 			   LIBINPUT_EVENT_TABLET_PAD_BUTTON,
3524 			   LIBINPUT_EVENT_TABLET_PAD_KEY);
3525 
3526 	return us2ms(event->time);
3527 }
3528 
3529 LIBINPUT_EXPORT uint64_t
libinput_event_tablet_pad_get_time_usec(struct libinput_event_tablet_pad * event)3530 libinput_event_tablet_pad_get_time_usec(struct libinput_event_tablet_pad *event)
3531 {
3532 	require_event_type(libinput_event_get_context(&event->base),
3533 			   event->base.type,
3534 			   0,
3535 			   LIBINPUT_EVENT_TABLET_PAD_RING,
3536 			   LIBINPUT_EVENT_TABLET_PAD_STRIP,
3537 			   LIBINPUT_EVENT_TABLET_PAD_BUTTON,
3538 			   LIBINPUT_EVENT_TABLET_PAD_KEY);
3539 
3540 	return event->time;
3541 }
3542 
3543 LIBINPUT_EXPORT struct libinput_event *
libinput_event_tablet_pad_get_base_event(struct libinput_event_tablet_pad * event)3544 libinput_event_tablet_pad_get_base_event(struct libinput_event_tablet_pad *event)
3545 {
3546 	require_event_type(libinput_event_get_context(&event->base),
3547 			   event->base.type,
3548 			   NULL,
3549 			   LIBINPUT_EVENT_TABLET_PAD_RING,
3550 			   LIBINPUT_EVENT_TABLET_PAD_STRIP,
3551 			   LIBINPUT_EVENT_TABLET_PAD_BUTTON,
3552 			   LIBINPUT_EVENT_TABLET_PAD_KEY);
3553 
3554 	return &event->base;
3555 }
3556 
3557 LIBINPUT_EXPORT struct libinput_device_group *
libinput_device_group_ref(struct libinput_device_group * group)3558 libinput_device_group_ref(struct libinput_device_group *group)
3559 {
3560 	group->refcount++;
3561 	return group;
3562 }
3563 
3564 struct libinput_device_group *
libinput_device_group_create(struct libinput * libinput,const char * identifier)3565 libinput_device_group_create(struct libinput *libinput,
3566 			     const char *identifier)
3567 {
3568 	struct libinput_device_group *group;
3569 
3570 	group = zalloc(sizeof *group);
3571 	group->refcount = 1;
3572 	group->identifier = safe_strdup(identifier);
3573 
3574 	list_init(&group->link);
3575 	list_insert(&libinput->device_group_list, &group->link);
3576 
3577 	return group;
3578 }
3579 
3580 struct libinput_device_group *
libinput_device_group_find_group(struct libinput * libinput,const char * identifier)3581 libinput_device_group_find_group(struct libinput *libinput,
3582 				 const char *identifier)
3583 {
3584 	struct libinput_device_group *g = NULL;
3585 
3586 	list_for_each(g, &libinput->device_group_list, link) {
3587 		if (identifier && g->identifier &&
3588 		    streq(g->identifier, identifier)) {
3589 			return g;
3590 		}
3591 	}
3592 
3593 	return NULL;
3594 }
3595 
3596 void
libinput_device_set_device_group(struct libinput_device * device,struct libinput_device_group * group)3597 libinput_device_set_device_group(struct libinput_device *device,
3598 				 struct libinput_device_group *group)
3599 {
3600 	device->group = group;
3601 	libinput_device_group_ref(group);
3602 }
3603 
3604 static void
libinput_device_group_destroy(struct libinput_device_group * group)3605 libinput_device_group_destroy(struct libinput_device_group *group)
3606 {
3607 	list_remove(&group->link);
3608 	free(group->identifier);
3609 	free(group);
3610 }
3611 
3612 LIBINPUT_EXPORT struct libinput_device_group *
libinput_device_group_unref(struct libinput_device_group * group)3613 libinput_device_group_unref(struct libinput_device_group *group)
3614 {
3615 	assert(group->refcount > 0);
3616 	group->refcount--;
3617 	if (group->refcount == 0) {
3618 		libinput_device_group_destroy(group);
3619 		return NULL;
3620 	} else {
3621 		return group;
3622 	}
3623 }
3624 
3625 LIBINPUT_EXPORT void
libinput_device_group_set_user_data(struct libinput_device_group * group,void * user_data)3626 libinput_device_group_set_user_data(struct libinput_device_group *group,
3627 				    void *user_data)
3628 {
3629 	group->user_data = user_data;
3630 }
3631 
3632 LIBINPUT_EXPORT void *
libinput_device_group_get_user_data(struct libinput_device_group * group)3633 libinput_device_group_get_user_data(struct libinput_device_group *group)
3634 {
3635 	return group->user_data;
3636 }
3637 
3638 LIBINPUT_EXPORT const char *
libinput_config_status_to_str(enum libinput_config_status status)3639 libinput_config_status_to_str(enum libinput_config_status status)
3640 {
3641 	const char *str = NULL;
3642 
3643 	switch(status) {
3644 	case LIBINPUT_CONFIG_STATUS_SUCCESS:
3645 		str = "Success";
3646 		break;
3647 	case LIBINPUT_CONFIG_STATUS_UNSUPPORTED:
3648 		str = "Unsupported configuration option";
3649 		break;
3650 	case LIBINPUT_CONFIG_STATUS_INVALID:
3651 		str = "Invalid argument range";
3652 		break;
3653 	}
3654 
3655 	return str;
3656 }
3657 
3658 LIBINPUT_EXPORT int
libinput_device_config_tap_get_finger_count(struct libinput_device * device)3659 libinput_device_config_tap_get_finger_count(struct libinput_device *device)
3660 {
3661 	return device->config.tap ? device->config.tap->count(device) : 0;
3662 }
3663 
3664 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_tap_set_enabled(struct libinput_device * device,enum libinput_config_tap_state enable)3665 libinput_device_config_tap_set_enabled(struct libinput_device *device,
3666 				       enum libinput_config_tap_state enable)
3667 {
3668 	if (enable != LIBINPUT_CONFIG_TAP_ENABLED &&
3669 	    enable != LIBINPUT_CONFIG_TAP_DISABLED)
3670 		return LIBINPUT_CONFIG_STATUS_INVALID;
3671 
3672 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3673 		return enable ? LIBINPUT_CONFIG_STATUS_UNSUPPORTED :
3674 				LIBINPUT_CONFIG_STATUS_SUCCESS;
3675 
3676 	return device->config.tap->set_enabled(device, enable);
3677 
3678 }
3679 
3680 LIBINPUT_EXPORT enum libinput_config_tap_state
libinput_device_config_tap_get_enabled(struct libinput_device * device)3681 libinput_device_config_tap_get_enabled(struct libinput_device *device)
3682 {
3683 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3684 		return LIBINPUT_CONFIG_TAP_DISABLED;
3685 
3686 	return device->config.tap->get_enabled(device);
3687 }
3688 
3689 LIBINPUT_EXPORT enum libinput_config_tap_state
libinput_device_config_tap_get_default_enabled(struct libinput_device * device)3690 libinput_device_config_tap_get_default_enabled(struct libinput_device *device)
3691 {
3692 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3693 		return LIBINPUT_CONFIG_TAP_DISABLED;
3694 
3695 	return device->config.tap->get_default(device);
3696 }
3697 
3698 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_tap_set_button_map(struct libinput_device * device,enum libinput_config_tap_button_map map)3699 libinput_device_config_tap_set_button_map(struct libinput_device *device,
3700 					    enum libinput_config_tap_button_map map)
3701 {
3702 	switch (map) {
3703 	case LIBINPUT_CONFIG_TAP_MAP_LRM:
3704 	case LIBINPUT_CONFIG_TAP_MAP_LMR:
3705 		break;
3706 	default:
3707 		return LIBINPUT_CONFIG_STATUS_INVALID;
3708 	}
3709 
3710 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3711 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
3712 
3713 	return device->config.tap->set_map(device, map);
3714 }
3715 
3716 LIBINPUT_EXPORT enum libinput_config_tap_button_map
libinput_device_config_tap_get_button_map(struct libinput_device * device)3717 libinput_device_config_tap_get_button_map(struct libinput_device *device)
3718 {
3719 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3720 		return LIBINPUT_CONFIG_TAP_MAP_LRM;
3721 
3722 	return device->config.tap->get_map(device);
3723 }
3724 
3725 LIBINPUT_EXPORT enum libinput_config_tap_button_map
libinput_device_config_tap_get_default_button_map(struct libinput_device * device)3726 libinput_device_config_tap_get_default_button_map(struct libinput_device *device)
3727 {
3728 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3729 		return LIBINPUT_CONFIG_TAP_MAP_LRM;
3730 
3731 	return device->config.tap->get_default_map(device);
3732 }
3733 
3734 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_tap_set_drag_enabled(struct libinput_device * device,enum libinput_config_drag_state enable)3735 libinput_device_config_tap_set_drag_enabled(struct libinput_device *device,
3736 					    enum libinput_config_drag_state enable)
3737 {
3738 	if (enable != LIBINPUT_CONFIG_DRAG_ENABLED &&
3739 	    enable != LIBINPUT_CONFIG_DRAG_DISABLED)
3740 		return LIBINPUT_CONFIG_STATUS_INVALID;
3741 
3742 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3743 		return enable ? LIBINPUT_CONFIG_STATUS_UNSUPPORTED :
3744 				LIBINPUT_CONFIG_STATUS_SUCCESS;
3745 
3746 	return device->config.tap->set_drag_enabled(device, enable);
3747 }
3748 
3749 LIBINPUT_EXPORT enum libinput_config_drag_state
libinput_device_config_tap_get_drag_enabled(struct libinput_device * device)3750 libinput_device_config_tap_get_drag_enabled(struct libinput_device *device)
3751 {
3752 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3753 		return LIBINPUT_CONFIG_DRAG_DISABLED;
3754 
3755 	return device->config.tap->get_drag_enabled(device);
3756 }
3757 
3758 LIBINPUT_EXPORT enum libinput_config_drag_state
libinput_device_config_tap_get_default_drag_enabled(struct libinput_device * device)3759 libinput_device_config_tap_get_default_drag_enabled(struct libinput_device *device)
3760 {
3761 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3762 		return LIBINPUT_CONFIG_DRAG_DISABLED;
3763 
3764 	return device->config.tap->get_default_drag_enabled(device);
3765 }
3766 
3767 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_tap_set_drag_lock_enabled(struct libinput_device * device,enum libinput_config_drag_lock_state enable)3768 libinput_device_config_tap_set_drag_lock_enabled(struct libinput_device *device,
3769 						 enum libinput_config_drag_lock_state enable)
3770 {
3771 	if (enable != LIBINPUT_CONFIG_DRAG_LOCK_ENABLED &&
3772 	    enable != LIBINPUT_CONFIG_DRAG_LOCK_DISABLED)
3773 		return LIBINPUT_CONFIG_STATUS_INVALID;
3774 
3775 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3776 		return enable ? LIBINPUT_CONFIG_STATUS_UNSUPPORTED :
3777 				LIBINPUT_CONFIG_STATUS_SUCCESS;
3778 
3779 	return device->config.tap->set_draglock_enabled(device, enable);
3780 }
3781 
3782 LIBINPUT_EXPORT enum libinput_config_drag_lock_state
libinput_device_config_tap_get_drag_lock_enabled(struct libinput_device * device)3783 libinput_device_config_tap_get_drag_lock_enabled(struct libinput_device *device)
3784 {
3785 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3786 		return LIBINPUT_CONFIG_DRAG_LOCK_DISABLED;
3787 
3788 	return device->config.tap->get_draglock_enabled(device);
3789 }
3790 
3791 LIBINPUT_EXPORT enum libinput_config_drag_lock_state
libinput_device_config_tap_get_default_drag_lock_enabled(struct libinput_device * device)3792 libinput_device_config_tap_get_default_drag_lock_enabled(struct libinput_device *device)
3793 {
3794 	if (libinput_device_config_tap_get_finger_count(device) == 0)
3795 		return LIBINPUT_CONFIG_DRAG_LOCK_DISABLED;
3796 
3797 	return device->config.tap->get_default_draglock_enabled(device);
3798 }
3799 
3800 LIBINPUT_EXPORT int
libinput_device_config_calibration_has_matrix(struct libinput_device * device)3801 libinput_device_config_calibration_has_matrix(struct libinput_device *device)
3802 {
3803 	return device->config.calibration ?
3804 		device->config.calibration->has_matrix(device) : 0;
3805 }
3806 
3807 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_calibration_set_matrix(struct libinput_device * device,const float matrix[6])3808 libinput_device_config_calibration_set_matrix(struct libinput_device *device,
3809 					      const float matrix[6])
3810 {
3811 	if (!libinput_device_config_calibration_has_matrix(device))
3812 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
3813 
3814 	return device->config.calibration->set_matrix(device, matrix);
3815 }
3816 
3817 LIBINPUT_EXPORT int
libinput_device_config_calibration_get_matrix(struct libinput_device * device,float matrix[6])3818 libinput_device_config_calibration_get_matrix(struct libinput_device *device,
3819 					      float matrix[6])
3820 {
3821 	if (!libinput_device_config_calibration_has_matrix(device))
3822 		return 0;
3823 
3824 	return device->config.calibration->get_matrix(device, matrix);
3825 }
3826 
3827 LIBINPUT_EXPORT int
libinput_device_config_calibration_get_default_matrix(struct libinput_device * device,float matrix[6])3828 libinput_device_config_calibration_get_default_matrix(struct libinput_device *device,
3829 						      float matrix[6])
3830 {
3831 	if (!libinput_device_config_calibration_has_matrix(device))
3832 		return 0;
3833 
3834 	return device->config.calibration->get_default_matrix(device, matrix);
3835 }
3836 
3837 LIBINPUT_EXPORT uint32_t
libinput_device_config_send_events_get_modes(struct libinput_device * device)3838 libinput_device_config_send_events_get_modes(struct libinput_device *device)
3839 {
3840 	uint32_t modes = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
3841 
3842 	if (device->config.sendevents)
3843 		modes |= device->config.sendevents->get_modes(device);
3844 
3845 	return modes;
3846 }
3847 
3848 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_send_events_set_mode(struct libinput_device * device,uint32_t mode)3849 libinput_device_config_send_events_set_mode(struct libinput_device *device,
3850 					    uint32_t mode)
3851 {
3852 	if ((libinput_device_config_send_events_get_modes(device) & mode) != mode)
3853 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
3854 
3855 	if (device->config.sendevents)
3856 		return device->config.sendevents->set_mode(device, mode);
3857 	else /* mode must be _ENABLED to get here */
3858 		return LIBINPUT_CONFIG_STATUS_SUCCESS;
3859 }
3860 
3861 LIBINPUT_EXPORT uint32_t
libinput_device_config_send_events_get_mode(struct libinput_device * device)3862 libinput_device_config_send_events_get_mode(struct libinput_device *device)
3863 {
3864 	if (device->config.sendevents)
3865 		return device->config.sendevents->get_mode(device);
3866 	else
3867 		return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
3868 }
3869 
3870 LIBINPUT_EXPORT uint32_t
libinput_device_config_send_events_get_default_mode(struct libinput_device * device)3871 libinput_device_config_send_events_get_default_mode(struct libinput_device *device)
3872 {
3873 	return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
3874 }
3875 
3876 LIBINPUT_EXPORT int
libinput_device_config_accel_is_available(struct libinput_device * device)3877 libinput_device_config_accel_is_available(struct libinput_device *device)
3878 {
3879 	return device->config.accel ?
3880 		device->config.accel->available(device) : 0;
3881 }
3882 
3883 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_accel_set_speed(struct libinput_device * device,double speed)3884 libinput_device_config_accel_set_speed(struct libinput_device *device,
3885 				       double speed)
3886 {
3887 	/* Need the negation in case speed is NaN */
3888 	if (!(speed >= -1.0 && speed <= 1.0))
3889 		return LIBINPUT_CONFIG_STATUS_INVALID;
3890 
3891 	if (!libinput_device_config_accel_is_available(device))
3892 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
3893 
3894 	return device->config.accel->set_speed(device, speed);
3895 }
3896 LIBINPUT_EXPORT double
libinput_device_config_accel_get_speed(struct libinput_device * device)3897 libinput_device_config_accel_get_speed(struct libinput_device *device)
3898 {
3899 	if (!libinput_device_config_accel_is_available(device))
3900 		return 0;
3901 
3902 	return device->config.accel->get_speed(device);
3903 }
3904 
3905 LIBINPUT_EXPORT double
libinput_device_config_accel_get_default_speed(struct libinput_device * device)3906 libinput_device_config_accel_get_default_speed(struct libinput_device *device)
3907 {
3908 	if (!libinput_device_config_accel_is_available(device))
3909 		return 0;
3910 
3911 	return device->config.accel->get_default_speed(device);
3912 }
3913 
3914 LIBINPUT_EXPORT uint32_t
libinput_device_config_accel_get_profiles(struct libinput_device * device)3915 libinput_device_config_accel_get_profiles(struct libinput_device *device)
3916 {
3917 	if (!libinput_device_config_accel_is_available(device))
3918 		return 0;
3919 
3920 	return device->config.accel->get_profiles(device);
3921 }
3922 
3923 LIBINPUT_EXPORT enum libinput_config_accel_profile
libinput_device_config_accel_get_profile(struct libinput_device * device)3924 libinput_device_config_accel_get_profile(struct libinput_device *device)
3925 {
3926 	if (!libinput_device_config_accel_is_available(device))
3927 		return LIBINPUT_CONFIG_ACCEL_PROFILE_NONE;
3928 
3929 	return device->config.accel->get_profile(device);
3930 }
3931 
3932 LIBINPUT_EXPORT enum libinput_config_accel_profile
libinput_device_config_accel_get_default_profile(struct libinput_device * device)3933 libinput_device_config_accel_get_default_profile(struct libinput_device *device)
3934 {
3935 	if (!libinput_device_config_accel_is_available(device))
3936 		return LIBINPUT_CONFIG_ACCEL_PROFILE_NONE;
3937 
3938 	return device->config.accel->get_default_profile(device);
3939 }
3940 
3941 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_accel_set_profile(struct libinput_device * device,enum libinput_config_accel_profile profile)3942 libinput_device_config_accel_set_profile(struct libinput_device *device,
3943 					 enum libinput_config_accel_profile profile)
3944 {
3945 	switch (profile) {
3946 	case LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT:
3947 	case LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE:
3948 		break;
3949 	default:
3950 		return LIBINPUT_CONFIG_STATUS_INVALID;
3951 	}
3952 
3953 	if (!libinput_device_config_accel_is_available(device) ||
3954 	    (libinput_device_config_accel_get_profiles(device) & profile) == 0)
3955 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
3956 
3957 	return device->config.accel->set_profile(device, profile);
3958 }
3959 
3960 LIBINPUT_EXPORT int
libinput_device_config_scroll_has_natural_scroll(struct libinput_device * device)3961 libinput_device_config_scroll_has_natural_scroll(struct libinput_device *device)
3962 {
3963 	if (!device->config.natural_scroll)
3964 		return 0;
3965 
3966 	return device->config.natural_scroll->has(device);
3967 }
3968 
3969 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_scroll_set_natural_scroll_enabled(struct libinput_device * device,int enabled)3970 libinput_device_config_scroll_set_natural_scroll_enabled(struct libinput_device *device,
3971 							 int enabled)
3972 {
3973 	if (!libinput_device_config_scroll_has_natural_scroll(device))
3974 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
3975 
3976 	return device->config.natural_scroll->set_enabled(device, enabled);
3977 }
3978 
3979 LIBINPUT_EXPORT int
libinput_device_config_scroll_get_natural_scroll_enabled(struct libinput_device * device)3980 libinput_device_config_scroll_get_natural_scroll_enabled(struct libinput_device *device)
3981 {
3982 	if (!device->config.natural_scroll)
3983 		return 0;
3984 
3985 	return device->config.natural_scroll->get_enabled(device);
3986 }
3987 
3988 LIBINPUT_EXPORT int
libinput_device_config_scroll_get_default_natural_scroll_enabled(struct libinput_device * device)3989 libinput_device_config_scroll_get_default_natural_scroll_enabled(struct libinput_device *device)
3990 {
3991 	if (!device->config.natural_scroll)
3992 		return 0;
3993 
3994 	return device->config.natural_scroll->get_default_enabled(device);
3995 }
3996 
3997 LIBINPUT_EXPORT int
libinput_device_config_left_handed_is_available(struct libinput_device * device)3998 libinput_device_config_left_handed_is_available(struct libinput_device *device)
3999 {
4000 	if (!device->config.left_handed)
4001 		return 0;
4002 
4003 	return device->config.left_handed->has(device);
4004 }
4005 
4006 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_left_handed_set(struct libinput_device * device,int left_handed)4007 libinput_device_config_left_handed_set(struct libinput_device *device,
4008 				       int left_handed)
4009 {
4010 	if (!libinput_device_config_left_handed_is_available(device))
4011 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
4012 
4013 	return device->config.left_handed->set(device, left_handed);
4014 }
4015 
4016 LIBINPUT_EXPORT int
libinput_device_config_left_handed_get(struct libinput_device * device)4017 libinput_device_config_left_handed_get(struct libinput_device *device)
4018 {
4019 	if (!libinput_device_config_left_handed_is_available(device))
4020 		return 0;
4021 
4022 	return device->config.left_handed->get(device);
4023 }
4024 
4025 LIBINPUT_EXPORT int
libinput_device_config_left_handed_get_default(struct libinput_device * device)4026 libinput_device_config_left_handed_get_default(struct libinput_device *device)
4027 {
4028 	if (!libinput_device_config_left_handed_is_available(device))
4029 		return 0;
4030 
4031 	return device->config.left_handed->get_default(device);
4032 }
4033 
4034 LIBINPUT_EXPORT uint32_t
libinput_device_config_click_get_methods(struct libinput_device * device)4035 libinput_device_config_click_get_methods(struct libinput_device *device)
4036 {
4037 	if (device->config.click_method)
4038 		return device->config.click_method->get_methods(device);
4039 	else
4040 		return 0;
4041 }
4042 
4043 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_click_set_method(struct libinput_device * device,enum libinput_config_click_method method)4044 libinput_device_config_click_set_method(struct libinput_device *device,
4045 					enum libinput_config_click_method method)
4046 {
4047 	/* Check method is a single valid method */
4048 	switch (method) {
4049 	case LIBINPUT_CONFIG_CLICK_METHOD_NONE:
4050 	case LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS:
4051 	case LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER:
4052 		break;
4053 	default:
4054 		return LIBINPUT_CONFIG_STATUS_INVALID;
4055 	}
4056 
4057 	if ((libinput_device_config_click_get_methods(device) & method) != method)
4058 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
4059 
4060 	if (device->config.click_method)
4061 		return device->config.click_method->set_method(device, method);
4062 	else /* method must be _NONE to get here */
4063 		return LIBINPUT_CONFIG_STATUS_SUCCESS;
4064 }
4065 
4066 LIBINPUT_EXPORT enum libinput_config_click_method
libinput_device_config_click_get_method(struct libinput_device * device)4067 libinput_device_config_click_get_method(struct libinput_device *device)
4068 {
4069 	if (device->config.click_method)
4070 		return device->config.click_method->get_method(device);
4071 	else
4072 		return LIBINPUT_CONFIG_CLICK_METHOD_NONE;
4073 }
4074 
4075 LIBINPUT_EXPORT enum libinput_config_click_method
libinput_device_config_click_get_default_method(struct libinput_device * device)4076 libinput_device_config_click_get_default_method(struct libinput_device *device)
4077 {
4078 	if (device->config.click_method)
4079 		return device->config.click_method->get_default_method(device);
4080 	else
4081 		return LIBINPUT_CONFIG_CLICK_METHOD_NONE;
4082 }
4083 
4084 LIBINPUT_EXPORT int
libinput_device_config_middle_emulation_is_available(struct libinput_device * device)4085 libinput_device_config_middle_emulation_is_available(
4086 		struct libinput_device *device)
4087 {
4088 	if (device->config.middle_emulation)
4089 		return device->config.middle_emulation->available(device);
4090 	else
4091 		return LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED;
4092 }
4093 
4094 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_middle_emulation_set_enabled(struct libinput_device * device,enum libinput_config_middle_emulation_state enable)4095 libinput_device_config_middle_emulation_set_enabled(
4096 		struct libinput_device *device,
4097 		enum libinput_config_middle_emulation_state enable)
4098 {
4099 	int available =
4100 		libinput_device_config_middle_emulation_is_available(device);
4101 
4102 	switch (enable) {
4103 	case LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED:
4104 		if (!available)
4105 			return LIBINPUT_CONFIG_STATUS_SUCCESS;
4106 		break;
4107 	case LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED:
4108 		if (!available)
4109 			return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
4110 		break;
4111 	default:
4112 		return LIBINPUT_CONFIG_STATUS_INVALID;
4113 	}
4114 
4115 	return device->config.middle_emulation->set(device, enable);
4116 }
4117 
4118 LIBINPUT_EXPORT enum libinput_config_middle_emulation_state
libinput_device_config_middle_emulation_get_enabled(struct libinput_device * device)4119 libinput_device_config_middle_emulation_get_enabled(
4120 		struct libinput_device *device)
4121 {
4122 	if (!libinput_device_config_middle_emulation_is_available(device))
4123 		return LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED;
4124 
4125 	return device->config.middle_emulation->get(device);
4126 }
4127 
4128 LIBINPUT_EXPORT enum libinput_config_middle_emulation_state
libinput_device_config_middle_emulation_get_default_enabled(struct libinput_device * device)4129 libinput_device_config_middle_emulation_get_default_enabled(
4130 		struct libinput_device *device)
4131 {
4132 	if (!libinput_device_config_middle_emulation_is_available(device))
4133 		return LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED;
4134 
4135 	return device->config.middle_emulation->get_default(device);
4136 }
4137 
4138 LIBINPUT_EXPORT uint32_t
libinput_device_config_scroll_get_methods(struct libinput_device * device)4139 libinput_device_config_scroll_get_methods(struct libinput_device *device)
4140 {
4141 	if (device->config.scroll_method)
4142 		return device->config.scroll_method->get_methods(device);
4143 	else
4144 		return 0;
4145 }
4146 
4147 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_scroll_set_method(struct libinput_device * device,enum libinput_config_scroll_method method)4148 libinput_device_config_scroll_set_method(struct libinput_device *device,
4149 					 enum libinput_config_scroll_method method)
4150 {
4151 	/* Check method is a single valid method */
4152 	switch (method) {
4153 	case LIBINPUT_CONFIG_SCROLL_NO_SCROLL:
4154 	case LIBINPUT_CONFIG_SCROLL_2FG:
4155 	case LIBINPUT_CONFIG_SCROLL_EDGE:
4156 	case LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN:
4157 		break;
4158 	default:
4159 		return LIBINPUT_CONFIG_STATUS_INVALID;
4160 	}
4161 
4162 	if ((libinput_device_config_scroll_get_methods(device) & method) != method)
4163 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
4164 
4165 	if (device->config.scroll_method)
4166 		return device->config.scroll_method->set_method(device, method);
4167 	else /* method must be _NO_SCROLL to get here */
4168 		return LIBINPUT_CONFIG_STATUS_SUCCESS;
4169 }
4170 
4171 LIBINPUT_EXPORT enum libinput_config_scroll_method
libinput_device_config_scroll_get_method(struct libinput_device * device)4172 libinput_device_config_scroll_get_method(struct libinput_device *device)
4173 {
4174 	if (device->config.scroll_method)
4175 		return device->config.scroll_method->get_method(device);
4176 	else
4177 		return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
4178 }
4179 
4180 LIBINPUT_EXPORT enum libinput_config_scroll_method
libinput_device_config_scroll_get_default_method(struct libinput_device * device)4181 libinput_device_config_scroll_get_default_method(struct libinput_device *device)
4182 {
4183 	if (device->config.scroll_method)
4184 		return device->config.scroll_method->get_default_method(device);
4185 	else
4186 		return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
4187 }
4188 
4189 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_scroll_set_button(struct libinput_device * device,uint32_t button)4190 libinput_device_config_scroll_set_button(struct libinput_device *device,
4191 					 uint32_t button)
4192 {
4193 	if ((libinput_device_config_scroll_get_methods(device) &
4194 	     LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) == 0)
4195 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
4196 
4197 	if (button && !libinput_device_pointer_has_button(device, button))
4198 		return LIBINPUT_CONFIG_STATUS_INVALID;
4199 
4200 	return device->config.scroll_method->set_button(device, button);
4201 }
4202 
4203 LIBINPUT_EXPORT uint32_t
libinput_device_config_scroll_get_button(struct libinput_device * device)4204 libinput_device_config_scroll_get_button(struct libinput_device *device)
4205 {
4206 	if ((libinput_device_config_scroll_get_methods(device) &
4207 	     LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) == 0)
4208 		return 0;
4209 
4210 	return device->config.scroll_method->get_button(device);
4211 }
4212 
4213 LIBINPUT_EXPORT uint32_t
libinput_device_config_scroll_get_default_button(struct libinput_device * device)4214 libinput_device_config_scroll_get_default_button(struct libinput_device *device)
4215 {
4216 	if ((libinput_device_config_scroll_get_methods(device) &
4217 	     LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) == 0)
4218 		return 0;
4219 
4220 	return device->config.scroll_method->get_default_button(device);
4221 }
4222 
4223 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_scroll_set_button_lock(struct libinput_device * device,enum libinput_config_scroll_button_lock_state state)4224 libinput_device_config_scroll_set_button_lock(struct libinput_device *device,
4225 					      enum libinput_config_scroll_button_lock_state state)
4226 {
4227 	if ((libinput_device_config_scroll_get_methods(device) &
4228 	     LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) == 0)
4229 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
4230 
4231 	switch (state) {
4232 	case LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_ENABLED:
4233 	case LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED:
4234 		break;
4235 	default:
4236 		return LIBINPUT_CONFIG_STATUS_INVALID;
4237 	}
4238 
4239 	return device->config.scroll_method->set_button_lock(device, state);
4240 }
4241 
4242 LIBINPUT_EXPORT enum libinput_config_scroll_button_lock_state
libinput_device_config_scroll_get_button_lock(struct libinput_device * device)4243 libinput_device_config_scroll_get_button_lock(struct libinput_device *device)
4244 {
4245 	if ((libinput_device_config_scroll_get_methods(device) &
4246 	     LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) == 0)
4247 		return LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED;
4248 
4249 	return device->config.scroll_method->get_button_lock(device);
4250 }
4251 
4252 LIBINPUT_EXPORT enum libinput_config_scroll_button_lock_state
libinput_device_config_scroll_get_default_button_lock(struct libinput_device * device)4253 libinput_device_config_scroll_get_default_button_lock(struct libinput_device *device)
4254 {
4255 	if ((libinput_device_config_scroll_get_methods(device) &
4256 	     LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) == 0)
4257 		return LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED;
4258 
4259 	return device->config.scroll_method->get_default_button_lock(device);
4260 }
4261 
4262 LIBINPUT_EXPORT int
libinput_device_config_dwt_is_available(struct libinput_device * device)4263 libinput_device_config_dwt_is_available(struct libinput_device *device)
4264 {
4265 	if (!device->config.dwt)
4266 		return 0;
4267 
4268 	return device->config.dwt->is_available(device);
4269 }
4270 
4271 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_dwt_set_enabled(struct libinput_device * device,enum libinput_config_dwt_state enable)4272 libinput_device_config_dwt_set_enabled(struct libinput_device *device,
4273 				       enum libinput_config_dwt_state enable)
4274 {
4275 	if (enable != LIBINPUT_CONFIG_DWT_ENABLED &&
4276 	    enable != LIBINPUT_CONFIG_DWT_DISABLED)
4277 		return LIBINPUT_CONFIG_STATUS_INVALID;
4278 
4279 	if (!libinput_device_config_dwt_is_available(device))
4280 		return enable ? LIBINPUT_CONFIG_STATUS_UNSUPPORTED :
4281 				LIBINPUT_CONFIG_STATUS_SUCCESS;
4282 
4283 	return device->config.dwt->set_enabled(device, enable);
4284 }
4285 
4286 LIBINPUT_EXPORT enum libinput_config_dwt_state
libinput_device_config_dwt_get_enabled(struct libinput_device * device)4287 libinput_device_config_dwt_get_enabled(struct libinput_device *device)
4288 {
4289 	if (!libinput_device_config_dwt_is_available(device))
4290 		return LIBINPUT_CONFIG_DWT_DISABLED;
4291 
4292 	return device->config.dwt->get_enabled(device);
4293 }
4294 
4295 LIBINPUT_EXPORT enum libinput_config_dwt_state
libinput_device_config_dwt_get_default_enabled(struct libinput_device * device)4296 libinput_device_config_dwt_get_default_enabled(struct libinput_device *device)
4297 {
4298 	if (!libinput_device_config_dwt_is_available(device))
4299 		return LIBINPUT_CONFIG_DWT_DISABLED;
4300 
4301 	return device->config.dwt->get_default_enabled(device);
4302 }
4303 
4304 LIBINPUT_EXPORT int
libinput_device_config_rotation_is_available(struct libinput_device * device)4305 libinput_device_config_rotation_is_available(struct libinput_device *device)
4306 {
4307 	if (!device->config.rotation)
4308 		return 0;
4309 
4310 	return device->config.rotation->is_available(device);
4311 }
4312 
4313 LIBINPUT_EXPORT enum libinput_config_status
libinput_device_config_rotation_set_angle(struct libinput_device * device,unsigned int degrees_cw)4314 libinput_device_config_rotation_set_angle(struct libinput_device *device,
4315 					  unsigned int degrees_cw)
4316 {
4317 	if (!libinput_device_config_rotation_is_available(device))
4318 		return degrees_cw ? LIBINPUT_CONFIG_STATUS_UNSUPPORTED :
4319 				    LIBINPUT_CONFIG_STATUS_SUCCESS;
4320 
4321 	if (degrees_cw >= 360 || degrees_cw % 90)
4322 		return LIBINPUT_CONFIG_STATUS_INVALID;
4323 
4324 	return device->config.rotation->set_angle(device, degrees_cw);
4325 }
4326 
4327 LIBINPUT_EXPORT unsigned int
libinput_device_config_rotation_get_angle(struct libinput_device * device)4328 libinput_device_config_rotation_get_angle(struct libinput_device *device)
4329 {
4330 	if (!libinput_device_config_rotation_is_available(device))
4331 		return 0;
4332 
4333 	return device->config.rotation->get_angle(device);
4334 }
4335 
4336 LIBINPUT_EXPORT unsigned int
libinput_device_config_rotation_get_default_angle(struct libinput_device * device)4337 libinput_device_config_rotation_get_default_angle(struct libinput_device *device)
4338 {
4339 	if (!libinput_device_config_rotation_is_available(device))
4340 		return 0;
4341 
4342 	return device->config.rotation->get_default_angle(device);
4343 }
4344 
4345 #if HAVE_LIBWACOM
4346 WacomDeviceDatabase *
libinput_libwacom_ref(struct libinput * li)4347 libinput_libwacom_ref(struct libinput *li)
4348 {
4349 	WacomDeviceDatabase *db = NULL;
4350 	if (!li->libwacom.db) {
4351 		db = libwacom_database_new();
4352 		if (!db) {
4353 			log_error(li,
4354 				  "Failed to initialize libwacom context\n");
4355 			return NULL;
4356 		}
4357 
4358 		li->libwacom.db = db;
4359 		li->libwacom.refcount = 0;
4360 	}
4361 
4362 	li->libwacom.refcount++;
4363 	db = li->libwacom.db;
4364 	return db;
4365 }
4366 
4367 void
libinput_libwacom_unref(struct libinput * li)4368 libinput_libwacom_unref(struct libinput *li)
4369 {
4370 	if (!li->libwacom.db)
4371 		return;
4372 
4373 	assert(li->libwacom.refcount >= 1);
4374 
4375 	if (--li->libwacom.refcount == 0) {
4376 		libwacom_database_destroy(li->libwacom.db);
4377 		li->libwacom.db = NULL;
4378 	}
4379 }
4380 #endif
4381