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")))
44 const char *signature;
45 const struct wl_interface **types;
52 const struct wl_message *methods;
54 const struct wl_message *events;
58 const struct wl_interface *interface;
59 void (* const * implementation)(void);
64 * wl_list - linked list
66 * The list head is of "struct wl_list" type, and must be initialized
67 * using wl_list_init(). All entries in the list must be of the same
68 * type. The item type must have a "struct wl_list" member. This
69 * member will be initialized by wl_list_insert(). There is no need to
70 * call wl_list_init() on the individual item. To query if the list is
71 * empty in O(1), use wl_list_empty().
73 * Let's call the list reference "struct wl_list foo_list", the item type as
74 * "item_t", and the item member as "struct wl_list link".
76 * The following code will initialize a list:
78 * struct wl_list foo_list;
82 * struct wl_list link;
84 * struct item_t item1, item2, item3;
86 * wl_list_init(&foo_list);
87 * wl_list_insert(&foo_list, &item1.link); Pushes item1 at the head
88 * wl_list_insert(&foo_list, &item2.link); Pushes item2 at the head
89 * wl_list_insert(&item2.link, &item3.link); Pushes item3 after item2
91 * The list now looks like [item2, item3, item1]
93 * Will iterate the list in ascending order:
96 * wl_list_for_each(item, foo_list, link) {
97 * Do_something_with_item(item);
101 struct wl_list *prev;
102 struct wl_list *next;
105 void wl_list_init(struct wl_list *list);
106 void wl_list_insert(struct wl_list *list, struct wl_list *elm);
107 void wl_list_remove(struct wl_list *elm);
108 int wl_list_length(struct wl_list *list);
109 int wl_list_empty(struct wl_list *list);
110 void wl_list_insert_list(struct wl_list *list, struct wl_list *other);
113 #define __wl_container_of(ptr, sample, member) \
114 (__typeof__(sample))((char *)(ptr) - \
115 ((char *)&(sample)->member - (char *)(sample)))
117 #define __wl_container_of(ptr, sample, member) \
118 (void *)((char *)(ptr) - \
119 ((char *)&(sample)->member - (char *)(sample)))
122 #define wl_list_for_each(pos, head, member) \
123 for (pos = 0, pos = __wl_container_of((head)->next, pos, member); \
124 &pos->member != (head); \
125 pos = __wl_container_of(pos->member.next, pos, member))
127 #define wl_list_for_each_safe(pos, tmp, head, member) \
128 for (pos = 0, tmp = 0, \
129 pos = __wl_container_of((head)->next, pos, member), \
130 tmp = __wl_container_of((pos)->member.next, tmp, member); \
131 &pos->member != (head); \
133 tmp = __wl_container_of(pos->member.next, tmp, member))
135 #define wl_list_for_each_reverse(pos, head, member) \
136 for (pos = 0, pos = __wl_container_of((head)->prev, pos, member); \
137 &pos->member != (head); \
138 pos = __wl_container_of(pos->member.prev, pos, member))
140 #define wl_list_for_each_reverse_safe(pos, tmp, head, member) \
141 for (pos = 0, tmp = 0, \
142 pos = __wl_container_of((head)->prev, pos, member), \
143 tmp = __wl_container_of((pos)->member.prev, tmp, member); \
144 &pos->member != (head); \
146 tmp = __wl_container_of(pos->member.prev, tmp, member))
154 #define wl_array_for_each(pos, array) \
155 for (pos = (array)->data; \
156 (const char *) pos < ((const char *) (array)->data + (array)->size); \
159 void wl_array_init(struct wl_array *array);
160 void wl_array_release(struct wl_array *array);
161 void *wl_array_add(struct wl_array *array, size_t size);
162 int wl_array_copy(struct wl_array *array, struct wl_array *source);
164 typedef int32_t wl_fixed_t;
167 wl_fixed_to_double (wl_fixed_t f)
174 u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
176 return u.d - (3LL << 43);
179 static inline wl_fixed_t
180 wl_fixed_from_double(double d)
187 u.d = d + (3LL << (51 - 8));
192 static inline int wl_fixed_to_int(wl_fixed_t f)
196 static inline wl_fixed_t wl_fixed_from_int(int i)
201 typedef void (*wl_log_func_t)(const char *, va_list);