5d366b02d805f5668174e3ce91e65d68368178aa
[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
29 #include "libinput.h"
30
31 void
32 set_logging_enabled(int enabled);
33
34 void
35 log_info(const char *format, ...);
36
37 /*
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.
40  */
41
42 struct list {
43         struct list *prev;
44         struct list *next;
45 };
46
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);
51
52 #ifdef __GNUC__
53 #define container_of(ptr, sample, member)                               \
54         (__typeof__(sample))((char *)(ptr)      -                       \
55                  ((char *)&(sample)->member - (char *)(sample)))
56 #else
57 #define container_of(ptr, sample, member)                               \
58         (void *)((char *)(ptr)  -                                       \
59                  ((char *)&(sample)->member - (char *)(sample)))
60 #endif
61
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))
66
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);                                    \
72              pos = tmp,                                                 \
73              tmp = container_of(pos->member.next, tmp, member))
74
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++)
80
81 #define min(a, b) (((a) < (b)) ? (a) : (b))
82 #define max(a, b) (((a) > (b)) ? (a) : (b))
83
84 #define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
85
86 static inline void *
87 zalloc(size_t size)
88 {
89         return calloc(1, size);
90 }
91
92 static inline void
93 msleep(unsigned int ms)
94 {
95         usleep(ms * 1000);
96 }
97
98 enum directions {
99         N  = 1 << 0,
100         NE = 1 << 1,
101         E  = 1 << 2,
102         SE = 1 << 3,
103         S  = 1 << 4,
104         SW = 1 << 5,
105         W  = 1 << 6,
106         NW = 1 << 7,
107         UNDEFINED_DIRECTION = 0xff
108 };
109
110 static inline int
111 vector_get_direction(int dx, int dy)
112 {
113         int dir = UNDEFINED_DIRECTION;
114         int d1, d2;
115         double r;
116
117         if (abs(dx) < 2 && abs(dy) < 2) {
118                 if (dx > 0 && dy > 0)
119                         dir = S | SE | E;
120                 else if (dx > 0 && dy < 0)
121                         dir = N | NE | E;
122                 else if (dx < 0 && dy > 0)
123                         dir = S | SW | W;
124                 else if (dx < 0 && dy < 0)
125                         dir = N | NW | W;
126                 else if (dx > 0)
127                         dir = NE | E | SE;
128                 else if (dx < 0)
129                         dir = NW | W | SW;
130                 else if (dy > 0)
131                         dir = SE | S | SW;
132                 else if (dy < 0)
133                         dir = NE | N | NW;
134         } else {
135                 /* Calculate r within the interval  [0 to 8)
136                  *
137                  * r = [0 .. 2π] where 0 is North
138                  * d_f = r / 2π  ([0 .. 1))
139                  * d_8 = 8 * d_f
140                  */
141                 r = atan2(dy, dx);
142                 r = fmod(r + 2.5*M_PI, 2*M_PI);
143                 r *= 4*M_1_PI;
144
145                 /* Mark one or two close enough octants */
146                 d1 = (int)(r + 0.9) % 8;
147                 d2 = (int)(r + 0.1) % 8;
148
149                 dir = (1 << d1) | (1 << d2);
150         }
151
152         return dir;
153 }
154
155 static inline int
156 long_bit_is_set(const unsigned long *array, int bit)
157 {
158     return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
159 }
160
161 static inline void
162 long_set_bit(unsigned long *array, int bit)
163 {
164     array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
165 }
166
167 static inline void
168 long_clear_bit(unsigned long *array, int bit)
169 {
170     array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS));
171 }
172
173 static inline void
174 long_set_bit_state(unsigned long *array, int bit, int state)
175 {
176         if (state)
177                 long_set_bit(array, bit);
178         else
179                 long_clear_bit(array, bit);
180 }
181
182 #endif /* LIBINPUT_UTIL_H */