Keep the LED state and sync it after SYN_DROPPED
[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 /**
55  * Sync state machine:
56  * default state: SYNC_NONE
57  *
58  * SYNC_NONE → SYN_DROPPED or forced sync → SYNC_NEEDED
59  * SYNC_NEEDED → libevdev_next_event(LIBEVDEV_READ_SYNC) → SYNC_IN_PROGRESS
60  * SYNC_NEEDED → libevdev_next_event(LIBEVDEV_READ_SYNC_NONE) → SYNC_NONE
61  * SYNC_IN_PROGRESS → libevdev_next_event(LIBEVDEV_READ_SYNC_NONE) → SYNC_NONE
62  * SYNC_IN_PROGRESS → no sync events left → SYNC_NONE
63  *
64  */
65 enum SyncState {
66         SYNC_NONE,
67         SYNC_NEEDED,
68         SYNC_IN_PROGRESS,
69 };
70
71 struct libevdev {
72         int fd;
73         libevdev_log_func_t log;
74
75         char *name;
76         char *phys;
77         char *uniq;
78         struct input_id ids;
79         int driver_version;
80         unsigned long bits[NLONGS(EV_CNT)];
81         unsigned long props[NLONGS(INPUT_PROP_CNT)];
82         unsigned long key_bits[NLONGS(KEY_CNT)];
83         unsigned long rel_bits[NLONGS(REL_CNT)];
84         unsigned long abs_bits[NLONGS(ABS_CNT)];
85         unsigned long led_bits[NLONGS(LED_CNT)];
86         unsigned long msc_bits[NLONGS(MSC_CNT)];
87         unsigned long sw_bits[NLONGS(SW_CNT)];
88         unsigned long rep_bits[NLONGS(REP_CNT)]; /* convenience, always 1 */
89         unsigned long ff_bits[NLONGS(FF_CNT)];
90         unsigned long snd_bits[NLONGS(SND_CNT)];
91         unsigned long key_values[NLONGS(KEY_CNT)];
92         unsigned long led_values[NLONGS(LED_CNT)];
93         struct input_absinfo abs_info[ABS_CNT];
94         unsigned int mt_slot_vals[MAX_SLOTS][ABS_MT_CNT];
95         int num_slots; /**< valid slots in mt_slot_vals */
96         int current_slot;
97         int rep_values[REP_CNT];
98
99         enum SyncState sync_state;
100         int grabbed;
101
102         struct input_event *queue;
103         size_t queue_size; /**< size of queue in elements */
104         size_t queue_next; /**< next event index */
105         size_t queue_nsync; /**< number of sync events */
106
107         struct timeval last_event_time;
108 };
109
110 /**
111  * @return a pointer to the next element in the queue, or NULL if the queue
112  * is full.
113  */
114 static inline struct input_event*
115 queue_push(struct libevdev *dev)
116 {
117         if (dev->queue_next >= dev->queue_size)
118                 return NULL;
119
120         return &dev->queue[dev->queue_next++];
121 }
122
123 /**
124  * Set ev to the last element in the queue, removing it from the queue.
125  *
126  * @return 0 on success, 1 if the queue is empty.
127  */
128 static inline int
129 queue_pop(struct libevdev *dev, struct input_event *ev)
130 {
131         if (dev->queue_next == 0)
132                 return 1;
133
134         *ev = dev->queue[--dev->queue_next];
135
136         return 0;
137 }
138
139 static inline int
140 queue_peek(struct libevdev *dev, size_t idx, struct input_event *ev)
141 {
142         if (dev->queue_next == 0 || idx > dev->queue_next)
143                 return 1;
144         *ev = dev->queue[idx];
145         return 0;
146 }
147
148
149 /**
150  * Shift the first n elements into ev and return the number of elements
151  * shifted.
152  * ev must be large enough to store n elements.
153  *
154  * @param ev The buffer to copy into, or NULL
155  * @return The number of elements in ev.
156  */
157 static inline int
158 queue_shift_multiple(struct libevdev *dev, size_t n, struct input_event *ev)
159 {
160         size_t i;
161
162         if (dev->queue_next == 0)
163                 return 0;
164
165         n = min(n, dev->queue_next);
166
167         if (ev) {
168                 for (i = 0; i < n; i++)
169                         ev[i] = dev->queue[i];
170         }
171
172         for (i = 0; i < dev->queue_next - n; i++)
173                 dev->queue[i] = dev->queue[n + i];
174
175         dev->queue_next -= n;
176         return n;
177 }
178
179 /**
180  * Set ev to the first element in the queue, shifting everything else
181  * forward by one.
182  *
183  * @return 0 on success, 1 if the queue is empty.
184  */
185 static inline int
186 queue_shift(struct libevdev *dev, struct input_event *ev)
187 {
188         return queue_shift_multiple(dev, 1, ev) == 1 ? 0 : 1;
189 }
190
191 static inline int
192 queue_alloc(struct libevdev *dev, size_t size)
193 {
194         if (size == 0)
195                 return -ENOSPC;
196
197         dev->queue = calloc(size, sizeof(struct input_event));
198         if (!dev->queue)
199                 return -ENOSPC;
200
201         dev->queue_size = size;
202         dev->queue_next = 0;
203         return 0;
204 }
205
206 static inline void
207 queue_free(struct libevdev *dev)
208 {
209         free(dev->queue);
210         dev->queue_size = 0;
211         dev->queue_next = 0;
212 }
213
214 static inline size_t
215 queue_num_elements(struct libevdev *dev)
216 {
217         return dev->queue_next;
218 }
219
220 static inline size_t
221 queue_size(struct libevdev *dev)
222 {
223         return dev->queue_size;
224 }
225
226 static inline size_t
227 queue_num_free_elements(struct libevdev *dev)
228 {
229         if (dev->queue_size == 0)
230                 return 0;
231
232         return dev->queue_size - dev->queue_next;
233 }
234
235 static inline struct input_event *
236 queue_next_element(struct libevdev *dev)
237 {
238         if (dev->queue_next == dev->queue_size)
239                 return NULL;
240
241         return &dev->queue[dev->queue_next];
242 }
243
244 static inline int
245 queue_set_num_elements(struct libevdev *dev, size_t nelem)
246 {
247         if (nelem > dev->queue_size)
248                 return 1;
249
250         dev->queue_next = nelem;
251
252         return 0;
253 }
254 #endif
255