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