Abstract the event queue away
[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 struct libevdev {
39         int fd;
40         libevdev_log_func_t log;
41
42         char name[MAX_NAME];
43         struct input_id ids;
44         unsigned long bits[NLONGS(EV_CNT)];
45         unsigned long props[NLONGS(INPUT_PROP_CNT)];
46         unsigned long key_bits[NLONGS(KEY_CNT)];
47         unsigned long rel_bits[NLONGS(REL_CNT)];
48         unsigned long abs_bits[NLONGS(ABS_CNT)];
49         unsigned long led_bits[NLONGS(LED_CNT)];
50         unsigned long key_values[NLONGS(KEY_CNT)];
51         struct input_absinfo abs_info[ABS_CNT];
52         unsigned int mt_slot_vals[MAX_SLOTS][ABS_MT_CNT];
53         int num_slots; /**< valid slots in mt_slot_vals */
54
55         int need_sync;
56         int grabbed;
57
58         struct input_event *queue;
59         size_t queue_size; /**< size of queue in elements */
60         size_t queue_next; /**< next event index */
61         size_t queue_nsync; /**< number of sync events */
62 };
63
64 /**
65  * @return a pointer to the next element in the queue, or NULL if the queue
66  * is full.
67  */
68 static inline struct input_event*
69 queue_push(struct libevdev *dev)
70 {
71         if (dev->queue_next >= dev->queue_size)
72                 return NULL;
73
74         return &dev->queue[dev->queue_next++];
75 }
76
77 /**
78  * Set ev to the last element in the queue, removing it from the queue.
79  *
80  * @return 0 on success, 1 if the queue is empty.
81  */
82 static inline int
83 queue_pop(struct libevdev *dev, struct input_event *ev)
84 {
85         if (dev->queue_next == 0)
86                 return 1;
87
88         *ev = dev->queue[--dev->queue_next];
89
90         return 0;
91 }
92
93 /**
94  * Set ev to the first element in the queue, shifting everything else
95  * forward by one.
96  *
97  * @return 0 on success, 1 if the queue is empty.
98  */
99 static inline int
100 queue_shift(struct libevdev *dev, struct input_event *ev)
101 {
102         int i;
103
104         if (dev->queue_next == 0)
105                 return 1;
106
107         *ev = dev->queue[0];
108
109         for (i = 0; i < dev->queue_next - 1; i++)
110                 dev->queue[i] = dev->queue[i + 1];
111
112         dev->queue_next--;
113
114         return 0;
115 }
116
117 static inline int
118 queue_alloc(struct libevdev *dev, int size)
119 {
120         dev->queue = calloc(size, sizeof(struct input_event));
121         if (!dev->queue)
122                 return -ENOSPC;
123
124         dev->queue_size = size;
125         dev->queue_next = 0;
126         return 0;
127 }
128
129 static inline int
130 queue_num_elements(struct libevdev *dev)
131 {
132         return dev->queue_next;
133 }
134
135 static inline int
136 queue_size(struct libevdev *dev)
137 {
138         return dev->queue_size;
139 }
140
141 static inline int
142 queue_num_free_elements(struct libevdev *dev)
143 {
144         return dev->queue_size - dev->queue_next - 1;
145 }
146
147 static inline struct input_event *
148 queue_next_element(struct libevdev *dev)
149 {
150         return &dev->queue[dev->queue_next];
151 }
152
153 static inline int
154 queue_set_num_elements(struct libevdev *dev, int nelem)
155 {
156         if (nelem > dev->queue_size)
157                 return 1;
158
159         dev->queue_next = nelem;
160
161         return 0;
162 }
163 #endif
164