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