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