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 ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
76 #define ARRAY_FOR_EACH(_arr, _elem) \
77 for (size_t _i = 0; (_elem = &_arr[_i]) && _i < ARRAY_LENGTH(_arr); _i++)
79 #define min(a, b) (((a) < (b)) ? (a) : (b))
80 #define max(a, b) (((a) > (b)) ? (a) : (b))
82 #define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
87 return calloc(1, size);
91 msleep(unsigned int ms)
105 UNDEFINED_DIRECTION = 0xff
109 vector_get_direction(int dx, int dy)
111 int dir = UNDEFINED_DIRECTION;
115 if (abs(dx) < 2 && abs(dy) < 2) {
116 if (dx > 0 && dy > 0)
118 else if (dx > 0 && dy < 0)
120 else if (dx < 0 && dy > 0)
122 else if (dx < 0 && dy < 0)
133 /* Calculate r within the interval [0 to 8)
135 * r = [0 .. 2π] where 0 is North
136 * d_f = r / 2π ([0 .. 1))
140 r = fmod(r + 2.5*M_PI, 2*M_PI);
143 /* Mark one or two close enough octants */
144 d1 = (int)(r + 0.9) % 8;
145 d2 = (int)(r + 0.1) % 8;
147 dir = (1 << d1) | (1 << d2);
153 #endif /* LIBINPUT_UTIL_H */