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