AtlatestRepositorysigil-ui
1
/*2
* Copyright © 2008 Kristian Høgsberg3
*4
* Permission is hereby granted, free of charge, to any person obtaining5
* a copy of this software and associated documentation files (the6
* "Software"), to deal in the Software without restriction, including7
* without limitation the rights to use, copy, modify, merge, publish,8
* distribute, sublicense, and/or sell copies of the Software, and to9
* permit persons to whom the Software is furnished to do so, subject to10
* the following conditions:11
*12
* The above copyright notice and this permission notice (including the13
* next paragraph) shall be included in all copies or substantial14
* 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 OF18
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND19
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS20
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN21
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN22
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE23
* SOFTWARE.24
*/26
/** \file wayland-util.h27
*28
* \brief Utility classes, functions, and macros.29
*/31
#ifndef WAYLAND_UTIL_H32
#define WAYLAND_UTIL_H34
#include <math.h>35
#include <stddef.h>36
#include <inttypes.h>37
#include <stdarg.h>39
#ifdef __cplusplus40
extern "C" {41
#endif43
/** Visibility attribute */44
#if defined(__GNUC__) && __GNUC__ >= 445
#define WL_EXPORT __attribute__ ((visibility("default")))46
#else47
#define WL_EXPORT48
#endif50
/** Deprecated attribute */51
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201402L)52
#define WL_DEPRECATED [[deprecated]]53
#elif defined(__GNUC__) && __GNUC__ >= 454
#define WL_DEPRECATED __attribute__ ((deprecated))55
#else56
#define WL_DEPRECATED57
#endif59
/**60
* Printf-style argument attribute61
*62
* \param x Ordinality of the format string argument63
* \param y Ordinality of the argument to check against the format string64
*65
* \sa https://gcc.gnu.org/onlinedocs/gcc-3.2.1/gcc/Function-Attributes.html66
*/67
#if defined(__GNUC__) && __GNUC__ >= 468
#define WL_PRINTF(x, y) __attribute__((__format__(__printf__, x, y)))69
#else70
#define WL_PRINTF(x, y)71
#endif73
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L74
#define WL_TYPEOF(expr) typeof(expr)75
#else76
#define WL_TYPEOF(expr) __typeof__(expr)77
#endif79
/** \class wl_object80
*81
* \brief A protocol object.82
*83
* A `wl_object` is an opaque struct identifying the protocol object84
* underlying a `wl_proxy` or `wl_resource`.85
*86
* \note Functions accessing a `wl_object` are not normally used by client code.87
* Clients should normally use the higher level interface generated by the88
* scanner to interact with compositor objects.89
*90
*/91
struct wl_object;93
/**94
* The maximum size of a protocol message.95
*96
* If a message size exceeds this value, the connection will be dropped.97
* Servers will send an invalid_method error before disconnecting.98
*/99
#define WL_MAX_MESSAGE_SIZE 4096101
/**102
* Protocol message signature103
*104
* A wl_message describes the signature of an actual protocol message, such as a105
* request or event, that adheres to the Wayland protocol wire format. The106
* protocol implementation uses a wl_message within its demarshal machinery for107
* decoding messages between a compositor and its clients. In a sense, a108
* wl_message is to a protocol message like a class is to an object.109
*110
* The `name` of a wl_message is the name of the corresponding protocol message.111
*112
* The `signature` is an ordered list of symbols representing the data types113
* of message arguments and, optionally, a protocol version and indicators for114
* nullability. A leading integer in the `signature` indicates the _since_115
* version of the protocol message. A `?` preceding a data type symbol indicates116
* that the following argument type is nullable. While it is a protocol violation117
* to send messages with non-nullable arguments set to `NULL`, event handlers in118
* clients might still get called with non-nullable object arguments set to119
* `NULL`. This can happen when the client destroyed the object being used as120
* argument on its side and an event referencing that object was sent before the121
* server knew about its destruction. As this race cannot be prevented, clients122
* should - as a general rule - program their event handlers such that they can123
* handle object arguments declared non-nullable being `NULL` gracefully.124
*125
* When no arguments accompany a message, `signature` is an empty string.126
*127
* Symbols:128
*129
* * `i`: int130
* * `u`: uint131
* * `f`: fixed132
* * `s`: string133
* * `o`: object134
* * `n`: new_id135
* * `a`: array136
* * `h`: fd137
* * `?`: following argument (`o` or `s`) is nullable138
*139
* While demarshaling primitive arguments is straightforward, when demarshaling140
* messages containing `object` or `new_id` arguments, the protocol141
* implementation often must determine the type of the object. The `types` of a142
* wl_message is an array of wl_interface references that correspond to `o` and143
* `n` arguments in `signature`, with `NULL` placeholders for arguments with144
* non-object types.145
*146
* Consider the protocol event wl_display `delete_id` that has a single `uint`147
* argument. The wl_message is:148
*149
* \code150
* { "delete_id", "u", [NULL] }151
* \endcode152
*153
* Here, the message `name` is `"delete_id"`, the `signature` is `"u"`, and the154
* argument `types` is `[NULL]`, indicating that the `uint` argument has no155
* corresponding wl_interface since it is a primitive argument.156
*157
* In contrast, consider a `wl_foo` interface supporting protocol request `bar`158
* that has existed since version 2, and has two arguments: a `uint` and an159
* object of type `wl_baz_interface` that may be `NULL`. Such a `wl_message`160
* might be:161
*162
* \code163
* { "bar", "2u?o", [NULL, &wl_baz_interface] }164
* \endcode165
*166
* Here, the message `name` is `"bar"`, and the `signature` is `"2u?o"`. Notice167
* how the `2` indicates the protocol version, the `u` indicates the first168
* argument type is `uint`, and the `?o` indicates that the second argument169
* is an object that may be `NULL`. Lastly, the argument `types` array indicates170
* that no wl_interface corresponds to the first argument, while the type171
* `wl_baz_interface` corresponds to the second argument.172
*173
* \sa wl_argument174
* \sa wl_interface175
* \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Wire-Format">Wire Format</a>176
*/177
struct wl_message {178
/** Message name */179
const char *name;180
/** Message signature */181
const char *signature;182
/** Object argument interfaces */183
const struct wl_interface **types;184
};186
/**187
* Protocol object interface188
*189
* A wl_interface describes the API of a protocol object defined in the Wayland190
* protocol specification. The protocol implementation uses a wl_interface191
* within its marshalling machinery for encoding client requests.192
*193
* The `name` of a wl_interface is the name of the corresponding protocol194
* interface, and `version` represents the version of the interface. The members195
* `method_count` and `event_count` represent the number of `methods` (requests)196
* and `events` in the respective wl_message members.197
*198
* For example, consider a protocol interface `foo`, marked as version `1`, with199
* two requests and one event.200
*201
* \code{.xml}202
* <interface name="foo" version="1">203
* <request name="a"></request>204
* <request name="b"></request>205
* <event name="c"></event>206
* </interface>207
* \endcode208
*209
* Given two wl_message arrays `foo_requests` and `foo_events`, a wl_interface210
* for `foo` might be:211
*212
* \code213
* struct wl_interface foo_interface = {214
* "foo", 1,215
* 2, foo_requests,216
* 1, foo_events217
* };218
* \endcode219
*220
* \note The server side of the protocol may define interface <em>implementation221
* types</em> that incorporate the term `interface` in their name. Take222
* care to not confuse these server-side `struct`s with a wl_interface223
* variable whose name also ends in `interface`. For example, while the224
* server may define a type `struct wl_foo_interface`, the client may225
* define a `struct wl_interface wl_foo_interface`.226
*227
* \sa wl_message228
* \sa wl_proxy229
* \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Interfaces">Interfaces</a>230
* \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Versioning">Versioning</a>231
*/232
struct wl_interface {233
/** Interface name */234
const char *name;235
/** Interface version */236
int version;237
/** Number of methods (requests) */238
int method_count;239
/** Method (request) signatures */240
const struct wl_message *methods;241
/** Number of events */242
int event_count;243
/** Event signatures */244
const struct wl_message *events;245
};247
/** \class wl_list248
*249
* \brief Doubly-linked list250
*251
* On its own, an instance of `struct wl_list` represents the sentinel head of252
* a doubly-linked list, and must be initialized using wl_list_init().253
* When empty, the list head's `next` and `prev` members point to the list head254
* itself, otherwise `next` references the first element in the list, and `prev`255
* refers to the last element in the list.256
*257
* Use the `struct wl_list` type to represent both the list head and the links258
* between elements within the list. Use wl_list_empty() to determine if the259
* list is empty in O(1).260
*261
* All elements in the list must be of the same type. The element type must have262
* a `struct wl_list` member, often named `link` by convention. Prior to263
* insertion, there is no need to initialize an element's `link` - invoking264
* wl_list_init() on an individual list element's `struct wl_list` member is265
* unnecessary if the very next operation is wl_list_insert(). However, a266
* common idiom is to initialize an element's `link` prior to removal - ensure267
* safety by invoking wl_list_init() before wl_list_remove().268
*269
* Consider a list reference `struct wl_list foo_list`, an element type as270
* `struct element`, and an element's link member as `struct wl_list link`.271
*272
* The following code initializes a list and adds three elements to it.273
*274
* \code275
* struct wl_list foo_list;276
*277
* struct element {278
* int foo;279
* struct wl_list link;280
* };281
* struct element e1, e2, e3;282
*283
* wl_list_init(&foo_list);284
* wl_list_insert(&foo_list, &e1.link); // e1 is the first element285
* wl_list_insert(&foo_list, &e2.link); // e2 is now the first element286
* wl_list_insert(&e2.link, &e3.link); // insert e3 after e2287
* \endcode288
*289
* The list now looks like <em>[e2, e3, e1]</em>.290
*291
* The `wl_list` API provides some iterator macros. For example, to iterate292
* a list in ascending order:293
*294
* \code295
* struct element *e;296
* wl_list_for_each(e, foo_list, link) {297
* do_something_with_element(e);298
* }299
* \endcode300
*301
* See the documentation of each iterator for details.302
* \sa http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/list.h303
*/304
struct wl_list {305
/** Previous list element */306
struct wl_list *prev;307
/** Next list element */308
struct wl_list *next;309
};311
/**312
* Initializes the list.313
*314
* \param list List to initialize315
*316
* \memberof wl_list317
*/318
void319
wl_list_init(struct wl_list *list);321
/**322
* Inserts an element into the list, after the element represented by \p list.323
* When \p list is a reference to the list itself (the head), set the containing324
* struct of \p elm as the first element in the list.325
*326
* \note If \p elm is already part of a list, inserting it again will lead to327
* list corruption.328
*329
* \param list List element after which the new element is inserted330
* \param elm Link of the containing struct to insert into the list331
*332
* \memberof wl_list333
*/334
void335
wl_list_insert(struct wl_list *list, struct wl_list *elm);337
/**338
* Removes an element from the list.339
*340
* \note This operation leaves \p elm in an invalid state.341
*342
* \param elm Link of the containing struct to remove from the list343
*344
* \memberof wl_list345
*/346
void347
wl_list_remove(struct wl_list *elm);349
/**350
* Determines the length of the list.351
*352
* \note This is an O(n) operation.353
*354
* \param list List whose length is to be determined355
*356
* \return Number of elements in the list357
*358
* \memberof wl_list359
*/360
int361
wl_list_length(const struct wl_list *list);363
/**364
* Determines if the list is empty.365
*366
* \param list List whose emptiness is to be determined367
*368
* \return 1 if empty, or 0 if not empty369
*370
* \memberof wl_list371
*/372
int373
wl_list_empty(const struct wl_list *list);375
/**376
* Inserts all of the elements of one list into another, after the element377
* represented by \p list.378
*379
* \note This leaves \p other in an invalid state.380
*381
* \param list List element after which the other list elements will be inserted382
* \param other List of elements to insert383
*384
* \memberof wl_list385
*/386
void387
wl_list_insert_list(struct wl_list *list, struct wl_list *other);389
/**390
* Retrieves a pointer to a containing struct, given a member name.391
*392
* This macro allows "conversion" from a pointer to a member to its containing393
* struct. This is useful if you have a contained item like a wl_list,394
* wl_listener, or wl_signal, provided via a callback or other means, and would395
* like to retrieve the struct that contains it.396
*397
* To demonstrate, the following example retrieves a pointer to398
* `example_container` given only its `destroy_listener` member:399
*400
* \code401
* struct example_container {402
* struct wl_listener destroy_listener;403
* // other members...404
* };405
*406
* void example_container_destroy(struct wl_listener *listener, void *data)407
* {408
* struct example_container *ctr;409
*410
* ctr = wl_container_of(listener, ctr, destroy_listener);411
* // destroy ctr...412
* }413
* \endcode414
*415
* \note `sample` need not be a valid pointer. A null or uninitialised pointer416
* is sufficient.417
*418
* \param ptr Valid pointer to the contained member419
* \param sample Pointer to a struct whose type contains \p ptr420
* \param member Named location of \p ptr within the \p sample type421
*422
* \return The container for the specified pointer423
*/424
#define wl_container_of(ptr, sample, member) \425
(WL_TYPEOF(sample))((char *)(ptr) - \426
offsetof(WL_TYPEOF(*sample), member))428
/**429
* Iterates over a list.430
*431
* This macro expresses a for-each iterator for wl_list. Given a list and432
* wl_list link member name (often named `link` by convention), this macro433
* assigns each element in the list to \p pos, which can then be referenced in434
* a trailing code block. For example, given a wl_list of `struct message`435
* elements:436
*437
* \code438
* struct message {439
* char *contents;440
* wl_list link;441
* };442
*443
* struct wl_list *message_list;444
* // Assume message_list now "contains" many messages445
*446
* struct message *m;447
* wl_list_for_each(m, message_list, link) {448
* do_something_with_message(m);449
* }450
* \endcode451
*452
* \param pos Cursor that each list element will be assigned to453
* \param head Head of the list to iterate over454
* \param member Name of the link member within the element struct455
*456
* \relates wl_list457
*/458
#define wl_list_for_each(pos, head, member) \459
for (pos = wl_container_of((head)->next, pos, member); \460
&pos->member != (head); \461
pos = wl_container_of(pos->member.next, pos, member))463
/**464
* Iterates over a list, safe against removal of the list element.465
*466
* \note Only removal of the current element, \p pos, is safe. Removing467
* 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 to472
* \param tmp Temporary pointer of the same type as \p pos473
* \param head Head of the list to iterate over474
* \param member Name of the link member within the element struct475
*476
* \relates wl_list477
*/478
#define wl_list_for_each_safe(pos, tmp, head, member) \479
for (pos = wl_container_of((head)->next, pos, member), \480
tmp = wl_container_of((pos)->member.next, tmp, member); \481
&pos->member != (head); \482
pos = tmp, \483
tmp = wl_container_of(pos->member.next, tmp, member))485
/**486
* Iterates backwards over a list.487
*488
* \sa wl_list_for_each()489
*490
* \param pos Cursor that each list element will be assigned to491
* \param head Head of the list to iterate over492
* \param member Name of the link member within the element struct493
*494
* \relates wl_list495
*/496
#define wl_list_for_each_reverse(pos, head, member) \497
for (pos = wl_container_of((head)->prev, pos, member); \498
&pos->member != (head); \499
pos = wl_container_of(pos->member.prev, pos, member))501
/**502
* Iterates backwards over a list, safe against removal of the list element.503
*504
* \note Only removal of the current element, \p pos, is safe. Removing505
* any other element during traversal may lead to a loop malfunction.506
*507
* \sa wl_list_for_each()508
*509
* \param pos Cursor that each list element will be assigned to510
* \param tmp Temporary pointer of the same type as \p pos511
* \param head Head of the list to iterate over512
* \param member Name of the link member within the element struct513
*514
* \relates wl_list515
*/516
#define wl_list_for_each_reverse_safe(pos, tmp, head, member) \517
for (pos = wl_container_of((head)->prev, pos, member), \518
tmp = wl_container_of((pos)->member.prev, tmp, member); \519
&pos->member != (head); \520
pos = tmp, \521
tmp = wl_container_of(pos->member.prev, tmp, member))523
/**524
* \class wl_array525
*526
* Dynamic array527
*528
* A wl_array is a dynamic array that can only grow until released. It is529
* intended for relatively small allocations whose size is variable or not known530
* in advance. While construction of a wl_array does not require all elements to531
* be of the same size, wl_array_for_each() does require all elements to have532
* the same type and size.533
*534
*/535
struct wl_array {536
/** Array size */537
size_t size;538
/** Allocated space */539
size_t alloc;540
/** Array data */541
void *data;542
};544
/**545
* Initializes the array.546
*547
* \param array Array to initialize548
*549
* \memberof wl_array550
*/551
void552
wl_array_init(struct wl_array *array);554
/**555
* Releases the array data.556
*557
* \note Leaves the array in an invalid state.558
*559
* \param array Array whose data is to be released560
*561
* \memberof wl_array562
*/563
void564
wl_array_release(struct wl_array *array);566
/**567
* Increases the size of the array by \p size bytes.568
*569
* \param array Array whose size is to be increased570
* \param size Number of bytes to increase the size of the array by571
*572
* \return A pointer to the beginning of the newly appended space, or NULL when573
* resizing fails.574
*575
* \memberof wl_array576
*/577
void *578
wl_array_add(struct wl_array *array, size_t size);580
/**581
* Copies the contents of \p source to \p array.582
*583
* \param array Destination array to copy to584
* \param source Source array to copy from585
*586
* \return 0 on success, or -1 on failure587
*588
* \memberof wl_array589
*/590
int591
wl_array_copy(struct wl_array *array, struct wl_array *source);593
/**594
* Iterates over an array.595
*596
* This macro expresses a for-each iterator for wl_array. It assigns each597
* element in the array to \p pos, which can then be referenced in a trailing598
* code block. \p pos must be a pointer to the array element type, and all599
* array elements must be of the same type and size.600
*601
* \param pos Cursor that each array element will be assigned to602
* \param array Array to iterate over603
*604
* \relates wl_array605
* \sa wl_list_for_each()606
*/607
#define wl_array_for_each(pos, array) \608
for (pos = (array)->data; \609
(array)->size != 0 && \610
(const char *) pos < ((const char *) (array)->data + (array)->size); \611
(pos)++)613
/**614
* Fixed-point number615
*616
* A `wl_fixed_t` is a 24.8 signed fixed-point number with a sign bit, 23 bits617
* of integer precision and 8 bits of decimal precision. Consider `wl_fixed_t`618
* as an opaque struct with methods that facilitate conversion to and from619
* `double` and `int` types.620
*/621
typedef int32_t wl_fixed_t;623
/**624
* Converts a fixed-point number to a floating-point number.625
*626
* \param f Fixed-point number to convert627
*628
* \return Floating-point representation of the fixed-point argument629
*/630
static inline double631
wl_fixed_to_double(wl_fixed_t f)632
{633
return f / 256.0;634
}636
/**637
* Converts a floating-point number to a fixed-point number.638
*639
* \param d Floating-point number to convert640
*641
* \return Fixed-point representation of the floating-point argument642
*/643
static inline wl_fixed_t644
wl_fixed_from_double(double d)645
{646
return (wl_fixed_t) (round(d * 256.0));647
}649
/**650
* Converts a fixed-point number to an integer.651
*652
* \param f Fixed-point number to convert653
*654
* \return Integer component of the fixed-point argument655
*/656
static inline int657
wl_fixed_to_int(wl_fixed_t f)658
{659
return f / 256;660
}662
/**663
* Converts an integer to a fixed-point number.664
*665
* \param i Integer to convert666
*667
* \return Fixed-point representation of the integer argument668
*/669
static inline wl_fixed_t670
wl_fixed_from_int(int i)671
{672
return i * 256;673
}675
/**676
* Protocol message argument data types677
*678
* This union represents all of the argument types in the Wayland protocol wire679
* format. The protocol implementation uses wl_argument within its marshalling680
* machinery for dispatching messages between a client and a compositor.681
*682
* \sa wl_message683
* \sa wl_interface684
* \sa <a href="https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-wire-Format">Wire Format</a>685
*/686
union wl_argument {687
int32_t i; /**< `int` */688
uint32_t u; /**< `uint` */689
wl_fixed_t f; /**< `fixed` */690
const char *s; /**< `string` */691
struct wl_object *o; /**< `object` */692
uint32_t n; /**< `new_id` */693
struct wl_array *a; /**< `array` */694
int32_t h; /**< `fd` */695
};697
/**698
* Dispatcher function type alias699
*700
* A dispatcher is a function that handles the emitting of callbacks in client701
* code. For programs directly using the C library, this is done by using702
* libffi to call function pointers. When binding to languages other than C,703
* dispatchers provide a way to abstract the function calling process to be704
* friendlier to other function calling systems.705
*706
* A dispatcher takes five arguments: The first is the dispatcher-specific707
* implementation associated with the target object. The second is the object708
* upon which the callback is being invoked (either wl_proxy or wl_resource).709
* The third and fourth arguments are the opcode and the wl_message710
* corresponding to the callback. The final argument is an array of arguments711
* received from the other process via the wire protocol.712
*713
* \param user_data Dispatcher-specific implementation data714
* \param target Callback invocation target (wl_proxy or `wl_resource`)715
* \param opcode Callback opcode716
* \param msg Callback message signature717
* \param args Array of received arguments718
*719
* \return 0 on success, or -1 on failure720
*/721
typedef int (*wl_dispatcher_func_t)(const void *user_data, void *target,722
uint32_t opcode, const struct wl_message *msg,723
union wl_argument *args);725
/**726
* Log function type alias727
*728
* The C implementation of the Wayland protocol abstracts the details of729
* logging. Users may customize the logging behavior, with a function conforming730
* to the `wl_log_func_t` type, via `wl_log_set_handler_client` and731
* `wl_log_set_handler_server`.732
*733
* A `wl_log_func_t` must conform to the expectations of `vprintf`, and734
* expects two arguments: a string to write and a corresponding variable735
* argument list. While the string to write may contain format specifiers and736
* use values in the variable argument list, the behavior of any `wl_log_func_t`737
* depends on the implementation.738
*739
* \note Take care to not confuse this with `wl_protocol_logger_func_t`, which740
* is a specific server-side logger for requests and events.741
*742
* \param fmt String to write to the log, containing optional format743
* specifiers744
* \param args Variable argument list745
*746
* \sa wl_log_set_handler_client747
* \sa wl_log_set_handler_server748
*/749
typedef void (*wl_log_func_t)(const char *fmt, va_list args) WL_PRINTF(1, 0);751
/**752
* Return value of an iterator function753
*754
* \sa wl_client_for_each_resource_iterator_func_t755
* \sa wl_client_for_each_resource756
*/757
enum wl_iterator_result {758
/** Stop the iteration */759
WL_ITERATOR_STOP,760
/** Continue the iteration */761
WL_ITERATOR_CONTINUE762
};764
#ifdef __cplusplus765
}766
#endif768
#endif