Warning: That file was not part of the compilation database. It may have many parsing errors.

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/** \file wayland-util.h
24 *
25 * \brief Utility classes, functions, and macros.
26 */
27
28#ifndef WAYLAND_UTIL_H
29#define WAYLAND_UTIL_H
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#include <math.h>
36#include <stddef.h>
37#include <inttypes.h>
38#include <stdarg.h>
39
40/* GCC visibility */
41#if defined(__GNUC__) && __GNUC__ >= 4
42#define WL_EXPORT __attribute__ ((visibility("default")))
43#else
44#define WL_EXPORT
45#endif
46
47/* Deprecated attribute */
48#if defined(__GNUC__) && __GNUC__ >= 4
49#define WL_DEPRECATED __attribute__ ((deprecated))
50#else
51#define WL_DEPRECATED
52#endif
53
54/* Printf annotation */
55#if defined(__GNUC__) && __GNUC__ >= 4
56#define WL_PRINTF(x, y) __attribute__((__format__(__printf__, x, y)))
57#else
58#define WL_PRINTF(x, y)
59#endif
60
61struct wl_message {
62 const char *name;
63 const char *signature;
64 const struct wl_interface **types;
65};
66
67struct wl_interface {
68 const char *name;
69 int version;
70 int method_count;
71 const struct wl_message *methods;
72 int event_count;
73 const struct wl_message *events;
74};
75
76/** \class wl_list
77 *
78 * \brief doubly-linked list
79 *
80 * The list head is of "struct wl_list" type, and must be initialized
81 * using wl_list_init(). All entries in the list must be of the same
82 * type. The item type must have a "struct wl_list" member. This
83 * member will be initialized by wl_list_insert(). There is no need to
84 * call wl_list_init() on the individual item. To query if the list is
85 * empty in O(1), use wl_list_empty().
86 *
87 * Let's call the list reference "struct wl_list foo_list", the item type as
88 * "item_t", and the item member as "struct wl_list link".
89 *
90 * The following code will initialize a list:
91 *
92 * struct wl_list foo_list;
93 *
94 * struct item_t {
95 * int foo;
96 * struct wl_list link;
97 * };
98 * struct item_t item1, item2, item3;
99 *
100 * wl_list_init(&foo_list);
101 * wl_list_insert(&foo_list, &item1.link); Pushes item1 at the head
102 * wl_list_insert(&foo_list, &item2.link); Pushes item2 at the head
103 * wl_list_insert(&item2.link, &item3.link); Pushes item3 after item2
104 *
105 * The list now looks like [item2, item3, item1]
106 *
107 * Will iterate the list in ascending order:
108 *
109 * item_t *item;
110 * wl_list_for_each(item, foo_list, link) {
111 * Do_something_with_item(item);
112 * }
113 */
114struct wl_list {
115 struct wl_list *prev;
116 struct wl_list *next;
117};
118
119void wl_list_init(struct wl_list *list);
120void wl_list_insert(struct wl_list *list, struct wl_list *elm);
121void wl_list_remove(struct wl_list *elm);
122int wl_list_length(const struct wl_list *list);
123int wl_list_empty(const struct wl_list *list);
124void wl_list_insert_list(struct wl_list *list, struct wl_list *other);
125
126/**
127 * Retrieves a pointer to the containing struct of a given member item.
128 *
129 * This macro allows conversion from a pointer to a item to its containing
130 * struct. This is useful if you have a contained item like a wl_list,
131 * wl_listener, or wl_signal, provided via a callback or other means and would
132 * like to retrieve the struct that contains it.
133 *
134 * To demonstrate, the following example retrieves a pointer to
135 * `example_container` given only its `destroy_listener` member:
136 *
137 * ~~~
138 * struct example_container {
139 * struct wl_listener destroy_listener;
140 * \comment{other members...}
141 * };
142 *
143 * void example_container_destroy(struct wl_listener *listener, void *data)
144 * {
145 * struct example_container *ctr;
146 *
147 * ctr = wl_container_of(listener, ctr, destroy_listener);
148 * \comment{destroy ctr...}
149 * }
150 * ~~~
151 *
152 * \param ptr A valid pointer to the contained item.
153 *
154 * \param sample A pointer to the type of content that the list item
155 * stores. Sample does not need be a valid pointer; a null or
156 * an uninitialised pointer will suffice.
157 *
158 * \param member The named location of ptr within the sample type.
159 *
160 * \return The container for the specified pointer.
161 */
162#define wl_container_of(ptr, sample, member) \
163 (__typeof__(sample))((char *)(ptr) - \
164 offsetof(__typeof__(*sample), member))
165/* If the above macro causes problems on your compiler you might be
166 * able to find an alternative name for the non-standard __typeof__
167 * operator and add a special case here */
168
169#define wl_list_for_each(pos, head, member) \
170 for (pos = wl_container_of((head)->next, pos, member); \
171 &pos->member != (head); \
172 pos = wl_container_of(pos->member.next, pos, member))
173
174#define wl_list_for_each_safe(pos, tmp, head, member) \
175 for (pos = wl_container_of((head)->next, pos, member), \
176 tmp = wl_container_of((pos)->member.next, tmp, member); \
177 &pos->member != (head); \
178 pos = tmp, \
179 tmp = wl_container_of(pos->member.next, tmp, member))
180
181#define wl_list_for_each_reverse(pos, head, member) \
182 for (pos = wl_container_of((head)->prev, pos, member); \
183 &pos->member != (head); \
184 pos = wl_container_of(pos->member.prev, pos, member))
185
186#define wl_list_for_each_reverse_safe(pos, tmp, head, member) \
187 for (pos = wl_container_of((head)->prev, pos, member), \
188 tmp = wl_container_of((pos)->member.prev, tmp, member); \
189 &pos->member != (head); \
190 pos = tmp, \
191 tmp = wl_container_of(pos->member.prev, tmp, member))
192
193struct wl_array {
194 size_t size;
195 size_t alloc;
196 void *data;
197};
198
199#define wl_array_for_each(pos, array) \
200 for (pos = (array)->data; \
201 (const char *) pos < ((const char *) (array)->data + (array)->size); \
202 (pos)++)
203
204void wl_array_init(struct wl_array *array);
205void wl_array_release(struct wl_array *array);
206void *wl_array_add(struct wl_array *array, size_t size);
207int wl_array_copy(struct wl_array *array, struct wl_array *source);
208
209typedef int32_t wl_fixed_t;
210
211static inline double
212wl_fixed_to_double (wl_fixed_t f)
213{
214 union {
215 double d;
216 int64_t i;
217 } u;
218
219 u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
220
221 return u.d - (3LL << 43);
222}
223
224static inline wl_fixed_t
225wl_fixed_from_double(double d)
226{
227 union {
228 double d;
229 int64_t i;
230 } u;
231
232 u.d = d + (3LL << (51 - 8));
233
234 return u.i;
235}
236
237static inline int wl_fixed_to_int(wl_fixed_t f)
238{
239 return f / 256;
240}
241static inline wl_fixed_t wl_fixed_from_int(int i)
242{
243 return i * 256;
244}
245
246/**
247 * \brief A union representing all of the basic data types that can be passed
248 * along the wayland wire format.
249 *
250 * This union represents all of the basic data types that can be passed in the
251 * wayland wire format. It is used by dispatchers and runtime-friendly
252 * versions of the event and request marshaling functions.
253 */
254union wl_argument {
255 int32_t i; /**< signed integer */
256 uint32_t u; /**< unsigned integer */
257 wl_fixed_t f; /**< fixed point */
258 const char *s; /**< string */
259 struct wl_object *o; /**< object */
260 uint32_t n; /**< new_id */
261 struct wl_array *a; /**< array */
262 int32_t h; /**< file descriptor */
263};
264
265/**
266 * \brief A function pointer type for a dispatcher.
267 *
268 * A dispatcher is a function that handles the emitting of callbacks in client
269 * code. For programs directly using the C library, this is done by using
270 * libffi to call function pointers. When binding to languages other than C,
271 * dispatchers provide a way to abstract the function calling process to be
272 * friendlier to other function calling systems.
273 *
274 * A dispatcher takes five arguments: The first is the dispatcher-specific
275 * implementation data associated with the target object. The second is the
276 * object on which the callback is being invoked (either wl_proxy or
277 * wl_resource). The third and fourth arguments are the opcode the wl_messsage
278 * structure corresponding to the callback being emitted. The final argument
279 * is an array of arguments recieved from the other process via the wire
280 * protocol.
281 */
282typedef int (*wl_dispatcher_func_t)(const void *, void *, uint32_t,
283 const struct wl_message *,
284 union wl_argument *);
285
286typedef void (*wl_log_func_t)(const char *, va_list) WL_PRINTF(1, 0);
287
288#ifdef __cplusplus
289}
290#endif
291
292#endif
293

Warning: That file was not part of the compilation database. It may have many parsing errors.