diff --git a/src/tools/wpctl.c b/src/tools/wpctl.c index eb60778f..6ab50c54 100644 --- a/src/tools/wpctl.c +++ b/src/tools/wpctl.c @@ -108,6 +108,10 @@ static struct { gboolean reset; } settings; + struct { + const gchar *collection_name; + } collections; + struct { guint64 id; const char *level; @@ -1765,6 +1769,133 @@ out: g_main_loop_quit (self->loop); } +/* collections */ + +static gboolean +collections_parse_positional (gint argc, gchar ** argv, GError **error) +{ + cmdline.collections.collection_name = NULL; + + if (argc >= 3) + cmdline.collections.collection_name = argv[2]; + + return TRUE; +} + +static gboolean +collections_prepare (WpCtl * self, GError ** error) +{ + wp_object_manager_add_interest (self->om, WP_TYPE_GLOBAL_PROXY, NULL); + wp_object_manager_request_object_features (self->om, WP_TYPE_GLOBAL_PROXY, + WP_OBJECT_FEATURES_ALL); + return TRUE; +} + +static void +print_global (WpGlobalProxy *global) +{ + g_autoptr (WpProperties) props = NULL; + const gchar *global_name = NULL; + gchar global_type = '-'; + guint32 bound_id; + + props = wp_global_proxy_get_global_properties (global); + bound_id = wp_proxy_get_bound_id (WP_PROXY (global)); + + if (WP_IS_NODE (global)) { + global_name = wp_properties_get (props, "node.name"); + global_type = 'n'; + } else if (WP_IS_PORT (global)) { + global_name = wp_properties_get (props, "port.name"); + global_type = 'p'; + } else if (WP_IS_DEVICE (global)) { + global_name = wp_properties_get (props, "device.name"); + global_type = 'd'; + } else if (WP_IS_CLIENT (global)) { + global_name = wp_properties_get (props, "client.name"); + global_type = 'c'; + } else if (WP_IS_METADATA (global)) { + global_name = wp_properties_get (props, "metadata.name"); + global_type = 'm'; + } else if (WP_IS_FACTORY (global)) { + global_name = wp_properties_get (props, "factory.name"); + global_type = 'f'; + } else if (WP_IS_COLLECTION (global)) { + global_name = wp_properties_get (props, "collection.name"); + global_type = 'o'; + } + + g_print (" [%c] %4u. %s\n", global_type, bound_id, + global_name ? global_name : "UNKNOWN"); +} + +static void +print_collection (WpCtl * self, WpCollection *collection) +{ + const gchar *collection_name = NULL; + g_autoptr (WpIterator) iter = NULL; + g_auto (GValue) val = G_VALUE_INIT; + guint32 bound_id; + + collection_name = wp_collection_get_name (collection); + bound_id = wp_proxy_get_bound_id (WP_PROXY (collection)); + + g_print ("%4u. %s\n", bound_id, collection_name); + + iter = wp_collection_new_iterator (collection); + while (wp_iterator_next (iter, &val)) { + guint32 global_id = g_value_get_uint (&val); + g_autoptr (WpGlobalProxy) global = NULL; + global = wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY, + WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", global_id, NULL); + if (global) + print_global (global); + g_value_unset (&val); + } + + printf ("\n"); +} + +static void +print_collections (WpCtl * self) +{ + g_autoptr (WpIterator) it = NULL; + g_auto (GValue) item = G_VALUE_INIT; + + printf ("Collections:\n\n"); + + it = wp_object_manager_new_filtered_iterator (self->om, WP_TYPE_COLLECTION, + NULL); + while (wp_iterator_next (it, &item)) { + WpCollection *collection = g_value_get_object (&item); + print_collection (self, collection); + g_value_unset (&item); + } +} + +static void +collections_run (WpCtl * self) +{ + const gchar *collection_name = cmdline.collections.collection_name; + + /* Print all collections if name is not provided */ + if (collection_name) { + g_autoptr (WpCollection) collection = wp_object_manager_lookup (self->om, + WP_TYPE_COLLECTION, WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, + "wireplumber.collection", "=s", "true", + WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "collection.name", "=s", + collection_name, NULL); + if (collection) + print_collection (self, collection); + else + printf ("Collection '%s' does not exist\n", collection_name); + } else { + print_collections (self); + } + + wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self); +} + /* set-log-level */ static gboolean @@ -2023,6 +2154,16 @@ static const struct subcommand { .prepare = settings_prepare, .run = settings_run, }, + { + .name = "collections", + .positional_args = "[COLLECTION]", + .summary = "Shows information about collections", + .description = NULL, + .entries = { { NULL } }, + .parse_positional = collections_parse_positional, + .prepare = collections_prepare, + .run = collections_run, + }, { .name = "set-log-level", .positional_args = "[ID] LEVEL",