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