2 * Copyright © 2008 Kristian Høgsberg
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.
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
23 #ifndef WAYLAND_UTIL_H
24 #define WAYLAND_UTIL_H
36 #if defined(__GNUC__) && __GNUC__ >= 4
37 #define WL_EXPORT __attribute__ ((visibility("default")))
42 #define container_of(ptr, type, member) ({ \
43 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
44 (type *)( (char *)__mptr - offsetof(type,member) );})
48 const char *signature;
49 const struct wl_interface **types;
56 const struct wl_message *methods;
58 const struct wl_message *events;
62 const struct wl_interface *interface;
63 void (* const * implementation)(void);
68 * wl_list - linked list
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().
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".
80 * The following code will initialize a list:
82 * struct wl_list foo_list;
86 * struct wl_list link;
88 * struct item_t item1, item2, item3;
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
95 * The list now looks like [item2, item3, item1]
97 * Will iterate the list in ascending order:
100 * wl_list_for_each(item, foo_list, link) {
101 * Do_something_with_item(item);
105 struct wl_list *prev;
106 struct wl_list *next;
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);
117 #define __wl_container_of(ptr, sample, member) \
118 (__typeof__(sample))((char *)(ptr) - \
119 ((char *)&(sample)->member - (char *)(sample)))
121 #define __wl_container_of(ptr, sample, member) \
122 (void *)((char *)(ptr) - \
123 ((char *)&(sample)->member - (char *)(sample)))
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))
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); \
137 tmp = __wl_container_of(pos->member.next, tmp, member))
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))
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); \
150 tmp = __wl_container_of(pos->member.prev, tmp, member))
158 #define wl_array_for_each(pos, array) \
159 for (pos = (array)->data; \
160 (const char *) pos < ((const char *) (array)->data + (array)->size); \
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);
168 typedef int32_t wl_fixed_t;
171 wl_fixed_to_double (wl_fixed_t f)
178 u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
180 return u.d - (3LL << 43);
183 static inline wl_fixed_t
184 wl_fixed_from_double(double d)
191 u.d = d + (3LL << (51 - 8));
196 static inline int wl_fixed_to_int(wl_fixed_t f)
200 static inline wl_fixed_t wl_fixed_from_int(int i)
205 typedef void (*wl_log_func_t)(const char *, va_list);