Path: blob/master/thirdparty/linuxbsd_headers/wayland/wayland-util.h
9898 views
/*1* Copyright © 2008 Kristian Høgsberg2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice (including the12* next paragraph) shall be included in all copies or substantial13* portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,16* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF17* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS19* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN20* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN21* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22* SOFTWARE.23*/2425/** \file wayland-util.h26*27* \brief Utility classes, functions, and macros.28*/2930#ifndef WAYLAND_UTIL_H31#define WAYLAND_UTIL_H3233#include <math.h>34#include <stddef.h>35#include <inttypes.h>36#include <stdarg.h>3738#ifdef __cplusplus39extern "C" {40#endif4142/** Visibility attribute */43#if defined(__GNUC__) && __GNUC__ >= 444#define WL_EXPORT __attribute__ ((visibility("default")))45#else46#define WL_EXPORT47#endif4849/** Deprecated attribute */50#if defined(__GNUC__) && __GNUC__ >= 451#define WL_DEPRECATED __attribute__ ((deprecated))52#else53#define WL_DEPRECATED54#endif5556/**57* Printf-style argument attribute58*59* \param x Ordinality of the format string argument60* \param y Ordinality of the argument to check against the format string61*62* \sa https://gcc.gnu.org/onlinedocs/gcc-3.2.1/gcc/Function-Attributes.html63*/64#if defined(__GNUC__) && __GNUC__ >= 465#define WL_PRINTF(x, y) __attribute__((__format__(__printf__, x, y)))66#else67#define WL_PRINTF(x, y)68#endif6970/** \class wl_object71*72* \brief A protocol object.73*74* A `wl_object` is an opaque struct identifying the protocol object75* underlying a `wl_proxy` or `wl_resource`.76*77* \note Functions accessing a `wl_object` are not normally used by client code.78* Clients should normally use the higher level interface generated by the79* scanner to interact with compositor objects.80*81*/82struct wl_object;8384/**85* Protocol message signature86*87* A wl_message describes the signature of an actual protocol message, such as a88* request or event, that adheres to the Wayland protocol wire format. The89* protocol implementation uses a wl_message within its demarshal machinery for90* decoding messages between a compositor and its clients. In a sense, a91* wl_message is to a protocol message like a class is to an object.92*93* The `name` of a wl_message is the name of the corresponding protocol message.94*95* The `signature` is an ordered list of symbols representing the data types96* of message arguments and, optionally, a protocol version and indicators for97* nullability. A leading integer in the `signature` indicates the _since_98* version of the protocol message. A `?` preceding a data type symbol indicates99* that the following argument type is nullable. While it is a protocol violation100* to send messages with non-nullable arguments set to `NULL`, event handlers in101* clients might still get called with non-nullable object arguments set to102* `NULL`. This can happen when the client destroyed the object being used as103* argument on its side and an event referencing that object was sent before the104* server knew about its destruction. As this race cannot be prevented, clients105* should - as a general rule - program their event handlers such that they can106* handle object arguments declared non-nullable being `NULL` gracefully.107*108* When no arguments accompany a message, `signature` is an empty string.109*110* Symbols:111*112* * `i`: int113* * `u`: uint114* * `f`: fixed115* * `s`: string116* * `o`: object117* * `n`: new_id118* * `a`: array119* * `h`: fd120* * `?`: following argument is nullable121*122* While demarshaling primitive arguments is straightforward, when demarshaling123* messages containing `object` or `new_id` arguments, the protocol124* implementation often must determine the type of the object. The `types` of a125* wl_message is an array of wl_interface references that correspond to `o` and126* `n` arguments in `signature`, with `NULL` placeholders for arguments with127* non-object types.128*129* Consider the protocol event wl_display `delete_id` that has a single `uint`130* argument. The wl_message is:131*132* \code133* { "delete_id", "u", [NULL] }134* \endcode135*136* Here, the message `name` is `"delete_id"`, the `signature` is `"u"`, and the137* argument `types` is `[NULL]`, indicating that the `uint` argument has no138* corresponding wl_interface since it is a primitive argument.139*140* In contrast, consider a `wl_foo` interface supporting protocol request `bar`141* that has existed since version 2, and has two arguments: a `uint` and an142* object of type `wl_baz_interface` that may be `NULL`. Such a `wl_message`143* might be:144*145* \code146* { "bar", "2u?o", [NULL, &wl_baz_interface] }147* \endcode148*149* Here, the message `name` is `"bar"`, and the `signature` is `"2u?o"`. Notice150* how the `2` indicates the protocol version, the `u` indicates the first151* argument type is `uint`, and the `?o` indicates that the second argument152* is an object that may be `NULL`. Lastly, the argument `types` array indicates153* that no wl_interface corresponds to the first argument, while the type154* `wl_baz_interface` corresponds to the second argument.155*156* \sa wl_argument157* \sa wl_interface158* \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Wire-Format">Wire Format</a>159*/160struct wl_message {161/** Message name */162const char *name;163/** Message signature */164const char *signature;165/** Object argument interfaces */166const struct wl_interface **types;167};168169/**170* Protocol object interface171*172* A wl_interface describes the API of a protocol object defined in the Wayland173* protocol specification. The protocol implementation uses a wl_interface174* within its marshalling machinery for encoding client requests.175*176* The `name` of a wl_interface is the name of the corresponding protocol177* interface, and `version` represents the version of the interface. The members178* `method_count` and `event_count` represent the number of `methods` (requests)179* and `events` in the respective wl_message members.180*181* For example, consider a protocol interface `foo`, marked as version `1`, with182* two requests and one event.183*184* \code{.xml}185* <interface name="foo" version="1">186* <request name="a"></request>187* <request name="b"></request>188* <event name="c"></event>189* </interface>190* \endcode191*192* Given two wl_message arrays `foo_requests` and `foo_events`, a wl_interface193* for `foo` might be:194*195* \code196* struct wl_interface foo_interface = {197* "foo", 1,198* 2, foo_requests,199* 1, foo_events200* };201* \endcode202*203* \note The server side of the protocol may define interface <em>implementation204* types</em> that incorporate the term `interface` in their name. Take205* care to not confuse these server-side `struct`s with a wl_interface206* variable whose name also ends in `interface`. For example, while the207* server may define a type `struct wl_foo_interface`, the client may208* define a `struct wl_interface wl_foo_interface`.209*210* \sa wl_message211* \sa wl_proxy212* \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Interfaces">Interfaces</a>213* \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Versioning">Versioning</a>214*/215struct wl_interface {216/** Interface name */217const char *name;218/** Interface version */219int version;220/** Number of methods (requests) */221int method_count;222/** Method (request) signatures */223const struct wl_message *methods;224/** Number of events */225int event_count;226/** Event signatures */227const struct wl_message *events;228};229230/** \class wl_list231*232* \brief Doubly-linked list233*234* On its own, an instance of `struct wl_list` represents the sentinel head of235* a doubly-linked list, and must be initialized using wl_list_init().236* When empty, the list head's `next` and `prev` members point to the list head237* itself, otherwise `next` references the first element in the list, and `prev`238* refers to the last element in the list.239*240* Use the `struct wl_list` type to represent both the list head and the links241* between elements within the list. Use wl_list_empty() to determine if the242* list is empty in O(1).243*244* All elements in the list must be of the same type. The element type must have245* a `struct wl_list` member, often named `link` by convention. Prior to246* insertion, there is no need to initialize an element's `link` - invoking247* wl_list_init() on an individual list element's `struct wl_list` member is248* unnecessary if the very next operation is wl_list_insert(). However, a249* common idiom is to initialize an element's `link` prior to removal - ensure250* safety by invoking wl_list_init() before wl_list_remove().251*252* Consider a list reference `struct wl_list foo_list`, an element type as253* `struct element`, and an element's link member as `struct wl_list link`.254*255* The following code initializes a list and adds three elements to it.256*257* \code258* struct wl_list foo_list;259*260* struct element {261* int foo;262* struct wl_list link;263* };264* struct element e1, e2, e3;265*266* wl_list_init(&foo_list);267* wl_list_insert(&foo_list, &e1.link); // e1 is the first element268* wl_list_insert(&foo_list, &e2.link); // e2 is now the first element269* wl_list_insert(&e2.link, &e3.link); // insert e3 after e2270* \endcode271*272* The list now looks like <em>[e2, e3, e1]</em>.273*274* The `wl_list` API provides some iterator macros. For example, to iterate275* a list in ascending order:276*277* \code278* struct element *e;279* wl_list_for_each(e, foo_list, link) {280* do_something_with_element(e);281* }282* \endcode283*284* See the documentation of each iterator for details.285* \sa http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/list.h286*/287struct wl_list {288/** Previous list element */289struct wl_list *prev;290/** Next list element */291struct wl_list *next;292};293294/**295* Initializes the list.296*297* \param list List to initialize298*299* \memberof wl_list300*/301void302wl_list_init(struct wl_list *list);303304/**305* Inserts an element into the list, after the element represented by \p list.306* When \p list is a reference to the list itself (the head), set the containing307* struct of \p elm as the first element in the list.308*309* \note If \p elm is already part of a list, inserting it again will lead to310* list corruption.311*312* \param list List element after which the new element is inserted313* \param elm Link of the containing struct to insert into the list314*315* \memberof wl_list316*/317void318wl_list_insert(struct wl_list *list, struct wl_list *elm);319320/**321* Removes an element from the list.322*323* \note This operation leaves \p elm in an invalid state.324*325* \param elm Link of the containing struct to remove from the list326*327* \memberof wl_list328*/329void330wl_list_remove(struct wl_list *elm);331332/**333* Determines the length of the list.334*335* \note This is an O(n) operation.336*337* \param list List whose length is to be determined338*339* \return Number of elements in the list340*341* \memberof wl_list342*/343int344wl_list_length(const struct wl_list *list);345346/**347* Determines if the list is empty.348*349* \param list List whose emptiness is to be determined350*351* \return 1 if empty, or 0 if not empty352*353* \memberof wl_list354*/355int356wl_list_empty(const struct wl_list *list);357358/**359* Inserts all of the elements of one list into another, after the element360* represented by \p list.361*362* \note This leaves \p other in an invalid state.363*364* \param list List element after which the other list elements will be inserted365* \param other List of elements to insert366*367* \memberof wl_list368*/369void370wl_list_insert_list(struct wl_list *list, struct wl_list *other);371372/**373* Retrieves a pointer to a containing struct, given a member name.374*375* This macro allows "conversion" from a pointer to a member to its containing376* struct. This is useful if you have a contained item like a wl_list,377* wl_listener, or wl_signal, provided via a callback or other means, and would378* like to retrieve the struct that contains it.379*380* To demonstrate, the following example retrieves a pointer to381* `example_container` given only its `destroy_listener` member:382*383* \code384* struct example_container {385* struct wl_listener destroy_listener;386* // other members...387* };388*389* void example_container_destroy(struct wl_listener *listener, void *data)390* {391* struct example_container *ctr;392*393* ctr = wl_container_of(listener, ctr, destroy_listener);394* // destroy ctr...395* }396* \endcode397*398* \note `sample` need not be a valid pointer. A null or uninitialised pointer399* is sufficient.400*401* \param ptr Valid pointer to the contained member402* \param sample Pointer to a struct whose type contains \p ptr403* \param member Named location of \p ptr within the \p sample type404*405* \return The container for the specified pointer406*/407#define wl_container_of(ptr, sample, member) \408(__typeof__(sample))((char *)(ptr) - \409offsetof(__typeof__(*sample), member))410411/**412* Iterates over a list.413*414* This macro expresses a for-each iterator for wl_list. Given a list and415* wl_list link member name (often named `link` by convention), this macro416* assigns each element in the list to \p pos, which can then be referenced in417* a trailing code block. For example, given a wl_list of `struct message`418* elements:419*420* \code421* struct message {422* char *contents;423* wl_list link;424* };425*426* struct wl_list *message_list;427* // Assume message_list now "contains" many messages428*429* struct message *m;430* wl_list_for_each(m, message_list, link) {431* do_something_with_message(m);432* }433* \endcode434*435* \param pos Cursor that each list element will be assigned to436* \param head Head of the list to iterate over437* \param member Name of the link member within the element struct438*439* \relates wl_list440*/441#define wl_list_for_each(pos, head, member) \442for (pos = wl_container_of((head)->next, pos, member); \443&pos->member != (head); \444pos = wl_container_of(pos->member.next, pos, member))445446/**447* Iterates over a list, safe against removal of the list element.448*449* \note Only removal of the current element, \p pos, is safe. Removing450* any other element during traversal may lead to a loop malfunction.451*452* \sa wl_list_for_each()453*454* \param pos Cursor that each list element will be assigned to455* \param tmp Temporary pointer of the same type as \p pos456* \param head Head of the list to iterate over457* \param member Name of the link member within the element struct458*459* \relates wl_list460*/461#define wl_list_for_each_safe(pos, tmp, head, member) \462for (pos = wl_container_of((head)->next, pos, member), \463tmp = wl_container_of((pos)->member.next, tmp, member); \464&pos->member != (head); \465pos = tmp, \466tmp = wl_container_of(pos->member.next, tmp, member))467468/**469* Iterates backwards over a list.470*471* \sa wl_list_for_each()472*473* \param pos Cursor that each list element will be assigned to474* \param head Head of the list to iterate over475* \param member Name of the link member within the element struct476*477* \relates wl_list478*/479#define wl_list_for_each_reverse(pos, head, member) \480for (pos = wl_container_of((head)->prev, pos, member); \481&pos->member != (head); \482pos = wl_container_of(pos->member.prev, pos, member))483484/**485* Iterates backwards over a list, safe against removal of the list element.486*487* \note Only removal of the current element, \p pos, is safe. Removing488* any other element during traversal may lead to a loop malfunction.489*490* \sa wl_list_for_each()491*492* \param pos Cursor that each list element will be assigned to493* \param tmp Temporary pointer of the same type as \p pos494* \param head Head of the list to iterate over495* \param member Name of the link member within the element struct496*497* \relates wl_list498*/499#define wl_list_for_each_reverse_safe(pos, tmp, head, member) \500for (pos = wl_container_of((head)->prev, pos, member), \501tmp = wl_container_of((pos)->member.prev, tmp, member); \502&pos->member != (head); \503pos = tmp, \504tmp = wl_container_of(pos->member.prev, tmp, member))505506/**507* \class wl_array508*509* Dynamic array510*511* A wl_array is a dynamic array that can only grow until released. It is512* intended for relatively small allocations whose size is variable or not known513* in advance. While construction of a wl_array does not require all elements to514* be of the same size, wl_array_for_each() does require all elements to have515* the same type and size.516*517*/518struct wl_array {519/** Array size */520size_t size;521/** Allocated space */522size_t alloc;523/** Array data */524void *data;525};526527/**528* Initializes the array.529*530* \param array Array to initialize531*532* \memberof wl_array533*/534void535wl_array_init(struct wl_array *array);536537/**538* Releases the array data.539*540* \note Leaves the array in an invalid state.541*542* \param array Array whose data is to be released543*544* \memberof wl_array545*/546void547wl_array_release(struct wl_array *array);548549/**550* Increases the size of the array by \p size bytes.551*552* \param array Array whose size is to be increased553* \param size Number of bytes to increase the size of the array by554*555* \return A pointer to the beginning of the newly appended space, or NULL when556* resizing fails.557*558* \memberof wl_array559*/560void *561wl_array_add(struct wl_array *array, size_t size);562563/**564* Copies the contents of \p source to \p array.565*566* \param array Destination array to copy to567* \param source Source array to copy from568*569* \return 0 on success, or -1 on failure570*571* \memberof wl_array572*/573int574wl_array_copy(struct wl_array *array, struct wl_array *source);575576/**577* Iterates over an array.578*579* This macro expresses a for-each iterator for wl_array. It assigns each580* element in the array to \p pos, which can then be referenced in a trailing581* code block. \p pos must be a pointer to the array element type, and all582* array elements must be of the same type and size.583*584* \param pos Cursor that each array element will be assigned to585* \param array Array to iterate over586*587* \relates wl_array588* \sa wl_list_for_each()589*/590#define wl_array_for_each(pos, array) \591for (pos = (array)->data; \592(const char *) pos < ((const char *) (array)->data + (array)->size); \593(pos)++)594595/**596* Fixed-point number597*598* A `wl_fixed_t` is a 24.8 signed fixed-point number with a sign bit, 23 bits599* of integer precision and 8 bits of decimal precision. Consider `wl_fixed_t`600* as an opaque struct with methods that facilitate conversion to and from601* `double` and `int` types.602*/603typedef int32_t wl_fixed_t;604605/**606* Converts a fixed-point number to a floating-point number.607*608* \param f Fixed-point number to convert609*610* \return Floating-point representation of the fixed-point argument611*/612static inline double613wl_fixed_to_double(wl_fixed_t f)614{615union {616double d;617int64_t i;618} u;619620u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;621622return u.d - (3LL << 43);623}624625/**626* Converts a floating-point number to a fixed-point number.627*628* \param d Floating-point number to convert629*630* \return Fixed-point representation of the floating-point argument631*/632static inline wl_fixed_t633wl_fixed_from_double(double d)634{635union {636double d;637int64_t i;638} u;639640u.d = d + (3LL << (51 - 8));641642return (wl_fixed_t)u.i;643}644645/**646* Converts a fixed-point number to an integer.647*648* \param f Fixed-point number to convert649*650* \return Integer component of the fixed-point argument651*/652static inline int653wl_fixed_to_int(wl_fixed_t f)654{655return f / 256;656}657658/**659* Converts an integer to a fixed-point number.660*661* \param i Integer to convert662*663* \return Fixed-point representation of the integer argument664*/665static inline wl_fixed_t666wl_fixed_from_int(int i)667{668return i * 256;669}670671/**672* Protocol message argument data types673*674* This union represents all of the argument types in the Wayland protocol wire675* format. The protocol implementation uses wl_argument within its marshalling676* machinery for dispatching messages between a client and a compositor.677*678* \sa wl_message679* \sa wl_interface680* \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-wire-Format">Wire Format</a>681*/682union wl_argument {683int32_t i; /**< `int` */684uint32_t u; /**< `uint` */685wl_fixed_t f; /**< `fixed` */686const char *s; /**< `string` */687struct wl_object *o; /**< `object` */688uint32_t n; /**< `new_id` */689struct wl_array *a; /**< `array` */690int32_t h; /**< `fd` */691};692693/**694* Dispatcher function type alias695*696* A dispatcher is a function that handles the emitting of callbacks in client697* code. For programs directly using the C library, this is done by using698* libffi to call function pointers. When binding to languages other than C,699* dispatchers provide a way to abstract the function calling process to be700* friendlier to other function calling systems.701*702* A dispatcher takes five arguments: The first is the dispatcher-specific703* implementation associated with the target object. The second is the object704* upon which the callback is being invoked (either wl_proxy or wl_resource).705* The third and fourth arguments are the opcode and the wl_message706* corresponding to the callback. The final argument is an array of arguments707* received from the other process via the wire protocol.708*709* \param "const void *" Dispatcher-specific implementation data710* \param "void *" Callback invocation target (wl_proxy or `wl_resource`)711* \param uint32_t Callback opcode712* \param "const struct wl_message *" Callback message signature713* \param "union wl_argument *" Array of received arguments714*715* \return 0 on success, or -1 on failure716*/717typedef int (*wl_dispatcher_func_t)(const void *, void *, uint32_t,718const struct wl_message *,719union wl_argument *);720721/**722* Log function type alias723*724* The C implementation of the Wayland protocol abstracts the details of725* logging. Users may customize the logging behavior, with a function conforming726* to the `wl_log_func_t` type, via `wl_log_set_handler_client` and727* `wl_log_set_handler_server`.728*729* A `wl_log_func_t` must conform to the expectations of `vprintf`, and730* expects two arguments: a string to write and a corresponding variable731* argument list. While the string to write may contain format specifiers and732* use values in the variable argument list, the behavior of any `wl_log_func_t`733* depends on the implementation.734*735* \note Take care to not confuse this with `wl_protocol_logger_func_t`, which736* is a specific server-side logger for requests and events.737*738* \param "const char *" String to write to the log, containing optional format739* specifiers740* \param "va_list" Variable argument list741*742* \sa wl_log_set_handler_client743* \sa wl_log_set_handler_server744*/745typedef void (*wl_log_func_t)(const char *, va_list) WL_PRINTF(1, 0);746747/**748* Return value of an iterator function749*750* \sa wl_client_for_each_resource_iterator_func_t751* \sa wl_client_for_each_resource752*/753enum wl_iterator_result {754/** Stop the iteration */755WL_ITERATOR_STOP,756/** Continue the iteration */757WL_ITERATOR_CONTINUE758};759760#ifdef __cplusplus761}762#endif763764#endif765766767