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
32 set_logging_enabled(int enabled);
35 log_info(const char *format, ...);
38 * This list data structure is a verbatim copy from wayland-util.h from the
39 * Wayland project; except that wl_ prefix has been removed.
47 void list_init(struct list *list);
48 void list_insert(struct list *list, struct list *elm);
49 void list_remove(struct list *elm);
50 int list_empty(const struct list *list);
53 #define container_of(ptr, sample, member) \
54 (__typeof__(sample))((char *)(ptr) - \
55 ((char *)&(sample)->member - (char *)(sample)))
57 #define container_of(ptr, sample, member) \
58 (void *)((char *)(ptr) - \
59 ((char *)&(sample)->member - (char *)(sample)))
62 #define list_for_each(pos, head, member) \
63 for (pos = 0, pos = container_of((head)->next, pos, member); \
64 &pos->member != (head); \
65 pos = container_of(pos->member.next, pos, member))
67 #define list_for_each_safe(pos, tmp, head, member) \
68 for (pos = 0, tmp = 0, \
69 pos = container_of((head)->next, pos, member), \
70 tmp = container_of((pos)->member.next, tmp, member); \
71 &pos->member != (head); \
73 tmp = container_of(pos->member.next, tmp, member))
75 #define LONG_BITS (sizeof(long) * 8)
76 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
77 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
78 #define ARRAY_FOR_EACH(_arr, _elem) \
79 for (size_t _i = 0; _i < ARRAY_LENGTH(_arr) && (_elem = &_arr[_i]); _i++)
81 #define min(a, b) (((a) < (b)) ? (a) : (b))
82 #define max(a, b) (((a) > (b)) ? (a) : (b))
84 #define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
89 return calloc(1, size);
93 msleep(unsigned int ms)
107 UNDEFINED_DIRECTION = 0xff
111 vector_get_direction(int dx, int dy)
113 int dir = UNDEFINED_DIRECTION;
117 if (abs(dx) < 2 && abs(dy) < 2) {
118 if (dx > 0 && dy > 0)
120 else if (dx > 0 && dy < 0)
122 else if (dx < 0 && dy > 0)
124 else if (dx < 0 && dy < 0)
135 /* Calculate r within the interval [0 to 8)
137 * r = [0 .. 2π] where 0 is North
138 * d_f = r / 2π ([0 .. 1))
142 r = fmod(r + 2.5*M_PI, 2*M_PI);
145 /* Mark one or two close enough octants */
146 d1 = (int)(r + 0.9) % 8;
147 d2 = (int)(r + 0.1) % 8;
149 dir = (1 << d1) | (1 << d2);
156 long_bit_is_set(const unsigned long *array, int bit)
158 return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
162 long_set_bit(unsigned long *array, int bit)
164 array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
168 long_clear_bit(unsigned long *array, int bit)
170 array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS));
174 long_set_bit_state(unsigned long *array, int bit, int state)
177 long_set_bit(array, bit);
179 long_clear_bit(array, bit);
182 #endif /* LIBINPUT_UTIL_H */