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