Add a few includes the libevdev-int.h needs
[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
41 #undef min
42 #undef max
43 #define min(a,b) \
44                 ({ __typeof__ (a) _a = (a); \
45                   __typeof__ (b) _b = (b); \
46                 _a > _b ? _b : _a; \
47                 })
48 #define max(a,b) \
49                 ({ __typeof__ (a) _a = (a); \
50                   __typeof__ (b) _b = (b); \
51                 _a > _b ? _a : _b; \
52                 })
53
54 struct libevdev {
55         int fd;
56         libevdev_log_func_t log;
57
58         char *name;
59         char *phys;
60         char *uniq;
61         struct input_id ids;
62         int driver_version;
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 key_values[NLONGS(KEY_CNT)];
70         struct input_absinfo abs_info[ABS_CNT];
71         unsigned int mt_slot_vals[MAX_SLOTS][ABS_MT_CNT];
72         int num_slots; /**< valid slots in mt_slot_vals */
73         int current_slot;
74
75         int need_sync;
76         int grabbed;
77
78         struct input_event *queue;
79         size_t queue_size; /**< size of queue in elements */
80         size_t queue_next; /**< next event index */
81         size_t queue_nsync; /**< number of sync events */
82
83         struct timeval last_event_time;
84 };
85
86 /**
87  * @return a pointer to the next element in the queue, or NULL if the queue
88  * is full.
89  */
90 static inline struct input_event*
91 queue_push(struct libevdev *dev)
92 {
93         if (dev->queue_next >= dev->queue_size)
94                 return NULL;
95
96         return &dev->queue[dev->queue_next++];
97 }
98
99 /**
100  * Set ev to the last element in the queue, removing it from the queue.
101  *
102  * @return 0 on success, 1 if the queue is empty.
103  */
104 static inline int
105 queue_pop(struct libevdev *dev, struct input_event *ev)
106 {
107         if (dev->queue_next == 0)
108                 return 1;
109
110         *ev = dev->queue[--dev->queue_next];
111
112         return 0;
113 }
114
115 static inline int
116 queue_peek(struct libevdev *dev, size_t idx, struct input_event *ev)
117 {
118         if (idx > dev->queue_next)
119                 return 1;
120         *ev = dev->queue[idx];
121         return 0;
122 }
123
124
125 /**
126  * Shift the first n elements into ev and return the number of elements
127  * shifted.
128  * ev must be large enough to store n elements.
129  *
130  * @param ev The buffer to copy into, or NULL
131  * @return The number of elements in ev.
132  */
133 static inline int
134 queue_shift_multiple(struct libevdev *dev, int n, struct input_event *ev)
135 {
136         int i;
137
138         if (dev->queue_next == 0)
139                 return 0;
140
141         n = min(n, dev->queue_next);
142
143         if (ev) {
144                 for (i = 0; i < n; i++)
145                         ev[i] = dev->queue[i];
146         }
147
148         for (i = 0; i < dev->queue_next - n; i++)
149                 dev->queue[i] = dev->queue[n + i];
150
151         dev->queue_next -= n;
152         return n;
153 }
154
155 /**
156  * Set ev to the first element in the queue, shifting everything else
157  * forward by one.
158  *
159  * @return 0 on success, 1 if the queue is empty.
160  */
161 static inline int
162 queue_shift(struct libevdev *dev, struct input_event *ev)
163 {
164         return queue_shift_multiple(dev, 1, ev) == 1 ? 0 : 1;
165 }
166
167 static inline int
168 queue_alloc(struct libevdev *dev, int size)
169 {
170         dev->queue = calloc(size, sizeof(struct input_event));
171         if (!dev->queue)
172                 return -ENOSPC;
173
174         dev->queue_size = size;
175         dev->queue_next = 0;
176         return 0;
177 }
178
179 static inline void
180 queue_free(struct libevdev *dev)
181 {
182         free(dev->queue);
183         dev->queue_size = 0;
184         dev->queue_next = 0;
185 }
186
187 static inline int
188 queue_num_elements(struct libevdev *dev)
189 {
190         return dev->queue_next;
191 }
192
193 static inline int
194 queue_size(struct libevdev *dev)
195 {
196         return dev->queue_size;
197 }
198
199 static inline int
200 queue_num_free_elements(struct libevdev *dev)
201 {
202         return dev->queue_size - dev->queue_next - 1;
203 }
204
205 static inline struct input_event *
206 queue_next_element(struct libevdev *dev)
207 {
208         return &dev->queue[dev->queue_next];
209 }
210
211 static inline int
212 queue_set_num_elements(struct libevdev *dev, int nelem)
213 {
214         if (nelem > dev->queue_size)
215                 return 1;
216
217         dev->queue_next = nelem;
218
219         return 0;
220 }
221 #endif
222