2 * Copyright © 2013 Red Hat, Inc.
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.
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
23 #ifndef libevdev_INT_H
24 #define libevdev_INT_H
32 #define LONG_BITS (sizeof(long) * 8)
33 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
34 #define ARRAY_LENGTH(a) (sizeof(a) / (sizeof((a)[0])))
37 #define ABS_MT_MIN ABS_MT_SLOT
38 #define ABS_MT_MAX ABS_MT_TOOL_Y
39 #define ABS_MT_CNT (ABS_MT_MAX - ABS_MT_MIN + 1)
44 ({ __typeof__ (a) _a = (a); \
45 __typeof__ (b) _b = (b); \
49 ({ __typeof__ (a) _a = (a); \
50 __typeof__ (b) _b = (b); \
56 libevdev_log_func_t log;
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 struct input_absinfo abs_info[ABS_CNT];
76 unsigned int mt_slot_vals[MAX_SLOTS][ABS_MT_CNT];
77 int num_slots; /**< valid slots in mt_slot_vals */
84 struct input_event *queue;
85 size_t queue_size; /**< size of queue in elements */
86 size_t queue_next; /**< next event index */
87 size_t queue_nsync; /**< number of sync events */
89 struct timeval last_event_time;
93 * @return a pointer to the next element in the queue, or NULL if the queue
96 static inline struct input_event*
97 queue_push(struct libevdev *dev)
99 if (dev->queue_next >= dev->queue_size)
102 return &dev->queue[dev->queue_next++];
106 * Set ev to the last element in the queue, removing it from the queue.
108 * @return 0 on success, 1 if the queue is empty.
111 queue_pop(struct libevdev *dev, struct input_event *ev)
113 if (dev->queue_next == 0)
116 *ev = dev->queue[--dev->queue_next];
122 queue_peek(struct libevdev *dev, size_t idx, struct input_event *ev)
124 if (idx > dev->queue_next)
126 *ev = dev->queue[idx];
132 * Shift the first n elements into ev and return the number of elements
134 * ev must be large enough to store n elements.
136 * @param ev The buffer to copy into, or NULL
137 * @return The number of elements in ev.
140 queue_shift_multiple(struct libevdev *dev, int n, struct input_event *ev)
144 if (dev->queue_next == 0)
147 n = min(n, dev->queue_next);
150 for (i = 0; i < n; i++)
151 ev[i] = dev->queue[i];
154 for (i = 0; i < dev->queue_next - n; i++)
155 dev->queue[i] = dev->queue[n + i];
157 dev->queue_next -= n;
162 * Set ev to the first element in the queue, shifting everything else
165 * @return 0 on success, 1 if the queue is empty.
168 queue_shift(struct libevdev *dev, struct input_event *ev)
170 return queue_shift_multiple(dev, 1, ev) == 1 ? 0 : 1;
174 queue_alloc(struct libevdev *dev, int size)
176 dev->queue = calloc(size, sizeof(struct input_event));
180 dev->queue_size = size;
186 queue_free(struct libevdev *dev)
194 queue_num_elements(struct libevdev *dev)
196 return dev->queue_next;
200 queue_size(struct libevdev *dev)
202 return dev->queue_size;
206 queue_num_free_elements(struct libevdev *dev)
208 return dev->queue_size - dev->queue_next - 1;
211 static inline struct input_event *
212 queue_next_element(struct libevdev *dev)
214 return &dev->queue[dev->queue_next];
218 queue_set_num_elements(struct libevdev *dev, int nelem)
220 if (nelem > dev->queue_size)
223 dev->queue_next = nelem;