7c8f563c0b70aabd35e38763f0bf74ba307ba9a7
[profile/ivi/wayland.git] / src / wayland-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 WAYLAND_UTIL_H
24 #define WAYLAND_UTIL_H
25
26 #ifdef  __cplusplus
27 extern "C" {
28 #endif
29
30 #include <math.h>
31 #include <stddef.h>
32 #include <inttypes.h>
33 #include <stdarg.h>
34
35 /* GCC visibility */
36 #if defined(__GNUC__) && __GNUC__ >= 4
37 #define WL_EXPORT __attribute__ ((visibility("default")))
38 #else
39 #define WL_EXPORT
40 #endif
41
42 #define container_of(ptr, type, member) ({                              \
43         const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
44         (type *)( (char *)__mptr - offsetof(type,member) );})
45
46 struct wl_message {
47         const char *name;
48         const char *signature;
49         const struct wl_interface **types;
50 };
51
52 struct wl_interface {
53         const char *name;
54         int version;
55         int method_count;
56         const struct wl_message *methods;
57         int event_count;
58         const struct wl_message *events;
59 };
60
61 struct wl_object {
62         const struct wl_interface *interface;
63         void (* const * implementation)(void);
64         uint32_t id;
65 };
66
67 /**
68  * wl_list - linked list
69  *
70  * The list head is of "struct wl_list" type, and must be initialized
71  * using wl_list_init().  All entries in the list must be of the same
72  * type.  The item type must have a "struct wl_list" member. This
73  * member will be initialized by wl_list_insert(). There is no need to
74  * call wl_list_init() on the individual item. To query if the list is
75  * empty in O(1), use wl_list_empty().
76  *
77  * Let's call the list reference "struct wl_list foo_list", the item type as
78  * "item_t", and the item member as "struct wl_list link".
79  *
80  * The following code will initialize a list:
81  *
82  *      struct wl_list foo_list;
83  *
84  *      struct item_t {
85  *              int foo;
86  *              struct wl_list link;
87  *      };
88  *      struct item_t item1, item2, item3;
89  *
90  *      wl_list_init(&foo_list);
91  *      wl_list_insert(&foo_list, &item1.link); Pushes item1 at the head
92  *      wl_list_insert(&foo_list, &item2.link); Pushes item2 at the head
93  *      wl_list_insert(&item2.link, &item3.link); Pushes item3 after item2
94  *
95  * The list now looks like [item2, item3, item1]
96  *
97  * Will iterate the list in ascending order:
98  *
99  *      item_t *item;
100  *      wl_list_for_each(item, foo_list, link) {
101  *              Do_something_with_item(item);
102  *      }
103  */
104 struct wl_list {
105         struct wl_list *prev;
106         struct wl_list *next;
107 };
108
109 void wl_list_init(struct wl_list *list);
110 void wl_list_insert(struct wl_list *list, struct wl_list *elm);
111 void wl_list_remove(struct wl_list *elm);
112 int wl_list_length(struct wl_list *list);
113 int wl_list_empty(struct wl_list *list);
114 void wl_list_insert_list(struct wl_list *list, struct wl_list *other);
115
116 #ifdef __GNUC__
117 #define __wl_container_of(ptr, sample, member)                          \
118         (__typeof__(sample))((char *)(ptr)      -                       \
119                  ((char *)&(sample)->member - (char *)(sample)))
120 #else
121 #define __wl_container_of(ptr, sample, member)                          \
122         (void *)((char *)(ptr)  -                                       \
123                  ((char *)&(sample)->member - (char *)(sample)))
124 #endif
125
126 #define wl_list_for_each(pos, head, member)                             \
127         for (pos = 0, pos = __wl_container_of((head)->next, pos, member);       \
128              &pos->member != (head);                                    \
129              pos = __wl_container_of(pos->member.next, pos, member))
130
131 #define wl_list_for_each_safe(pos, tmp, head, member)                   \
132         for (pos = 0, tmp = 0,                                          \
133              pos = __wl_container_of((head)->next, pos, member),                \
134              tmp = __wl_container_of((pos)->member.next, tmp, member);  \
135              &pos->member != (head);                                    \
136              pos = tmp,                                                 \
137              tmp = __wl_container_of(pos->member.next, tmp, member))
138
139 #define wl_list_for_each_reverse(pos, head, member)                     \
140         for (pos = 0, pos = __wl_container_of((head)->prev, pos, member);       \
141              &pos->member != (head);                                    \
142              pos = __wl_container_of(pos->member.prev, pos, member))
143
144 #define wl_list_for_each_reverse_safe(pos, tmp, head, member)           \
145         for (pos = 0, tmp = 0,                                          \
146              pos = __wl_container_of((head)->prev, pos, member),        \
147              tmp = __wl_container_of((pos)->member.prev, tmp, member);  \
148              &pos->member != (head);                                    \
149              pos = tmp,                                                 \
150              tmp = __wl_container_of(pos->member.prev, tmp, member))
151
152 struct wl_array {
153         size_t size;
154         size_t alloc;
155         void *data;
156 };
157
158 #define wl_array_for_each(pos, array)                                   \
159         for (pos = (array)->data;                                       \
160              (const char *) pos < ((const char *) (array)->data + (array)->size); \
161              (pos)++)
162
163 void wl_array_init(struct wl_array *array);
164 void wl_array_release(struct wl_array *array);
165 void *wl_array_add(struct wl_array *array, size_t size);
166 int wl_array_copy(struct wl_array *array, struct wl_array *source);
167
168 typedef int32_t wl_fixed_t;
169
170 static inline double
171 wl_fixed_to_double (wl_fixed_t f)
172 {
173         union {
174                 double d;
175                 int64_t i;
176         } u;
177
178         u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
179
180         return u.d - (3LL << 43);
181 }
182
183 static inline wl_fixed_t
184 wl_fixed_from_double(double d)
185 {
186         union {
187                 double d;
188                 int64_t i;
189         } u;
190
191         u.d = d + (3LL << (51 - 8));
192
193         return u.i;
194 }
195
196 static inline int wl_fixed_to_int(wl_fixed_t f)
197 {
198         return f / 256;
199 }
200 static inline wl_fixed_t wl_fixed_from_int(int i)
201 {
202         return i * 256;
203 }
204
205 typedef void (*wl_log_func_t)(const char *, va_list);
206
207 #ifdef  __cplusplus
208 }
209 #endif
210
211 #endif