• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2013 Red Hat, Inc.
4  */
5 
6 #ifndef LIBEVDEV_INT_H
7 #define LIBEVDEV_INT_H
8 
9 #include "config.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13 #include <errno.h>
14 #include "libevdev.h"
15 #include "libevdev-util.h"
16 
17 #define MAX_NAME 256
18 #define ABS_MT_MIN ABS_MT_SLOT
19 #define ABS_MT_MAX ABS_MT_TOOL_Y
20 #define ABS_MT_CNT (ABS_MT_MAX - ABS_MT_MIN + 1)
21 #define LIBEVDEV_EXPORT __attribute__((visibility("default")))
22 #define ALIAS(_to) __attribute__((alias(#_to)))
23 
24 /**
25  * Sync state machine:
26  * default state: SYNC_NONE
27  *
28  * SYNC_NONE → SYN_DROPPED or forced sync → SYNC_NEEDED
29  * SYNC_NEEDED → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC) → SYNC_IN_PROGRESS
30  * SYNC_NEEDED → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC_NONE) → SYNC_NONE
31  * SYNC_IN_PROGRESS → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC_NONE) → SYNC_NONE
32  * SYNC_IN_PROGRESS → no sync events left → SYNC_NONE
33  *
34  */
35 enum SyncState {
36 	SYNC_NONE,
37 	SYNC_NEEDED,
38 	SYNC_IN_PROGRESS,
39 };
40 
41 /**
42  * Internal only: log data used to send messages to the respective log
43  * handler. We re-use the same struct for a global and inside
44  * struct libevdev.
45  * For the global, device_handler is NULL, for per-device instance
46  * global_handler is NULL.
47  */
48 struct logdata {
49 	enum libevdev_log_priority priority;		/** minimum logging priority */
50 	libevdev_log_func_t global_handler;		/** global handler function */
51 	libevdev_device_log_func_t device_handler;	/** per-device handler function */
52 	void *userdata;					/** user-defined data pointer */
53 };
54 
55 struct libevdev {
56 	int fd;
57 	bool initialized;
58 	char *name;
59 	char *phys;
60 	char *uniq;
61 	struct input_id ids;
62 	int driver_version;
63 	unsigned long bits[NLONGS(EV_CNT)];
64 	unsigned long props[NLONGS(INPUT_PROP_CNT)];
65 	unsigned long key_bits[NLONGS(KEY_CNT)];
66 	unsigned long rel_bits[NLONGS(REL_CNT)];
67 	unsigned long abs_bits[NLONGS(ABS_CNT)];
68 	unsigned long led_bits[NLONGS(LED_CNT)];
69 	unsigned long msc_bits[NLONGS(MSC_CNT)];
70 	unsigned long sw_bits[NLONGS(SW_CNT)];
71 	unsigned long rep_bits[NLONGS(REP_CNT)]; /* convenience, always 1 */
72 	unsigned long ff_bits[NLONGS(FF_CNT)];
73 	unsigned long snd_bits[NLONGS(SND_CNT)];
74 	unsigned long key_values[NLONGS(KEY_CNT)];
75 	unsigned long led_values[NLONGS(LED_CNT)];
76 	unsigned long sw_values[NLONGS(SW_CNT)];
77 	struct input_absinfo abs_info[ABS_CNT];
78 	int *mt_slot_vals; /* [num_slots * ABS_MT_CNT] */
79 	int num_slots; /**< valid slots in mt_slot_vals */
80 	int current_slot;
81 	int rep_values[REP_CNT];
82 
83 	enum SyncState sync_state;
84 	enum libevdev_grab_mode grabbed;
85 
86 	struct input_event *queue;
87 	size_t queue_size; /**< size of queue in elements */
88 	size_t queue_next; /**< next event index */
89 	size_t queue_nsync; /**< number of sync events */
90 
91 	struct timeval last_event_time;
92 
93 	struct logdata log;
94 };
95 
96 #define log_msg_cond(dev, priority, ...) \
97 	do { \
98 		if (_libevdev_log_priority(dev) >= priority) \
99 			_libevdev_log_msg(dev, priority, __FILE__, __LINE__, __func__, __VA_ARGS__); \
100 	} while(0)
101 
102 #define log_error(dev, ...) log_msg_cond(dev, LIBEVDEV_LOG_ERROR, __VA_ARGS__)
103 #define log_info(dev, ...) log_msg_cond(dev, LIBEVDEV_LOG_INFO, __VA_ARGS__)
104 #define log_dbg(dev, ...) log_msg_cond(dev, LIBEVDEV_LOG_DEBUG, __VA_ARGS__)
105 #define log_bug(dev, ...) log_msg_cond(dev, LIBEVDEV_LOG_ERROR, "BUG: "__VA_ARGS__)
106 
107 extern void
108 _libevdev_log_msg(const struct libevdev *dev,
109 		  enum libevdev_log_priority priority,
110 		  const char *file, int line, const char *func,
111 		  const char *format, ...) LIBEVDEV_ATTRIBUTE_PRINTF(6, 7);
112 extern enum libevdev_log_priority
113 _libevdev_log_priority(const struct libevdev *dev);
114 
115 static inline void
init_event(struct libevdev * dev,struct input_event * ev,int type,int code,int value)116 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
117 {
118 	ev->input_event_sec = dev->last_event_time.tv_sec;
119 	ev->input_event_usec = dev->last_event_time.tv_usec;
120 	ev->type = type;
121 	ev->code = code;
122 	ev->value = value;
123 }
124 
125 /**
126  * @return a pointer to the next element in the queue, or NULL if the queue
127  * is full.
128  */
129 static inline struct input_event*
queue_push(struct libevdev * dev)130 queue_push(struct libevdev *dev)
131 {
132 	if (dev->queue_next >= dev->queue_size)
133 		return NULL;
134 
135 	return &dev->queue[dev->queue_next++];
136 }
137 
138 static inline bool
queue_push_event(struct libevdev * dev,unsigned int type,unsigned int code,int value)139 queue_push_event(struct libevdev *dev, unsigned int type,
140 		 unsigned int code, int value)
141 {
142 	struct input_event *ev = queue_push(dev);
143 
144 	if (ev)
145 		init_event(dev, ev, type, code, value);
146 
147 	return ev != NULL;
148 }
149 
150 /**
151  * Set ev to the last element in the queue, removing it from the queue.
152  *
153  * @return 0 on success, 1 if the queue is empty.
154  */
155 static inline int
queue_pop(struct libevdev * dev,struct input_event * ev)156 queue_pop(struct libevdev *dev, struct input_event *ev)
157 {
158 	if (dev->queue_next == 0)
159 		return 1;
160 
161 	*ev = dev->queue[--dev->queue_next];
162 
163 	return 0;
164 }
165 
166 static inline int
queue_peek(struct libevdev * dev,size_t idx,struct input_event * ev)167 queue_peek(struct libevdev *dev, size_t idx, struct input_event *ev)
168 {
169 	if (dev->queue_next == 0 || idx > dev->queue_next)
170 		return 1;
171 	*ev = dev->queue[idx];
172 	return 0;
173 }
174 
175 /**
176  * Shift the first n elements into ev and return the number of elements
177  * shifted.
178  * ev must be large enough to store n elements.
179  *
180  * @param ev The buffer to copy into, or NULL
181  * @return The number of elements in ev.
182  */
183 static inline int
queue_shift_multiple(struct libevdev * dev,size_t n,struct input_event * ev)184 queue_shift_multiple(struct libevdev *dev, size_t n, struct input_event *ev)
185 {
186 	size_t remaining;
187 
188 	if (dev->queue_next == 0)
189 		return 0;
190 
191 	remaining = dev->queue_next;
192 	n = min(n, remaining);
193 	remaining -= n;
194 
195 	if (ev)
196 		memcpy(ev, dev->queue, n * sizeof(*ev));
197 
198 	memmove(dev->queue, &dev->queue[n], remaining * sizeof(*dev->queue));
199 
200 	dev->queue_next = remaining;
201 	return n;
202 }
203 
204 /**
205  * Set ev to the first element in the queue, shifting everything else
206  * forward by one.
207  *
208  * @return 0 on success, 1 if the queue is empty.
209  */
210 static inline int
queue_shift(struct libevdev * dev,struct input_event * ev)211 queue_shift(struct libevdev *dev, struct input_event *ev)
212 {
213 	return queue_shift_multiple(dev, 1, ev) == 1 ? 0 : 1;
214 }
215 
216 static inline int
queue_alloc(struct libevdev * dev,size_t size)217 queue_alloc(struct libevdev *dev, size_t size)
218 {
219 	if (size == 0)
220 		return -ENOMEM;
221 
222 	dev->queue = calloc(size, sizeof(struct input_event));
223 	if (!dev->queue)
224 		return -ENOMEM;
225 
226 	dev->queue_size = size;
227 	dev->queue_next = 0;
228 	return 0;
229 }
230 
231 static inline void
queue_free(struct libevdev * dev)232 queue_free(struct libevdev *dev)
233 {
234 	free(dev->queue);
235 	dev->queue_size = 0;
236 	dev->queue_next = 0;
237 }
238 
239 static inline size_t
queue_num_elements(struct libevdev * dev)240 queue_num_elements(struct libevdev *dev)
241 {
242 	return dev->queue_next;
243 }
244 
245 static inline size_t
queue_size(struct libevdev * dev)246 queue_size(struct libevdev *dev)
247 {
248 	return dev->queue_size;
249 }
250 
251 static inline size_t
queue_num_free_elements(struct libevdev * dev)252 queue_num_free_elements(struct libevdev *dev)
253 {
254 	if (dev->queue_size == 0)
255 		return 0;
256 
257 	return dev->queue_size - dev->queue_next;
258 }
259 
260 static inline struct input_event *
queue_next_element(struct libevdev * dev)261 queue_next_element(struct libevdev *dev)
262 {
263 	if (dev->queue_next == dev->queue_size)
264 		return NULL;
265 
266 	return &dev->queue[dev->queue_next];
267 }
268 
269 static inline int
queue_set_num_elements(struct libevdev * dev,size_t nelem)270 queue_set_num_elements(struct libevdev *dev, size_t nelem)
271 {
272 	if (nelem > dev->queue_size)
273 		return 1;
274 
275 	dev->queue_next = nelem;
276 
277 	return 0;
278 }
279 
280 #define max_mask(uc, lc) \
281 	case EV_##uc: \
282 			*mask = dev->lc##_bits; \
283 			max = libevdev_event_type_get_max(type); \
284 		break;
285 
286 static inline int
type_to_mask_const(const struct libevdev * dev,unsigned int type,const unsigned long ** mask)287 type_to_mask_const(const struct libevdev *dev, unsigned int type, const unsigned long **mask)
288 {
289 	int max;
290 
291 	switch(type) {
292 		max_mask(ABS, abs);
293 		max_mask(REL, rel);
294 		max_mask(KEY, key);
295 		max_mask(LED, led);
296 		max_mask(MSC, msc);
297 		max_mask(SW, sw);
298 		max_mask(FF, ff);
299 		max_mask(REP, rep);
300 		max_mask(SND, snd);
301 		default:
302 		     max = -1;
303 		     break;
304 	}
305 
306 	return max;
307 }
308 
309 static inline int
type_to_mask(struct libevdev * dev,unsigned int type,unsigned long ** mask)310 type_to_mask(struct libevdev *dev, unsigned int type, unsigned long **mask)
311 {
312 	int max;
313 
314 	switch(type) {
315 		max_mask(ABS, abs);
316 		max_mask(REL, rel);
317 		max_mask(KEY, key);
318 		max_mask(LED, led);
319 		max_mask(MSC, msc);
320 		max_mask(SW, sw);
321 		max_mask(FF, ff);
322 		max_mask(REP, rep);
323 		max_mask(SND, snd);
324 		default:
325 		     max = -1;
326 		     break;
327 	}
328 
329 	return max;
330 }
331 
332 #undef max_mask
333 #endif
334