2 * Copyright © 2008 Kristian Høgsberg
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.
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
23 #ifndef LIBINPUT_UTIL_H
24 #define LIBINPUT_UTIL_H
33 set_logging_enabled(int enabled);
36 log_info(const char *format, ...);
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.
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);
54 #define container_of(ptr, sample, member) \
55 (__typeof__(sample))((char *)(ptr) - \
56 ((char *)&(sample)->member - (char *)(sample)))
58 #define container_of(ptr, sample, member) \
59 (void *)((char *)(ptr) - \
60 ((char *)&(sample)->member - (char *)(sample)))
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))
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); \
74 tmp = container_of(pos->member.next, tmp, member))
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++)
82 #define min(a, b) (((a) < (b)) ? (a) : (b))
83 #define max(a, b) (((a) > (b)) ? (a) : (b))
85 #define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
90 return calloc(1, size);
94 msleep(unsigned int ms)
108 UNDEFINED_DIRECTION = 0xff
112 vector_get_direction(int dx, int dy)
114 int dir = UNDEFINED_DIRECTION;
118 if (abs(dx) < 2 && abs(dy) < 2) {
119 if (dx > 0 && dy > 0)
121 else if (dx > 0 && dy < 0)
123 else if (dx < 0 && dy > 0)
125 else if (dx < 0 && dy < 0)
136 /* Calculate r within the interval [0 to 8)
138 * r = [0 .. 2π] where 0 is North
139 * d_f = r / 2π ([0 .. 1))
143 r = fmod(r + 2.5*M_PI, 2*M_PI);
146 /* Mark one or two close enough octants */
147 d1 = (int)(r + 0.9) % 8;
148 d2 = (int)(r + 0.1) % 8;
150 dir = (1 << d1) | (1 << d2);
157 long_bit_is_set(const unsigned long *array, int bit)
159 return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
163 long_set_bit(unsigned long *array, int bit)
165 array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
169 long_clear_bit(unsigned long *array, int bit)
171 array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS));
175 long_set_bit_state(unsigned long *array, int bit, int state)
178 long_set_bit(array, bit);
180 long_clear_bit(array, bit);
184 float val[3][3]; /* [row][col] */
188 matrix_init_identity(struct matrix *m)
190 memset(m, 0, sizeof(*m));
197 matrix_from_farray6(struct matrix *m, const float values[6])
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];
209 matrix_init_scale(struct matrix *m, float sx, float sy)
211 matrix_init_identity(m);
217 matrix_init_translate(struct matrix *m, float x, float y)
219 matrix_init_identity(m);
225 matrix_is_identity(struct matrix *m)
227 return (m->val[0][0] == 1 &&
239 matrix_mult(struct matrix *dest,
240 const struct matrix *m1,
241 const struct matrix *m2)
243 struct matrix m; /* allow for dest == m1 or dest == m2 */
246 for (row = 0; row < 3; row++) {
247 for (col = 0; col < 3; col++) {
249 for (i = 0; i < 3; i++) {
250 v += m1->val[row][i] * m2->val[i][col];
256 memcpy(dest, &m, sizeof(m));
260 matrix_mult_vec(struct matrix *m, int *x, int *y)
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];
272 matrix_to_farray6(const struct matrix *m, float out[6])
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];
282 #endif /* LIBINPUT_UTIL_H */