Introduce libinput object managing all input data
[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 /*
27  * This list data structure is a verbatim copy from wayland-util.h from the
28  * Wayland project; except that wl_ prefix has been removed.
29  */
30
31 struct list {
32         struct list *prev;
33         struct list *next;
34 };
35
36 void list_init(struct list *list);
37 void list_insert(struct list *list, struct list *elm);
38 void list_remove(struct list *elm);
39 int list_empty(const struct list *list);
40
41 #ifdef __GNUC__
42 #define container_of(ptr, sample, member)                               \
43         (__typeof__(sample))((char *)(ptr)      -                       \
44                  ((char *)&(sample)->member - (char *)(sample)))
45 #else
46 #define container_of(ptr, sample, member)                               \
47         (void *)((char *)(ptr)  -                                       \
48                  ((char *)&(sample)->member - (char *)(sample)))
49 #endif
50
51 #define list_for_each(pos, head, member)                                \
52         for (pos = 0, pos = container_of((head)->next, pos, member);    \
53              &pos->member != (head);                                    \
54              pos = container_of(pos->member.next, pos, member))
55
56 #define list_for_each_safe(pos, tmp, head, member)                      \
57         for (pos = 0, tmp = 0,                                          \
58              pos = container_of((head)->next, pos, member),             \
59              tmp = container_of((pos)->member.next, tmp, member);       \
60              &pos->member != (head);                                    \
61              pos = tmp,                                                 \
62              tmp = container_of(pos->member.next, tmp, member))
63
64 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
65
66 /*
67  * This fixed point implementation is a verbatim copy from wayland-util.h from
68  * the Wayland project, with the wl_ prefix renamed li_.
69  */
70
71 static inline li_fixed_t li_fixed_from_int(int i)
72 {
73         return i * 256;
74 }
75
76 static inline li_fixed_t
77 li_fixed_from_double(double d)
78 {
79         union {
80                 double d;
81                 int64_t i;
82         } u;
83
84         u.d = d + (3LL << (51 - 8));
85
86         return u.i;
87 }
88
89 #define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
90
91 #endif /* LIBINPUT_UTIL_H */