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