| 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-type.h" |
| 10 |
#include "log.h" |
| 11 |
|
| 12 |
#include <spa/utils/type-info.h> |
| 13 |
#include <spa/debug/types.h> |
| 14 |
#include <pipewire/pipewire.h> |
| 15 |
|
| 16 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-spa-type") |
| 17 |
|
| 18 |
/*! \defgroup wpspatype WpSpaType |
| 19 |
* |
| 20 |
* Spa has a type system that is represented by a set of arrays that contain |
| 21 |
* `spa_type_info` structures. This type system is simple, yet complex to |
| 22 |
* work with for a couple of reasons. |
| 23 |
* |
| 24 |
* WirePlumber uses this API to access the spa type system, which makes some |
| 25 |
* things easier to understand and work with. The main benefit of using this |
| 26 |
* API is that it makes it easy to work with string representations of the |
| 27 |
* types, allowing easier access from script bindings. |
| 28 |
* |
| 29 |
* \b Type \b hierarchy |
| 30 |
* |
| 31 |
* On the top level, there is a list of types like Int, Bool, String, Id, Object. |
| 32 |
* These are called fundamental types (terms borrowed from GType). |
| 33 |
* Fundamental types can be derived and therefore we can have other types |
| 34 |
* that represent specific objects, for instance. |
| 35 |
* |
| 36 |
* Enum and flag types are all represented with `SPA_TYPE_Id`. These types |
| 37 |
* may have a list of possible values that one can select from (enums) |
| 38 |
* or combine (flags). These values are accessed with the WpSpaIdTable API. |
| 39 |
* |
| 40 |
* Object types can have fields. All objects always have a special "id" field, |
| 41 |
* which is an enum. Its possible values can be given by |
| 42 |
* wp_spa_type_get_object_id_values_table(). Optionally, objects can also have |
| 43 |
* other object-specific fields, which can be accessed with |
| 44 |
* wp_spa_type_get_values_table(). |
| 45 |
* |
| 46 |
* Every object field or enum value is represented by a WpSpaIdValue. In the |
| 47 |
* case of object fields, each field can be of a specific type, which is |
| 48 |
* returned by wp_spa_id_value_get_value_type(). |
| 49 |
*/ |
| 50 |
|
| 51 |
static const WpSpaType SPA_TYPE_VENDOR_WirePlumber = 0x03000000; |
| 52 |
static GArray *extra_types = NULL; |
| 53 |
static GArray *extra_id_tables = NULL; |
| 54 |
|
| 55 |
typedef struct { |
| 56 |
const char *name; |
| 57 |
const struct spa_type_info *values; |
| 58 |
} WpSpaIdTableInfo; |
| 59 |
|
| 60 |
static const WpSpaIdTableInfo static_id_tables[] = { |
| 61 |
{ SPA_TYPE_INFO_Choice, spa_type_choice }, |
| 62 |
{ SPA_TYPE_INFO_Direction, spa_type_direction }, |
| 63 |
{ SPA_TYPE_INFO_ParamId, spa_type_param }, |
| 64 |
{ SPA_TYPE_INFO_MediaType, spa_type_media_type }, |
| 65 |
{ SPA_TYPE_INFO_MediaSubtype, spa_type_media_subtype }, |
| 66 |
{ SPA_TYPE_INFO_ParamAvailability, spa_type_param_availability }, |
| 67 |
{ SPA_TYPE_INFO_ParamPortConfigMode, spa_type_param_port_config_mode }, |
| 68 |
{ SPA_TYPE_INFO_VideoFormat, spa_type_video_format }, |
| 69 |
{ SPA_TYPE_INFO_AudioFormat, spa_type_audio_format }, |
| 70 |
{ SPA_TYPE_INFO_AudioFlags, spa_type_audio_flags }, |
| 71 |
{ SPA_TYPE_INFO_AudioChannel, spa_type_audio_channel }, |
| 72 |
{ SPA_TYPE_INFO_AudioIEC958Codec, spa_type_audio_iec958_codec }, |
| 73 |
{ SPA_TYPE_INFO_IO, spa_type_io }, |
| 74 |
{ SPA_TYPE_INFO_Control, spa_type_control }, |
| 75 |
{ SPA_TYPE_INFO_Data, spa_type_data_type }, |
| 76 |
{ SPA_TYPE_INFO_Meta, spa_type_meta_type }, |
| 77 |
{ SPA_TYPE_INFO_DeviceEventId, spa_type_device_event_id }, |
| 78 |
{ SPA_TYPE_INFO_NodeEvent, spa_type_node_event_id }, |
| 79 |
{ SPA_TYPE_INFO_NodeCommand, spa_type_node_command_id }, |
| 80 |
{ NULL, NULL } |
| 81 |
}; |
| 82 |
|
| 83 |
GType wp_spa_type_get_type (void) |
| 84 |
{ |
| 85 |
static gsize id__volatile = 0; |
| 86 |
if (g_once_init_enter (&id__volatile)) { |
| 87 |
GType id = g_type_register_static_simple ( |
| 88 |
G_TYPE_UINT, g_intern_static_string ("WpSpaType"), |
| 89 |
0, NULL, 0, NULL, 0); |
| 90 |
g_once_init_leave (&id__volatile, id); |
| 91 |
} |
| 92 |
return id__volatile; |
| 93 |
} |
| 94 |
|
| 95 |
G_DEFINE_POINTER_TYPE (WpSpaIdTable, wp_spa_id_table) |
| 96 |
|
| 97 |
G_DEFINE_POINTER_TYPE (WpSpaIdValue, wp_spa_id_value) |
| 98 |
|
| 99 |
|
| 100 |
static const struct spa_type_info * |
| 101 |
wp_spa_type_info_find_by_type (WpSpaType type) |
| 102 |
{ |
| 103 |
const struct spa_type_info *info; |
| 104 |
|
| 105 |
g_return_val_if_fail (type != WP_SPA_TYPE_INVALID, NULL); |
| 106 |
g_return_val_if_fail (type != 0, NULL); |
| 107 |
|
| 108 |
if (extra_types) |
| 109 |
info = spa_debug_type_find ( |
| 110 |
(const struct spa_type_info *) extra_types->data, type); |
| 111 |
else |
| 112 |
info = spa_debug_type_find (SPA_TYPE_ROOT, type); |
| 113 |
|
| 114 |
return info; |
| 115 |
} |
| 116 |
|
| 117 |
/* similar to spa_debug_type_find() and unlike spa_debug_type_find_type(), |
| 118 |
which steps into id values / object fields */ |
| 119 |
static const struct spa_type_info * |
| 120 |
_spa_type_find_by_name (const struct spa_type_info * info, const char * name) |
| 121 |
{ |
| 122 |
const struct spa_type_info * res; |
| 123 |
|
| 124 |
while (info->name) { |
| 125 |
if (info->type == SPA_ID_INVALID) { |
| 126 |
if (info->values && (res = _spa_type_find_by_name (info->values, name))) |
| 127 |
return res; |
| 128 |
} |
| 129 |
if (strcmp (info->name, name) == 0) |
| 130 |
return info; |
| 131 |
info++; |
| 132 |
} |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
static const struct spa_type_info * |
| 137 |
wp_spa_type_info_find_by_name (const gchar *name) |
| 138 |
{ |
| 139 |
const struct spa_type_info *info = NULL; |
| 140 |
|
| 141 |
g_return_val_if_fail (name != NULL, NULL); |
| 142 |
|
| 143 |
if (extra_types) |
| 144 |
info = _spa_type_find_by_name ( |
| 145 |
(const struct spa_type_info *) extra_types->data, name); |
| 146 |
else |
| 147 |
info = _spa_type_find_by_name (SPA_TYPE_ROOT, name); |
| 148 |
|
| 149 |
return info; |
| 150 |
} |
| 151 |
|
| 152 |
/*! |
| 153 |
* \brief Looks up the type id from a given type name |
| 154 |
* |
| 155 |
* \ingroup wpspatype |
| 156 |
* \param name the name to look up |
| 157 |
* \returns (transfer none): the corresponding type id or WP_SPA_TYPE_INVALID |
| 158 |
* if not found |
| 159 |
*/ |
| 160 |
WpSpaType |
| 161 |
wp_spa_type_from_name (const gchar *name) |
| 162 |
{ |
| 163 |
const struct spa_type_info *info = wp_spa_type_info_find_by_name (name); |
| 164 |
return info ? info->type : WP_SPA_TYPE_INVALID; |
| 165 |
} |
| 166 |
|
| 167 |
/*! |
| 168 |
* \brief Gets the parent type of an SPA type |
| 169 |
* |
| 170 |
* \ingroup wpspatype |
| 171 |
* \param type a type id |
| 172 |
* \returns (transfer none): the direct parent type of the given \a type; if the |
| 173 |
* type is fundamental (i.e. has no parent), the returned type is the same |
| 174 |
* as \a type |
| 175 |
*/ |
| 176 |
WpSpaType |
| 177 |
wp_spa_type_parent (WpSpaType type) |
| 178 |
{ |
| 179 |
const struct spa_type_info *info = wp_spa_type_info_find_by_type (type); |
| 180 |
return info ? info->parent : WP_SPA_TYPE_INVALID; |
| 181 |
} |
| 182 |
|
| 183 |
/*! |
| 184 |
* \brief Gets the name of an SPA type |
| 185 |
* |
| 186 |
* \ingroup wpspatype |
| 187 |
* \param type a type id |
| 188 |
* \returns the complete name of the given \a type or NULL if \a type is invalid |
| 189 |
*/ |
| 190 |
const gchar * |
| 191 |
wp_spa_type_name (WpSpaType type) |
| 192 |
{ |
| 193 |
const struct spa_type_info *info = wp_spa_type_info_find_by_type (type); |
| 194 |
return info ? info->name : NULL; |
| 195 |
} |
| 196 |
|
| 197 |
/*! |
| 198 |
* \brief Checks if an SPA type is a fundamental type |
| 199 |
* |
| 200 |
* \ingroup wpspatype |
| 201 |
* \param type a type id |
| 202 |
* \returns TRUE if the \a type has no parent, FALSE otherwise |
| 203 |
*/ |
| 204 |
gboolean |
| 205 |
wp_spa_type_is_fundamental (WpSpaType type) |
| 206 |
{ |
| 207 |
const struct spa_type_info *info = wp_spa_type_info_find_by_type (type); |
| 208 |
return info ? (info->type == info->parent) : FALSE; |
| 209 |
} |
| 210 |
|
| 211 |
/*! |
| 212 |
* \brief Checks if an SPA type is an Id type |
| 213 |
* |
| 214 |
* \ingroup wpspatype |
| 215 |
* \param type a type id |
| 216 |
* \returns TRUE if the \a type is a SPA_TYPE_Id, FALSE otherwise |
| 217 |
*/ |
| 218 |
gboolean |
| 219 |
wp_spa_type_is_id (WpSpaType type) |
| 220 |
{ |
| 221 |
const struct spa_type_info *info = wp_spa_type_info_find_by_type (type); |
| 222 |
return info ? (info->parent == SPA_TYPE_Id) : FALSE; |
| 223 |
} |
| 224 |
|
| 225 |
/*! |
| 226 |
* \brief Checks if an SPA type is an Object type |
| 227 |
* |
| 228 |
* \ingroup wpspatype |
| 229 |
* \param type a type id |
| 230 |
* \returns TRUE if the \a type is a SPA_TYPE_Object, FALSE otherwise |
| 231 |
*/ |
| 232 |
gboolean |
| 233 |
wp_spa_type_is_object (WpSpaType type) |
| 234 |
{ |
| 235 |
const struct spa_type_info *info = wp_spa_type_info_find_by_type (type); |
| 236 |
return info ? (info->parent == SPA_TYPE_Object) : FALSE; |
| 237 |
} |
| 238 |
|
| 239 |
/*! |
| 240 |
* \brief Gets the table with the values that can be stored in the special "id" |
| 241 |
* field of an object of the given \a type |
| 242 |
* |
| 243 |
* Object pods (see WpSpaPod) always have a special "id" field along with |
| 244 |
* other fields that can be defined. This "id" field can only store values |
| 245 |
* of a specific `SPA_TYPE_Id` type. This function returns the table that |
| 246 |
* contains the possible values for that field. |
| 247 |
* |
| 248 |
* \ingroup wpspatype |
| 249 |
* \param type the type id of an object type |
| 250 |
* \returns the table with the values that can be stored in the special "id" |
| 251 |
* field of an object of the given \a type |
| 252 |
*/ |
| 253 |
WpSpaIdTable |
| 254 |
wp_spa_type_get_object_id_values_table (WpSpaType type) |
| 255 |
{ |
| 256 |
const struct spa_type_info *info = wp_spa_type_info_find_by_type (type); |
| 257 |
|
| 258 |
g_return_val_if_fail (info != NULL, NULL); |
| 259 |
g_return_val_if_fail (info->parent == SPA_TYPE_Object, NULL); |
| 260 |
g_return_val_if_fail (info->values != NULL, NULL); |
| 261 |
g_return_val_if_fail (info->values->name != NULL, NULL); |
| 262 |
g_return_val_if_fail (info->values->parent == SPA_TYPE_Id, NULL); |
| 263 |
|
| 264 |
return info->values->values; |
| 265 |
} |
| 266 |
|
| 267 |
/*! |
| 268 |
* \brief Gets the values table of an SPA type |
| 269 |
* |
| 270 |
* \ingroup wpspatype |
| 271 |
* \param type a type id |
| 272 |
* \returns the associated WpSpaIdTable that contains possible |
| 273 |
* values or object fields for this type, or NULL |
| 274 |
*/ |
| 275 |
WpSpaIdTable |
| 276 |
wp_spa_type_get_values_table (WpSpaType type) |
| 277 |
{ |
| 278 |
const struct spa_type_info *info = wp_spa_type_info_find_by_type (type); |
| 279 |
|
| 280 |
g_return_val_if_fail (info != NULL, NULL); |
| 281 |
return info->values; |
| 282 |
} |
| 283 |
|
| 284 |
struct spa_type_info_iterator_data |
| 285 |
{ |
| 286 |
const struct spa_type_info *base; |
| 287 |
const struct spa_type_info *cur; |
| 288 |
}; |
| 289 |
|
| 290 |
static void |
| 291 |
spa_type_info_iterator_reset (WpIterator *it) |
| 292 |
{ |
| 293 |
struct spa_type_info_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 294 |
it_data->cur = it_data->base; |
| 295 |
} |
| 296 |
|
| 297 |
static gboolean |
| 298 |
spa_type_info_iterator_next (WpIterator *it, GValue *item) |
| 299 |
{ |
| 300 |
struct spa_type_info_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 301 |
|
| 302 |
if (it_data->cur->name) { |
| 303 |
g_value_init (item, WP_TYPE_SPA_ID_VALUE); |
| 304 |
g_value_set_pointer (item, (gpointer) it_data->cur); |
| 305 |
it_data->cur++; |
| 306 |
return TRUE; |
| 307 |
} |
| 308 |
return FALSE; |
| 309 |
} |
| 310 |
|
| 311 |
static gboolean |
| 312 |
spa_type_info_iterator_fold (WpIterator *it, WpIteratorFoldFunc func, |
| 313 |
GValue *ret, gpointer data) |
| 314 |
{ |
| 315 |
struct spa_type_info_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 316 |
const struct spa_type_info *cur, *base; |
| 317 |
|
| 318 |
cur = base = it_data->base; |
| 319 |
|
| 320 |
while (cur->name) { |
| 321 |
g_auto (GValue) item = G_VALUE_INIT; |
| 322 |
g_value_init (&item, WP_TYPE_SPA_ID_VALUE); |
| 323 |
g_value_set_pointer (&item, (gpointer) cur); |
| 324 |
if (!func (&item, ret, data)) |
| 325 |
return FALSE; |
| 326 |
cur++; |
| 327 |
} |
| 328 |
return TRUE; |
| 329 |
} |
| 330 |
|
| 331 |
static const WpIteratorMethods spa_type_info_iterator_methods = { |
| 332 |
.version = WP_ITERATOR_METHODS_VERSION, |
| 333 |
.reset = spa_type_info_iterator_reset, |
| 334 |
.next = spa_type_info_iterator_next, |
| 335 |
.fold = spa_type_info_iterator_fold, |
| 336 |
}; |
| 337 |
|
| 338 |
/*! |
| 339 |
* \brief Finds a WpSpaIdTable given its name. |
| 340 |
* |
| 341 |
* This name can either be the full type name of an object type, |
| 342 |
* or the name of an enum (which is \b not(!!) a type). |
| 343 |
* For example, "Spa:Pod:Object:Param:Format" and "Spa:Enum:ParamId" are |
| 344 |
* both valid table names. |
| 345 |
* |
| 346 |
* \ingroup wpspatype |
| 347 |
* \param name the full name of an id table |
| 348 |
* \returns (nullable): the associated table, or NULL |
| 349 |
*/ |
| 350 |
WpSpaIdTable |
| 351 |
wp_spa_id_table_from_name (const gchar *name) |
| 352 |
{ |
| 353 |
g_return_val_if_fail (name != NULL, NULL); |
| 354 |
const WpSpaIdTableInfo *info = NULL; |
| 355 |
|
| 356 |
/* first look in dynamic id tables */ |
| 357 |
if (extra_id_tables) { |
| 358 |
info = (const WpSpaIdTableInfo *) extra_id_tables->data; |
| 359 |
while (info && info->name) { |
| 360 |
if (strcmp (info->name, name) == 0) |
| 361 |
return info->values; |
| 362 |
info++; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
/* then look at the well-known static ones */ |
| 367 |
info = static_id_tables; |
| 368 |
while (info && info->name) { |
| 369 |
if (strcmp (info->name, name) == 0) |
| 370 |
return info->values; |
| 371 |
info++; |
| 372 |
} |
| 373 |
|
| 374 |
/* then look into types, hoping to find an object type */ |
| 375 |
const struct spa_type_info *tinfo = wp_spa_type_info_find_by_name (name); |
| 376 |
return tinfo ? tinfo->values : NULL; |
| 377 |
} |
| 378 |
|
| 379 |
/*! |
| 380 |
* \brief This function returns an iterator that allows you to iterate |
| 381 |
* through the values associated with this table. |
| 382 |
* |
| 383 |
* The items in the iterator are of type WpSpaIdValue. |
| 384 |
* |
| 385 |
* \ingroup wpspatype |
| 386 |
* \param table the id table |
| 387 |
* \returns a WpIterator that iterates over WpSpaIdValue items |
| 388 |
*/ |
| 389 |
WpIterator * |
| 390 |
wp_spa_id_table_new_iterator (WpSpaIdTable table) |
| 391 |
{ |
| 392 |
g_return_val_if_fail (table != NULL, NULL); |
| 393 |
|
| 394 |
WpIterator *it = wp_iterator_new (&spa_type_info_iterator_methods, |
| 395 |
sizeof (struct spa_type_info_iterator_data)); |
| 396 |
struct spa_type_info_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 397 |
it_data->base = (const struct spa_type_info *) table; |
| 398 |
it_data->cur = it_data->base; |
| 399 |
return it; |
| 400 |
} |
| 401 |
|
| 402 |
/*! |
| 403 |
* \brief Finds a value in an SPA Id table |
| 404 |
* |
| 405 |
* \ingroup wpspatype |
| 406 |
* \param table the id table |
| 407 |
* \param value a numeric value that is contained in the table |
| 408 |
* \returns (nullable): the WpSpaIdValue associated with \a value, or NULL |
| 409 |
*/ |
| 410 |
WpSpaIdValue |
| 411 |
wp_spa_id_table_find_value (WpSpaIdTable table, guint value) |
| 412 |
{ |
| 413 |
g_return_val_if_fail (table != NULL, NULL); |
| 414 |
|
| 415 |
const struct spa_type_info *info = table; |
| 416 |
while (info && info->name) { |
| 417 |
if (info->type == value) |
| 418 |
return info; |
| 419 |
info++; |
| 420 |
} |
| 421 |
return NULL; |
| 422 |
} |
| 423 |
|
| 424 |
/*! |
| 425 |
* \brief Finds a named value in an SPA Id table |
| 426 |
* |
| 427 |
* \ingroup wpspatype |
| 428 |
* \param table the id table |
| 429 |
* \param name the full name of a value that is contained in the table |
| 430 |
* \returns (nullable): the WpSpaIdValue associated with \a name, or NULL |
| 431 |
*/ |
| 432 |
WpSpaIdValue |
| 433 |
wp_spa_id_table_find_value_from_name (WpSpaIdTable table, const gchar * name) |
| 434 |
{ |
| 435 |
g_return_val_if_fail (table != NULL, NULL); |
| 436 |
|
| 437 |
const struct spa_type_info *info = table; |
| 438 |
while (info && info->name) { |
| 439 |
if (!strcmp (info->name, name)) |
| 440 |
return info; |
| 441 |
info++; |
| 442 |
} |
| 443 |
return NULL; |
| 444 |
} |
| 445 |
|
| 446 |
/*! |
| 447 |
* \brief Finds a short named value in an SPA Id table |
| 448 |
* |
| 449 |
* \ingroup wpspatype |
| 450 |
* \param table the id table |
| 451 |
* \param short_name the short name of a value that is contained in the table |
| 452 |
* \returns (nullable): the WpSpaIdValue associated with \a short_name, or NULL |
| 453 |
*/ |
| 454 |
WpSpaIdValue |
| 455 |
wp_spa_id_table_find_value_from_short_name (WpSpaIdTable table, |
| 456 |
const gchar * short_name) |
| 457 |
{ |
| 458 |
g_return_val_if_fail (table != NULL, NULL); |
| 459 |
|
| 460 |
const struct spa_type_info *info = table; |
| 461 |
while (info && info->name) { |
| 462 |
if (!strcmp (spa_debug_type_short_name (info->name), short_name)) |
| 463 |
return info; |
| 464 |
info++; |
| 465 |
} |
| 466 |
return NULL; |
| 467 |
} |
| 468 |
|
| 469 |
static WpSpaIdTable |
| 470 |
wp_spa_id_name_find_id_table (const gchar * name) |
| 471 |
{ |
| 472 |
WpSpaIdTable table = NULL; |
| 473 |
g_autofree gchar *parent_name = g_strdup (name); |
| 474 |
gchar *h; |
| 475 |
|
| 476 |
if ((h = strrchr(parent_name, ':')) != NULL) { |
| 477 |
/* chop the enum name to get the type, ex: |
| 478 |
Spa:Enum:Direction:Input -> Spa:Enum:Direction */ |
| 479 |
*h = '\0'; |
| 480 |
table = wp_spa_id_table_from_name (parent_name); |
| 481 |
|
| 482 |
/* in some cases, the parent name is one layer further up, ex: |
| 483 |
Spa:Pod:Object:Param:Format:Audio:rate -> Spa:Pod:Object:Param:Format */ |
| 484 |
if (!table && (h = strrchr(parent_name, ':')) != NULL) { |
| 485 |
*h = '\0'; |
| 486 |
table = wp_spa_id_table_from_name (parent_name); |
| 487 |
} |
| 488 |
} |
| 489 |
return table; |
| 490 |
} |
| 491 |
|
| 492 |
/*! |
| 493 |
* \brief Looks up an id value (enum, flag or object field) directly from its |
| 494 |
* full name. |
| 495 |
* |
| 496 |
* For instance, "Spa:Enum:Direction:Input" will resolve to the |
| 497 |
* id value that represents "Input" in the "Spa:Enum:Direction" enum. |
| 498 |
* |
| 499 |
* \ingroup wpspatype |
| 500 |
* \param name the full name of an id value |
| 501 |
* \returns the id value for \a name, or NULL if no such id value was found |
| 502 |
*/ |
| 503 |
WpSpaIdValue |
| 504 |
wp_spa_id_value_from_name (const gchar * name) |
| 505 |
{ |
| 506 |
g_return_val_if_fail (name != NULL, NULL); |
| 507 |
|
| 508 |
WpSpaIdTable table = wp_spa_id_name_find_id_table (name); |
| 509 |
return wp_spa_id_table_find_value_from_name (table, name); |
| 510 |
} |
| 511 |
|
| 512 |
/*! |
| 513 |
* \brief Looks up an id value given its container \a table_name and its |
| 514 |
* \a short_name |
| 515 |
* |
| 516 |
* \ingroup wpspatype |
| 517 |
* \param table_name the name of the WpSpaIdTable to look up the value in |
| 518 |
* \param short_name the short name of the value to look up |
| 519 |
* \returns the id value or NULL if it was not found |
| 520 |
*/ |
| 521 |
WpSpaIdValue |
| 522 |
wp_spa_id_value_from_short_name (const gchar * table_name, |
| 523 |
const gchar * short_name) |
| 524 |
{ |
| 525 |
g_return_val_if_fail (table_name != NULL, NULL); |
| 526 |
g_return_val_if_fail (short_name != NULL, NULL); |
| 527 |
|
| 528 |
WpSpaIdTable table = wp_spa_id_table_from_name (table_name); |
| 529 |
return wp_spa_id_table_find_value_from_short_name (table, short_name); |
| 530 |
} |
| 531 |
|
| 532 |
/*! |
| 533 |
* \brief Looks up an id value given its container \a table_name and its numeric |
| 534 |
* representation, \a id |
| 535 |
* |
| 536 |
* \ingroup wpspatype |
| 537 |
* \param table_name the name of the WpSpaIdTable to look up the value in |
| 538 |
* \param id the numeric representation of the value to look up |
| 539 |
* \returns the id value or NULL if it was not found |
| 540 |
*/ |
| 541 |
WpSpaIdValue |
| 542 |
wp_spa_id_value_from_number (const gchar * table_name, guint id) |
| 543 |
{ |
| 544 |
g_return_val_if_fail (table_name != NULL, NULL); |
| 545 |
|
| 546 |
WpSpaIdTable table = wp_spa_id_table_from_name (table_name); |
| 547 |
return wp_spa_id_table_find_value (table, id); |
| 548 |
} |
| 549 |
|
| 550 |
/*! |
| 551 |
* \brief Gets the numeric value of an id value |
| 552 |
* |
| 553 |
* \ingroup wpspatype |
| 554 |
* \param id an id value |
| 555 |
* \returns the numeric representation of this id value |
| 556 |
*/ |
| 557 |
guint |
| 558 |
wp_spa_id_value_number (WpSpaIdValue id) |
| 559 |
{ |
| 560 |
g_return_val_if_fail (id != NULL, -1); |
| 561 |
|
| 562 |
const struct spa_type_info *info = id; |
| 563 |
return info->type; |
| 564 |
} |
| 565 |
|
| 566 |
/*! |
| 567 |
* \brief Gets the name of an id value |
| 568 |
* |
| 569 |
* \ingroup wpspatype |
| 570 |
* \param id an id value |
| 571 |
* \returns the full name of this id value |
| 572 |
*/ |
| 573 |
const gchar * |
| 574 |
wp_spa_id_value_name (WpSpaIdValue id) |
| 575 |
{ |
| 576 |
g_return_val_if_fail (id != NULL, NULL); |
| 577 |
|
| 578 |
const struct spa_type_info *info = id; |
| 579 |
return info->name; |
| 580 |
} |
| 581 |
|
| 582 |
/*! |
| 583 |
* \brief Gets the short name of an id value |
| 584 |
* |
| 585 |
* \ingroup wpspatype |
| 586 |
* \param id an id value |
| 587 |
* \returns the short name of this id value |
| 588 |
*/ |
| 589 |
const gchar * |
| 590 |
wp_spa_id_value_short_name (WpSpaIdValue id) |
| 591 |
{ |
| 592 |
g_return_val_if_fail (id != NULL, NULL); |
| 593 |
|
| 594 |
const struct spa_type_info *info = id; |
| 595 |
return spa_debug_type_short_name (info->name); |
| 596 |
} |
| 597 |
|
| 598 |
/*! |
| 599 |
* \brief Returns the value type associated with this WpSpaIdValue. |
| 600 |
* |
| 601 |
* This information is useful when \a id represents an object field, |
| 602 |
* which can take a value of an arbitrary type. |
| 603 |
* |
| 604 |
* When the returned type is (or is derived from) `SPA_TYPE_Id` or |
| 605 |
* `SPA_TYPE_Object`, \a table is set to point to the WpSpaIdTable |
| 606 |
* that contains the possible Id values / object fields. |
| 607 |
* |
| 608 |
* \ingroup wpspatype |
| 609 |
* \param id an id value |
| 610 |
* \param table (out) (optional): the associated WpSpaIdTable |
| 611 |
* \returns (transfer none): the value type associated with \a id |
| 612 |
*/ |
| 613 |
WpSpaType |
| 614 |
wp_spa_id_value_get_value_type (WpSpaIdValue id, WpSpaIdTable * table) |
| 615 |
{ |
| 616 |
g_return_val_if_fail (id != NULL, WP_SPA_TYPE_INVALID); |
| 617 |
|
| 618 |
const struct spa_type_info *info = id; |
| 619 |
|
| 620 |
if (table) { |
| 621 |
/* info->values has different semantics on Array types */ |
| 622 |
if (info->values && info->parent != SPA_TYPE_Array) { |
| 623 |
*table = info->values; |
| 624 |
} |
| 625 |
/* derived object types normally don't have info->values directly set, |
| 626 |
so we need to look them up */ |
| 627 |
else if (wp_spa_type_is_object (info->parent)) { |
| 628 |
WpSpaIdTable t = wp_spa_type_get_values_table (info->parent); |
| 629 |
if (t) *table = t; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
return info->parent; |
| 634 |
} |
| 635 |
|
| 636 |
/*! |
| 637 |
* \brief If the value type of \a id is `SPA_TYPE_Array`, this function |
| 638 |
* returns the type that is allowed to be contained inside the array. |
| 639 |
* |
| 640 |
* When the returned type is (or is derived from) `SPA_TYPE_Id` or |
| 641 |
* `SPA_TYPE_Object`, \a table is set to point to the WpSpaIdTable |
| 642 |
* that contains the possible Id values / object fields. |
| 643 |
* |
| 644 |
* \ingroup wpspatype |
| 645 |
* \param id an id value |
| 646 |
* \param table (out) (optional): the associated WpSpaIdTable |
| 647 |
* \returns (transfer none): the type that is allowed in the array, if \a id |
| 648 |
* represents an object field that takes an array as value |
| 649 |
*/ |
| 650 |
WpSpaType |
| 651 |
wp_spa_id_value_array_get_item_type (WpSpaIdValue id, WpSpaIdTable * table) |
| 652 |
{ |
| 653 |
g_return_val_if_fail (id != NULL, WP_SPA_TYPE_INVALID); |
| 654 |
|
| 655 |
const struct spa_type_info *info = id; |
| 656 |
g_return_val_if_fail (info->parent == SPA_TYPE_Array, WP_SPA_TYPE_INVALID); |
| 657 |
|
| 658 |
return info->values ? |
| 659 |
wp_spa_id_value_get_value_type (info->values, table) : |
| 660 |
WP_SPA_TYPE_INVALID; |
| 661 |
} |
| 662 |
|
| 663 |
/*! |
| 664 |
* \brief Initializes the spa dynamic type registry. |
| 665 |
* |
| 666 |
* This allows registering new spa types at runtime. The spa type system |
| 667 |
* still works if this function is not called. |
| 668 |
* |
| 669 |
* Normally called by wp_init() when WP_INIT_SPA_TYPES is passed in its flags. |
| 670 |
* |
| 671 |
* \ingroup wpspatype |
| 672 |
*/ |
| 673 |
void |
| 674 |
wp_spa_dynamic_type_init (void) |
| 675 |
{ |
| 676 |
extra_types = g_array_new (TRUE, FALSE, sizeof (struct spa_type_info)); |
| 677 |
extra_id_tables = g_array_new (TRUE, FALSE, sizeof (WpSpaIdTableInfo)); |
| 678 |
|
| 679 |
/* init to chain up to spa types */ |
| 680 |
struct spa_type_info info = { |
| 681 |
SPA_ID_INVALID, SPA_ID_INVALID, "spa_types", SPA_TYPE_ROOT |
| 682 |
}; |
| 683 |
g_array_append_val (extra_types, info); |
| 684 |
} |
| 685 |
|
| 686 |
/*! |
| 687 |
* \brief Deinitializes the spa type registry. |
| 688 |
* |
| 689 |
* You do not need to ever call this, unless you want to free memory at the |
| 690 |
* end of the execution of a test, so that it doesn't show as leaked in |
| 691 |
* the memory profiler. |
| 692 |
* |
| 693 |
* \ingroup wpspatype |
| 694 |
*/ |
| 695 |
void |
| 696 |
wp_spa_dynamic_type_deinit (void) |
| 697 |
{ |
| 698 |
g_clear_pointer (&extra_types, g_array_unref); |
| 699 |
g_clear_pointer (&extra_id_tables, g_array_unref); |
| 700 |
} |
| 701 |
|
| 702 |
/*! |
| 703 |
* \brief Registers an additional type in the spa type system. |
| 704 |
* |
| 705 |
* This is useful to add a custom pod object type. |
| 706 |
* |
| 707 |
* Note that both \a name and \a values must be statically allocated, or |
| 708 |
* otherwise guaranteed to be kept in memory until wp_spa_dynamic_type_deinit() |
| 709 |
* is called. No memory copy is done by this function. |
| 710 |
* |
| 711 |
* \ingroup wpspatype |
| 712 |
* \param name the name of the type |
| 713 |
* \param parent the parent type |
| 714 |
* \param values an array of `spa_type_info` that contains the values of the type, |
| 715 |
* used only for Object types |
| 716 |
* \returns (transfer none): the new type |
| 717 |
*/ |
| 718 |
WpSpaType |
| 719 |
wp_spa_dynamic_type_register (const gchar *name, WpSpaType parent, |
| 720 |
const struct spa_type_info * values) |
| 721 |
{ |
| 722 |
struct spa_type_info info; |
| 723 |
info.type = SPA_TYPE_VENDOR_WirePlumber + extra_types->len; |
| 724 |
info.name = name; |
| 725 |
info.parent = parent; |
| 726 |
info.values = values; |
| 727 |
g_array_append_val (extra_types, info); |
| 728 |
return info.type; |
| 729 |
} |
| 730 |
|
| 731 |
/*! |
| 732 |
* \brief Registers an additional WpSpaIdTable in the spa type system. |
| 733 |
* |
| 734 |
* This is useful to add custom enumeration types. |
| 735 |
* |
| 736 |
* Note that both \a name and \a values must be statically allocated, or |
| 737 |
* otherwise guaranteed to be kept in memory until wp_spa_dynamic_type_deinit() |
| 738 |
* is called. No memory copy is done by this function. |
| 739 |
* |
| 740 |
* \ingroup wpspatype |
| 741 |
* \param name the name of the id table |
| 742 |
* \param values an array of `spa_type_info` that contains the values of the table |
| 743 |
* \returns the new table |
| 744 |
*/ |
| 745 |
WpSpaIdTable |
| 746 |
wp_spa_dynamic_id_table_register (const gchar *name, |
| 747 |
const struct spa_type_info * values) |
| 748 |
{ |
| 749 |
WpSpaIdTableInfo info; |
| 750 |
info.name = name; |
| 751 |
info.values = values; |
| 752 |
g_array_append_val (extra_id_tables, info); |
| 753 |
return values; |
| 754 |
} |