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