wayland-util.h 24 KB

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