Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

517 lines
14KB

  1. /*
  2. Copyright 2008-2015 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. /**
  15. @file util.h Helper functions for the LV2 Atom extension.
  16. Note these functions are all static inline, do not take their address.
  17. This header is non-normative, it is provided for convenience.
  18. */
  19. /**
  20. @defgroup util Utilities
  21. @ingroup atom
  22. @{
  23. */
  24. #ifndef LV2_ATOM_UTIL_H
  25. #define LV2_ATOM_UTIL_H
  26. #include <stdarg.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "atom.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #else
  33. # include <stdbool.h>
  34. #endif
  35. /** Pad a size to 64 bits. */
  36. static inline uint32_t
  37. lv2_atom_pad_size(uint32_t size)
  38. {
  39. return (size + 7U) & (~7U);
  40. }
  41. /** Return the total size of `atom`, including the header. */
  42. static inline uint32_t
  43. lv2_atom_total_size(const LV2_Atom* atom)
  44. {
  45. return (uint32_t)sizeof(LV2_Atom) + atom->size;
  46. }
  47. /** Return true iff `atom` is null. */
  48. static inline bool
  49. lv2_atom_is_null(const LV2_Atom* atom)
  50. {
  51. return !atom || (atom->type == 0 && atom->size == 0);
  52. }
  53. /** Return true iff `a` is equal to `b`. */
  54. static inline bool
  55. lv2_atom_equals(const LV2_Atom* a, const LV2_Atom* b)
  56. {
  57. return (a == b) || ((a->type == b->type) &&
  58. (a->size == b->size) &&
  59. !memcmp(a + 1, b + 1, a->size));
  60. }
  61. /**
  62. @name Sequence Iterator
  63. @{
  64. */
  65. /** Get an iterator pointing to the first event in a Sequence body. */
  66. static inline const LV2_Atom_Event*
  67. lv2_atom_sequence_begin(const LV2_Atom_Sequence_Body* body)
  68. {
  69. return (const LV2_Atom_Event*)(body + 1);
  70. }
  71. /** Get an iterator pointing to the end of a Sequence body. */
  72. static inline const LV2_Atom_Event*
  73. lv2_atom_sequence_end(const LV2_Atom_Sequence_Body* body, uint32_t size)
  74. {
  75. return (const LV2_Atom_Event*)((const uint8_t*)body + lv2_atom_pad_size(size));
  76. }
  77. /** Get an iterator pointing to the end of a Sequence body. */
  78. static inline LV2_Atom_Event*
  79. lv2_atom_sequence_end2(LV2_Atom_Sequence_Body* body, uint32_t size)
  80. {
  81. return (LV2_Atom_Event*)((uint8_t*)body + lv2_atom_pad_size(size));
  82. }
  83. /** Return true iff `i` has reached the end of `body`. */
  84. static inline bool
  85. lv2_atom_sequence_is_end(const LV2_Atom_Sequence_Body* body,
  86. uint32_t size,
  87. const LV2_Atom_Event* i)
  88. {
  89. return (const uint8_t*)i >= ((const uint8_t*)body + size);
  90. }
  91. /** Return an iterator to the element following `i`. */
  92. static inline const LV2_Atom_Event*
  93. lv2_atom_sequence_next(const LV2_Atom_Event* i)
  94. {
  95. return (const LV2_Atom_Event*)((const uint8_t*)i
  96. + sizeof(LV2_Atom_Event)
  97. + lv2_atom_pad_size(i->body.size));
  98. }
  99. /**
  100. A macro for iterating over all events in a Sequence.
  101. @param seq The sequence to iterate over
  102. @param iter The name of the iterator
  103. This macro is used similarly to a for loop (which it expands to), e.g.:
  104. @code
  105. LV2_ATOM_SEQUENCE_FOREACH(sequence, ev) {
  106. // Do something with ev (an LV2_Atom_Event*) here...
  107. }
  108. @endcode
  109. */
  110. #define LV2_ATOM_SEQUENCE_FOREACH(seq, iter) \
  111. for (const LV2_Atom_Event* iter = lv2_atom_sequence_begin(&(seq)->body); \
  112. !lv2_atom_sequence_is_end(&(seq)->body, (seq)->atom.size, (iter)); \
  113. iter = lv2_atom_sequence_next(iter))
  114. /** Like LV2_ATOM_SEQUENCE_FOREACH but for a headerless sequence body. */
  115. #define LV2_ATOM_SEQUENCE_BODY_FOREACH(body, size, iter) \
  116. for (const LV2_Atom_Event* iter = lv2_atom_sequence_begin(body); \
  117. !lv2_atom_sequence_is_end(body, size, (iter)); \
  118. iter = lv2_atom_sequence_next(iter))
  119. /**
  120. @}
  121. @name Sequence Utilities
  122. @{
  123. */
  124. /**
  125. Clear all events from `sequence`.
  126. This simply resets the size field, the other fields are left untouched.
  127. */
  128. static inline void
  129. lv2_atom_sequence_clear(LV2_Atom_Sequence* seq)
  130. {
  131. seq->atom.size = sizeof(LV2_Atom_Sequence_Body);
  132. }
  133. /**
  134. Append an event at the end of `sequence`.
  135. @param seq Sequence to append to.
  136. @param capacity Total capacity of the sequence atom
  137. (e.g. as set by the host for sequence output ports).
  138. @param event Event to write.
  139. @return A pointer to the newly written event in `seq`,
  140. or NULL on failure (insufficient space).
  141. */
  142. static inline LV2_Atom_Event*
  143. lv2_atom_sequence_append_event(LV2_Atom_Sequence* seq,
  144. uint32_t capacity,
  145. const LV2_Atom_Event* event)
  146. {
  147. const uint32_t total_size = (uint32_t)sizeof(*event) + event->body.size;
  148. if (capacity - seq->atom.size < total_size) {
  149. return NULL;
  150. }
  151. LV2_Atom_Event* e = lv2_atom_sequence_end2(&seq->body, seq->atom.size);
  152. memcpy(e, event, total_size);
  153. seq->atom.size += lv2_atom_pad_size(total_size);
  154. return e;
  155. }
  156. /**
  157. @}
  158. @name Tuple Iterator
  159. @{
  160. */
  161. /** Get an iterator pointing to the first element in `tup`. */
  162. static inline const LV2_Atom*
  163. lv2_atom_tuple_begin(const LV2_Atom_Tuple* tup)
  164. {
  165. return (const LV2_Atom*)(LV2_ATOM_BODY_CONST(tup));
  166. }
  167. /** Return true iff `i` has reached the end of `body`. */
  168. static inline bool
  169. lv2_atom_tuple_is_end(const void* body, uint32_t size, const LV2_Atom* i)
  170. {
  171. return (const uint8_t*)i >= ((const uint8_t*)body + size);
  172. }
  173. /** Return an iterator to the element following `i`. */
  174. static inline const LV2_Atom*
  175. lv2_atom_tuple_next(const LV2_Atom* i)
  176. {
  177. return (const LV2_Atom*)(
  178. (const uint8_t*)i + sizeof(LV2_Atom) + lv2_atom_pad_size(i->size));
  179. }
  180. /**
  181. A macro for iterating over all properties of a Tuple.
  182. @param tuple The tuple to iterate over
  183. @param iter The name of the iterator
  184. This macro is used similarly to a for loop (which it expands to), e.g.:
  185. @code
  186. LV2_ATOM_TUPLE_FOREACH(tuple, elem) {
  187. // Do something with elem (an LV2_Atom*) here...
  188. }
  189. @endcode
  190. */
  191. #define LV2_ATOM_TUPLE_FOREACH(tuple, iter) \
  192. for (const LV2_Atom* iter = lv2_atom_tuple_begin(tuple); \
  193. !lv2_atom_tuple_is_end(LV2_ATOM_BODY_CONST(tuple), (tuple)->atom.size, (iter)); \
  194. iter = lv2_atom_tuple_next(iter))
  195. /** Like LV2_ATOM_TUPLE_FOREACH but for a headerless tuple body. */
  196. #define LV2_ATOM_TUPLE_BODY_FOREACH(body, size, iter) \
  197. for (const LV2_Atom* iter = (const LV2_Atom*)body; \
  198. !lv2_atom_tuple_is_end(body, size, (iter)); \
  199. iter = lv2_atom_tuple_next(iter))
  200. /**
  201. @}
  202. @name Object Iterator
  203. @{
  204. */
  205. /** Return a pointer to the first property in `body`. */
  206. static inline const LV2_Atom_Property_Body*
  207. lv2_atom_object_begin(const LV2_Atom_Object_Body* body)
  208. {
  209. return (const LV2_Atom_Property_Body*)(body + 1);
  210. }
  211. /** Return true iff `i` has reached the end of `obj`. */
  212. static inline bool
  213. lv2_atom_object_is_end(const LV2_Atom_Object_Body* body,
  214. uint32_t size,
  215. const LV2_Atom_Property_Body* i)
  216. {
  217. return (const uint8_t*)i >= ((const uint8_t*)body + size);
  218. }
  219. /** Return an iterator to the property following `i`. */
  220. static inline const LV2_Atom_Property_Body*
  221. lv2_atom_object_next(const LV2_Atom_Property_Body* i)
  222. {
  223. const LV2_Atom* const value = (const LV2_Atom*)(
  224. (const uint8_t*)i + 2 * sizeof(uint32_t));
  225. return (const LV2_Atom_Property_Body*)(
  226. (const uint8_t*)i + lv2_atom_pad_size(
  227. (uint32_t)sizeof(LV2_Atom_Property_Body) + value->size));
  228. }
  229. /**
  230. A macro for iterating over all properties of an Object.
  231. @param obj The object to iterate over
  232. @param iter The name of the iterator
  233. This macro is used similarly to a for loop (which it expands to), e.g.:
  234. @code
  235. LV2_ATOM_OBJECT_FOREACH(object, i) {
  236. // Do something with prop (an LV2_Atom_Property_Body*) here...
  237. }
  238. @endcode
  239. */
  240. #define LV2_ATOM_OBJECT_FOREACH(obj, iter) \
  241. for (const LV2_Atom_Property_Body* iter = lv2_atom_object_begin(&(obj)->body); \
  242. !lv2_atom_object_is_end(&(obj)->body, (obj)->atom.size, (iter)); \
  243. iter = lv2_atom_object_next(iter))
  244. /** Like LV2_ATOM_OBJECT_FOREACH but for a headerless object body. */
  245. #define LV2_ATOM_OBJECT_BODY_FOREACH(body, size, iter) \
  246. for (const LV2_Atom_Property_Body* iter = lv2_atom_object_begin(body); \
  247. !lv2_atom_object_is_end(body, size, (iter)); \
  248. iter = lv2_atom_object_next(iter))
  249. /**
  250. @}
  251. @name Object Query
  252. @{
  253. */
  254. /** A single entry in an Object query. */
  255. typedef struct {
  256. uint32_t key; /**< Key to query (input set by user) */
  257. const LV2_Atom** value; /**< Found value (output set by query function) */
  258. } LV2_Atom_Object_Query;
  259. static const LV2_Atom_Object_Query LV2_ATOM_OBJECT_QUERY_END = { 0, NULL };
  260. /**
  261. Get an object's values for various keys.
  262. The value pointer of each item in `query` will be set to the location of
  263. the corresponding value in `object`. Every value pointer in `query` MUST
  264. be initialised to NULL. This function reads `object` in a single linear
  265. sweep. By allocating `query` on the stack, objects can be "queried"
  266. quickly without allocating any memory. This function is realtime safe.
  267. This function can only do "flat" queries, it is not smart enough to match
  268. variables in nested objects.
  269. For example:
  270. @code
  271. const LV2_Atom* name = NULL;
  272. const LV2_Atom* age = NULL;
  273. LV2_Atom_Object_Query q[] = {
  274. { urids.eg_name, &name },
  275. { urids.eg_age, &age },
  276. LV2_ATOM_OBJECT_QUERY_END
  277. };
  278. lv2_atom_object_query(obj, q);
  279. // name and age are now set to the appropriate values in obj, or NULL.
  280. @endcode
  281. */
  282. static inline int
  283. lv2_atom_object_query(const LV2_Atom_Object* object,
  284. LV2_Atom_Object_Query* query)
  285. {
  286. int matches = 0;
  287. int n_queries = 0;
  288. /* Count number of query keys so we can short-circuit when done */
  289. for (LV2_Atom_Object_Query* q = query; q->key; ++q) {
  290. ++n_queries;
  291. }
  292. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  293. for (LV2_Atom_Object_Query* q = query; q->key; ++q) {
  294. if (q->key == prop->key && !*q->value) {
  295. *q->value = &prop->value;
  296. if (++matches == n_queries) {
  297. return matches;
  298. }
  299. break;
  300. }
  301. }
  302. }
  303. return matches;
  304. }
  305. /**
  306. Body only version of lv2_atom_object_get().
  307. */
  308. static inline int
  309. lv2_atom_object_body_get(uint32_t size, const LV2_Atom_Object_Body* body, ...)
  310. {
  311. int matches = 0;
  312. int n_queries = 0;
  313. /* Count number of keys so we can short-circuit when done */
  314. va_list args;
  315. va_start(args, body);
  316. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  317. if (!va_arg(args, const LV2_Atom**)) {
  318. return -1;
  319. }
  320. }
  321. va_end(args);
  322. LV2_ATOM_OBJECT_BODY_FOREACH(body, size, prop) {
  323. va_start(args, body);
  324. for (int i = 0; i < n_queries; ++i) {
  325. uint32_t qkey = va_arg(args, uint32_t);
  326. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  327. if (qkey == prop->key && !*qval) {
  328. *qval = &prop->value;
  329. if (++matches == n_queries) {
  330. return matches;
  331. }
  332. break;
  333. }
  334. }
  335. va_end(args);
  336. }
  337. return matches;
  338. }
  339. /**
  340. Variable argument version of lv2_atom_object_query().
  341. This is nicer-looking in code, but a bit more error-prone since it is not
  342. type safe and the argument list must be terminated.
  343. The arguments should be a series of uint32_t key and const LV2_Atom** value
  344. pairs, terminated by a zero key. The value pointers MUST be initialized to
  345. NULL. For example:
  346. @code
  347. const LV2_Atom* name = NULL;
  348. const LV2_Atom* age = NULL;
  349. lv2_atom_object_get(obj,
  350. uris.name_key, &name,
  351. uris.age_key, &age,
  352. 0);
  353. @endcode
  354. */
  355. static inline int
  356. lv2_atom_object_get(const LV2_Atom_Object* object, ...)
  357. {
  358. int matches = 0;
  359. int n_queries = 0;
  360. /* Count number of keys so we can short-circuit when done */
  361. va_list args;
  362. va_start(args, object);
  363. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  364. if (!va_arg(args, const LV2_Atom**)) {
  365. return -1;
  366. }
  367. }
  368. va_end(args);
  369. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  370. va_start(args, object);
  371. for (int i = 0; i < n_queries; ++i) {
  372. uint32_t qkey = va_arg(args, uint32_t);
  373. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  374. if (qkey == prop->key && !*qval) {
  375. *qval = &prop->value;
  376. if (++matches == n_queries) {
  377. return matches;
  378. }
  379. break;
  380. }
  381. }
  382. va_end(args);
  383. }
  384. return matches;
  385. }
  386. /**
  387. Variable argument version of lv2_atom_object_query() with types.
  388. This is like lv2_atom_object_get(), but each entry has an additional
  389. parameter to specify the required type. Only atoms with a matching type
  390. will be selected.
  391. The arguments should be a series of uint32_t key, const LV2_Atom**, uint32_t
  392. type triples, terminated by a zero key. The value pointers MUST be
  393. initialized to NULL. For example:
  394. @code
  395. const LV2_Atom_String* name = NULL;
  396. const LV2_Atom_Int* age = NULL;
  397. lv2_atom_object_get(obj,
  398. uris.name_key, &name, uris.atom_String,
  399. uris.age_key, &age, uris.atom_Int
  400. 0);
  401. @endcode
  402. */
  403. static inline int
  404. lv2_atom_object_get_typed(const LV2_Atom_Object* object, ...)
  405. {
  406. int matches = 0;
  407. int n_queries = 0;
  408. /* Count number of keys so we can short-circuit when done */
  409. va_list args;
  410. va_start(args, object);
  411. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  412. if (!va_arg(args, const LV2_Atom**) ||
  413. !va_arg(args, uint32_t)) {
  414. return -1;
  415. }
  416. }
  417. va_end(args);
  418. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  419. va_start(args, object);
  420. for (int i = 0; i < n_queries; ++i) {
  421. const uint32_t qkey = va_arg(args, uint32_t);
  422. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  423. const uint32_t qtype = va_arg(args, uint32_t);
  424. if (!*qval && qkey == prop->key && qtype == prop->value.type) {
  425. *qval = &prop->value;
  426. if (++matches == n_queries) {
  427. return matches;
  428. }
  429. break;
  430. }
  431. }
  432. va_end(args);
  433. }
  434. return matches;
  435. }
  436. /**
  437. @}
  438. @}
  439. */
  440. #ifdef __cplusplus
  441. } /* extern "C" */
  442. #endif
  443. #endif /* LV2_ATOM_UTIL_H */