Debian Codegraph @

lib/wp/spa-pod.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "spa-pod.h"
10 #include "spa-type.h"
11 #include "log.h"
12
13 #include <spa/utils/type-info.h>
14 #include <spa/pod/builder.h>
15 #include <spa/pod/parser.h>
16 #include <spa/pod/filter.h>
17
18 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-spa-pod")
19
20 #define WP_SPA_POD_BUILDER_REALLOC_STEP_SIZE 64
21 #define WP_SPA_POD_ID_PROPERTY_NAME_MAX 16
22
23 /*! \defgroup wpspapod WpSpaPod */
24 /*!
25 * \struct WpSpaPod
26 */
27 /*!
28 * \struct WpSpaPodBuilder
29 */
30 /*!
31 * \struct WpSpaPodParser
32 */
33
34 enum {
35 FLAG_NO_OWNERSHIP = (1 << 0),
36 FLAG_CONSTANT = (1 << 1)
37 };
38
39 typedef enum {
40 WP_SPA_POD_REGULAR = 0,
41 WP_SPA_POD_PROPERTY,
42 WP_SPA_POD_CONTROL,
43 } WpSpaPodType;
44
45 struct _WpSpaPod
46 {
47 grefcount ref;
48 guint32 flags;
49
50 /* The pipewire spa pod API does not have a type for Property and Control,
51 * so we create our own and separate them with their data from the regular
52 * spa pod types */
53 WpSpaPodType type;
54
55 /* Pod */
56 union {
57 struct spa_pod pod_none;
58 struct spa_pod_bool pod_bool;
59 struct spa_pod_id pod_id;
60 struct spa_pod_int pod_int;
61 struct spa_pod_long pod_long;
62 struct spa_pod_float pod_float;
63 struct spa_pod_double pod_double;
64 struct spa_pod_pointer pod_pointer;
65 struct spa_pod_fd pod_fd;
66 struct spa_pod_rectangle pod_rectangle;
67 struct spa_pod_fraction pod_fraction;
68 struct wp_property_data {
69 WpSpaIdTable table;
70 guint32 key;
71 guint32 flags;
72 gchar id_key_name[WP_SPA_POD_ID_PROPERTY_NAME_MAX];
73 } data_property; /* Only used for property pods */
74 struct wp_control_data {
75 guint32 offset;
76 enum spa_control_type type;
77 } data_control; /* Only used for control pods */
78 } static_pod; /* Only used for statically allocated pods */
79 WpSpaPodBuilder *builder; /* Only used for dynamically allocated pods */
80 struct spa_pod *pod;
81 };
82
83 G_DEFINE_BOXED_TYPE (WpSpaPod, wp_spa_pod, wp_spa_pod_ref, wp_spa_pod_unref)
84
85 struct _WpSpaPodBuilder
86 {
87 struct spa_pod_builder builder;
88 struct spa_pod_frame frame;
89 WpSpaType type;
90 size_t size;
91 guint8 *buf;
92 };
93
94 G_DEFINE_BOXED_TYPE (WpSpaPodBuilder, wp_spa_pod_builder,
95 wp_spa_pod_builder_ref, wp_spa_pod_builder_unref)
96
97 struct _WpSpaPodParser
98 {
99 struct spa_pod_parser parser;
100 struct spa_pod_frame frame;
101 WpSpaType type;
102 WpSpaPod *pod;
103 };
104
105 G_DEFINE_BOXED_TYPE (WpSpaPodParser, wp_spa_pod_parser,
106 wp_spa_pod_parser_ref, wp_spa_pod_parser_unref)
107
108 static int
109 wp_spa_pod_builder_overflow (gpointer data, uint32_t size)
110 {
111 WpSpaPodBuilder *self = data;
112 const uint32_t next_size = self->size + WP_SPA_POD_BUILDER_REALLOC_STEP_SIZE;
113 const uint32_t new_size = size > next_size ? size : next_size;
114 self->buf = g_realloc (self->buf, new_size);
115 self->builder.data = self->buf;
116 self->builder.size = new_size;
117 self->size = new_size;
118 return 0;
119 }
120
121 static const struct spa_pod_builder_callbacks builder_callbacks = {
122 SPA_VERSION_POD_BUILDER_CALLBACKS,
123 .overflow = wp_spa_pod_builder_overflow
124 };
125
126 static WpSpaPodBuilder *
127 wp_spa_pod_builder_new (size_t size, WpSpaType type)
128 {
129 WpSpaPodBuilder *self = g_rc_box_new0 (WpSpaPodBuilder);
130 self->size = size;
131 self->buf = g_new0 (guint8, self->size);
132 self->builder = SPA_POD_BUILDER_INIT (self->buf, self->size);
133 self->type = type;
134
135 spa_pod_builder_set_callbacks (&self->builder, &builder_callbacks, self);
136
137 return self;
138 }
139
140 /*!
141 * \brief Increases the reference count of a spa pod object
142 * \ingroup wpspapod
143 * \param self a spa pod object
144 * \returns (transfer full): \a self with an additional reference count on it
145 */
146 WpSpaPod *
147 wp_spa_pod_ref (WpSpaPod *self)
148 {
149 g_ref_count_inc (&self->ref);
150 return self;
151 }
152
153 static void
154 wp_spa_pod_free (WpSpaPod *self)
155 {
156 g_clear_pointer (&self->builder, wp_spa_pod_builder_unref);
157 self->pod = NULL;
158 g_slice_free (WpSpaPod, self);
159 }
160
161 /*!
162 * \brief Decreases the reference count on \a self and frees it when the ref
163 * count reaches zero.
164 * \ingroup wpspapod
165 * \param self (transfer full): a spa pod object
166 */
167 void
168 wp_spa_pod_unref (WpSpaPod *self)
169 {
170 if (g_ref_count_dec (&self->ref))
171 wp_spa_pod_free (self);
172 }
173
174 static WpSpaPod *
175 wp_spa_pod_new (const struct spa_pod *pod, WpSpaPodType type, guint32 flags)
176 {
177 WpSpaPod *self = g_slice_new0 (WpSpaPod);
178 g_ref_count_init (&self->ref);
179 self->flags = flags;
180 self->type = type;
181
182 /* Copy the reference if no ownership, otherwise copy the pod */
183 if (self->flags & FLAG_NO_OWNERSHIP) {
184 self->pod = (struct spa_pod *)pod;
185 } else {
186 self->builder = wp_spa_pod_builder_new (
187 SPA_ROUND_UP_N (sizeof (*pod) + pod->size, 8), pod->type);
188 self->pod = self->builder->builder.data;
189 spa_pod_builder_primitive (&self->builder->builder, pod);
190 }
191
192 /* Set the prop table if it is an object */
193 if (pod->type == SPA_TYPE_Object) {
194 self->static_pod.data_property.table =
195 wp_spa_type_get_values_table (((struct spa_pod_object *) pod)->body.type);
196 }
197
198 return self;
199 }
200
201 /*!
202 * \brief Constructs a new WpSpaPod that wraps the given `spa_pod`.
203 *
204 * \ingroup wpspapod
205 * \param pod a spa_pod
206 * \returns a new WpSpaPod that references the data in \a pod. \a pod is not
207 * copied, so it needs to stay alive. The returned WpSpaPod can be modified
208 * by using the setter functions, in which case \a pod will be modified
209 * underneath.
210 */
211 WpSpaPod *
212 wp_spa_pod_new_wrap (struct spa_pod *pod)
213 {
214 return wp_spa_pod_new (pod, WP_SPA_POD_REGULAR, FLAG_NO_OWNERSHIP);
215 }
216
217 /*!
218 * \brief Constructs a new immutable WpSpaPod that wraps the given `spa_pod`.
219 *
220 * \ingroup wpspapod
221 * \param pod a constant spa_pod
222 * \returns a new WpSpaPod that references the data in \a pod. \a pod is not
223 * copied, so it needs to stay alive. The returned WpSpaPod cannot be
224 * modified, unless it's copied first.
225 */
226 WpSpaPod *
227 wp_spa_pod_new_wrap_const (const struct spa_pod *pod)
228 {
229 return wp_spa_pod_new (pod, WP_SPA_POD_REGULAR,
230 FLAG_NO_OWNERSHIP | FLAG_CONSTANT);
231 }
232
233 static WpSpaPod *
234 wp_spa_pod_new_property_wrap (WpSpaIdTable table, guint32 key, guint32 flags,
235 struct spa_pod *pod)
236 {
237 WpSpaPod *self = wp_spa_pod_new (pod, WP_SPA_POD_PROPERTY, FLAG_NO_OWNERSHIP);
238 self->static_pod.data_property.table = table;
239 self->static_pod.data_property.key = key;
240 self->static_pod.data_property.flags = flags;
241 return self;
242 }
243
244 static WpSpaPod *
245 wp_spa_pod_new_control_wrap (guint32 offset, enum spa_control_type type,
246 struct spa_pod *pod)
247 {
248 WpSpaPod *self = wp_spa_pod_new (pod, WP_SPA_POD_CONTROL, FLAG_NO_OWNERSHIP);
249 self->static_pod.data_control.offset = offset;
250 self->static_pod.data_control.type = type;
251 return self;
252 }
253
254 #if 0
255 /* there is no use for these _const variants, but let's keep them just in case */
256
257 static WpSpaPod *
258 wp_spa_pod_new_property_wrap_const (WpSpaIdTable table, guint32 key,
259 guint32 flags, const struct spa_pod *pod)
260 {
261 WpSpaPod *self = wp_spa_pod_new (pod, WP_SPA_POD_PROPERTY,
262 FLAG_NO_OWNERSHIP | FLAG_CONSTANT);
263 self->static_pod.data_property.table = table;
264 self->static_pod.data_property.key = key;
265 self->static_pod.data_property.flags = flags;
266 return self;
267 }
268
269 static WpSpaPod *
270 wp_spa_pod_new_control_wrap_const (guint32 offset, enum spa_control_type type,
271 const struct spa_pod *pod)
272 {
273 WpSpaPod *self = wp_spa_pod_new (pod, WP_SPA_POD_CONTROL,
274 FLAG_NO_OWNERSHIP | FLAG_CONSTANT);
275 self->static_pod.data_control.offset = offset;
276 self->static_pod.data_control.type = type;
277 return self;
278 }
279 #endif
280
281 static WpSpaPod *
282 wp_spa_pod_new_wrap_copy (const struct spa_pod *pod)
283 {
284 return wp_spa_pod_new (pod, WP_SPA_POD_REGULAR, 0);
285 }
286
287 static WpSpaPod *
288 wp_spa_pod_new_property_wrap_copy (WpSpaIdTable table, guint32 key,
289 guint32 flags, const struct spa_pod *pod)
290 {
291 WpSpaPod *self = wp_spa_pod_new (pod, WP_SPA_POD_PROPERTY, 0);
292 self->static_pod.data_property.table = table;
293 self->static_pod.data_property.key = key;
294 self->static_pod.data_property.flags = flags;
295 return self;
296 }
297
298 static WpSpaPod *
299 wp_spa_pod_new_control_wrap_copy (guint32 offset, enum spa_control_type type,
300 const struct spa_pod *pod)
301 {
302 WpSpaPod *self = wp_spa_pod_new (pod, WP_SPA_POD_CONTROL, 0);
303 self->static_pod.data_control.offset = offset;
304 self->static_pod.data_control.type = type;
305 return self;
306 }
307
308 /*!
309 * \brief Converts a WpSpaPod pointer to a `struct spa_pod` one, for use with
310 * native pipewire & spa functions. The returned pointer is owned by WpSpaPod
311 * and may not be modified or freed.
312 *
313 * \ingroup wpspapod
314 * \param self a spa pod object
315 * \returns a const pointer to the underlying spa_pod structure
316 */
317 const struct spa_pod *
318 wp_spa_pod_get_spa_pod (const WpSpaPod *self)
319 {
320 return self->pod;
321 }
322
323 /*!
324 * \brief Gets the SPA type of the spa pod.
325 *
326 * If the pod is an object or pointer, this will return the derived
327 * object/pointer type directly.
328 * If the pod is an object property or a control, this will return the type
329 * of the contained value.
330 *
331 * \ingroup wpspapod
332 * \param self a spa pod
333 * \returns (transfer none): the type of the spa pod
334 */
335 WpSpaType
336 wp_spa_pod_get_spa_type (WpSpaPod *self)
337 {
338 g_return_val_if_fail (self != NULL, WP_SPA_TYPE_INVALID);
339
340 if (wp_spa_pod_is_object (self) || wp_spa_pod_is_pointer (self))
341 return SPA_POD_OBJECT_TYPE (self->pod);
342 else
343 return SPA_POD_TYPE (self->pod);
344 }
345
346 /*!
347 * \brief If the pod is a Choice, this gets the choice type
348 * (Range, Step, Enum, ...)
349 *
350 * \ingroup wpspapod
351 * \param self a choice pod
352 * \returns the choice type of the choice pod
353 */
354 WpSpaIdValue
355 wp_spa_pod_get_choice_type (WpSpaPod *self)
356 {
357 g_return_val_if_fail (wp_spa_pod_is_choice (self), NULL);
358 return wp_spa_id_value_from_number (
359 SPA_TYPE_INFO_Choice, SPA_POD_CHOICE_TYPE (self->pod));
360 }
361
362 /*!
363 * \brief Copies a spa pod object
364 *
365 * \ingroup wpspapod
366 * \param other a spa pod object
367 * \returns (transfer full): The newly copied spa pod
368 */
369 WpSpaPod *
370 wp_spa_pod_copy (WpSpaPod *other)
371 {
372 g_return_val_if_fail (other, NULL);
373 switch (other->type) {
374 case WP_SPA_POD_PROPERTY:
375 return wp_spa_pod_new_property_wrap_copy (
376 other->static_pod.data_property.table,
377 other->static_pod.data_property.key,
378 other->static_pod.data_property.flags, other->pod);
379 case WP_SPA_POD_CONTROL:
380 return wp_spa_pod_new_control_wrap_copy (
381 other->static_pod.data_control.offset,
382 other->static_pod.data_control.type, other->pod);
383 case WP_SPA_POD_REGULAR:
384 default:
385 break;
386 }
387 return wp_spa_pod_new_wrap_copy (other->pod);
388 }
389
390 /*!
391 * \brief Checks if the pod is the unique owner of its data or not
392 *
393 * \ingroup wpspapod
394 * \param self a spa pod object
395 * \returns TRUE if the pod owns the data, FALSE otherwise.
396 */
397 gboolean
398 wp_spa_pod_is_unique_owner (WpSpaPod *self)
399 {
400 return g_ref_count_compare (&self->ref, 1) &&
401 !(self->flags & FLAG_NO_OWNERSHIP);
402 }
403
404 /*!
405 * \brief If \a self is not uniquely owned already, then it is unrefed and a
406 * copy of it is returned instead. You should always consider \a self as unsafe
407 * to use after this call and you should use the returned object instead.
408 *
409 * \ingroup wpspapod
410 * \param self (transfer full): a spa pod object
411 * \returns (transfer full): the uniquely owned spa pod object which may or may
412 * not be the same as \a self.
413 */
414 WpSpaPod *
415 wp_spa_pod_ensure_unique_owner (WpSpaPod *self)
416 {
417 WpSpaPod *copy = NULL;
418
419 if (wp_spa_pod_is_unique_owner (self))
420 return self;
421
422 copy = wp_spa_pod_copy (self);
423 wp_spa_pod_unref (self);
424 return copy;
425 }
426
427 /*!
428 * \brief Creates a spa pod of type None
429 * \ingroup wpspapod
430 * \returns (transfer full): The new spa pod
431 */
432 WpSpaPod *
433 wp_spa_pod_new_none (void)
434 {
435 WpSpaPod *self = g_slice_new0 (WpSpaPod);
436 g_ref_count_init (&self->ref);
437 self->type = WP_SPA_POD_REGULAR;
438 self->static_pod.pod_none = SPA_POD_INIT_None();
439 self->pod = &self->static_pod.pod_none;
440 return self;
441 }
442
443 /*!
444 * \brief Creates a spa pod of type boolean
445 *
446 * \ingroup wpspapod
447 * \param value the boolean value
448 * \returns (transfer full): The new spa pod
449 */
450 WpSpaPod *
451 wp_spa_pod_new_boolean (gboolean value)
452 {
453 WpSpaPod *self = g_slice_new0 (WpSpaPod);
454 g_ref_count_init (&self->ref);
455 self->type = WP_SPA_POD_REGULAR;
456 self->static_pod.pod_bool = SPA_POD_INIT_Bool (value ? true : false);
457 self->pod = &self->static_pod.pod_bool.pod;
458 return self;
459 }
460
461 /*!
462 * \brief Creates a spa pod of type Id
463 *
464 * \ingroup wpspapod
465 * \param value the Id value
466 * \returns (transfer full): The new spa pod
467 */
468 WpSpaPod *
469 wp_spa_pod_new_id (guint32 value)
470 {
471 WpSpaPod *self = g_slice_new0 (WpSpaPod);
472 g_ref_count_init (&self->ref);
473 self->type = WP_SPA_POD_REGULAR;
474 self->static_pod.pod_id = SPA_POD_INIT_Id (value);
475 self->pod = &self->static_pod.pod_id.pod;
476 return self;
477 }
478
479 /*!
480 * \brief Creates a spa pod of type int
481 *
482 * \ingroup wpspapod
483 * \param value the int value
484 * \returns (transfer full): The new spa pod
485 */
486 WpSpaPod *
487 wp_spa_pod_new_int (gint32 value)
488 {
489 WpSpaPod *self = g_slice_new0 (WpSpaPod);
490 g_ref_count_init (&self->ref);
491 self->type = WP_SPA_POD_REGULAR;
492 self->static_pod.pod_int = SPA_POD_INIT_Int (value);
493 self->pod = &self->static_pod.pod_int.pod;
494 return self;
495 }
496
497 /*!
498 * \brief Creates a spa pod of type long
499 *
500 * \ingroup wpspapod
501 * \param value the long value
502 * \returns (transfer full): The new spa pod
503 */
504 WpSpaPod *
505 wp_spa_pod_new_long (gint64 value)
506 {
507 WpSpaPod *self = g_slice_new0 (WpSpaPod);
508 g_ref_count_init (&self->ref);
509 self->type = WP_SPA_POD_REGULAR;
510 self->static_pod.pod_long = SPA_POD_INIT_Long (value);
511 self->pod = &self->static_pod.pod_long.pod;
512 return self;
513 }
514
515 /*!
516 * \brief Creates a spa pod of type float
517 *
518 * \ingroup wpspapod
519 * \param value the float value
520 * \returns (transfer full): The new spa pod
521 */
522 WpSpaPod *
523 wp_spa_pod_new_float (float value)
524 {
525 WpSpaPod *self = g_slice_new0 (WpSpaPod);
526 g_ref_count_init (&self->ref);
527 self->type = WP_SPA_POD_REGULAR;
528 self->static_pod.pod_float = SPA_POD_INIT_Float (value);
529 self->pod = &self->static_pod.pod_float.pod;
530 return self;
531 }
532
533 /*!
534 * \brief Creates a spa pod of type double
535 *
536 * \ingroup wpspapod
537 * \param value the double value
538 * \returns (transfer full): The new spa pod
539 */
540 WpSpaPod *
541 wp_spa_pod_new_double (double value)
542 {
543 WpSpaPod *self = g_slice_new0 (WpSpaPod);
544 g_ref_count_init (&self->ref);
545 self->type = WP_SPA_POD_REGULAR;
546 self->static_pod.pod_double = SPA_POD_INIT_Double (value);
547 self->pod = &self->static_pod.pod_double.pod;
548 return self;
549 }
550
551 /*!
552 * \brief Creates a spa pod of type string
553 *
554 * \ingroup wpspapod
555 * \param value the string value
556 * \returns (transfer full): The new spa pod
557 */
558 WpSpaPod *
559 wp_spa_pod_new_string (const char *value)
560 {
561 const uint32_t len = value ? strlen (value) : 0;
562 const char *str = value ? value : "";
563 WpSpaPod *self = g_slice_new0 (WpSpaPod);
564 g_ref_count_init (&self->ref);
565 self->type = WP_SPA_POD_REGULAR;
566
567 struct spa_pod_string p = SPA_POD_INIT_String (len + 1);
568 self->builder = wp_spa_pod_builder_new (
569 SPA_ROUND_UP_N (sizeof (p) + len + 1, 8), SPA_TYPE_String);
570
571 self->pod = self->builder->builder.data;
572
573 spa_pod_builder_raw (&self->builder->builder, &p, sizeof(p));
574 spa_pod_builder_write_string (&self->builder->builder, str, len);
575 return self;
576 }
577
578 /*!
579 * \brief Creates a spa pod of type bytes
580 *
581 * \ingroup wpspapod
582 * \param value the bytes value
583 * \param len the length of the bytes value
584 * \returns (transfer full): The new spa pod
585 */
586 WpSpaPod *
587 wp_spa_pod_new_bytes (gconstpointer value, guint32 len)
588 {
589 WpSpaPod *self = g_slice_new0 (WpSpaPod);
590 g_ref_count_init (&self->ref);
591 self->type = WP_SPA_POD_REGULAR;
592 const struct spa_pod_bytes p = SPA_POD_INIT_Bytes (len);
593 self->builder = wp_spa_pod_builder_new (
594 SPA_ROUND_UP_N (sizeof (p) + p.pod.size, 8),
595 SPA_TYPE_Bytes);
596
597 self->pod = self->builder->builder.data;
598
599 spa_pod_builder_raw (&self->builder->builder, &p, sizeof(p));
600 spa_pod_builder_raw_padded (&self->builder->builder, value, len);
601 return self;
602 }
603
604 /*!
605 * \brief Creates a spa pod of type pointer
606 *
607 * \ingroup wpspapod
608 * \param type_name the name of the type of the pointer
609 * \param value the pointer value
610 * \returns (transfer full): The new spa pod
611 */
612 WpSpaPod *
613 wp_spa_pod_new_pointer (const char *type_name, gconstpointer value)
614 {
615 WpSpaType type = wp_spa_type_from_name (type_name);
616 g_return_val_if_fail (type != WP_SPA_TYPE_INVALID, NULL);
617
618 WpSpaPod *self = g_slice_new0 (WpSpaPod);
619 g_ref_count_init (&self->ref);
620 self->type = WP_SPA_POD_REGULAR;
621 self->static_pod.pod_pointer = SPA_POD_INIT_Pointer (type, value);
622 self->pod = &self->static_pod.pod_pointer.pod;
623 return self;
624 }
625
626 /*!
627 * \brief Creates a spa pod of type Fd
628 *
629 * \ingroup wpspapod
630 * \param value the Fd value
631 * \returns (transfer full): The new spa pod
632 */
633 WpSpaPod *
634 wp_spa_pod_new_fd (gint64 value)
635 {
636 WpSpaPod *self = g_slice_new0 (WpSpaPod);
637 g_ref_count_init (&self->ref);
638 self->type = WP_SPA_POD_REGULAR;
639 self->static_pod.pod_fd = SPA_POD_INIT_Fd (value);
640 self->pod = &self->static_pod.pod_fd.pod;
641 return self;
642 }
643
644 /*!
645 * \brief Creates a spa pod of type rectangle
646 *
647 * \ingroup wpspapod
648 * \param width the width value of the rectangle
649 * \param height the height value of the rectangle
650 * \returns (transfer full): The new spa pod
651 */
652 WpSpaPod *
653 wp_spa_pod_new_rectangle (guint32 width, guint32 height)
654 {
655 WpSpaPod *self = g_slice_new0 (WpSpaPod);
656 g_ref_count_init (&self->ref);
657 self->type = WP_SPA_POD_REGULAR;
658 self->static_pod.pod_rectangle =
659 SPA_POD_INIT_Rectangle (SPA_RECTANGLE (width, height));
660 self->pod = &self->static_pod.pod_rectangle.pod;
661 return self;
662 }
663
664 /*!
665 * \brief Creates a spa pod of type fraction
666 *
667 * \ingroup wpspapod
668 * \param num the numerator value of the fraction
669 * \param denom the denominator value of the fraction
670 * \returns (transfer full): The new spa pod
671 */
672 WpSpaPod *
673 wp_spa_pod_new_fraction (guint32 num, guint32 denom)
674 {
675 WpSpaPod *self = g_slice_new0 (WpSpaPod);
676 g_ref_count_init (&self->ref);
677 self->type = WP_SPA_POD_REGULAR;
678 self->static_pod.pod_fraction =
679 SPA_POD_INIT_Fraction (SPA_FRACTION (num, denom));
680 self->pod = &self->static_pod.pod_fraction.pod;
681 return self;
682 }
683
684 /*!
685 * \brief Creates a spa pod of type choice
686 *
687 * \ingroup wpspapod
688 * \param choice_type the name of the choice type ("Range", "Step", ...),
689 * \param ... a list of choice values, followed by NULL
690 * \returns (transfer full): The new spa pod
691 */
692 WpSpaPod *
693 wp_spa_pod_new_choice (const char *choice_type, ...)
694 {
695 WpSpaPod *self;
696 va_list args;
697
698 va_start (args, choice_type);
699 self = wp_spa_pod_new_choice_valist (choice_type, args);
700 va_end (args);
701
702 return self;
703 }
704
705 /*!
706 * \brief This is the `va_list` version of wp_spa_pod_new_choice()
707 *
708 * \ingroup wpspapod
709 * \param choice_type the name of the choice type ("Range", "Step", ...)
710 * \param args the variable arguments passed to wp_spa_pod_new_choice()
711 * \returns (transfer full): The new spa pod
712 */
713 WpSpaPod *
714 wp_spa_pod_new_choice_valist (const char *choice_type, va_list args)
715 {
716 g_autoptr (WpSpaPodBuilder) b = wp_spa_pod_builder_new_choice (choice_type);
717 wp_spa_pod_builder_add_valist (b, args);
718 return wp_spa_pod_builder_end (b);
719 }
720
721 /*!
722 * \brief Creates a spa pod of type object
723 *
724 * \ingroup wpspapod
725 * \param type_name the type name of the object type
726 * \param id_name the id name of the object,
727 * \param ... a list of object properties with their values, followed by NULL
728 * \returns (transfer full): The new spa pod
729 */
730 WpSpaPod *
731 wp_spa_pod_new_object (const char *type_name, const char *id_name, ...)
732 {
733 WpSpaPod *self;
734 va_list args;
735
736 va_start (args, id_name);
737 self = wp_spa_pod_new_object_valist (type_name, id_name, args);
738 va_end (args);
739
740 return self;
741 }
742
743 /*!
744 * \brief This is the `va_list` version of wp_spa_pod_new_object()
745 *
746 * \ingroup wpspapod
747 * \param type_name the type name of the object type
748 * \param id_name the id name of the object
749 * \param args the variable arguments passed to wp_spa_pod_new_object()
750 * \returns (transfer full): The new spa pod
751 */
752 WpSpaPod *
753 wp_spa_pod_new_object_valist (const char *type_name, const char *id_name,
754 va_list args)
755 {
756 g_autoptr (WpSpaPodBuilder) b = wp_spa_pod_builder_new_object (type_name,
757 id_name);
758 wp_spa_pod_builder_add_valist (b, args);
759 return wp_spa_pod_builder_end (b);
760 }
761
762 /*!
763 * \brief Creates a spa pod of type sequence
764 *
765 * \ingroup wpspapod
766 * \param unit the unit of the sequence
767 * \param ... a list of sequence controls with their values, followed by NULL
768 * \returns (transfer full): The new spa pod
769 */
770 WpSpaPod *
771 wp_spa_pod_new_sequence (guint unit, ...)
772 {
773 WpSpaPod *self;
774 va_list args;
775
776 va_start(args, unit);
777 self = wp_spa_pod_new_sequence_valist (unit, args);
778 va_end(args);
779
780 return self;
781 }
782
783 /*!
784 * \brief This is the `va_list` version of wp_spa_pod_new_sequence()
785 *
786 * \ingroup wpspapod
787 * \param unit the unit of the sequence
788 * \param args the variable arguments passed to wp_spa_pod_new_sequence()
789 * \returns (transfer full): The new spa pod
790 */
791 WpSpaPod *
792 wp_spa_pod_new_sequence_valist (guint unit, va_list args)
793 {
794 g_autoptr (WpSpaPodBuilder) b = wp_spa_pod_builder_new_sequence (unit);
795 wp_spa_pod_builder_add_valist (b, args);
796 return wp_spa_pod_builder_end (b);
797 }
798
799 /*!
800 * \brief Checks whether the spa pod is of type none or not
801 *
802 * \ingroup wpspapod
803 * \param self the spa pod object
804 * \returns TRUE if it is of type none, FALSE otherwise
805 */
806 gboolean
807 wp_spa_pod_is_none (WpSpaPod *self)
808 {
809 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_none (self->pod);
810 }
811
812 /*!
813 * \brief Checks whether the spa pod is of type boolean or not
814 *
815 * \ingroup wpspapod
816 * \param self the spa pod object
817 * \returns TRUE if it is of type boolean, FALSE otherwise
818 */
819 gboolean
820 wp_spa_pod_is_boolean (WpSpaPod *self)
821 {
822 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_bool (self->pod);
823 }
824
825 /*!
826 * \brief Checks whether the spa pod is of type Id or not
827 *
828 * \ingroup wpspapod
829 * \param self the spa pod object
830 * \returns TRUE if it is of type Id, FALSE otherwise
831 */
832 gboolean
833 wp_spa_pod_is_id (WpSpaPod *self)
834 {
835 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_id (self->pod);
836 }
837
838 /*!
839 * \brief Checks whether the spa pod is of type int or not
840 *
841 * \ingroup wpspapod
842 * \param self the spa pod object
843 * \returns TRUE if it is of type int, FALSE otherwise
844 */
845 gboolean
846 wp_spa_pod_is_int (WpSpaPod *self)
847 {
848 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_int (self->pod);
849 }
850
851 /*!
852 * \brief Checks whether the spa pod is of type long or not
853 *
854 * \ingroup wpspapod
855 * \param self the spa pod object
856 * \returns TRUE if it is of type long, FALSE otherwise
857 */
858 gboolean
859 wp_spa_pod_is_long (WpSpaPod *self)
860 {
861 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_long (self->pod);
862 }
863
864 /*!
865 * \brief Checks whether the spa pod is of type float or not
866 *
867 * \ingroup wpspapod
868 * \param self the spa pod object
869 * \returns TRUE if it is of type float, FALSE otherwise
870 */
871 gboolean
872 wp_spa_pod_is_float (WpSpaPod *self)
873 {
874 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_float (self->pod);
875 }
876
877 /*!
878 * \brief Checks whether the spa pod is of type double or not
879 *
880 * \ingroup wpspapod
881 * \param self the spa pod object
882 * \returns TRUE if it is of type double, FALSE otherwise
883 */
884 gboolean
885 wp_spa_pod_is_double (WpSpaPod *self)
886 {
887 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_double (self->pod);
888 }
889
890 /*!
891 * \brief Checks whether the spa pod is of type string or not
892 *
893 * \ingroup wpspapod
894 * \param self the spa pod object
895 * \returns TRUE if it is of type string, FALSE otherwise
896 */
897 gboolean
898 wp_spa_pod_is_string (WpSpaPod *self)
899 {
900 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_string (self->pod);
901 }
902
903 /*!
904 * \brief Checks whether the spa pod is of type bytes or not
905 *
906 * \ingroup wpspapod
907 * \param self the spa pod object
908 * \returns TRUE if it is of type bytes, FALSE otherwise
909 */
910 gboolean
911 wp_spa_pod_is_bytes (WpSpaPod *self)
912 {
913 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_bytes (self->pod);
914 }
915
916 /*!
917 * \brief Checks whether the spa pod is of type pointer or not
918 *
919 * \ingroup wpspapod
920 * \param self the spa pod object
921 * \returns TRUE if it is of type pointer, FALSE otherwise
922 */
923 gboolean
924 wp_spa_pod_is_pointer (WpSpaPod *self)
925 {
926 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_pointer (self->pod);
927 }
928
929 /*!
930 * \brief Checks whether the spa pod is of type Fd or not
931 *
932 * \ingroup wpspapod
933 * \param self the spa pod object
934 * \returns TRUE if it is of type Fd, FALSE otherwise
935 */
936 gboolean
937 wp_spa_pod_is_fd (WpSpaPod *self)
938 {
939 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_fd (self->pod);
940 }
941
942 /*!
943 * \brief Checks whether the spa pod is of type rectangle or not
944 *
945 * \ingroup wpspapod
946 * \param self the spa pod object
947 * \returns TRUE if it is of type rectangle, FALSE otherwise
948 */
949 gboolean
950 wp_spa_pod_is_rectangle (WpSpaPod *self)
951 {
952 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_rectangle (self->pod);
953 }
954
955 /*!
956 * \brief Checks whether the spa pod is of type fraction or not
957 *
958 * \ingroup wpspapod
959 * \param self the spa pod object
960 * \returns TRUE if it is of type fraction, FALSE otherwise
961 */
962 gboolean
963 wp_spa_pod_is_fraction (WpSpaPod *self)
964 {
965 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_fraction (self->pod);
966 }
967
968 /*!
969 * \brief Checks whether the spa pod is of type array or not
970 *
971 * \ingroup wpspapod
972 * \param self the spa pod object
973 * \returns TRUE if it is of type array, FALSE otherwise
974 */
975 gboolean
976 wp_spa_pod_is_array (WpSpaPod *self)
977 {
978 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_array (self->pod);
979 }
980
981 /*!
982 * \brief Checks whether the spa pod is of type choice or not
983 *
984 * \ingroup wpspapod
985 * \param self the spa pod object
986 * \returns TRUE if it is of type choice, FALSE otherwise
987 */
988 gboolean
989 wp_spa_pod_is_choice (WpSpaPod *self)
990 {
991 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_choice (self->pod);
992 }
993
994 /*!
995 * \brief Checks whether the spa pod is of type object or not
996 *
997 * \ingroup wpspapod
998 * \param self the spa pod object
999 * \returns TRUE if it is of type object, FALSE otherwise
1000 */
1001 gboolean
1002 wp_spa_pod_is_object (WpSpaPod *self)
1003 {
1004 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_object (self->pod);
1005 }
1006
1007 /*!
1008 * \brief Checks whether the spa pod is of type struct or not
1009 *
1010 * \ingroup wpspapod
1011 * \param self the spa pod object
1012 * \returns TRUE if it is of type struct, FALSE otherwise
1013 */
1014 gboolean
1015 wp_spa_pod_is_struct (WpSpaPod *self)
1016 {
1017 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_struct (self->pod);
1018 }
1019
1020 /*!
1021 * \brief Checks whether the spa pod is of type sequence or not
1022 *
1023 * \ingroup wpspapod
1024 * \param self the spa pod object
1025 * \returns TRUE if it is of type sequence, FALSE otherwise
1026 */
1027 gboolean
1028 wp_spa_pod_is_sequence (WpSpaPod *self)
1029 {
1030 return self->type == WP_SPA_POD_REGULAR && spa_pod_is_sequence (self->pod);
1031 }
1032
1033 /*!
1034 * \brief Checks whether the spa pod is of type property or not
1035 *
1036 * \ingroup wpspapod
1037 * \param self the spa pod object
1038 * \returns TRUE if it is of type property, FALSE otherwise
1039 */
1040 gboolean
1041 wp_spa_pod_is_property (WpSpaPod *self)
1042 {
1043 return self->type == WP_SPA_POD_PROPERTY;
1044 }
1045
1046 /*!
1047 * \brief Checks whether the spa pod is of type control or not
1048 *
1049 * \ingroup wpspapod
1050 * \param self the spa pod object
1051 * \returns TRUE if it is of type control, FALSE otherwise
1052 */
1053 gboolean
1054 wp_spa_pod_is_control (WpSpaPod *self)
1055 {
1056 return self->type == WP_SPA_POD_CONTROL;
1057 }
1058
1059 /*!
1060 * \brief Gets the boolean value of a spa pod object
1061 *
1062 * \ingroup wpspapod
1063 * \param self the spa pod object
1064 * \param value (out): the boolean value
1065 * \returns TRUE if the value was obtained, FALSE otherwise
1066 */
1067 gboolean
1068 wp_spa_pod_get_boolean (WpSpaPod *self, gboolean *value)
1069 {
1070 g_return_val_if_fail (self, FALSE);
1071 g_return_val_if_fail (value, FALSE);
1072 bool v = FALSE;
1073 const int res = spa_pod_get_bool (self->pod, &v);
1074 *value = v ? TRUE : FALSE;
1075 return res >= 0;
1076 }
1077
1078 /*!
1079 * \brief Gets the Id value of a spa pod object
1080 *
1081 * \ingroup wpspapod
1082 * \param self the spa pod object
1083 * \param value (out): the Id value
1084 * \returns TRUE if the value was obtained, FALSE otherwise
1085 */
1086 gboolean
1087 wp_spa_pod_get_id (WpSpaPod *self, guint32 *value)
1088 {
1089 g_return_val_if_fail (self, FALSE);
1090 g_return_val_if_fail (value, FALSE);
1091 uint32_t v = 0;
1092 const int res = spa_pod_get_id (self->pod, &v);
1093 *value = v;
1094 return res >= 0;
1095 }
1096
1097 /*!
1098 * \brief Gets the int value of a spa pod object
1099 *
1100 * \ingroup wpspapod
1101 * \param self the spa pod object
1102 * \param value (out): the int value
1103 * \returns TRUE if the value was obtained, FALSE otherwise
1104 */
1105 gboolean
1106 wp_spa_pod_get_int (WpSpaPod *self, gint32 *value)
1107 {
1108 g_return_val_if_fail (self, FALSE);
1109 g_return_val_if_fail (value, FALSE);
1110 return spa_pod_get_int (self->pod, value) >= 0;
1111 }
1112
1113 /*!
1114 * \brief Gets the long value of a spa pod object
1115 *
1116 * \ingroup wpspapod
1117 * \param self the spa pod object
1118 * \param value (out): the long value
1119 * \returns TRUE if the value was obtained, FALSE otherwise
1120 */
1121 gboolean
1122 wp_spa_pod_get_long (WpSpaPod *self, gint64 *value)
1123 {
1124 g_return_val_if_fail (self, FALSE);
1125 g_return_val_if_fail (value, FALSE);
1126 return spa_pod_get_long (self->pod, value) >= 0;
1127 }
1128
1129 /*!
1130 * \brief Gets the float value of a spa pod object
1131 *
1132 * \ingroup wpspapod
1133 * \param self the spa pod object
1134 * \param value (out): the float value
1135 * \returns TRUE if the value was obtained, FALSE otherwise
1136 */
1137 gboolean
1138 wp_spa_pod_get_float (WpSpaPod *self, float *value)
1139 {
1140 g_return_val_if_fail (self, FALSE);
1141 g_return_val_if_fail (value, FALSE);
1142 return spa_pod_get_float (self->pod, value) >= 0;
1143 }
1144
1145 /*!
1146 * \brief Gets the double value of a spa pod object
1147 *
1148 * \ingroup wpspapod
1149 * \param self the spa pod object
1150 * \param value (out): the double value
1151 * \returns TRUE if the value was obtained, FALSE otherwise
1152 */
1153 gboolean
1154 wp_spa_pod_get_double (WpSpaPod *self, double *value)
1155 {
1156 g_return_val_if_fail (self, FALSE);
1157 g_return_val_if_fail (value, FALSE);
1158 return spa_pod_get_double (self->pod, value) >= 0;
1159 }
1160
1161 /*!
1162 * \brief Gets the string value of a spa pod object
1163 *
1164 * \ingroup wpspapod
1165 * \param self the spa pod object
1166 * \param value (out): the string value
1167 * \returns TRUE if the value was obtained, FALSE otherwise
1168 */
1169 gboolean
1170 wp_spa_pod_get_string (WpSpaPod *self, const char **value)
1171 {
1172 g_return_val_if_fail (self, FALSE);
1173 g_return_val_if_fail (value, FALSE);
1174 return spa_pod_get_string (self->pod, value) >= 0;
1175 }
1176
1177 /*!
1178 * \brief Gets the bytes value and its len of a spa pod object
1179 *
1180 * \ingroup wpspapod
1181 * \param self the spa pod object
1182 * \param value (out): the bytes value
1183 * \param len (out): the length of the bytes value
1184 * \returns TRUE if the value was obtained, FALSE otherwise
1185 */
1186 gboolean
1187 wp_spa_pod_get_bytes (WpSpaPod *self, gconstpointer *value, guint32 *len)
1188 {
1189 g_return_val_if_fail (self, FALSE);
1190 g_return_val_if_fail (value, FALSE);
1191 g_return_val_if_fail (len, FALSE);
1192 return spa_pod_get_bytes (self->pod, value, len) >= 0;
1193 }
1194
1195 /*!
1196 * \brief Gets the pointer value and its type name of a spa pod object
1197 *
1198 * \ingroup wpspapod
1199 * \param self the spa pod object
1200 * \param value (out): the pointer value
1201 * \returns TRUE if the value was obtained, FALSE otherwise
1202 */
1203 gboolean
1204 wp_spa_pod_get_pointer (WpSpaPod *self, gconstpointer *value)
1205 {
1206 g_return_val_if_fail (self, FALSE);
1207 g_return_val_if_fail (value, FALSE);
1208
1209 guint32 type = 0;
1210 return spa_pod_get_pointer (self->pod, &type, value) >= 0;
1211 }
1212
1213 /*!
1214 * \brief Gets the Fd value of a spa pod object
1215 *
1216 * \ingroup wpspapod
1217 * \param self the spa pod object
1218 * \param value (out): the Fd value
1219 * \returns TRUE if the value was obtained, FALSE otherwise
1220 */
1221 gboolean
1222 wp_spa_pod_get_fd (WpSpaPod *self, gint64 *value)
1223 {
1224 g_return_val_if_fail (self, FALSE);
1225 g_return_val_if_fail (value, FALSE);
1226 return spa_pod_get_fd (self->pod, value) >= 0;
1227 }
1228
1229 /*!
1230 * \brief Gets the rectangle's width and height value of a spa pod object
1231 *
1232 * \ingroup wpspapod
1233 * \param self the spa pod object
1234 * \param width (out): the rectangle's width value
1235 * \param height (out): the rectangle's height value
1236 * \returns TRUE if the value was obtained, FALSE otherwise
1237 */
1238 gboolean
1239 wp_spa_pod_get_rectangle (WpSpaPod *self, guint32 *width, guint32 *height)
1240 {
1241 g_return_val_if_fail (self, FALSE);
1242 struct spa_rectangle rectangle = { 0, };
1243 const gboolean res = spa_pod_get_rectangle (self->pod, &rectangle) >= 0;
1244 if (width)
1245 *width = rectangle.width;
1246 if (height)
1247 *height = rectangle.height;
1248 return res;
1249 }
1250
1251 /*!
1252 * \brief Gets the fractions's numerator and denominator value of a spa pod object
1253 *
1254 * \ingroup wpspapod
1255 * \param self the spa pod object
1256 * \param num (out): the fractions's numerator value
1257 * \param denom (out): the fractions's denominator value
1258 * \returns TRUE if the value was obtained, FALSE otherwise
1259 */
1260 gboolean
1261 wp_spa_pod_get_fraction (WpSpaPod *self, guint32 *num, guint32 *denom)
1262 {
1263 g_return_val_if_fail (self, FALSE);
1264 struct spa_fraction fraction = { 0, };
1265 const gboolean res = spa_pod_get_fraction (self->pod, &fraction) >= 0;
1266 if (num)
1267 *num = fraction.num;
1268 if (denom)
1269 *denom = fraction.denom;
1270 return res;
1271 }
1272
1273 /*!
1274 * \brief Sets a boolean value in the spa pod object.
1275 *
1276 * \ingroup wpspapod
1277 * \param self the spa pod object
1278 * \param value the boolean value
1279 * \returns TRUE if the value could be set, FALSE othewrise.
1280 */
1281 gboolean
1282 wp_spa_pod_set_boolean (WpSpaPod *self, gboolean value)
1283 {
1284 g_return_val_if_fail (wp_spa_pod_is_boolean (self), FALSE);
1285 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1286 ((struct spa_pod_bool *)self->pod)->value = value ? true : false;
1287 return TRUE;
1288 }
1289
1290 /*!
1291 * \brief Sets an Id value in the spa pod object.
1292 *
1293 * \ingroup wpspapod
1294 * \param self the spa pod object
1295 * \param value the Id value
1296 * \returns TRUE if the value could be set, FALSE othewrise.
1297 */
1298 gboolean
1299 wp_spa_pod_set_id (WpSpaPod *self, guint32 value)
1300 {
1301 g_return_val_if_fail (wp_spa_pod_is_id (self), FALSE);
1302 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1303 ((struct spa_pod_id *)self->pod)->value = value;
1304 return TRUE;
1305 }
1306
1307 /*!
1308 * \brief Sets an int value in the spa pod object.
1309 *
1310 * \ingroup wpspapod
1311 * \param self the spa pod object
1312 * \param value the int value
1313 * \returns TRUE if the value could be set, FALSE othewrise.
1314 */
1315 gboolean
1316 wp_spa_pod_set_int (WpSpaPod *self, gint32 value)
1317 {
1318 g_return_val_if_fail (wp_spa_pod_is_int (self), FALSE);
1319 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1320 ((struct spa_pod_int *)self->pod)->value = value;
1321 return TRUE;
1322 }
1323
1324 /*!
1325 * \brief Sets a long value in the spa pod object.
1326 *
1327 * \ingroup wpspapod
1328 * \param self the spa pod object
1329 * \param value the long value
1330 * \returns TRUE if the value could be set, FALSE othewrise.
1331 */
1332 gboolean
1333 wp_spa_pod_set_long (WpSpaPod *self, gint64 value)
1334 {
1335 g_return_val_if_fail (wp_spa_pod_is_long (self), FALSE);
1336 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1337 ((struct spa_pod_long *)self->pod)->value = value;
1338 return TRUE;
1339 }
1340
1341 /*!
1342 * \brief Sets a float value in the spa pod object.
1343 *
1344 * \ingroup wpspapod
1345 * \param self the spa pod object
1346 * \param value the float value
1347 * \returns TRUE if the value could be set, FALSE othewrise.
1348 */
1349 gboolean
1350 wp_spa_pod_set_float (WpSpaPod *self, float value)
1351 {
1352 g_return_val_if_fail (wp_spa_pod_is_float (self), FALSE);
1353 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1354 ((struct spa_pod_float *)self->pod)->value = value;
1355 return TRUE;
1356 }
1357
1358 /*!
1359 * \brief Sets a double value in the spa pod object.
1360 *
1361 * \ingroup wpspapod
1362 * \param self the spa pod object
1363 * \param value the double value
1364 * \returns TRUE if the value could be set, FALSE othewrise.
1365 */
1366 gboolean
1367 wp_spa_pod_set_double (WpSpaPod *self, double value)
1368 {
1369 g_return_val_if_fail (wp_spa_pod_is_double (self), FALSE);
1370 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1371 ((struct spa_pod_double *)self->pod)->value = value;
1372 return TRUE;
1373 }
1374
1375 /*!
1376 * \brief Sets a pointer value with its type name in the spa pod object.
1377 *
1378 * \ingroup wpspapod
1379 * \param self the spa pod object
1380 * \param type_name the name of the type of the pointer
1381 * \param value the pointer value
1382 * \returns TRUE if the value could be set, FALSE othewrise.
1383 */
1384 gboolean
1385 wp_spa_pod_set_pointer (WpSpaPod *self, const char *type_name,
1386 gconstpointer value)
1387 {
1388 WpSpaType type = wp_spa_type_from_name (type_name);
1389
1390 g_return_val_if_fail (wp_spa_pod_is_pointer (self), FALSE);
1391 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1392 g_return_val_if_fail (type != WP_SPA_TYPE_INVALID, FALSE);
1393
1394 ((struct spa_pod_pointer *)self->pod)->body.type = type;
1395 ((struct spa_pod_pointer *)self->pod)->body.value = value;
1396 return TRUE;
1397 }
1398
1399 /*!
1400 * \brief Sets a Fd value in the spa pod object.
1401 *
1402 * \ingroup wpspapod
1403 * \param self the spa pod object
1404 * \param value the Fd value
1405 * \returns TRUE if the value could be set, FALSE othewrise.
1406 */
1407 gboolean
1408 wp_spa_pod_set_fd (WpSpaPod *self, gint64 value)
1409 {
1410 g_return_val_if_fail (wp_spa_pod_is_fd (self), FALSE);
1411 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1412 ((struct spa_pod_fd *)self->pod)->value = value;
1413 return TRUE;
1414 }
1415
1416 /*!
1417 * \brief Sets the width and height values of a rectangle in the spa pod object.
1418 *
1419 * \ingroup wpspapod
1420 * \param self the spa pod object
1421 * \param width the width value of the rectangle
1422 * \param height the height value of the rectangle
1423 * \returns TRUE if the value could be set, FALSE othewrise.
1424 */
1425 gboolean
1426 wp_spa_pod_set_rectangle (WpSpaPod *self, guint32 width, guint32 height)
1427 {
1428 g_return_val_if_fail (wp_spa_pod_is_rectangle (self), FALSE);
1429 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1430 ((struct spa_pod_rectangle *)self->pod)->value.width = width;
1431 ((struct spa_pod_rectangle *)self->pod)->value.height = height;
1432 return TRUE;
1433 }
1434
1435 /*!
1436 * \brief Sets the numerator and denominator values of a fraction in the
1437 * spa pod object.
1438 *
1439 * \ingroup wpspapod
1440 * \param self the spa pod object
1441 * \param num the numerator value of the fraction
1442 * \param denom the denominator value of the fraction
1443 * \returns TRUE if the value could be set, FALSE othewrise.
1444 */
1445 gboolean
1446 wp_spa_pod_set_fraction (WpSpaPod *self, guint32 num, guint32 denom)
1447 {
1448 g_return_val_if_fail (wp_spa_pod_is_fraction (self), FALSE);
1449 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1450 ((struct spa_pod_fraction *)self->pod)->value.num = num;
1451 ((struct spa_pod_fraction *)self->pod)->value.denom = denom;
1452 return TRUE;
1453 }
1454
1455 /*!
1456 * \brief Sets the value of a spa pod object in the current spa pod object.
1457 * The spa pod objects must be of the same value.
1458 *
1459 * \ingroup wpspapod
1460 * \param self the spa pod object
1461 * \param pod the pod with the value to be set
1462 * \returns TRUE if the value could be set, FALSE othewrise.
1463 */
1464 gboolean
1465 wp_spa_pod_set_pod (WpSpaPod *self, WpSpaPod *pod)
1466 {
1467 g_return_val_if_fail (self->type == pod->type, FALSE);
1468 g_return_val_if_fail (SPA_POD_TYPE (self->pod) == SPA_POD_TYPE (pod->pod), FALSE);
1469 g_return_val_if_fail (!(self->flags & FLAG_CONSTANT), FALSE);
1470
1471 switch (SPA_POD_TYPE (self->pod)) {
1472 case SPA_TYPE_None:
1473 break;
1474 case SPA_TYPE_Bool:
1475 ((struct spa_pod_bool *)self->pod)->value = ((struct spa_pod_bool *)pod->pod)->value;
1476 break;
1477 case SPA_TYPE_Id:
1478 ((struct spa_pod_id *)self->pod)->value = ((struct spa_pod_id *)pod->pod)->value;
1479 break;
1480 case SPA_TYPE_Int:
1481 ((struct spa_pod_int *)self->pod)->value = ((struct spa_pod_int *)pod->pod)->value;
1482 break;
1483 case SPA_TYPE_Long:
1484 ((struct spa_pod_long *)self->pod)->value = ((struct spa_pod_long *)pod->pod)->value;
1485 break;
1486 case SPA_TYPE_Float:
1487 ((struct spa_pod_float *)self->pod)->value = ((struct spa_pod_float *)pod->pod)->value;
1488 break;
1489 case SPA_TYPE_Double:
1490 ((struct spa_pod_double *)self->pod)->value = ((struct spa_pod_double *)pod->pod)->value;
1491 break;
1492 case SPA_TYPE_Pointer:
1493 ((struct spa_pod_pointer *)self->pod)->body.type = ((struct spa_pod_pointer *)pod->pod)->body.type;
1494 ((struct spa_pod_pointer *)self->pod)->body.value = ((struct spa_pod_pointer *)pod->pod)->body.value;
1495 break;
1496 case SPA_TYPE_Fd:
1497 ((struct spa_pod_fd *)self->pod)->value = ((struct spa_pod_fd *)pod->pod)->value;
1498 break;
1499 case SPA_TYPE_Rectangle:
1500 ((struct spa_pod_rectangle *)self->pod)->value.width = ((struct spa_pod_rectangle *)pod->pod)->value.width;
1501 ((struct spa_pod_rectangle *)self->pod)->value.height = ((struct spa_pod_rectangle *)pod->pod)->value.height;
1502 break;
1503 case SPA_TYPE_Fraction:
1504 ((struct spa_pod_fraction *)self->pod)->value.num = ((struct spa_pod_fraction *)pod->pod)->value.num;
1505 ((struct spa_pod_fraction *)self->pod)->value.denom = ((struct spa_pod_fraction *)pod->pod)->value.denom;
1506 break;
1507 default:
1508 g_return_val_if_fail (self->pod->size >= pod->pod->size, FALSE);
1509 memcpy (SPA_MEMBER (self->pod, sizeof (struct spa_pod), void),
1510 SPA_MEMBER (pod->pod, sizeof (struct spa_pod), void),
1511 SPA_MIN (self->pod->size, pod->pod->size));
1512 self->pod->type = pod->pod->type;
1513 self->pod->size = pod->pod->size;
1514 break;
1515 }
1516
1517 switch (self->type) {
1518 case WP_SPA_POD_PROPERTY:
1519 self->static_pod.data_property.table = pod->static_pod.data_property.table;
1520 self->static_pod.data_property.key = pod->static_pod.data_property.key;
1521 self->static_pod.data_property.flags = pod->static_pod.data_property.flags;
1522 break;
1523 case WP_SPA_POD_CONTROL:
1524 self->static_pod.data_control.offset = pod->static_pod.data_control.offset;
1525 self->static_pod.data_control.type = pod->static_pod.data_control.type;
1526 break;
1527 case WP_SPA_POD_REGULAR:
1528 default:
1529 break;
1530 }
1531
1532 return TRUE;
1533 }
1534
1535 /*!
1536 * \brief Checks whether two spa pod objects have the same value or not
1537 *
1538 * \ingroup wpspapod
1539 * \param self the spa pod object
1540 * \param pod the pod with the value to be compared with
1541 * \returns TRUE if both spa pod objects have the same values, FALSE othewrise.
1542 */
1543 gboolean
1544 wp_spa_pod_equal (WpSpaPod *self, WpSpaPod *pod)
1545 {
1546 g_return_val_if_fail (self != NULL, FALSE);
1547 g_return_val_if_fail (pod != NULL, FALSE);
1548
1549 if (self->type != pod->type)
1550 return FALSE;
1551 if (SPA_POD_TYPE (self->pod) != SPA_POD_TYPE (pod->pod))
1552 return FALSE;
1553
1554 switch (SPA_POD_TYPE (self->pod)) {
1555 case SPA_TYPE_None:
1556 break;
1557 case SPA_TYPE_Bool:
1558 if (((struct spa_pod_bool *)self->pod)->value != ((struct spa_pod_bool *)pod->pod)->value)
1559 return FALSE;
1560 break;
1561 case SPA_TYPE_Id:
1562 if (((struct spa_pod_id *)self->pod)->value != ((struct spa_pod_id *)pod->pod)->value)
1563 return FALSE;
1564 break;
1565 case SPA_TYPE_Int:
1566 if (((struct spa_pod_int *)self->pod)->value != ((struct spa_pod_int *)pod->pod)->value)
1567 return FALSE;
1568 break;
1569 case SPA_TYPE_Long:
1570 if (((struct spa_pod_long *)self->pod)->value != ((struct spa_pod_long *)pod->pod)->value)
1571 return FALSE;
1572 break;
1573 case SPA_TYPE_Float:
1574 if (((struct spa_pod_float *)self->pod)->value != ((struct spa_pod_float *)pod->pod)->value)
1575 return FALSE;
1576 break;
1577 case SPA_TYPE_Double:
1578 if (((struct spa_pod_double *)self->pod)->value != ((struct spa_pod_double *)pod->pod)->value)
1579 return FALSE;
1580 break;
1581 case SPA_TYPE_Pointer:
1582 if (((struct spa_pod_pointer *)self->pod)->body.type != ((struct spa_pod_pointer *)pod->pod)->body.type ||
1583 ((struct spa_pod_pointer *)self->pod)->body.value != ((struct spa_pod_pointer *)pod->pod)->body.value)
1584 return FALSE;
1585 break;
1586 case SPA_TYPE_Fd:
1587 if (((struct spa_pod_fd *)self->pod)->value != ((struct spa_pod_fd *)pod->pod)->value)
1588 return FALSE;
1589 break;
1590 case SPA_TYPE_Rectangle:
1591 if (((struct spa_pod_rectangle *)self->pod)->value.width != ((struct spa_pod_rectangle *)pod->pod)->value.width ||
1592 ((struct spa_pod_rectangle *)self->pod)->value.height != ((struct spa_pod_rectangle *)pod->pod)->value.height)
1593 return FALSE;
1594 break;
1595 case SPA_TYPE_Fraction:
1596 if (((struct spa_pod_fraction *)self->pod)->value.num != ((struct spa_pod_fraction *)pod->pod)->value.num ||
1597 ((struct spa_pod_fraction *)self->pod)->value.denom != ((struct spa_pod_fraction *)pod->pod)->value.denom)
1598 return FALSE;
1599 break;
1600 default:
1601 if (self->pod->size != pod->pod->size ||
1602 memcmp (SPA_MEMBER (self->pod, sizeof (struct spa_pod), void),
1603 SPA_MEMBER (pod->pod, sizeof (struct spa_pod), void),
1604 self->pod->size) != 0)
1605 return FALSE;
1606 break;
1607 }
1608
1609 switch (self->type) {
1610 case WP_SPA_POD_PROPERTY:
1611 if (self->static_pod.data_property.table != pod->static_pod.data_property.table ||
1612 self->static_pod.data_property.key != pod->static_pod.data_property.key ||
1613 self->static_pod.data_property.flags != pod->static_pod.data_property.flags)
1614 return FALSE;
1615 break;
1616 case WP_SPA_POD_CONTROL:
1617 if (self->static_pod.data_control.offset != pod->static_pod.data_control.offset ||
1618 self->static_pod.data_control.type != pod->static_pod.data_control.type)
1619 return FALSE;
1620 break;
1621 case WP_SPA_POD_REGULAR:
1622 default:
1623 break;
1624 }
1625
1626 return TRUE;
1627 }
1628
1629 /*!
1630 * \brief Gets the object properties values of a spa pod object
1631 *
1632 * \ingroup wpspapod
1633 * \param self the spa pod object
1634 * \param id_name (out): the id name of the object,
1635 * \param ... (out): the list of the object properties values, followed by NULL
1636 * \returns TRUE if the object properties values were obtained, FALSE otherwise
1637 */
1638 gboolean
1639 wp_spa_pod_get_object (WpSpaPod *self, const char **id_name, ...)
1640 {
1641 va_list args;
1642 gboolean res;
1643 va_start (args, id_name);
1644 res = wp_spa_pod_get_object_valist (self, id_name, args);
1645 va_end (args);
1646 return res;
1647 }
1648
1649 /*!
1650 * \brief This is the `va_list` version of wp_spa_pod_get_object()
1651 *
1652 * \ingroup wpspapod
1653 * \param self the spa pod object
1654 * \param id_name (out): the id name of the object
1655 * \param args (out): the variable arguments passed to wp_spa_pod_get_object()
1656 * \returns TRUE if the object properties values were obtained, FALSE otherwise
1657 */
1658 gboolean
1659 wp_spa_pod_get_object_valist (WpSpaPod *self, const char **id_name, va_list args)
1660 {
1661 g_return_val_if_fail (self, FALSE);
1662 g_return_val_if_fail (wp_spa_pod_is_object (self), FALSE);
1663 g_autoptr (WpSpaPodParser) p = wp_spa_pod_parser_new_object (self, id_name);
1664 const gboolean res = wp_spa_pod_parser_get_valist (p, args);
1665 wp_spa_pod_parser_end (p);
1666 return res;
1667 }
1668
1669 /*!
1670 * \brief Gets the struct's values of a spa pod object
1671 *
1672 * \ingroup wpspapod
1673 * \param self the spa pod object
1674 * \param ... (out): the list of the struct values, followed by NULL
1675 * \returns TRUE if the struct values were obtained, FALSE otherwise
1676 */
1677 gboolean
1678 wp_spa_pod_get_struct (WpSpaPod *self, ...)
1679 {
1680 va_list args;
1681 gboolean res;
1682 va_start (args, self);
1683 res = wp_spa_pod_get_struct_valist (self, args);
1684 va_end (args);
1685 return res;
1686 }
1687
1688 /*!
1689 * \brief This is the `va_list` version of wp_spa_pod_get_struct()
1690 *
1691 * \ingroup wpspapod
1692 * \param self the spa pod object
1693 * \param args (out): the variable arguments passed to wp_spa_pod_get_struct()
1694 * \returns TRUE if the struct values were obtained, FALSE otherwise
1695 */
1696 gboolean
1697 wp_spa_pod_get_struct_valist (WpSpaPod *self, va_list args)
1698 {
1699 g_return_val_if_fail (self, FALSE);
1700 g_return_val_if_fail (wp_spa_pod_is_struct (self), FALSE);
1701 g_autoptr (WpSpaPodParser) p = wp_spa_pod_parser_new_struct (self);
1702 const gboolean res = wp_spa_pod_parser_get_valist (p, args);
1703 wp_spa_pod_parser_end (p);
1704 return res;
1705 }
1706
1707 /*!
1708 * \brief Gets the name, flags and spa pod value of a spa pod property
1709 *
1710 * \ingroup wpspapod
1711 * \param self the spa pod object
1712 * \param key (out) (optional): the name of the property
1713 * \param value (out) (optional): the spa pod value of the property
1714 * \returns TRUE if the value was obtained, FALSE otherwise
1715 */
1716 gboolean
1717 wp_spa_pod_get_property (WpSpaPod *self, const char **key,
1718 WpSpaPod **value)
1719 {
1720 g_return_val_if_fail (self, FALSE);
1721 g_return_val_if_fail (wp_spa_pod_is_property (self), FALSE);
1722
1723 if (key) {
1724 WpSpaIdValue key_val = wp_spa_id_table_find_value (
1725 self->static_pod.data_property.table,
1726 self->static_pod.data_property.key);
1727 if (key_val) {
1728 *key = wp_spa_id_value_short_name (key_val);
1729 } else {
1730 g_snprintf (self->static_pod.data_property.id_key_name,
1731 WP_SPA_POD_ID_PROPERTY_NAME_MAX, "id-%08x",
1732 self->static_pod.data_property.key);
1733 *key = self->static_pod.data_property.id_key_name;
1734 }
1735 }
1736 if (value)
1737 *value = wp_spa_pod_new_wrap (self->pod);
1738
1739 return TRUE;
1740 }
1741
1742 /*!
1743 * \brief Gets the offset, type name and spa pod value of a spa pod control
1744 *
1745 * \ingroup wpspapod
1746 * \param self the spa pod object
1747 * \param offset (out) (optional): the offset of the control
1748 * \param ctl_type (out) (optional): the control type (Properties, Midi, ...)
1749 * \param value (out) (optional): the spa pod value of the control
1750 * \returns TRUE if the value was obtained, FALSE otherwise
1751 */
1752 gboolean
1753 wp_spa_pod_get_control (WpSpaPod *self, guint32 *offset, const char **ctl_type,
1754 WpSpaPod **value)
1755 {
1756 g_return_val_if_fail (self, FALSE);
1757 g_return_val_if_fail (wp_spa_pod_is_control (self), FALSE);
1758
1759 if (offset)
1760 *offset = self->static_pod.data_control.offset;
1761 if (ctl_type) {
1762 WpSpaIdValue type_val = wp_spa_id_value_from_number (
1763 SPA_TYPE_INFO_Control, self->static_pod.data_control.type);
1764 g_return_val_if_fail (type_val != NULL, FALSE);
1765 *ctl_type = wp_spa_id_value_short_name (type_val);
1766 }
1767 if (value)
1768 *value = wp_spa_pod_new_wrap (self->pod);
1769
1770 return TRUE;
1771 }
1772
1773 /*!
1774 * \brief Gets the child of a spa pod choice object
1775 *
1776 * \ingroup wpspapod
1777 * \param self a spa pod choice object
1778 * \returns (transfer full): the child of the spa pod choice object
1779 */
1780 WpSpaPod *
1781 wp_spa_pod_get_choice_child (WpSpaPod *self)
1782 {
1783 g_return_val_if_fail (wp_spa_pod_is_choice (self), NULL);
1784 return wp_spa_pod_new_wrap (SPA_POD_CHOICE_CHILD (self->pod));
1785 }
1786
1787 /*!
1788 * \brief Gets the child of a spa pod array object
1789 *
1790 * \ingroup wpspapod
1791 * \param self a spa pod choice object
1792 * \returns (transfer full): the child of the spa pod array object
1793 */
1794 WpSpaPod *
1795 wp_spa_pod_get_array_child (WpSpaPod *self)
1796 {
1797 g_return_val_if_fail (wp_spa_pod_is_array (self), NULL);
1798 return wp_spa_pod_new_wrap (SPA_POD_ARRAY_CHILD (self->pod));
1799 }
1800
1801 /*!
1802 * \brief Fixates choices in an object pod so that they only have one value
1803 *
1804 * \ingroup wpspapod
1805 * \param self a spa pod
1806 * \returns TRUE if the pod was an object and it went through the fixation
1807 * procedure, FALSE otherwise
1808 */
1809 gboolean
1810 wp_spa_pod_fixate (WpSpaPod *self)
1811 {
1812 g_return_val_if_fail (self, FALSE);
1813
1814 if (wp_spa_pod_is_object (self))
1815 return spa_pod_object_fixate ((struct spa_pod_object *) self->pod) == 0;
1816 return FALSE;
1817 }
1818
1819 /*!
1820 * \brief Returns the intersection between \a self and \a filter
1821 *
1822 * This is typically used to intersect pods that describe formats, in order to
1823 * find a common format that is acceptable by both sides. For that purpose,
1824 * this is not exactly an intersection with its mathematical meaning.
1825 * Object properties can be thought of as format constraints. When one side does
1826 * not specify a specific property, it is considered to accept any value for it,
1827 * so the value of this property from the other side is added in the result.
1828 *
1829 * Both input pods are left unmodified after this function call.
1830 *
1831 * If NULL is passed in the \a filter, this function just copies \a self and
1832 * returns the copy.
1833 *
1834 * \param self the first pod
1835 * \param filter (nullable): the second pod
1836 * \return (transfer full) (nullable): a new pod that contains the intersection
1837 * between \a self and \a filter, or NULL if the intersection was not possible
1838 * to make
1839 */
1840 WpSpaPod *
1841 wp_spa_pod_filter (WpSpaPod *self, WpSpaPod *filter)
1842 {
1843 char buffer[1024];
1844 struct spa_pod_builder b = SPA_POD_BUILDER_INIT(&buffer, sizeof(buffer));
1845 struct spa_pod *result = NULL;
1846
1847 g_return_val_if_fail (self, NULL);
1848
1849 if (spa_pod_filter(&b, &result, self->pod, filter ? filter->pod : NULL) >= 0)
1850 return wp_spa_pod_new_wrap_copy (result);
1851 return NULL;
1852 }
1853
1854 /*!
1855 * \brief Increases the reference count of a spa pod builder
1856 *
1857 * \ingroup wpspapod
1858 * \param self a spa pod builder object
1859 * \returns (transfer full): \a self with an additional reference count on it
1860 */
1861 WpSpaPodBuilder *
1862 wp_spa_pod_builder_ref (WpSpaPodBuilder *self)
1863 {
1864 return (WpSpaPodBuilder *) g_rc_box_acquire ((gpointer) self);
1865 }
1866
1867 static void
1868 wp_spa_pod_builder_free (WpSpaPodBuilder *self)
1869 {
1870 g_clear_pointer (&self->buf, g_free);
1871 }
1872
1873 /*!
1874 * \brief Decreases the reference count on \a self and frees it when the ref
1875 * count reaches zero.
1876 *
1877 * \ingroup wpspapod
1878 * \param self (transfer full): a spa pod builder object
1879 */
1880 void
1881 wp_spa_pod_builder_unref (WpSpaPodBuilder *self)
1882 {
1883 g_rc_box_release_full (self, (GDestroyNotify) wp_spa_pod_builder_free);
1884 }
1885
1886 /*!
1887 * \brief Creates a spa pod builder of type array
1888 * \ingroup wpspapod
1889 * \returns (transfer full): the new spa pod builder
1890 */
1891 WpSpaPodBuilder *
1892 wp_spa_pod_builder_new_array (void)
1893 {
1894 WpSpaPodBuilder *self = wp_spa_pod_builder_new (
1895 WP_SPA_POD_BUILDER_REALLOC_STEP_SIZE, SPA_TYPE_Array);
1896 spa_pod_builder_push_array (&self->builder, &self->frame);
1897 return self;
1898 }
1899
1900 /*!
1901 * \brief Creates a spa pod builder of type choice
1902 *
1903 * \ingroup wpspapod
1904 * \param choice_type the name of the choice type ("Range", "Step", ...)
1905 * \returns (transfer full): the new spa pod builder
1906 */
1907 WpSpaPodBuilder *
1908 wp_spa_pod_builder_new_choice (const char *choice_type)
1909 {
1910 WpSpaPodBuilder *self = NULL;
1911 WpSpaIdValue type = wp_spa_id_value_from_short_name (
1912 SPA_TYPE_INFO_Choice, choice_type);
1913 g_return_val_if_fail (type != NULL, NULL);
1914
1915 /* Construct the builder */
1916 self = wp_spa_pod_builder_new (WP_SPA_POD_BUILDER_REALLOC_STEP_SIZE,
1917 SPA_TYPE_Choice);
1918
1919 /* Push the array */
1920 spa_pod_builder_push_choice (&self->builder, &self->frame,
1921 wp_spa_id_value_number (type), 0);
1922
1923 return self;
1924 }
1925
1926 /*!
1927 * \brief Creates a spa pod builder of type object
1928 *
1929 * \ingroup wpspapod
1930 * \param type_name the type name of the object type
1931 * \param id_name the Id name of the object
1932 * \returns (transfer full): the new spa pod builder
1933 */
1934 WpSpaPodBuilder *
1935 wp_spa_pod_builder_new_object (const char *type_name, const char *id_name)
1936 {
1937 WpSpaPodBuilder *self = NULL;
1938 WpSpaType type;
1939 WpSpaIdTable table;
1940 WpSpaIdValue id;
1941
1942 /* Find the type */
1943 type = wp_spa_type_from_name (type_name);
1944 g_return_val_if_fail (wp_spa_type_is_object (type), NULL);
1945
1946 /* Find the id */
1947 table = wp_spa_type_get_object_id_values_table (type);
1948 g_return_val_if_fail (table != NULL, NULL);
1949
1950 id = wp_spa_id_table_find_value_from_short_name (table, id_name);
1951 g_return_val_if_fail (id != NULL, NULL);
1952
1953 /* Construct the builder */
1954 self = wp_spa_pod_builder_new (WP_SPA_POD_BUILDER_REALLOC_STEP_SIZE, type);
1955
1956 /* Push the object */
1957 spa_pod_builder_push_object (&self->builder, &self->frame, type,
1958 wp_spa_id_value_number (id));
1959
1960 return self;
1961 }
1962
1963 /*!
1964 * \brief Creates a spa pod builder of type struct
1965 *
1966 * \ingroup wpspapod
1967 * \returns (transfer full): the new spa pod builder
1968 */
1969 WpSpaPodBuilder *
1970 wp_spa_pod_builder_new_struct (void)
1971 {
1972 WpSpaPodBuilder *self = NULL;
1973 self = wp_spa_pod_builder_new (WP_SPA_POD_BUILDER_REALLOC_STEP_SIZE,
1974 SPA_TYPE_Struct);
1975 spa_pod_builder_push_struct (&self->builder, &self->frame);
1976 return self;
1977 }
1978
1979 /*!
1980 * \brief Creates a spa pod builder of type sequence
1981 *
1982 * \ingroup wpspapod
1983 * \returns (transfer full): the new spa pod builder
1984 */
1985 WpSpaPodBuilder *
1986 wp_spa_pod_builder_new_sequence (guint unit)
1987 {
1988 WpSpaPodBuilder *self = NULL;
1989 self = wp_spa_pod_builder_new (WP_SPA_POD_BUILDER_REALLOC_STEP_SIZE,
1990 SPA_TYPE_Sequence);
1991 spa_pod_builder_push_sequence (&self->builder, &self->frame, unit);
1992 return self;
1993 }
1994
1995 /*!
1996 * \brief Adds a none value into the builder
1997 *
1998 * \ingroup wpspapod
1999 * \param self the spa pod builder object
2000 */
2001
2002 void
2003 wp_spa_pod_builder_add_none (WpSpaPodBuilder *self)
2004 {
2005 spa_pod_builder_none (&self->builder);
2006 }
2007
2008 /*!
2009 * \brief Adds a boolean value into the builder
2010 *
2011 * \ingroup wpspapod
2012 * \param self the spa pod builder object
2013 * \param value the boolean value
2014 */
2015 void
2016 wp_spa_pod_builder_add_boolean (WpSpaPodBuilder *self, gboolean value)
2017 {
2018 spa_pod_builder_bool (&self->builder, value ? true : false);
2019 }
2020
2021 /*!
2022 * \brief Adds a Id value into the builder
2023 *
2024 * \ingroup wpspapod
2025 * \param self the spa pod builder object
2026 * \param value the Id value
2027 */
2028 void
2029 wp_spa_pod_builder_add_id (WpSpaPodBuilder *self, guint32 value)
2030 {
2031 spa_pod_builder_id (&self->builder, value);
2032 }
2033
2034 /*!
2035 * \brief Adds a int value into the builder
2036 *
2037 * \ingroup wpspapod
2038 * \param self the spa pod builder object
2039 * \param value the int value
2040 */
2041 void
2042 wp_spa_pod_builder_add_int (WpSpaPodBuilder *self, gint32 value)
2043 {
2044 spa_pod_builder_int (&self->builder, value);
2045 }
2046
2047 /*!
2048 * \brief Adds a long value into the builder
2049 *
2050 * \ingroup wpspapod
2051 * \param self the spa pod builder object
2052 * \param value the long value
2053 */
2054 void
2055 wp_spa_pod_builder_add_long (WpSpaPodBuilder *self, gint64 value)
2056 {
2057 spa_pod_builder_long (&self->builder, value);
2058 }
2059
2060 /*!
2061 * \brief Adds a float value into the builder
2062 *
2063 * \ingroup wpspapod
2064 * \param self the spa pod builder object
2065 * \param value the float value
2066 */
2067 void
2068 wp_spa_pod_builder_add_float (WpSpaPodBuilder *self, float value)
2069 {
2070 spa_pod_builder_float (&self->builder, value);
2071 }
2072
2073 /*!
2074 * \brief Adds a double value into the builder
2075 *
2076 * \ingroup wpspapod
2077 * \param self the spa pod builder object
2078 * \param value the double value
2079 */
2080 void
2081 wp_spa_pod_builder_add_double (WpSpaPodBuilder *self, double value)
2082 {
2083 spa_pod_builder_double (&self->builder, value);
2084 }
2085
2086 /*!
2087 * \brief Adds a string value into the builder
2088 *
2089 * \ingroup wpspapod
2090 * \param self the spa pod builder object
2091 * \param value the string value
2092 */
2093 void
2094 wp_spa_pod_builder_add_string (WpSpaPodBuilder *self, const char *value)
2095 {
2096 spa_pod_builder_string (&self->builder, value);
2097 }
2098
2099 /*!
2100 * \brief Adds a bytes value with its length into the builder
2101 *
2102 * \ingroup wpspapod
2103 * \param self the spa pod builder object
2104 * \param value the bytes value
2105 * \param len the length of the bytes value
2106 */
2107 void
2108 wp_spa_pod_builder_add_bytes (WpSpaPodBuilder *self, gconstpointer value,
2109 guint32 len)
2110 {
2111 spa_pod_builder_bytes (&self->builder, value, len);
2112 }
2113
2114 /*!
2115 * \brief Adds a pointer value with its type name into the builder
2116 *
2117 * \ingroup wpspapod
2118 * \param self the spa pod builder object
2119 * \param type_name the type name that the pointer points to
2120 * \param value the pointer value
2121 */
2122 void
2123 wp_spa_pod_builder_add_pointer (WpSpaPodBuilder *self, const char *type_name,
2124 gconstpointer value)
2125 {
2126 WpSpaType type = wp_spa_type_from_name (type_name);
2127 g_return_if_fail (wp_spa_type_parent (type) == SPA_TYPE_Pointer);
2128 spa_pod_builder_pointer (&self->builder, type, value);
2129 }
2130
2131 /*!
2132 * \brief Adds a Fd value into the builder
2133 *
2134 * \ingroup wpspapod
2135 * \param self the spa pod builder object
2136 * \param value the Fd value
2137 */
2138 void
2139 wp_spa_pod_builder_add_fd (WpSpaPodBuilder *self, gint64 value)
2140 {
2141 spa_pod_builder_fd (&self->builder, value);
2142 }
2143
2144 /*!
2145 * \brief Adds the width and height values of a rectangle into the builder
2146 *
2147 * \ingroup wpspapod
2148 * \param self the spa pod builder object
2149 * \param width the width value of the rectangle
2150 * \param height the height value of the rectangle
2151 */
2152 void
2153 wp_spa_pod_builder_add_rectangle (WpSpaPodBuilder *self, guint32 width,
2154 guint32 height)
2155 {
2156 spa_pod_builder_rectangle (&self->builder, width, height);
2157 }
2158
2159 /*!
2160 * \brief Adds the numerator and denominator values of a fraction into the builder
2161 *
2162 * \ingroup wpspapod
2163 * \param self the spa pod builder object
2164 * \param num the numerator value of the fraction
2165 * \param denom the denominator value of the fraction
2166 */
2167 void
2168 wp_spa_pod_builder_add_fraction (WpSpaPodBuilder *self, guint32 num,
2169 guint32 denom)
2170 {
2171 spa_pod_builder_fraction (&self->builder, num, denom);
2172 }
2173
2174 /*!
2175 * \brief Adds a pod value into the builder
2176 *
2177 * \ingroup wpspapod
2178 * \param self the spa pod builder object
2179 * \param pod the pod value
2180 */
2181 void
2182 wp_spa_pod_builder_add_pod (WpSpaPodBuilder *self, WpSpaPod *pod)
2183 {
2184 spa_pod_builder_primitive (&self->builder, pod->pod);
2185 }
2186
2187 /*!
2188 * \brief Adds a property into the builder
2189 *
2190 * \ingroup wpspapod
2191 * \param self the spa pod builder object
2192 * \param key the name of the property
2193 */
2194 void
2195 wp_spa_pod_builder_add_property (WpSpaPodBuilder *self, const char *key)
2196 {
2197 guint key_id;
2198 if (g_str_has_prefix (key, "id-")) {
2199 g_return_if_fail (sscanf (key, "id-%08x", &key_id) == 1);
2200 } else {
2201 WpSpaIdTable table = wp_spa_type_get_values_table (self->type);
2202 WpSpaIdValue id = wp_spa_id_table_find_value_from_short_name (table, key);
2203 g_return_if_fail (id != NULL);
2204 key_id = wp_spa_id_value_number (id);
2205 }
2206 spa_pod_builder_prop (&self->builder, key_id, 0);
2207 }
2208
2209 /*!
2210 * \brief Adds a property into the builder
2211 *
2212 * \ingroup wpspapod
2213 * \param self the spa pod builder object
2214 * \param id the id of the property
2215 */
2216 void
2217 wp_spa_pod_builder_add_property_id (WpSpaPodBuilder *self, guint32 id)
2218 {
2219 spa_pod_builder_prop (&self->builder, id, 0);
2220 }
2221
2222 /*!
2223 * \brief Adds a control into the builder
2224 *
2225 * \ingroup wpspapod
2226 * \param self the spa pod builder object
2227 * \param offset the offset of the control
2228 * \param ctl_type the type name of the control
2229 */
2230 void
2231 wp_spa_pod_builder_add_control (WpSpaPodBuilder *self, guint32 offset,
2232 const char *ctl_type)
2233 {
2234 WpSpaIdValue id = wp_spa_id_value_from_short_name (
2235 SPA_TYPE_INFO_Control, ctl_type);
2236 g_return_if_fail (id != NULL);
2237 spa_pod_builder_control (&self->builder, offset, wp_spa_id_value_number (id));
2238 }
2239
2240 /*!
2241 * \brief Adds a list of values into the builder
2242 *
2243 * \ingroup wpspapod
2244 * \param self the spa pod builder object
2245 * \param ... a list of additional values, followed by NULL
2246 */
2247 void
2248 wp_spa_pod_builder_add (WpSpaPodBuilder *self, ...)
2249 {
2250 va_list args;
2251 va_start (args, self);
2252 wp_spa_pod_builder_add_valist (self, args);
2253 va_end (args);
2254 }
2255
2256 /*!
2257 * \brief Adds a list of values into the builder
2258 *
2259 * \ingroup wpspapod
2260 * \param self the spa pod builder object
2261 * \param args the variable arguments passed to wp_spa_pod_builder_add()
2262 */
2263 void
2264 wp_spa_pod_builder_add_valist (WpSpaPodBuilder *self, va_list args)
2265 {
2266 WpSpaIdTable table = wp_spa_type_get_values_table (self->type);
2267
2268 do {
2269 WpSpaIdValue key = NULL;
2270 const char *format;
2271 int n_values = 1;
2272 struct spa_pod_frame f;
2273 gboolean choice;
2274
2275 if (wp_spa_type_is_object (self->type)) {
2276 guint key_id;
2277 const char *key_name = va_arg(args, const char *);
2278 if (!key_name)
2279 return;
2280 if (g_str_has_prefix (key_name, "id-")) {
2281 g_return_if_fail (sscanf (key_name, "id-%08x", &key_id) == 1);
2282 } else {
2283 key = wp_spa_id_table_find_value_from_short_name (table, key_name);
2284 g_return_if_fail (key != NULL);
2285 key_id = wp_spa_id_value_number (key);
2286 }
2287 spa_pod_builder_prop (&self->builder, key_id, 0);
2288 }
2289 else if (self->type == SPA_TYPE_Sequence) {
2290 guint32 offset = va_arg(args, uint32_t);
2291 if (offset == 0)
2292 return;
2293 const char *control_name = va_arg(args, const char *);
2294 if (!control_name)
2295 return;
2296 WpSpaIdValue type = wp_spa_id_value_from_short_name (
2297 SPA_TYPE_INFO_Control, control_name);
2298 g_return_if_fail (type != NULL);
2299
2300 spa_pod_builder_control (&self->builder, offset,
2301 wp_spa_id_value_number (type));
2302 }
2303
2304 if ((format = va_arg(args, const char *)) == NULL)
2305 break;
2306
2307 choice = *format == '?';
2308 if (choice) {
2309 uint32_t type = spa_choice_from_id (*++format);
2310 if (*format != '\0')
2311 format++;
2312 spa_pod_builder_push_choice (&self->builder, &f, type, 0);
2313 n_values = va_arg(args, int);
2314 }
2315 while (n_values-- > 0) {
2316 switch (*format) {
2317 case 'P': /* Pod */
2318 case 'V': /* Choice */
2319 case 'O': /* Object */
2320 case 'T': { /* Struct */
2321 WpSpaPod *arg = va_arg(args, WpSpaPod *);
2322 if (arg == NULL)
2323 spa_pod_builder_none (&self->builder);
2324 else
2325 spa_pod_builder_primitive (&self->builder, arg->pod);
2326 break;
2327 }
2328 case 'K': { /* Id as string - WirePlumber extension */
2329 const char * id = va_arg(args, const char *);
2330 if (key) {
2331 WpSpaIdTable id_table = NULL;
2332 wp_spa_id_value_get_value_type (key, &id_table);
2333 WpSpaIdValue id_val =
2334 wp_spa_id_table_find_value_from_short_name (id_table, id);
2335 spa_pod_builder_id (&self->builder, wp_spa_id_value_number (id_val));
2336 }
2337 break;
2338 }
2339 case 'b':
2340 spa_pod_builder_bool(&self->builder,
2341 va_arg(args, gboolean) ? true : false);
2342 break;
2343 default:
2344 SPA_POD_BUILDER_COLLECT(&self->builder, *format, args);
2345 break;
2346 }
2347 }
2348
2349 if (choice)
2350 spa_pod_builder_pop (&self->builder, &f);
2351
2352 } while (TRUE);
2353 }
2354
2355 /*!
2356 * \brief Ends the builder process and returns the constructed spa pod object
2357 *
2358 * \ingroup wpspapod
2359 * \param self the spa pod builder object
2360 * \returns (transfer full): the constructed spa pod object
2361 */
2362 WpSpaPod *
2363 wp_spa_pod_builder_end (WpSpaPodBuilder *self)
2364 {
2365 WpSpaPod *ret = NULL;
2366
2367 /* Construct the pod */
2368 ret = g_slice_new0 (WpSpaPod);
2369 g_ref_count_init (&ret->ref);
2370 ret->type = WP_SPA_POD_REGULAR;
2371 ret->pod = spa_pod_builder_pop (&self->builder, &self->frame);
2372 ret->builder = wp_spa_pod_builder_ref (self);
2373
2374 /* Also copy the specific object type if it is an object */
2375 if (spa_pod_is_object (ret->pod))
2376 ret->static_pod.data_property.table =
2377 wp_spa_type_get_values_table (ret->builder->type);
2378
2379 return ret;
2380 }
2381
2382 /*!
2383 * \brief Increases the reference count of a spa pod parser
2384 * \ingroup wpspapod
2385 * \param self a spa pod sparser object
2386 * \returns (transfer full): \a self with an additional reference count on it
2387 */
2388 WpSpaPodParser *
2389 wp_spa_pod_parser_ref (WpSpaPodParser *self)
2390 {
2391 return (WpSpaPodParser *) g_rc_box_acquire ((gpointer) self);
2392 }
2393
2394 static void
2395 wp_spa_pod_parser_free (WpSpaPodParser *self)
2396 {
2397 self->pod = NULL;
2398 }
2399
2400 /*!
2401 * \brief Decreases the reference count on \a self and frees it when the ref
2402 * count reaches zero.
2403 *
2404 * \ingroup wpspapod
2405 * \param self (transfer full): a spa pod parser object
2406 */
2407 void
2408 wp_spa_pod_parser_unref (WpSpaPodParser *self)
2409 {
2410 g_rc_box_release_full (self, (GDestroyNotify) wp_spa_pod_parser_free);
2411 }
2412
2413 static WpSpaPodParser *
2414 wp_spa_pod_parser_new (WpSpaPod *pod, guint32 type)
2415 {
2416 WpSpaPodParser *self = g_rc_box_new0 (WpSpaPodParser);
2417 self->type = type;
2418 self->pod = pod;
2419 spa_pod_parser_pod (&self->parser, self->pod->pod);
2420 return self;
2421 }
2422
2423 /*!
2424 * \brief Creates an object spa pod parser. The \a pod object must be valid for
2425 * the entire life-cycle of the returned parser.
2426 *
2427 * \ingroup wpspapod
2428 * \param pod the object spa pod to parse
2429 * \param id_name the Id name of the object
2430 * \returns (transfer full): The new spa pod parser
2431 */
2432 WpSpaPodParser *
2433 wp_spa_pod_parser_new_object (WpSpaPod *pod, const char **id_name)
2434 {
2435 WpSpaPodParser *self = NULL;
2436 WpSpaType type = wp_spa_pod_get_spa_type (pod);
2437 guint32 id = SPA_ID_INVALID;
2438
2439 g_return_val_if_fail (wp_spa_pod_is_object (pod), NULL);
2440
2441 self = wp_spa_pod_parser_new (pod, type);
2442 spa_pod_parser_push_object (&self->parser, &self->frame, type, &id);
2443 if (id_name) {
2444 WpSpaIdTable table = wp_spa_type_get_object_id_values_table (type);
2445 *id_name = wp_spa_id_value_short_name (
2446 wp_spa_id_table_find_value (table, id));
2447 }
2448 return self;
2449 }
2450
2451 /*!
2452 * \brief Creates an struct spa pod parser. The \a pod object must be valid for
2453 * the entire life-cycle of the returned parser.
2454 *
2455 * \ingroup wpspapod
2456 * \param pod the struct spa pod to parse
2457 * \returns (transfer full): The new spa pod parser
2458 */
2459 WpSpaPodParser *
2460 wp_spa_pod_parser_new_struct (WpSpaPod *pod)
2461 {
2462 WpSpaPodParser *self = NULL;
2463
2464 g_return_val_if_fail (wp_spa_pod_is_struct (pod), NULL);
2465
2466 self = wp_spa_pod_parser_new (pod, SPA_TYPE_Struct);
2467 spa_pod_parser_push_struct (&self->parser, &self->frame);
2468 return self;
2469 }
2470
2471 /*!
2472 * \brief Gets the boolean value from a spa pod parser
2473 *
2474 * \ingroup wpspapod
2475 * \param self the spa pod parser object
2476 * \param value (out): the boolean value
2477 * \returns TRUE if the value was obtained, FALSE otherwise
2478 */
2479 gboolean
2480 wp_spa_pod_parser_get_boolean (WpSpaPodParser *self, gboolean *value)
2481 {
2482 g_return_val_if_fail (value, FALSE);
2483 bool v = FALSE;
2484 gboolean res = spa_pod_parser_get_bool (&self->parser, &v) >= 0;
2485 *value = v ? TRUE : FALSE;
2486 return res;
2487 }
2488
2489 /*!
2490 * \brief Gets the Id value from a spa pod parser object
2491 *
2492 * \ingroup wpspapod
2493 * \param self the spa pod parser object
2494 * \param value (out): the Id value
2495 * \returns TRUE if the value was obtained, FALSE otherwise
2496 */
2497 gboolean
2498 wp_spa_pod_parser_get_id (WpSpaPodParser *self, guint32 *value)
2499 {
2500 g_return_val_if_fail (value, FALSE);
2501 return spa_pod_parser_get_id (&self->parser, value) >= 0;
2502 }
2503
2504 /*!
2505 * \brief Gets the int value from a spa pod parser object
2506 *
2507 * \ingroup wpspapod
2508 * \param self the spa pod parser object
2509 * \param value (out): the int value
2510 * \returns TRUE if the value was obtained, FALSE otherwise
2511 */
2512 gboolean
2513 wp_spa_pod_parser_get_int (WpSpaPodParser *self, gint32 *value)
2514 {
2515 g_return_val_if_fail (value, FALSE);
2516 return spa_pod_parser_get_int (&self->parser, value) >= 0;
2517 }
2518
2519 /*!
2520 * \brief Gets the long value from a spa pod parser object
2521 *
2522 * \ingroup wpspapod
2523 * \param self the spa pod parser object
2524 * \param value (out): the long value
2525 * \returns TRUE if the value was obtained, FALSE otherwise
2526 */
2527 gboolean
2528 wp_spa_pod_parser_get_long (WpSpaPodParser *self, gint64 *value)
2529 {
2530 g_return_val_if_fail (value, FALSE);
2531 return spa_pod_parser_get_long (&self->parser, value) >= 0;
2532 }
2533
2534 /*!
2535 * \brief Gets the float value from a spa pod parser object
2536 *
2537 * \ingroup wpspapod
2538 * \param self the spa pod parser object
2539 * \param value (out): the float value
2540 * \returns TRUE if the value was obtained, FALSE otherwise
2541 */
2542 gboolean
2543 wp_spa_pod_parser_get_float (WpSpaPodParser *self, float *value)
2544 {
2545 g_return_val_if_fail (value, FALSE);
2546 return spa_pod_parser_get_float (&self->parser, value) >= 0;
2547 }
2548
2549 /*!
2550 * \brief Gets the double value from a spa pod parser object
2551 *
2552 * \ingroup wpspapod
2553 * \param self the spa pod parser object
2554 * \param value (out): the double value
2555 * \returns TRUE if the value was obtained, FALSE otherwise
2556 */
2557 gboolean
2558 wp_spa_pod_parser_get_double (WpSpaPodParser *self, double *value)
2559 {
2560 g_return_val_if_fail (value, FALSE);
2561 return spa_pod_parser_get_double (&self->parser, value) >= 0;
2562 }
2563
2564 /*!
2565 * \brief Gets the string value from a spa pod parser object
2566 *
2567 * \ingroup wpspapod
2568 * \param self the spa pod parser object
2569 * \param value (out): the string value
2570 * \returns TRUE if the value was obtained, FALSE otherwise
2571 */
2572 gboolean
2573 wp_spa_pod_parser_get_string (WpSpaPodParser *self, const char **value)
2574 {
2575 g_return_val_if_fail (value, FALSE);
2576 return spa_pod_parser_get_string (&self->parser, value) >= 0;
2577 }
2578
2579 /*!
2580 * \brief Gets the bytes value and its length from a spa pod parser object
2581 *
2582 * \ingroup wpspapod
2583 * \param self the spa pod parser object
2584 * \param value (out): the bytes value
2585 * \param len (out): the length of the bytes value
2586 * \returns TRUE if the value was obtained, FALSE otherwise
2587 */
2588 gboolean
2589 wp_spa_pod_parser_get_bytes (WpSpaPodParser *self, gconstpointer *value,
2590 guint32 *len)
2591 {
2592 return spa_pod_parser_get_bytes (&self->parser, value, len) >= 0;
2593 }
2594
2595 /*!
2596 * \brief Gets the pointer value and its type name from a spa pod parser object
2597 *
2598 * \ingroup wpspapod
2599 * \param self the spa pod parser object
2600 * \param value (out): the pointer value
2601 * \returns TRUE if the value was obtained, FALSE otherwise
2602 */
2603 gboolean
2604 wp_spa_pod_parser_get_pointer (WpSpaPodParser *self, gconstpointer *value)
2605 {
2606 g_return_val_if_fail (value, FALSE);
2607 guint32 type = 0;
2608 return spa_pod_parser_get_pointer (&self->parser, &type, value) >= 0;
2609 }
2610
2611 /*!
2612 * \brief Gets the Fd value from a spa pod parser object
2613 *
2614 * \ingroup wpspapod
2615 * \param self the spa pod parser object
2616 * \param value (out): the Fd value
2617 * \returns TRUE if the value was obtained, FALSE otherwise
2618 */
2619 gboolean
2620 wp_spa_pod_parser_get_fd (WpSpaPodParser *self, gint64 *value)
2621 {
2622 g_return_val_if_fail (value, FALSE);
2623 return spa_pod_parser_get_fd (&self->parser, value) >= 0;
2624 }
2625
2626 /*!
2627 * \brief Gets the rectangle's width and height value from a spa pod parser object
2628 *
2629 * \ingroup wpspapod
2630 * \param self the spa pod parser object
2631 * \param width (out): the rectangle's width value
2632 * \param height (out): the rectangle's height value
2633 * \returns TRUE if the value was obtained, FALSE otherwise
2634 */
2635 gboolean
2636 wp_spa_pod_parser_get_rectangle (WpSpaPodParser *self, guint32 *width,
2637 guint32 *height)
2638 {
2639 struct spa_rectangle r = { 0, };
2640 gboolean res = spa_pod_parser_get_rectangle (&self->parser, &r) >= 0;
2641 if (width)
2642 *width = r.width;
2643 if (height)
2644 *height = r.height;
2645 return res;
2646 }
2647
2648 /*!
2649 * \brief Gets the fractions's numerator and denominator value from a spa pod
2650 * parser object
2651 *
2652 * \ingroup wpspapod
2653 * \param self the spa pod parser object
2654 * \param num (out): the fractions's numerator value
2655 * \param denom (out): the fractions's denominator value
2656 * \returns TRUE if the value was obtained, FALSE otherwise
2657 */
2658 gboolean
2659 wp_spa_pod_parser_get_fraction (WpSpaPodParser *self, guint32 *num,
2660 guint32 *denom)
2661 {
2662 struct spa_fraction f = { 0, };
2663 gboolean res = spa_pod_parser_get_fraction (&self->parser, &f) >= 0;
2664 if (num)
2665 *num = f.num;
2666 if (denom)
2667 *denom = f.denom;
2668 return res;
2669 }
2670
2671 /*!
2672 * \brief Gets the spa pod value from a spa pod parser object
2673 *
2674 * \ingroup wpspapod
2675 * \param self the spa pod parser object
2676 * \returns (transfer full): The spa pod value or NULL if it could not be
2677 * obtained
2678 */
2679 WpSpaPod *
2680 wp_spa_pod_parser_get_pod (WpSpaPodParser *self)
2681 {
2682 struct spa_pod *p = NULL;
2683 gboolean res = spa_pod_parser_get_pod (&self->parser, &p) >= 0;
2684 if (!res || !p)
2685 return NULL;
2686
2687 return wp_spa_pod_new_wrap (p);
2688 }
2689
2690 /*!
2691 * \brief Gets a list of values from a spa pod parser object
2692 *
2693 * \ingroup wpspapod
2694 * \param self the spa pod parser object
2695 * \param ... (out): a list of values to get, followed by NULL
2696 * \returns TRUE if the values were obtained, FALSE otherwise
2697 */
2698 gboolean
2699 wp_spa_pod_parser_get (WpSpaPodParser *self, ...)
2700 {
2701 gboolean res;
2702 va_list args;
2703
2704 va_start (args, self);
2705 res = wp_spa_pod_parser_get_valist (self, args);
2706 va_end (args);
2707
2708 return res;
2709 }
2710
2711 static inline gboolean
2712 wp_spa_pod_parser_can_collect (const struct spa_pod *pod, char format)
2713 {
2714 format = (format == 'K') ? 'I' : format;
2715 return spa_pod_parser_can_collect (pod, format);
2716 }
2717
2718 /*!
2719 * \brief This is the `va_list` version of wp_spa_pod_parser_get()
2720 *
2721 * \ingroup wpspapod
2722 * \param self the spa pod parser object
2723 * \param args the variable arguments passed to wp_spa_pod_parser_get()
2724 * \returns TRUE if the values were obtained, FALSE otherwise
2725 */
2726 gboolean
2727 wp_spa_pod_parser_get_valist (WpSpaPodParser *self, va_list args)
2728 {
2729 const struct spa_pod_prop *prop = NULL;
2730 WpSpaIdTable table = wp_spa_type_get_values_table (self->type);
2731
2732 do {
2733 WpSpaIdValue key = NULL;
2734 bool optional;
2735 const struct spa_pod *pod = NULL;
2736 const char *format;
2737
2738 if (wp_spa_type_is_object (self->type)) {
2739 guint key_id;
2740 const struct spa_pod_object *object;
2741 const char *key_name = va_arg(args, const char *);
2742 if (!key_name)
2743 break;
2744
2745 if (g_str_has_prefix (key_name, "id-")) {
2746 if (sscanf (key_name, "id-%08x", &key_id) != 1)
2747 return FALSE;
2748 } else {
2749 key = wp_spa_id_table_find_value_from_short_name (table, key_name);
2750 g_return_val_if_fail (key != NULL, FALSE);
2751 key_id = wp_spa_id_value_number (key);
2752 }
2753
2754 object = (const struct spa_pod_object *)spa_pod_parser_frame
2755 (&self->parser, &self->frame);
2756 prop = spa_pod_object_find_prop (object, prop, key_id);
2757 pod = prop ? &prop->value : NULL;
2758 }
2759
2760 if ((format = va_arg(args, char *)) == NULL)
2761 break;
2762
2763 if (self->type == SPA_TYPE_Struct)
2764 pod = spa_pod_parser_next (&self->parser);
2765
2766 if ((optional = (*format == '?')))
2767 format++;
2768
2769 if (!pod || !wp_spa_pod_parser_can_collect (pod, *format)) {
2770 if (!optional)
2771 return FALSE;
2772
2773 SPA_POD_PARSER_SKIP (*format, args);
2774 } else {
2775 if (pod->type == SPA_TYPE_Choice && *format != 'V' &&
2776 SPA_POD_CHOICE_TYPE(pod) == SPA_CHOICE_None)
2777 pod = SPA_POD_CHOICE_CHILD(pod);
2778
2779 switch (*format) {
2780 case 'P': /* Pod */
2781 case 'V': /* Choice */
2782 case 'O': /* Object */
2783 case 'T': /* Struct */
2784 *va_arg(args, WpSpaPod**) = wp_spa_pod_new_wrap_copy (pod);
2785 break;
2786 case 'K': { /* Id as string - WirePlumber extension */
2787 const char ** idstr = va_arg(args, const char **);
2788 uint32_t id = SPA_POD_VALUE(struct spa_pod_id, pod);
2789 if (key) {
2790 WpSpaIdTable id_table = NULL;
2791 wp_spa_id_value_get_value_type (key, &id_table);
2792 WpSpaIdValue id_val = wp_spa_id_table_find_value (id_table, id);
2793 *idstr = wp_spa_id_value_short_name (id_val);
2794 }
2795 break;
2796 }
2797 case 'b':
2798 *va_arg(args, gboolean*) =
2799 SPA_POD_VALUE(struct spa_pod_bool, pod) ? TRUE : FALSE;
2800 break;
2801 default:
2802 SPA_POD_PARSER_COLLECT (pod, *format, args);
2803 break;
2804 }
2805 }
2806 } while (TRUE);
2807
2808 return TRUE;
2809 }
2810
2811 /*!
2812 * \brief Ends the parser process
2813 *
2814 * \ingroup wpspapod
2815 * \param self the spa pod parser object
2816 */
2817 void
2818 wp_spa_pod_parser_end (WpSpaPodParser *self)
2819 {
2820 spa_pod_parser_pop (&self->parser, &self->frame);
2821 }
2822
2823 struct _WpSpaPodIterator
2824 {
2825 WpSpaPod *pod;
2826 union {
2827 gpointer value; /* Array and Choice */
2828 struct spa_pod *pod; /* Struct */
2829 struct spa_pod_prop *prop; /* Object */
2830 struct spa_pod_control *control; /* Sequence */
2831 } curr;
2832 };
2833 typedef struct _WpSpaPodIterator WpSpaPodIterator;
2834
2835 static gboolean
2836 wp_spa_pod_iterator_next_choice (WpSpaPodIterator *self, GValue *item)
2837 {
2838 const struct spa_pod_choice *pod_choice =
2839 (const struct spa_pod_choice *) self->pod->pod;
2840
2841 if (!self->curr.value)
2842 self->curr.value = SPA_MEMBER (&pod_choice->body, sizeof(struct spa_pod_choice_body), void);
2843 else
2844 self->curr.value = SPA_MEMBER (self->curr.value, pod_choice->body.child.size, void);
2845
2846 if (self->curr.value >= SPA_MEMBER(&pod_choice->body, SPA_POD_BODY_SIZE (pod_choice), void))
2847 return FALSE;
2848
2849 if (item) {
2850 g_value_init (item, G_TYPE_POINTER);
2851 g_value_set_pointer (item, self->curr.value);
2852 }
2853 return TRUE;
2854 }
2855
2856 static gboolean
2857 wp_spa_pod_iterator_next_array (WpSpaPodIterator *self, GValue *item)
2858 {
2859 const struct spa_pod_array *pod_arr =
2860 (const struct spa_pod_array *) self->pod->pod;
2861
2862 if (!self->curr.value)
2863 self->curr.value = SPA_MEMBER (&pod_arr->body, sizeof(struct spa_pod_array_body), void);
2864 else
2865 self->curr.value = SPA_MEMBER (self->curr.value, pod_arr->body.child.size, void);
2866
2867 if (self->curr.value >= SPA_MEMBER(&pod_arr->body, SPA_POD_BODY_SIZE (pod_arr), void))
2868 return FALSE;
2869
2870 if (item) {
2871 g_value_init (item, G_TYPE_POINTER);
2872 g_value_set_pointer (item, self->curr.value);
2873 }
2874 return TRUE;
2875 }
2876
2877 static gboolean
2878 wp_spa_pod_iterator_next_object (WpSpaPodIterator *self, GValue *item)
2879 {
2880 const struct spa_pod_object *pod_obj =
2881 (const struct spa_pod_object *) self->pod->pod;
2882
2883 if (!self->curr.prop)
2884 self->curr.prop = spa_pod_prop_first (&pod_obj->body);
2885 else
2886 self->curr.prop = spa_pod_prop_next (self->curr.prop);
2887
2888 if (!spa_pod_prop_is_inside (&pod_obj->body, SPA_POD_BODY_SIZE (pod_obj),
2889 self->curr.prop))
2890 return FALSE;
2891
2892 if (item) {
2893 g_value_init (item, WP_TYPE_SPA_POD);
2894 g_value_take_boxed (item, wp_spa_pod_new_property_wrap (
2895 self->pod->static_pod.data_property.table, self->curr.prop->key,
2896 self->curr.prop->flags, &self->curr.prop->value));
2897 }
2898 return TRUE;
2899 }
2900
2901 static gboolean
2902 wp_spa_pod_iterator_next_struct (WpSpaPodIterator *self, GValue *item)
2903 {
2904 if (!self->curr.pod)
2905 self->curr.pod = SPA_POD_BODY (self->pod->pod);
2906 else
2907 self->curr.pod = spa_pod_next (self->curr.pod);
2908
2909 if (!spa_pod_is_inside (SPA_POD_BODY (self->pod->pod),
2910 SPA_POD_BODY_SIZE (self->pod->pod), self->curr.pod))
2911 return FALSE;
2912
2913 if (item) {
2914 g_value_init (item, WP_TYPE_SPA_POD);
2915 g_value_take_boxed (item, wp_spa_pod_new_wrap (self->curr.pod));
2916 }
2917 return TRUE;
2918 }
2919
2920 static gboolean
2921 wp_spa_pod_iterator_next_sequence (WpSpaPodIterator *self, GValue *item)
2922 {
2923 const struct spa_pod_sequence *pod_seq =
2924 (const struct spa_pod_sequence *) self->pod->pod;
2925
2926 if (!self->curr.control)
2927 self->curr.control = spa_pod_control_first (&pod_seq->body);
2928 else
2929 self->curr.control = spa_pod_control_next (self->curr.control);
2930
2931 if (!spa_pod_control_is_inside (&pod_seq->body, SPA_POD_BODY_SIZE (pod_seq),
2932 self->curr.control))
2933 return FALSE;
2934
2935 if (item) {
2936 g_value_init (item, WP_TYPE_SPA_POD);
2937 g_value_take_boxed (item, wp_spa_pod_new_control_wrap (
2938 self->curr.control->offset, self->curr.control->type,
2939 &self->curr.control->value));
2940 }
2941 return TRUE;
2942 }
2943
2944
2945 static void
2946 wp_spa_pod_iterator_reset (WpIterator *iterator)
2947 {
2948 WpSpaPodIterator *self = wp_iterator_get_user_data (iterator);
2949 self->curr.value = NULL;
2950 self->curr.pod = NULL;
2951 self->curr.prop = NULL;
2952 self->curr.control = NULL;
2953 }
2954
2955 static gboolean
2956 wp_spa_pod_iterator_next (WpIterator *iterator, GValue *item)
2957 {
2958 WpSpaPodIterator *self = wp_iterator_get_user_data (iterator);
2959
2960 switch (self->pod->pod->type) {
2961 case SPA_TYPE_Choice:
2962 return wp_spa_pod_iterator_next_choice (self, item);
2963 case SPA_TYPE_Array:
2964 return wp_spa_pod_iterator_next_array (self, item);
2965 case SPA_TYPE_Object:
2966 return wp_spa_pod_iterator_next_object (self, item);
2967 case SPA_TYPE_Struct:
2968 return wp_spa_pod_iterator_next_struct (self, item);
2969 case SPA_TYPE_Sequence:
2970 return wp_spa_pod_iterator_next_sequence (self, item);
2971 default:
2972 break;
2973 }
2974
2975 return FALSE;
2976 }
2977
2978 static gboolean
2979 wp_spa_pod_iterator_fold (WpIterator *iterator, WpIteratorFoldFunc func,
2980 GValue *ret, gpointer data)
2981 {
2982 WpSpaPodIterator *self = wp_iterator_get_user_data (iterator);
2983
2984 wp_iterator_reset (iterator);
2985
2986 switch (self->pod->pod->type) {
2987 case SPA_TYPE_Choice:
2988 {
2989 const struct spa_pod_choice *pod_choice =
2990 (const struct spa_pod_choice *) self->pod->pod;
2991 gpointer p = NULL;
2992 SPA_POD_CHOICE_FOREACH (pod_choice, p) {
2993 GValue v = G_VALUE_INIT;
2994 g_value_init (&v, G_TYPE_POINTER);
2995 g_value_set_pointer (&v, p);
2996 const gboolean res = func (&v, ret, data);
2997 g_value_unset (&v);
2998 if (!res)
2999 return FALSE;
3000 }
3001 break;
3002 }
3003 case SPA_TYPE_Array:
3004 {
3005 const struct spa_pod_array *pod_arr =
3006 (const struct spa_pod_array *) self->pod->pod;
3007 gpointer p = NULL;
3008 SPA_POD_ARRAY_FOREACH (pod_arr, p) {
3009 GValue v = G_VALUE_INIT;
3010 g_value_init (&v, G_TYPE_POINTER);
3011 g_value_set_pointer (&v, p);
3012 const gboolean res = func (&v, ret, data);
3013 g_value_unset (&v);
3014 if (!res)
3015 return FALSE;
3016 }
3017 break;
3018 }
3019 case SPA_TYPE_Object:
3020 {
3021 const struct spa_pod_object *pod_obj =
3022 (const struct spa_pod_object *) self->pod->pod;
3023 struct spa_pod_prop *p = NULL;
3024 SPA_POD_OBJECT_FOREACH (pod_obj, p) {
3025 GValue v = G_VALUE_INIT;
3026 g_value_init (&v, WP_TYPE_SPA_POD);
3027 g_value_take_boxed (&v, wp_spa_pod_new_property_wrap (
3028 self->pod->static_pod.data_property.table, p->key, p->flags,
3029 &p->value));
3030 const gboolean res = func (&v, ret, data);
3031 g_value_unset (&v);
3032 if (!res)
3033 return FALSE;
3034 }
3035 break;
3036 }
3037 case SPA_TYPE_Struct:
3038 {
3039 struct spa_pod *p = NULL;
3040 SPA_POD_STRUCT_FOREACH (self->pod->pod, p) {
3041 GValue v = G_VALUE_INIT;
3042 g_value_init (&v, WP_TYPE_SPA_POD);
3043 g_value_take_boxed (&v, wp_spa_pod_new_wrap (p));
3044 const gboolean res = func (&v, ret, data);
3045 g_value_unset (&v);
3046 if (!res)
3047 return FALSE;
3048 }
3049 break;
3050 }
3051 case SPA_TYPE_Sequence:
3052 {
3053 const struct spa_pod_sequence *pod_seq =
3054 (const struct spa_pod_sequence *) self->pod->pod;
3055 struct spa_pod_control *p = NULL;
3056 SPA_POD_SEQUENCE_FOREACH (pod_seq, p) {
3057 GValue v = G_VALUE_INIT;
3058 g_value_init (&v, WP_TYPE_SPA_POD);
3059 g_value_take_boxed (&v, wp_spa_pod_new_control_wrap (p->offset, p->type,
3060 &p->value));
3061 const gboolean res = func (&v, ret, data);
3062 g_value_unset (&v);
3063 if (!res)
3064 return FALSE;
3065 }
3066 break;
3067 }
3068 default:
3069 return FALSE;
3070 }
3071
3072 return TRUE;
3073 }
3074
3075 static void
3076 wp_spa_pod_iterator_finalize (WpIterator *iterator)
3077 {
3078 WpSpaPodIterator *self = wp_iterator_get_user_data (iterator);
3079 g_clear_pointer (&self->pod, wp_spa_pod_unref);
3080 }
3081
3082 /*!
3083 * \brief Creates a new iterator for a spa pod object.
3084 *
3085 * \ingroup wpspapod
3086 * \param pod a spa pod object
3087 * \returns (transfer full): the new spa pod iterator
3088 */
3089 WpIterator *
3090 wp_spa_pod_new_iterator (WpSpaPod *pod)
3091 {
3092 static const WpIteratorMethods methods = {
3093 .version = WP_ITERATOR_METHODS_VERSION,
3094 .reset = wp_spa_pod_iterator_reset,
3095 .next = wp_spa_pod_iterator_next,
3096 .fold = wp_spa_pod_iterator_fold,
3097 .foreach = NULL,
3098 .finalize = wp_spa_pod_iterator_finalize
3099 };
3100 WpIterator *it = wp_iterator_new (&methods, sizeof (WpSpaPodIterator));
3101 WpSpaPodIterator *self = wp_iterator_get_user_data (it);
3102
3103 self->pod = wp_spa_pod_ref (pod);
3104
3105 return it;
3106 }