]> code.delx.au - pulseaudio/blob - src/utils/pactl.c
pactl: Use colon to separate priority in profiles output
[pulseaudio] / src / utils / pactl.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <getopt.h>
34 #include <locale.h>
35 #include <ctype.h>
36
37 #include <sndfile.h>
38
39 #include <pulse/pulseaudio.h>
40 #include <pulse/ext-device-restore.h>
41
42 #include <pulsecore/i18n.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/sndfile-util.h>
47
48 static pa_context *context = NULL;
49 static pa_mainloop_api *mainloop_api = NULL;
50
51 static char
52 *list_type = NULL,
53 *sample_name = NULL,
54 *sink_name = NULL,
55 *source_name = NULL,
56 *module_name = NULL,
57 *module_args = NULL,
58 *card_name = NULL,
59 *profile_name = NULL,
60 *port_name = NULL,
61 *formats = NULL;
62
63 static uint32_t
64 sink_input_idx = PA_INVALID_INDEX,
65 source_output_idx = PA_INVALID_INDEX,
66 sink_idx = PA_INVALID_INDEX;
67
68 static pa_bool_t short_list_format = FALSE;
69 static uint32_t module_index;
70 static int32_t latency_offset;
71 static pa_bool_t suspend;
72 static pa_volume_t volume;
73 static enum volume_flags {
74 VOL_UINT = 0,
75 VOL_PERCENT = 1,
76 VOL_LINEAR = 2,
77 VOL_DECIBEL = 3,
78 VOL_ABSOLUTE = 0 << 4,
79 VOL_RELATIVE = 1 << 4,
80 } volume_flags;
81
82 static enum mute_flags {
83 INVALID_MUTE = -1,
84 UNMUTE = 0,
85 MUTE = 1,
86 TOGGLE_MUTE = 2
87 } mute = INVALID_MUTE;
88
89 static pa_proplist *proplist = NULL;
90
91 static SNDFILE *sndfile = NULL;
92 static pa_stream *sample_stream = NULL;
93 static pa_sample_spec sample_spec;
94 static pa_channel_map channel_map;
95 static size_t sample_length = 0;
96 static int actions = 1;
97
98 static pa_bool_t nl = FALSE;
99
100 static enum {
101 NONE,
102 EXIT,
103 STAT,
104 INFO,
105 UPLOAD_SAMPLE,
106 PLAY_SAMPLE,
107 REMOVE_SAMPLE,
108 LIST,
109 MOVE_SINK_INPUT,
110 MOVE_SOURCE_OUTPUT,
111 LOAD_MODULE,
112 UNLOAD_MODULE,
113 SUSPEND_SINK,
114 SUSPEND_SOURCE,
115 SET_CARD_PROFILE,
116 SET_SINK_PORT,
117 SET_DEFAULT_SINK,
118 SET_SOURCE_PORT,
119 SET_DEFAULT_SOURCE,
120 SET_SINK_VOLUME,
121 SET_SOURCE_VOLUME,
122 SET_SINK_INPUT_VOLUME,
123 SET_SOURCE_OUTPUT_VOLUME,
124 SET_SINK_MUTE,
125 SET_SOURCE_MUTE,
126 SET_SINK_INPUT_MUTE,
127 SET_SOURCE_OUTPUT_MUTE,
128 SET_SINK_FORMATS,
129 SET_PORT_LATENCY_OFFSET,
130 SUBSCRIBE
131 } action = NONE;
132
133 static void quit(int ret) {
134 pa_assert(mainloop_api);
135 mainloop_api->quit(mainloop_api, ret);
136 }
137
138 static void context_drain_complete(pa_context *c, void *userdata) {
139 pa_context_disconnect(c);
140 }
141
142 static void drain(void) {
143 pa_operation *o;
144
145 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
146 pa_context_disconnect(context);
147 else
148 pa_operation_unref(o);
149 }
150
151 static void complete_action(void) {
152 pa_assert(actions > 0);
153
154 if (!(--actions))
155 drain();
156 }
157
158 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
159 char s[PA_BYTES_SNPRINT_MAX];
160 if (!i) {
161 pa_log(_("Failed to get statistics: %s"), pa_strerror(pa_context_errno(c)));
162 quit(1);
163 return;
164 }
165
166 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
167 printf(_("Currently in use: %u blocks containing %s bytes total.\n"), i->memblock_total, s);
168
169 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
170 printf(_("Allocated during whole lifetime: %u blocks containing %s bytes total.\n"), i->memblock_allocated, s);
171
172 pa_bytes_snprint(s, sizeof(s), i->scache_size);
173 printf(_("Sample cache size: %s\n"), s);
174
175 complete_action();
176 }
177
178 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
179 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
180
181 if (!i) {
182 pa_log(_("Failed to get server information: %s"), pa_strerror(pa_context_errno(c)));
183 quit(1);
184 return;
185 }
186
187 printf(_("Server String: %s\n"
188 "Library Protocol Version: %u\n"
189 "Server Protocol Version: %u\n"
190 "Is Local: %s\n"
191 "Client Index: %u\n"
192 "Tile Size: %zu\n"),
193 pa_context_get_server(c),
194 pa_context_get_protocol_version(c),
195 pa_context_get_server_protocol_version(c),
196 pa_yes_no(pa_context_is_local(c)),
197 pa_context_get_index(c),
198 pa_context_get_tile_size(c, NULL));
199
200 pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec);
201 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map);
202
203 printf(_("User Name: %s\n"
204 "Host Name: %s\n"
205 "Server Name: %s\n"
206 "Server Version: %s\n"
207 "Default Sample Specification: %s\n"
208 "Default Channel Map: %s\n"
209 "Default Sink: %s\n"
210 "Default Source: %s\n"
211 "Cookie: %04x:%04x\n"),
212 i->user_name,
213 i->host_name,
214 i->server_name,
215 i->server_version,
216 ss,
217 cm,
218 i->default_sink_name,
219 i->default_source_name,
220 i->cookie >> 16,
221 i->cookie & 0xFFFFU);
222
223 complete_action();
224 }
225
226 static const char* get_available_str_ynonly(int available)
227 {
228 switch (available) {
229 case PA_PORT_AVAILABLE_YES: return ", available";
230 case PA_PORT_AVAILABLE_NO: return ", not available";
231 }
232 return "";
233 }
234
235 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
236
237 static const char *state_table[] = {
238 [1+PA_SINK_INVALID_STATE] = "n/a",
239 [1+PA_SINK_RUNNING] = "RUNNING",
240 [1+PA_SINK_IDLE] = "IDLE",
241 [1+PA_SINK_SUSPENDED] = "SUSPENDED"
242 };
243
244 char
245 s[PA_SAMPLE_SPEC_SNPRINT_MAX],
246 cv[PA_CVOLUME_SNPRINT_MAX],
247 cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX],
248 v[PA_VOLUME_SNPRINT_MAX],
249 vdb[PA_SW_VOLUME_SNPRINT_DB_MAX],
250 cm[PA_CHANNEL_MAP_SNPRINT_MAX],
251 f[PA_FORMAT_INFO_SNPRINT_MAX];
252 char *pl;
253
254 if (is_last < 0) {
255 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
256 quit(1);
257 return;
258 }
259
260 if (is_last) {
261 complete_action();
262 return;
263 }
264
265 pa_assert(i);
266
267 if (nl && !short_list_format)
268 printf("\n");
269 nl = TRUE;
270
271 if (short_list_format) {
272 printf("%u\t%s\t%s\t%s\t%s\n",
273 i->index,
274 i->name,
275 pa_strnull(i->driver),
276 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
277 state_table[1+i->state]);
278 return;
279 }
280
281 printf(_("Sink #%u\n"
282 "\tState: %s\n"
283 "\tName: %s\n"
284 "\tDescription: %s\n"
285 "\tDriver: %s\n"
286 "\tSample Specification: %s\n"
287 "\tChannel Map: %s\n"
288 "\tOwner Module: %u\n"
289 "\tMute: %s\n"
290 "\tVolume: %s%s%s\n"
291 "\t balance %0.2f\n"
292 "\tBase Volume: %s%s%s\n"
293 "\tMonitor Source: %s\n"
294 "\tLatency: %0.0f usec, configured %0.0f usec\n"
295 "\tFlags: %s%s%s%s%s%s%s\n"
296 "\tProperties:\n\t\t%s\n"),
297 i->index,
298 state_table[1+i->state],
299 i->name,
300 pa_strnull(i->description),
301 pa_strnull(i->driver),
302 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
303 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
304 i->owner_module,
305 pa_yes_no(i->mute),
306 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
307 i->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t " : "",
308 i->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume) : "",
309 pa_cvolume_get_balance(&i->volume, &i->channel_map),
310 pa_volume_snprint(v, sizeof(v), i->base_volume),
311 i->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t " : "",
312 i->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), i->base_volume) : "",
313 pa_strnull(i->monitor_source_name),
314 (double) i->latency, (double) i->configured_latency,
315 i->flags & PA_SINK_HARDWARE ? "HARDWARE " : "",
316 i->flags & PA_SINK_NETWORK ? "NETWORK " : "",
317 i->flags & PA_SINK_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
318 i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
319 i->flags & PA_SINK_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
320 i->flags & PA_SINK_LATENCY ? "LATENCY " : "",
321 i->flags & PA_SINK_SET_FORMATS ? "SET_FORMATS " : "",
322 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
323
324 pa_xfree(pl);
325
326 if (i->ports) {
327 pa_sink_port_info **p;
328
329 printf(_("\tPorts:\n"));
330 for (p = i->ports; *p; p++)
331 printf("\t\t%s: %s (priority: %u%s)\n", (*p)->name, (*p)->description,
332 (*p)->priority, get_available_str_ynonly((*p)->available));
333 }
334
335 if (i->active_port)
336 printf(_("\tActive Port: %s\n"),
337 i->active_port->name);
338
339 if (i->formats) {
340 uint8_t j;
341
342 printf(_("\tFormats:\n"));
343 for (j = 0; j < i->n_formats; j++)
344 printf("\t\t%s\n", pa_format_info_snprint(f, sizeof(f), i->formats[j]));
345 }
346 }
347
348 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
349
350 static const char *state_table[] = {
351 [1+PA_SOURCE_INVALID_STATE] = "n/a",
352 [1+PA_SOURCE_RUNNING] = "RUNNING",
353 [1+PA_SOURCE_IDLE] = "IDLE",
354 [1+PA_SOURCE_SUSPENDED] = "SUSPENDED"
355 };
356
357 char
358 s[PA_SAMPLE_SPEC_SNPRINT_MAX],
359 cv[PA_CVOLUME_SNPRINT_MAX],
360 cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX],
361 v[PA_VOLUME_SNPRINT_MAX],
362 vdb[PA_SW_VOLUME_SNPRINT_DB_MAX],
363 cm[PA_CHANNEL_MAP_SNPRINT_MAX],
364 f[PA_FORMAT_INFO_SNPRINT_MAX];
365 char *pl;
366
367 if (is_last < 0) {
368 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
369 quit(1);
370 return;
371 }
372
373 if (is_last) {
374 complete_action();
375 return;
376 }
377
378 pa_assert(i);
379
380 if (nl && !short_list_format)
381 printf("\n");
382 nl = TRUE;
383
384 if (short_list_format) {
385 printf("%u\t%s\t%s\t%s\t%s\n",
386 i->index,
387 i->name,
388 pa_strnull(i->driver),
389 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
390 state_table[1+i->state]);
391 return;
392 }
393
394 printf(_("Source #%u\n"
395 "\tState: %s\n"
396 "\tName: %s\n"
397 "\tDescription: %s\n"
398 "\tDriver: %s\n"
399 "\tSample Specification: %s\n"
400 "\tChannel Map: %s\n"
401 "\tOwner Module: %u\n"
402 "\tMute: %s\n"
403 "\tVolume: %s%s%s\n"
404 "\t balance %0.2f\n"
405 "\tBase Volume: %s%s%s\n"
406 "\tMonitor of Sink: %s\n"
407 "\tLatency: %0.0f usec, configured %0.0f usec\n"
408 "\tFlags: %s%s%s%s%s%s\n"
409 "\tProperties:\n\t\t%s\n"),
410 i->index,
411 state_table[1+i->state],
412 i->name,
413 pa_strnull(i->description),
414 pa_strnull(i->driver),
415 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
416 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
417 i->owner_module,
418 pa_yes_no(i->mute),
419 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
420 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "",
421 i->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume) : "",
422 pa_cvolume_get_balance(&i->volume, &i->channel_map),
423 pa_volume_snprint(v, sizeof(v), i->base_volume),
424 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "",
425 i->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), i->base_volume) : "",
426 i->monitor_of_sink_name ? i->monitor_of_sink_name : _("n/a"),
427 (double) i->latency, (double) i->configured_latency,
428 i->flags & PA_SOURCE_HARDWARE ? "HARDWARE " : "",
429 i->flags & PA_SOURCE_NETWORK ? "NETWORK " : "",
430 i->flags & PA_SOURCE_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
431 i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
432 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
433 i->flags & PA_SOURCE_LATENCY ? "LATENCY " : "",
434 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
435
436 pa_xfree(pl);
437
438 if (i->ports) {
439 pa_source_port_info **p;
440
441 printf(_("\tPorts:\n"));
442 for (p = i->ports; *p; p++)
443 printf("\t\t%s: %s (priority: %u%s)\n", (*p)->name, (*p)->description,
444 (*p)->priority, get_available_str_ynonly((*p)->available));
445 }
446
447 if (i->active_port)
448 printf(_("\tActive Port: %s\n"),
449 i->active_port->name);
450
451 if (i->formats) {
452 uint8_t j;
453
454 printf(_("\tFormats:\n"));
455 for (j = 0; j < i->n_formats; j++)
456 printf("\t\t%s\n", pa_format_info_snprint(f, sizeof(f), i->formats[j]));
457 }
458 }
459
460 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
461 char t[32];
462 char *pl;
463
464 if (is_last < 0) {
465 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
466 quit(1);
467 return;
468 }
469
470 if (is_last) {
471 complete_action();
472 return;
473 }
474
475 pa_assert(i);
476
477 if (nl && !short_list_format)
478 printf("\n");
479 nl = TRUE;
480
481 pa_snprintf(t, sizeof(t), "%u", i->n_used);
482
483 if (short_list_format) {
484 printf("%u\t%s\t%s\t\n", i->index, i->name, i->argument ? i->argument : "");
485 return;
486 }
487
488 printf(_("Module #%u\n"
489 "\tName: %s\n"
490 "\tArgument: %s\n"
491 "\tUsage counter: %s\n"
492 "\tProperties:\n\t\t%s\n"),
493 i->index,
494 i->name,
495 i->argument ? i->argument : "",
496 i->n_used != PA_INVALID_INDEX ? t : _("n/a"),
497 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
498
499 pa_xfree(pl);
500 }
501
502 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
503 char t[32];
504 char *pl;
505
506 if (is_last < 0) {
507 pa_log(_("Failed to get client information: %s"), pa_strerror(pa_context_errno(c)));
508 quit(1);
509 return;
510 }
511
512 if (is_last) {
513 complete_action();
514 return;
515 }
516
517 pa_assert(i);
518
519 if (nl && !short_list_format)
520 printf("\n");
521 nl = TRUE;
522
523 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
524
525 if (short_list_format) {
526 printf("%u\t%s\t%s\n",
527 i->index,
528 pa_strnull(i->driver),
529 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)));
530 return;
531 }
532
533 printf(_("Client #%u\n"
534 "\tDriver: %s\n"
535 "\tOwner Module: %s\n"
536 "\tProperties:\n\t\t%s\n"),
537 i->index,
538 pa_strnull(i->driver),
539 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
540 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
541
542 pa_xfree(pl);
543 }
544
545 static void get_card_info_callback(pa_context *c, const pa_card_info *i, int is_last, void *userdata) {
546 char t[32];
547 char *pl;
548
549 if (is_last < 0) {
550 pa_log(_("Failed to get card information: %s"), pa_strerror(pa_context_errno(c)));
551 complete_action();
552 return;
553 }
554
555 if (is_last) {
556 complete_action();
557 return;
558 }
559
560 pa_assert(i);
561
562 if (nl && !short_list_format)
563 printf("\n");
564 nl = TRUE;
565
566 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
567
568 if (short_list_format) {
569 printf("%u\t%s\t%s\n", i->index, i->name, pa_strnull(i->driver));
570 return;
571 }
572
573 printf(_("Card #%u\n"
574 "\tName: %s\n"
575 "\tDriver: %s\n"
576 "\tOwner Module: %s\n"
577 "\tProperties:\n\t\t%s\n"),
578 i->index,
579 i->name,
580 pa_strnull(i->driver),
581 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
582 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
583
584 pa_xfree(pl);
585
586 if (i->profiles) {
587 pa_card_profile_info *p;
588
589 printf(_("\tProfiles:\n"));
590 for (p = i->profiles; p->name; p++)
591 printf("\t\t%s: %s (sinks: %u, sources: %u, priority: %u)\n", p->name, p->description, p->n_sinks, p->n_sources, p->priority);
592 }
593
594 if (i->active_profile)
595 printf(_("\tActive Profile: %s\n"),
596 i->active_profile->name);
597
598 if (i->ports) {
599 pa_card_port_info **p;
600
601 printf(_("\tPorts:\n"));
602 for (p = i->ports; *p; p++) {
603 pa_card_profile_info **pr = (*p)->profiles;
604 printf("\t\t%s: %s (priority: %u, latency offset: %" PRId64 " usec%s)\n", (*p)->name,
605 (*p)->description, (*p)->priority, (*p)->latency_offset,
606 get_available_str_ynonly((*p)->available));
607
608 if (!pa_proplist_isempty((*p)->proplist)) {
609 printf(_("\t\t\tProperties:\n\t\t\t\t%s\n"), pl = pa_proplist_to_string_sep((*p)->proplist, "\n\t\t\t\t"));
610 pa_xfree(pl);
611 }
612
613 if (pr) {
614 printf(_("\t\t\tPart of profile(s): %s"), pa_strnull((*pr)->name));
615 pr++;
616 while (*pr) {
617 printf(", %s", pa_strnull((*pr)->name));
618 pr++;
619 }
620 printf("\n");
621 }
622 }
623 }
624 }
625
626 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
627 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], f[PA_FORMAT_INFO_SNPRINT_MAX];
628 char *pl;
629
630 if (is_last < 0) {
631 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
632 quit(1);
633 return;
634 }
635
636 if (is_last) {
637 complete_action();
638 return;
639 }
640
641 pa_assert(i);
642
643 if (nl && !short_list_format)
644 printf("\n");
645 nl = TRUE;
646
647 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
648 pa_snprintf(k, sizeof(k), "%u", i->client);
649
650 if (short_list_format) {
651 printf("%u\t%u\t%s\t%s\t%s\n",
652 i->index,
653 i->sink,
654 i->client != PA_INVALID_INDEX ? k : "-",
655 pa_strnull(i->driver),
656 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec));
657 return;
658 }
659
660 printf(_("Sink Input #%u\n"
661 "\tDriver: %s\n"
662 "\tOwner Module: %s\n"
663 "\tClient: %s\n"
664 "\tSink: %u\n"
665 "\tSample Specification: %s\n"
666 "\tChannel Map: %s\n"
667 "\tFormat: %s\n"
668 "\tCorked: %s\n"
669 "\tMute: %s\n"
670 "\tVolume: %s\n"
671 "\t %s\n"
672 "\t balance %0.2f\n"
673 "\tBuffer Latency: %0.0f usec\n"
674 "\tSink Latency: %0.0f usec\n"
675 "\tResample method: %s\n"
676 "\tProperties:\n\t\t%s\n"),
677 i->index,
678 pa_strnull(i->driver),
679 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
680 i->client != PA_INVALID_INDEX ? k : _("n/a"),
681 i->sink,
682 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
683 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
684 pa_format_info_snprint(f, sizeof(f), i->format),
685 pa_yes_no(i->corked),
686 pa_yes_no(i->mute),
687 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
688 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
689 pa_cvolume_get_balance(&i->volume, &i->channel_map),
690 (double) i->buffer_usec,
691 (double) i->sink_usec,
692 i->resample_method ? i->resample_method : _("n/a"),
693 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
694
695 pa_xfree(pl);
696 }
697
698 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
699 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], f[PA_FORMAT_INFO_SNPRINT_MAX];
700 char *pl;
701
702 if (is_last < 0) {
703 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
704 quit(1);
705 return;
706 }
707
708 if (is_last) {
709 complete_action();
710 return;
711 }
712
713 pa_assert(i);
714
715 if (nl && !short_list_format)
716 printf("\n");
717 nl = TRUE;
718
719
720 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
721 pa_snprintf(k, sizeof(k), "%u", i->client);
722
723 if (short_list_format) {
724 printf("%u\t%u\t%s\t%s\t%s\n",
725 i->index,
726 i->source,
727 i->client != PA_INVALID_INDEX ? k : "-",
728 pa_strnull(i->driver),
729 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec));
730 return;
731 }
732
733 printf(_("Source Output #%u\n"
734 "\tDriver: %s\n"
735 "\tOwner Module: %s\n"
736 "\tClient: %s\n"
737 "\tSource: %u\n"
738 "\tSample Specification: %s\n"
739 "\tChannel Map: %s\n"
740 "\tFormat: %s\n"
741 "\tCorked: %s\n"
742 "\tMute: %s\n"
743 "\tVolume: %s\n"
744 "\t %s\n"
745 "\t balance %0.2f\n"
746 "\tBuffer Latency: %0.0f usec\n"
747 "\tSource Latency: %0.0f usec\n"
748 "\tResample method: %s\n"
749 "\tProperties:\n\t\t%s\n"),
750 i->index,
751 pa_strnull(i->driver),
752 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
753 i->client != PA_INVALID_INDEX ? k : _("n/a"),
754 i->source,
755 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
756 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
757 pa_format_info_snprint(f, sizeof(f), i->format),
758 pa_yes_no(i->corked),
759 pa_yes_no(i->mute),
760 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
761 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
762 pa_cvolume_get_balance(&i->volume, &i->channel_map),
763 (double) i->buffer_usec,
764 (double) i->source_usec,
765 i->resample_method ? i->resample_method : _("n/a"),
766 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
767
768 pa_xfree(pl);
769 }
770
771 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
772 char t[PA_BYTES_SNPRINT_MAX], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
773 char *pl;
774
775 if (is_last < 0) {
776 pa_log(_("Failed to get sample information: %s"), pa_strerror(pa_context_errno(c)));
777 quit(1);
778 return;
779 }
780
781 if (is_last) {
782 complete_action();
783 return;
784 }
785
786 pa_assert(i);
787
788 if (nl && !short_list_format)
789 printf("\n");
790 nl = TRUE;
791
792 pa_bytes_snprint(t, sizeof(t), i->bytes);
793
794 if (short_list_format) {
795 printf("%u\t%s\t%s\t%0.3f\n",
796 i->index,
797 i->name,
798 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : "-",
799 (double) i->duration/1000000.0);
800 return;
801 }
802
803 printf(_("Sample #%u\n"
804 "\tName: %s\n"
805 "\tSample Specification: %s\n"
806 "\tChannel Map: %s\n"
807 "\tVolume: %s\n"
808 "\t %s\n"
809 "\t balance %0.2f\n"
810 "\tDuration: %0.1fs\n"
811 "\tSize: %s\n"
812 "\tLazy: %s\n"
813 "\tFilename: %s\n"
814 "\tProperties:\n\t\t%s\n"),
815 i->index,
816 i->name,
817 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : _("n/a"),
818 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : _("n/a"),
819 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
820 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
821 pa_cvolume_get_balance(&i->volume, &i->channel_map),
822 (double) i->duration/1000000.0,
823 t,
824 pa_yes_no(i->lazy),
825 i->filename ? i->filename : _("n/a"),
826 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
827
828 pa_xfree(pl);
829 }
830
831 static void simple_callback(pa_context *c, int success, void *userdata) {
832 if (!success) {
833 pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
834 quit(1);
835 return;
836 }
837
838 complete_action();
839 }
840
841 static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
842 if (idx == PA_INVALID_INDEX) {
843 pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
844 quit(1);
845 return;
846 }
847
848 printf("%u\n", idx);
849
850 complete_action();
851 }
852
853 static void volume_relative_adjust(pa_cvolume *cv) {
854 pa_assert((volume_flags & VOL_RELATIVE) == VOL_RELATIVE);
855
856 /* Relative volume change is additive in case of UINT or PERCENT
857 * and multiplicative for LINEAR or DECIBEL */
858 if ((volume_flags & 0x0F) == VOL_UINT || (volume_flags & 0x0F) == VOL_PERCENT) {
859 pa_volume_t v = pa_cvolume_avg(cv);
860 v = v + volume < PA_VOLUME_NORM ? PA_VOLUME_MUTED : v + volume - PA_VOLUME_NORM;
861 pa_cvolume_set(cv, 1, v);
862 }
863 if ((volume_flags & 0x0F) == VOL_LINEAR || (volume_flags & 0x0F) == VOL_DECIBEL) {
864 pa_sw_cvolume_multiply_scalar(cv, cv, volume);
865 }
866 }
867
868 static void unload_module_by_name_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
869 static pa_bool_t unloaded = FALSE;
870
871 if (is_last < 0) {
872 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
873 quit(1);
874 return;
875 }
876
877 if (is_last) {
878 if (unloaded == FALSE)
879 pa_log(_("Failed to unload module: Module %s not loaded"), module_name);
880 complete_action();
881 return;
882 }
883
884 pa_assert(i);
885
886 if (pa_streq(module_name, i->name)) {
887 unloaded = TRUE;
888 actions++;
889 pa_operation_unref(pa_context_unload_module(c, i->index, simple_callback, NULL));
890 }
891 }
892
893 static void get_sink_volume_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
894 pa_cvolume cv;
895
896 if (is_last < 0) {
897 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
898 quit(1);
899 return;
900 }
901
902 if (is_last)
903 return;
904
905 pa_assert(i);
906
907 cv = i->volume;
908 volume_relative_adjust(&cv);
909 pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &cv, simple_callback, NULL));
910 }
911
912 static void get_source_volume_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
913 pa_cvolume cv;
914
915 if (is_last < 0) {
916 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
917 quit(1);
918 return;
919 }
920
921 if (is_last)
922 return;
923
924 pa_assert(i);
925
926 cv = i->volume;
927 volume_relative_adjust(&cv);
928 pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &cv, simple_callback, NULL));
929 }
930
931 static void get_sink_input_volume_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
932 pa_cvolume cv;
933
934 if (is_last < 0) {
935 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
936 quit(1);
937 return;
938 }
939
940 if (is_last)
941 return;
942
943 pa_assert(i);
944
945 cv = i->volume;
946 volume_relative_adjust(&cv);
947 pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &cv, simple_callback, NULL));
948 }
949
950 static void get_source_output_volume_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
951 pa_cvolume cv;
952
953 if (is_last < 0) {
954 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
955 quit(1);
956 return;
957 }
958
959 if (is_last)
960 return;
961
962 pa_assert(o);
963
964 cv = o->volume;
965 volume_relative_adjust(&cv);
966 pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &cv, simple_callback, NULL));
967 }
968
969 static void sink_toggle_mute_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
970 if (is_last < 0) {
971 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
972 quit(1);
973 return;
974 }
975
976 if (is_last)
977 return;
978
979 pa_assert(i);
980
981 pa_operation_unref(pa_context_set_sink_mute_by_name(c, i->name, !i->mute, simple_callback, NULL));
982 }
983
984 static void source_toggle_mute_callback(pa_context *c, const pa_source_info *o, int is_last, void *userdata) {
985 if (is_last < 0) {
986 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
987 quit(1);
988 return;
989 }
990
991 if (is_last)
992 return;
993
994 pa_assert(o);
995
996 pa_operation_unref(pa_context_set_source_mute_by_name(c, o->name, !o->mute, simple_callback, NULL));
997 }
998
999 static void sink_input_toggle_mute_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
1000 if (is_last < 0) {
1001 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
1002 quit(1);
1003 return;
1004 }
1005
1006 if (is_last)
1007 return;
1008
1009 pa_assert(i);
1010
1011 pa_operation_unref(pa_context_set_sink_input_mute(c, i->index, !i->mute, simple_callback, NULL));
1012 }
1013
1014 static void source_output_toggle_mute_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
1015 if (is_last < 0) {
1016 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
1017 quit(1);
1018 return;
1019 }
1020
1021 if (is_last)
1022 return;
1023
1024 pa_assert(o);
1025
1026 pa_operation_unref(pa_context_set_source_output_mute(c, o->index, !o->mute, simple_callback, NULL));
1027 }
1028
1029 /* PA_MAX_FORMATS is defined in internal.h so we just define a sane value here */
1030 #define MAX_FORMATS 256
1031
1032 static void set_sink_formats(pa_context *c, uint32_t sink, const char *str) {
1033 pa_format_info *f_arr[MAX_FORMATS];
1034 char *format = NULL;
1035 const char *state = NULL;
1036 int i = 0;
1037
1038 while ((format = pa_split(str, ";", &state))) {
1039 pa_format_info *f = pa_format_info_from_string(pa_strip(format));
1040
1041 if (!f) {
1042 pa_log(_("Failed to set format: invalid format string %s"), format);
1043 goto error;
1044 }
1045
1046 f_arr[i++] = f;
1047 pa_xfree(format);
1048 }
1049
1050 pa_operation_unref(pa_ext_device_restore_save_formats(c, PA_DEVICE_TYPE_SINK, sink, i, f_arr, simple_callback, NULL));
1051
1052 done:
1053 if (format)
1054 pa_xfree(format);
1055 while(i--)
1056 pa_format_info_free(f_arr[i]);
1057
1058 return;
1059
1060 error:
1061 while(i--)
1062 pa_format_info_free(f_arr[i]);
1063 quit(1);
1064 goto done;
1065 }
1066
1067 static void stream_state_callback(pa_stream *s, void *userdata) {
1068 pa_assert(s);
1069
1070 switch (pa_stream_get_state(s)) {
1071 case PA_STREAM_CREATING:
1072 case PA_STREAM_READY:
1073 break;
1074
1075 case PA_STREAM_TERMINATED:
1076 drain();
1077 break;
1078
1079 case PA_STREAM_FAILED:
1080 default:
1081 pa_log(_("Failed to upload sample: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
1082 quit(1);
1083 }
1084 }
1085
1086 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
1087 sf_count_t l;
1088 float *d;
1089 pa_assert(s && length && sndfile);
1090
1091 d = pa_xmalloc(length);
1092
1093 pa_assert(sample_length >= length);
1094 l = (sf_count_t) (length/pa_frame_size(&sample_spec));
1095
1096 if ((sf_readf_float(sndfile, d, l)) != l) {
1097 pa_xfree(d);
1098 pa_log(_("Premature end of file"));
1099 quit(1);
1100 return;
1101 }
1102
1103 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
1104
1105 sample_length -= length;
1106
1107 if (sample_length <= 0) {
1108 pa_stream_set_write_callback(sample_stream, NULL, NULL);
1109 pa_stream_finish_upload(sample_stream);
1110 }
1111 }
1112
1113 static const char *subscription_event_type_to_string(pa_subscription_event_type_t t) {
1114
1115 switch (t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
1116
1117 case PA_SUBSCRIPTION_EVENT_NEW:
1118 return _("new");
1119
1120 case PA_SUBSCRIPTION_EVENT_CHANGE:
1121 return _("change");
1122
1123 case PA_SUBSCRIPTION_EVENT_REMOVE:
1124 return _("remove");
1125 }
1126
1127 return _("unknown");
1128 }
1129
1130 static const char *subscription_event_facility_to_string(pa_subscription_event_type_t t) {
1131
1132 switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
1133
1134 case PA_SUBSCRIPTION_EVENT_SINK:
1135 return _("sink");
1136
1137 case PA_SUBSCRIPTION_EVENT_SOURCE:
1138 return _("source");
1139
1140 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
1141 return _("sink-input");
1142
1143 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
1144 return _("source-output");
1145
1146 case PA_SUBSCRIPTION_EVENT_MODULE:
1147 return _("module");
1148
1149 case PA_SUBSCRIPTION_EVENT_CLIENT:
1150 return _("client");
1151
1152 case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE:
1153 return _("sample-cache");
1154
1155 case PA_SUBSCRIPTION_EVENT_SERVER:
1156 return _("server");
1157
1158 case PA_SUBSCRIPTION_EVENT_CARD:
1159 return _("server");
1160 }
1161
1162 return _("unknown");
1163 }
1164
1165 static void context_subscribe_callback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1166 pa_assert(c);
1167
1168 printf(_("Event '%s' on %s #%u\n"),
1169 subscription_event_type_to_string(t),
1170 subscription_event_facility_to_string(t),
1171 idx);
1172 fflush(stdout);
1173 }
1174
1175 static void context_state_callback(pa_context *c, void *userdata) {
1176 pa_assert(c);
1177 switch (pa_context_get_state(c)) {
1178 case PA_CONTEXT_CONNECTING:
1179 case PA_CONTEXT_AUTHORIZING:
1180 case PA_CONTEXT_SETTING_NAME:
1181 break;
1182
1183 case PA_CONTEXT_READY:
1184 switch (action) {
1185 case STAT:
1186 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
1187 if (short_list_format)
1188 break;
1189 actions++;
1190
1191 case INFO:
1192 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
1193 break;
1194
1195 case PLAY_SAMPLE:
1196 pa_operation_unref(pa_context_play_sample(c, sample_name, sink_name, PA_VOLUME_NORM, simple_callback, NULL));
1197 break;
1198
1199 case REMOVE_SAMPLE:
1200 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
1201 break;
1202
1203 case UPLOAD_SAMPLE:
1204 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
1205 pa_assert(sample_stream);
1206
1207 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
1208 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
1209 pa_stream_connect_upload(sample_stream, sample_length);
1210 break;
1211
1212 case EXIT:
1213 pa_operation_unref(pa_context_exit_daemon(c, simple_callback, NULL));
1214 break;
1215
1216 case LIST:
1217 if (list_type) {
1218 if (pa_streq(list_type, "modules"))
1219 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
1220 else if (pa_streq(list_type, "sinks"))
1221 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
1222 else if (pa_streq(list_type, "sources"))
1223 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
1224 else if (pa_streq(list_type, "sink-inputs"))
1225 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
1226 else if (pa_streq(list_type, "source-outputs"))
1227 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
1228 else if (pa_streq(list_type, "clients"))
1229 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
1230 else if (pa_streq(list_type, "samples"))
1231 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
1232 else if (pa_streq(list_type, "cards"))
1233 pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL));
1234 else
1235 pa_assert_not_reached();
1236 } else {
1237 actions = 8;
1238 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
1239 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
1240 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
1241 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
1242 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
1243 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
1244 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
1245 pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL));
1246 }
1247 break;
1248
1249 case MOVE_SINK_INPUT:
1250 pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL));
1251 break;
1252
1253 case MOVE_SOURCE_OUTPUT:
1254 pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL));
1255 break;
1256
1257 case LOAD_MODULE:
1258 pa_operation_unref(pa_context_load_module(c, module_name, module_args, index_callback, NULL));
1259 break;
1260
1261 case UNLOAD_MODULE:
1262 if (module_name)
1263 pa_operation_unref(pa_context_get_module_info_list(c, unload_module_by_name_callback, NULL));
1264 else
1265 pa_operation_unref(pa_context_unload_module(c, module_index, simple_callback, NULL));
1266 break;
1267
1268 case SUSPEND_SINK:
1269 if (sink_name)
1270 pa_operation_unref(pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL));
1271 else
1272 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
1273 break;
1274
1275 case SUSPEND_SOURCE:
1276 if (source_name)
1277 pa_operation_unref(pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL));
1278 else
1279 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
1280 break;
1281
1282 case SET_CARD_PROFILE:
1283 pa_operation_unref(pa_context_set_card_profile_by_name(c, card_name, profile_name, simple_callback, NULL));
1284 break;
1285
1286 case SET_SINK_PORT:
1287 pa_operation_unref(pa_context_set_sink_port_by_name(c, sink_name, port_name, simple_callback, NULL));
1288 break;
1289
1290 case SET_DEFAULT_SINK:
1291 pa_operation_unref(pa_context_set_default_sink(c, sink_name, simple_callback, NULL));
1292 break;
1293
1294 case SET_SOURCE_PORT:
1295 pa_operation_unref(pa_context_set_source_port_by_name(c, source_name, port_name, simple_callback, NULL));
1296 break;
1297
1298 case SET_DEFAULT_SOURCE:
1299 pa_operation_unref(pa_context_set_default_source(c, source_name, simple_callback, NULL));
1300 break;
1301
1302 case SET_SINK_MUTE:
1303 if (mute == TOGGLE_MUTE)
1304 pa_operation_unref(pa_context_get_sink_info_by_name(c, sink_name, sink_toggle_mute_callback, NULL));
1305 else
1306 pa_operation_unref(pa_context_set_sink_mute_by_name(c, sink_name, mute, simple_callback, NULL));
1307 break;
1308
1309 case SET_SOURCE_MUTE:
1310 if (mute == TOGGLE_MUTE)
1311 pa_operation_unref(pa_context_get_source_info_by_name(c, source_name, source_toggle_mute_callback, NULL));
1312 else
1313 pa_operation_unref(pa_context_set_source_mute_by_name(c, source_name, mute, simple_callback, NULL));
1314 break;
1315
1316 case SET_SINK_INPUT_MUTE:
1317 if (mute == TOGGLE_MUTE)
1318 pa_operation_unref(pa_context_get_sink_input_info(c, sink_input_idx, sink_input_toggle_mute_callback, NULL));
1319 else
1320 pa_operation_unref(pa_context_set_sink_input_mute(c, sink_input_idx, mute, simple_callback, NULL));
1321 break;
1322
1323 case SET_SOURCE_OUTPUT_MUTE:
1324 if (mute == TOGGLE_MUTE)
1325 pa_operation_unref(pa_context_get_source_output_info(c, source_output_idx, source_output_toggle_mute_callback, NULL));
1326 else
1327 pa_operation_unref(pa_context_set_source_output_mute(c, source_output_idx, mute, simple_callback, NULL));
1328 break;
1329
1330 case SET_SINK_VOLUME:
1331 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1332 pa_operation_unref(pa_context_get_sink_info_by_name(c, sink_name, get_sink_volume_callback, NULL));
1333 } else {
1334 pa_cvolume v;
1335 pa_cvolume_set(&v, 1, volume);
1336 pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &v, simple_callback, NULL));
1337 }
1338 break;
1339
1340 case SET_SOURCE_VOLUME:
1341 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1342 pa_operation_unref(pa_context_get_source_info_by_name(c, source_name, get_source_volume_callback, NULL));
1343 } else {
1344 pa_cvolume v;
1345 pa_cvolume_set(&v, 1, volume);
1346 pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &v, simple_callback, NULL));
1347 }
1348 break;
1349
1350 case SET_SINK_INPUT_VOLUME:
1351 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1352 pa_operation_unref(pa_context_get_sink_input_info(c, sink_input_idx, get_sink_input_volume_callback, NULL));
1353 } else {
1354 pa_cvolume v;
1355 pa_cvolume_set(&v, 1, volume);
1356 pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &v, simple_callback, NULL));
1357 }
1358 break;
1359
1360 case SET_SOURCE_OUTPUT_VOLUME:
1361 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1362 pa_operation_unref(pa_context_get_source_output_info(c, source_output_idx, get_source_output_volume_callback, NULL));
1363 } else {
1364 pa_cvolume v;
1365 pa_cvolume_set(&v, 1, volume);
1366 pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &v, simple_callback, NULL));
1367 }
1368 break;
1369
1370 case SET_SINK_FORMATS:
1371 set_sink_formats(c, sink_idx, formats);
1372 break;
1373
1374 case SET_PORT_LATENCY_OFFSET:
1375 pa_operation_unref(pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL));
1376 break;
1377
1378 case SUBSCRIBE:
1379 pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL);
1380
1381 pa_operation_unref(pa_context_subscribe(
1382 c,
1383 PA_SUBSCRIPTION_MASK_SINK|
1384 PA_SUBSCRIPTION_MASK_SOURCE|
1385 PA_SUBSCRIPTION_MASK_SINK_INPUT|
1386 PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT|
1387 PA_SUBSCRIPTION_MASK_MODULE|
1388 PA_SUBSCRIPTION_MASK_CLIENT|
1389 PA_SUBSCRIPTION_MASK_SAMPLE_CACHE|
1390 PA_SUBSCRIPTION_MASK_SERVER|
1391 PA_SUBSCRIPTION_MASK_CARD,
1392 NULL,
1393 NULL));
1394 break;
1395
1396 default:
1397 pa_assert_not_reached();
1398 }
1399 break;
1400
1401 case PA_CONTEXT_TERMINATED:
1402 quit(0);
1403 break;
1404
1405 case PA_CONTEXT_FAILED:
1406 default:
1407 pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c)));
1408 quit(1);
1409 }
1410 }
1411
1412 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
1413 pa_log(_("Got SIGINT, exiting."));
1414 quit(0);
1415 }
1416
1417 static int parse_volume(const char *vol_spec, pa_volume_t *vol, enum volume_flags *vol_flags) {
1418 double v;
1419 char *vs;
1420
1421 pa_assert(vol_spec);
1422 pa_assert(vol);
1423 pa_assert(vol_flags);
1424
1425 vs = pa_xstrdup(vol_spec);
1426
1427 *vol_flags = (pa_startswith(vs, "+") || pa_startswith(vs, "-")) ? VOL_RELATIVE : VOL_ABSOLUTE;
1428 if (strchr(vs, '.'))
1429 *vol_flags |= VOL_LINEAR;
1430 if (pa_endswith(vs, "%")) {
1431 *vol_flags |= VOL_PERCENT;
1432 vs[strlen(vs)-1] = 0;
1433 }
1434 if (pa_endswith(vs, "db") || pa_endswith(vs, "dB")) {
1435 *vol_flags |= VOL_DECIBEL;
1436 vs[strlen(vs)-2] = 0;
1437 }
1438
1439 if (pa_atod(vs, &v) < 0) {
1440 pa_log(_("Invalid volume specification"));
1441 pa_xfree(vs);
1442 return -1;
1443 }
1444
1445 pa_xfree(vs);
1446
1447 if ((*vol_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1448 if ((*vol_flags & 0x0F) == VOL_UINT)
1449 v += (double) PA_VOLUME_NORM;
1450 if ((*vol_flags & 0x0F) == VOL_PERCENT)
1451 v += 100.0;
1452 if ((*vol_flags & 0x0F) == VOL_LINEAR)
1453 v += 1.0;
1454 }
1455 if ((*vol_flags & 0x0F) == VOL_PERCENT)
1456 v = v * (double) PA_VOLUME_NORM / 100;
1457 if ((*vol_flags & 0x0F) == VOL_LINEAR)
1458 v = pa_sw_volume_from_linear(v);
1459 if ((*vol_flags & 0x0F) == VOL_DECIBEL)
1460 v = pa_sw_volume_from_dB(v);
1461
1462 if (!PA_VOLUME_IS_VALID((pa_volume_t) v)) {
1463 pa_log(_("Volume outside permissible range.\n"));
1464 return -1;
1465 }
1466
1467 *vol = (pa_volume_t) v;
1468
1469 return 0;
1470 }
1471
1472 static enum mute_flags parse_mute(const char *mute_text) {
1473 int b;
1474
1475 pa_assert(mute_text);
1476
1477 if (pa_streq("toggle", mute_text))
1478 return TOGGLE_MUTE;
1479
1480 b = pa_parse_boolean(mute_text);
1481 switch (b) {
1482 case 0:
1483 return UNMUTE;
1484 case 1:
1485 return MUTE;
1486 default:
1487 return INVALID_MUTE;
1488 }
1489 }
1490
1491 static void help(const char *argv0) {
1492
1493 printf("%s %s %s\n", argv0, _("[options]"), "stat [short]");
1494 printf("%s %s %s\n", argv0, _("[options]"), "info");
1495 printf("%s %s %s %s\n", argv0, _("[options]"), "list [short]", _("[TYPE]"));
1496 printf("%s %s %s\n", argv0, _("[options]"), "exit");
1497 printf("%s %s %s %s\n", argv0, _("[options]"), "upload-sample", _("FILENAME [NAME]"));
1498 printf("%s %s %s %s\n", argv0, _("[options]"), "play-sample ", _("NAME [SINK]"));
1499 printf("%s %s %s %s\n", argv0, _("[options]"), "remove-sample ", _("NAME"));
1500 printf("%s %s %s %s\n", argv0, _("[options]"), "load-module ", _("NAME [ARGS ...]"));
1501 printf("%s %s %s %s\n", argv0, _("[options]"), "unload-module ", _("NAME|#N"));
1502 printf("%s %s %s %s\n", argv0, _("[options]"), "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
1503 printf("%s %s %s %s\n", argv0, _("[options]"), "suspend-(sink|source)", _("NAME|#N 1|0"));
1504 printf("%s %s %s %s\n", argv0, _("[options]"), "set-card-profile ", _("CARD PROFILE"));
1505 printf("%s %s %s %s\n", argv0, _("[options]"), "set-default-(sink|source)", _("NAME"));
1506 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-port", _("NAME|#N PORT"));
1507 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-volume", _("NAME|#N VOLUME"));
1508 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-volume", _("#N VOLUME"));
1509 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-mute", _("NAME|#N 1|0|toggle"));
1510 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0|toggle"));
1511 printf("%s %s %s %s\n", argv0, _("[options]"), "set-sink-formats", _("#N FORMATS"));
1512 printf("%s %s %s %s\n", argv0, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
1513 printf("%s %s %s\n", argv0, _("[options]"), "subscribe");
1514 printf(_("\nThe special names @DEFAULT_SINK@, @DEFAULT_SOURCE@ and @DEFAULT_MONITOR@\n"
1515 "can be used to specify the default sink, source and monitor.\n"));
1516
1517 printf(_("\n"
1518 " -h, --help Show this help\n"
1519 " --version Show version\n\n"
1520 " -s, --server=SERVER The name of the server to connect to\n"
1521 " -n, --client-name=NAME How to call this client on the server\n"));
1522 }
1523
1524 enum {
1525 ARG_VERSION = 256
1526 };
1527
1528 int main(int argc, char *argv[]) {
1529 pa_mainloop *m = NULL;
1530 int ret = 1, c;
1531 char *server = NULL, *bn;
1532
1533 static const struct option long_options[] = {
1534 {"server", 1, NULL, 's'},
1535 {"client-name", 1, NULL, 'n'},
1536 {"version", 0, NULL, ARG_VERSION},
1537 {"help", 0, NULL, 'h'},
1538 {NULL, 0, NULL, 0}
1539 };
1540
1541 setlocale(LC_ALL, "");
1542 #ifdef ENABLE_NLS
1543 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
1544 #endif
1545
1546 bn = pa_path_get_filename(argv[0]);
1547
1548 proplist = pa_proplist_new();
1549
1550 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
1551 switch (c) {
1552 case 'h' :
1553 help(bn);
1554 ret = 0;
1555 goto quit;
1556
1557 case ARG_VERSION:
1558 printf(_("pactl %s\n"
1559 "Compiled with libpulse %s\n"
1560 "Linked with libpulse %s\n"),
1561 PACKAGE_VERSION,
1562 pa_get_headers_version(),
1563 pa_get_library_version());
1564 ret = 0;
1565 goto quit;
1566
1567 case 's':
1568 pa_xfree(server);
1569 server = pa_xstrdup(optarg);
1570 break;
1571
1572 case 'n': {
1573 char *t;
1574
1575 if (!(t = pa_locale_to_utf8(optarg)) ||
1576 pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t) < 0) {
1577
1578 pa_log(_("Invalid client name '%s'"), t ? t : optarg);
1579 pa_xfree(t);
1580 goto quit;
1581 }
1582
1583 pa_xfree(t);
1584 break;
1585 }
1586
1587 default:
1588 goto quit;
1589 }
1590 }
1591
1592 if (optind < argc) {
1593 if (pa_streq(argv[optind], "stat")) {
1594 action = STAT;
1595 short_list_format = FALSE;
1596 if (optind+1 < argc && pa_streq(argv[optind+1], "short"))
1597 short_list_format = TRUE;
1598
1599 } else if (pa_streq(argv[optind], "info"))
1600 action = INFO;
1601
1602 else if (pa_streq(argv[optind], "exit"))
1603 action = EXIT;
1604
1605 else if (pa_streq(argv[optind], "list")) {
1606 action = LIST;
1607
1608 for (int i = optind+1; i < argc; i++){
1609 if (pa_streq(argv[i], "modules") || pa_streq(argv[i], "clients") ||
1610 pa_streq(argv[i], "sinks") || pa_streq(argv[i], "sink-inputs") ||
1611 pa_streq(argv[i], "sources") || pa_streq(argv[i], "source-outputs") ||
1612 pa_streq(argv[i], "samples") || pa_streq(argv[i], "cards")) {
1613 list_type = pa_xstrdup(argv[i]);
1614 } else if (pa_streq(argv[i], "short")) {
1615 short_list_format = TRUE;
1616 } else {
1617 pa_log(_("Specify nothing, or one of: %s"), "modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards");
1618 goto quit;
1619 }
1620 }
1621
1622 } else if (pa_streq(argv[optind], "upload-sample")) {
1623 struct SF_INFO sfi;
1624 action = UPLOAD_SAMPLE;
1625
1626 if (optind+1 >= argc) {
1627 pa_log(_("Please specify a sample file to load"));
1628 goto quit;
1629 }
1630
1631 if (optind+2 < argc)
1632 sample_name = pa_xstrdup(argv[optind+2]);
1633 else {
1634 char *f = pa_path_get_filename(argv[optind+1]);
1635 sample_name = pa_xstrndup(f, strcspn(f, "."));
1636 }
1637
1638 pa_zero(sfi);
1639 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfi))) {
1640 pa_log(_("Failed to open sound file."));
1641 goto quit;
1642 }
1643
1644 if (pa_sndfile_read_sample_spec(sndfile, &sample_spec) < 0) {
1645 pa_log(_("Failed to determine sample specification from file."));
1646 goto quit;
1647 }
1648 sample_spec.format = PA_SAMPLE_FLOAT32;
1649
1650 if (pa_sndfile_read_channel_map(sndfile, &channel_map) < 0) {
1651 if (sample_spec.channels > 2)
1652 pa_log(_("Warning: Failed to determine sample specification from file."));
1653 pa_channel_map_init_extend(&channel_map, sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
1654 }
1655
1656 pa_assert(pa_channel_map_compatible(&channel_map, &sample_spec));
1657 sample_length = (size_t) sfi.frames*pa_frame_size(&sample_spec);
1658
1659 } else if (pa_streq(argv[optind], "play-sample")) {
1660 action = PLAY_SAMPLE;
1661 if (argc != optind+2 && argc != optind+3) {
1662 pa_log(_("You have to specify a sample name to play"));
1663 goto quit;
1664 }
1665
1666 sample_name = pa_xstrdup(argv[optind+1]);
1667
1668 if (optind+2 < argc)
1669 sink_name = pa_xstrdup(argv[optind+2]);
1670
1671 } else if (pa_streq(argv[optind], "remove-sample")) {
1672 action = REMOVE_SAMPLE;
1673 if (argc != optind+2) {
1674 pa_log(_("You have to specify a sample name to remove"));
1675 goto quit;
1676 }
1677
1678 sample_name = pa_xstrdup(argv[optind+1]);
1679
1680 } else if (pa_streq(argv[optind], "move-sink-input")) {
1681 action = MOVE_SINK_INPUT;
1682 if (argc != optind+3) {
1683 pa_log(_("You have to specify a sink input index and a sink"));
1684 goto quit;
1685 }
1686
1687 sink_input_idx = (uint32_t) atoi(argv[optind+1]);
1688 sink_name = pa_xstrdup(argv[optind+2]);
1689
1690 } else if (pa_streq(argv[optind], "move-source-output")) {
1691 action = MOVE_SOURCE_OUTPUT;
1692 if (argc != optind+3) {
1693 pa_log(_("You have to specify a source output index and a source"));
1694 goto quit;
1695 }
1696
1697 source_output_idx = (uint32_t) atoi(argv[optind+1]);
1698 source_name = pa_xstrdup(argv[optind+2]);
1699
1700 } else if (pa_streq(argv[optind], "load-module")) {
1701 int i;
1702 size_t n = 0;
1703 char *p;
1704
1705 action = LOAD_MODULE;
1706
1707 if (argc <= optind+1) {
1708 pa_log(_("You have to specify a module name and arguments."));
1709 goto quit;
1710 }
1711
1712 module_name = argv[optind+1];
1713
1714 for (i = optind+2; i < argc; i++)
1715 n += strlen(argv[i])+1;
1716
1717 if (n > 0) {
1718 p = module_args = pa_xmalloc(n);
1719
1720 for (i = optind+2; i < argc; i++)
1721 p += sprintf(p, "%s%s", p == module_args ? "" : " ", argv[i]);
1722 }
1723
1724 } else if (pa_streq(argv[optind], "unload-module")) {
1725 action = UNLOAD_MODULE;
1726
1727 if (argc != optind+2) {
1728 pa_log(_("You have to specify a module index or name"));
1729 goto quit;
1730 }
1731
1732 if (pa_atou(argv[optind + 1], &module_index) < 0)
1733 module_name = argv[optind + 1];
1734
1735 } else if (pa_streq(argv[optind], "suspend-sink")) {
1736 int b;
1737
1738 action = SUSPEND_SINK;
1739
1740 if (argc > optind+3 || optind+1 >= argc) {
1741 pa_log(_("You may not specify more than one sink. You have to specify a boolean value."));
1742 goto quit;
1743 }
1744
1745 if ((b = pa_parse_boolean(argv[argc-1])) < 0) {
1746 pa_log(_("Invalid suspend specification."));
1747 goto quit;
1748 }
1749
1750 suspend = !!b;
1751
1752 if (argc > optind+2)
1753 sink_name = pa_xstrdup(argv[optind+1]);
1754
1755 } else if (pa_streq(argv[optind], "suspend-source")) {
1756 int b;
1757
1758 action = SUSPEND_SOURCE;
1759
1760 if (argc > optind+3 || optind+1 >= argc) {
1761 pa_log(_("You may not specify more than one source. You have to specify a boolean value."));
1762 goto quit;
1763 }
1764
1765 if ((b = pa_parse_boolean(argv[argc-1])) < 0) {
1766 pa_log(_("Invalid suspend specification."));
1767 goto quit;
1768 }
1769
1770 suspend = !!b;
1771
1772 if (argc > optind+2)
1773 source_name = pa_xstrdup(argv[optind+1]);
1774 } else if (pa_streq(argv[optind], "set-card-profile")) {
1775 action = SET_CARD_PROFILE;
1776
1777 if (argc != optind+3) {
1778 pa_log(_("You have to specify a card name/index and a profile name"));
1779 goto quit;
1780 }
1781
1782 card_name = pa_xstrdup(argv[optind+1]);
1783 profile_name = pa_xstrdup(argv[optind+2]);
1784
1785 } else if (pa_streq(argv[optind], "set-sink-port")) {
1786 action = SET_SINK_PORT;
1787
1788 if (argc != optind+3) {
1789 pa_log(_("You have to specify a sink name/index and a port name"));
1790 goto quit;
1791 }
1792
1793 sink_name = pa_xstrdup(argv[optind+1]);
1794 port_name = pa_xstrdup(argv[optind+2]);
1795
1796 } else if (pa_streq(argv[optind], "set-default-sink")) {
1797 action = SET_DEFAULT_SINK;
1798
1799 if (argc != optind+2) {
1800 pa_log(_("You have to specify a sink name"));
1801 goto quit;
1802 }
1803
1804 sink_name = pa_xstrdup(argv[optind+1]);
1805
1806 } else if (pa_streq(argv[optind], "set-source-port")) {
1807 action = SET_SOURCE_PORT;
1808
1809 if (argc != optind+3) {
1810 pa_log(_("You have to specify a source name/index and a port name"));
1811 goto quit;
1812 }
1813
1814 source_name = pa_xstrdup(argv[optind+1]);
1815 port_name = pa_xstrdup(argv[optind+2]);
1816
1817 } else if (pa_streq(argv[optind], "set-default-source")) {
1818 action = SET_DEFAULT_SOURCE;
1819
1820 if (argc != optind+2) {
1821 pa_log(_("You have to specify a source name"));
1822 goto quit;
1823 }
1824
1825 source_name = pa_xstrdup(argv[optind+1]);
1826
1827 } else if (pa_streq(argv[optind], "set-sink-volume")) {
1828 action = SET_SINK_VOLUME;
1829
1830 if (argc != optind+3) {
1831 pa_log(_("You have to specify a sink name/index and a volume"));
1832 goto quit;
1833 }
1834
1835 sink_name = pa_xstrdup(argv[optind+1]);
1836
1837 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1838 goto quit;
1839
1840 } else if (pa_streq(argv[optind], "set-source-volume")) {
1841 action = SET_SOURCE_VOLUME;
1842
1843 if (argc != optind+3) {
1844 pa_log(_("You have to specify a source name/index and a volume"));
1845 goto quit;
1846 }
1847
1848 source_name = pa_xstrdup(argv[optind+1]);
1849
1850 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1851 goto quit;
1852
1853 } else if (pa_streq(argv[optind], "set-sink-input-volume")) {
1854 action = SET_SINK_INPUT_VOLUME;
1855
1856 if (argc != optind+3) {
1857 pa_log(_("You have to specify a sink input index and a volume"));
1858 goto quit;
1859 }
1860
1861 if (pa_atou(argv[optind+1], &sink_input_idx) < 0) {
1862 pa_log(_("Invalid sink input index"));
1863 goto quit;
1864 }
1865
1866 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1867 goto quit;
1868
1869 } else if (pa_streq(argv[optind], "set-source-output-volume")) {
1870 action = SET_SOURCE_OUTPUT_VOLUME;
1871
1872 if (argc != optind+3) {
1873 pa_log(_("You have to specify a source output index and a volume"));
1874 goto quit;
1875 }
1876
1877 if (pa_atou(argv[optind+1], &source_output_idx) < 0) {
1878 pa_log(_("Invalid source output index"));
1879 goto quit;
1880 }
1881
1882 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1883 goto quit;
1884
1885 } else if (pa_streq(argv[optind], "set-sink-mute")) {
1886 action = SET_SINK_MUTE;
1887
1888 if (argc != optind+3) {
1889 pa_log(_("You have to specify a sink name/index and a mute boolean"));
1890 goto quit;
1891 }
1892
1893 if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
1894 pa_log(_("Invalid mute specification"));
1895 goto quit;
1896 }
1897
1898 sink_name = pa_xstrdup(argv[optind+1]);
1899
1900 } else if (pa_streq(argv[optind], "set-source-mute")) {
1901 action = SET_SOURCE_MUTE;
1902
1903 if (argc != optind+3) {
1904 pa_log(_("You have to specify a source name/index and a mute boolean"));
1905 goto quit;
1906 }
1907
1908 if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
1909 pa_log(_("Invalid mute specification"));
1910 goto quit;
1911 }
1912
1913 source_name = pa_xstrdup(argv[optind+1]);
1914
1915 } else if (pa_streq(argv[optind], "set-sink-input-mute")) {
1916 action = SET_SINK_INPUT_MUTE;
1917
1918 if (argc != optind+3) {
1919 pa_log(_("You have to specify a sink input index and a mute boolean"));
1920 goto quit;
1921 }
1922
1923 if (pa_atou(argv[optind+1], &sink_input_idx) < 0) {
1924 pa_log(_("Invalid sink input index specification"));
1925 goto quit;
1926 }
1927
1928 if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
1929 pa_log(_("Invalid mute specification"));
1930 goto quit;
1931 }
1932
1933 } else if (pa_streq(argv[optind], "set-source-output-mute")) {
1934 action = SET_SOURCE_OUTPUT_MUTE;
1935
1936 if (argc != optind+3) {
1937 pa_log(_("You have to specify a source output index and a mute boolean"));
1938 goto quit;
1939 }
1940
1941 if (pa_atou(argv[optind+1], &source_output_idx) < 0) {
1942 pa_log(_("Invalid source output index specification"));
1943 goto quit;
1944 }
1945
1946 if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
1947 pa_log(_("Invalid mute specification"));
1948 goto quit;
1949 }
1950
1951 } else if (pa_streq(argv[optind], "subscribe"))
1952
1953 action = SUBSCRIBE;
1954
1955 else if (pa_streq(argv[optind], "set-sink-formats")) {
1956 int32_t tmp;
1957
1958 if (argc != optind+3 || pa_atoi(argv[optind+1], &tmp) < 0) {
1959 pa_log(_("You have to specify a sink index and a semicolon-separated list of supported formats"));
1960 goto quit;
1961 }
1962
1963 sink_idx = tmp;
1964 action = SET_SINK_FORMATS;
1965 formats = pa_xstrdup(argv[optind+2]);
1966
1967 } else if (pa_streq(argv[optind], "set-port-latency-offset")) {
1968 action = SET_PORT_LATENCY_OFFSET;
1969
1970 if (argc != optind+4) {
1971 pa_log(_("You have to specify a card name/index, a port name and a latency offset"));
1972 goto quit;
1973 }
1974
1975 card_name = pa_xstrdup(argv[optind+1]);
1976 port_name = pa_xstrdup(argv[optind+2]);
1977 if (pa_atoi(argv[optind + 3], &latency_offset) < 0) {
1978 pa_log(_("Could not parse latency offset"));
1979 goto quit;
1980 }
1981
1982 } else if (pa_streq(argv[optind], "help")) {
1983 help(bn);
1984 ret = 0;
1985 goto quit;
1986 }
1987 }
1988
1989 if (action == NONE) {
1990 pa_log(_("No valid command specified."));
1991 goto quit;
1992 }
1993
1994 if (!(m = pa_mainloop_new())) {
1995 pa_log(_("pa_mainloop_new() failed."));
1996 goto quit;
1997 }
1998
1999 mainloop_api = pa_mainloop_get_api(m);
2000
2001 pa_assert_se(pa_signal_init(mainloop_api) == 0);
2002 pa_signal_new(SIGINT, exit_signal_callback, NULL);
2003 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
2004 pa_disable_sigpipe();
2005
2006 if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) {
2007 pa_log(_("pa_context_new() failed."));
2008 goto quit;
2009 }
2010
2011 pa_context_set_state_callback(context, context_state_callback, NULL);
2012 if (pa_context_connect(context, server, 0, NULL) < 0) {
2013 pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
2014 goto quit;
2015 }
2016
2017 if (pa_mainloop_run(m, &ret) < 0) {
2018 pa_log(_("pa_mainloop_run() failed."));
2019 goto quit;
2020 }
2021
2022 quit:
2023 if (sample_stream)
2024 pa_stream_unref(sample_stream);
2025
2026 if (context)
2027 pa_context_unref(context);
2028
2029 if (m) {
2030 pa_signal_done();
2031 pa_mainloop_free(m);
2032 }
2033
2034 pa_xfree(server);
2035 pa_xfree(list_type);
2036 pa_xfree(sample_name);
2037 pa_xfree(sink_name);
2038 pa_xfree(source_name);
2039 pa_xfree(module_args);
2040 pa_xfree(card_name);
2041 pa_xfree(profile_name);
2042 pa_xfree(port_name);
2043 pa_xfree(formats);
2044
2045 if (sndfile)
2046 sf_close(sndfile);
2047
2048 if (proplist)
2049 pa_proplist_free(proplist);
2050
2051 return ret;
2052 }