| 1 | /* | 
| 2 |  * Copyright © 2008 Kristian Høgsberg | 
| 3 |  * | 
| 4 |  * Permission is hereby granted, free of charge, to any person obtaining | 
| 5 |  * a copy of this software and associated documentation files (the | 
| 6 |  * "Software"), to deal in the Software without restriction, including | 
| 7 |  * without limitation the rights to use, copy, modify, merge, publish, | 
| 8 |  * distribute, sublicense, and/or sell copies of the Software, and to | 
| 9 |  * permit persons to whom the Software is furnished to do so, subject to | 
| 10 |  * the following conditions: | 
| 11 |  * | 
| 12 |  * The above copyright notice and this permission notice (including the | 
| 13 |  * next paragraph) shall be included in all copies or substantial | 
| 14 |  * portions of the Software. | 
| 15 |  * | 
| 16 |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
| 17 |  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
| 18 |  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
| 19 |  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | 
| 20 |  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | 
| 21 |  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 
| 22 |  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 
| 23 |  * SOFTWARE. | 
| 24 |  */ | 
| 25 |  | 
| 26 | /** \file wayland-util.h | 
| 27 |  * | 
| 28 |  * \brief Utility classes, functions, and macros. | 
| 29 |  */ | 
| 30 |  | 
| 31 | #ifndef WAYLAND_UTIL_H | 
| 32 | #define WAYLAND_UTIL_H | 
| 33 |  | 
| 34 | #include <math.h> | 
| 35 | #include <stddef.h> | 
| 36 | #include <inttypes.h> | 
| 37 | #include <stdarg.h> | 
| 38 |  | 
| 39 | #ifdef  __cplusplus | 
| 40 | extern "C"  { | 
| 41 | #endif | 
| 42 |  | 
| 43 | /** Visibility attribute */ | 
| 44 | #if defined(__GNUC__) && __GNUC__ >= 4 | 
| 45 | #define WL_EXPORT __attribute__ ((visibility("default"))) | 
| 46 | #else | 
| 47 | #define WL_EXPORT | 
| 48 | #endif | 
| 49 |  | 
| 50 | /** Deprecated attribute */ | 
| 51 | #if defined(__GNUC__) && __GNUC__ >= 4 | 
| 52 | #define WL_DEPRECATED __attribute__ ((deprecated)) | 
| 53 | #else | 
| 54 | #define WL_DEPRECATED | 
| 55 | #endif | 
| 56 |  | 
| 57 | /** | 
| 58 |  * Printf-style argument attribute | 
| 59 |  * | 
| 60 |  * \param x Ordinality of the format string argument | 
| 61 |  * \param y Ordinality of the argument to check against the format string | 
| 62 |  * | 
| 63 |  * \sa https://gcc.gnu.org/onlinedocs/gcc-3.2.1/gcc/Function-Attributes.html | 
| 64 |  */ | 
| 65 | #if defined(__GNUC__) && __GNUC__ >= 4 | 
| 66 | #define WL_PRINTF(x, y) __attribute__((__format__(__printf__, x, y))) | 
| 67 | #else | 
| 68 | #define WL_PRINTF(x, y) | 
| 69 | #endif | 
| 70 |  | 
| 71 | /** \class wl_object | 
| 72 |  * | 
| 73 |  * \brief A protocol object. | 
| 74 |  * | 
| 75 |  * A `wl_object` is an opaque struct identifying the protocol object | 
| 76 |  * underlying a `wl_proxy` or `wl_resource`. | 
| 77 |  * | 
| 78 |  * \note Functions accessing a `wl_object` are not normally used by client code. | 
| 79 |  * Clients should normally use the higher level interface generated by the | 
| 80 |  * scanner to interact with compositor objects. | 
| 81 |  * | 
| 82 |  */ | 
| 83 | struct wl_object; | 
| 84 |  | 
| 85 | /** | 
| 86 |  * Protocol message signature | 
| 87 |  * | 
| 88 |  * A wl_message describes the signature of an actual protocol message, such as a | 
| 89 |  * request or event, that adheres to the Wayland protocol wire format. The | 
| 90 |  * protocol implementation uses a wl_message within its demarshal machinery for | 
| 91 |  * decoding messages between a compositor and its clients. In a sense, a | 
| 92 |  * wl_message is to a protocol message like a class is to an object. | 
| 93 |  * | 
| 94 |  * The `name` of a wl_message is the name of the corresponding protocol message. | 
| 95 |  * | 
| 96 |  * The `signature` is an ordered list of symbols representing the data types | 
| 97 |  * of message arguments and, optionally, a protocol version and indicators for | 
| 98 |  * nullability. A leading integer in the `signature` indicates the _since_ | 
| 99 |  * version of the protocol message. A `?` preceding a data type symbol indicates | 
| 100 |  * that the following argument type is nullable. While it is a protocol violation | 
| 101 |  * to send messages with non-nullable arguments set to `NULL`, event handlers in | 
| 102 |  * clients might still get called with non-nullable object arguments set to | 
| 103 |  * `NULL`. This can happen when the client destroyed the object being used as | 
| 104 |  * argument on its side and an event referencing that object was sent before the | 
| 105 |  * server knew about its destruction. As this race cannot be prevented, clients | 
| 106 |  * should - as a general rule - program their event handlers such that they can | 
| 107 |  * handle object arguments declared non-nullable being `NULL` gracefully. | 
| 108 |  * | 
| 109 |  * When no arguments accompany a message, `signature` is an empty string. | 
| 110 |  * | 
| 111 |  * Symbols: | 
| 112 |  * | 
| 113 |  * * `i`: int | 
| 114 |  * * `u`: uint | 
| 115 |  * * `f`: fixed | 
| 116 |  * * `s`: string | 
| 117 |  * * `o`: object | 
| 118 |  * * `n`: new_id | 
| 119 |  * * `a`: array | 
| 120 |  * * `h`: fd | 
| 121 |  * * `?`: following argument is nullable | 
| 122 |  * | 
| 123 |  * While demarshaling primitive arguments is straightforward, when demarshaling | 
| 124 |  * messages containing `object` or `new_id` arguments, the protocol | 
| 125 |  * implementation often must determine the type of the object. The `types` of a | 
| 126 |  * wl_message is an array of wl_interface references that correspond to `o` and | 
| 127 |  * `n` arguments in `signature`, with `NULL` placeholders for arguments with | 
| 128 |  * non-object types. | 
| 129 |  * | 
| 130 |  * Consider the protocol event wl_display `delete_id` that has a single `uint` | 
| 131 |  * argument. The wl_message is: | 
| 132 |  * | 
| 133 |  * \code | 
| 134 |  * { "delete_id", "u", [NULL] } | 
| 135 |  * \endcode | 
| 136 |  * | 
| 137 |  * Here, the message `name` is `"delete_id"`, the `signature` is `"u"`, and the | 
| 138 |  * argument `types` is `[NULL]`, indicating that the `uint` argument has no | 
| 139 |  * corresponding wl_interface since it is a primitive argument. | 
| 140 |  * | 
| 141 |  * In contrast, consider a `wl_foo` interface supporting protocol request `bar` | 
| 142 |  * that has existed since version 2, and has two arguments: a `uint` and an | 
| 143 |  * object of type `wl_baz_interface` that may be `NULL`. Such a `wl_message` | 
| 144 |  * might be: | 
| 145 |  * | 
| 146 |  * \code | 
| 147 |  * { "bar", "2u?o", [NULL, &wl_baz_interface] } | 
| 148 |  * \endcode | 
| 149 |  * | 
| 150 |  * Here, the message `name` is `"bar"`, and the `signature` is `"2u?o"`. Notice | 
| 151 |  * how the `2` indicates the protocol version, the `u` indicates the first | 
| 152 |  * argument type is `uint`, and the `?o` indicates that the second argument | 
| 153 |  * is an object that may be `NULL`. Lastly, the argument `types` array indicates | 
| 154 |  * that no wl_interface corresponds to the first argument, while the type | 
| 155 |  * `wl_baz_interface` corresponds to the second argument. | 
| 156 |  * | 
| 157 |  * \sa wl_argument | 
| 158 |  * \sa wl_interface | 
| 159 |  * \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Wire-Format">Wire Format</a> | 
| 160 |  */ | 
| 161 | struct wl_message { | 
| 162 | 	/** Message name */ | 
| 163 | 	const char *name; | 
| 164 | 	/** Message signature */ | 
| 165 | 	const char *signature; | 
| 166 | 	/** Object argument interfaces */ | 
| 167 | 	const struct wl_interface **types; | 
| 168 | }; | 
| 169 |  | 
| 170 | /** | 
| 171 |  * Protocol object interface | 
| 172 |  * | 
| 173 |  * A wl_interface describes the API of a protocol object defined in the Wayland | 
| 174 |  * protocol specification. The protocol implementation uses a wl_interface | 
| 175 |  * within its marshalling machinery for encoding client requests. | 
| 176 |  * | 
| 177 |  * The `name` of a wl_interface is the name of the corresponding protocol | 
| 178 |  * interface, and `version` represents the version of the interface. The members | 
| 179 |  * `method_count` and `event_count` represent the number of `methods` (requests) | 
| 180 |  * and `events` in the respective wl_message members. | 
| 181 |  * | 
| 182 |  * For example, consider a protocol interface `foo`, marked as version `1`, with | 
| 183 |  * two requests and one event. | 
| 184 |  * | 
| 185 |  * \code | 
| 186 |  * <interface name="foo" version="1"> | 
| 187 |  *   <request name="a"></request> | 
| 188 |  *   <request name="b"></request> | 
| 189 |  *   <event name="c"></event> | 
| 190 |  * </interface> | 
| 191 |  * \endcode | 
| 192 |  * | 
| 193 |  * Given two wl_message arrays `foo_requests` and `foo_events`, a wl_interface | 
| 194 |  * for `foo` might be: | 
| 195 |  * | 
| 196 |  * \code | 
| 197 |  * struct wl_interface foo_interface = { | 
| 198 |  *         "foo", 1, | 
| 199 |  *         2, foo_requests, | 
| 200 |  *         1, foo_events | 
| 201 |  * }; | 
| 202 |  * \endcode | 
| 203 |  * | 
| 204 |  * \note The server side of the protocol may define interface <em>implementation | 
| 205 |  *       types</em> that incorporate the term `interface` in their name. Take | 
| 206 |  *       care to not confuse these server-side `struct`s with a wl_interface | 
| 207 |  *       variable whose name also ends in `interface`. For example, while the | 
| 208 |  *       server may define a type `struct wl_foo_interface`, the client may | 
| 209 |  *       define a `struct wl_interface wl_foo_interface`. | 
| 210 |  * | 
| 211 |  * \sa wl_message | 
| 212 |  * \sa wl_proxy | 
| 213 |  * \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Interfaces">Interfaces</a> | 
| 214 |  * \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Versioning">Versioning</a> | 
| 215 |  */ | 
| 216 | struct wl_interface { | 
| 217 | 	/** Interface name */ | 
| 218 | 	const char *name; | 
| 219 | 	/** Interface version */ | 
| 220 | 	int version; | 
| 221 | 	/** Number of methods (requests) */ | 
| 222 | 	int method_count; | 
| 223 | 	/** Method (request) signatures */ | 
| 224 | 	const struct wl_message *methods; | 
| 225 | 	/** Number of events */ | 
| 226 | 	int event_count; | 
| 227 | 	/** Event signatures */ | 
| 228 | 	const struct wl_message *events; | 
| 229 | }; | 
| 230 |  | 
| 231 | /** \class wl_list | 
| 232 |  * | 
| 233 |  * \brief Doubly-linked list | 
| 234 |  * | 
| 235 |  * On its own, an instance of `struct wl_list` represents the sentinel head of | 
| 236 |  * a doubly-linked list, and must be initialized using wl_list_init(). | 
| 237 |  * When empty, the list head's `next` and `prev` members point to the list head | 
| 238 |  * itself, otherwise `next` references the first element in the list, and `prev` | 
| 239 |  * refers to the last element in the list. | 
| 240 |  * | 
| 241 |  * Use the `struct wl_list` type to represent both the list head and the links | 
| 242 |  * between elements within the list. Use wl_list_empty() to determine if the | 
| 243 |  * list is empty in O(1). | 
| 244 |  * | 
| 245 |  * All elements in the list must be of the same type. The element type must have | 
| 246 |  * a `struct wl_list` member, often named `link` by convention. Prior to | 
| 247 |  * insertion, there is no need to initialize an element's `link` - invoking | 
| 248 |  * wl_list_init() on an individual list element's `struct wl_list` member is | 
| 249 |  * unnecessary if the very next operation is wl_list_insert(). However, a | 
| 250 |  * common idiom is to initialize an element's `link` prior to removal - ensure | 
| 251 |  * safety by invoking wl_list_init() before wl_list_remove(). | 
| 252 |  * | 
| 253 |  * Consider a list reference `struct wl_list foo_list`, an element type as | 
| 254 |  * `struct element`, and an element's link member as `struct wl_list link`. | 
| 255 |  * | 
| 256 |  * The following code initializes a list and adds three elements to it. | 
| 257 |  * | 
| 258 |  * \code | 
| 259 |  * struct wl_list foo_list; | 
| 260 |  * | 
| 261 |  * struct element { | 
| 262 |  *         int foo; | 
| 263 |  *         struct wl_list link; | 
| 264 |  * }; | 
| 265 |  * struct element e1, e2, e3; | 
| 266 |  * | 
| 267 |  * wl_list_init(&foo_list); | 
| 268 |  * wl_list_insert(&foo_list, &e1.link);   // e1 is the first element | 
| 269 |  * wl_list_insert(&foo_list, &e2.link);   // e2 is now the first element | 
| 270 |  * wl_list_insert(&e2.link, &e3.link); // insert e3 after e2 | 
| 271 |  * \endcode | 
| 272 |  * | 
| 273 |  * The list now looks like <em>[e2, e3, e1]</em>. | 
| 274 |  * | 
| 275 |  * The `wl_list` API provides some iterator macros. For example, to iterate | 
| 276 |  * a list in ascending order: | 
| 277 |  * | 
| 278 |  * \code | 
| 279 |  * struct element *e; | 
| 280 |  * wl_list_for_each(e, foo_list, link) { | 
| 281 |  *         do_something_with_element(e); | 
| 282 |  * } | 
| 283 |  * \endcode | 
| 284 |  * | 
| 285 |  * See the documentation of each iterator for details. | 
| 286 |  * \sa http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/list.h | 
| 287 |  */ | 
| 288 | struct wl_list { | 
| 289 | 	/** Previous list element */ | 
| 290 | 	struct wl_list *prev; | 
| 291 | 	/** Next list element */ | 
| 292 | 	struct wl_list *next; | 
| 293 | }; | 
| 294 |  | 
| 295 | /** | 
| 296 |  * Initializes the list. | 
| 297 |  * | 
| 298 |  * \param list List to initialize | 
| 299 |  * | 
| 300 |  * \memberof wl_list | 
| 301 |  */ | 
| 302 | void | 
| 303 | wl_list_init(struct wl_list *list); | 
| 304 |  | 
| 305 | /** | 
| 306 |  * Inserts an element into the list, after the element represented by \p list. | 
| 307 |  * When \p list is a reference to the list itself (the head), set the containing | 
| 308 |  * struct of \p elm as the first element in the list. | 
| 309 |  * | 
| 310 |  * \note If \p elm is already part of a list, inserting it again will lead to | 
| 311 |  *       list corruption. | 
| 312 |  * | 
| 313 |  * \param list List element after which the new element is inserted | 
| 314 |  * \param elm Link of the containing struct to insert into the list | 
| 315 |  * | 
| 316 |  * \memberof wl_list | 
| 317 |  */ | 
| 318 | void | 
| 319 | wl_list_insert(struct wl_list *list, struct wl_list *elm); | 
| 320 |  | 
| 321 | /** | 
| 322 |  * Removes an element from the list. | 
| 323 |  * | 
| 324 |  * \note This operation leaves \p elm in an invalid state. | 
| 325 |  * | 
| 326 |  * \param elm Link of the containing struct to remove from the list | 
| 327 |  * | 
| 328 |  * \memberof wl_list | 
| 329 |  */ | 
| 330 | void | 
| 331 | wl_list_remove(struct wl_list *elm); | 
| 332 |  | 
| 333 | /** | 
| 334 |  * Determines the length of the list. | 
| 335 |  * | 
| 336 |  * \note This is an O(n) operation. | 
| 337 |  * | 
| 338 |  * \param list List whose length is to be determined | 
| 339 |  * | 
| 340 |  * \return Number of elements in the list | 
| 341 |  * | 
| 342 |  * \memberof wl_list | 
| 343 |  */ | 
| 344 | int | 
| 345 | wl_list_length(const struct wl_list *list); | 
| 346 |  | 
| 347 | /** | 
| 348 |  * Determines if the list is empty. | 
| 349 |  * | 
| 350 |  * \param list List whose emptiness is to be determined | 
| 351 |  * | 
| 352 |  * \return 1 if empty, or 0 if not empty | 
| 353 |  * | 
| 354 |  * \memberof wl_list | 
| 355 |  */ | 
| 356 | int | 
| 357 | wl_list_empty(const struct wl_list *list); | 
| 358 |  | 
| 359 | /** | 
| 360 |  * Inserts all of the elements of one list into another, after the element | 
| 361 |  * represented by \p list. | 
| 362 |  * | 
| 363 |  * \note This leaves \p other in an invalid state. | 
| 364 |  * | 
| 365 |  * \param list List element after which the other list elements will be inserted | 
| 366 |  * \param other List of elements to insert | 
| 367 |  * | 
| 368 |  * \memberof wl_list | 
| 369 |  */ | 
| 370 | void | 
| 371 | wl_list_insert_list(struct wl_list *list, struct wl_list *other); | 
| 372 |  | 
| 373 | /** | 
| 374 |  * Retrieves a pointer to a containing struct, given a member name. | 
| 375 |  * | 
| 376 |  * This macro allows "conversion" from a pointer to a member to its containing | 
| 377 |  * struct. This is useful if you have a contained item like a wl_list, | 
| 378 |  * wl_listener, or wl_signal, provided via a callback or other means, and would | 
| 379 |  * like to retrieve the struct that contains it. | 
| 380 |  * | 
| 381 |  * To demonstrate, the following example retrieves a pointer to | 
| 382 |  * `example_container` given only its `destroy_listener` member: | 
| 383 |  * | 
| 384 |  * \code | 
| 385 |  * struct example_container { | 
| 386 |  *         struct wl_listener destroy_listener; | 
| 387 |  *         // other members... | 
| 388 |  * }; | 
| 389 |  * | 
| 390 |  * void example_container_destroy(struct wl_listener *listener, void *data) | 
| 391 |  * { | 
| 392 |  *         struct example_container *ctr; | 
| 393 |  * | 
| 394 |  *         ctr = wl_container_of(listener, ctr, destroy_listener); | 
| 395 |  *         // destroy ctr... | 
| 396 |  * } | 
| 397 |  * \endcode | 
| 398 |  * | 
| 399 |  * \note `sample` need not be a valid pointer. A null or uninitialised pointer | 
| 400 |  *       is sufficient. | 
| 401 |  * | 
| 402 |  * \param ptr Valid pointer to the contained member | 
| 403 |  * \param sample Pointer to a struct whose type contains \p ptr | 
| 404 |  * \param member Named location of \p ptr within the \p sample type | 
| 405 |  * | 
| 406 |  * \return The container for the specified pointer | 
| 407 |  */ | 
| 408 | #define wl_container_of(ptr, sample, member)				\ | 
| 409 | 	(__typeof__(sample))((char *)(ptr) -				\ | 
| 410 | 			     offsetof(__typeof__(*sample), member)) | 
| 411 |  | 
| 412 | /** | 
| 413 |  * Iterates over a list. | 
| 414 |  * | 
| 415 |  * This macro expresses a for-each iterator for wl_list. Given a list and | 
| 416 |  * wl_list link member name (often named `link` by convention), this macro | 
| 417 |  * assigns each element in the list to \p pos, which can then be referenced in | 
| 418 |  * a trailing code block. For example, given a wl_list of `struct message` | 
| 419 |  * elements: | 
| 420 |  * | 
| 421 |  * \code | 
| 422 |  * struct message { | 
| 423 |  *         char *contents; | 
| 424 |  *         wl_list link; | 
| 425 |  * }; | 
| 426 |  * | 
| 427 |  * struct wl_list *message_list; | 
| 428 |  * // Assume message_list now "contains" many messages | 
| 429 |  * | 
| 430 |  * struct message *m; | 
| 431 |  * wl_list_for_each(m, message_list, link) { | 
| 432 |  *         do_something_with_message(m); | 
| 433 |  * } | 
| 434 |  * \endcode | 
| 435 |  * | 
| 436 |  * \param pos Cursor that each list element will be assigned to | 
| 437 |  * \param head Head of the list to iterate over | 
| 438 |  * \param member Name of the link member within the element struct | 
| 439 |  * | 
| 440 |  * \relates wl_list | 
| 441 |  */ | 
| 442 | #define wl_list_for_each(pos, head, member)				\ | 
| 443 | 	for (pos = wl_container_of((head)->next, pos, member);	\ | 
| 444 | 	     &pos->member != (head);					\ | 
| 445 | 	     pos = wl_container_of(pos->member.next, pos, member)) | 
| 446 |  | 
| 447 | /** | 
| 448 |  * Iterates over a list, safe against removal of the list element. | 
| 449 |  * | 
| 450 |  * \note Only removal of the current element, \p pos, is safe. Removing | 
| 451 |  *       any other element during traversal may lead to a loop malfunction. | 
| 452 |  * | 
| 453 |  * \sa wl_list_for_each() | 
| 454 |  * | 
| 455 |  * \param pos Cursor that each list element will be assigned to | 
| 456 |  * \param tmp Temporary pointer of the same type as \p pos | 
| 457 |  * \param head Head of the list to iterate over | 
| 458 |  * \param member Name of the link member within the element struct | 
| 459 |  * | 
| 460 |  * \relates wl_list | 
| 461 |  */ | 
| 462 | #define wl_list_for_each_safe(pos, tmp, head, member)			\ | 
| 463 | 	for (pos = wl_container_of((head)->next, pos, member),		\ | 
| 464 | 	     tmp = wl_container_of((pos)->member.next, tmp, member);	\ | 
| 465 | 	     &pos->member != (head);					\ | 
| 466 | 	     pos = tmp,							\ | 
| 467 | 	     tmp = wl_container_of(pos->member.next, tmp, member)) | 
| 468 |  | 
| 469 | /** | 
| 470 |  * Iterates backwards over a list. | 
| 471 |  * | 
| 472 |  * \sa wl_list_for_each() | 
| 473 |  * | 
| 474 |  * \param pos Cursor that each list element will be assigned to | 
| 475 |  * \param head Head of the list to iterate over | 
| 476 |  * \param member Name of the link member within the element struct | 
| 477 |  * | 
| 478 |  * \relates wl_list | 
| 479 |  */ | 
| 480 | #define wl_list_for_each_reverse(pos, head, member)			\ | 
| 481 | 	for (pos = wl_container_of((head)->prev, pos, member);	\ | 
| 482 | 	     &pos->member != (head);					\ | 
| 483 | 	     pos = wl_container_of(pos->member.prev, pos, member)) | 
| 484 |  | 
| 485 | /** | 
| 486 |  * Iterates backwards over a list, safe against removal of the list element. | 
| 487 |  * | 
| 488 |  * \note Only removal of the current element, \p pos, is safe. Removing | 
| 489 |  *       any other element during traversal may lead to a loop malfunction. | 
| 490 |  * | 
| 491 |  * \sa wl_list_for_each() | 
| 492 |  * | 
| 493 |  * \param pos Cursor that each list element will be assigned to | 
| 494 |  * \param tmp Temporary pointer of the same type as \p pos | 
| 495 |  * \param head Head of the list to iterate over | 
| 496 |  * \param member Name of the link member within the element struct | 
| 497 |  * | 
| 498 |  * \relates wl_list | 
| 499 |  */ | 
| 500 | #define wl_list_for_each_reverse_safe(pos, tmp, head, member)		\ | 
| 501 | 	for (pos = wl_container_of((head)->prev, pos, member),	\ | 
| 502 | 	     tmp = wl_container_of((pos)->member.prev, tmp, member);	\ | 
| 503 | 	     &pos->member != (head);					\ | 
| 504 | 	     pos = tmp,							\ | 
| 505 | 	     tmp = wl_container_of(pos->member.prev, tmp, member)) | 
| 506 |  | 
| 507 | /** | 
| 508 |  * \class wl_array | 
| 509 |  * | 
| 510 |  * Dynamic array | 
| 511 |  * | 
| 512 |  * A wl_array is a dynamic array that can only grow until released. It is | 
| 513 |  * intended for relatively small allocations whose size is variable or not known | 
| 514 |  * in advance. While construction of a wl_array does not require all elements to | 
| 515 |  * be of the same size, wl_array_for_each() does require all elements to have | 
| 516 |  * the same type and size. | 
| 517 |  * | 
| 518 |  */ | 
| 519 | struct wl_array { | 
| 520 | 	/** Array size */ | 
| 521 | 	size_t size; | 
| 522 | 	/** Allocated space */ | 
| 523 | 	size_t alloc; | 
| 524 | 	/** Array data */ | 
| 525 | 	void *data; | 
| 526 | }; | 
| 527 |  | 
| 528 | /** | 
| 529 |  * Initializes the array. | 
| 530 |  * | 
| 531 |  * \param array Array to initialize | 
| 532 |  * | 
| 533 |  * \memberof wl_array | 
| 534 |  */ | 
| 535 | void | 
| 536 | wl_array_init(struct wl_array *array); | 
| 537 |  | 
| 538 | /** | 
| 539 |  * Releases the array data. | 
| 540 |  * | 
| 541 |  * \note Leaves the array in an invalid state. | 
| 542 |  * | 
| 543 |  * \param array Array whose data is to be released | 
| 544 |  * | 
| 545 |  * \memberof wl_array | 
| 546 |  */ | 
| 547 | void | 
| 548 | wl_array_release(struct wl_array *array); | 
| 549 |  | 
| 550 | /** | 
| 551 |  * Increases the size of the array by \p size bytes. | 
| 552 |  * | 
| 553 |  * \param array Array whose size is to be increased | 
| 554 |  * \param size Number of bytes to increase the size of the array by | 
| 555 |  * | 
| 556 |  * \return A pointer to the beginning of the newly appended space, or NULL when | 
| 557 |  *         resizing fails. | 
| 558 |  * | 
| 559 |  * \memberof wl_array | 
| 560 |  */ | 
| 561 | void * | 
| 562 | wl_array_add(struct wl_array *array, size_t size); | 
| 563 |  | 
| 564 | /** | 
| 565 |  * Copies the contents of \p source to \p array. | 
| 566 |  * | 
| 567 |  * \param array Destination array to copy to | 
| 568 |  * \param source Source array to copy from | 
| 569 |  * | 
| 570 |  * \return 0 on success, or -1 on failure | 
| 571 |  * | 
| 572 |  * \memberof wl_array | 
| 573 |  */ | 
| 574 | int | 
| 575 | wl_array_copy(struct wl_array *array, struct wl_array *source); | 
| 576 |  | 
| 577 | /** | 
| 578 |  * Iterates over an array. | 
| 579 |  * | 
| 580 |  * This macro expresses a for-each iterator for wl_array. It assigns each | 
| 581 |  * element in the array to \p pos, which can then be referenced in a trailing | 
| 582 |  * code block. \p pos must be a pointer to the array element type, and all | 
| 583 |  * array elements must be of the same type and size. | 
| 584 |  * | 
| 585 |  * \param pos Cursor that each array element will be assigned to | 
| 586 |  * \param array Array to iterate over | 
| 587 |  * | 
| 588 |  * \relates wl_array | 
| 589 |  * \sa wl_list_for_each() | 
| 590 |  */ | 
| 591 | #define wl_array_for_each(pos, array)					\ | 
| 592 | 	for (pos = (array)->data;					\ | 
| 593 | 	     (const char *) pos < ((const char *) (array)->data + (array)->size); \ | 
| 594 | 	     (pos)++) | 
| 595 |  | 
| 596 | /** | 
| 597 |  * Fixed-point number | 
| 598 |  * | 
| 599 |  * A `wl_fixed_t` is a 24.8 signed fixed-point number with a sign bit, 23 bits | 
| 600 |  * of integer precision and 8 bits of decimal precision. Consider `wl_fixed_t` | 
| 601 |  * as an opaque struct with methods that facilitate conversion to and from | 
| 602 |  * `double` and `int` types. | 
| 603 |  */ | 
| 604 | typedef int32_t wl_fixed_t; | 
| 605 |  | 
| 606 | /** | 
| 607 |  * Converts a fixed-point number to a floating-point number. | 
| 608 |  * | 
| 609 |  * \param f Fixed-point number to convert | 
| 610 |  * | 
| 611 |  * \return Floating-point representation of the fixed-point argument | 
| 612 |  */ | 
| 613 | static inline double | 
| 614 | wl_fixed_to_double(wl_fixed_t f) | 
| 615 | { | 
| 616 | 	union { | 
| 617 | 		double d; | 
| 618 | 		int64_t i; | 
| 619 | 	} u; | 
| 620 |  | 
| 621 | 	u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f; | 
| 622 |  | 
| 623 | 	return u.d - (3LL << 43); | 
| 624 | } | 
| 625 |  | 
| 626 | /** | 
| 627 |  * Converts a floating-point number to a fixed-point number. | 
| 628 |  * | 
| 629 |  * \param d Floating-point number to convert | 
| 630 |  * | 
| 631 |  * \return Fixed-point representation of the floating-point argument | 
| 632 |  */ | 
| 633 | static inline wl_fixed_t | 
| 634 | wl_fixed_from_double(double d) | 
| 635 | { | 
| 636 | 	union { | 
| 637 | 		double d; | 
| 638 | 		int64_t i; | 
| 639 | 	} u; | 
| 640 |  | 
| 641 | 	u.d = d + (3LL << (51 - 8)); | 
| 642 |  | 
| 643 | 	return (wl_fixed_t)u.i; | 
| 644 | } | 
| 645 |  | 
| 646 | /** | 
| 647 |  * Converts a fixed-point number to an integer. | 
| 648 |  * | 
| 649 |  * \param f Fixed-point number to convert | 
| 650 |  * | 
| 651 |  * \return Integer component of the fixed-point argument | 
| 652 |  */ | 
| 653 | static inline int | 
| 654 | wl_fixed_to_int(wl_fixed_t f) | 
| 655 | { | 
| 656 | 	return f / 256; | 
| 657 | } | 
| 658 |  | 
| 659 | /** | 
| 660 |  * Converts an integer to a fixed-point number. | 
| 661 |  * | 
| 662 |  * \param i Integer to convert | 
| 663 |  * | 
| 664 |  * \return Fixed-point representation of the integer argument | 
| 665 |  */ | 
| 666 | static inline wl_fixed_t | 
| 667 | wl_fixed_from_int(int i) | 
| 668 | { | 
| 669 | 	return i * 256; | 
| 670 | } | 
| 671 |  | 
| 672 | /** | 
| 673 |  * Protocol message argument data types | 
| 674 |  * | 
| 675 |  * This union represents all of the argument types in the Wayland protocol wire | 
| 676 |  * format. The protocol implementation uses wl_argument within its marshalling | 
| 677 |  * machinery for dispatching messages between a client and a compositor. | 
| 678 |  * | 
| 679 |  * \sa wl_message | 
| 680 |  * \sa wl_interface | 
| 681 |  * \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-wire-Format">Wire Format</a> | 
| 682 |  */ | 
| 683 | union wl_argument { | 
| 684 | 	int32_t i;           /**< `int`    */ | 
| 685 | 	uint32_t u;          /**< `uint`   */ | 
| 686 | 	wl_fixed_t f;        /**< `fixed`  */ | 
| 687 | 	const char *s;       /**< `string` */ | 
| 688 | 	struct wl_object *o; /**< `object` */ | 
| 689 | 	uint32_t n;          /**< `new_id` */ | 
| 690 | 	struct wl_array *a;  /**< `array`  */ | 
| 691 | 	int32_t h;           /**< `fd`     */ | 
| 692 | }; | 
| 693 |  | 
| 694 | /** | 
| 695 |  * Dispatcher function type alias | 
| 696 |  * | 
| 697 |  * A dispatcher is a function that handles the emitting of callbacks in client | 
| 698 |  * code. For programs directly using the C library, this is done by using | 
| 699 |  * libffi to call function pointers. When binding to languages other than C, | 
| 700 |  * dispatchers provide a way to abstract the function calling process to be | 
| 701 |  * friendlier to other function calling systems. | 
| 702 |  * | 
| 703 |  * A dispatcher takes five arguments: The first is the dispatcher-specific | 
| 704 |  * implementation associated with the target object. The second is the object | 
| 705 |  * upon which the callback is being invoked (either wl_proxy or wl_resource). | 
| 706 |  * The third and fourth arguments are the opcode and the wl_message | 
| 707 |  * corresponding to the callback. The final argument is an array of arguments | 
| 708 |  * received from the other process via the wire protocol. | 
| 709 |  * | 
| 710 |  * \param "const void *" Dispatcher-specific implementation data | 
| 711 |  * \param "void *" Callback invocation target (wl_proxy or `wl_resource`) | 
| 712 |  * \param uint32_t Callback opcode | 
| 713 |  * \param "const struct wl_message *" Callback message signature | 
| 714 |  * \param "union wl_argument *" Array of received arguments | 
| 715 |  * | 
| 716 |  * \return 0 on success, or -1 on failure | 
| 717 |  */ | 
| 718 | typedef int (*wl_dispatcher_func_t)(const void *, void *, uint32_t, | 
| 719 | 				    const struct wl_message *, | 
| 720 | 				    union wl_argument *); | 
| 721 |  | 
| 722 | /** | 
| 723 |  * Log function type alias | 
| 724 |  * | 
| 725 |  * The C implementation of the Wayland protocol abstracts the details of | 
| 726 |  * logging. Users may customize the logging behavior, with a function conforming | 
| 727 |  * to the `wl_log_func_t` type, via `wl_log_set_handler_client` and | 
| 728 |  * `wl_log_set_handler_server`. | 
| 729 |  * | 
| 730 |  * A `wl_log_func_t` must conform to the expectations of `vprintf`, and | 
| 731 |  * expects two arguments: a string to write and a corresponding variable | 
| 732 |  * argument list. While the string to write may contain format specifiers and | 
| 733 |  * use values in the variable argument list, the behavior of any `wl_log_func_t` | 
| 734 |  * depends on the implementation. | 
| 735 |  * | 
| 736 |  * \note Take care to not confuse this with `wl_protocol_logger_func_t`, which | 
| 737 |  *       is a specific server-side logger for requests and events. | 
| 738 |  * | 
| 739 |  * \param "const char *" String to write to the log, containing optional format | 
| 740 |  *                       specifiers | 
| 741 |  * \param "va_list" Variable argument list | 
| 742 |  * | 
| 743 |  * \sa wl_log_set_handler_client | 
| 744 |  * \sa wl_log_set_handler_server | 
| 745 |  */ | 
| 746 | typedef void (*wl_log_func_t)(const char *, va_list) WL_PRINTF(1, 0); | 
| 747 |  | 
| 748 | /** | 
| 749 |  * Return value of an iterator function | 
| 750 |  * | 
| 751 |  * \sa wl_client_for_each_resource_iterator_func_t | 
| 752 |  * \sa wl_client_for_each_resource | 
| 753 |  */ | 
| 754 | enum wl_iterator_result { | 
| 755 | 	/** Stop the iteration */ | 
| 756 | 	WL_ITERATOR_STOP, | 
| 757 | 	/** Continue the iteration */ | 
| 758 | 	WL_ITERATOR_CONTINUE | 
| 759 | }; | 
| 760 |  | 
| 761 | #ifdef  __cplusplus | 
| 762 | } | 
| 763 | #endif | 
| 764 |  | 
| 765 | #endif | 
| 766 |  |