modargs: New function: pa_modargs_get_value_double().
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / utils / pactl.c
blobe15520c7c2edc32b5568a8c37adab298947bf862
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
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>
37 #include <sndfile.h>
39 #include <pulse/pulseaudio.h>
40 #include <pulse/ext-device-restore.h>
42 #include <pulsecore/i18n.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/sndfile-util.h>
48 static pa_context *context = NULL;
49 static pa_mainloop_api *mainloop_api = NULL;
51 static char
52 *list_type = NULL,
53 *sample_name = NULL,
54 *sink_name = NULL,
55 *source_name = NULL,
56 *module_name = NULL,
57 *module_args = NULL,
58 *card_name = NULL,
59 *profile_name = NULL,
60 *port_name = NULL,
61 *formats = NULL;
63 static uint32_t
64 sink_input_idx = PA_INVALID_INDEX,
65 source_output_idx = PA_INVALID_INDEX,
66 sink_idx = PA_INVALID_INDEX;
68 static pa_bool_t short_list_format = FALSE;
69 static uint32_t module_index;
70 static int32_t latency_offset;
71 static pa_bool_t suspend;
72 static pa_bool_t mute;
73 static pa_volume_t volume;
74 static enum volume_flags {
75 VOL_UINT = 0,
76 VOL_PERCENT = 1,
77 VOL_LINEAR = 2,
78 VOL_DECIBEL = 3,
79 VOL_ABSOLUTE = 0 << 4,
80 VOL_RELATIVE = 1 << 4,
81 } volume_flags;
83 static pa_proplist *proplist = NULL;
85 static SNDFILE *sndfile = NULL;
86 static pa_stream *sample_stream = NULL;
87 static pa_sample_spec sample_spec;
88 static pa_channel_map channel_map;
89 static size_t sample_length = 0;
90 static int actions = 1;
92 static pa_bool_t nl = FALSE;
94 static enum {
95 NONE,
96 EXIT,
97 STAT,
98 INFO,
99 UPLOAD_SAMPLE,
100 PLAY_SAMPLE,
101 REMOVE_SAMPLE,
102 LIST,
103 MOVE_SINK_INPUT,
104 MOVE_SOURCE_OUTPUT,
105 LOAD_MODULE,
106 UNLOAD_MODULE,
107 SUSPEND_SINK,
108 SUSPEND_SOURCE,
109 SET_CARD_PROFILE,
110 SET_SINK_PORT,
111 SET_SOURCE_PORT,
112 SET_SINK_VOLUME,
113 SET_SOURCE_VOLUME,
114 SET_SINK_INPUT_VOLUME,
115 SET_SOURCE_OUTPUT_VOLUME,
116 SET_SINK_MUTE,
117 SET_SOURCE_MUTE,
118 SET_SINK_INPUT_MUTE,
119 SET_SOURCE_OUTPUT_MUTE,
120 SET_SINK_FORMATS,
121 SET_PORT_LATENCY_OFFSET,
122 SUBSCRIBE
123 } action = NONE;
125 static void quit(int ret) {
126 pa_assert(mainloop_api);
127 mainloop_api->quit(mainloop_api, ret);
130 static void context_drain_complete(pa_context *c, void *userdata) {
131 pa_context_disconnect(c);
134 static void drain(void) {
135 pa_operation *o;
137 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
138 pa_context_disconnect(context);
139 else
140 pa_operation_unref(o);
143 static void complete_action(void) {
144 pa_assert(actions > 0);
146 if (!(--actions))
147 drain();
150 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
151 char s[PA_BYTES_SNPRINT_MAX];
152 if (!i) {
153 pa_log(_("Failed to get statistics: %s"), pa_strerror(pa_context_errno(c)));
154 quit(1);
155 return;
158 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
159 printf(_("Currently in use: %u blocks containing %s bytes total.\n"), i->memblock_total, s);
161 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
162 printf(_("Allocated during whole lifetime: %u blocks containing %s bytes total.\n"), i->memblock_allocated, s);
164 pa_bytes_snprint(s, sizeof(s), i->scache_size);
165 printf(_("Sample cache size: %s\n"), s);
167 complete_action();
170 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
171 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
173 if (!i) {
174 pa_log(_("Failed to get server information: %s"), pa_strerror(pa_context_errno(c)));
175 quit(1);
176 return;
179 printf(_("Server String: %s\n"
180 "Library Protocol Version: %u\n"
181 "Server Protocol Version: %u\n"
182 "Is Local: %s\n"
183 "Client Index: %u\n"
184 "Tile Size: %zu\n"),
185 pa_context_get_server(c),
186 pa_context_get_protocol_version(c),
187 pa_context_get_server_protocol_version(c),
188 pa_yes_no(pa_context_is_local(c)),
189 pa_context_get_index(c),
190 pa_context_get_tile_size(c, NULL));
192 pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec);
193 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map);
195 printf(_("User Name: %s\n"
196 "Host Name: %s\n"
197 "Server Name: %s\n"
198 "Server Version: %s\n"
199 "Default Sample Specification: %s\n"
200 "Default Channel Map: %s\n"
201 "Default Sink: %s\n"
202 "Default Source: %s\n"
203 "Cookie: %04x:%04x\n"),
204 i->user_name,
205 i->host_name,
206 i->server_name,
207 i->server_version,
210 i->default_sink_name,
211 i->default_source_name,
212 i->cookie >> 16,
213 i->cookie & 0xFFFFU);
215 complete_action();
218 static const char* get_available_str_ynonly(int available)
220 switch (available) {
221 case PA_PORT_AVAILABLE_YES: return ", available";
222 case PA_PORT_AVAILABLE_NO: return ", not available";
224 return "";
227 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
229 static const char *state_table[] = {
230 [1+PA_SINK_INVALID_STATE] = "n/a",
231 [1+PA_SINK_RUNNING] = "RUNNING",
232 [1+PA_SINK_IDLE] = "IDLE",
233 [1+PA_SINK_SUSPENDED] = "SUSPENDED"
236 char
237 s[PA_SAMPLE_SPEC_SNPRINT_MAX],
238 cv[PA_CVOLUME_SNPRINT_MAX],
239 cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX],
240 v[PA_VOLUME_SNPRINT_MAX],
241 vdb[PA_SW_VOLUME_SNPRINT_DB_MAX],
242 cm[PA_CHANNEL_MAP_SNPRINT_MAX],
243 f[PA_FORMAT_INFO_SNPRINT_MAX];
244 char *pl;
246 if (is_last < 0) {
247 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
248 quit(1);
249 return;
252 if (is_last) {
253 complete_action();
254 return;
257 pa_assert(i);
259 if (nl && !short_list_format)
260 printf("\n");
261 nl = TRUE;
263 if (short_list_format) {
264 printf("%u\t%s\t%s\t%s\t%s\n",
265 i->index,
266 i->name,
267 pa_strnull(i->driver),
268 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
269 state_table[1+i->state]);
270 return;
273 printf(_("Sink #%u\n"
274 "\tState: %s\n"
275 "\tName: %s\n"
276 "\tDescription: %s\n"
277 "\tDriver: %s\n"
278 "\tSample Specification: %s\n"
279 "\tChannel Map: %s\n"
280 "\tOwner Module: %u\n"
281 "\tMute: %s\n"
282 "\tVolume: %s%s%s\n"
283 "\t balance %0.2f\n"
284 "\tBase Volume: %s%s%s\n"
285 "\tMonitor Source: %s\n"
286 "\tLatency: %0.0f usec, configured %0.0f usec\n"
287 "\tFlags: %s%s%s%s%s%s%s\n"
288 "\tProperties:\n\t\t%s\n"),
289 i->index,
290 state_table[1+i->state],
291 i->name,
292 pa_strnull(i->description),
293 pa_strnull(i->driver),
294 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
295 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
296 i->owner_module,
297 pa_yes_no(i->mute),
298 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
299 i->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t " : "",
300 i->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume) : "",
301 pa_cvolume_get_balance(&i->volume, &i->channel_map),
302 pa_volume_snprint(v, sizeof(v), i->base_volume),
303 i->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t " : "",
304 i->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), i->base_volume) : "",
305 pa_strnull(i->monitor_source_name),
306 (double) i->latency, (double) i->configured_latency,
307 i->flags & PA_SINK_HARDWARE ? "HARDWARE " : "",
308 i->flags & PA_SINK_NETWORK ? "NETWORK " : "",
309 i->flags & PA_SINK_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
310 i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
311 i->flags & PA_SINK_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
312 i->flags & PA_SINK_LATENCY ? "LATENCY " : "",
313 i->flags & PA_SINK_SET_FORMATS ? "SET_FORMATS " : "",
314 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
316 pa_xfree(pl);
318 if (i->ports) {
319 pa_sink_port_info **p;
321 printf(_("\tPorts:\n"));
322 for (p = i->ports; *p; p++)
323 printf("\t\t%s: %s (priority: %u%s)\n", (*p)->name, (*p)->description,
324 (*p)->priority, get_available_str_ynonly((*p)->available));
327 if (i->active_port)
328 printf(_("\tActive Port: %s\n"),
329 i->active_port->name);
331 if (i->formats) {
332 uint8_t j;
334 printf(_("\tFormats:\n"));
335 for (j = 0; j < i->n_formats; j++)
336 printf("\t\t%s\n", pa_format_info_snprint(f, sizeof(f), i->formats[j]));
340 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
342 static const char *state_table[] = {
343 [1+PA_SOURCE_INVALID_STATE] = "n/a",
344 [1+PA_SOURCE_RUNNING] = "RUNNING",
345 [1+PA_SOURCE_IDLE] = "IDLE",
346 [1+PA_SOURCE_SUSPENDED] = "SUSPENDED"
349 char
350 s[PA_SAMPLE_SPEC_SNPRINT_MAX],
351 cv[PA_CVOLUME_SNPRINT_MAX],
352 cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX],
353 v[PA_VOLUME_SNPRINT_MAX],
354 vdb[PA_SW_VOLUME_SNPRINT_DB_MAX],
355 cm[PA_CHANNEL_MAP_SNPRINT_MAX],
356 f[PA_FORMAT_INFO_SNPRINT_MAX];
357 char *pl;
359 if (is_last < 0) {
360 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
361 quit(1);
362 return;
365 if (is_last) {
366 complete_action();
367 return;
370 pa_assert(i);
372 if (nl && !short_list_format)
373 printf("\n");
374 nl = TRUE;
376 if (short_list_format) {
377 printf("%u\t%s\t%s\t%s\t%s\n",
378 i->index,
379 i->name,
380 pa_strnull(i->driver),
381 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
382 state_table[1+i->state]);
383 return;
386 printf(_("Source #%u\n"
387 "\tState: %s\n"
388 "\tName: %s\n"
389 "\tDescription: %s\n"
390 "\tDriver: %s\n"
391 "\tSample Specification: %s\n"
392 "\tChannel Map: %s\n"
393 "\tOwner Module: %u\n"
394 "\tMute: %s\n"
395 "\tVolume: %s%s%s\n"
396 "\t balance %0.2f\n"
397 "\tBase Volume: %s%s%s\n"
398 "\tMonitor of Sink: %s\n"
399 "\tLatency: %0.0f usec, configured %0.0f usec\n"
400 "\tFlags: %s%s%s%s%s%s\n"
401 "\tProperties:\n\t\t%s\n"),
402 i->index,
403 state_table[1+i->state],
404 i->name,
405 pa_strnull(i->description),
406 pa_strnull(i->driver),
407 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
408 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
409 i->owner_module,
410 pa_yes_no(i->mute),
411 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
412 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "",
413 i->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume) : "",
414 pa_cvolume_get_balance(&i->volume, &i->channel_map),
415 pa_volume_snprint(v, sizeof(v), i->base_volume),
416 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "",
417 i->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), i->base_volume) : "",
418 i->monitor_of_sink_name ? i->monitor_of_sink_name : _("n/a"),
419 (double) i->latency, (double) i->configured_latency,
420 i->flags & PA_SOURCE_HARDWARE ? "HARDWARE " : "",
421 i->flags & PA_SOURCE_NETWORK ? "NETWORK " : "",
422 i->flags & PA_SOURCE_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
423 i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
424 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
425 i->flags & PA_SOURCE_LATENCY ? "LATENCY " : "",
426 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
428 pa_xfree(pl);
430 if (i->ports) {
431 pa_source_port_info **p;
433 printf(_("\tPorts:\n"));
434 for (p = i->ports; *p; p++)
435 printf("\t\t%s: %s (priority: %u%s)\n", (*p)->name, (*p)->description,
436 (*p)->priority, get_available_str_ynonly((*p)->available));
439 if (i->active_port)
440 printf(_("\tActive Port: %s\n"),
441 i->active_port->name);
443 if (i->formats) {
444 uint8_t j;
446 printf(_("\tFormats:\n"));
447 for (j = 0; j < i->n_formats; j++)
448 printf("\t\t%s\n", pa_format_info_snprint(f, sizeof(f), i->formats[j]));
452 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
453 char t[32];
454 char *pl;
456 if (is_last < 0) {
457 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
458 quit(1);
459 return;
462 if (is_last) {
463 complete_action();
464 return;
467 pa_assert(i);
469 if (nl && !short_list_format)
470 printf("\n");
471 nl = TRUE;
473 pa_snprintf(t, sizeof(t), "%u", i->n_used);
475 if (short_list_format) {
476 printf("%u\t%s\t%s\t\n", i->index, i->name, i->argument ? i->argument : "");
477 return;
480 printf(_("Module #%u\n"
481 "\tName: %s\n"
482 "\tArgument: %s\n"
483 "\tUsage counter: %s\n"
484 "\tProperties:\n\t\t%s\n"),
485 i->index,
486 i->name,
487 i->argument ? i->argument : "",
488 i->n_used != PA_INVALID_INDEX ? t : _("n/a"),
489 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
491 pa_xfree(pl);
494 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
495 char t[32];
496 char *pl;
498 if (is_last < 0) {
499 pa_log(_("Failed to get client information: %s"), pa_strerror(pa_context_errno(c)));
500 quit(1);
501 return;
504 if (is_last) {
505 complete_action();
506 return;
509 pa_assert(i);
511 if (nl && !short_list_format)
512 printf("\n");
513 nl = TRUE;
515 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
517 if (short_list_format) {
518 printf("%u\t%s\t%s\n",
519 i->index,
520 pa_strnull(i->driver),
521 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)));
522 return;
525 printf(_("Client #%u\n"
526 "\tDriver: %s\n"
527 "\tOwner Module: %s\n"
528 "\tProperties:\n\t\t%s\n"),
529 i->index,
530 pa_strnull(i->driver),
531 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
532 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
534 pa_xfree(pl);
537 static void get_card_info_callback(pa_context *c, const pa_card_info *i, int is_last, void *userdata) {
538 char t[32];
539 char *pl;
541 if (is_last < 0) {
542 pa_log(_("Failed to get card information: %s"), pa_strerror(pa_context_errno(c)));
543 complete_action();
544 return;
547 if (is_last) {
548 complete_action();
549 return;
552 pa_assert(i);
554 if (nl && !short_list_format)
555 printf("\n");
556 nl = TRUE;
558 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
560 if (short_list_format) {
561 printf("%u\t%s\t%s\n", i->index, i->name, pa_strnull(i->driver));
562 return;
565 printf(_("Card #%u\n"
566 "\tName: %s\n"
567 "\tDriver: %s\n"
568 "\tOwner Module: %s\n"
569 "\tProperties:\n\t\t%s\n"),
570 i->index,
571 i->name,
572 pa_strnull(i->driver),
573 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
574 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
576 pa_xfree(pl);
578 if (i->profiles) {
579 pa_card_profile_info *p;
581 printf(_("\tProfiles:\n"));
582 for (p = i->profiles; p->name; p++)
583 printf("\t\t%s: %s (sinks: %u, sources: %u, priority. %u)\n", p->name, p->description, p->n_sinks, p->n_sources, p->priority);
586 if (i->active_profile)
587 printf(_("\tActive Profile: %s\n"),
588 i->active_profile->name);
590 if (i->ports) {
591 pa_card_port_info **p;
593 printf(_("\tPorts:\n"));
594 for (p = i->ports; *p; p++) {
595 pa_card_profile_info **pr = (*p)->profiles;
596 printf("\t\t%s: %s (priority: %u, latency offset: %" PRId64 " usec%s)\n", (*p)->name,
597 (*p)->description, (*p)->priority, (*p)->latency_offset,
598 get_available_str_ynonly((*p)->available));
600 if (!pa_proplist_isempty((*p)->proplist)) {
601 printf(_("\t\t\tProperties:\n\t\t\t\t%s\n"), pl = pa_proplist_to_string_sep((*p)->proplist, "\n\t\t\t\t"));
602 pa_xfree(pl);
605 if (pr) {
606 printf(_("\t\t\tPart of profile(s): %s"), pa_strnull((*pr)->name));
607 pr++;
608 while (*pr) {
609 printf(", %s", pa_strnull((*pr)->name));
610 pr++;
612 printf("\n");
618 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
619 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], f[PA_FORMAT_INFO_SNPRINT_MAX];
620 char *pl;
622 if (is_last < 0) {
623 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
624 quit(1);
625 return;
628 if (is_last) {
629 complete_action();
630 return;
633 pa_assert(i);
635 if (nl && !short_list_format)
636 printf("\n");
637 nl = TRUE;
639 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
640 pa_snprintf(k, sizeof(k), "%u", i->client);
642 if (short_list_format) {
643 printf("%u\t%u\t%s\t%s\t%s\n",
644 i->index,
645 i->sink,
646 i->client != PA_INVALID_INDEX ? k : "-",
647 pa_strnull(i->driver),
648 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec));
649 return;
652 printf(_("Sink Input #%u\n"
653 "\tDriver: %s\n"
654 "\tOwner Module: %s\n"
655 "\tClient: %s\n"
656 "\tSink: %u\n"
657 "\tSample Specification: %s\n"
658 "\tChannel Map: %s\n"
659 "\tFormat: %s\n"
660 "\tCorked: %s\n"
661 "\tMute: %s\n"
662 "\tVolume: %s\n"
663 "\t %s\n"
664 "\t balance %0.2f\n"
665 "\tBuffer Latency: %0.0f usec\n"
666 "\tSink Latency: %0.0f usec\n"
667 "\tResample method: %s\n"
668 "\tProperties:\n\t\t%s\n"),
669 i->index,
670 pa_strnull(i->driver),
671 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
672 i->client != PA_INVALID_INDEX ? k : _("n/a"),
673 i->sink,
674 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
675 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
676 pa_format_info_snprint(f, sizeof(f), i->format),
677 pa_yes_no(i->corked),
678 pa_yes_no(i->mute),
679 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
680 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
681 pa_cvolume_get_balance(&i->volume, &i->channel_map),
682 (double) i->buffer_usec,
683 (double) i->sink_usec,
684 i->resample_method ? i->resample_method : _("n/a"),
685 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
687 pa_xfree(pl);
690 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
691 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], f[PA_FORMAT_INFO_SNPRINT_MAX];
692 char *pl;
694 if (is_last < 0) {
695 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
696 quit(1);
697 return;
700 if (is_last) {
701 complete_action();
702 return;
705 pa_assert(i);
707 if (nl && !short_list_format)
708 printf("\n");
709 nl = TRUE;
712 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
713 pa_snprintf(k, sizeof(k), "%u", i->client);
715 if (short_list_format) {
716 printf("%u\t%u\t%s\t%s\t%s\n",
717 i->index,
718 i->source,
719 i->client != PA_INVALID_INDEX ? k : "-",
720 pa_strnull(i->driver),
721 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec));
722 return;
725 printf(_("Source Output #%u\n"
726 "\tDriver: %s\n"
727 "\tOwner Module: %s\n"
728 "\tClient: %s\n"
729 "\tSource: %u\n"
730 "\tSample Specification: %s\n"
731 "\tChannel Map: %s\n"
732 "\tFormat: %s\n"
733 "\tCorked: %s\n"
734 "\tMute: %s\n"
735 "\tVolume: %s\n"
736 "\t %s\n"
737 "\t balance %0.2f\n"
738 "\tBuffer Latency: %0.0f usec\n"
739 "\tSource Latency: %0.0f usec\n"
740 "\tResample method: %s\n"
741 "\tProperties:\n\t\t%s\n"),
742 i->index,
743 pa_strnull(i->driver),
744 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
745 i->client != PA_INVALID_INDEX ? k : _("n/a"),
746 i->source,
747 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
748 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
749 pa_format_info_snprint(f, sizeof(f), i->format),
750 pa_yes_no(i->corked),
751 pa_yes_no(i->mute),
752 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
753 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
754 pa_cvolume_get_balance(&i->volume, &i->channel_map),
755 (double) i->buffer_usec,
756 (double) i->source_usec,
757 i->resample_method ? i->resample_method : _("n/a"),
758 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
760 pa_xfree(pl);
763 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
764 char t[PA_BYTES_SNPRINT_MAX], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
765 char *pl;
767 if (is_last < 0) {
768 pa_log(_("Failed to get sample information: %s"), pa_strerror(pa_context_errno(c)));
769 quit(1);
770 return;
773 if (is_last) {
774 complete_action();
775 return;
778 pa_assert(i);
780 if (nl && !short_list_format)
781 printf("\n");
782 nl = TRUE;
784 pa_bytes_snprint(t, sizeof(t), i->bytes);
786 if (short_list_format) {
787 printf("%u\t%s\t%s\t%0.3f\n",
788 i->index,
789 i->name,
790 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : "-",
791 (double) i->duration/1000000.0);
792 return;
795 printf(_("Sample #%u\n"
796 "\tName: %s\n"
797 "\tSample Specification: %s\n"
798 "\tChannel Map: %s\n"
799 "\tVolume: %s\n"
800 "\t %s\n"
801 "\t balance %0.2f\n"
802 "\tDuration: %0.1fs\n"
803 "\tSize: %s\n"
804 "\tLazy: %s\n"
805 "\tFilename: %s\n"
806 "\tProperties:\n\t\t%s\n"),
807 i->index,
808 i->name,
809 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : _("n/a"),
810 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : _("n/a"),
811 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
812 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
813 pa_cvolume_get_balance(&i->volume, &i->channel_map),
814 (double) i->duration/1000000.0,
816 pa_yes_no(i->lazy),
817 i->filename ? i->filename : _("n/a"),
818 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
820 pa_xfree(pl);
823 static void simple_callback(pa_context *c, int success, void *userdata) {
824 if (!success) {
825 pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
826 quit(1);
827 return;
830 complete_action();
833 static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
834 if (idx == PA_INVALID_INDEX) {
835 pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
836 quit(1);
837 return;
840 printf("%u\n", idx);
842 complete_action();
845 static void volume_relative_adjust(pa_cvolume *cv) {
846 pa_assert((volume_flags & VOL_RELATIVE) == VOL_RELATIVE);
848 /* Relative volume change is additive in case of UINT or PERCENT
849 * and multiplicative for LINEAR or DECIBEL */
850 if ((volume_flags & 0x0F) == VOL_UINT || (volume_flags & 0x0F) == VOL_PERCENT) {
851 pa_volume_t v = pa_cvolume_avg(cv);
852 v = v + volume < PA_VOLUME_NORM ? PA_VOLUME_MUTED : v + volume - PA_VOLUME_NORM;
853 pa_cvolume_set(cv, 1, v);
855 if ((volume_flags & 0x0F) == VOL_LINEAR || (volume_flags & 0x0F) == VOL_DECIBEL) {
856 pa_sw_cvolume_multiply_scalar(cv, cv, volume);
860 static void unload_module_by_name_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
861 static pa_bool_t unloaded = FALSE;
863 if (is_last < 0) {
864 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
865 quit(1);
866 return;
869 if (is_last) {
870 if (unloaded == FALSE)
871 pa_log(_("Failed to unload module: Module %s not loaded"), module_name);
872 complete_action();
873 return;
876 pa_assert(i);
878 if (pa_streq(module_name, i->name)) {
879 unloaded = TRUE;
880 actions++;
881 pa_operation_unref(pa_context_unload_module(c, i->index, simple_callback, NULL));
885 static void get_sink_volume_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
886 pa_cvolume cv;
888 if (is_last < 0) {
889 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
890 quit(1);
891 return;
894 if (is_last)
895 return;
897 pa_assert(i);
899 cv = i->volume;
900 volume_relative_adjust(&cv);
901 pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &cv, simple_callback, NULL));
904 static void get_source_volume_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
905 pa_cvolume cv;
907 if (is_last < 0) {
908 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
909 quit(1);
910 return;
913 if (is_last)
914 return;
916 pa_assert(i);
918 cv = i->volume;
919 volume_relative_adjust(&cv);
920 pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &cv, simple_callback, NULL));
923 static void get_sink_input_volume_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
924 pa_cvolume cv;
926 if (is_last < 0) {
927 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
928 quit(1);
929 return;
932 if (is_last)
933 return;
935 pa_assert(i);
937 cv = i->volume;
938 volume_relative_adjust(&cv);
939 pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &cv, simple_callback, NULL));
942 static void get_source_output_volume_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
943 pa_cvolume cv;
945 if (is_last < 0) {
946 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
947 quit(1);
948 return;
951 if (is_last)
952 return;
954 pa_assert(o);
956 cv = o->volume;
957 volume_relative_adjust(&cv);
958 pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &cv, simple_callback, NULL));
961 /* PA_MAX_FORMATS is defined in internal.h so we just define a sane value here */
962 #define MAX_FORMATS 256
964 static void set_sink_formats(pa_context *c, uint32_t sink, const char *str) {
965 pa_format_info *f_arr[MAX_FORMATS];
966 char *format = NULL;
967 const char *state = NULL;
968 int i = 0;
970 while ((format = pa_split(str, ";", &state))) {
971 pa_format_info *f = pa_format_info_from_string(pa_strip(format));
973 if (!f) {
974 pa_log(_("Failed to set format: invalid format string %s"), format);
975 goto error;
978 f_arr[i++] = f;
979 pa_xfree(format);
982 pa_operation_unref(pa_ext_device_restore_save_formats(c, PA_DEVICE_TYPE_SINK, sink, i, f_arr, simple_callback, NULL));
984 done:
985 if (format)
986 pa_xfree(format);
987 while(i--)
988 pa_format_info_free(f_arr[i]);
990 return;
992 error:
993 while(i--)
994 pa_format_info_free(f_arr[i]);
995 quit(1);
996 goto done;
999 static void stream_state_callback(pa_stream *s, void *userdata) {
1000 pa_assert(s);
1002 switch (pa_stream_get_state(s)) {
1003 case PA_STREAM_CREATING:
1004 case PA_STREAM_READY:
1005 break;
1007 case PA_STREAM_TERMINATED:
1008 drain();
1009 break;
1011 case PA_STREAM_FAILED:
1012 default:
1013 pa_log(_("Failed to upload sample: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
1014 quit(1);
1018 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
1019 sf_count_t l;
1020 float *d;
1021 pa_assert(s && length && sndfile);
1023 d = pa_xmalloc(length);
1025 pa_assert(sample_length >= length);
1026 l = (sf_count_t) (length/pa_frame_size(&sample_spec));
1028 if ((sf_readf_float(sndfile, d, l)) != l) {
1029 pa_xfree(d);
1030 pa_log(_("Premature end of file"));
1031 quit(1);
1032 return;
1035 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
1037 sample_length -= length;
1039 if (sample_length <= 0) {
1040 pa_stream_set_write_callback(sample_stream, NULL, NULL);
1041 pa_stream_finish_upload(sample_stream);
1045 static const char *subscription_event_type_to_string(pa_subscription_event_type_t t) {
1047 switch (t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
1049 case PA_SUBSCRIPTION_EVENT_NEW:
1050 return _("new");
1052 case PA_SUBSCRIPTION_EVENT_CHANGE:
1053 return _("change");
1055 case PA_SUBSCRIPTION_EVENT_REMOVE:
1056 return _("remove");
1059 return _("unknown");
1062 static const char *subscription_event_facility_to_string(pa_subscription_event_type_t t) {
1064 switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
1066 case PA_SUBSCRIPTION_EVENT_SINK:
1067 return _("sink");
1069 case PA_SUBSCRIPTION_EVENT_SOURCE:
1070 return _("source");
1072 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
1073 return _("sink-input");
1075 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
1076 return _("source-output");
1078 case PA_SUBSCRIPTION_EVENT_MODULE:
1079 return _("module");
1081 case PA_SUBSCRIPTION_EVENT_CLIENT:
1082 return _("client");
1084 case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE:
1085 return _("sample-cache");
1087 case PA_SUBSCRIPTION_EVENT_SERVER:
1088 return _("server");
1090 case PA_SUBSCRIPTION_EVENT_CARD:
1091 return _("server");
1094 return _("unknown");
1097 static void context_subscribe_callback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1098 pa_assert(c);
1100 printf(_("Event '%s' on %s #%u\n"),
1101 subscription_event_type_to_string(t),
1102 subscription_event_facility_to_string(t),
1103 idx);
1106 static void context_state_callback(pa_context *c, void *userdata) {
1107 pa_assert(c);
1108 switch (pa_context_get_state(c)) {
1109 case PA_CONTEXT_CONNECTING:
1110 case PA_CONTEXT_AUTHORIZING:
1111 case PA_CONTEXT_SETTING_NAME:
1112 break;
1114 case PA_CONTEXT_READY:
1115 switch (action) {
1116 case STAT:
1117 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
1118 if (short_list_format)
1119 break;
1120 actions++;
1122 case INFO:
1123 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
1124 break;
1126 case PLAY_SAMPLE:
1127 pa_operation_unref(pa_context_play_sample(c, sample_name, sink_name, PA_VOLUME_NORM, simple_callback, NULL));
1128 break;
1130 case REMOVE_SAMPLE:
1131 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
1132 break;
1134 case UPLOAD_SAMPLE:
1135 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
1136 pa_assert(sample_stream);
1138 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
1139 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
1140 pa_stream_connect_upload(sample_stream, sample_length);
1141 break;
1143 case EXIT:
1144 pa_operation_unref(pa_context_exit_daemon(c, simple_callback, NULL));
1145 break;
1147 case LIST:
1148 if (list_type) {
1149 if (pa_streq(list_type, "modules"))
1150 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
1151 else if (pa_streq(list_type, "sinks"))
1152 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
1153 else if (pa_streq(list_type, "sources"))
1154 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
1155 else if (pa_streq(list_type, "sink-inputs"))
1156 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
1157 else if (pa_streq(list_type, "source-outputs"))
1158 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
1159 else if (pa_streq(list_type, "clients"))
1160 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
1161 else if (pa_streq(list_type, "samples"))
1162 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
1163 else if (pa_streq(list_type, "cards"))
1164 pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL));
1165 else
1166 pa_assert_not_reached();
1167 } else {
1168 actions = 8;
1169 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
1170 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
1171 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
1172 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
1173 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
1174 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
1175 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
1176 pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL));
1178 break;
1180 case MOVE_SINK_INPUT:
1181 pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL));
1182 break;
1184 case MOVE_SOURCE_OUTPUT:
1185 pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL));
1186 break;
1188 case LOAD_MODULE:
1189 pa_operation_unref(pa_context_load_module(c, module_name, module_args, index_callback, NULL));
1190 break;
1192 case UNLOAD_MODULE:
1193 if (module_name)
1194 pa_operation_unref(pa_context_get_module_info_list(c, unload_module_by_name_callback, NULL));
1195 else
1196 pa_operation_unref(pa_context_unload_module(c, module_index, simple_callback, NULL));
1197 break;
1199 case SUSPEND_SINK:
1200 if (sink_name)
1201 pa_operation_unref(pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL));
1202 else
1203 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
1204 break;
1206 case SUSPEND_SOURCE:
1207 if (source_name)
1208 pa_operation_unref(pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL));
1209 else
1210 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
1211 break;
1213 case SET_CARD_PROFILE:
1214 pa_operation_unref(pa_context_set_card_profile_by_name(c, card_name, profile_name, simple_callback, NULL));
1215 break;
1217 case SET_SINK_PORT:
1218 pa_operation_unref(pa_context_set_sink_port_by_name(c, sink_name, port_name, simple_callback, NULL));
1219 break;
1221 case SET_SOURCE_PORT:
1222 pa_operation_unref(pa_context_set_source_port_by_name(c, source_name, port_name, simple_callback, NULL));
1223 break;
1225 case SET_SINK_MUTE:
1226 pa_operation_unref(pa_context_set_sink_mute_by_name(c, sink_name, mute, simple_callback, NULL));
1227 break;
1229 case SET_SOURCE_MUTE:
1230 pa_operation_unref(pa_context_set_source_mute_by_name(c, source_name, mute, simple_callback, NULL));
1231 break;
1233 case SET_SINK_INPUT_MUTE:
1234 pa_operation_unref(pa_context_set_sink_input_mute(c, sink_input_idx, mute, simple_callback, NULL));
1235 break;
1237 case SET_SOURCE_OUTPUT_MUTE:
1238 pa_operation_unref(pa_context_set_source_output_mute(c, source_output_idx, mute, simple_callback, NULL));
1239 break;
1241 case SET_SINK_VOLUME:
1242 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1243 pa_operation_unref(pa_context_get_sink_info_by_name(c, sink_name, get_sink_volume_callback, NULL));
1244 } else {
1245 pa_cvolume v;
1246 pa_cvolume_set(&v, 1, volume);
1247 pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &v, simple_callback, NULL));
1249 break;
1251 case SET_SOURCE_VOLUME:
1252 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1253 pa_operation_unref(pa_context_get_source_info_by_name(c, source_name, get_source_volume_callback, NULL));
1254 } else {
1255 pa_cvolume v;
1256 pa_cvolume_set(&v, 1, volume);
1257 pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &v, simple_callback, NULL));
1259 break;
1261 case SET_SINK_INPUT_VOLUME:
1262 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1263 pa_operation_unref(pa_context_get_sink_input_info(c, sink_input_idx, get_sink_input_volume_callback, NULL));
1264 } else {
1265 pa_cvolume v;
1266 pa_cvolume_set(&v, 1, volume);
1267 pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &v, simple_callback, NULL));
1269 break;
1271 case SET_SOURCE_OUTPUT_VOLUME:
1272 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1273 pa_operation_unref(pa_context_get_source_output_info(c, source_output_idx, get_source_output_volume_callback, NULL));
1274 } else {
1275 pa_cvolume v;
1276 pa_cvolume_set(&v, 1, volume);
1277 pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &v, simple_callback, NULL));
1279 break;
1281 case SET_SINK_FORMATS:
1282 set_sink_formats(c, sink_idx, formats);
1283 break;
1285 case SET_PORT_LATENCY_OFFSET:
1286 pa_operation_unref(pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL));
1287 break;
1289 case SUBSCRIBE:
1290 pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL);
1292 pa_operation_unref(pa_context_subscribe(
1294 PA_SUBSCRIPTION_MASK_SINK|
1295 PA_SUBSCRIPTION_MASK_SOURCE|
1296 PA_SUBSCRIPTION_MASK_SINK_INPUT|
1297 PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT|
1298 PA_SUBSCRIPTION_MASK_MODULE|
1299 PA_SUBSCRIPTION_MASK_CLIENT|
1300 PA_SUBSCRIPTION_MASK_SAMPLE_CACHE|
1301 PA_SUBSCRIPTION_MASK_SERVER|
1302 PA_SUBSCRIPTION_MASK_CARD,
1303 NULL,
1304 NULL));
1305 break;
1307 default:
1308 pa_assert_not_reached();
1310 break;
1312 case PA_CONTEXT_TERMINATED:
1313 quit(0);
1314 break;
1316 case PA_CONTEXT_FAILED:
1317 default:
1318 pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c)));
1319 quit(1);
1323 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
1324 pa_log(_("Got SIGINT, exiting."));
1325 quit(0);
1328 static int parse_volume(const char *vol_spec, pa_volume_t *vol, enum volume_flags *vol_flags) {
1329 double v;
1330 char *vs;
1332 pa_assert(vol_spec);
1333 pa_assert(vol);
1334 pa_assert(vol_flags);
1336 vs = pa_xstrdup(vol_spec);
1338 *vol_flags = (pa_startswith(vs, "+") || pa_startswith(vs, "-")) ? VOL_RELATIVE : VOL_ABSOLUTE;
1339 if (strchr(vs, '.'))
1340 *vol_flags |= VOL_LINEAR;
1341 if (pa_endswith(vs, "%")) {
1342 *vol_flags |= VOL_PERCENT;
1343 vs[strlen(vs)-1] = 0;
1345 if (pa_endswith(vs, "db") || pa_endswith(vs, "dB")) {
1346 *vol_flags |= VOL_DECIBEL;
1347 vs[strlen(vs)-2] = 0;
1350 if (pa_atod(vs, &v) < 0) {
1351 pa_log(_("Invalid volume specification"));
1352 pa_xfree(vs);
1353 return -1;
1356 pa_xfree(vs);
1358 if ((*vol_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1359 if ((*vol_flags & 0x0F) == VOL_UINT)
1360 v += (double) PA_VOLUME_NORM;
1361 if ((*vol_flags & 0x0F) == VOL_PERCENT)
1362 v += 100.0;
1363 if ((*vol_flags & 0x0F) == VOL_LINEAR)
1364 v += 1.0;
1366 if ((*vol_flags & 0x0F) == VOL_PERCENT)
1367 v = v * (double) PA_VOLUME_NORM / 100;
1368 if ((*vol_flags & 0x0F) == VOL_LINEAR)
1369 v = pa_sw_volume_from_linear(v);
1370 if ((*vol_flags & 0x0F) == VOL_DECIBEL)
1371 v = pa_sw_volume_from_dB(v);
1373 if (!PA_VOLUME_IS_VALID((pa_volume_t) v)) {
1374 pa_log(_("Volume outside permissible range.\n"));
1375 return -1;
1378 *vol = (pa_volume_t) v;
1380 return 0;
1383 static void help(const char *argv0) {
1385 printf("%s %s %s\n", argv0, _("[options]"), "stat [short]");
1386 printf("%s %s %s\n", argv0, _("[options]"), "info");
1387 printf("%s %s %s %s\n", argv0, _("[options]"), "list [short]", _("[TYPE]"));
1388 printf("%s %s %s\n", argv0, _("[options]"), "exit");
1389 printf("%s %s %s %s\n", argv0, _("[options]"), "upload-sample", _("FILENAME [NAME]"));
1390 printf("%s %s %s %s\n", argv0, _("[options]"), "play-sample ", _("NAME [SINK]"));
1391 printf("%s %s %s %s\n", argv0, _("[options]"), "remove-sample ", _("NAME"));
1392 printf("%s %s %s %s\n", argv0, _("[options]"), "load-module ", _("NAME [ARGS ...]"));
1393 printf("%s %s %s %s\n", argv0, _("[options]"), "unload-module ", _("NAME|#N"));
1394 printf("%s %s %s %s\n", argv0, _("[options]"), "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
1395 printf("%s %s %s %s\n", argv0, _("[options]"), "suspend-(sink|source)", _("NAME|#N 1|0"));
1396 printf("%s %s %s %s\n", argv0, _("[options]"), "set-card-profile ", _("CARD PROFILE"));
1397 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-port", _("NAME|#N PORT"));
1398 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-volume", _("NAME|#N VOLUME"));
1399 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-volume", _("#N VOLUME"));
1400 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-mute", _("NAME|#N 1|0"));
1401 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0"));
1402 printf("%s %s %s %s\n", argv0, _("[options]"), "set-sink-formats", _("#N FORMATS"));
1403 printf("%s %s %s %s\n", argv0, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
1404 printf("%s %s %s\n", argv0, _("[options]"), "subscribe");
1406 printf(_("\n"
1407 " -h, --help Show this help\n"
1408 " --version Show version\n\n"
1409 " -s, --server=SERVER The name of the server to connect to\n"
1410 " -n, --client-name=NAME How to call this client on the server\n"));
1413 enum {
1414 ARG_VERSION = 256
1417 int main(int argc, char *argv[]) {
1418 pa_mainloop *m = NULL;
1419 int ret = 1, c;
1420 char *server = NULL, *bn;
1422 static const struct option long_options[] = {
1423 {"server", 1, NULL, 's'},
1424 {"client-name", 1, NULL, 'n'},
1425 {"version", 0, NULL, ARG_VERSION},
1426 {"help", 0, NULL, 'h'},
1427 {NULL, 0, NULL, 0}
1430 setlocale(LC_ALL, "");
1431 #ifdef ENABLE_NLS
1432 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
1433 #endif
1435 bn = pa_path_get_filename(argv[0]);
1437 proplist = pa_proplist_new();
1439 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
1440 switch (c) {
1441 case 'h' :
1442 help(bn);
1443 ret = 0;
1444 goto quit;
1446 case ARG_VERSION:
1447 printf(_("pactl %s\n"
1448 "Compiled with libpulse %s\n"
1449 "Linked with libpulse %s\n"),
1450 PACKAGE_VERSION,
1451 pa_get_headers_version(),
1452 pa_get_library_version());
1453 ret = 0;
1454 goto quit;
1456 case 's':
1457 pa_xfree(server);
1458 server = pa_xstrdup(optarg);
1459 break;
1461 case 'n': {
1462 char *t;
1464 if (!(t = pa_locale_to_utf8(optarg)) ||
1465 pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t) < 0) {
1467 pa_log(_("Invalid client name '%s'"), t ? t : optarg);
1468 pa_xfree(t);
1469 goto quit;
1472 pa_xfree(t);
1473 break;
1476 default:
1477 goto quit;
1481 if (optind < argc) {
1482 if (pa_streq(argv[optind], "stat")) {
1483 action = STAT;
1484 short_list_format = FALSE;
1485 if (optind+1 < argc && pa_streq(argv[optind+1], "short"))
1486 short_list_format = TRUE;
1488 } else if (pa_streq(argv[optind], "info"))
1489 action = INFO;
1491 else if (pa_streq(argv[optind], "exit"))
1492 action = EXIT;
1494 else if (pa_streq(argv[optind], "list")) {
1495 action = LIST;
1497 for (int i = optind+1; i < argc; i++){
1498 if (pa_streq(argv[i], "modules") || pa_streq(argv[i], "clients") ||
1499 pa_streq(argv[i], "sinks") || pa_streq(argv[i], "sink-inputs") ||
1500 pa_streq(argv[i], "sources") || pa_streq(argv[i], "source-outputs") ||
1501 pa_streq(argv[i], "samples") || pa_streq(argv[i], "cards")) {
1502 list_type = pa_xstrdup(argv[i]);
1503 } else if (pa_streq(argv[i], "short")) {
1504 short_list_format = TRUE;
1505 } else {
1506 pa_log(_("Specify nothing, or one of: %s"), "modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards");
1507 goto quit;
1511 } else if (pa_streq(argv[optind], "upload-sample")) {
1512 struct SF_INFO sfi;
1513 action = UPLOAD_SAMPLE;
1515 if (optind+1 >= argc) {
1516 pa_log(_("Please specify a sample file to load"));
1517 goto quit;
1520 if (optind+2 < argc)
1521 sample_name = pa_xstrdup(argv[optind+2]);
1522 else {
1523 char *f = pa_path_get_filename(argv[optind+1]);
1524 sample_name = pa_xstrndup(f, strcspn(f, "."));
1527 pa_zero(sfi);
1528 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfi))) {
1529 pa_log(_("Failed to open sound file."));
1530 goto quit;
1533 if (pa_sndfile_read_sample_spec(sndfile, &sample_spec) < 0) {
1534 pa_log(_("Failed to determine sample specification from file."));
1535 goto quit;
1537 sample_spec.format = PA_SAMPLE_FLOAT32;
1539 if (pa_sndfile_read_channel_map(sndfile, &channel_map) < 0) {
1540 if (sample_spec.channels > 2)
1541 pa_log(_("Warning: Failed to determine sample specification from file."));
1542 pa_channel_map_init_extend(&channel_map, sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
1545 pa_assert(pa_channel_map_compatible(&channel_map, &sample_spec));
1546 sample_length = (size_t) sfi.frames*pa_frame_size(&sample_spec);
1548 } else if (pa_streq(argv[optind], "play-sample")) {
1549 action = PLAY_SAMPLE;
1550 if (argc != optind+2 && argc != optind+3) {
1551 pa_log(_("You have to specify a sample name to play"));
1552 goto quit;
1555 sample_name = pa_xstrdup(argv[optind+1]);
1557 if (optind+2 < argc)
1558 sink_name = pa_xstrdup(argv[optind+2]);
1560 } else if (pa_streq(argv[optind], "remove-sample")) {
1561 action = REMOVE_SAMPLE;
1562 if (argc != optind+2) {
1563 pa_log(_("You have to specify a sample name to remove"));
1564 goto quit;
1567 sample_name = pa_xstrdup(argv[optind+1]);
1569 } else if (pa_streq(argv[optind], "move-sink-input")) {
1570 action = MOVE_SINK_INPUT;
1571 if (argc != optind+3) {
1572 pa_log(_("You have to specify a sink input index and a sink"));
1573 goto quit;
1576 sink_input_idx = (uint32_t) atoi(argv[optind+1]);
1577 sink_name = pa_xstrdup(argv[optind+2]);
1579 } else if (pa_streq(argv[optind], "move-source-output")) {
1580 action = MOVE_SOURCE_OUTPUT;
1581 if (argc != optind+3) {
1582 pa_log(_("You have to specify a source output index and a source"));
1583 goto quit;
1586 source_output_idx = (uint32_t) atoi(argv[optind+1]);
1587 source_name = pa_xstrdup(argv[optind+2]);
1589 } else if (pa_streq(argv[optind], "load-module")) {
1590 int i;
1591 size_t n = 0;
1592 char *p;
1594 action = LOAD_MODULE;
1596 if (argc <= optind+1) {
1597 pa_log(_("You have to specify a module name and arguments."));
1598 goto quit;
1601 module_name = argv[optind+1];
1603 for (i = optind+2; i < argc; i++)
1604 n += strlen(argv[i])+1;
1606 if (n > 0) {
1607 p = module_args = pa_xmalloc(n);
1609 for (i = optind+2; i < argc; i++)
1610 p += sprintf(p, "%s%s", p == module_args ? "" : " ", argv[i]);
1613 } else if (pa_streq(argv[optind], "unload-module")) {
1614 action = UNLOAD_MODULE;
1616 if (argc != optind+2) {
1617 pa_log(_("You have to specify a module index or name"));
1618 goto quit;
1621 if (pa_atou(argv[optind + 1], &module_index) < 0)
1622 module_name = argv[optind + 1];
1624 } else if (pa_streq(argv[optind], "suspend-sink")) {
1625 action = SUSPEND_SINK;
1627 if (argc > optind+3 || optind+1 >= argc) {
1628 pa_log(_("You may not specify more than one sink. You have to specify a boolean value."));
1629 goto quit;
1632 suspend = pa_parse_boolean(argv[argc-1]);
1634 if (argc > optind+2)
1635 sink_name = pa_xstrdup(argv[optind+1]);
1637 } else if (pa_streq(argv[optind], "suspend-source")) {
1638 action = SUSPEND_SOURCE;
1640 if (argc > optind+3 || optind+1 >= argc) {
1641 pa_log(_("You may not specify more than one source. You have to specify a boolean value."));
1642 goto quit;
1645 suspend = pa_parse_boolean(argv[argc-1]);
1647 if (argc > optind+2)
1648 source_name = pa_xstrdup(argv[optind+1]);
1649 } else if (pa_streq(argv[optind], "set-card-profile")) {
1650 action = SET_CARD_PROFILE;
1652 if (argc != optind+3) {
1653 pa_log(_("You have to specify a card name/index and a profile name"));
1654 goto quit;
1657 card_name = pa_xstrdup(argv[optind+1]);
1658 profile_name = pa_xstrdup(argv[optind+2]);
1660 } else if (pa_streq(argv[optind], "set-sink-port")) {
1661 action = SET_SINK_PORT;
1663 if (argc != optind+3) {
1664 pa_log(_("You have to specify a sink name/index and a port name"));
1665 goto quit;
1668 sink_name = pa_xstrdup(argv[optind+1]);
1669 port_name = pa_xstrdup(argv[optind+2]);
1671 } else if (pa_streq(argv[optind], "set-source-port")) {
1672 action = SET_SOURCE_PORT;
1674 if (argc != optind+3) {
1675 pa_log(_("You have to specify a source name/index and a port name"));
1676 goto quit;
1679 source_name = pa_xstrdup(argv[optind+1]);
1680 port_name = pa_xstrdup(argv[optind+2]);
1682 } else if (pa_streq(argv[optind], "set-sink-volume")) {
1683 action = SET_SINK_VOLUME;
1685 if (argc != optind+3) {
1686 pa_log(_("You have to specify a sink name/index and a volume"));
1687 goto quit;
1690 sink_name = pa_xstrdup(argv[optind+1]);
1692 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1693 goto quit;
1695 } else if (pa_streq(argv[optind], "set-source-volume")) {
1696 action = SET_SOURCE_VOLUME;
1698 if (argc != optind+3) {
1699 pa_log(_("You have to specify a source name/index and a volume"));
1700 goto quit;
1703 source_name = pa_xstrdup(argv[optind+1]);
1705 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1706 goto quit;
1708 } else if (pa_streq(argv[optind], "set-sink-input-volume")) {
1709 action = SET_SINK_INPUT_VOLUME;
1711 if (argc != optind+3) {
1712 pa_log(_("You have to specify a sink input index and a volume"));
1713 goto quit;
1716 if (pa_atou(argv[optind+1], &sink_input_idx) < 0) {
1717 pa_log(_("Invalid sink input index"));
1718 goto quit;
1721 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1722 goto quit;
1724 } else if (pa_streq(argv[optind], "set-source-output-volume")) {
1725 action = SET_SOURCE_OUTPUT_VOLUME;
1727 if (argc != optind+3) {
1728 pa_log(_("You have to specify a source output index and a volume"));
1729 goto quit;
1732 if (pa_atou(argv[optind+1], &source_output_idx) < 0) {
1733 pa_log(_("Invalid source output index"));
1734 goto quit;
1737 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1738 goto quit;
1740 } else if (pa_streq(argv[optind], "set-sink-mute")) {
1741 int b;
1742 action = SET_SINK_MUTE;
1744 if (argc != optind+3) {
1745 pa_log(_("You have to specify a sink name/index and a mute boolean"));
1746 goto quit;
1749 if ((b = pa_parse_boolean(argv[optind+2])) < 0) {
1750 pa_log(_("Invalid mute specification"));
1751 goto quit;
1754 sink_name = pa_xstrdup(argv[optind+1]);
1755 mute = b;
1757 } else if (pa_streq(argv[optind], "set-source-mute")) {
1758 int b;
1759 action = SET_SOURCE_MUTE;
1761 if (argc != optind+3) {
1762 pa_log(_("You have to specify a source name/index and a mute boolean"));
1763 goto quit;
1766 if ((b = pa_parse_boolean(argv[optind+2])) < 0) {
1767 pa_log(_("Invalid mute specification"));
1768 goto quit;
1771 source_name = pa_xstrdup(argv[optind+1]);
1772 mute = b;
1774 } else if (pa_streq(argv[optind], "set-sink-input-mute")) {
1775 int b;
1776 action = SET_SINK_INPUT_MUTE;
1778 if (argc != optind+3) {
1779 pa_log(_("You have to specify a sink input index and a mute boolean"));
1780 goto quit;
1783 if (pa_atou(argv[optind+1], &sink_input_idx) < 0) {
1784 pa_log(_("Invalid sink input index specification"));
1785 goto quit;
1788 if ((b = pa_parse_boolean(argv[optind+2])) < 0) {
1789 pa_log(_("Invalid mute specification"));
1790 goto quit;
1793 mute = b;
1795 } else if (pa_streq(argv[optind], "set-source-output-mute")) {
1796 int b;
1797 action = SET_SOURCE_OUTPUT_MUTE;
1799 if (argc != optind+3) {
1800 pa_log(_("You have to specify a source output index and a mute boolean"));
1801 goto quit;
1804 if (pa_atou(argv[optind+1], &source_output_idx) < 0) {
1805 pa_log(_("Invalid source output index specification"));
1806 goto quit;
1809 if ((b = pa_parse_boolean(argv[optind+2])) < 0) {
1810 pa_log(_("Invalid mute specification"));
1811 goto quit;
1814 mute = b;
1816 } else if (pa_streq(argv[optind], "subscribe"))
1818 action = SUBSCRIBE;
1820 else if (pa_streq(argv[optind], "set-sink-formats")) {
1821 int32_t tmp;
1823 if (argc != optind+3 || pa_atoi(argv[optind+1], &tmp) < 0) {
1824 pa_log(_("You have to specify a sink index and a semicolon-separated list of supported formats"));
1825 goto quit;
1828 sink_idx = tmp;
1829 action = SET_SINK_FORMATS;
1830 formats = pa_xstrdup(argv[optind+2]);
1832 } else if (pa_streq(argv[optind], "set-port-latency-offset")) {
1833 action = SET_PORT_LATENCY_OFFSET;
1835 if (argc != optind+4) {
1836 pa_log(_("You have to specify a card name/index, a port name and a latency offset"));
1837 goto quit;
1840 card_name = pa_xstrdup(argv[optind+1]);
1841 port_name = pa_xstrdup(argv[optind+2]);
1842 if (pa_atoi(argv[optind + 3], &latency_offset) < 0) {
1843 pa_log(_("Could not parse latency offset"));
1844 goto quit;
1847 } else if (pa_streq(argv[optind], "help")) {
1848 help(bn);
1849 ret = 0;
1850 goto quit;
1854 if (action == NONE) {
1855 pa_log(_("No valid command specified."));
1856 goto quit;
1859 if (!(m = pa_mainloop_new())) {
1860 pa_log(_("pa_mainloop_new() failed."));
1861 goto quit;
1864 mainloop_api = pa_mainloop_get_api(m);
1866 pa_assert_se(pa_signal_init(mainloop_api) == 0);
1867 pa_signal_new(SIGINT, exit_signal_callback, NULL);
1868 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
1869 pa_disable_sigpipe();
1871 if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) {
1872 pa_log(_("pa_context_new() failed."));
1873 goto quit;
1876 pa_context_set_state_callback(context, context_state_callback, NULL);
1877 if (pa_context_connect(context, server, 0, NULL) < 0) {
1878 pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
1879 goto quit;
1882 if (pa_mainloop_run(m, &ret) < 0) {
1883 pa_log(_("pa_mainloop_run() failed."));
1884 goto quit;
1887 quit:
1888 if (sample_stream)
1889 pa_stream_unref(sample_stream);
1891 if (context)
1892 pa_context_unref(context);
1894 if (m) {
1895 pa_signal_done();
1896 pa_mainloop_free(m);
1899 pa_xfree(server);
1900 pa_xfree(list_type);
1901 pa_xfree(sample_name);
1902 pa_xfree(sink_name);
1903 pa_xfree(source_name);
1904 pa_xfree(module_args);
1905 pa_xfree(card_name);
1906 pa_xfree(profile_name);
1907 pa_xfree(port_name);
1908 pa_xfree(formats);
1910 if (sndfile)
1911 sf_close(sndfile);
1913 if (proplist)
1914 pa_proplist_free(proplist);
1916 return ret;