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
34 set_logging_enabled(int enabled);
37 log_info(const char *format, ...);
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.
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);
55 #define container_of(ptr, sample, member) \
56 (__typeof__(sample))((char *)(ptr) - \
57 ((char *)&(sample)->member - (char *)(sample)))
59 #define container_of(ptr, sample, member) \
60 (void *)((char *)(ptr) - \
61 ((char *)&(sample)->member - (char *)(sample)))
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))
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); \
75 tmp = container_of(pos->member.next, tmp, member))
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++)
83 #define min(a, b) (((a) < (b)) ? (a) : (b))
84 #define max(a, b) (((a) > (b)) ? (a) : (b))
86 #define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
91 return calloc(1, size);
95 msleep(unsigned int ms)
109 UNDEFINED_DIRECTION = 0xff
113 vector_get_direction(int dx, int dy)
115 int dir = UNDEFINED_DIRECTION;
119 if (abs(dx) < 2 && abs(dy) < 2) {
120 if (dx > 0 && dy > 0)
122 else if (dx > 0 && dy < 0)
124 else if (dx < 0 && dy > 0)
126 else if (dx < 0 && dy < 0)
137 /* Calculate r within the interval [0 to 8)
139 * r = [0 .. 2π] where 0 is North
140 * d_f = r / 2π ([0 .. 1))
144 r = fmod(r + 2.5*M_PI, 2*M_PI);
147 /* Mark one or two close enough octants */
148 d1 = (int)(r + 0.9) % 8;
149 d2 = (int)(r + 0.1) % 8;
151 dir = (1 << d1) | (1 << d2);
158 long_bit_is_set(const unsigned long *array, int bit)
160 return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
164 long_set_bit(unsigned long *array, int bit)
166 array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
170 long_clear_bit(unsigned long *array, int bit)
172 array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS));
176 long_set_bit_state(unsigned long *array, int bit, int state)
179 long_set_bit(array, bit);
181 long_clear_bit(array, bit);
185 float val[3][3]; /* [row][col] */
189 matrix_init_identity(struct matrix *m)
191 memset(m, 0, sizeof(*m));
198 matrix_from_farray6(struct matrix *m, const float values[6])
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];
210 matrix_init_scale(struct matrix *m, float sx, float sy)
212 matrix_init_identity(m);
218 matrix_init_translate(struct matrix *m, float x, float y)
220 matrix_init_identity(m);
226 matrix_is_identity(struct matrix *m)
228 return (m->val[0][0] == 1 &&
240 matrix_mult(struct matrix *dest,
241 const struct matrix *m1,
242 const struct matrix *m2)
244 struct matrix m; /* allow for dest == m1 or dest == m2 */
247 for (row = 0; row < 3; row++) {
248 for (col = 0; col < 3; col++) {
250 for (i = 0; i < 3; i++) {
251 v += m1->val[row][i] * m2->val[i][col];
257 memcpy(dest, &m, sizeof(m));
261 matrix_mult_vec(struct matrix *m, int *x, int *y)
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];
273 matrix_to_farray6(const struct matrix *m, float out[6])
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];
283 #endif /* LIBINPUT_UTIL_H */