Fix indent
[platform/upstream/pulseaudio.git] / 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 #include <pulse/ext-node-manager.h>
42 #include <pulse/ext-volume-api.h>
43
44 #include <pulsecore/i18n.h>
45 #include <pulsecore/macro.h>
46 #include <pulsecore/core-util.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/sndfile-util.h>
49
50 static pa_context *context = NULL;
51 static pa_mainloop_api *mainloop_api = NULL;
52
53 static char
54     *list_type = NULL,
55     *sample_name = NULL,
56     *sink_name = NULL,
57     *source_name = NULL,
58     *module_name = NULL,
59     *module_args = NULL,
60     *card_name = NULL,
61     *profile_name = NULL,
62     *port_name = NULL,
63     *formats = NULL,
64     *volume_control_name = NULL,
65     *mute_control_name = NULL;
66
67 static uint32_t
68     sink_input_idx = PA_INVALID_INDEX,
69     source_output_idx = PA_INVALID_INDEX,
70     sink_idx = PA_INVALID_INDEX;
71
72 static bool short_list_format = false;
73 static uint32_t module_index;
74 static int32_t latency_offset;
75 static bool suspend;
76 static pa_volume_t volume;
77 static enum volume_flags {
78     VOL_UINT     = 0,
79     VOL_PERCENT  = 1,
80     VOL_LINEAR   = 2,
81     VOL_DECIBEL  = 3,
82     VOL_ABSOLUTE = 0 << 4,
83     VOL_RELATIVE = 1 << 4,
84 } volume_flags;
85
86 static enum mute_flags {
87     INVALID_MUTE = -1,
88     UNMUTE = 0,
89     MUTE = 1,
90     TOGGLE_MUTE = 2
91 } mute = INVALID_MUTE;
92
93 static pa_proplist *proplist = NULL;
94
95 static SNDFILE *sndfile = NULL;
96 static pa_stream *sample_stream = NULL;
97 static pa_sample_spec sample_spec;
98 static pa_channel_map channel_map;
99 static size_t sample_length = 0;
100 static int actions = 0;
101
102 static bool nl = false;
103
104 static uint32_t src_node_id;
105 static uint32_t dst_node_id;
106 static uint32_t conn_id;
107 bool volume_api_connected = false;
108 pa_ext_volume_api_bvolume bvolume;
109 bool volume_valid = false;
110 bool balance_valid = false;
111 uint32_t main_output_volume_control = PA_INVALID_INDEX;
112 uint32_t main_input_volume_control = PA_INVALID_INDEX;
113 uint32_t main_output_mute_control = PA_INVALID_INDEX;
114 uint32_t main_input_mute_control = PA_INVALID_INDEX;
115
116 static enum {
117     NONE,
118     EXIT,
119     STAT,
120     INFO,
121     UPLOAD_SAMPLE,
122     PLAY_SAMPLE,
123     REMOVE_SAMPLE,
124     LIST,
125     MOVE_SINK_INPUT,
126     MOVE_SOURCE_OUTPUT,
127     LOAD_MODULE,
128     UNLOAD_MODULE,
129     SUSPEND_SINK,
130     SUSPEND_SOURCE,
131     SET_CARD_PROFILE,
132     SET_SINK_PORT,
133     SET_DEFAULT_SINK,
134     SET_SOURCE_PORT,
135     SET_DEFAULT_SOURCE,
136     SET_SINK_VOLUME,
137     SET_SOURCE_VOLUME,
138     SET_SINK_INPUT_VOLUME,
139     SET_SOURCE_OUTPUT_VOLUME,
140     SET_SINK_MUTE,
141     SET_SOURCE_MUTE,
142     SET_SINK_INPUT_MUTE,
143     SET_SOURCE_OUTPUT_MUTE,
144     SET_SINK_FORMATS,
145     SET_PORT_LATENCY_OFFSET,
146     SET_VOLUME_CONTROL_VOLUME,
147     SET_MUTE_CONTROL_MUTE,
148     SUBSCRIBE,
149     NODE_CONNECT,
150     NODE_DISCONNECT
151 } action = NONE;
152
153 static void quit(int ret) {
154     pa_assert(mainloop_api);
155     mainloop_api->quit(mainloop_api, ret);
156 }
157
158 static void context_drain_complete(pa_context *c, void *userdata) {
159     pa_context_disconnect(c);
160 }
161
162 static void drain(void) {
163     pa_operation *o;
164
165     if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
166         pa_context_disconnect(context);
167     else
168         pa_operation_unref(o);
169 }
170
171 static void complete_action(void) {
172     pa_assert(actions > 0);
173
174     if (!(--actions))
175         drain();
176 }
177
178 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
179     char s[PA_BYTES_SNPRINT_MAX];
180     if (!i) {
181         pa_log(_("Failed to get statistics: %s"), pa_strerror(pa_context_errno(c)));
182         quit(1);
183         return;
184     }
185
186     pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
187     printf(_("Currently in use: %u blocks containing %s bytes total.\n"), i->memblock_total, s);
188
189     pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
190     printf(_("Allocated during whole lifetime: %u blocks containing %s bytes total.\n"), i->memblock_allocated, s);
191
192     pa_bytes_snprint(s, sizeof(s), i->scache_size);
193     printf(_("Sample cache size: %s\n"), s);
194
195     complete_action();
196 }
197
198 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
199     char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
200
201     if (!i) {
202         pa_log(_("Failed to get server information: %s"), pa_strerror(pa_context_errno(c)));
203         quit(1);
204         return;
205     }
206
207     printf(_("Server String: %s\n"
208              "Library Protocol Version: %u\n"
209              "Server Protocol Version: %u\n"
210              "Is Local: %s\n"
211              "Client Index: %u\n"
212              "Tile Size: %zu\n"),
213              pa_context_get_server(c),
214              pa_context_get_protocol_version(c),
215              pa_context_get_server_protocol_version(c),
216              pa_yes_no(pa_context_is_local(c)),
217              pa_context_get_index(c),
218              pa_context_get_tile_size(c, NULL));
219
220     pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec);
221     pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map);
222
223     printf(_("User Name: %s\n"
224              "Host Name: %s\n"
225              "Server Name: %s\n"
226              "Server Version: %s\n"
227              "Default Sample Specification: %s\n"
228              "Default Channel Map: %s\n"
229              "Default Sink: %s\n"
230              "Default Source: %s\n"
231              "Cookie: %04x:%04x\n"),
232            i->user_name,
233            i->host_name,
234            i->server_name,
235            i->server_version,
236            ss,
237            cm,
238            i->default_sink_name,
239            i->default_source_name,
240            i->cookie >> 16,
241            i->cookie & 0xFFFFU);
242
243     complete_action();
244 }
245
246 static const char* get_available_str_ynonly(int available) {
247     switch (available) {
248         case PA_PORT_AVAILABLE_YES: return ", available";
249         case PA_PORT_AVAILABLE_NO: return ", not available";
250     }
251     return "";
252 }
253
254 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
255
256     static const char *state_table[] = {
257         [1+PA_SINK_INVALID_STATE] = "n/a",
258         [1+PA_SINK_RUNNING] = "RUNNING",
259         [1+PA_SINK_IDLE] = "IDLE",
260         [1+PA_SINK_SUSPENDED] = "SUSPENDED"
261     };
262
263     char
264         s[PA_SAMPLE_SPEC_SNPRINT_MAX],
265         cv[PA_CVOLUME_SNPRINT_VERBOSE_MAX],
266         v[PA_VOLUME_SNPRINT_VERBOSE_MAX],
267         cm[PA_CHANNEL_MAP_SNPRINT_MAX],
268         f[PA_FORMAT_INFO_SNPRINT_MAX];
269     char *pl;
270
271     if (is_last < 0) {
272         pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
273         quit(1);
274         return;
275     }
276
277     if (is_last) {
278         complete_action();
279         return;
280     }
281
282     pa_assert(i);
283
284     if (nl && !short_list_format)
285         printf("\n");
286     nl = true;
287
288     if (short_list_format) {
289         printf("%u\t%s\t%s\t%s\t%s\n",
290                i->index,
291                i->name,
292                pa_strnull(i->driver),
293                pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
294                state_table[1+i->state]);
295         return;
296     }
297
298     printf(_("Sink #%u\n"
299              "\tState: %s\n"
300              "\tName: %s\n"
301              "\tDescription: %s\n"
302              "\tDriver: %s\n"
303              "\tSample Specification: %s\n"
304              "\tChannel Map: %s\n"
305              "\tOwner Module: %u\n"
306              "\tMute: %s\n"
307              "\tVolume: %s\n"
308              "\t        balance %0.2f\n"
309              "\tBase Volume: %s\n"
310              "\tMonitor Source: %s\n"
311              "\tLatency: %0.0f usec, configured %0.0f usec\n"
312              "\tFlags: %s%s%s%s%s%s%s\n"
313              "\tProperties:\n\t\t%s\n"),
314            i->index,
315            state_table[1+i->state],
316            i->name,
317            pa_strnull(i->description),
318            pa_strnull(i->driver),
319            pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
320            pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
321            i->owner_module,
322            pa_yes_no(i->mute),
323            pa_cvolume_snprint_verbose(cv, sizeof(cv), &i->volume, &i->channel_map, i->flags & PA_SINK_DECIBEL_VOLUME),
324            pa_cvolume_get_balance(&i->volume, &i->channel_map),
325            pa_volume_snprint_verbose(v, sizeof(v), i->base_volume, i->flags & PA_SINK_DECIBEL_VOLUME),
326            pa_strnull(i->monitor_source_name),
327            (double) i->latency, (double) i->configured_latency,
328            i->flags & PA_SINK_HARDWARE ? "HARDWARE " : "",
329            i->flags & PA_SINK_NETWORK ? "NETWORK " : "",
330            i->flags & PA_SINK_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
331            i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
332            i->flags & PA_SINK_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
333            i->flags & PA_SINK_LATENCY ? "LATENCY " : "",
334            i->flags & PA_SINK_SET_FORMATS ? "SET_FORMATS " : "",
335            pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
336
337     pa_xfree(pl);
338
339     if (i->ports) {
340         pa_sink_port_info **p;
341
342         printf(_("\tPorts:\n"));
343         for (p = i->ports; *p; p++)
344             printf("\t\t%s: %s (priority: %u%s)\n", (*p)->name, (*p)->description,
345                     (*p)->priority, get_available_str_ynonly((*p)->available));
346     }
347
348     if (i->active_port)
349         printf(_("\tActive Port: %s\n"),
350                i->active_port->name);
351
352     if (i->formats) {
353         uint8_t j;
354
355         printf(_("\tFormats:\n"));
356         for (j = 0; j < i->n_formats; j++)
357             printf("\t\t%s\n", pa_format_info_snprint(f, sizeof(f), i->formats[j]));
358     }
359 }
360
361 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
362
363     static const char *state_table[] = {
364         [1+PA_SOURCE_INVALID_STATE] = "n/a",
365         [1+PA_SOURCE_RUNNING] = "RUNNING",
366         [1+PA_SOURCE_IDLE] = "IDLE",
367         [1+PA_SOURCE_SUSPENDED] = "SUSPENDED"
368     };
369
370     char
371         s[PA_SAMPLE_SPEC_SNPRINT_MAX],
372         cv[PA_CVOLUME_SNPRINT_VERBOSE_MAX],
373         v[PA_VOLUME_SNPRINT_VERBOSE_MAX],
374         cm[PA_CHANNEL_MAP_SNPRINT_MAX],
375         f[PA_FORMAT_INFO_SNPRINT_MAX];
376     char *pl;
377
378     if (is_last < 0) {
379         pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
380         quit(1);
381         return;
382     }
383
384     if (is_last) {
385         complete_action();
386         return;
387     }
388
389     pa_assert(i);
390
391     if (nl && !short_list_format)
392         printf("\n");
393     nl = true;
394
395     if (short_list_format) {
396         printf("%u\t%s\t%s\t%s\t%s\n",
397                i->index,
398                i->name,
399                pa_strnull(i->driver),
400                pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
401                state_table[1+i->state]);
402         return;
403     }
404
405     printf(_("Source #%u\n"
406              "\tState: %s\n"
407              "\tName: %s\n"
408              "\tDescription: %s\n"
409              "\tDriver: %s\n"
410              "\tSample Specification: %s\n"
411              "\tChannel Map: %s\n"
412              "\tOwner Module: %u\n"
413              "\tMute: %s\n"
414              "\tVolume: %s\n"
415              "\t        balance %0.2f\n"
416              "\tBase Volume: %s\n"
417              "\tMonitor of Sink: %s\n"
418              "\tLatency: %0.0f usec, configured %0.0f usec\n"
419              "\tFlags: %s%s%s%s%s%s\n"
420              "\tProperties:\n\t\t%s\n"),
421            i->index,
422            state_table[1+i->state],
423            i->name,
424            pa_strnull(i->description),
425            pa_strnull(i->driver),
426            pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
427            pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
428            i->owner_module,
429            pa_yes_no(i->mute),
430            pa_cvolume_snprint_verbose(cv, sizeof(cv), &i->volume, &i->channel_map, i->flags & PA_SOURCE_DECIBEL_VOLUME),
431            pa_cvolume_get_balance(&i->volume, &i->channel_map),
432            pa_volume_snprint_verbose(v, sizeof(v), i->base_volume, i->flags & PA_SOURCE_DECIBEL_VOLUME),
433            i->monitor_of_sink_name ? i->monitor_of_sink_name : _("n/a"),
434            (double) i->latency, (double) i->configured_latency,
435            i->flags & PA_SOURCE_HARDWARE ? "HARDWARE " : "",
436            i->flags & PA_SOURCE_NETWORK ? "NETWORK " : "",
437            i->flags & PA_SOURCE_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
438            i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
439            i->flags & PA_SOURCE_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
440            i->flags & PA_SOURCE_LATENCY ? "LATENCY " : "",
441            pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
442
443     pa_xfree(pl);
444
445     if (i->ports) {
446         pa_source_port_info **p;
447
448         printf(_("\tPorts:\n"));
449         for (p = i->ports; *p; p++)
450             printf("\t\t%s: %s (priority: %u%s)\n", (*p)->name, (*p)->description,
451                     (*p)->priority, get_available_str_ynonly((*p)->available));
452     }
453
454     if (i->active_port)
455         printf(_("\tActive Port: %s\n"),
456                i->active_port->name);
457
458     if (i->formats) {
459         uint8_t j;
460
461         printf(_("\tFormats:\n"));
462         for (j = 0; j < i->n_formats; j++)
463             printf("\t\t%s\n", pa_format_info_snprint(f, sizeof(f), i->formats[j]));
464     }
465 }
466
467 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
468     char t[32];
469     char *pl;
470
471     if (is_last < 0) {
472         pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
473         quit(1);
474         return;
475     }
476
477     if (is_last) {
478         complete_action();
479         return;
480     }
481
482     pa_assert(i);
483
484     if (nl && !short_list_format)
485         printf("\n");
486     nl = true;
487
488     pa_snprintf(t, sizeof(t), "%u", i->n_used);
489
490     if (short_list_format) {
491         printf("%u\t%s\t%s\t\n", i->index, i->name, i->argument ? i->argument : "");
492         return;
493     }
494
495     printf(_("Module #%u\n"
496              "\tName: %s\n"
497              "\tArgument: %s\n"
498              "\tUsage counter: %s\n"
499              "\tProperties:\n\t\t%s\n"),
500            i->index,
501            i->name,
502            i->argument ? i->argument : "",
503            i->n_used != PA_INVALID_INDEX ? t : _("n/a"),
504            pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
505
506     pa_xfree(pl);
507 }
508
509 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
510     char t[32];
511     char *pl;
512
513     if (is_last < 0) {
514         pa_log(_("Failed to get client information: %s"), pa_strerror(pa_context_errno(c)));
515         quit(1);
516         return;
517     }
518
519     if (is_last) {
520         complete_action();
521         return;
522     }
523
524     pa_assert(i);
525
526     if (nl && !short_list_format)
527         printf("\n");
528     nl = true;
529
530     pa_snprintf(t, sizeof(t), "%u", i->owner_module);
531
532     if (short_list_format) {
533         printf("%u\t%s\t%s\n",
534                i->index,
535                pa_strnull(i->driver),
536                pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)));
537         return;
538     }
539
540     printf(_("Client #%u\n"
541              "\tDriver: %s\n"
542              "\tOwner Module: %s\n"
543              "\tProperties:\n\t\t%s\n"),
544            i->index,
545            pa_strnull(i->driver),
546            i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
547            pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
548
549     pa_xfree(pl);
550 }
551
552 static void get_card_info_callback(pa_context *c, const pa_card_info *i, int is_last, void *userdata) {
553     char t[32];
554     char *pl;
555
556     if (is_last < 0) {
557         pa_log(_("Failed to get card information: %s"), pa_strerror(pa_context_errno(c)));
558         complete_action();
559         return;
560     }
561
562     if (is_last) {
563         complete_action();
564         return;
565     }
566
567     pa_assert(i);
568
569     if (nl && !short_list_format)
570         printf("\n");
571     nl = true;
572
573     pa_snprintf(t, sizeof(t), "%u", i->owner_module);
574
575     if (short_list_format) {
576         printf("%u\t%s\t%s\n", i->index, i->name, pa_strnull(i->driver));
577         return;
578     }
579
580     printf(_("Card #%u\n"
581              "\tName: %s\n"
582              "\tDriver: %s\n"
583              "\tOwner Module: %s\n"
584              "\tProperties:\n\t\t%s\n"),
585            i->index,
586            i->name,
587            pa_strnull(i->driver),
588            i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
589            pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
590
591     pa_xfree(pl);
592
593     if (i->n_profiles > 0) {
594         pa_card_profile_info2 **p;
595
596         printf(_("\tProfiles:\n"));
597         for (p = i->profiles2; *p; p++)
598             printf("\t\t%s: %s (sinks: %u, sources: %u, priority: %u, available: %s)\n", (*p)->name,
599                 (*p)->description, (*p)->n_sinks, (*p)->n_sources, (*p)->priority, pa_yes_no((*p)->available));
600     }
601
602     if (i->active_profile)
603         printf(_("\tActive Profile: %s\n"),
604                i->active_profile->name);
605
606     if (i->ports) {
607         pa_card_port_info **p;
608
609         printf(_("\tPorts:\n"));
610         for (p = i->ports; *p; p++) {
611             pa_card_profile_info **pr = (*p)->profiles;
612             printf("\t\t%s: %s (priority: %u, latency offset: %" PRId64 " usec%s)\n", (*p)->name,
613                 (*p)->description, (*p)->priority, (*p)->latency_offset,
614                 get_available_str_ynonly((*p)->available));
615
616             if (!pa_proplist_isempty((*p)->proplist)) {
617                 printf(_("\t\t\tProperties:\n\t\t\t\t%s\n"), pl = pa_proplist_to_string_sep((*p)->proplist, "\n\t\t\t\t"));
618                 pa_xfree(pl);
619             }
620
621             if (pr) {
622                 printf(_("\t\t\tPart of profile(s): %s"), pa_strnull((*pr)->name));
623                 pr++;
624                 while (*pr) {
625                     printf(", %s", pa_strnull((*pr)->name));
626                     pr++;
627                 }
628                 printf("\n");
629             }
630         }
631     }
632 }
633
634 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
635     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];
636     char *pl;
637
638     if (is_last < 0) {
639         pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
640         quit(1);
641         return;
642     }
643
644     if (is_last) {
645         complete_action();
646         return;
647     }
648
649     pa_assert(i);
650
651     if (nl && !short_list_format)
652         printf("\n");
653     nl = true;
654
655     pa_snprintf(t, sizeof(t), "%u", i->owner_module);
656     pa_snprintf(k, sizeof(k), "%u", i->client);
657
658     if (short_list_format) {
659         printf("%u\t%u\t%s\t%s\t%s\n",
660                i->index,
661                i->sink,
662                i->client != PA_INVALID_INDEX ? k : "-",
663                pa_strnull(i->driver),
664                pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec));
665         return;
666     }
667
668     printf(_("Sink Input #%u\n"
669              "\tDriver: %s\n"
670              "\tOwner Module: %s\n"
671              "\tClient: %s\n"
672              "\tSink: %u\n"
673              "\tSample Specification: %s\n"
674              "\tChannel Map: %s\n"
675              "\tFormat: %s\n"
676              "\tCorked: %s\n"
677              "\tMute: %s\n"
678              "\tVolume: %s\n"
679              "\t        balance %0.2f\n"
680              "\tBuffer Latency: %0.0f usec\n"
681              "\tSink Latency: %0.0f usec\n"
682              "\tResample method: %s\n"
683              "\tProperties:\n\t\t%s\n"),
684            i->index,
685            pa_strnull(i->driver),
686            i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
687            i->client != PA_INVALID_INDEX ? k : _("n/a"),
688            i->sink,
689            pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
690            pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
691            pa_format_info_snprint(f, sizeof(f), i->format),
692            pa_yes_no(i->corked),
693            pa_yes_no(i->mute),
694            pa_cvolume_snprint_verbose(cv, sizeof(cv), &i->volume, &i->channel_map, true),
695            pa_cvolume_get_balance(&i->volume, &i->channel_map),
696            (double) i->buffer_usec,
697            (double) i->sink_usec,
698            i->resample_method ? i->resample_method : _("n/a"),
699            pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
700
701     pa_xfree(pl);
702 }
703
704 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
705     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];
706     char *pl;
707
708     if (is_last < 0) {
709         pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
710         quit(1);
711         return;
712     }
713
714     if (is_last) {
715         complete_action();
716         return;
717     }
718
719     pa_assert(i);
720
721     if (nl && !short_list_format)
722         printf("\n");
723     nl = true;
724
725     pa_snprintf(t, sizeof(t), "%u", i->owner_module);
726     pa_snprintf(k, sizeof(k), "%u", i->client);
727
728     if (short_list_format) {
729         printf("%u\t%u\t%s\t%s\t%s\n",
730                i->index,
731                i->source,
732                i->client != PA_INVALID_INDEX ? k : "-",
733                pa_strnull(i->driver),
734                pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec));
735         return;
736     }
737
738     printf(_("Source Output #%u\n"
739              "\tDriver: %s\n"
740              "\tOwner Module: %s\n"
741              "\tClient: %s\n"
742              "\tSource: %u\n"
743              "\tSample Specification: %s\n"
744              "\tChannel Map: %s\n"
745              "\tFormat: %s\n"
746              "\tCorked: %s\n"
747              "\tMute: %s\n"
748              "\tVolume: %s\n"
749              "\t        balance %0.2f\n"
750              "\tBuffer Latency: %0.0f usec\n"
751              "\tSource Latency: %0.0f usec\n"
752              "\tResample method: %s\n"
753              "\tProperties:\n\t\t%s\n"),
754            i->index,
755            pa_strnull(i->driver),
756            i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
757            i->client != PA_INVALID_INDEX ? k : _("n/a"),
758            i->source,
759            pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
760            pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
761            pa_format_info_snprint(f, sizeof(f), i->format),
762            pa_yes_no(i->corked),
763            pa_yes_no(i->mute),
764            pa_cvolume_snprint_verbose(cv, sizeof(cv), &i->volume, &i->channel_map, true),
765            pa_cvolume_get_balance(&i->volume, &i->channel_map),
766            (double) i->buffer_usec,
767            (double) i->source_usec,
768            i->resample_method ? i->resample_method : _("n/a"),
769            pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
770
771     pa_xfree(pl);
772 }
773
774 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
775     char t[PA_BYTES_SNPRINT_MAX], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_VERBOSE_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
776     char *pl;
777
778     if (is_last < 0) {
779         pa_log(_("Failed to get sample information: %s"), pa_strerror(pa_context_errno(c)));
780         quit(1);
781         return;
782     }
783
784     if (is_last) {
785         complete_action();
786         return;
787     }
788
789     pa_assert(i);
790
791     if (nl && !short_list_format)
792         printf("\n");
793     nl = true;
794
795     pa_bytes_snprint(t, sizeof(t), i->bytes);
796
797     if (short_list_format) {
798         printf("%u\t%s\t%s\t%0.3f\n",
799                i->index,
800                i->name,
801                pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : "-",
802                (double) i->duration/1000000.0);
803         return;
804     }
805
806     printf(_("Sample #%u\n"
807              "\tName: %s\n"
808              "\tSample Specification: %s\n"
809              "\tChannel Map: %s\n"
810              "\tVolume: %s\n"
811              "\t        balance %0.2f\n"
812              "\tDuration: %0.1fs\n"
813              "\tSize: %s\n"
814              "\tLazy: %s\n"
815              "\tFilename: %s\n"
816              "\tProperties:\n\t\t%s\n"),
817            i->index,
818            i->name,
819            pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : _("n/a"),
820            pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : _("n/a"),
821            pa_cvolume_snprint_verbose(cv, sizeof(cv), &i->volume, &i->channel_map, true),
822            pa_cvolume_get_balance(&i->volume, &i->channel_map),
823            (double) i->duration/1000000.0,
824            t,
825            pa_yes_no(i->lazy),
826            i->filename ? i->filename : _("n/a"),
827            pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
828
829     pa_xfree(pl);
830 }
831
832 static void simple_callback(pa_context *c, int success, void *userdata) {
833     if (!success) {
834         pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
835         quit(1);
836         return;
837     }
838
839     complete_action();
840 }
841
842 static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
843     if (idx == PA_INVALID_INDEX) {
844         pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
845         quit(1);
846         return;
847     }
848
849     printf("%u\n", idx);
850
851     complete_action();
852 }
853
854 static void volume_relative_adjust(pa_cvolume *cv, pa_volume_t adjustment) {
855     pa_assert((volume_flags & VOL_RELATIVE) == VOL_RELATIVE);
856
857     /* Relative volume change is additive in case of UINT or PERCENT
858      * and multiplicative for LINEAR or DECIBEL */
859     if ((volume_flags & 0x0F) == VOL_UINT || (volume_flags & 0x0F) == VOL_PERCENT) {
860         pa_volume_t v = pa_cvolume_avg(cv);
861         v = v + adjustment < PA_VOLUME_NORM ? PA_VOLUME_MUTED : v + adjustment - PA_VOLUME_NORM;
862         pa_cvolume_set(cv, 1, v);
863     }
864     if ((volume_flags & 0x0F) == VOL_LINEAR || (volume_flags & 0x0F) == VOL_DECIBEL) {
865         pa_sw_cvolume_multiply_scalar(cv, cv, adjustment);
866     }
867 }
868
869 static void unload_module_by_name_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
870     static bool unloaded = false;
871
872     if (is_last < 0) {
873         pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
874         quit(1);
875         return;
876     }
877
878     if (is_last) {
879         if (unloaded == false)
880             pa_log(_("Failed to unload module: Module %s not loaded"), module_name);
881         complete_action();
882         return;
883     }
884
885     pa_assert(i);
886
887     if (pa_streq(module_name, i->name)) {
888         unloaded = true;
889         actions++;
890         pa_operation_unref(pa_context_unload_module(c, i->index, simple_callback, NULL));
891     }
892 }
893
894 static void get_sink_volume_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
895     pa_cvolume cv;
896
897     if (is_last < 0) {
898         pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
899         quit(1);
900         return;
901     }
902
903     if (is_last)
904         return;
905
906     pa_assert(i);
907
908     cv = i->volume;
909     volume_relative_adjust(&cv, volume);
910     pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &cv, simple_callback, NULL));
911 }
912
913 static void get_source_volume_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
914     pa_cvolume cv;
915
916     if (is_last < 0) {
917         pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
918         quit(1);
919         return;
920     }
921
922     if (is_last)
923         return;
924
925     pa_assert(i);
926
927     cv = i->volume;
928     volume_relative_adjust(&cv, volume);
929     pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &cv, simple_callback, NULL));
930 }
931
932 static void get_sink_input_volume_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
933     pa_cvolume cv;
934
935     if (is_last < 0) {
936         pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
937         quit(1);
938         return;
939     }
940
941     if (is_last)
942         return;
943
944     pa_assert(i);
945
946     cv = i->volume;
947     volume_relative_adjust(&cv, volume);
948     pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &cv, simple_callback, NULL));
949 }
950
951 static void get_source_output_volume_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
952     pa_cvolume cv;
953
954     if (is_last < 0) {
955         pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
956         quit(1);
957         return;
958     }
959
960     if (is_last)
961         return;
962
963     pa_assert(o);
964
965     cv = o->volume;
966     volume_relative_adjust(&cv, volume);
967     pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &cv, simple_callback, NULL));
968 }
969
970 static void sink_toggle_mute_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
971     if (is_last < 0) {
972         pa_log(_("Failed to get sink 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(i);
981
982     pa_operation_unref(pa_context_set_sink_mute_by_name(c, i->name, !i->mute, simple_callback, NULL));
983 }
984
985 static void source_toggle_mute_callback(pa_context *c, const pa_source_info *o, int is_last, void *userdata) {
986     if (is_last < 0) {
987         pa_log(_("Failed to get source 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(o);
996
997     pa_operation_unref(pa_context_set_source_mute_by_name(c, o->name, !o->mute, simple_callback, NULL));
998 }
999
1000 static void sink_input_toggle_mute_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
1001     if (is_last < 0) {
1002         pa_log(_("Failed to get sink input 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(i);
1011
1012     pa_operation_unref(pa_context_set_sink_input_mute(c, i->index, !i->mute, simple_callback, NULL));
1013 }
1014
1015 static void source_output_toggle_mute_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
1016     if (is_last < 0) {
1017         pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
1018         quit(1);
1019         return;
1020     }
1021
1022     if (is_last)
1023         return;
1024
1025     pa_assert(o);
1026
1027     pa_operation_unref(pa_context_set_source_output_mute(c, o->index, !o->mute, simple_callback, NULL));
1028 }
1029
1030 static void node_list_callback(pa_context *c,
1031                 const pa_ext_node_manager_info *info,
1032                 int eol,
1033                 void *userdata) {
1034
1035     if (!eol) {
1036         const char *node_id = pa_proplist_gets(info->props, "index");
1037         if (node_id != NULL) {
1038             printf("Node #%s (%s)\n", node_id, info->name);
1039             printf("%s\n", pa_proplist_to_string(info->props));
1040         }
1041     } else
1042         complete_action();
1043 }
1044
1045 static void node_connect_callback(pa_context *c,
1046                 uint32_t conne_id,
1047                 void *userdata) {
1048
1049     printf("New connection id: %d\n", conne_id);
1050
1051     complete_action();
1052 }
1053
1054 /* PA_MAX_FORMATS is defined in internal.h so we just define a sane value here */
1055 #define MAX_FORMATS 256
1056
1057 static void set_sink_formats(pa_context *c, uint32_t sink, const char *str) {
1058     pa_format_info *f_arr[MAX_FORMATS];
1059     char *format = NULL;
1060     const char *state = NULL;
1061     int i = 0;
1062     pa_operation *o = NULL;
1063
1064     while ((format = pa_split(str, ";", &state))) {
1065         pa_format_info *f = pa_format_info_from_string(pa_strip(format));
1066
1067         if (!f) {
1068             pa_log(_("Failed to set format: invalid format string %s"), format);
1069             goto error;
1070         }
1071
1072         f_arr[i++] = f;
1073         pa_xfree(format);
1074     }
1075
1076     o = pa_ext_device_restore_save_formats(c, PA_DEVICE_TYPE_SINK, sink, i, f_arr, simple_callback, NULL);
1077     if (o) {
1078         pa_operation_unref(o);
1079         actions++;
1080     }
1081
1082 done:
1083     if (format)
1084         pa_xfree(format);
1085     while(i--)
1086         pa_format_info_free(f_arr[i]);
1087
1088     return;
1089
1090 error:
1091     while(i--)
1092         pa_format_info_free(f_arr[i]);
1093     quit(1);
1094     goto done;
1095 }
1096
1097 static void stream_state_callback(pa_stream *s, void *userdata) {
1098     pa_assert(s);
1099
1100     switch (pa_stream_get_state(s)) {
1101         case PA_STREAM_CREATING:
1102         case PA_STREAM_READY:
1103             break;
1104
1105         case PA_STREAM_TERMINATED:
1106             drain();
1107             break;
1108
1109         case PA_STREAM_FAILED:
1110         default:
1111             pa_log(_("Failed to upload sample: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
1112             quit(1);
1113     }
1114 }
1115
1116 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
1117     sf_count_t l;
1118     float *d;
1119     pa_assert(s && length && sndfile);
1120
1121     d = pa_xmalloc(length);
1122
1123     pa_assert(sample_length >= length);
1124     l = (sf_count_t) (length/pa_frame_size(&sample_spec));
1125
1126     if ((sf_readf_float(sndfile, d, l)) != l) {
1127         pa_xfree(d);
1128         pa_log(_("Premature end of file"));
1129         quit(1);
1130         return;
1131     }
1132
1133     pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
1134
1135     sample_length -= length;
1136
1137     if (sample_length <= 0) {
1138         pa_stream_set_write_callback(sample_stream, NULL, NULL);
1139         pa_stream_finish_upload(sample_stream);
1140     }
1141 }
1142
1143 static const char *subscription_event_type_to_string(pa_subscription_event_type_t t) {
1144
1145     switch (t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
1146
1147     case PA_SUBSCRIPTION_EVENT_NEW:
1148         return _("new");
1149
1150     case PA_SUBSCRIPTION_EVENT_CHANGE:
1151         return _("change");
1152
1153     case PA_SUBSCRIPTION_EVENT_REMOVE:
1154         return _("remove");
1155     }
1156
1157     return _("unknown");
1158 }
1159
1160 static const char *subscription_event_facility_to_string(pa_subscription_event_type_t t) {
1161
1162     switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
1163
1164     case PA_SUBSCRIPTION_EVENT_SINK:
1165         return _("sink");
1166
1167     case PA_SUBSCRIPTION_EVENT_SOURCE:
1168         return _("source");
1169
1170     case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
1171         return _("sink-input");
1172
1173     case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
1174         return _("source-output");
1175
1176     case PA_SUBSCRIPTION_EVENT_MODULE:
1177         return _("module");
1178
1179     case PA_SUBSCRIPTION_EVENT_CLIENT:
1180         return _("client");
1181
1182     case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE:
1183         return _("sample-cache");
1184
1185     case PA_SUBSCRIPTION_EVENT_SERVER:
1186         return _("server");
1187
1188     case PA_SUBSCRIPTION_EVENT_CARD:
1189         return _("server");
1190     }
1191
1192     return _("unknown");
1193 }
1194
1195 static void context_subscribe_callback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1196     pa_assert(c);
1197
1198     printf(_("Event '%s' on %s #%u\n"),
1199            subscription_event_type_to_string(t),
1200            subscription_event_facility_to_string(t),
1201            idx);
1202     fflush(stdout);
1203 }
1204
1205 static void get_volume_control_info_callback(pa_context *c, const pa_ext_volume_api_volume_control_info *info,
1206                                              int is_last, void *userdata) {
1207     char volume_str[PA_VOLUME_SNPRINT_VERBOSE_MAX];
1208     char balance_str[PA_EXT_VOLUME_API_BVOLUME_SNPRINT_BALANCE_MAX];
1209     char *proplist_str;
1210
1211     pa_assert(c);
1212
1213     if (is_last < 0) {
1214         pa_log(_("Failed to get volume control information: %s"), pa_strerror(pa_context_errno(c)));
1215         quit(1);
1216         return;
1217     }
1218
1219     if (is_last) {
1220         complete_action();
1221         return;
1222     }
1223
1224     pa_assert(info);
1225
1226     if (action == INFO) {
1227         if (info->index == main_output_volume_control)
1228             printf(_("Main output volume control: %s\n"), info->name);
1229
1230         if (info->index == main_input_volume_control)
1231             printf(_("Main input volume control: %s\n"), info->name);
1232
1233         return;
1234     }
1235
1236     if (action == SET_VOLUME_CONTROL_VOLUME) {
1237         pa_ext_volume_api_bvolume bv;
1238
1239         if (balance_valid && bvolume.channel_map.channels != info->volume.channel_map.channels) {
1240             pa_log(_("Incompatible number of channels, expected %u channels."), info->volume.channel_map.channels);
1241             quit(1);
1242         }
1243
1244         bv = info->volume;
1245
1246         if (volume_valid) {
1247             if (volume_flags & VOL_RELATIVE) {
1248                 pa_cvolume cv;
1249
1250                 pa_cvolume_set(&cv, 1, info->volume.volume);
1251                 volume_relative_adjust(&cv, bvolume.volume);
1252                 bv.volume = cv.values[0];
1253             } else
1254                 bv.volume = bvolume.volume;
1255         }
1256
1257         if (balance_valid)
1258             memcpy(bv.balance, bvolume.balance, sizeof(bv.balance));
1259
1260         pa_operation_unref(pa_ext_volume_api_set_volume_control_volume_by_name(c, volume_control_name, &bv,
1261                                                                                volume_valid, balance_valid,
1262                                                                                simple_callback, NULL));
1263         actions++;
1264
1265         return;
1266     }
1267
1268     pa_assert(action == LIST);
1269
1270     if (nl && !short_list_format)
1271         printf("\n");
1272     nl = true;
1273
1274     if (short_list_format) {
1275         printf("%u\t%s\t%u\n", info->index, info->name, info->volume.volume);
1276         return;
1277     }
1278
1279     pa_volume_snprint_verbose(volume_str, sizeof(volume_str), info->volume.volume, info->convertible_to_dB);
1280     pa_ext_volume_api_bvolume_snprint_balance(balance_str, sizeof(balance_str), &info->volume);
1281     proplist_str = pa_proplist_to_string_sep(info->proplist, "\n\t\t");
1282
1283     printf(_("Volume Control #%u\n"
1284              "\tName: %s\n"
1285              "\tDescription: %s\n"
1286              "\tVolume: %s\n"
1287              "\tBalance: %s\n"
1288              "\tProperties: %s%s\n"),
1289              info->index,
1290              info->name,
1291              info->description,
1292              volume_str,
1293              balance_str,
1294              *proplist_str ? "\n\t\t" : _("(none)"),
1295              proplist_str);
1296
1297     pa_xfree(proplist_str);
1298 }
1299
1300 static void get_mute_control_info_callback(pa_context *c, const pa_ext_volume_api_mute_control_info *info, int is_last,
1301                                            void *userdata) {
1302     char *proplist_str;
1303
1304     pa_assert(c);
1305
1306     if (is_last < 0) {
1307         pa_log(_("Failed to get mute control information: %s"), pa_strerror(pa_context_errno(c)));
1308         quit(1);
1309         return;
1310     }
1311
1312     if (is_last) {
1313         complete_action();
1314         return;
1315     }
1316
1317     pa_assert(info);
1318
1319     if (action == INFO) {
1320         if (info->index == main_output_mute_control)
1321             printf(_("Main output mute control: %s\n"), info->name);
1322
1323         if (info->index == main_input_mute_control)
1324             printf(_("Main input mute control: %s\n"), info->name);
1325
1326         return;
1327     }
1328
1329     if (action == SET_MUTE_CONTROL_MUTE) {
1330         pa_operation_unref(pa_ext_volume_api_set_mute_control_mute_by_index(c, info->index, info->mute ? false : true,
1331                                                                                    simple_callback, NULL));
1332         actions++;
1333         return;
1334     }
1335
1336     pa_assert(action == LIST);
1337
1338     if (nl && !short_list_format)
1339         printf("\n");
1340     nl = true;
1341
1342     if (short_list_format) {
1343         printf("%u\t%s\t%s\n", info->index, info->name, pa_yes_no(info->mute));
1344         return;
1345     }
1346
1347     proplist_str = pa_proplist_to_string_sep(info->proplist, "\n\t\t");
1348
1349     printf(_("Mute Control #%u\n"
1350              "\tName: %s\n"
1351              "\tDescription: %s\n"
1352              "\tMute: %s\n"
1353              "\tProperties: %s%s\n"),
1354              info->index,
1355              info->name,
1356              info->description,
1357              pa_yes_no(info->mute),
1358              *proplist_str ? "\n\t\t" : _("(none)"),
1359              proplist_str);
1360
1361     pa_xfree(proplist_str);
1362 }
1363
1364 static void volume_api_get_server_info_callback(pa_context *c, const pa_ext_volume_api_server_info *info, void *userdata) {
1365     pa_assert(c);
1366
1367     if (!info) {
1368         pa_log(_("Failed to get server information: %s"), pa_strerror(pa_context_errno(c)));
1369         quit(1);
1370         return;
1371     }
1372
1373     main_output_volume_control = info->main_output_volume_control;
1374     main_input_volume_control = info->main_input_volume_control;
1375     main_output_mute_control = info->main_output_mute_control;
1376     main_input_mute_control = info->main_input_mute_control;
1377
1378     if (main_output_volume_control == PA_INVALID_INDEX)
1379         printf(_("Main output volume control: (unset)\n"));
1380
1381     if (main_input_volume_control == PA_INVALID_INDEX)
1382         printf(_("Main input volume control: (unset)\n"));
1383
1384     if (main_output_mute_control == PA_INVALID_INDEX)
1385         printf(_("Main output mute control: (unset)\n"));
1386
1387     if (main_input_mute_control == PA_INVALID_INDEX)
1388         printf(_("Main input mute control: (unset)\n"));
1389
1390     if (main_output_volume_control != PA_INVALID_INDEX || main_input_volume_control != PA_INVALID_INDEX) {
1391         pa_operation_unref(pa_ext_volume_api_get_volume_control_info_list(c, get_volume_control_info_callback, NULL));
1392         actions++;
1393     }
1394
1395     if (main_output_mute_control != PA_INVALID_INDEX || main_input_mute_control != PA_INVALID_INDEX) {
1396         pa_operation_unref(pa_ext_volume_api_get_mute_control_info_list(c, get_mute_control_info_callback, NULL));
1397         actions++;
1398     }
1399
1400     complete_action();
1401 }
1402
1403 static void get_device_info_callback(pa_context *c, const pa_ext_volume_api_device_info *info, int is_last,
1404                                      void *userdata) {
1405     char *device_types_str = NULL;
1406     char *volume_control_str;
1407     char *mute_control_str;
1408     char *proplist_str;
1409
1410     pa_assert(c);
1411
1412     if (is_last < 0) {
1413         pa_log(_("Failed to get device information: %s"), pa_strerror(pa_context_errno(c)));
1414         quit(1);
1415         return;
1416     }
1417
1418     if (is_last) {
1419         complete_action();
1420         return;
1421     }
1422
1423     pa_assert(info);
1424
1425     if (nl && !short_list_format)
1426         printf("\n");
1427     nl = true;
1428
1429     if (info->n_device_types > 0)
1430         device_types_str = pa_join(info->device_types, info->n_device_types, ", ");
1431     else
1432         device_types_str = pa_xstrdup(_("(none)"));
1433
1434     if (info->volume_control != PA_INVALID_INDEX)
1435         volume_control_str = pa_sprintf_malloc("%u", info->volume_control);
1436     else
1437         volume_control_str = pa_xstrdup(_("(unset)"));
1438
1439     if (info->mute_control != PA_INVALID_INDEX)
1440         mute_control_str = pa_sprintf_malloc("%u", info->mute_control);
1441     else
1442         mute_control_str = pa_xstrdup(_("(unset)"));
1443
1444     if (short_list_format) {
1445         printf("%u\t%s\t%s\t%s\t%s\t%s\n", info->index, info->name, pa_direction_to_string(info->direction), device_types_str,
1446                volume_control_str, mute_control_str);
1447         pa_xfree(mute_control_str);
1448         pa_xfree(volume_control_str);
1449         pa_xfree(device_types_str);
1450         return;
1451     }
1452
1453     proplist_str = pa_proplist_to_string_sep(info->proplist, "\n\t\t");
1454
1455     printf(_("Device #%u\n"
1456              "\tName: %s\n"
1457              "\tDescription: %s\n"
1458              "\tDirection: %s\n"
1459              "\tDevice Types: %s\n"
1460              "\tVolume Control: %s\n"
1461              "\tMute Control: %s\n"
1462              "\tProperties: %s%s\n"),
1463              info->index,
1464              info->name,
1465              info->description,
1466              pa_direction_to_string(info->direction),
1467              device_types_str,
1468              volume_control_str,
1469              mute_control_str,
1470              *proplist_str ? "\n\t\t" : _("(none)"),
1471              proplist_str);
1472
1473     pa_xfree(proplist_str);
1474     pa_xfree(mute_control_str);
1475     pa_xfree(volume_control_str);
1476     pa_xfree(device_types_str);
1477 }
1478
1479 static void get_stream_info_callback(pa_context *c, const pa_ext_volume_api_stream_info *info, int is_last,
1480                                      void *userdata) {
1481     char *volume_control_str;
1482     char *mute_control_str;
1483     char *proplist_str;
1484
1485     pa_assert(c);
1486
1487     if (is_last < 0) {
1488         pa_log(_("Failed to get stream information: %s"), pa_strerror(pa_context_errno(c)));
1489         quit(1);
1490         return;
1491     }
1492
1493     if (is_last) {
1494         complete_action();
1495         return;
1496     }
1497
1498     pa_assert(info);
1499
1500     if (nl && !short_list_format)
1501         printf("\n");
1502     nl = true;
1503
1504     if (info->volume_control != PA_INVALID_INDEX)
1505         volume_control_str = pa_sprintf_malloc("%u", info->volume_control);
1506     else
1507         volume_control_str = pa_xstrdup(_("(unset)"));
1508
1509     if (info->mute_control != PA_INVALID_INDEX)
1510         mute_control_str = pa_sprintf_malloc("%u", info->mute_control);
1511     else
1512         mute_control_str = pa_xstrdup(_("(unset)"));
1513
1514     if (short_list_format) {
1515         printf("%u\t%s\t%s\t%s\t%s\n", info->index, info->name, pa_direction_to_string(info->direction), volume_control_str,
1516                mute_control_str);
1517         pa_xfree(mute_control_str);
1518         pa_xfree(volume_control_str);
1519         return;
1520     }
1521
1522     proplist_str = pa_proplist_to_string_sep(info->proplist, "\n\t\t");
1523
1524     printf(_("Stream #%u\n"
1525              "\tName: %s\n"
1526              "\tDescription: %s\n"
1527              "\tDirection: %s\n"
1528              "\tVolume Control: %s\n"
1529              "\tMute Control: %s\n"
1530              "\tProperties: %s%s\n"),
1531              info->index,
1532              info->name,
1533              info->description,
1534              pa_direction_to_string(info->direction),
1535              volume_control_str,
1536              mute_control_str,
1537              *proplist_str ? "\n\t\t" : _("(none)"),
1538              proplist_str);
1539
1540     pa_xfree(proplist_str);
1541     pa_xfree(mute_control_str);
1542     pa_xfree(volume_control_str);
1543 }
1544
1545 static void get_audio_group_info_callback(pa_context *c, const pa_ext_volume_api_audio_group_info *info, int is_last,
1546                                           void *userdata) {
1547     char *volume_control_str;
1548     char *mute_control_str;
1549     char *proplist_str;
1550
1551     pa_assert(c);
1552
1553     if (is_last < 0) {
1554         pa_log(_("Failed to get audio group information: %s"), pa_strerror(pa_context_errno(c)));
1555         quit(1);
1556         return;
1557     }
1558
1559     if (is_last) {
1560         complete_action();
1561         return;
1562     }
1563
1564     pa_assert(info);
1565
1566     if (nl && !short_list_format)
1567         printf("\n");
1568     nl = true;
1569
1570     if (info->volume_control != PA_INVALID_INDEX)
1571         volume_control_str = pa_sprintf_malloc("%u", info->volume_control);
1572     else
1573         volume_control_str = pa_xstrdup(_("(unset)"));
1574
1575     if (info->mute_control != PA_INVALID_INDEX)
1576         mute_control_str = pa_sprintf_malloc("%u", info->mute_control);
1577     else
1578         mute_control_str = pa_xstrdup(_("(unset)"));
1579
1580     if (short_list_format) {
1581         printf("%u\t%s\t%s\t%s\n", info->index, info->name, volume_control_str, mute_control_str);
1582         pa_xfree(mute_control_str);
1583         pa_xfree(volume_control_str);
1584         return;
1585     }
1586
1587     proplist_str = pa_proplist_to_string_sep(info->proplist, "\n\t\t");
1588
1589     printf(_("Audio Group #%u\n"
1590              "\tName: %s\n"
1591              "\tDescription: %s\n"
1592              "\tVolume Control: %s\n"
1593              "\tMute Control: %s\n"
1594              "\tProperties: %s%s\n"),
1595              info->index,
1596              info->name,
1597              info->description,
1598              volume_control_str,
1599              mute_control_str,
1600              *proplist_str ? "\n\t\t" : _("(none)"),
1601              proplist_str);
1602
1603     pa_xfree(proplist_str);
1604     pa_xfree(mute_control_str);
1605     pa_xfree(volume_control_str);
1606 }
1607
1608 static const char *volume_api_subscription_event_facility_to_string(pa_ext_volume_api_subscription_event_type_t type) {
1609
1610     switch (type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
1611         case PA_EXT_VOLUME_API_SUBSCRIPTION_EVENT_SERVER:
1612             return _("server (volume API)");
1613
1614         case PA_EXT_VOLUME_API_SUBSCRIPTION_EVENT_VOLUME_CONTROL:
1615             return _("volume-control");
1616
1617         case PA_EXT_VOLUME_API_SUBSCRIPTION_EVENT_MUTE_CONTROL:
1618             return _("mute-control");
1619
1620         case PA_EXT_VOLUME_API_SUBSCRIPTION_EVENT_DEVICE:
1621             return _("device");
1622
1623         case PA_EXT_VOLUME_API_SUBSCRIPTION_EVENT_STREAM:
1624             return _("stream");
1625
1626         case PA_EXT_VOLUME_API_SUBSCRIPTION_EVENT_AUDIO_GROUP:
1627             return _("audio-group");
1628     }
1629
1630     return _("unknown");
1631 }
1632
1633 static void volume_api_subscribe_cb(pa_context *c, pa_ext_volume_api_subscription_event_type_t event_type, uint32_t idx,
1634                                     void *userdata) {
1635     pa_assert(c);
1636
1637     printf(_("Event '%s' on %s #%u\n"),
1638            subscription_event_type_to_string(event_type),
1639            volume_api_subscription_event_facility_to_string(event_type),
1640            idx);
1641     fflush(stdout);
1642 }
1643
1644 static void volume_api_state_cb(pa_context *c, void *userdata) {
1645     pa_ext_volume_api_state_t state;
1646
1647     pa_assert(c);
1648
1649     state = pa_ext_volume_api_get_state(c);
1650
1651     switch (state) {
1652         case PA_EXT_VOLUME_API_STATE_READY: {
1653             pa_operation *o = NULL;
1654
1655             volume_api_connected = true;
1656
1657             switch (action) {
1658                 case INFO:
1659                     o = pa_ext_volume_api_get_server_info(c, volume_api_get_server_info_callback, NULL);
1660                     actions++;
1661                     break;
1662
1663                 case LIST:
1664                     if (!list_type) {
1665                         o = pa_ext_volume_api_get_volume_control_info_list(c, get_volume_control_info_callback, NULL);
1666                         pa_operation_unref(o);
1667                         o = pa_ext_volume_api_get_mute_control_info_list(c, get_mute_control_info_callback, NULL);
1668                         pa_operation_unref(o);
1669                         o = pa_ext_volume_api_get_device_info_list(c, get_device_info_callback, NULL);
1670                         pa_operation_unref(o);
1671                         o = pa_ext_volume_api_get_stream_info_list(c, get_stream_info_callback, NULL);
1672                         pa_operation_unref(o);
1673                         o = pa_ext_volume_api_get_audio_group_info_list(c, get_audio_group_info_callback, NULL);
1674                         pa_operation_unref(o);
1675                         o = NULL;
1676                         actions += 5;
1677                     } else if (pa_streq(list_type, "volume-controls")) {
1678                         o = pa_ext_volume_api_get_volume_control_info_list(c, get_volume_control_info_callback, NULL);
1679                         actions++;
1680                     } else if (pa_streq(list_type, "mute-controls")) {
1681                         o = pa_ext_volume_api_get_mute_control_info_list(c, get_mute_control_info_callback, NULL);
1682                         actions++;
1683                     } else if (pa_streq(list_type, "devices")) {
1684                         o = pa_ext_volume_api_get_device_info_list(c, get_device_info_callback, NULL);
1685                         actions++;
1686                     } else if (pa_streq(list_type, "streams")) {
1687                         o = pa_ext_volume_api_get_stream_info_list(c, get_stream_info_callback, NULL);
1688                         actions++;
1689                     } else if (pa_streq(list_type, "audio-groups")) {
1690                         o = pa_ext_volume_api_get_audio_group_info_list(c, get_audio_group_info_callback, NULL);
1691                         actions++;
1692                     }
1693                     break;
1694
1695                 case SET_VOLUME_CONTROL_VOLUME:
1696                     if (!balance_valid && !(volume_flags & VOL_RELATIVE)) {
1697                         pa_assert(volume_valid);
1698                         o = pa_ext_volume_api_set_volume_control_volume_by_name(c, volume_control_name, &bvolume, true,
1699                                                                                 false, simple_callback, NULL);
1700                     } else
1701                         o = pa_ext_volume_api_get_volume_control_info_by_name(c, volume_control_name,
1702                                                                               get_volume_control_info_callback, NULL);
1703
1704                     actions++;
1705                     break;
1706
1707                 case SET_MUTE_CONTROL_MUTE:
1708                     if (mute == TOGGLE_MUTE)
1709                         o = pa_ext_volume_api_get_mute_control_info_by_name(c, mute_control_name,
1710                                                                                   get_mute_control_info_callback, NULL);
1711                     else
1712                         o = pa_ext_volume_api_set_mute_control_mute_by_name(c, mute_control_name, mute, simple_callback,
1713                                                                             NULL);
1714
1715                     actions++;
1716                     break;
1717
1718                 case SUBSCRIBE:
1719                     pa_ext_volume_api_set_subscribe_callback(c, volume_api_subscribe_cb, NULL);
1720                     o = pa_ext_volume_api_subscribe(c, PA_EXT_VOLUME_API_SUBSCRIPTION_MASK_ALL, NULL, NULL);
1721                     break;
1722
1723                 default:
1724                     break;
1725             }
1726
1727             if (o)
1728                 pa_operation_unref(o);
1729
1730             complete_action();
1731             break;
1732         }
1733
1734         case PA_EXT_VOLUME_API_STATE_FAILED:
1735             pa_log("Volume API context failed: %s", pa_strerror(pa_context_errno(c)));
1736
1737             /* If the main context failed too, let's not do anything, because
1738              * calling complete_action() would reset the context error code to
1739              * PA_ERR_BADSTATE, meaning that the original error code would be
1740              * lost. */
1741             if (pa_context_get_state(c) == PA_CONTEXT_FAILED)
1742                 break;
1743
1744             if (action == INFO || (action == LIST && !list_type) || action == SUBSCRIBE) {
1745                 /* In these cases we shouldn't exit with an error if the volume
1746                  * API happens to be or become unavailable. If we haven't yet
1747                  * connected to the volume API, then we need to complete the
1748                  * "connect to volume API" action. */
1749
1750                 if (!volume_api_connected)
1751                     complete_action();
1752             } else
1753                 quit(1);
1754
1755             break;
1756
1757         default:
1758             break;
1759     }
1760 }
1761
1762 static void connect_to_volume_api(void) {
1763     int r;
1764
1765     pa_assert(context);
1766
1767     pa_ext_volume_api_set_state_callback(context, volume_api_state_cb, NULL);
1768
1769     r = pa_ext_volume_api_connect(context);
1770     if (r >= 0)
1771         actions++;
1772 }
1773
1774 static void context_state_callback(pa_context *c, void *userdata) {
1775     pa_operation *o = NULL;
1776
1777     pa_assert(c);
1778
1779     switch (pa_context_get_state(c)) {
1780         case PA_CONTEXT_CONNECTING:
1781         case PA_CONTEXT_AUTHORIZING:
1782         case PA_CONTEXT_SETTING_NAME:
1783             break;
1784
1785         case PA_CONTEXT_READY:
1786             switch (action) {
1787                 case STAT:
1788                     o = pa_context_stat(c, stat_callback, NULL);
1789                     if (short_list_format)
1790                         break;
1791
1792                     if (o) {
1793                         pa_operation_unref(o);
1794                         actions++;
1795                     }
1796                     /* Fall through */
1797
1798                 case INFO:
1799                     o = pa_context_get_server_info(c, get_server_info_callback, NULL);
1800                     connect_to_volume_api();
1801                     break;
1802
1803                 case PLAY_SAMPLE:
1804                     o = pa_context_play_sample(c, sample_name, sink_name, PA_VOLUME_NORM, simple_callback, NULL);
1805                     break;
1806
1807                 case REMOVE_SAMPLE:
1808                     o = pa_context_remove_sample(c, sample_name, simple_callback, NULL);
1809                     break;
1810
1811                 case UPLOAD_SAMPLE:
1812                     sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
1813                     pa_assert(sample_stream);
1814
1815                     pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
1816                     pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
1817                     pa_stream_connect_upload(sample_stream, sample_length);
1818                     actions++;
1819                     break;
1820
1821                 case EXIT:
1822                     o = pa_context_exit_daemon(c, simple_callback, NULL);
1823                     break;
1824
1825                 case LIST:
1826                     if (list_type) {
1827                         if (pa_streq(list_type, "modules"))
1828                             o = pa_context_get_module_info_list(c, get_module_info_callback, NULL);
1829                         else if (pa_streq(list_type, "sinks"))
1830                             o = pa_context_get_sink_info_list(c, get_sink_info_callback, NULL);
1831                         else if (pa_streq(list_type, "sources"))
1832                             o = pa_context_get_source_info_list(c, get_source_info_callback, NULL);
1833                         else if (pa_streq(list_type, "sink-inputs"))
1834                             o = pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL);
1835                         else if (pa_streq(list_type, "source-outputs"))
1836                             o = pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL);
1837                         else if (pa_streq(list_type, "clients"))
1838                             o = pa_context_get_client_info_list(c, get_client_info_callback, NULL);
1839                         else if (pa_streq(list_type, "samples"))
1840                             o = pa_context_get_sample_info_list(c, get_sample_info_callback, NULL);
1841                         else if (pa_streq(list_type, "cards"))
1842                             o = pa_context_get_card_info_list(c, get_card_info_callback, NULL);
1843                         else if (pa_streq(list_type, "nodes"))
1844                             o = pa_ext_node_manager_read_nodes(c, node_list_callback, NULL);
1845                         else if (pa_streq(list_type, "volume-controls")
1846                                      || pa_streq(list_type, "mute-controls")
1847                                      || pa_streq(list_type, "devices")
1848                                      || pa_streq(list_type, "streams")
1849                                      || pa_streq(list_type, "audio-groups")) {
1850                             connect_to_volume_api();
1851                             o = NULL;
1852                         } else
1853                             pa_assert_not_reached();
1854                     } else {
1855                         o = pa_context_get_module_info_list(c, get_module_info_callback, NULL);
1856                         if (o) {
1857                             pa_operation_unref(o);
1858                             actions++;
1859                         }
1860
1861                         o = pa_context_get_sink_info_list(c, get_sink_info_callback, NULL);
1862                         if (o) {
1863                             pa_operation_unref(o);
1864                             actions++;
1865                         }
1866
1867                         o = pa_context_get_source_info_list(c, get_source_info_callback, NULL);
1868                         if (o) {
1869                             pa_operation_unref(o);
1870                             actions++;
1871                         }
1872                         o = pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL);
1873                         if (o) {
1874                             pa_operation_unref(o);
1875                             actions++;
1876                         }
1877
1878                         o = pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL);
1879                         if (o) {
1880                             pa_operation_unref(o);
1881                             actions++;
1882                         }
1883
1884                         o = pa_context_get_client_info_list(c, get_client_info_callback, NULL);
1885                         if (o) {
1886                             pa_operation_unref(o);
1887                             actions++;
1888                         }
1889
1890                         o = pa_context_get_sample_info_list(c, get_sample_info_callback, NULL);
1891                         if (o) {
1892                             pa_operation_unref(o);
1893                             actions++;
1894                         }
1895
1896                         o = pa_context_get_card_info_list(c, get_card_info_callback, NULL);
1897                         if (o) {
1898                             pa_operation_unref(o);
1899                             actions++;
1900                         }
1901
1902                         connect_to_volume_api();
1903                         o = NULL;
1904                     }
1905                     break;
1906
1907                 case MOVE_SINK_INPUT:
1908                     o = pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL);
1909                     break;
1910
1911                 case MOVE_SOURCE_OUTPUT:
1912                     o = pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL);
1913                     break;
1914
1915                 case LOAD_MODULE:
1916                     o = pa_context_load_module(c, module_name, module_args, index_callback, NULL);
1917                     break;
1918
1919                 case UNLOAD_MODULE:
1920                     if (module_name)
1921                         o = pa_context_get_module_info_list(c, unload_module_by_name_callback, NULL);
1922                     else
1923                         o = pa_context_unload_module(c, module_index, simple_callback, NULL);
1924                     break;
1925
1926                 case SUSPEND_SINK:
1927                     if (sink_name)
1928                         o = pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL);
1929                     else
1930                         o = pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL);
1931                     break;
1932
1933                 case SUSPEND_SOURCE:
1934                     if (source_name)
1935                         o = pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL);
1936                     else
1937                         o = pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL);
1938                     break;
1939
1940                 case SET_CARD_PROFILE:
1941                     o = pa_context_set_card_profile_by_name(c, card_name, profile_name, simple_callback, NULL);
1942                     break;
1943
1944                 case SET_SINK_PORT:
1945                     o = pa_context_set_sink_port_by_name(c, sink_name, port_name, simple_callback, NULL);
1946                     break;
1947
1948                 case SET_DEFAULT_SINK:
1949                     o = pa_context_set_default_sink(c, sink_name, simple_callback, NULL);
1950                     break;
1951
1952                 case SET_SOURCE_PORT:
1953                     o = pa_context_set_source_port_by_name(c, source_name, port_name, simple_callback, NULL);
1954                     break;
1955
1956                 case SET_DEFAULT_SOURCE:
1957                     o = pa_context_set_default_source(c, source_name, simple_callback, NULL);
1958                     break;
1959
1960                 case SET_SINK_MUTE:
1961                     if (mute == TOGGLE_MUTE)
1962                         o = pa_context_get_sink_info_by_name(c, sink_name, sink_toggle_mute_callback, NULL);
1963                     else
1964                         o = pa_context_set_sink_mute_by_name(c, sink_name, mute, simple_callback, NULL);
1965                     break;
1966
1967                 case SET_SOURCE_MUTE:
1968                     if (mute == TOGGLE_MUTE)
1969                         o = pa_context_get_source_info_by_name(c, source_name, source_toggle_mute_callback, NULL);
1970                     else
1971                         o = pa_context_set_source_mute_by_name(c, source_name, mute, simple_callback, NULL);
1972                     break;
1973
1974                 case SET_SINK_INPUT_MUTE:
1975                     if (mute == TOGGLE_MUTE)
1976                         o = pa_context_get_sink_input_info(c, sink_input_idx, sink_input_toggle_mute_callback, NULL);
1977                     else
1978                         o = pa_context_set_sink_input_mute(c, sink_input_idx, mute, simple_callback, NULL);
1979                     break;
1980
1981                 case SET_SOURCE_OUTPUT_MUTE:
1982                     if (mute == TOGGLE_MUTE)
1983                         o = pa_context_get_source_output_info(c, source_output_idx, source_output_toggle_mute_callback, NULL);
1984                     else
1985                         o = pa_context_set_source_output_mute(c, source_output_idx, mute, simple_callback, NULL);
1986                     break;
1987
1988                 case SET_SINK_VOLUME:
1989                     if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1990                         o = pa_context_get_sink_info_by_name(c, sink_name, get_sink_volume_callback, NULL);
1991                     } else {
1992                         pa_cvolume v;
1993                         pa_cvolume_set(&v, 1, volume);
1994                         o = pa_context_set_sink_volume_by_name(c, sink_name, &v, simple_callback, NULL);
1995                     }
1996                     break;
1997
1998                 case SET_SOURCE_VOLUME:
1999                     if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
2000                         o = pa_context_get_source_info_by_name(c, source_name, get_source_volume_callback, NULL);
2001                     } else {
2002                         pa_cvolume v;
2003                         pa_cvolume_set(&v, 1, volume);
2004                         o = pa_context_set_source_volume_by_name(c, source_name, &v, simple_callback, NULL);
2005                     }
2006                     break;
2007
2008                 case SET_SINK_INPUT_VOLUME:
2009                     if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
2010                         o = pa_context_get_sink_input_info(c, sink_input_idx, get_sink_input_volume_callback, NULL);
2011                     } else {
2012                         pa_cvolume v;
2013                         pa_cvolume_set(&v, 1, volume);
2014                         o = pa_context_set_sink_input_volume(c, sink_input_idx, &v, simple_callback, NULL);
2015                     }
2016                     break;
2017
2018                 case SET_SOURCE_OUTPUT_VOLUME:
2019                     if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
2020                         o = pa_context_get_source_output_info(c, source_output_idx, get_source_output_volume_callback, NULL);
2021                     } else {
2022                         pa_cvolume v;
2023                         pa_cvolume_set(&v, 1, volume);
2024                         o = pa_context_set_source_output_volume(c, source_output_idx, &v, simple_callback, NULL);
2025                     }
2026                     break;
2027
2028                 case SET_SINK_FORMATS:
2029                     set_sink_formats(c, sink_idx, formats);
2030                     break;
2031
2032                 case SET_PORT_LATENCY_OFFSET:
2033                     o = pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL);
2034                     break;
2035
2036                 case SET_VOLUME_CONTROL_VOLUME:
2037                 case SET_MUTE_CONTROL_MUTE:
2038                     connect_to_volume_api();
2039                     break;
2040
2041                 case SUBSCRIBE:
2042                     pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL);
2043
2044                     o = pa_context_subscribe(c,
2045                                              PA_SUBSCRIPTION_MASK_SINK|
2046                                              PA_SUBSCRIPTION_MASK_SOURCE|
2047                                              PA_SUBSCRIPTION_MASK_SINK_INPUT|
2048                                              PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT|
2049                                              PA_SUBSCRIPTION_MASK_MODULE|
2050                                              PA_SUBSCRIPTION_MASK_CLIENT|
2051                                              PA_SUBSCRIPTION_MASK_SAMPLE_CACHE|
2052                                              PA_SUBSCRIPTION_MASK_SERVER|
2053                                              PA_SUBSCRIPTION_MASK_CARD,
2054                                              NULL,
2055                                              NULL);
2056
2057                     if (o) {
2058                         pa_operation_unref(o);
2059                         actions++;
2060                         o = NULL;
2061                     }
2062
2063                     connect_to_volume_api();
2064                     break;
2065
2066                 case NODE_CONNECT:
2067                     pa_operation_unref(pa_ext_node_manager_connect_nodes(c,
2068                                        src_node_id,
2069                                        dst_node_id,
2070                                        node_connect_callback,
2071                                        NULL));
2072                     break;
2073
2074                 case NODE_DISCONNECT:
2075                     pa_operation_unref(pa_ext_node_manager_disconnect_nodes(c, conn_id,
2076                                        simple_callback,
2077                                        NULL));
2078                     break;
2079
2080                 default:
2081                     pa_assert_not_reached();
2082             }
2083
2084             if (o) {
2085                 pa_operation_unref(o);
2086                 actions++;
2087             }
2088
2089             if (actions == 0) {
2090                 pa_log("Operation failed: %s", pa_strerror(pa_context_errno(c)));
2091                 quit(1);
2092             }
2093
2094             break;
2095
2096         case PA_CONTEXT_TERMINATED:
2097             quit(0);
2098             break;
2099
2100         case PA_CONTEXT_FAILED:
2101         default:
2102             pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c)));
2103             quit(1);
2104     }
2105 }
2106
2107 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
2108     pa_log(_("Got SIGINT, exiting."));
2109     quit(0);
2110 }
2111
2112 static int parse_volume(const char *vol_spec, pa_volume_t *vol, enum volume_flags *vol_flags) {
2113     double v;
2114     char *vs;
2115
2116     pa_assert(vol_spec);
2117     pa_assert(vol);
2118     pa_assert(vol_flags);
2119
2120     vs = pa_xstrdup(vol_spec);
2121
2122     *vol_flags = (pa_startswith(vs, "+") || pa_startswith(vs, "-")) ? VOL_RELATIVE : VOL_ABSOLUTE;
2123     if (strchr(vs, '.'))
2124         *vol_flags |= VOL_LINEAR;
2125     if (pa_endswith(vs, "%")) {
2126         *vol_flags |= VOL_PERCENT;
2127         vs[strlen(vs)-1] = 0;
2128     }
2129     if (pa_endswith(vs, "db") || pa_endswith(vs, "dB")) {
2130         *vol_flags |= VOL_DECIBEL;
2131         vs[strlen(vs)-2] = 0;
2132     }
2133
2134     if (pa_atod(vs, &v) < 0) {
2135         pa_log(_("Invalid volume specification"));
2136         pa_xfree(vs);
2137         return -1;
2138     }
2139
2140     pa_xfree(vs);
2141
2142     if ((*vol_flags & VOL_RELATIVE) == VOL_RELATIVE) {
2143         if ((*vol_flags & 0x0F) == VOL_UINT)
2144             v += (double) PA_VOLUME_NORM;
2145         if ((*vol_flags & 0x0F) == VOL_PERCENT)
2146             v += 100.0;
2147         if ((*vol_flags & 0x0F) == VOL_LINEAR)
2148             v += 1.0;
2149     }
2150     if ((*vol_flags & 0x0F) == VOL_PERCENT)
2151         v = v * (double) PA_VOLUME_NORM / 100;
2152     if ((*vol_flags & 0x0F) == VOL_LINEAR)
2153         v = pa_sw_volume_from_linear(v);
2154     if ((*vol_flags & 0x0F) == VOL_DECIBEL)
2155         v = pa_sw_volume_from_dB(v);
2156
2157     if (!PA_VOLUME_IS_VALID((pa_volume_t) v)) {
2158         pa_log(_("Volume outside permissible range.\n"));
2159         return -1;
2160     }
2161
2162     *vol = (pa_volume_t) v;
2163
2164     return 0;
2165 }
2166
2167 static enum mute_flags parse_mute(const char *mute_text) {
2168     int b;
2169
2170     pa_assert(mute_text);
2171
2172     if (pa_streq("toggle", mute_text))
2173         return TOGGLE_MUTE;
2174
2175     b = pa_parse_boolean(mute_text);
2176     switch (b) {
2177         case 0:
2178             return UNMUTE;
2179         case 1:
2180             return MUTE;
2181         default:
2182             return INVALID_MUTE;
2183     }
2184 }
2185
2186 static void help(const char *argv0) {
2187
2188     printf("%s %s %s\n",    argv0, _("[options]"), "stat [short]");
2189     printf("%s %s %s\n",    argv0, _("[options]"), "info");
2190     printf("%s %s %s %s\n", argv0, _("[options]"), "list [short]", _("[TYPE]"));
2191     printf("%s %s %s\n",    argv0, _("[options]"), "exit");
2192     printf("%s %s %s %s\n", argv0, _("[options]"), "upload-sample", _("FILENAME [NAME]"));
2193     printf("%s %s %s %s\n", argv0, _("[options]"), "play-sample ", _("NAME [SINK]"));
2194     printf("%s %s %s %s\n", argv0, _("[options]"), "remove-sample ", _("NAME"));
2195     printf("%s %s %s %s\n", argv0, _("[options]"), "load-module ", _("NAME [ARGS ...]"));
2196     printf("%s %s %s %s\n", argv0, _("[options]"), "unload-module ", _("NAME|#N"));
2197     printf("%s %s %s %s\n", argv0, _("[options]"), "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
2198     printf("%s %s %s %s\n", argv0, _("[options]"), "suspend-(sink|source)", _("NAME|#N 1|0"));
2199     printf("%s %s %s %s\n", argv0, _("[options]"), "set-card-profile ", _("CARD PROFILE"));
2200     printf("%s %s %s %s\n", argv0, _("[options]"), "set-default-(sink|source)", _("NAME"));
2201     printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-port", _("NAME|#N PORT"));
2202     printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-volume", _("NAME|#N VOLUME"));
2203     printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-volume", _("#N VOLUME"));
2204     printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-mute", _("NAME|#N 1|0|toggle"));
2205     printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0|toggle"));
2206     printf("%s %s %s %s\n", argv0, _("[options]"), "set-sink-formats", _("#N FORMATS"));
2207     printf("%s %s %s %s\n", argv0, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
2208     printf("%s %s %s %s\n", argv0, _("[options]"), "set-volume-control-volume", _("NAME|#N VOLUME [BALANCE ...]"));
2209     printf("%s %s %s %s\n", argv0, _("[options]"), "set-volume-control-balance", _("NAME|#N BALANCE ..."));
2210     printf("%s %s %s %s\n", argv0, _("[options]"), "set-mute-control-mute", _("NAME|#N 1|0|toggle"));
2211     printf("%s %s %s\n",    argv0, _("[options]"), "subscribe");
2212     printf(_("\nThe special names @DEFAULT_SINK@, @DEFAULT_SOURCE@ and @DEFAULT_MONITOR@\n"
2213              "can be used to specify the default sink, source and monitor.\n"));
2214     printf("%s %s %s\n",    argv0, _("[options]"), "node-list ");
2215     printf("%s %s %s %s %s\n", argv0, _("[options]"), "node-connect ", _("#N"), _("#N"));
2216     printf("%s %s %s %s\n", argv0, _("[options]"), "node-disconnect ", _("#N"));
2217
2218     printf(_("\n"
2219              "  -h, --help                            Show this help\n"
2220              "      --version                         Show version\n\n"
2221              "  -s, --server=SERVER                   The name of the server to connect to\n"
2222              "  -n, --client-name=NAME                How to call this client on the server\n"));
2223 }
2224
2225 static int parse_balance(char *argv[], unsigned first_arg, unsigned n_channels, pa_ext_volume_api_bvolume *bv) {
2226     pa_ext_volume_api_bvolume bv_local;
2227     unsigned i;
2228
2229     pa_assert(n_channels > 0);
2230     pa_assert(bv);
2231
2232     if (n_channels > PA_CHANNELS_MAX) {
2233         pa_log("Too many channels, the maximum is %u.", PA_CHANNELS_MAX);
2234         return -1;
2235     }
2236
2237     bv_local = *bv;
2238
2239     for (i = 0; i < n_channels; i++) {
2240         const char *balance_str;
2241         double balance;
2242
2243         balance_str = argv[first_arg + i];
2244
2245         if (pa_atod(balance_str, &balance) < 0 || !pa_ext_volume_api_balance_valid(balance)) {
2246             pa_log(_("Invalid balance value: %s"), balance_str);
2247             return -1;
2248         }
2249
2250         bv_local.balance[i] = balance;
2251     }
2252
2253     bv_local.channel_map.channels = n_channels;
2254     *bv = bv_local;
2255
2256     return 0;
2257 }
2258
2259 enum {
2260     ARG_VERSION = 256
2261 };
2262
2263 int main(int argc, char *argv[]) {
2264     pa_mainloop *m = NULL;
2265     int ret = 1, c;
2266     char *server = NULL, *bn;
2267
2268     static const struct option long_options[] = {
2269         {"server",      1, NULL, 's'},
2270         {"client-name", 1, NULL, 'n'},
2271         {"version",     0, NULL, ARG_VERSION},
2272         {"help",        0, NULL, 'h'},
2273         {NULL,          0, NULL, 0}
2274     };
2275
2276     setlocale(LC_ALL, "");
2277 #ifdef ENABLE_NLS
2278     bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
2279 #endif
2280
2281     bn = pa_path_get_filename(argv[0]);
2282
2283     proplist = pa_proplist_new();
2284
2285     while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
2286         switch (c) {
2287             case 'h' :
2288                 help(bn);
2289                 ret = 0;
2290                 goto quit;
2291
2292             case ARG_VERSION:
2293                 printf(_("pactl %s\n"
2294                          "Compiled with libpulse %s\n"
2295                          "Linked with libpulse %s\n"),
2296                        PACKAGE_VERSION,
2297                        pa_get_headers_version(),
2298                        pa_get_library_version());
2299                 ret = 0;
2300                 goto quit;
2301
2302             case 's':
2303                 pa_xfree(server);
2304                 server = pa_xstrdup(optarg);
2305                 break;
2306
2307             case 'n': {
2308                 char *t;
2309
2310                 if (!(t = pa_locale_to_utf8(optarg)) ||
2311                     pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t) < 0) {
2312
2313                     pa_log(_("Invalid client name '%s'"), t ? t : optarg);
2314                     pa_xfree(t);
2315                     goto quit;
2316                 }
2317
2318                 pa_xfree(t);
2319                 break;
2320             }
2321
2322             default:
2323                 goto quit;
2324         }
2325     }
2326
2327     if (optind < argc) {
2328         if (pa_streq(argv[optind], "stat")) {
2329             action = STAT;
2330             short_list_format = false;
2331             if (optind+1 < argc && pa_streq(argv[optind+1], "short"))
2332                 short_list_format = true;
2333
2334         } else if (pa_streq(argv[optind], "info"))
2335             action = INFO;
2336
2337         else if (pa_streq(argv[optind], "exit"))
2338             action = EXIT;
2339
2340         else if (pa_streq(argv[optind], "list")) {
2341             action = LIST;
2342
2343             for (int i = optind+1; i < argc; i++) {
2344                 if (pa_streq(argv[i], "modules")         || pa_streq(argv[i], "clients") ||
2345                     pa_streq(argv[i], "sinks")           || pa_streq(argv[i], "sink-inputs") ||
2346                     pa_streq(argv[i], "sources")         || pa_streq(argv[i], "source-outputs") ||
2347                     pa_streq(argv[i], "samples")         || pa_streq(argv[i], "cards") || pa_streq(argv[i], "nodes") ||
2348                     pa_streq(argv[i], "volume-controls") || pa_streq(argv[i], "mute-controls") ||
2349                     pa_streq(argv[i], "devices")         || pa_streq(argv[i], "streams") ||
2350                     pa_streq(argv[i], "audio-groups")) {
2351                     list_type = pa_xstrdup(argv[i]);
2352                 } else if (pa_streq(argv[i], "short")) {
2353                     short_list_format = true;
2354                 } else {
2355                     pa_log(_("Specify nothing, or one of: %s"), "modules, sinks, sources, sink-inputs, source-outputs, "
2356                              "clients, samples, cards, volume-controls, mute-controls, devices, streams, audio-groups");
2357                     goto quit;
2358                 }
2359             }
2360
2361         } else if (pa_streq(argv[optind], "upload-sample")) {
2362             struct SF_INFO sfi;
2363             action = UPLOAD_SAMPLE;
2364
2365             if (optind+1 >= argc) {
2366                 pa_log(_("Please specify a sample file to load"));
2367                 goto quit;
2368             }
2369
2370             if (optind+2 < argc)
2371                 sample_name = pa_xstrdup(argv[optind+2]);
2372             else {
2373                 char *f = pa_path_get_filename(argv[optind+1]);
2374                 sample_name = pa_xstrndup(f, strcspn(f, "."));
2375             }
2376
2377             pa_zero(sfi);
2378             if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfi))) {
2379                 pa_log(_("Failed to open sound file."));
2380                 goto quit;
2381             }
2382
2383             if (pa_sndfile_read_sample_spec(sndfile, &sample_spec) < 0) {
2384                 pa_log(_("Failed to determine sample specification from file."));
2385                 goto quit;
2386             }
2387             sample_spec.format = PA_SAMPLE_FLOAT32;
2388
2389             if (pa_sndfile_read_channel_map(sndfile, &channel_map) < 0) {
2390                 if (sample_spec.channels > 2)
2391                     pa_log(_("Warning: Failed to determine sample specification from file."));
2392                 pa_channel_map_init_extend(&channel_map, sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
2393             }
2394
2395             pa_assert(pa_channel_map_compatible(&channel_map, &sample_spec));
2396             sample_length = (size_t) sfi.frames*pa_frame_size(&sample_spec);
2397
2398         } else if (pa_streq(argv[optind], "play-sample")) {
2399             action = PLAY_SAMPLE;
2400             if (argc != optind+2 && argc != optind+3) {
2401                 pa_log(_("You have to specify a sample name to play"));
2402                 goto quit;
2403             }
2404
2405             sample_name = pa_xstrdup(argv[optind+1]);
2406
2407             if (optind+2 < argc)
2408                 sink_name = pa_xstrdup(argv[optind+2]);
2409
2410         } else if (pa_streq(argv[optind], "remove-sample")) {
2411             action = REMOVE_SAMPLE;
2412             if (argc != optind+2) {
2413                 pa_log(_("You have to specify a sample name to remove"));
2414                 goto quit;
2415             }
2416
2417             sample_name = pa_xstrdup(argv[optind+1]);
2418
2419         } else if (pa_streq(argv[optind], "move-sink-input")) {
2420             action = MOVE_SINK_INPUT;
2421             if (argc != optind+3) {
2422                 pa_log(_("You have to specify a sink input index and a sink"));
2423                 goto quit;
2424             }
2425
2426             sink_input_idx = (uint32_t) atoi(argv[optind+1]);
2427             sink_name = pa_xstrdup(argv[optind+2]);
2428
2429         } else if (pa_streq(argv[optind], "move-source-output")) {
2430             action = MOVE_SOURCE_OUTPUT;
2431             if (argc != optind+3) {
2432                 pa_log(_("You have to specify a source output index and a source"));
2433                 goto quit;
2434             }
2435
2436             source_output_idx = (uint32_t) atoi(argv[optind+1]);
2437             source_name = pa_xstrdup(argv[optind+2]);
2438
2439         } else if (pa_streq(argv[optind], "load-module")) {
2440             int i;
2441             size_t n = 0;
2442             char *p;
2443
2444             action = LOAD_MODULE;
2445
2446             if (argc <= optind+1) {
2447                 pa_log(_("You have to specify a module name and arguments."));
2448                 goto quit;
2449             }
2450
2451             module_name = argv[optind+1];
2452
2453             for (i = optind+2; i < argc; i++)
2454                 n += strlen(argv[i])+1;
2455
2456             if (n > 0) {
2457                 p = module_args = pa_xmalloc(n);
2458
2459                 for (i = optind+2; i < argc; i++)
2460                     p += sprintf(p, "%s%s", p == module_args ? "" : " ", argv[i]);
2461             }
2462
2463         } else if (pa_streq(argv[optind], "unload-module")) {
2464             action = UNLOAD_MODULE;
2465
2466             if (argc != optind+2) {
2467                 pa_log(_("You have to specify a module index or name"));
2468                 goto quit;
2469             }
2470
2471             if (pa_atou(argv[optind + 1], &module_index) < 0)
2472                 module_name = argv[optind + 1];
2473
2474         } else if (pa_streq(argv[optind], "suspend-sink")) {
2475             int b;
2476
2477             action = SUSPEND_SINK;
2478
2479             if (argc > optind+3 || optind+1 >= argc) {
2480                 pa_log(_("You may not specify more than one sink. You have to specify a boolean value."));
2481                 goto quit;
2482             }
2483
2484             if ((b = pa_parse_boolean(argv[argc-1])) < 0) {
2485                 pa_log(_("Invalid suspend specification."));
2486                 goto quit;
2487             }
2488
2489             suspend = !!b;
2490
2491             if (argc > optind+2)
2492                 sink_name = pa_xstrdup(argv[optind+1]);
2493
2494         } else if (pa_streq(argv[optind], "suspend-source")) {
2495             int b;
2496
2497             action = SUSPEND_SOURCE;
2498
2499             if (argc > optind+3 || optind+1 >= argc) {
2500                 pa_log(_("You may not specify more than one source. You have to specify a boolean value."));
2501                 goto quit;
2502             }
2503
2504             if ((b = pa_parse_boolean(argv[argc-1])) < 0) {
2505                 pa_log(_("Invalid suspend specification."));
2506                 goto quit;
2507             }
2508
2509             suspend = !!b;
2510
2511             if (argc > optind+2)
2512                 source_name = pa_xstrdup(argv[optind+1]);
2513         } else if (pa_streq(argv[optind], "set-card-profile")) {
2514             action = SET_CARD_PROFILE;
2515
2516             if (argc != optind+3) {
2517                 pa_log(_("You have to specify a card name/index and a profile name"));
2518                 goto quit;
2519             }
2520
2521             card_name = pa_xstrdup(argv[optind+1]);
2522             profile_name = pa_xstrdup(argv[optind+2]);
2523
2524         } else if (pa_streq(argv[optind], "set-sink-port")) {
2525             action = SET_SINK_PORT;
2526
2527             if (argc != optind+3) {
2528                 pa_log(_("You have to specify a sink name/index and a port name"));
2529                 goto quit;
2530             }
2531
2532             sink_name = pa_xstrdup(argv[optind+1]);
2533             port_name = pa_xstrdup(argv[optind+2]);
2534
2535         } else if (pa_streq(argv[optind], "set-default-sink")) {
2536             action = SET_DEFAULT_SINK;
2537
2538             if (argc != optind+2) {
2539                 pa_log(_("You have to specify a sink name"));
2540                 goto quit;
2541             }
2542
2543             sink_name = pa_xstrdup(argv[optind+1]);
2544
2545         } else if (pa_streq(argv[optind], "set-source-port")) {
2546             action = SET_SOURCE_PORT;
2547
2548             if (argc != optind+3) {
2549                 pa_log(_("You have to specify a source name/index and a port name"));
2550                 goto quit;
2551             }
2552
2553             source_name = pa_xstrdup(argv[optind+1]);
2554             port_name = pa_xstrdup(argv[optind+2]);
2555
2556         } else if (pa_streq(argv[optind], "set-default-source")) {
2557             action = SET_DEFAULT_SOURCE;
2558
2559             if (argc != optind+2) {
2560                 pa_log(_("You have to specify a source name"));
2561                 goto quit;
2562             }
2563
2564             source_name = pa_xstrdup(argv[optind+1]);
2565
2566         } else if (pa_streq(argv[optind], "set-sink-volume")) {
2567             action = SET_SINK_VOLUME;
2568
2569             if (argc != optind+3) {
2570                 pa_log(_("You have to specify a sink name/index and a volume"));
2571                 goto quit;
2572             }
2573
2574             sink_name = pa_xstrdup(argv[optind+1]);
2575
2576             if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
2577                 goto quit;
2578
2579         } else if (pa_streq(argv[optind], "set-source-volume")) {
2580             action = SET_SOURCE_VOLUME;
2581
2582             if (argc != optind+3) {
2583                 pa_log(_("You have to specify a source name/index and a volume"));
2584                 goto quit;
2585             }
2586
2587             source_name = pa_xstrdup(argv[optind+1]);
2588
2589             if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
2590                 goto quit;
2591
2592         } else if (pa_streq(argv[optind], "set-sink-input-volume")) {
2593             action = SET_SINK_INPUT_VOLUME;
2594
2595             if (argc != optind+3) {
2596                 pa_log(_("You have to specify a sink input index and a volume"));
2597                 goto quit;
2598             }
2599
2600             if (pa_atou(argv[optind+1], &sink_input_idx) < 0) {
2601                 pa_log(_("Invalid sink input index"));
2602                 goto quit;
2603             }
2604
2605             if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
2606                 goto quit;
2607
2608         } else if (pa_streq(argv[optind], "set-source-output-volume")) {
2609             action = SET_SOURCE_OUTPUT_VOLUME;
2610
2611             if (argc != optind+3) {
2612                 pa_log(_("You have to specify a source output index and a volume"));
2613                 goto quit;
2614             }
2615
2616             if (pa_atou(argv[optind+1], &source_output_idx) < 0) {
2617                 pa_log(_("Invalid source output index"));
2618                 goto quit;
2619             }
2620
2621             if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
2622                 goto quit;
2623
2624         } else if (pa_streq(argv[optind], "set-sink-mute")) {
2625             action = SET_SINK_MUTE;
2626
2627             if (argc != optind+3) {
2628                 pa_log(_("You have to specify a sink name/index and a mute boolean"));
2629                 goto quit;
2630             }
2631
2632             if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
2633                 pa_log(_("Invalid mute specification"));
2634                 goto quit;
2635             }
2636
2637             sink_name = pa_xstrdup(argv[optind+1]);
2638
2639         } else if (pa_streq(argv[optind], "set-source-mute")) {
2640             action = SET_SOURCE_MUTE;
2641
2642             if (argc != optind+3) {
2643                 pa_log(_("You have to specify a source name/index and a mute boolean"));
2644                 goto quit;
2645             }
2646
2647             if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
2648                 pa_log(_("Invalid mute specification"));
2649                 goto quit;
2650             }
2651
2652             source_name = pa_xstrdup(argv[optind+1]);
2653
2654         } else if (pa_streq(argv[optind], "set-sink-input-mute")) {
2655             action = SET_SINK_INPUT_MUTE;
2656
2657             if (argc != optind+3) {
2658                 pa_log(_("You have to specify a sink input index and a mute boolean"));
2659                 goto quit;
2660             }
2661
2662             if (pa_atou(argv[optind+1], &sink_input_idx) < 0) {
2663                 pa_log(_("Invalid sink input index specification"));
2664                 goto quit;
2665             }
2666
2667             if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
2668                 pa_log(_("Invalid mute specification"));
2669                 goto quit;
2670             }
2671
2672         } else if (pa_streq(argv[optind], "set-source-output-mute")) {
2673             action = SET_SOURCE_OUTPUT_MUTE;
2674
2675             if (argc != optind+3) {
2676                 pa_log(_("You have to specify a source output index and a mute boolean"));
2677                 goto quit;
2678             }
2679
2680             if (pa_atou(argv[optind+1], &source_output_idx) < 0) {
2681                 pa_log(_("Invalid source output index specification"));
2682                 goto quit;
2683             }
2684
2685             if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
2686                 pa_log(_("Invalid mute specification"));
2687                 goto quit;
2688             }
2689
2690         } else if (pa_streq(argv[optind], "subscribe"))
2691
2692             action = SUBSCRIBE;
2693
2694         else if (pa_streq(argv[optind], "set-sink-formats")) {
2695             int32_t tmp;
2696
2697             if (argc != optind+3 || pa_atoi(argv[optind+1], &tmp) < 0) {
2698                 pa_log(_("You have to specify a sink index and a semicolon-separated list of supported formats"));
2699                 goto quit;
2700             }
2701
2702             sink_idx = tmp;
2703             action = SET_SINK_FORMATS;
2704             formats = pa_xstrdup(argv[optind+2]);
2705
2706         } else if (pa_streq(argv[optind], "set-port-latency-offset")) {
2707             action = SET_PORT_LATENCY_OFFSET;
2708
2709             if (argc != optind+4) {
2710                 pa_log(_("You have to specify a card name/index, a port name and a latency offset"));
2711                 goto quit;
2712             }
2713
2714             card_name = pa_xstrdup(argv[optind+1]);
2715             port_name = pa_xstrdup(argv[optind+2]);
2716             if (pa_atoi(argv[optind + 3], &latency_offset) < 0) {
2717                 pa_log(_("Could not parse latency offset"));
2718                 goto quit;
2719             }
2720
2721         } else if (pa_streq(argv[optind], "node-connect")) {
2722             action = NODE_CONNECT;
2723
2724             if (argc != optind+3) {
2725                 pa_log(_("You have to specify a source and destination node indexes"));
2726                 goto quit;
2727             }
2728
2729             src_node_id = (uint32_t) atoi(argv[optind+1]);
2730             dst_node_id = (uint32_t) atoi(argv[optind+2]);
2731
2732         } else if (pa_streq(argv[optind], "node-disconnect")) {
2733             action = NODE_DISCONNECT;
2734
2735             if (argc != optind+2) {
2736                 pa_log(_("You have to specify a connection id"));
2737                 goto quit;
2738             }
2739
2740             conn_id = (uint32_t) atoi(argv[optind+1]);
2741
2742         } else if (pa_streq(argv[optind], "set-volume-control-volume")) {
2743             action = SET_VOLUME_CONTROL_VOLUME;
2744
2745             if (argc < optind + 3) {
2746                 pa_log(_("You have to specify a volume control name/index and a volume, and optionally balance parameters."));
2747                 goto quit;
2748             }
2749
2750             volume_control_name = pa_xstrdup(argv[optind + 1]);
2751
2752             if (parse_volume(argv[optind + 2], &bvolume.volume, &volume_flags) < 0)
2753                 goto quit;
2754
2755             volume_valid = true;
2756
2757             if (argc > optind + 3) {
2758                 if (parse_balance(argv, optind + 3, argc - (optind + 3), &bvolume) < 0)
2759                     goto quit;
2760
2761                 balance_valid = true;
2762             }
2763
2764         } else if (pa_streq(argv[optind], "set-volume-control-balance")) {
2765             action = SET_VOLUME_CONTROL_VOLUME;
2766
2767             if (argc < optind + 3) {
2768                 pa_log(_("You have to specify a volume control name/index and balance parameters."));
2769                 goto quit;
2770             }
2771
2772             volume_control_name = pa_xstrdup(argv[optind + 1]);
2773
2774             if (parse_balance(argv, optind + 2, argc - (optind + 2), &bvolume) < 0)
2775                 goto quit;
2776
2777             balance_valid = true;
2778
2779         } else if (pa_streq(argv[optind], "set-mute-control-mute")) {
2780             action = SET_MUTE_CONTROL_MUTE;
2781
2782             if (argc != optind + 3) {
2783                 pa_log(_("You have to specify a mute control name/index and a mute value."));
2784                 goto quit;
2785             }
2786
2787             mute_control_name = pa_xstrdup(argv[optind + 1]);
2788
2789             if ((mute = parse_mute(argv[optind + 2])) == INVALID_MUTE) {
2790                 pa_log(_("Invalid mute specification"));
2791                 goto quit;
2792             }
2793
2794         } else if (pa_streq(argv[optind], "help")) {
2795             help(bn);
2796             ret = 0;
2797             goto quit;
2798         }
2799     }
2800
2801     if (action == NONE) {
2802         pa_log(_("No valid command specified."));
2803         goto quit;
2804     }
2805
2806     if (!(m = pa_mainloop_new())) {
2807         pa_log(_("pa_mainloop_new() failed."));
2808         goto quit;
2809     }
2810
2811     mainloop_api = pa_mainloop_get_api(m);
2812
2813     pa_assert_se(pa_signal_init(mainloop_api) == 0);
2814     pa_signal_new(SIGINT, exit_signal_callback, NULL);
2815     pa_signal_new(SIGTERM, exit_signal_callback, NULL);
2816     pa_disable_sigpipe();
2817
2818     if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) {
2819         pa_log(_("pa_context_new() failed."));
2820         goto quit;
2821     }
2822
2823     pa_context_set_state_callback(context, context_state_callback, NULL);
2824     if (pa_context_connect(context, server, 0, NULL) < 0) {
2825         pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
2826         goto quit;
2827     }
2828
2829     if (pa_mainloop_run(m, &ret) < 0) {
2830         pa_log(_("pa_mainloop_run() failed."));
2831         goto quit;
2832     }
2833
2834 quit:
2835     if (sample_stream)
2836         pa_stream_unref(sample_stream);
2837
2838     if (context)
2839         pa_context_unref(context);
2840
2841     if (m) {
2842         pa_signal_done();
2843         pa_mainloop_free(m);
2844     }
2845
2846     pa_xfree(server);
2847     pa_xfree(list_type);
2848     pa_xfree(sample_name);
2849     pa_xfree(sink_name);
2850     pa_xfree(source_name);
2851     pa_xfree(module_args);
2852     pa_xfree(card_name);
2853     pa_xfree(profile_name);
2854     pa_xfree(port_name);
2855     pa_xfree(formats);
2856     pa_xfree(mute_control_name);
2857     pa_xfree(volume_control_name);
2858
2859     if (sndfile)
2860         sf_close(sndfile);
2861
2862     if (proplist)
2863         pa_proplist_free(proplist);
2864
2865     return ret;
2866 }