wayland-util.h 8.8 KB

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