touchpad: Only break out of tap FSM for clickpad button presses
[platform/upstream/libinput.git] / src / libinput-util.h
1 /*
2  * Copyright © 2008 Kristian Høgsberg
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 LIBINPUT_UTIL_H
24 #define LIBINPUT_UTIL_H
25
26 #include <unistd.h>
27 #include <math.h>
28 #include <string.h>
29
30 #include "libinput.h"
31
32 void
33 set_logging_enabled(int enabled);
34
35 void
36 log_info(const char *format, ...);
37
38 /*
39  * This list data structure is a verbatim copy from wayland-util.h from the
40  * Wayland project; except that wl_ prefix has been removed.
41  */
42
43 struct list {
44         struct list *prev;
45         struct list *next;
46 };
47
48 void list_init(struct list *list);
49 void list_insert(struct list *list, struct list *elm);
50 void list_remove(struct list *elm);
51 int list_empty(const struct list *list);
52
53 #ifdef __GNUC__
54 #define container_of(ptr, sample, member)                               \
55         (__typeof__(sample))((char *)(ptr)      -                       \
56                  ((char *)&(sample)->member - (char *)(sample)))
57 #else
58 #define container_of(ptr, sample, member)                               \
59         (void *)((char *)(ptr)  -                                       \
60                  ((char *)&(sample)->member - (char *)(sample)))
61 #endif
62
63 #define list_for_each(pos, head, member)                                \
64         for (pos = 0, pos = container_of((head)->next, pos, member);    \
65              &pos->member != (head);                                    \
66              pos = container_of(pos->member.next, pos, member))
67
68 #define list_for_each_safe(pos, tmp, head, member)                      \
69         for (pos = 0, tmp = 0,                                          \
70              pos = container_of((head)->next, pos, member),             \
71              tmp = container_of((pos)->member.next, tmp, member);       \
72              &pos->member != (head);                                    \
73              pos = tmp,                                                 \
74              tmp = container_of(pos->member.next, tmp, member))
75
76 #define LONG_BITS (sizeof(long) * 8)
77 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
78 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
79 #define ARRAY_FOR_EACH(_arr, _elem) \
80         for (size_t _i = 0; _i < ARRAY_LENGTH(_arr) && (_elem = &_arr[_i]); _i++)
81
82 #define min(a, b) (((a) < (b)) ? (a) : (b))
83 #define max(a, b) (((a) > (b)) ? (a) : (b))
84
85 #define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
86
87 static inline void *
88 zalloc(size_t size)
89 {
90         return calloc(1, size);
91 }
92
93 static inline void
94 msleep(unsigned int ms)
95 {
96         usleep(ms * 1000);
97 }
98
99 enum directions {
100         N  = 1 << 0,
101         NE = 1 << 1,
102         E  = 1 << 2,
103         SE = 1 << 3,
104         S  = 1 << 4,
105         SW = 1 << 5,
106         W  = 1 << 6,
107         NW = 1 << 7,
108         UNDEFINED_DIRECTION = 0xff
109 };
110
111 static inline int
112 vector_get_direction(int dx, int dy)
113 {
114         int dir = UNDEFINED_DIRECTION;
115         int d1, d2;
116         double r;
117
118         if (abs(dx) < 2 && abs(dy) < 2) {
119                 if (dx > 0 && dy > 0)
120                         dir = S | SE | E;
121                 else if (dx > 0 && dy < 0)
122                         dir = N | NE | E;
123                 else if (dx < 0 && dy > 0)
124                         dir = S | SW | W;
125                 else if (dx < 0 && dy < 0)
126                         dir = N | NW | W;
127                 else if (dx > 0)
128                         dir = NE | E | SE;
129                 else if (dx < 0)
130                         dir = NW | W | SW;
131                 else if (dy > 0)
132                         dir = SE | S | SW;
133                 else if (dy < 0)
134                         dir = NE | N | NW;
135         } else {
136                 /* Calculate r within the interval  [0 to 8)
137                  *
138                  * r = [0 .. 2π] where 0 is North
139                  * d_f = r / 2π  ([0 .. 1))
140                  * d_8 = 8 * d_f
141                  */
142                 r = atan2(dy, dx);
143                 r = fmod(r + 2.5*M_PI, 2*M_PI);
144                 r *= 4*M_1_PI;
145
146                 /* Mark one or two close enough octants */
147                 d1 = (int)(r + 0.9) % 8;
148                 d2 = (int)(r + 0.1) % 8;
149
150                 dir = (1 << d1) | (1 << d2);
151         }
152
153         return dir;
154 }
155
156 static inline int
157 long_bit_is_set(const unsigned long *array, int bit)
158 {
159     return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
160 }
161
162 static inline void
163 long_set_bit(unsigned long *array, int bit)
164 {
165     array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
166 }
167
168 static inline void
169 long_clear_bit(unsigned long *array, int bit)
170 {
171     array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS));
172 }
173
174 static inline void
175 long_set_bit_state(unsigned long *array, int bit, int state)
176 {
177         if (state)
178                 long_set_bit(array, bit);
179         else
180                 long_clear_bit(array, bit);
181 }
182
183 struct matrix {
184         float val[3][3]; /* [row][col] */
185 };
186
187 static inline void
188 matrix_init_identity(struct matrix *m)
189 {
190         memset(m, 0, sizeof(*m));
191         m->val[0][0] = 1;
192         m->val[1][1] = 1;
193         m->val[2][2] = 1;
194 }
195
196 static inline void
197 matrix_from_farray6(struct matrix *m, const float values[6])
198 {
199         matrix_init_identity(m);
200         m->val[0][0] = values[0];
201         m->val[0][1] = values[1];
202         m->val[0][2] = values[2];
203         m->val[1][0] = values[3];
204         m->val[1][1] = values[4];
205         m->val[1][2] = values[5];
206 }
207
208 static inline void
209 matrix_init_scale(struct matrix *m, float sx, float sy)
210 {
211         matrix_init_identity(m);
212         m->val[0][0] = sx;
213         m->val[1][1] = sy;
214 }
215
216 static inline void
217 matrix_init_translate(struct matrix *m, float x, float y)
218 {
219         matrix_init_identity(m);
220         m->val[0][2] = x;
221         m->val[1][2] = y;
222 }
223
224 static inline int
225 matrix_is_identity(struct matrix *m)
226 {
227         return (m->val[0][0] == 1 &&
228                 m->val[0][1] == 0 &&
229                 m->val[0][2] == 0 &&
230                 m->val[1][0] == 0 &&
231                 m->val[1][1] == 1 &&
232                 m->val[1][2] == 0 &&
233                 m->val[2][0] == 0 &&
234                 m->val[2][1] == 0 &&
235                 m->val[2][2] == 1);
236 }
237
238 static inline void
239 matrix_mult(struct matrix *dest,
240             const struct matrix *m1,
241             const struct matrix *m2)
242 {
243         struct matrix m; /* allow for dest == m1 or dest == m2 */
244         int row, col, i;
245
246         for (row = 0; row < 3; row++) {
247                 for (col = 0; col < 3; col++) {
248                         double v = 0;
249                         for (i = 0; i < 3; i++) {
250                                 v += m1->val[row][i] * m2->val[i][col];
251                         }
252                         m.val[row][col] = v;
253                 }
254         }
255
256         memcpy(dest, &m, sizeof(m));
257 }
258
259 static inline void
260 matrix_mult_vec(struct matrix *m, int *x, int *y)
261 {
262         int tx, ty;
263
264         tx = *x * m->val[0][0] + *y * m->val[0][1] + m->val[0][2];
265         ty = *x * m->val[1][0] + *y * m->val[1][1] + m->val[1][2];
266
267         *x = tx;
268         *y = ty;
269 }
270
271 static inline void
272 matrix_to_farray6(const struct matrix *m, float out[6])
273 {
274         out[0] = m->val[0][0];
275         out[1] = m->val[0][1];
276         out[2] = m->val[0][2];
277         out[3] = m->val[1][0];
278         out[4] = m->val[1][1];
279         out[5] = m->val[1][2];
280 }
281
282 #endif /* LIBINPUT_UTIL_H */