tizen 2.4 release
[framework/uifw/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 #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 struct mt_sync_state {
59         int code;
60         int val[];
61 };
62
63 struct libevdev {
64         int fd;
65         bool initialized;
66         char *name;
67         char *phys;
68         char *uniq;
69         struct input_id ids;
70         int driver_version;
71         unsigned long bits[NLONGS(EV_CNT)];
72         unsigned long props[NLONGS(INPUT_PROP_CNT)];
73         unsigned long key_bits[NLONGS(KEY_CNT)];
74         unsigned long rel_bits[NLONGS(REL_CNT)];
75         unsigned long abs_bits[NLONGS(ABS_CNT)];
76         unsigned long led_bits[NLONGS(LED_CNT)];
77         unsigned long msc_bits[NLONGS(MSC_CNT)];
78         unsigned long sw_bits[NLONGS(SW_CNT)];
79         unsigned long rep_bits[NLONGS(REP_CNT)]; /* convenience, always 1 */
80         unsigned long ff_bits[NLONGS(FF_CNT)];
81         unsigned long snd_bits[NLONGS(SND_CNT)];
82         unsigned long key_values[NLONGS(KEY_CNT)];
83         unsigned long led_values[NLONGS(LED_CNT)];
84         unsigned long sw_values[NLONGS(SW_CNT)];
85         struct input_absinfo abs_info[ABS_CNT];
86         int *mt_slot_vals; /* [num_slots * ABS_MT_CNT] */
87         int num_slots; /**< valid slots in mt_slot_vals */
88         int current_slot;
89         int rep_values[REP_CNT];
90
91         enum SyncState sync_state;
92         enum libevdev_grab_mode grabbed;
93
94         struct input_event *queue;
95         size_t queue_size; /**< size of queue in elements */
96         size_t queue_next; /**< next event index */
97         size_t queue_nsync; /**< number of sync events */
98
99         struct timeval last_event_time;
100
101         struct {
102                 struct mt_sync_state *mt_state;
103                 size_t mt_state_sz;              /* in bytes */
104                 unsigned long *slot_update;
105                 size_t slot_update_sz;           /* in bytes */
106                 unsigned long *tracking_id_changes;
107                 size_t tracking_id_changes_sz;   /* in bytes */
108         } mt_sync;
109 };
110
111 struct logdata {
112         enum libevdev_log_priority priority;    /** minimum logging priority */
113         libevdev_log_func_t handler;            /** handler function */
114         void *userdata;                         /** user-defined data pointer */
115 };
116 extern struct logdata log_data;
117
118 #define log_msg_cond(priority, ...) \
119         do { \
120                 if (libevdev_get_log_priority() >= priority) \
121                         log_msg(priority, log_data.userdata, __FILE__, __LINE__, __func__, __VA_ARGS__); \
122         } while(0)
123
124 #define log_error(...) log_msg_cond(LIBEVDEV_LOG_ERROR, __VA_ARGS__)
125 #define log_info(...) log_msg_cond(LIBEVDEV_LOG_INFO, __VA_ARGS__)
126 #define log_dbg(...) log_msg_cond(LIBEVDEV_LOG_DEBUG, __VA_ARGS__)
127 #define log_bug(...) log_msg_cond(LIBEVDEV_LOG_ERROR, "BUG: "__VA_ARGS__)
128
129 extern void
130 log_msg(enum libevdev_log_priority priority,
131         void *data,
132         const char *file, int line, const char *func,
133         const char *format, ...) LIBEVDEV_ATTRIBUTE_PRINTF(6, 7);
134
135 /**
136  * @return a pointer to the next element in the queue, or NULL if the queue
137  * is full.
138  */
139 static inline struct input_event*
140 queue_push(struct libevdev *dev)
141 {
142         if (dev->queue_next >= dev->queue_size)
143                 return NULL;
144
145         return &dev->queue[dev->queue_next++];
146 }
147
148 /**
149  * Set ev to the last element in the queue, removing it from the queue.
150  *
151  * @return 0 on success, 1 if the queue is empty.
152  */
153 static inline int
154 queue_pop(struct libevdev *dev, struct input_event *ev)
155 {
156         if (dev->queue_next == 0)
157                 return 1;
158
159         *ev = dev->queue[--dev->queue_next];
160
161         return 0;
162 }
163
164 static inline int
165 queue_peek(struct libevdev *dev, size_t idx, struct input_event *ev)
166 {
167         if (dev->queue_next == 0 || idx > dev->queue_next)
168                 return 1;
169         *ev = dev->queue[idx];
170         return 0;
171 }
172
173
174 /**
175  * Shift the first n elements into ev and return the number of elements
176  * shifted.
177  * ev must be large enough to store n elements.
178  *
179  * @param ev The buffer to copy into, or NULL
180  * @return The number of elements in ev.
181  */
182 static inline int
183 queue_shift_multiple(struct libevdev *dev, size_t n, struct input_event *ev)
184 {
185         size_t i;
186
187         if (dev->queue_next == 0)
188                 return 0;
189
190         n = min(n, dev->queue_next);
191
192         if (ev) {
193                 for (i = 0; i < n; i++)
194                         ev[i] = dev->queue[i];
195         }
196
197         for (i = 0; i < dev->queue_next - n; i++)
198                 dev->queue[i] = dev->queue[n + i];
199
200         dev->queue_next -= n;
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
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
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
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
240 queue_num_elements(struct libevdev *dev)
241 {
242         return dev->queue_next;
243 }
244
245 static inline size_t
246 queue_size(struct libevdev *dev)
247 {
248         return dev->queue_size;
249 }
250
251 static inline size_t
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 *
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
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
287 static inline int
288 type_to_mask_const(const struct libevdev *dev, unsigned int type, const unsigned long **mask)
289 {
290         int max;
291
292         switch(type) {
293                 max_mask(ABS, abs);
294                 max_mask(REL, rel);
295                 max_mask(KEY, key);
296                 max_mask(LED, led);
297                 max_mask(MSC, msc);
298                 max_mask(SW, sw);
299                 max_mask(FF, ff);
300                 max_mask(REP, rep);
301                 max_mask(SND, snd);
302                 default:
303                      max = -1;
304                      break;
305         }
306
307         return max;
308 }
309
310 static inline int
311 type_to_mask(struct libevdev *dev, unsigned int type, unsigned long **mask)
312 {
313         int max;
314
315         switch(type) {
316                 max_mask(ABS, abs);
317                 max_mask(REL, rel);
318                 max_mask(KEY, key);
319                 max_mask(LED, led);
320                 max_mask(MSC, msc);
321                 max_mask(SW, sw);
322                 max_mask(FF, ff);
323                 max_mask(REP, rep);
324                 max_mask(SND, snd);
325                 default:
326                      max = -1;
327                      break;
328         }
329
330         return max;
331 }
332
333 #undef max_mask
334 #endif
335