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.

519 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. va_end(args);
  319. return -1;
  320. }
  321. }
  322. va_end(args);
  323. LV2_ATOM_OBJECT_BODY_FOREACH(body, size, prop) {
  324. va_start(args, body);
  325. for (int i = 0; i < n_queries; ++i) {
  326. uint32_t qkey = va_arg(args, uint32_t);
  327. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  328. if (qkey == prop->key && !*qval) {
  329. *qval = &prop->value;
  330. if (++matches == n_queries) {
  331. va_end(args);
  332. return matches;
  333. }
  334. break;
  335. }
  336. }
  337. va_end(args);
  338. }
  339. return matches;
  340. }
  341. /**
  342. Variable argument version of lv2_atom_object_query().
  343. This is nicer-looking in code, but a bit more error-prone since it is not
  344. type safe and the argument list must be terminated.
  345. The arguments should be a series of uint32_t key and const LV2_Atom** value
  346. pairs, terminated by a zero key. The value pointers MUST be initialized to
  347. NULL. For example:
  348. @code
  349. const LV2_Atom* name = NULL;
  350. const LV2_Atom* age = NULL;
  351. lv2_atom_object_get(obj,
  352. uris.name_key, &name,
  353. uris.age_key, &age,
  354. 0);
  355. @endcode
  356. */
  357. static inline int
  358. lv2_atom_object_get(const LV2_Atom_Object* object, ...)
  359. {
  360. int matches = 0;
  361. int n_queries = 0;
  362. /* Count number of keys so we can short-circuit when done */
  363. va_list args;
  364. va_start(args, object);
  365. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  366. if (!va_arg(args, const LV2_Atom**)) {
  367. return -1;
  368. }
  369. }
  370. va_end(args);
  371. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  372. va_start(args, object);
  373. for (int i = 0; i < n_queries; ++i) {
  374. uint32_t qkey = va_arg(args, uint32_t);
  375. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  376. if (qkey == prop->key && !*qval) {
  377. *qval = &prop->value;
  378. if (++matches == n_queries) {
  379. return matches;
  380. }
  381. break;
  382. }
  383. }
  384. va_end(args);
  385. }
  386. return matches;
  387. }
  388. /**
  389. Variable argument version of lv2_atom_object_query() with types.
  390. This is like lv2_atom_object_get(), but each entry has an additional
  391. parameter to specify the required type. Only atoms with a matching type
  392. will be selected.
  393. The arguments should be a series of uint32_t key, const LV2_Atom**, uint32_t
  394. type triples, terminated by a zero key. The value pointers MUST be
  395. initialized to NULL. For example:
  396. @code
  397. const LV2_Atom_String* name = NULL;
  398. const LV2_Atom_Int* age = NULL;
  399. lv2_atom_object_get(obj,
  400. uris.name_key, &name, uris.atom_String,
  401. uris.age_key, &age, uris.atom_Int
  402. 0);
  403. @endcode
  404. */
  405. static inline int
  406. lv2_atom_object_get_typed(const LV2_Atom_Object* object, ...)
  407. {
  408. int matches = 0;
  409. int n_queries = 0;
  410. /* Count number of keys so we can short-circuit when done */
  411. va_list args;
  412. va_start(args, object);
  413. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  414. if (!va_arg(args, const LV2_Atom**) ||
  415. !va_arg(args, uint32_t)) {
  416. return -1;
  417. }
  418. }
  419. va_end(args);
  420. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  421. va_start(args, object);
  422. for (int i = 0; i < n_queries; ++i) {
  423. const uint32_t qkey = va_arg(args, uint32_t);
  424. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  425. const uint32_t qtype = va_arg(args, uint32_t);
  426. if (!*qval && qkey == prop->key && qtype == prop->value.type) {
  427. *qval = &prop->value;
  428. if (++matches == n_queries) {
  429. return matches;
  430. }
  431. break;
  432. }
  433. }
  434. va_end(args);
  435. }
  436. return matches;
  437. }
  438. /**
  439. @}
  440. @}
  441. */
  442. #ifdef __cplusplus
  443. } /* extern "C" */
  444. #endif
  445. #endif /* LV2_ATOM_UTIL_H */