Debian Codegraph @

src/tools/wpctl.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2019-2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <wp/wp.h>
10 #include <stdio.h>
11 #include <locale.h>
12 #include <libintl.h>
13 #include <spa/utils/defs.h>
14 #include <spa/utils/string.h>
15 #include <pipewire/pipewire.h>
16 #include <pipewire/keys.h>
17 #include <pipewire/extensions/session-manager/keys.h>
18
19 WP_DEFINE_LOCAL_LOG_TOPIC ("wpctl")
20
21 static const gchar *DEFAULT_NODE_MEDIA_CLASSES[] = {
22 "Audio/Sink",
23 "Audio/Source",
24 "Video/Source",
25 };
26
27 typedef struct _WpCtl WpCtl;
28 struct _WpCtl
29 {
30 GOptionContext *context;
31 GMainLoop *loop;
32 WpCore *core;
33 WpObjectManager *om;
34 guint pending_plugins;
35 gint exit_code;
36 };
37
38 static struct {
39 union {
40 struct {
41 gboolean display_nicknames;
42 gboolean display_names;
43 } status;
44 struct {
45 guint64 id;
46 gboolean show_referenced;
47 gboolean show_associated;
48 } inspect;
49 struct {
50 guint64 id;
51 } set_default;
52 struct {
53 guint64 id;
54 gfloat volume;
55 gdouble limit;
56 gboolean is_pid;
57 gchar type;
58 } set_volume;
59
60 struct {
61 guint64 id;
62 } get_volume;
63
64 struct {
65 guint64 id;
66 guint mute;
67 gboolean is_pid;
68 } set_mute;
69
70 struct {
71 guint64 id;
72 gint index;
73 } set_profile;
74
75 struct {
76 guint64 id;
77 gint index;
78 } set_route;
79
80 struct {
81 guint64 id;
82 } clear_default;
83
84 struct {
85 const gchar *key;
86 const gchar *val;
87 gboolean delete;
88 gboolean save;
89 gboolean reset;
90 } settings;
91
92 struct {
93 guint64 id;
94 const char *level;
95 } set_log_level;
96 };
97 } cmdline;
98
99 G_DEFINE_QUARK (wpctl-error, wpctl_error_domain)
100
101 static void
102 wp_ctl_clear (WpCtl * self)
103 {
104 g_clear_object (&self->om);
105 g_clear_object (&self->core);
106 g_clear_pointer (&self->loop, g_main_loop_unref);
107 g_clear_pointer (&self->context, g_option_context_free);
108 }
109
110 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (WpCtl, wp_ctl_clear)
111
112 static void
113 async_quit (WpCore *core, GAsyncResult *res, WpCtl * self)
114 {
115 g_main_loop_quit (self->loop);
116 }
117
118 #define DEFAULT_AUDIO_SINK_ID ((guint64)1 << 32)
119 #define DEFAULT_AUDIO_SOURCE_ID ((guint64)1 << 33)
120 #define DEFAULT_VIDEO_SOURCE_ID ((guint64)1 << 34)
121
122 static gboolean
123 parse_id (gboolean allow_def_audio, gboolean allow_def_video, gchar *arg, guint64 *result_id, GError **error)
124 {
125 if (allow_def_audio && (g_strcmp0(arg, "@DEFAULT_SINK@") == 0 ||
126 g_strcmp0(arg, "@DEFAULT_AUDIO_SINK@") == 0)) {
127 *result_id = DEFAULT_AUDIO_SINK_ID;
128 } else if (allow_def_audio && (g_strcmp0(arg, "@DEFAULT_SOURCE@") == 0 ||
129 g_strcmp0(arg, "@DEFAULT_AUDIO_SOURCE@") == 0)) {
130 *result_id = DEFAULT_AUDIO_SOURCE_ID;
131 } else if (allow_def_video && g_strcmp0(arg, "@DEFAULT_VIDEO_SOURCE@") == 0) {
132 *result_id = DEFAULT_VIDEO_SOURCE_ID;
133 } else {
134 long id = strtol (arg, NULL, 10);
135 if (id <= 0 || id >= G_MAXUINT32) {
136 g_set_error (error, wpctl_error_domain_quark(), 0,
137 "'%s' is not a valid number", arg);
138 return FALSE;
139 }
140 *result_id = id;
141 }
142
143 return TRUE;
144 }
145
146 static gboolean
147 translate_id (WpPlugin *def_nodes_api, guint64 id, guint32 *res, GError **error)
148 {
149 gchar *media_class = NULL;
150
151 if (id == DEFAULT_AUDIO_SINK_ID) {
152 media_class = "Audio/Sink";
153 } else if (id == DEFAULT_AUDIO_SOURCE_ID) {
154 media_class = "Audio/Source";
155 } else if (id == DEFAULT_VIDEO_SOURCE_ID) {
156 media_class = "Video/Source";
157 } else {
158 /* SPA_ID_INVALID is a special case used by some parse_positional() to
159 indicate that no specific ID was given. It needs to be checked because
160 currently SPA_ID_INVALID == G_MAXUINT32. */
161 if ((id <= 0 || id >= G_MAXUINT32) && id != SPA_ID_INVALID) {
162 g_set_error (error, wpctl_error_domain_quark(), 0,
163 "'%ld' is not a valid ID", id);
164 return FALSE;
165 }
166
167 *res = (guint32)id;
168 return TRUE;
169 }
170
171 if (!def_nodes_api) {
172 g_set_error (error, wpctl_error_domain_quark(), 0,
173 "Default nodes API is not loaded\n");
174 return FALSE;
175 }
176
177 g_signal_emit_by_name (def_nodes_api, "get-default-node", media_class, res);
178
179 if (*res <= 0 || *res >= G_MAXUINT32) {
180 g_set_error (error, wpctl_error_domain_quark(), 0,
181 "'%d' is not a valid ID (returned by default-nodes-api)", *res);
182 return FALSE;
183 }
184
185 return TRUE;
186 }
187
188 static gboolean
189 run_nodes_by_pid (WpObjectManager *om, guint32 pid,
190 WpIteratorFoldFunc func, gpointer data)
191 {
192 gboolean res = TRUE;
193 g_autoptr (WpIterator) client_it = NULL;
194 g_auto (GValue) client_val = G_VALUE_INIT;
195 client_it = wp_object_manager_new_filtered_iterator (om,
196 WP_TYPE_CLIENT, WP_CONSTRAINT_TYPE_PW_PROPERTY,
197 PW_KEY_APP_PROCESS_ID, "=u", pid, NULL);
198 for (; wp_iterator_next (client_it, &client_val); g_value_unset (&client_val)) {
199 WpPipewireObject *client = g_value_get_object (&client_val);
200 guint32 client_id = wp_proxy_get_bound_id (WP_PROXY (client));
201 g_autoptr (WpIterator) node_it = NULL;
202 node_it = wp_object_manager_new_filtered_iterator (om,
203 WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY,
204 PW_KEY_CLIENT_ID, "=u", client_id, NULL);
205 if (!wp_iterator_fold (node_it, func, NULL, data))
206 res = FALSE;
207 }
208
209 return res;
210 }
211
212 /* status */
213
214 static gboolean
215 status_prepare (WpCtl * self, GError ** error)
216 {
217 wp_object_manager_add_interest (self->om, WP_TYPE_CLIENT, NULL);
218 wp_object_manager_add_interest (self->om, WP_TYPE_DEVICE, NULL);
219 wp_object_manager_add_interest (self->om, WP_TYPE_NODE, NULL);
220 wp_object_manager_add_interest (self->om, WP_TYPE_PORT, NULL);
221 wp_object_manager_add_interest (self->om, WP_TYPE_LINK, NULL);
222 wp_object_manager_request_object_features (self->om, WP_TYPE_GLOBAL_PROXY,
223 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
224 wp_object_manager_add_interest (self->om, WP_TYPE_METADATA, NULL);
225 return TRUE;
226 }
227
228 #define TREE_INDENT_LINE " │ "
229 #define TREE_INDENT_NODE " ├─ "
230 #define TREE_INDENT_END " └─ "
231 #define TREE_INDENT_EMPTY " "
232
233 struct print_context
234 {
235 WpCtl *self;
236 guint32 default_node;
237 WpPlugin *mixer_api;
238 GHashTable *printed_filters;
239 };
240
241 static void
242 print_controls (guint32 id, struct print_context *context)
243 {
244 g_autoptr (GVariant) dict = NULL;
245
246 if (context->mixer_api)
247 g_signal_emit_by_name (context->mixer_api, "get-volume", id, &dict);
248
249 if (dict) {
250 gboolean mute = FALSE;
251 gdouble volume = 1.0;
252 if (g_variant_lookup (dict, "mute", "b", &mute) &&
253 g_variant_lookup (dict, "volume", "d", &volume))
254 printf (" [vol: %.2f%s", volume, mute ? " MUTED]" : "]");
255 }
256 printf ("\n");
257 }
258
259 static void
260 print_device (const GValue *item, gpointer data)
261 {
262 WpPipewireObject *obj = g_value_get_object (item);
263 guint32 id = wp_proxy_get_bound_id (WP_PROXY (obj));
264 const gchar *api = wp_pipewire_object_get_property (obj, PW_KEY_DEVICE_API);
265 const gchar *name = NULL;
266
267 if (cmdline.status.display_nicknames)
268 name = wp_pipewire_object_get_property (obj, PW_KEY_DEVICE_NICK);
269 else if (cmdline.status.display_names)
270 name = wp_pipewire_object_get_property (obj, PW_KEY_DEVICE_NAME);
271
272 if (!name)
273 name = wp_pipewire_object_get_property (obj, PW_KEY_DEVICE_DESCRIPTION);
274
275 printf (TREE_INDENT_LINE " %4u. %-35s [%s]\n", id, name, api);
276 }
277
278 static void
279 print_dev_node (const GValue *item, gpointer data)
280 {
281 WpPipewireObject *obj = g_value_get_object (item);
282 struct print_context *context = data;
283 guint32 id = wp_proxy_get_bound_id (WP_PROXY (obj));
284 gboolean is_default = (context->default_node == id);
285 const gchar *name = NULL;
286
287 if (cmdline.status.display_nicknames)
288 name = wp_pipewire_object_get_property (obj, PW_KEY_NODE_NICK);
289 else if (cmdline.status.display_names)
290 name = wp_pipewire_object_get_property (obj, PW_KEY_NODE_NAME);
291
292 if (!name)
293 name = wp_pipewire_object_get_property (obj, PW_KEY_NODE_DESCRIPTION);
294
295 printf (TREE_INDENT_LINE "%c %4u. %-35s", is_default ? '*' : ' ', id, name);
296 print_controls (id, context);
297 }
298
299 static void
300 print_filter_node (const GValue *item, gpointer data)
301 {
302 struct print_context *context = data;
303 g_autoptr (WpPlugin) def_nodes_api = NULL;
304 WpPipewireObject *obj = g_value_get_object (item);
305 g_autoptr (WpIterator) it = NULL;
306 g_auto (GValue) val = G_VALUE_INIT;
307 const gchar *link_group;
308
309 /* Skip already printed filters */
310 link_group = wp_pipewire_object_get_property (obj, PW_KEY_NODE_LINK_GROUP);
311 if (g_hash_table_contains (context->printed_filters, link_group))
312 return;
313
314 def_nodes_api = wp_plugin_find (context->self->core, "default-nodes-api");
315
316 /* Print all nodes for this link_group */
317 printf (TREE_INDENT_LINE " - %-60s\n", link_group);
318 it = wp_object_manager_new_filtered_iterator (context->self->om,
319 WP_TYPE_NODE,
320 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_NODE_LINK_GROUP, "=s", link_group,
321 NULL);
322 for (; wp_iterator_next (it, &val); g_value_unset (&val)) {
323 WpPipewireObject *node = g_value_get_object (&val);
324 guint32 id = wp_proxy_get_bound_id (WP_PROXY (node));
325 const gchar *name, *media_class;
326
327 name = wp_pipewire_object_get_property (node, PW_KEY_NODE_NAME);
328 if (cmdline.status.display_nicknames)
329 name = wp_pipewire_object_get_property (node, PW_KEY_NODE_NICK);
330 else if (cmdline.status.display_names)
331 name = wp_pipewire_object_get_property (node, PW_KEY_NODE_NAME);
332 if (!name)
333 name = wp_pipewire_object_get_property (node, PW_KEY_NODE_DESCRIPTION);
334 media_class = wp_pipewire_object_get_property (node, PW_KEY_MEDIA_CLASS);
335
336 context->default_node = -1;
337 if (def_nodes_api)
338 g_signal_emit_by_name (def_nodes_api, "get-default-node", media_class,
339 &context->default_node);
340
341 printf (TREE_INDENT_LINE "%c %4u. %-60s [%s]\n",
342 context->default_node == id ? '*' : ' ', id, name, media_class);
343 }
344 g_clear_pointer (&it, wp_iterator_unref);
345
346 /* Insert link-group in table to not print them again */
347 g_hash_table_insert (context->printed_filters, g_strdup (link_group),
348 NULL);
349 }
350
351 static void
352 print_stream_node (const GValue *item, gpointer data)
353 {
354 WpCtl * self = data;
355 WpPipewireObject *obj = g_value_get_object (item);
356 guint32 id = wp_proxy_get_bound_id (WP_PROXY (obj));
357 const gchar *name = wp_pipewire_object_get_property (obj, PW_KEY_APP_NAME);
358 if (!name)
359 name = wp_pipewire_object_get_property (obj, PW_KEY_NODE_NAME);
360
361 printf (TREE_INDENT_EMPTY " %4u. %-60s\n", id, name);
362
363 g_autoptr (WpIterator) it = wp_object_manager_new_filtered_iterator (self->om,
364 WP_TYPE_PORT, WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_NODE_ID, "=u", id,
365 NULL);
366 g_auto (GValue) val = G_VALUE_INIT;
367
368 for (; wp_iterator_next (it, &val); g_value_unset (&val)) {
369 WpPort *port = g_value_get_object (&val);
370 obj = WP_PIPEWIRE_OBJECT (port);
371 id = wp_proxy_get_bound_id (WP_PROXY (obj));
372 name = wp_pipewire_object_get_property (obj, PW_KEY_PORT_NAME);
373 WpDirection dir = wp_port_get_direction (port);
374
375 printf (TREE_INDENT_EMPTY " %4u. %-15s", id, name);
376
377 g_autoptr (WpLink) link = wp_object_manager_lookup (self->om, WP_TYPE_LINK,
378 WP_CONSTRAINT_TYPE_PW_PROPERTY, (dir == WP_DIRECTION_OUTPUT) ?
379 PW_KEY_LINK_OUTPUT_PORT : PW_KEY_LINK_INPUT_PORT, "=u", id,
380 NULL);
381 if (link) {
382 guint32 peer_id = -1;
383 wp_link_get_linked_object_ids(link,
384 NULL, (dir == WP_DIRECTION_INPUT) ? &peer_id : NULL,
385 NULL, (dir == WP_DIRECTION_OUTPUT) ? &peer_id : NULL);
386 g_autoptr (WpPipewireObject) peer = wp_object_manager_lookup (
387 self->om, WP_TYPE_PORT,
388 WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", peer_id,
389 NULL);
390 name = wp_pipewire_object_get_property (peer, PW_KEY_PORT_ALIAS);
391
392 g_autoptr (GEnumClass) klass = g_type_class_ref (WP_TYPE_LINK_STATE);
393 GEnumValue *state = g_enum_get_value (klass, wp_link_get_state (link, NULL));
394
395 printf (" %c %s\t[%s]\n", (dir == WP_DIRECTION_OUTPUT) ? '>' : '<', name,
396 state->value_nick);
397 } else {
398 printf ("\n");
399 }
400 }
401 }
402
403 static void
404 status_run (WpCtl * self)
405 {
406 g_autoptr (WpIterator) it = NULL;
407 g_auto (GValue) val = G_VALUE_INIT;
408 g_autoptr (WpPlugin) def_nodes_api = NULL;
409 struct print_context context = { .self = self };
410
411 def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
412 context.mixer_api = wp_plugin_find (self->core, "mixer-api");
413
414 /* server + clients */
415 printf ("PipeWire '%s' [%s, %s@%s, cookie:%u]\n",
416 wp_core_get_remote_name (self->core),
417 wp_core_get_remote_version (self->core),
418 wp_core_get_remote_user_name (self->core),
419 wp_core_get_remote_host_name (self->core),
420 wp_core_get_remote_cookie (self->core));
421
422 printf (TREE_INDENT_END "Clients:\n");
423 it = wp_object_manager_new_filtered_iterator (self->om, WP_TYPE_CLIENT, NULL);
424 for (; wp_iterator_next (it, &val); g_value_unset (&val)) {
425 WpProxy *client = g_value_get_object (&val);
426 g_autoptr (WpProperties) properties =
427 wp_pipewire_object_get_properties (WP_PIPEWIRE_OBJECT (client));
428
429 printf (TREE_INDENT_EMPTY " %4u. %-35s [%s, %s@%s, pid:%s]\n",
430 wp_proxy_get_bound_id (client),
431 wp_properties_get (properties, PW_KEY_APP_NAME),
432 wp_properties_get (properties, PW_KEY_CORE_VERSION),
433 wp_properties_get (properties, PW_KEY_APP_PROCESS_USER),
434 wp_properties_get (properties, PW_KEY_APP_PROCESS_HOST),
435 wp_properties_get (properties, PW_KEY_APP_PROCESS_ID));
436 }
437 g_clear_pointer (&it, wp_iterator_unref);
438 printf ("\n");
439
440 /* sessions */
441 const gchar *MEDIA_TYPES[] = { "Audio", "Video" };
442
443 for (guint i = 0; i < G_N_ELEMENTS (MEDIA_TYPES); i++) {
444 const gchar *media_type = MEDIA_TYPES[i];
445 g_autoptr (WpIterator) child_it = NULL;
446
447 printf ("%s\n", media_type);
448
449 if (media_type && *media_type != '\0') {
450 gchar media_type_glob[16];
451 gchar media_class[24];
452
453 g_snprintf (media_type_glob, sizeof(media_type_glob), "*%s*", media_type);
454
455 printf (TREE_INDENT_NODE "Devices:\n");
456 child_it = wp_object_manager_new_filtered_iterator (self->om,
457 WP_TYPE_DEVICE,
458 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_MEDIA_CLASS, "#s", media_type_glob,
459 NULL);
460 wp_iterator_foreach (child_it, print_device, self);
461 g_clear_pointer (&child_it, wp_iterator_unref);
462
463 printf (TREE_INDENT_LINE "\n");
464
465 printf (TREE_INDENT_NODE "Sinks:\n");
466 g_snprintf (media_class, sizeof(media_class), "%s/Sink", media_type);
467 context.default_node = -1;
468 if (def_nodes_api)
469 g_signal_emit_by_name (def_nodes_api, "get-default-node", media_class,
470 &context.default_node);
471 child_it = wp_object_manager_new_filtered_iterator (self->om,
472 WP_TYPE_NODE,
473 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_MEDIA_CLASS, "#s", "*/Sink*",
474 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_MEDIA_CLASS, "#s", media_type_glob,
475 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_NODE_LINK_GROUP, "-",
476 NULL);
477 wp_iterator_foreach (child_it, print_dev_node, (gpointer) &context);
478 g_clear_pointer (&child_it, wp_iterator_unref);
479
480 printf (TREE_INDENT_LINE "\n");
481
482 printf (TREE_INDENT_NODE "Sources:\n");
483 g_snprintf (media_class, sizeof(media_class), "%s/Source", media_type);
484 context.default_node = -1;
485 if (def_nodes_api)
486 g_signal_emit_by_name (def_nodes_api, "get-default-node", media_class,
487 &context.default_node);
488 child_it = wp_object_manager_new_filtered_iterator (self->om,
489 WP_TYPE_NODE,
490 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_MEDIA_CLASS, "#s", "*/Source*",
491 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_MEDIA_CLASS, "#s", media_type_glob,
492 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_NODE_LINK_GROUP, "-",
493 NULL);
494 wp_iterator_foreach (child_it, print_dev_node, (gpointer) &context);
495 g_clear_pointer (&child_it, wp_iterator_unref);
496
497 printf (TREE_INDENT_LINE "\n");
498
499 printf (TREE_INDENT_NODE "Filters:\n");
500 context.printed_filters = g_hash_table_new_full (g_str_hash,
501 g_str_equal, g_free, NULL);
502 child_it = wp_object_manager_new_filtered_iterator (self->om,
503 WP_TYPE_NODE,
504 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_MEDIA_CLASS, "#s", media_type_glob,
505 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_NODE_LINK_GROUP, "+",
506 NULL);
507 wp_iterator_foreach (child_it, print_filter_node, (gpointer) &context);
508 g_clear_pointer (&child_it, wp_iterator_unref);
509 g_clear_pointer (&context.printed_filters, g_hash_table_unref);
510
511 printf (TREE_INDENT_LINE "\n");
512
513 printf (TREE_INDENT_END "Streams:\n");
514 child_it = wp_object_manager_new_filtered_iterator (self->om,
515 WP_TYPE_NODE,
516 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_MEDIA_CLASS, "#s", "Stream/*",
517 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_MEDIA_CLASS, "#s", media_type_glob,
518 WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_NODE_LINK_GROUP, "-",
519 NULL);
520 wp_iterator_foreach (child_it, print_stream_node, self);
521 g_clear_pointer (&child_it, wp_iterator_unref);
522 }
523
524 printf ("\n");
525 }
526
527 /* Settings */
528 printf ("Settings\n");
529
530 printf (TREE_INDENT_END "Default Configured Devices:\n");
531 if (def_nodes_api) {
532 for (guint i = 0; i < G_N_ELEMENTS (DEFAULT_NODE_MEDIA_CLASSES); i++) {
533 g_autofree gchar *name = NULL;
534 g_signal_emit_by_name (def_nodes_api, "get-default-configured-node-name",
535 DEFAULT_NODE_MEDIA_CLASSES[i], &name);
536 if (name)
537 printf (TREE_INDENT_EMPTY " %4u. %-12s %s\n", i,
538 DEFAULT_NODE_MEDIA_CLASSES[i], name);
539 }
540 }
541
542 g_clear_object (&context.mixer_api);
543 g_main_loop_quit (self->loop);
544 }
545
546 /* get-volume */
547
548 static gboolean
549 get_volume_parse_positional (gint argc, gchar ** argv, GError **error)
550 {
551 if (argc < 3) {
552 g_set_error (error, wpctl_error_domain_quark(), 0,
553 "ID is required");
554 return FALSE;
555 } else {
556 return parse_id (true, false, argv[2], &cmdline.get_volume.id, error);
557 }
558 }
559
560 static gboolean
561 get_volume_prepare (WpCtl * self, GError ** error)
562 {
563 wp_object_manager_add_interest (self->om, WP_TYPE_NODE, NULL);
564 wp_object_manager_request_object_features (self->om, WP_TYPE_GLOBAL_PROXY,
565 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
566 return TRUE;
567 }
568
569 static void
570 do_print_volume (WpCtl * self, WpPipewireObject *proxy)
571 {
572 g_autoptr (WpPlugin) mixer_api = wp_plugin_find (self->core, "mixer-api");
573 GVariant *variant = NULL;
574 gboolean mute = FALSE;
575 gdouble volume = 1.0;
576 guint32 id = wp_proxy_get_bound_id (WP_PROXY (proxy));
577
578 g_signal_emit_by_name (mixer_api, "get-volume", id, &variant);
579 if (!variant) {
580 fprintf (stderr, "Node %d does not support volume\n", id);
581 return;
582 }
583 g_variant_lookup (variant, "volume", "d", &volume);
584 g_variant_lookup (variant, "mute", "b", &mute);
585 g_clear_pointer (&variant, g_variant_unref);
586
587 printf ("Volume: %.2f%s", volume, mute ? " [MUTED]\n" : "\n");
588 }
589
590 static void
591 get_volume_run (WpCtl * self)
592 {
593 g_autoptr (WpPlugin) def_nodes_api = NULL;
594 g_autoptr (GError) error = NULL;
595 g_autoptr (WpPipewireObject) proxy = NULL;
596 guint32 id;
597
598 def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
599
600 if (!translate_id (def_nodes_api, cmdline.get_volume.id, &id, &error)) {
601 fprintf(stderr, "Translate ID error: %s\n\n", error->message);
602 goto out;
603 }
604
605 proxy = wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY,
606 WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", id, NULL);
607 if (!proxy) {
608 fprintf (stderr, "Node '%d' not found\n", id);
609 goto out;
610 }
611
612 do_print_volume (self, proxy);
613
614 out:
615 g_main_loop_quit (self->loop);
616 }
617
618 /* inspect */
619
620 static gboolean
621 inspect_parse_positional (gint argc, gchar ** argv, GError **error)
622 {
623 if (argc < 3) {
624 g_set_error (error, wpctl_error_domain_quark(), 0, "ID is required");
625 return FALSE;
626 }
627
628 return parse_id (true, true, argv[2], &cmdline.inspect.id, error);
629 }
630
631 static gboolean
632 inspect_prepare (WpCtl * self, GError ** error)
633 {
634 /* collect all objects */
635 wp_object_manager_add_interest (self->om, WP_TYPE_GLOBAL_PROXY, NULL);
636 wp_object_manager_request_object_features (self->om, WP_TYPE_GLOBAL_PROXY,
637 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
638 return TRUE;
639 }
640
641 static inline void
642 inspect_prefix_line (guint nest_level, gboolean node)
643 {
644 for (guint i = 1; i < nest_level; i++)
645 printf (TREE_INDENT_EMPTY TREE_INDENT_LINE);
646 if (nest_level > 0)
647 printf (TREE_INDENT_EMPTY "%s", node ? TREE_INDENT_NODE : TREE_INDENT_LINE);
648 }
649
650 struct {
651 const gchar *key;
652 const gchar *type;
653 } assoc_keys[] = {
654 { PW_KEY_CLIENT_ID, "Client" },
655 { PW_KEY_DEVICE_ID, "Device" },
656 { PW_KEY_LINK_INPUT_NODE, NULL },
657 { PW_KEY_LINK_INPUT_PORT, NULL },
658 { PW_KEY_LINK_OUTPUT_NODE, NULL },
659 { PW_KEY_LINK_OUTPUT_PORT, NULL },
660 { PW_KEY_LINK_ID, "Link" },
661 { PW_KEY_NODE_ID, "Node" },
662 { PW_KEY_PORT_ID, "Port" },
663 { PW_KEY_SESSION_ID, "Session" },
664 };
665
666 static inline gboolean
667 key_is_object_reference (const gchar *key)
668 {
669 for (guint i = 0; i < G_N_ELEMENTS (assoc_keys); i++)
670 if (!g_strcmp0 (key, assoc_keys[i].key))
671 return TRUE;
672 return FALSE;
673 }
674
675 static inline const gchar *
676 get_association_key (WpProxy * proxy)
677 {
678 for (guint i = 0; i < G_N_ELEMENTS (assoc_keys); i++) {
679 if (assoc_keys[i].type &&
680 strstr (WP_PROXY_GET_CLASS (proxy)->pw_iface_type, assoc_keys[i].type))
681 return assoc_keys[i].key;
682 }
683 return NULL;
684 }
685
686 struct property_item {
687 const gchar *key;
688 const gchar *value;
689 };
690
691 static gint
692 property_item_compare (gconstpointer a, gconstpointer b)
693 {
694 return g_strcmp0 (
695 ((struct property_item *) a)->key,
696 ((struct property_item *) b)->key);
697 }
698
699 static void
700 inspect_print_object (WpCtl * self, WpProxy * proxy, guint nest_level)
701 {
702 g_autoptr (WpProperties) properties =
703 WP_IS_PIPEWIRE_OBJECT (proxy) ?
704 wp_pipewire_object_get_properties (WP_PIPEWIRE_OBJECT (proxy)) :
705 wp_properties_new_empty ();
706 g_autoptr (WpProperties) global_p =
707 wp_global_proxy_get_global_properties (WP_GLOBAL_PROXY (proxy));
708 g_autoptr (GArray) array =
709 g_array_new (FALSE, FALSE, sizeof (struct property_item));
710
711 /* print basic object info */
712 inspect_prefix_line (nest_level, TRUE);
713 printf ("id %u, type %s\n",
714 wp_proxy_get_bound_id (proxy),
715 WP_PROXY_GET_CLASS (proxy)->pw_iface_type);
716
717 /* merge the two property sets */
718 properties = wp_properties_ensure_unique_owner (properties);
719 wp_properties_add (properties, global_p);
720 wp_properties_set (properties, "object.id", NULL);
721
722 /* copy key/value pointers to an array for sorting */
723 {
724 g_autoptr (WpIterator) it = NULL;
725 g_auto (GValue) item = G_VALUE_INIT;
726
727 for (it = wp_properties_new_iterator (properties);
728 wp_iterator_next (it, &item);
729 g_value_unset (&item)) {
730 WpPropertiesItem *pi = g_value_get_boxed (&item);
731 struct property_item prop_item = {
732 .key = wp_properties_item_get_key (pi),
733 .value = wp_properties_item_get_value (pi),
734 };
735 g_array_append_val (array, prop_item);
736 }
737 }
738
739 /* sort */
740 g_array_sort (array, property_item_compare);
741
742 /* print */
743 for (guint i = 0; i < array->len; i++) {
744 struct property_item *prop_item =
745 &g_array_index (array, struct property_item, i);
746 gboolean is_global =
747 (wp_properties_get (global_p, prop_item->key) != NULL);
748
749 inspect_prefix_line (nest_level, FALSE);
750 printf (" %c %s = \"%s\"\n", is_global ? '*' : ' ',
751 prop_item->key, prop_item->value);
752
753 /* if the property is referencing an object, print the object */
754 if (cmdline.inspect.show_referenced && nest_level == 0 &&
755 key_is_object_reference (prop_item->key))
756 {
757 guint32 id = (guint32) strtol (prop_item->value, NULL, 10);
758 g_autoptr (WpProxy) refer_proxy =
759 wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY,
760 WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", id, NULL);
761
762 if (refer_proxy)
763 inspect_print_object (self, refer_proxy, nest_level + 1);
764 }
765 }
766
767 /* print associated objects */
768 if (cmdline.inspect.show_associated && nest_level == 0) {
769 const gchar *lookup_key = get_association_key (proxy);
770 if (lookup_key) {
771 g_autoptr (WpIterator) it =
772 wp_object_manager_new_filtered_iterator (self->om,
773 WP_TYPE_PIPEWIRE_OBJECT, WP_CONSTRAINT_TYPE_PW_PROPERTY,
774 lookup_key, "=u", wp_proxy_get_bound_id (proxy), NULL);
775 g_auto (GValue) item = G_VALUE_INIT;
776
777 inspect_prefix_line (nest_level, TRUE);
778 printf ("associated objects:\n");
779
780 for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
781 WpProxy *assoc_proxy = g_value_get_object (&item);
782 inspect_print_object (self, assoc_proxy, nest_level + 1);
783 }
784 }
785 }
786 }
787
788 static void
789 inspect_run (WpCtl * self)
790 {
791 g_autoptr (WpProxy) proxy = NULL;
792 g_autoptr (WpPlugin) def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
793 g_autoptr (GError) error = NULL;
794 guint32 id;
795
796
797 if (!translate_id (def_nodes_api, cmdline.inspect.id, &id, &error)) {
798 fprintf(stderr, "Translate ID error: %s\n\n", error->message);
799 goto out_err;
800 }
801
802 proxy = wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY,
803 WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=u", id, NULL);
804 if (!proxy) {
805 fprintf (stderr, "Object '%d' not found\n", id);
806 goto out_err;
807 }
808
809 inspect_print_object (self, proxy, 0);
810
811 out:
812 g_main_loop_quit (self->loop);
813 return;
814
815 out_err:
816 self->exit_code = 3;
817 goto out;
818 }
819
820 /* set-default */
821 static gboolean
822 set_default_parse_positional (gint argc, gchar ** argv, GError **error)
823 {
824 if (argc < 3) {
825 g_set_error (error, wpctl_error_domain_quark(), 0, "ID is required");
826 return FALSE;
827 }
828
829 return parse_id (false, false, argv[2], &cmdline.set_default.id, error);
830 }
831
832 static gboolean
833 set_default_prepare (WpCtl * self, GError ** error)
834 {
835 wp_object_manager_add_interest (self->om, WP_TYPE_NODE,
836 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY,
837 "object.id", "=u", (guint32) cmdline.set_default.id,
838 NULL);
839 wp_object_manager_request_object_features (self->om, WP_TYPE_NODE,
840 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
841 return TRUE;
842 }
843
844 static void
845 set_default_run (WpCtl * self)
846 {
847 g_autoptr (WpPlugin) def_nodes_api = NULL;
848 g_autoptr (WpProxy) proxy = NULL;
849 guint32 id;
850 g_autoptr (GError) error = NULL;
851 const gchar *media_class;
852
853 def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
854 if (!def_nodes_api) {
855 fprintf (stderr, "Default nodes API not loaded\n");
856 goto out;
857 }
858
859 if (!translate_id (def_nodes_api, cmdline.set_default.id, &id, &error)) {
860 fprintf(stderr, "Translate ID error: %s\n\n", error->message);
861 goto out;
862 }
863
864 proxy = wp_object_manager_lookup (self->om, WP_TYPE_NODE, NULL);
865 if (!proxy) {
866 fprintf (stderr, "Node '%d' not found\n", id);
867 goto out;
868 }
869
870 media_class = wp_pipewire_object_get_property (WP_PIPEWIRE_OBJECT (proxy),
871 PW_KEY_MEDIA_CLASS);
872 for (guint i = 0; i < G_N_ELEMENTS (DEFAULT_NODE_MEDIA_CLASSES); i++) {
873 if (g_str_has_prefix (media_class, DEFAULT_NODE_MEDIA_CLASSES[i]) &&
874 !g_str_has_suffix (media_class, "/Internal")) {
875 gboolean res = FALSE;
876 const gchar *name = wp_pipewire_object_get_property (
877 WP_PIPEWIRE_OBJECT (proxy), PW_KEY_NODE_NAME);
878 if (!name) {
879 fprintf (stderr, "node %u does not have a valid node.name\n", id);
880 goto out;
881 }
882
883 g_signal_emit_by_name (def_nodes_api, "set-default-configured-node-name",
884 DEFAULT_NODE_MEDIA_CLASSES[i], name, &res);
885 if (!res) {
886 fprintf (stderr, "failed to set default node %u (media.class = %s)\n", id,
887 media_class);
888 goto out;
889 }
890
891 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self);
892 return;
893 }
894 }
895
896 fprintf (stderr, "%u is not a device node (media.class = %s)\n", id, media_class);
897
898 out:
899 self->exit_code = 3;
900 g_main_loop_quit (self->loop);
901 }
902
903 /* set-volume */
904
905 static gboolean
906 set_volume_parse_positional (gint argc, gchar ** argv, GError **error)
907 {
908 g_autoptr (GRegex) regex = NULL;
909 g_autoptr (GMatchInfo) info = NULL;
910
911 if (argc < 4) {
912 g_set_error_literal (error, wpctl_error_domain_quark(), 0,
913 "ID and VOL[%][-/+] are required");
914 return FALSE;
915 }
916
917 regex = g_regex_new ("^(\\d*\\.?\\d*)(%?)([-+]?)$", 0, 0, NULL);
918
919 if (g_regex_match(regex, argv[3], 0, &info)) {
920 gchar *str = g_match_info_fetch (info, 1);
921 cmdline.set_volume.volume = strtof (str, NULL);
922 cmdline.set_volume.type = 'a';
923 g_free (str);
924
925 str = g_match_info_fetch (info, 2);
926 if (g_strcmp0 (str, "%") == 0)
927 cmdline.set_volume.volume /= 100;
928 g_free (str);
929
930 str = g_match_info_fetch (info, 3);
931 if (g_strcmp0 (str, "-") == 0) {
932 cmdline.set_volume.volume = -(cmdline.set_volume.volume);
933 cmdline.set_volume.type = 's';
934 } else if (g_strcmp0 (str, "+") == 0) {
935 cmdline.set_volume.type = 's';
936 }
937 g_free (str);
938 } else {
939 g_set_error (error, wpctl_error_domain_quark(), 0,
940 "Invalid volume argument. See wpctl set-volume --help");
941 return FALSE;
942 }
943
944 return parse_id (!cmdline.set_volume.is_pid, false, argv[2],
945 &cmdline.set_volume.id, error);
946 }
947
948 static gboolean
949 set_volume_prepare (WpCtl * self, GError ** error)
950 {
951 wp_object_manager_add_interest (self->om, WP_TYPE_NODE, NULL);
952 wp_object_manager_add_interest (self->om, WP_TYPE_CLIENT, NULL);
953 wp_object_manager_request_object_features (self->om, WP_TYPE_GLOBAL_PROXY,
954 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
955 return TRUE;
956 }
957
958 static gboolean
959 do_set_volume (WpCtl * self, WpPipewireObject *proxy)
960 {
961 g_autoptr (WpPlugin) mixer_api = wp_plugin_find (self->core, "mixer-api");
962 g_auto (GVariantBuilder) b = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
963 GVariant *variant = NULL;
964 gboolean res = FALSE;
965 gdouble curr_volume = 1.0;
966 guint32 id = wp_proxy_get_bound_id (WP_PROXY (proxy));
967
968 if (cmdline.set_volume.type == 's') {
969 g_signal_emit_by_name (mixer_api, "get-volume", id, &variant);
970 if (!variant) {
971 fprintf (stderr, "Node %d does not support volume\n", id);
972 g_clear_pointer (&variant, g_variant_unref);
973 return FALSE;
974 }
975 g_variant_lookup (variant, "volume", "d", &curr_volume);
976 g_clear_pointer (&variant, g_variant_unref);
977
978 cmdline.set_volume.volume = (cmdline.set_volume.volume + curr_volume);
979 }
980 if (cmdline.set_volume.volume < 0) {
981 cmdline.set_volume.volume = 0.0;
982 }
983 if (cmdline.set_volume.limit > 0) {
984 if (cmdline.set_volume.volume > cmdline.set_volume.limit) {
985 cmdline.set_volume.volume = cmdline.set_volume.limit;
986 }
987 }
988
989 g_variant_builder_add (&b, "{sv}", "volume",
990 g_variant_new_double (cmdline.set_volume.volume));
991 variant = g_variant_builder_end (&b);
992
993 g_signal_emit_by_name (mixer_api, "set-volume", id, variant, &res);
994 if (!res) {
995 fprintf (stderr, "Node %d does not support volume\n", id);
996 return FALSE;
997 }
998
999 return TRUE;
1000 }
1001
1002 static gboolean
1003 do_set_volume_cb (const GValue *node_val, GValue *ret, gpointer data)
1004 {
1005 WpCtl * self = data;
1006 WpPipewireObject *node = g_value_get_object (node_val);
1007 return do_set_volume (self, node);
1008 }
1009
1010 static void
1011 set_volume_run (WpCtl * self)
1012 {
1013 g_autoptr (WpPipewireObject) proxy = NULL;
1014 g_autoptr (WpPlugin) def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
1015 g_autoptr (GError) error = NULL;
1016 guint32 id;
1017
1018 if (!translate_id (def_nodes_api, cmdline.set_volume.id, &id, &error)) {
1019 fprintf(stderr, "Translate ID error: %s\n\n", error->message);
1020 goto out;
1021 }
1022
1023 if (cmdline.set_volume.is_pid) {
1024 if (!run_nodes_by_pid (self->om, id, do_set_volume_cb, self)) {
1025 fprintf (stderr, "Could not set volume in all nodes with PID '%d'\n", id);
1026 goto out;
1027 }
1028 } else {
1029 proxy = wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY,
1030 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "object.id", "=u", id, NULL);
1031 if (!proxy) {
1032 fprintf (stderr, "Object '%d' not found\n", id);
1033 goto out;
1034 }
1035 if (!do_set_volume (self, proxy))
1036 goto out;
1037 }
1038
1039 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self);
1040 return;
1041
1042 out:
1043 self->exit_code = 3;
1044 g_main_loop_quit (self->loop);
1045 }
1046
1047 /* set-mute */
1048
1049 static gboolean
1050 set_mute_parse_positional (gint argc, gchar ** argv, GError **error)
1051 {
1052 if (argc < 4) {
1053 g_set_error (error, wpctl_error_domain_quark(), 0,
1054 "ID and one of '1', '0' or 'toggle' are required");
1055 return FALSE;
1056 }
1057
1058 if (!g_strcmp0 (argv[3], "1"))
1059 cmdline.set_mute.mute = 1;
1060 else if (!g_strcmp0 (argv[3], "0"))
1061 cmdline.set_mute.mute = 0;
1062 else if (!g_strcmp0 (argv[3], "toggle"))
1063 cmdline.set_mute.mute = 2;
1064 else {
1065 g_set_error (error, wpctl_error_domain_quark(), 0,
1066 "'%s' is not a valid mute option", argv[3]);
1067 return FALSE;
1068 }
1069
1070 return parse_id (!cmdline.set_mute.is_pid, false, argv[2],
1071 &cmdline.set_mute.id, error);
1072 }
1073
1074 static gboolean
1075 set_mute_prepare (WpCtl * self, GError ** error)
1076 {
1077 wp_object_manager_add_interest (self->om, WP_TYPE_NODE, NULL);
1078 wp_object_manager_add_interest (self->om, WP_TYPE_CLIENT, NULL);
1079 wp_object_manager_request_object_features (self->om, WP_TYPE_GLOBAL_PROXY,
1080 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
1081 return TRUE;
1082 }
1083
1084 static gboolean
1085 do_set_mute (WpCtl * self, WpPipewireObject *proxy)
1086 {
1087 g_autoptr (WpPlugin) mixer_api = wp_plugin_find (self->core, "mixer-api");
1088 g_auto (GVariantBuilder) b = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
1089 GVariant *variant = NULL;
1090 gboolean res = FALSE;
1091 gboolean mute = FALSE;
1092 guint32 id = wp_proxy_get_bound_id (WP_PROXY (proxy));
1093
1094 g_signal_emit_by_name (mixer_api, "get-volume", id, &variant);
1095 if (!variant) {
1096 fprintf (stderr, "Node %d does not support mute\n", id);
1097 return FALSE;
1098 }
1099
1100 g_variant_lookup (variant, "mute", "b", &mute);
1101 g_clear_pointer (&variant, g_variant_unref);
1102
1103 if (cmdline.set_mute.mute == 2)
1104 mute = !mute;
1105 else
1106 mute = !!cmdline.set_mute.mute;
1107
1108 g_variant_builder_add (&b, "{sv}", "mute", g_variant_new_boolean (mute));
1109 variant = g_variant_builder_end (&b);
1110
1111 g_signal_emit_by_name (mixer_api, "set-volume", id, variant, &res);
1112
1113 return TRUE;
1114 }
1115
1116 static gboolean
1117 do_set_mute_cb (const GValue *node_val, GValue *ret, gpointer data)
1118 {
1119 WpCtl * self = data;
1120 WpPipewireObject *node = g_value_get_object (node_val);
1121 return do_set_mute (self, node);
1122 }
1123
1124 static void
1125 set_mute_run (WpCtl * self)
1126 {
1127 g_autoptr (WpPipewireObject) proxy = NULL;
1128 g_autoptr (WpPlugin) def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
1129 g_autoptr (GError) error = NULL;
1130 guint32 id;
1131
1132 if (!translate_id (def_nodes_api, cmdline.set_mute.id, &id, &error)) {
1133 fprintf(stderr, "Translate ID error: %s\n\n", error->message);
1134 goto out;
1135 }
1136
1137 if (cmdline.set_mute.is_pid) {
1138 if (!run_nodes_by_pid (self->om, id, do_set_mute_cb, self)) {
1139 fprintf (stderr, "Could not set mute in all nodes with PID '%d'\n", id);
1140 goto out;
1141 }
1142 } else {
1143 proxy = wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY,
1144 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "object.id", "=u", id, NULL);
1145 if (!proxy) {
1146 fprintf (stderr, "Object '%d' not found\n", id);
1147 goto out;
1148 }
1149 if (!do_set_mute (self, proxy))
1150 goto out;
1151 }
1152
1153 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self);
1154 return;
1155
1156 out:
1157 self->exit_code = 3;
1158 g_main_loop_quit (self->loop);
1159 }
1160
1161 /* set-profile */
1162
1163 static gboolean
1164 set_profile_parse_positional (gint argc, gchar ** argv, GError **error)
1165 {
1166 if (argc < 4) {
1167 g_set_error (error, wpctl_error_domain_quark(), 0,
1168 "ID and INDEX are required");
1169 return FALSE;
1170 }
1171
1172 cmdline.set_profile.index = atoi (argv[3]);
1173 return parse_id (true, true, argv[2], &cmdline.set_profile.id, error);
1174 }
1175
1176 static gboolean
1177 set_profile_prepare (WpCtl * self, GError ** error)
1178 {
1179 wp_object_manager_add_interest (self->om, WP_TYPE_GLOBAL_PROXY, NULL);
1180 wp_object_manager_request_object_features (self->om, WP_TYPE_GLOBAL_PROXY,
1181 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
1182 return TRUE;
1183 }
1184
1185 static void
1186 set_profile_run (WpCtl * self)
1187 {
1188 g_autoptr (WpPlugin) def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
1189 g_autoptr (WpPipewireObject) proxy = NULL;
1190 g_autoptr (GError) error = NULL;
1191 guint32 node_id;
1192
1193 if (!translate_id (def_nodes_api, cmdline.set_profile.id, &node_id, &error)) {
1194 fprintf(stderr, "Translate ID error: %s\n\n", error->message);
1195 goto out;
1196 }
1197
1198 proxy = wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY,
1199 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "object.id", "=u", node_id, NULL);
1200 if (!proxy) {
1201 fprintf (stderr, "Object '%d' not found\n", node_id);
1202 goto out;
1203 }
1204 wp_pipewire_object_set_param (proxy, "Profile", 0,
1205 wp_spa_pod_new_object (
1206 "Spa:Pod:Object:Param:Profile", "Profile",
1207 "index", "i", cmdline.set_profile.index,
1208 "save", "b", TRUE,
1209 NULL));
1210 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self);
1211 return;
1212
1213 out:
1214 self->exit_code = 3;
1215 g_main_loop_quit (self->loop);
1216 }
1217
1218 /* set-route */
1219
1220 static gboolean
1221 set_route_parse_positional (gint argc, gchar ** argv, GError **error)
1222 {
1223 if (argc < 4) {
1224 g_set_error (error, wpctl_error_domain_quark(), 0,
1225 "ID and INDEX required");
1226 return FALSE;
1227 }
1228
1229 cmdline.set_route.index = atoi (argv[3]);
1230 return parse_id (true, true, argv[2], &cmdline.set_route.id, error);
1231 }
1232
1233 static gboolean
1234 set_route_prepare (WpCtl * self, GError ** error)
1235 {
1236 wp_object_manager_add_interest (self->om, WP_TYPE_GLOBAL_PROXY, NULL);
1237 wp_object_manager_request_object_features (self->om, WP_TYPE_GLOBAL_PROXY,
1238 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
1239 return TRUE;
1240 }
1241
1242 static void
1243 set_route_run (WpCtl * self)
1244 {
1245 g_autoptr (WpPlugin) def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
1246 g_autoptr (WpPipewireObject) proxy = NULL;
1247 g_autoptr (WpPipewireObject) device_proxy = NULL;
1248 g_autoptr (GError) error = NULL;
1249 guint32 node_id;
1250
1251 if (!translate_id (def_nodes_api, cmdline.set_route.id, &node_id, &error)) {
1252 fprintf(stderr, "Translate ID error: %s\n\n", error->message);
1253 goto out;
1254 }
1255
1256 proxy = wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY,
1257 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "object.id", "=u", node_id, NULL);
1258 if (!proxy) {
1259 fprintf (stderr, "Object '%d' not found\n", node_id);
1260 goto out;
1261 }
1262
1263 const char * route_device_str = wp_pipewire_object_get_property(proxy, "card.profile.device");
1264 if (!route_device_str) {
1265 fprintf (stderr, "Property 'card.profile.device' not found\n");
1266 goto out;
1267 }
1268 guint32 route_device = atoi (route_device_str);
1269
1270 const char * device_id_str = wp_pipewire_object_get_property (proxy, "device.id");
1271 if (!device_id_str) {
1272 fprintf (stderr, "Property 'device.id' not found\n");
1273 goto out;
1274 }
1275 guint32 device_id = atoi (device_id_str);
1276
1277 device_proxy = wp_object_manager_lookup (self->om, WP_TYPE_GLOBAL_PROXY,
1278 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "object.id", "=u", device_id, NULL);
1279 if (!device_proxy) {
1280 fprintf (stderr, "Object '%d' not found\n", device_id);
1281 goto out;
1282 }
1283
1284 wp_pipewire_object_set_param (device_proxy, "Route", 0,
1285 wp_spa_pod_new_object (
1286 "Spa:Pod:Object:Param:Route", "Route",
1287 "index", "i", cmdline.set_route.index,
1288 "device", "i", route_device,
1289 NULL));
1290 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self);
1291 return;
1292
1293 out:
1294 self->exit_code = 3;
1295 g_main_loop_quit (self->loop);
1296 }
1297
1298 /* clear-default */
1299
1300 static gboolean
1301 clear_default_parse_positional (gint argc, gchar ** argv, GError **error)
1302 {
1303 if (argc >= 3) {
1304 long id = strtol (argv[2], NULL, 10);
1305 if (id < 0 || id >= (long)G_N_ELEMENTS (DEFAULT_NODE_MEDIA_CLASSES)) {
1306 g_set_error (error, wpctl_error_domain_quark(), 0,
1307 "The setting ID value must be between 0 and %ld inclusive",
1308 G_N_ELEMENTS (DEFAULT_NODE_MEDIA_CLASSES) - 1);
1309 return FALSE;
1310 }
1311 cmdline.clear_default.id = id;
1312 } else {
1313 cmdline.clear_default.id = SPA_ID_INVALID;
1314 }
1315
1316 return TRUE;
1317 }
1318
1319 static gboolean
1320 clear_default_prepare (WpCtl * self, GError ** error)
1321 {
1322 return TRUE;
1323 }
1324
1325 static void
1326 clear_default_run (WpCtl * self)
1327 {
1328 g_autoptr (WpPlugin) def_nodes_api = NULL;
1329 gboolean res = FALSE;
1330
1331 def_nodes_api = wp_plugin_find (self->core, "default-nodes-api");
1332 if (!def_nodes_api) {
1333 fprintf (stderr, "Default nodes API not loaded\n");
1334 goto out;
1335 }
1336
1337 /* clear all defaults if id was not given */
1338 if (cmdline.clear_default.id == SPA_ID_INVALID) {
1339 for (guint i = 0; i < G_N_ELEMENTS (DEFAULT_NODE_MEDIA_CLASSES); i++) {
1340 g_signal_emit_by_name (def_nodes_api, "set-default-configured-node-name",
1341 DEFAULT_NODE_MEDIA_CLASSES[i], NULL, &res);
1342 if (!res) {
1343 fprintf (stderr, "failed to clear default configured node (%s)\n",
1344 DEFAULT_NODE_MEDIA_CLASSES[i]);
1345 goto out;
1346 }
1347 }
1348 } else {
1349 g_signal_emit_by_name (def_nodes_api, "set-default-configured-node-name",
1350 DEFAULT_NODE_MEDIA_CLASSES[cmdline.clear_default.id], NULL, &res);
1351 if (!res) {
1352 fprintf (stderr, "failed to clear default configured node (%s)\n",
1353 DEFAULT_NODE_MEDIA_CLASSES[cmdline.clear_default.id]);
1354 goto out;
1355 }
1356 }
1357
1358 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self);
1359 return;
1360
1361 out:
1362 self->exit_code = 3;
1363 g_main_loop_quit (self->loop);
1364 }
1365
1366 /* settings */
1367
1368 static gboolean
1369 settings_parse_positional (gint argc, gchar ** argv, GError **error)
1370 {
1371 cmdline.settings.key = NULL;
1372 cmdline.settings.val = NULL;
1373 if (argc >= 3) {
1374 cmdline.settings.key = argv[2];
1375 if (argc >= 4)
1376 cmdline.settings.val = argv[3];
1377 }
1378
1379 if (cmdline.settings.delete && cmdline.settings.save) {
1380 g_set_error (error, wpctl_error_domain_quark(), 0,
1381 "Cannot use --delete and --save flags at the same time");
1382 return FALSE;
1383 }
1384 if (cmdline.settings.delete && cmdline.settings.reset) {
1385 g_set_error (error, wpctl_error_domain_quark(), 0,
1386 "Cannot use --delete and --reset flags at the same time");
1387 return FALSE;
1388 }
1389 if (cmdline.settings.save && cmdline.settings.reset) {
1390 g_set_error (error, wpctl_error_domain_quark(), 0,
1391 "Cannot use --save and --reset flags at the same time");
1392 return FALSE;
1393 }
1394
1395 return TRUE;
1396 }
1397
1398 static gboolean
1399 settings_prepare (WpCtl * self, GError ** error)
1400 {
1401 wp_object_manager_add_interest (self->om, WP_TYPE_METADATA, NULL);
1402 wp_object_manager_request_object_features (self->om, WP_TYPE_METADATA,
1403 WP_OBJECT_FEATURES_ALL);
1404 return TRUE;
1405 }
1406
1407 static const char *
1408 settings_spec_type_to_string (WpSettingsSpecType type)
1409 {
1410 switch (type) {
1411 case WP_SETTINGS_SPEC_TYPE_BOOL:
1412 return "Boolean";
1413 case WP_SETTINGS_SPEC_TYPE_INT:
1414 return "Integer";
1415 case WP_SETTINGS_SPEC_TYPE_FLOAT:
1416 return "Float";
1417 case WP_SETTINGS_SPEC_TYPE_STRING:
1418 return "String";
1419 case WP_SETTINGS_SPEC_TYPE_ARRAY:
1420 return "Array";
1421 case WP_SETTINGS_SPEC_TYPE_OBJECT:
1422 return "Object";
1423 case WP_SETTINGS_SPEC_TYPE_UNKNOWN:
1424 default:
1425 break;
1426 }
1427 return "Uknown";
1428 }
1429
1430 static void
1431 print_setting (WpSettings *s, const gchar *key)
1432 {
1433 g_autoptr (WpSpaJson) value = NULL;
1434 g_autoptr (WpSpaJson) saved = NULL;
1435 g_autoptr (WpSettingsSpec) spec = NULL;
1436 const gchar *name;
1437 const gchar *desc;
1438 WpSettingsSpecType val_type;
1439 g_autoptr (WpSpaJson) def = NULL;
1440 g_autoptr (WpSpaJson) min = NULL;
1441 g_autoptr (WpSpaJson) max = NULL;
1442
1443 value = wp_settings_get (s, key);
1444 saved = wp_settings_get_saved (s, key);
1445 spec = wp_settings_get_spec (s, key);
1446 name = wp_settings_spec_get_name (spec);
1447 desc = wp_settings_spec_get_description (spec);
1448 val_type = wp_settings_spec_get_value_type (spec);
1449 def = wp_settings_spec_get_default_value (spec);
1450 min = wp_settings_spec_get_min_value (spec);
1451 max = wp_settings_spec_get_max_value (spec);
1452
1453 /* print key */
1454 printf ("- Id: %s\n", key);
1455
1456 /* print spec */
1457 if (name)
1458 printf (" Name: %s\n", dgettext (GETTEXT_PACKAGE, name));
1459 printf (" Desc: %s\n", dgettext (GETTEXT_PACKAGE, desc));
1460 printf (" Type: %s\n", settings_spec_type_to_string (val_type));
1461 printf (" Default: %s", wp_spa_json_get_data (def));
1462 if (min && max)
1463 printf ("\t[Min: %s, Max: %s]", wp_spa_json_get_data (min),
1464 wp_spa_json_get_data (max));
1465 printf ("\n");
1466 printf (" Value: %s", wp_spa_json_get_data (value));
1467 if (saved)
1468 printf ("\t[Saved: %s]", wp_spa_json_get_data (saved));
1469 printf ("\n\n");
1470 }
1471
1472 static void
1473 print_settings (WpSettings *s)
1474 {
1475 g_autoptr (WpIterator) it = NULL;
1476 g_auto (GValue) item = G_VALUE_INIT;
1477
1478 printf ("Settings:\n\n");
1479
1480 it = wp_settings_new_iterator (s);
1481 while (wp_iterator_next (it, &item)) {
1482 WpSettingsItem *si = g_value_get_boxed (&item);
1483 const gchar *key = wp_settings_item_get_key (si);
1484 print_setting (s, key);
1485 g_value_unset (&item);
1486 }
1487 }
1488
1489 static void
1490 settings_run (WpCtl * self)
1491 {
1492 g_autoptr (WpSettings) s = NULL;
1493 const gchar *key = cmdline.settings.key, *val = cmdline.settings.val;
1494 gboolean delete_flag = cmdline.settings.delete;
1495 gboolean save_flag = cmdline.settings.save;
1496 gboolean reset_flag = cmdline.settings.reset;
1497
1498 s = wp_settings_find (self->core, NULL);
1499 if (!s) {
1500 printf ("Could not find registered settings\n");
1501 goto out;
1502 }
1503
1504 /* If no key or value are provided */
1505 if (!key && !val) {
1506 if (!delete_flag && !save_flag && !reset_flag) {
1507 print_settings (s);
1508 } else if (!delete_flag && save_flag && !reset_flag) {
1509 wp_settings_save_all (s);
1510 fprintf (stderr, "Saved all settings\n");
1511 } else if (delete_flag && !save_flag && !reset_flag) {
1512 wp_settings_delete_all (s);
1513 fprintf (stderr, "Deleted all saved settings\n");
1514 } else if (!delete_flag && !save_flag && reset_flag) {
1515 wp_settings_reset_all (s);
1516 fprintf (stderr, "Reset all settings\n");
1517 } else {
1518 g_assert_not_reached ();
1519 }
1520 }
1521
1522 /* If key is only provided */
1523 else if (key && !val) {
1524 if (!delete_flag && !save_flag && !reset_flag) {
1525 g_autoptr (WpSpaJson) value = NULL;
1526 value = wp_settings_get (s, key);
1527 if (value) {
1528 g_autoptr (WpSpaJson) saved = wp_settings_get_saved (s, key);
1529 printf ("Value: %s", wp_spa_json_get_data (value));
1530 if (saved)
1531 printf (" (Saved: %s)", wp_spa_json_get_data (saved));
1532 printf ("\n");
1533 } else {
1534 printf ("Setting '%s' not found\n", key);
1535 }
1536 } else if (!delete_flag && save_flag && !reset_flag) {
1537 if (wp_settings_save (s, key))
1538 printf ("Saved setting '%s' successfully\n", key);
1539 else
1540 printf ("Setting '%s' not found\n", key);
1541 } else if (delete_flag && !save_flag && !reset_flag) {
1542 if (wp_settings_delete (s, key))
1543 printf ("Deleted setting '%s' successfully\n", key);
1544 else
1545 printf ("Setting '%s' not found\n", key);
1546 } else if (!delete_flag && !save_flag && reset_flag) {
1547 if (wp_settings_reset (s, key))
1548 printf ("Reset setting '%s' successfully\n", key);
1549 else
1550 printf ("Setting '%s' not found\n", key);
1551 } else {
1552 g_assert_not_reached ();
1553 }
1554 }
1555
1556 /* If both key and value are provided */
1557 else if (key && val) {
1558 if (!delete_flag && !save_flag && !reset_flag) {
1559 g_autoptr (WpSpaJson) value = wp_spa_json_new_from_string (val);
1560 if (wp_settings_set (s, key, value))
1561 printf ("Updated setting '%s' to: %s\n", key, val);
1562 else
1563 printf ("Failed to set setting '%s' to: %s\n", key, val);
1564 } else if (!delete_flag && save_flag && !reset_flag) {
1565 g_autoptr (WpSpaJson) value = wp_spa_json_new_from_string (val);
1566 if (wp_settings_set (s, key, value) && wp_settings_save (s, key))
1567 printf ("Updated and saved setting '%s' to: %s\n", key, val);
1568 else
1569 printf ("Failed to update and save setting '%s' to: %s\n", key, val);
1570 } else if (delete_flag && !save_flag && !reset_flag) {
1571 if (wp_settings_delete (s, key))
1572 printf ("Deleted setting '%s' successfully\n", key);
1573 else
1574 printf ("Setting %s not found\n", key);
1575 } else if (!delete_flag && !save_flag && reset_flag) {
1576 if (wp_settings_reset (s, key))
1577 printf ("Reset setting '%s' successfully\n", key);
1578 else
1579 printf ("Setting '%s' not found\n", key);
1580 } else {
1581 g_assert_not_reached ();
1582 }
1583 } else {
1584 g_assert_not_reached ();
1585 }
1586
1587 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self);
1588 return;
1589
1590 out:
1591 self->exit_code = 3;
1592 g_main_loop_quit (self->loop);
1593 }
1594
1595 /* set-log-level */
1596
1597 static gboolean
1598 set_log_level_parse_positional (gint argc, gchar ** argv, GError **error)
1599 {
1600 if (argc == 4) {
1601 if (!spa_atou64 (argv[2], &cmdline.set_log_level.id, 10)) {
1602 g_set_error (error, wpctl_error_domain_quark(), 0,
1603 "failed to parse client id");
1604 return FALSE;
1605 }
1606 cmdline.set_log_level.level = argv[3];
1607 } else if (argc == 3) {
1608 cmdline.set_log_level.id = SPA_ID_INVALID;
1609 cmdline.set_log_level.level = argv[2];
1610 } else {
1611 g_set_error (error, wpctl_error_domain_quark(), 0,
1612 "wrong number of arguments for set-log-level");
1613 return FALSE;
1614 }
1615
1616 if (spa_streq(cmdline.set_log_level.level, "-"))
1617 cmdline.set_log_level.level = NULL;
1618
1619 return TRUE;
1620 }
1621
1622 static gboolean
1623 set_log_level_prepare (WpCtl * self, GError ** error)
1624 {
1625 wp_object_manager_add_interest (self->om, WP_TYPE_METADATA,
1626 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY,
1627 "metadata.name", "=s", "settings",
1628 NULL);
1629 wp_object_manager_add_interest (self->om, WP_TYPE_CLIENT,
1630 WP_CONSTRAINT_TYPE_PW_PROPERTY,
1631 "wireplumber.daemon", "+",
1632 NULL);
1633 wp_object_manager_request_object_features (self->om, WP_TYPE_METADATA,
1634 WP_OBJECT_FEATURES_ALL);
1635 wp_object_manager_request_object_features (self->om, WP_TYPE_CLIENT,
1636 WP_OBJECT_FEATURES_ALL);
1637 return TRUE;
1638 }
1639
1640 static void
1641 set_log_level_run (WpCtl * self)
1642 {
1643 g_autoptr (WpPlugin) def_nodes_api = NULL;
1644 g_autoptr (GError) error = NULL;
1645 g_autoptr (WpIterator) client_it = NULL;
1646 g_auto (GValue) client_val = G_VALUE_INIT;
1647
1648 g_autoptr (WpMetadata) settings = wp_object_manager_lookup (self->om, WP_TYPE_METADATA, NULL);
1649 if (!settings) {
1650 fprintf (stderr, "No settings metadata found\n");
1651 goto out;
1652 }
1653
1654 if (cmdline.set_log_level.id == SPA_ID_INVALID)
1655 client_it = wp_object_manager_new_filtered_iterator (self->om, WP_TYPE_CLIENT, NULL);
1656
1657 if (client_it) {
1658 for (; wp_iterator_next (client_it, &client_val); g_value_unset (&client_val)) {
1659 WpPipewireObject *client = g_value_get_object (&client_val);
1660 guint32 client_id = wp_proxy_get_bound_id (WP_PROXY (client));
1661
1662 if (client_id == SPA_ID_INVALID)
1663 continue;
1664
1665 wp_metadata_set (settings, client_id, "log.level", "", cmdline.set_log_level.level);
1666 }
1667 } else {
1668 wp_metadata_set (settings, cmdline.set_log_level.id, "log.level", "", cmdline.set_log_level.level);
1669 }
1670
1671 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) async_quit, self);
1672 return;
1673
1674 out:
1675 self->exit_code = 3;
1676 g_main_loop_quit (self->loop);
1677 }
1678
1679 #define N_ENTRIES 4
1680
1681 static const struct subcommand {
1682 /* the name to match on the command line */
1683 const gchar *name;
1684 /* description of positional arguments, shown in the help message */
1685 const gchar *positional_args;
1686 /* short description, shown at the top of the help message */
1687 const gchar *summary;
1688 /* long description, shown at the bottom of the help message */
1689 const gchar *description;
1690 /* additional cmdline arguments for this subcommand */
1691 const GOptionEntry entries[N_ENTRIES];
1692 /* function to parse positional arguments */
1693 gboolean (*parse_positional) (gint, gchar **, GError **);
1694 /* function to prepare the object manager */
1695 gboolean (*prepare) (WpCtl *, GError **);
1696 /* function to run after the object manager is installed */
1697 void (*run) (WpCtl *);
1698 } subcommands[] = {
1699 {
1700 .name = "status",
1701 .positional_args = "",
1702 .summary = "Displays the current state of objects in PipeWire",
1703 .description = NULL,
1704 .entries = {
1705 { "nick", 'k', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1706 &cmdline.status.display_nicknames,
1707 "Display device and node nicknames instead of descriptions", NULL },
1708 { "name", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1709 &cmdline.status.display_names,
1710 "Display device and node names instead of descriptions", NULL },
1711 { NULL }
1712 },
1713 .parse_positional = NULL,
1714 .prepare = status_prepare,
1715 .run = status_run,
1716 },
1717 {
1718 .name = "get-volume",
1719 .positional_args = "ID",
1720 .summary = "Displays volume information about the specified node in PipeWire",
1721 .description = NULL,
1722 .entries = { { NULL } },
1723 .parse_positional = get_volume_parse_positional,
1724 .prepare = get_volume_prepare,
1725 .run = get_volume_run,
1726 },
1727 {
1728 .name = "inspect",
1729 .positional_args = "ID",
1730 .summary = "Displays information about the specified object",
1731 .description = NULL,
1732 .entries = {
1733 { "referenced", 'r', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1734 &cmdline.inspect.show_referenced,
1735 "Show objects that are referenced in properties", NULL },
1736 { "associated", 'a', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1737 &cmdline.inspect.show_associated, "Show associated objects", NULL },
1738 { NULL }
1739 },
1740 .parse_positional = inspect_parse_positional,
1741 .prepare = inspect_prepare,
1742 .run = inspect_run,
1743 },
1744 {
1745 .name = "set-default",
1746 .positional_args = "ID",
1747 .summary = "Sets ID to be the default object of its kind "
1748 "(capture/playback) in its session",
1749 .description = NULL,
1750 .entries = { { NULL } },
1751 .parse_positional = set_default_parse_positional,
1752 .prepare = set_default_prepare,
1753 .run = set_default_run,
1754 },
1755 {
1756 .name = "set-volume",
1757 .positional_args = "ID VOL[%][-/+]",
1758 .summary =
1759 "Sets the volume of ID from specified argument. (floating point, 1.0 is 100%)\n"
1760 " VOL%[-/+] - Step up/down volume by specified percent (Example: 0.5%+)\n"
1761 " VOL[-/+] - Step up/down volume by specified value (Example: 0.5+)\n"
1762 " VOL - Set volume as the specified value (Example: 0.5)",
1763 .description = NULL,
1764 .entries = {
1765 { "pid", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1766 &cmdline.set_volume.is_pid,
1767 "Selects all nodes associated to the given PID number", NULL },
1768 { "limit", 'l', G_OPTION_FLAG_NONE, G_OPTION_ARG_DOUBLE,
1769 &cmdline.set_volume.limit,
1770 "Limits the final volume percentage to below this value. (floating point, 1.0 is 100%)", NULL },
1771 { NULL }
1772 },
1773 .parse_positional = set_volume_parse_positional,
1774 .prepare = set_volume_prepare,
1775 .run = set_volume_run,
1776 },
1777 {
1778 .name = "set-mute",
1779 .positional_args = "ID 1|0|toggle",
1780 .summary = "Changes the mute state of ID",
1781 .description = NULL,
1782 .entries = {
1783 { "pid", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1784 &cmdline.set_mute.is_pid,
1785 "Selects all nodes associated to the given PID number", NULL },
1786 { NULL }
1787 },
1788 .parse_positional = set_mute_parse_positional,
1789 .prepare = set_mute_prepare,
1790 .run = set_mute_run,
1791 },
1792 {
1793 .name = "set-profile",
1794 .positional_args = "ID INDEX",
1795 .summary = "Sets the profile of ID to INDEX (integer, 0 is 'off')",
1796 .description = NULL,
1797 .entries = { { NULL } },
1798 .parse_positional = set_profile_parse_positional,
1799 .prepare = set_profile_prepare,
1800 .run = set_profile_run,
1801 },
1802 {
1803 .name = "set-route",
1804 .positional_args = "ID INDEX",
1805 .summary = "Sets the route of ID to INDEX (integer, 0 is 'off')",
1806 .description = NULL,
1807 .entries = { { NULL } },
1808 .parse_positional = set_route_parse_positional,
1809 .prepare = set_route_prepare,
1810 .run = set_route_run,
1811 },
1812 {
1813 .name = "clear-default",
1814 .positional_args = "[ID]",
1815 .summary = "Clears the default configured node (no ID means 'all')",
1816 .description = NULL,
1817 .entries = { { NULL } },
1818 .parse_positional = clear_default_parse_positional,
1819 .prepare = clear_default_prepare,
1820 .run = clear_default_run,
1821 },
1822 {
1823 .name = "settings",
1824 .positional_args = "[KEY] [VAL]",
1825 .summary = "Shows, changes or removes settings",
1826 .description = NULL,
1827 .entries = {
1828 { "delete", 'd', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1829 &cmdline.settings.delete,
1830 "Deletes the saved setting value (no KEY means 'all')", NULL },
1831 { "save", 's', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1832 &cmdline.settings.save,
1833 "Saves the setting value (no KEY means 'all', no VAL means current value)", NULL },
1834 { "reset", 'r', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
1835 &cmdline.settings.reset,
1836 "Resets the saved setting to its default value", NULL },
1837 { NULL }
1838 },
1839 .parse_positional = settings_parse_positional,
1840 .prepare = settings_prepare,
1841 .run = settings_run,
1842 },
1843 {
1844 .name = "set-log-level",
1845 .positional_args = "[ID] LEVEL",
1846 .summary = "Sets the log level of a client (no ID means Wireplumber, 0 means Pipewire server)",
1847 .description = NULL,
1848 .entries = { { NULL } },
1849 .parse_positional = set_log_level_parse_positional,
1850 .prepare = set_log_level_prepare,
1851 .run = set_log_level_run,
1852 }
1853 };
1854
1855 static void
1856 on_settings_activated (WpSettings *s, GAsyncResult *res, WpCtl *ctl)
1857 {
1858 GError *error = NULL;
1859
1860 if (!wp_object_activate_finish (WP_OBJECT (s), res, &error)) {
1861 fprintf (stderr, "%s\n", error->message);
1862 ctl->exit_code = 1;
1863 g_main_loop_quit (ctl->loop);
1864 return;
1865 }
1866
1867 wp_core_register_object (ctl->core, g_object_ref (s));
1868 }
1869
1870 static void
1871 on_plugin_loaded (WpCore * core, GAsyncResult * res, WpCtl *ctl)
1872 {
1873 GError *error = NULL;
1874
1875 if (!wp_core_load_component_finish (core, res, &error)) {
1876 fprintf (stderr, "%s\n", error->message);
1877 ctl->exit_code = 1;
1878 g_main_loop_quit (ctl->loop);
1879 return;
1880 }
1881
1882 if (--ctl->pending_plugins == 0) {
1883 g_autoptr (WpPlugin) mixer_api = wp_plugin_find (core, "mixer-api");
1884 g_object_set (mixer_api, "scale", 1 /* cubic */, NULL);
1885 wp_core_install_object_manager (ctl->core, ctl->om);
1886 }
1887 }
1888
1889 gint
1890 main (gint argc, gchar **argv)
1891 {
1892 g_auto (WpCtl) ctl = {0};
1893 const struct subcommand *cmd = NULL;
1894 g_autoptr (GError) error = NULL;
1895 g_autofree gchar *summary = NULL;
1896 g_autoptr (WpSettings) settings = NULL;
1897
1898 setlocale (LC_ALL, "");
1899 setlocale (LC_NUMERIC, "C");
1900 wp_init (WP_INIT_ALL);
1901
1902 ctl.context = g_option_context_new (
1903 "COMMAND [COMMAND_OPTIONS] - WirePlumber Control CLI");
1904 ctl.loop = g_main_loop_new (NULL, FALSE);
1905 ctl.core = wp_core_new (NULL, NULL, NULL);
1906 ctl.om = wp_object_manager_new ();
1907
1908 /* find the subcommand */
1909 if (argc > 1) {
1910 for (guint i = 0; i < G_N_ELEMENTS (subcommands); i++) {
1911 if (!g_strcmp0 (argv[1], subcommands[i].name)) {
1912 cmd = &subcommands[i];
1913 break;
1914 }
1915 }
1916 }
1917
1918 /* prepare the subcommand options */
1919 if (cmd) {
1920 GOptionGroup *group;
1921
1922 /* options */
1923 group = g_option_group_new (cmd->name, NULL, NULL, &ctl, NULL);
1924 g_option_group_add_entries (group, cmd->entries);
1925 g_option_context_set_main_group (ctl.context, group);
1926
1927 /* summary */
1928 summary = g_strdup_printf ("Command: %s %s\n %s",
1929 cmd->name, cmd->positional_args, cmd->summary);
1930 g_option_context_set_summary (ctl.context, summary);
1931
1932 /* description */
1933 if (cmd->description)
1934 g_option_context_set_description (ctl.context, cmd->description);
1935 }
1936 else {
1937 /* build the generic summary */
1938 GString *summary_str = g_string_new ("Commands:");
1939 for (guint i = 0; i < G_N_ELEMENTS (subcommands); i++) {
1940 g_string_append_printf (summary_str, "\n %s %s", subcommands[i].name,
1941 subcommands[i].positional_args);
1942 }
1943 summary = g_string_free (summary_str, FALSE);
1944 g_option_context_set_summary (ctl.context, summary);
1945 g_option_context_set_description (ctl.context, "Pass -h after a command "
1946 "to see command-specific options\n");
1947 }
1948
1949 /* parse options */
1950 if (!g_option_context_parse (ctl.context, &argc, &argv, &error) ||
1951 (cmd && cmd->parse_positional &&
1952 !cmd->parse_positional (argc, argv, &error))) {
1953 fprintf (stderr, "Error: %s\n\n", error->message);
1954 cmd = NULL;
1955 }
1956
1957 /* no active subcommand, show usage and exit */
1958 if (!cmd) {
1959 g_autofree gchar *help =
1960 g_option_context_get_help (ctl.context, FALSE, NULL);
1961 printf ("%s", help);
1962 return 1;
1963 }
1964
1965 /* prepare the subcommand */
1966 if (!cmd->prepare (&ctl, &error)) {
1967 fprintf (stderr, "%s\n", error->message);
1968 return 1;
1969 }
1970
1971 /* load and register settings */
1972 settings = wp_settings_new (ctl.core, NULL);
1973 wp_object_activate (WP_OBJECT (settings),
1974 WP_OBJECT_FEATURES_ALL,
1975 NULL,
1976 (GAsyncReadyCallback)on_settings_activated,
1977 &ctl);
1978
1979 /* load required API modules */
1980 ctl.pending_plugins++;
1981 wp_core_load_component (ctl.core, "libwireplumber-module-default-nodes-api",
1982 "module", NULL, NULL, NULL, (GAsyncReadyCallback) on_plugin_loaded, &ctl);
1983 ctl.pending_plugins++;
1984 wp_core_load_component (ctl.core, "libwireplumber-module-mixer-api",
1985 "module", NULL, NULL, NULL, (GAsyncReadyCallback) on_plugin_loaded, &ctl);
1986
1987 /* connect */
1988 if (!wp_core_connect (ctl.core)) {
1989 fprintf (stderr, "Could not connect to PipeWire\n");
1990 return 2;
1991 }
1992
1993 /* run */
1994 g_signal_connect_swapped (ctl.core, "disconnected",
1995 (GCallback) g_main_loop_quit, ctl.loop);
1996 g_signal_connect_swapped (ctl.om, "installed",
1997 (GCallback) cmd->run, &ctl);
1998
1999 g_main_loop_run (ctl.loop);
2000
2001 return ctl.exit_code;
2002 }