Fix two doxygen errors
[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 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++)
78
79 #define min(a, b) (((a) < (b)) ? (a) : (b))
80 #define max(a, b) (((a) > (b)) ? (a) : (b))
81
82 #define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
83
84 static inline void *
85 zalloc(size_t size)
86 {
87         return calloc(1, size);
88 }
89
90 static inline void
91 msleep(unsigned int ms)
92 {
93         usleep(ms * 1000);
94 }
95
96 enum directions {
97         N  = 1 << 0,
98         NE = 1 << 1,
99         E  = 1 << 2,
100         SE = 1 << 3,
101         S  = 1 << 4,
102         SW = 1 << 5,
103         W  = 1 << 6,
104         NW = 1 << 7,
105         UNDEFINED_DIRECTION = 0xff
106 };
107
108 static inline int
109 vector_get_direction(int dx, int dy)
110 {
111         int dir = UNDEFINED_DIRECTION;
112         int d1, d2;
113         double r;
114
115         if (abs(dx) < 2 && abs(dy) < 2) {
116                 if (dx > 0 && dy > 0)
117                         dir = S | SE | E;
118                 else if (dx > 0 && dy < 0)
119                         dir = N | NE | E;
120                 else if (dx < 0 && dy > 0)
121                         dir = S | SW | W;
122                 else if (dx < 0 && dy < 0)
123                         dir = N | NW | W;
124                 else if (dx > 0)
125                         dir = NE | E | SE;
126                 else if (dx < 0)
127                         dir = NW | W | SW;
128                 else if (dy > 0)
129                         dir = SE | S | SW;
130                 else if (dy < 0)
131                         dir = NE | N | NW;
132         } else {
133                 /* Calculate r within the interval  [0 to 8)
134                  *
135                  * r = [0 .. 2π] where 0 is North
136                  * d_f = r / 2π  ([0 .. 1))
137                  * d_8 = 8 * d_f
138                  */
139                 r = atan2(dy, dx);
140                 r = fmod(r + 2.5*M_PI, 2*M_PI);
141                 r *= 4*M_1_PI;
142
143                 /* Mark one or two close enough octants */
144                 d1 = (int)(r + 0.9) % 8;
145                 d2 = (int)(r + 0.1) % 8;
146
147                 dir = (1 << d1) | (1 << d2);
148         }
149
150         return dir;
151 }
152
153 #endif /* LIBINPUT_UTIL_H */