Name-space the read flags better
[platform/upstream/libevdev.git] / libevdev / libevdev-int.h
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 <errno.h>
30 #include "libevdev.h"
31
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])))
35 #define MAX_NAME 256
36 #define MAX_SLOTS 32
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)
40 #define LIBEVDEV_EXPORT __attribute__((visibility("default")))
41 #define LIBEVDEV_PRINTF(_format, _args) __attribute__ ((format (printf, _format, _args)))
42 #define ALIAS(_to) __attribute__((alias(#_to)))
43
44 #undef min
45 #undef max
46 #define min(a,b) \
47                 ({ __typeof__ (a) _a = (a); \
48                   __typeof__ (b) _b = (b); \
49                 _a > _b ? _b : _a; \
50                 })
51 #define max(a,b) \
52                 ({ __typeof__ (a) _a = (a); \
53                   __typeof__ (b) _b = (b); \
54                 _a > _b ? _a : _b; \
55                 })
56
57 /**
58  * Sync state machine:
59  * default state: SYNC_NONE
60  *
61  * SYNC_NONE → SYN_DROPPED or forced sync → SYNC_NEEDED
62  * SYNC_NEEDED → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC) → SYNC_IN_PROGRESS
63  * SYNC_NEEDED → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC_NONE) → SYNC_NONE
64  * SYNC_IN_PROGRESS → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC_NONE) → SYNC_NONE
65  * SYNC_IN_PROGRESS → no sync events left → SYNC_NONE
66  *
67  */
68 enum SyncState {
69         SYNC_NONE,
70         SYNC_NEEDED,
71         SYNC_IN_PROGRESS,
72 };
73
74 struct libevdev {
75         int fd;
76         char *name;
77         char *phys;
78         char *uniq;
79         struct input_id ids;
80         int driver_version;
81         unsigned long bits[NLONGS(EV_CNT)];
82         unsigned long props[NLONGS(INPUT_PROP_CNT)];
83         unsigned long key_bits[NLONGS(KEY_CNT)];
84         unsigned long rel_bits[NLONGS(REL_CNT)];
85         unsigned long abs_bits[NLONGS(ABS_CNT)];
86         unsigned long led_bits[NLONGS(LED_CNT)];
87         unsigned long msc_bits[NLONGS(MSC_CNT)];
88         unsigned long sw_bits[NLONGS(SW_CNT)];
89         unsigned long rep_bits[NLONGS(REP_CNT)]; /* convenience, always 1 */
90         unsigned long ff_bits[NLONGS(FF_CNT)];
91         unsigned long snd_bits[NLONGS(SND_CNT)];
92         unsigned long key_values[NLONGS(KEY_CNT)];
93         unsigned long led_values[NLONGS(LED_CNT)];
94         unsigned long sw_values[NLONGS(SW_CNT)];
95         struct input_absinfo abs_info[ABS_CNT];
96         int mt_slot_vals[MAX_SLOTS][ABS_MT_CNT];
97         int num_slots; /**< valid slots in mt_slot_vals */
98         int current_slot;
99         int rep_values[REP_CNT];
100
101         enum SyncState sync_state;
102         enum libevdev_grab_mode grabbed;
103
104         struct input_event *queue;
105         size_t queue_size; /**< size of queue in elements */
106         size_t queue_next; /**< next event index */
107         size_t queue_nsync; /**< number of sync events */
108
109         struct timeval last_event_time;
110 };
111
112 struct logdata {
113         enum libevdev_log_priority priority;    /** minimum logging priority */
114         libevdev_log_func_t handler;            /** handler function */
115         void *userdata;                         /** user-defined data pointer */
116 };
117 extern struct logdata log_data;
118
119 #define log_msg_cond(priority, ...) \
120         do { \
121                 if (libevdev_get_log_priority() >= priority) \
122                         log_msg(priority, log_data.userdata, __FILE__, __LINE__, __func__, __VA_ARGS__); \
123         } while(0)
124
125 #define log_error(...) log_msg_cond(LIBEVDEV_LOG_ERROR, __VA_ARGS__)
126 #define log_info(...) log_msg_cond(LIBEVDEV_LOG_INFO, __VA_ARGS__)
127 #define log_dbg(...) log_msg_cond(LIBEVDEV_LOG_DEBUG, __VA_ARGS__)
128 #define log_bug(...) log_msg_cond(LIBEVDEV_LOG_ERROR, "BUG: "__VA_ARGS__)
129
130 extern void
131 log_msg(enum libevdev_log_priority priority,
132         void *data,
133         const char *file, int line, const char *func,
134         const char *format, ...) LIBEVDEV_PRINTF(6, 7);
135
136 /**
137  * @return a pointer to the next element in the queue, or NULL if the queue
138  * is full.
139  */
140 static inline struct input_event*
141 queue_push(struct libevdev *dev)
142 {
143         if (dev->queue_next >= dev->queue_size)
144                 return NULL;
145
146         return &dev->queue[dev->queue_next++];
147 }
148
149 /**
150  * Set ev to the last element in the queue, removing it from the queue.
151  *
152  * @return 0 on success, 1 if the queue is empty.
153  */
154 static inline int
155 queue_pop(struct libevdev *dev, struct input_event *ev)
156 {
157         if (dev->queue_next == 0)
158                 return 1;
159
160         *ev = dev->queue[--dev->queue_next];
161
162         return 0;
163 }
164
165 static inline int
166 queue_peek(struct libevdev *dev, size_t idx, struct input_event *ev)
167 {
168         if (dev->queue_next == 0 || idx > dev->queue_next)
169                 return 1;
170         *ev = dev->queue[idx];
171         return 0;
172 }
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
184 queue_shift_multiple(struct libevdev *dev, size_t n, struct input_event *ev)
185 {
186         size_t i;
187
188         if (dev->queue_next == 0)
189                 return 0;
190
191         n = min(n, dev->queue_next);
192
193         if (ev) {
194                 for (i = 0; i < n; i++)
195                         ev[i] = dev->queue[i];
196         }
197
198         for (i = 0; i < dev->queue_next - n; i++)
199                 dev->queue[i] = dev->queue[n + i];
200
201         dev->queue_next -= n;
202         return n;
203 }
204
205 /**
206  * Set ev to the first element in the queue, shifting everything else
207  * forward by one.
208  *
209  * @return 0 on success, 1 if the queue is empty.
210  */
211 static inline int
212 queue_shift(struct libevdev *dev, struct input_event *ev)
213 {
214         return queue_shift_multiple(dev, 1, ev) == 1 ? 0 : 1;
215 }
216
217 static inline int
218 queue_alloc(struct libevdev *dev, size_t size)
219 {
220         if (size == 0)
221                 return -ENOMEM;
222
223         dev->queue = calloc(size, sizeof(struct input_event));
224         if (!dev->queue)
225                 return -ENOMEM;
226
227         dev->queue_size = size;
228         dev->queue_next = 0;
229         return 0;
230 }
231
232 static inline void
233 queue_free(struct libevdev *dev)
234 {
235         free(dev->queue);
236         dev->queue_size = 0;
237         dev->queue_next = 0;
238 }
239
240 static inline size_t
241 queue_num_elements(struct libevdev *dev)
242 {
243         return dev->queue_next;
244 }
245
246 static inline size_t
247 queue_size(struct libevdev *dev)
248 {
249         return dev->queue_size;
250 }
251
252 static inline size_t
253 queue_num_free_elements(struct libevdev *dev)
254 {
255         if (dev->queue_size == 0)
256                 return 0;
257
258         return dev->queue_size - dev->queue_next;
259 }
260
261 static inline struct input_event *
262 queue_next_element(struct libevdev *dev)
263 {
264         if (dev->queue_next == dev->queue_size)
265                 return NULL;
266
267         return &dev->queue[dev->queue_next];
268 }
269
270 static inline int
271 queue_set_num_elements(struct libevdev *dev, size_t nelem)
272 {
273         if (nelem > dev->queue_size)
274                 return 1;
275
276         dev->queue_next = nelem;
277
278         return 0;
279 }
280 #endif
281