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
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
;
64 sink_input_idx
= PA_INVALID_INDEX
,
65 source_output_idx
= PA_INVALID_INDEX
,
66 sink_idx
= PA_INVALID_INDEX
;
68 static bool short_list_format
= false;
69 static uint32_t module_index
;
70 static int32_t latency_offset
;
72 static pa_volume_t volume
;
73 static enum volume_flags
{
78 VOL_ABSOLUTE
= 0 << 4,
79 VOL_RELATIVE
= 1 << 4,
82 static enum mute_flags
{
87 } mute
= INVALID_MUTE
;
89 static pa_proplist
*proplist
= NULL
;
91 static SNDFILE
*sndfile
= NULL
;
92 static pa_stream
*sample_stream
= NULL
;
93 static pa_sample_spec sample_spec
;
94 static pa_channel_map channel_map
;
95 static size_t sample_length
= 0;
97 /* This variable tracks the number of ongoing asynchronous operations. When a
98 * new operation begins, this is incremented simply with actions++, and when
99 * an operation finishes, this is decremented with the complete_action()
100 * function, which shuts down the program if actions reaches zero. */
101 static int actions
= 0;
103 static bool nl
= false;
127 SET_SINK_INPUT_VOLUME
,
128 SET_SOURCE_OUTPUT_VOLUME
,
132 SET_SOURCE_OUTPUT_MUTE
,
134 SET_PORT_LATENCY_OFFSET
,
138 static void quit(int ret
) {
139 pa_assert(mainloop_api
);
140 mainloop_api
->quit(mainloop_api
, ret
);
143 static void context_drain_complete(pa_context
*c
, void *userdata
) {
144 pa_context_disconnect(c
);
147 static void drain(void) {
150 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
151 pa_context_disconnect(context
);
153 pa_operation_unref(o
);
156 static void complete_action(void) {
157 pa_assert(actions
> 0);
163 static void stat_callback(pa_context
*c
, const pa_stat_info
*i
, void *userdata
) {
164 char s
[PA_BYTES_SNPRINT_MAX
];
166 pa_log(_("Failed to get statistics: %s"), pa_strerror(pa_context_errno(c
)));
171 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_total_size
);
172 printf(_("Currently in use: %u blocks containing %s bytes total.\n"), i
->memblock_total
, s
);
174 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_allocated_size
);
175 printf(_("Allocated during whole lifetime: %u blocks containing %s bytes total.\n"), i
->memblock_allocated
, s
);
177 pa_bytes_snprint(s
, sizeof(s
), i
->scache_size
);
178 printf(_("Sample cache size: %s\n"), s
);
183 static void get_server_info_callback(pa_context
*c
, const pa_server_info
*i
, void *useerdata
) {
184 char ss
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
187 pa_log(_("Failed to get server information: %s"), pa_strerror(pa_context_errno(c
)));
192 printf(_("Server String: %s\n"
193 "Library Protocol Version: %u\n"
194 "Server Protocol Version: %u\n"
198 pa_context_get_server(c
),
199 pa_context_get_protocol_version(c
),
200 pa_context_get_server_protocol_version(c
),
201 pa_yes_no(pa_context_is_local(c
)),
202 pa_context_get_index(c
),
203 pa_context_get_tile_size(c
, NULL
));
205 pa_sample_spec_snprint(ss
, sizeof(ss
), &i
->sample_spec
);
206 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
);
208 printf(_("User Name: %s\n"
211 "Server Version: %s\n"
212 "Default Sample Specification: %s\n"
213 "Default Channel Map: %s\n"
215 "Default Source: %s\n"
216 "Cookie: %04x:%04x\n"),
223 i
->default_sink_name
,
224 i
->default_source_name
,
226 i
->cookie
& 0xFFFFU
);
231 static const char* get_available_str_ynonly(int available
) {
233 case PA_PORT_AVAILABLE_YES
: return ", available";
234 case PA_PORT_AVAILABLE_NO
: return ", not available";
239 static void get_sink_info_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
241 static const char *state_table
[] = {
242 [1+PA_SINK_INVALID_STATE
] = "n/a",
243 [1+PA_SINK_RUNNING
] = "RUNNING",
244 [1+PA_SINK_IDLE
] = "IDLE",
245 [1+PA_SINK_SUSPENDED
] = "SUSPENDED"
249 s
[PA_SAMPLE_SPEC_SNPRINT_MAX
],
250 cv
[PA_CVOLUME_SNPRINT_VERBOSE_MAX
],
251 v
[PA_VOLUME_SNPRINT_VERBOSE_MAX
],
252 cm
[PA_CHANNEL_MAP_SNPRINT_MAX
],
253 f
[PA_FORMAT_INFO_SNPRINT_MAX
];
257 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c
)));
269 if (nl
&& !short_list_format
)
273 if (short_list_format
) {
274 printf("%u\t%s\t%s\t%s\t%s\n",
277 pa_strnull(i
->driver
),
278 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
279 state_table
[1+i
->state
]);
283 printf(_("Sink #%u\n"
286 "\tDescription: %s\n"
288 "\tSample Specification: %s\n"
289 "\tChannel Map: %s\n"
290 "\tOwner Module: %u\n"
294 "\tBase Volume: %s\n"
295 "\tMonitor Source: %s\n"
296 "\tLatency: %0.0f usec, configured %0.0f usec\n"
297 "\tFlags: %s%s%s%s%s%s%s\n"
298 "\tProperties:\n\t\t%s\n"),
300 state_table
[1+i
->state
],
302 pa_strnull(i
->description
),
303 pa_strnull(i
->driver
),
304 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
305 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
308 pa_cvolume_snprint_verbose(cv
, sizeof(cv
), &i
->volume
, &i
->channel_map
, i
->flags
& PA_SINK_DECIBEL_VOLUME
),
309 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
310 pa_volume_snprint_verbose(v
, sizeof(v
), i
->base_volume
, i
->flags
& PA_SINK_DECIBEL_VOLUME
),
311 pa_strnull(i
->monitor_source_name
),
312 (double) i
->latency
, (double) i
->configured_latency
,
313 i
->flags
& PA_SINK_HARDWARE
? "HARDWARE " : "",
314 i
->flags
& PA_SINK_NETWORK
? "NETWORK " : "",
315 i
->flags
& PA_SINK_HW_MUTE_CTRL
? "HW_MUTE_CTRL " : "",
316 i
->flags
& PA_SINK_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
317 i
->flags
& PA_SINK_DECIBEL_VOLUME
? "DECIBEL_VOLUME " : "",
318 i
->flags
& PA_SINK_LATENCY
? "LATENCY " : "",
319 i
->flags
& PA_SINK_SET_FORMATS
? "SET_FORMATS " : "",
320 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
325 pa_sink_port_info
**p
;
327 printf(_("\tPorts:\n"));
328 for (p
= i
->ports
; *p
; p
++)
329 printf("\t\t%s: %s (priority: %u%s)\n", (*p
)->name
, (*p
)->description
,
330 (*p
)->priority
, get_available_str_ynonly((*p
)->available
));
334 printf(_("\tActive Port: %s\n"),
335 i
->active_port
->name
);
340 printf(_("\tFormats:\n"));
341 for (j
= 0; j
< i
->n_formats
; j
++)
342 printf("\t\t%s\n", pa_format_info_snprint(f
, sizeof(f
), i
->formats
[j
]));
346 static void get_source_info_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
348 static const char *state_table
[] = {
349 [1+PA_SOURCE_INVALID_STATE
] = "n/a",
350 [1+PA_SOURCE_RUNNING
] = "RUNNING",
351 [1+PA_SOURCE_IDLE
] = "IDLE",
352 [1+PA_SOURCE_SUSPENDED
] = "SUSPENDED"
356 s
[PA_SAMPLE_SPEC_SNPRINT_MAX
],
357 cv
[PA_CVOLUME_SNPRINT_VERBOSE_MAX
],
358 v
[PA_VOLUME_SNPRINT_VERBOSE_MAX
],
359 cm
[PA_CHANNEL_MAP_SNPRINT_MAX
],
360 f
[PA_FORMAT_INFO_SNPRINT_MAX
];
364 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c
)));
376 if (nl
&& !short_list_format
)
380 if (short_list_format
) {
381 printf("%u\t%s\t%s\t%s\t%s\n",
384 pa_strnull(i
->driver
),
385 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
386 state_table
[1+i
->state
]);
390 printf(_("Source #%u\n"
393 "\tDescription: %s\n"
395 "\tSample Specification: %s\n"
396 "\tChannel Map: %s\n"
397 "\tOwner Module: %u\n"
401 "\tBase Volume: %s\n"
402 "\tMonitor of Sink: %s\n"
403 "\tLatency: %0.0f usec, configured %0.0f usec\n"
404 "\tFlags: %s%s%s%s%s%s\n"
405 "\tProperties:\n\t\t%s\n"),
407 state_table
[1+i
->state
],
409 pa_strnull(i
->description
),
410 pa_strnull(i
->driver
),
411 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
412 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
415 pa_cvolume_snprint_verbose(cv
, sizeof(cv
), &i
->volume
, &i
->channel_map
, i
->flags
& PA_SOURCE_DECIBEL_VOLUME
),
416 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
417 pa_volume_snprint_verbose(v
, sizeof(v
), i
->base_volume
, i
->flags
& PA_SOURCE_DECIBEL_VOLUME
),
418 i
->monitor_of_sink_name
? i
->monitor_of_sink_name
: _("n/a"),
419 (double) i
->latency
, (double) i
->configured_latency
,
420 i
->flags
& PA_SOURCE_HARDWARE
? "HARDWARE " : "",
421 i
->flags
& PA_SOURCE_NETWORK
? "NETWORK " : "",
422 i
->flags
& PA_SOURCE_HW_MUTE_CTRL
? "HW_MUTE_CTRL " : "",
423 i
->flags
& PA_SOURCE_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
424 i
->flags
& PA_SOURCE_DECIBEL_VOLUME
? "DECIBEL_VOLUME " : "",
425 i
->flags
& PA_SOURCE_LATENCY
? "LATENCY " : "",
426 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
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
));
440 printf(_("\tActive Port: %s\n"),
441 i
->active_port
->name
);
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
) {
457 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c
)));
469 if (nl
&& !short_list_format
)
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
: "");
480 printf(_("Module #%u\n"
483 "\tUsage counter: %s\n"
484 "\tProperties:\n\t\t%s\n"),
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"));
494 static void get_client_info_callback(pa_context
*c
, const pa_client_info
*i
, int is_last
, void *userdata
) {
499 pa_log(_("Failed to get client information: %s"), pa_strerror(pa_context_errno(c
)));
511 if (nl
&& !short_list_format
)
515 pa_snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
517 if (short_list_format
) {
518 printf("%u\t%s\t%s\n",
520 pa_strnull(i
->driver
),
521 pa_strnull(pa_proplist_gets(i
->proplist
, PA_PROP_APPLICATION_PROCESS_BINARY
)));
525 printf(_("Client #%u\n"
527 "\tOwner Module: %s\n"
528 "\tProperties:\n\t\t%s\n"),
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"));
537 static void get_card_info_callback(pa_context
*c
, const pa_card_info
*i
, int is_last
, void *userdata
) {
542 pa_log(_("Failed to get card information: %s"), pa_strerror(pa_context_errno(c
)));
554 if (nl
&& !short_list_format
)
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
));
565 printf(_("Card #%u\n"
568 "\tOwner Module: %s\n"
569 "\tProperties:\n\t\t%s\n"),
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"));
578 if (i
->n_profiles
> 0) {
579 pa_card_profile_info2
**p
;
581 printf(_("\tProfiles:\n"));
582 for (p
= i
->profiles2
; *p
; p
++)
583 printf("\t\t%s: %s (sinks: %u, sources: %u, priority: %u, available: %s)\n", (*p
)->name
,
584 (*p
)->description
, (*p
)->n_sinks
, (*p
)->n_sources
, (*p
)->priority
, pa_yes_no((*p
)->available
));
587 if (i
->active_profile
)
588 printf(_("\tActive Profile: %s\n"),
589 i
->active_profile
->name
);
592 pa_card_port_info
**p
;
594 printf(_("\tPorts:\n"));
595 for (p
= i
->ports
; *p
; p
++) {
596 pa_card_profile_info
**pr
= (*p
)->profiles
;
597 printf("\t\t%s: %s (priority: %u, latency offset: %" PRId64
" usec%s)\n", (*p
)->name
,
598 (*p
)->description
, (*p
)->priority
, (*p
)->latency_offset
,
599 get_available_str_ynonly((*p
)->available
));
601 if (!pa_proplist_isempty((*p
)->proplist
)) {
602 printf(_("\t\t\tProperties:\n\t\t\t\t%s\n"), pl
= pa_proplist_to_string_sep((*p
)->proplist
, "\n\t\t\t\t"));
607 printf(_("\t\t\tPart of profile(s): %s"), pa_strnull((*pr
)->name
));
610 printf(", %s", pa_strnull((*pr
)->name
));
619 static void get_sink_input_info_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
620 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_VERBOSE_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
], f
[PA_FORMAT_INFO_SNPRINT_MAX
];
624 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c
)));
636 if (nl
&& !short_list_format
)
640 pa_snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
641 pa_snprintf(k
, sizeof(k
), "%u", i
->client
);
643 if (short_list_format
) {
644 printf("%u\t%u\t%s\t%s\t%s\n",
647 i
->client
!= PA_INVALID_INDEX
? k
: "-",
648 pa_strnull(i
->driver
),
649 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
));
653 printf(_("Sink Input #%u\n"
655 "\tOwner Module: %s\n"
658 "\tSample Specification: %s\n"
659 "\tChannel Map: %s\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"),
670 pa_strnull(i
->driver
),
671 i
->owner_module
!= PA_INVALID_INDEX
? t
: _("n/a"),
672 i
->client
!= PA_INVALID_INDEX
? k
: _("n/a"),
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
),
679 pa_cvolume_snprint_verbose(cv
, sizeof(cv
), &i
->volume
, &i
->channel_map
, true),
680 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
681 (double) i
->buffer_usec
,
682 (double) i
->sink_usec
,
683 i
->resample_method
? i
->resample_method
: _("n/a"),
684 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
689 static void get_source_output_info_callback(pa_context
*c
, const pa_source_output_info
*i
, int is_last
, void *userdata
) {
690 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_VERBOSE_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
], f
[PA_FORMAT_INFO_SNPRINT_MAX
];
694 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c
)));
706 if (nl
&& !short_list_format
)
710 pa_snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
711 pa_snprintf(k
, sizeof(k
), "%u", i
->client
);
713 if (short_list_format
) {
714 printf("%u\t%u\t%s\t%s\t%s\n",
717 i
->client
!= PA_INVALID_INDEX
? k
: "-",
718 pa_strnull(i
->driver
),
719 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
));
723 printf(_("Source Output #%u\n"
725 "\tOwner Module: %s\n"
728 "\tSample Specification: %s\n"
729 "\tChannel Map: %s\n"
735 "\tBuffer Latency: %0.0f usec\n"
736 "\tSource Latency: %0.0f usec\n"
737 "\tResample method: %s\n"
738 "\tProperties:\n\t\t%s\n"),
740 pa_strnull(i
->driver
),
741 i
->owner_module
!= PA_INVALID_INDEX
? t
: _("n/a"),
742 i
->client
!= PA_INVALID_INDEX
? k
: _("n/a"),
744 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
745 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
746 pa_format_info_snprint(f
, sizeof(f
), i
->format
),
747 pa_yes_no(i
->corked
),
749 pa_cvolume_snprint_verbose(cv
, sizeof(cv
), &i
->volume
, &i
->channel_map
, true),
750 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
751 (double) i
->buffer_usec
,
752 (double) i
->source_usec
,
753 i
->resample_method
? i
->resample_method
: _("n/a"),
754 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
759 static void get_sample_info_callback(pa_context
*c
, const pa_sample_info
*i
, int is_last
, void *userdata
) {
760 char t
[PA_BYTES_SNPRINT_MAX
], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_VERBOSE_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
764 pa_log(_("Failed to get sample information: %s"), pa_strerror(pa_context_errno(c
)));
776 if (nl
&& !short_list_format
)
780 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
782 if (short_list_format
) {
783 printf("%u\t%s\t%s\t%0.3f\n",
786 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : "-",
787 (double) i
->duration
/1000000.0);
791 printf(_("Sample #%u\n"
793 "\tSample Specification: %s\n"
794 "\tChannel Map: %s\n"
797 "\tDuration: %0.1fs\n"
801 "\tProperties:\n\t\t%s\n"),
804 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : _("n/a"),
805 pa_sample_spec_valid(&i
->sample_spec
) ? pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
) : _("n/a"),
806 pa_cvolume_snprint_verbose(cv
, sizeof(cv
), &i
->volume
, &i
->channel_map
, true),
807 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
808 (double) i
->duration
/1000000.0,
811 i
->filename
? i
->filename
: _("n/a"),
812 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
817 static void simple_callback(pa_context
*c
, int success
, void *userdata
) {
819 pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c
)));
827 static void index_callback(pa_context
*c
, uint32_t idx
, void *userdata
) {
828 if (idx
== PA_INVALID_INDEX
) {
829 pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c
)));
839 static void volume_relative_adjust(pa_cvolume
*cv
) {
840 pa_assert((volume_flags
& VOL_RELATIVE
) == VOL_RELATIVE
);
842 /* Relative volume change is additive in case of UINT or PERCENT
843 * and multiplicative for LINEAR or DECIBEL */
844 if ((volume_flags
& 0x0F) == VOL_UINT
|| (volume_flags
& 0x0F) == VOL_PERCENT
) {
845 pa_volume_t v
= pa_cvolume_avg(cv
);
846 v
= v
+ volume
< PA_VOLUME_NORM
? PA_VOLUME_MUTED
: v
+ volume
- PA_VOLUME_NORM
;
847 pa_cvolume_set(cv
, 1, v
);
849 if ((volume_flags
& 0x0F) == VOL_LINEAR
|| (volume_flags
& 0x0F) == VOL_DECIBEL
) {
850 pa_sw_cvolume_multiply_scalar(cv
, cv
, volume
);
854 static void unload_module_by_name_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
855 static bool unloaded
= false;
858 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c
)));
864 if (unloaded
== false)
865 pa_log(_("Failed to unload module: Module %s not loaded"), module_name
);
872 if (pa_streq(module_name
, i
->name
)) {
875 pa_operation_unref(pa_context_unload_module(c
, i
->index
, simple_callback
, NULL
));
879 static void get_sink_volume_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
883 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c
)));
894 volume_relative_adjust(&cv
);
895 pa_operation_unref(pa_context_set_sink_volume_by_name(c
, sink_name
, &cv
, simple_callback
, NULL
));
898 static void get_source_volume_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
902 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c
)));
913 volume_relative_adjust(&cv
);
914 pa_operation_unref(pa_context_set_source_volume_by_name(c
, source_name
, &cv
, simple_callback
, NULL
));
917 static void get_sink_input_volume_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
921 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c
)));
932 volume_relative_adjust(&cv
);
933 pa_operation_unref(pa_context_set_sink_input_volume(c
, sink_input_idx
, &cv
, simple_callback
, NULL
));
936 static void get_source_output_volume_callback(pa_context
*c
, const pa_source_output_info
*o
, int is_last
, void *userdata
) {
940 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c
)));
951 volume_relative_adjust(&cv
);
952 pa_operation_unref(pa_context_set_source_output_volume(c
, source_output_idx
, &cv
, simple_callback
, NULL
));
955 static void sink_toggle_mute_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
957 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c
)));
967 pa_operation_unref(pa_context_set_sink_mute_by_name(c
, i
->name
, !i
->mute
, simple_callback
, NULL
));
970 static void source_toggle_mute_callback(pa_context
*c
, const pa_source_info
*o
, int is_last
, void *userdata
) {
972 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c
)));
982 pa_operation_unref(pa_context_set_source_mute_by_name(c
, o
->name
, !o
->mute
, simple_callback
, NULL
));
985 static void sink_input_toggle_mute_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
987 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c
)));
997 pa_operation_unref(pa_context_set_sink_input_mute(c
, i
->index
, !i
->mute
, simple_callback
, NULL
));
1000 static void source_output_toggle_mute_callback(pa_context
*c
, const pa_source_output_info
*o
, int is_last
, void *userdata
) {
1002 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c
)));
1012 pa_operation_unref(pa_context_set_source_output_mute(c
, o
->index
, !o
->mute
, simple_callback
, NULL
));
1015 /* PA_MAX_FORMATS is defined in internal.h so we just define a sane value here */
1016 #define MAX_FORMATS 256
1018 static void set_sink_formats(pa_context
*c
, uint32_t sink
, const char *str
) {
1019 pa_format_info
*f_arr
[MAX_FORMATS
];
1020 char *format
= NULL
;
1021 const char *state
= NULL
;
1023 pa_operation
*o
= NULL
;
1025 while ((format
= pa_split(str
, ";", &state
))) {
1026 pa_format_info
*f
= pa_format_info_from_string(pa_strip(format
));
1029 pa_log(_("Failed to set format: invalid format string %s"), format
);
1037 o
= pa_ext_device_restore_save_formats(c
, PA_DEVICE_TYPE_SINK
, sink
, i
, f_arr
, simple_callback
, NULL
);
1039 pa_operation_unref(o
);
1047 pa_format_info_free(f_arr
[i
]);
1053 pa_format_info_free(f_arr
[i
]);
1058 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
1061 switch (pa_stream_get_state(s
)) {
1062 case PA_STREAM_CREATING
:
1063 case PA_STREAM_READY
:
1066 case PA_STREAM_TERMINATED
:
1070 case PA_STREAM_FAILED
:
1072 pa_log(_("Failed to upload sample: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
1077 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
1080 pa_assert(s
&& length
&& sndfile
);
1082 d
= pa_xmalloc(length
);
1084 pa_assert(sample_length
>= length
);
1085 l
= (sf_count_t
) (length
/pa_frame_size(&sample_spec
));
1087 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
1089 pa_log(_("Premature end of file"));
1094 pa_stream_write(s
, d
, length
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
1096 sample_length
-= length
;
1098 if (sample_length
<= 0) {
1099 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
1100 pa_stream_finish_upload(sample_stream
);
1104 static const char *subscription_event_type_to_string(pa_subscription_event_type_t t
) {
1106 switch (t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) {
1108 case PA_SUBSCRIPTION_EVENT_NEW
:
1111 case PA_SUBSCRIPTION_EVENT_CHANGE
:
1114 case PA_SUBSCRIPTION_EVENT_REMOVE
:
1118 return _("unknown");
1121 static const char *subscription_event_facility_to_string(pa_subscription_event_type_t t
) {
1123 switch (t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) {
1125 case PA_SUBSCRIPTION_EVENT_SINK
:
1128 case PA_SUBSCRIPTION_EVENT_SOURCE
:
1131 case PA_SUBSCRIPTION_EVENT_SINK_INPUT
:
1132 return _("sink-input");
1134 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
:
1135 return _("source-output");
1137 case PA_SUBSCRIPTION_EVENT_MODULE
:
1140 case PA_SUBSCRIPTION_EVENT_CLIENT
:
1143 case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
:
1144 return _("sample-cache");
1146 case PA_SUBSCRIPTION_EVENT_SERVER
:
1149 case PA_SUBSCRIPTION_EVENT_CARD
:
1153 return _("unknown");
1156 static void context_subscribe_callback(pa_context
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
1159 printf(_("Event '%s' on %s #%u\n"),
1160 subscription_event_type_to_string(t
),
1161 subscription_event_facility_to_string(t
),
1166 static void context_state_callback(pa_context
*c
, void *userdata
) {
1167 pa_operation
*o
= NULL
;
1171 switch (pa_context_get_state(c
)) {
1172 case PA_CONTEXT_CONNECTING
:
1173 case PA_CONTEXT_AUTHORIZING
:
1174 case PA_CONTEXT_SETTING_NAME
:
1177 case PA_CONTEXT_READY
:
1180 o
= pa_context_stat(c
, stat_callback
, NULL
);
1181 if (short_list_format
)
1185 pa_operation_unref(o
);
1191 o
= pa_context_get_server_info(c
, get_server_info_callback
, NULL
);
1195 o
= pa_context_play_sample(c
, sample_name
, sink_name
, PA_VOLUME_NORM
, simple_callback
, NULL
);
1199 o
= pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
);
1203 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
1204 pa_assert(sample_stream
);
1206 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
1207 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
1208 pa_stream_connect_upload(sample_stream
, sample_length
);
1213 o
= pa_context_exit_daemon(c
, simple_callback
, NULL
);
1218 if (pa_streq(list_type
, "modules"))
1219 o
= pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
);
1220 else if (pa_streq(list_type
, "sinks"))
1221 o
= pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
);
1222 else if (pa_streq(list_type
, "sources"))
1223 o
= pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
);
1224 else if (pa_streq(list_type
, "sink-inputs"))
1225 o
= pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
);
1226 else if (pa_streq(list_type
, "source-outputs"))
1227 o
= pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
);
1228 else if (pa_streq(list_type
, "clients"))
1229 o
= pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
);
1230 else if (pa_streq(list_type
, "samples"))
1231 o
= pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
);
1232 else if (pa_streq(list_type
, "cards"))
1233 o
= pa_context_get_card_info_list(c
, get_card_info_callback
, NULL
);
1235 pa_assert_not_reached();
1237 o
= pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
);
1239 pa_operation_unref(o
);
1243 o
= pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
);
1245 pa_operation_unref(o
);
1249 o
= pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
);
1251 pa_operation_unref(o
);
1254 o
= pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
);
1256 pa_operation_unref(o
);
1260 o
= pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
);
1262 pa_operation_unref(o
);
1266 o
= pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
);
1268 pa_operation_unref(o
);
1272 o
= pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
);
1274 pa_operation_unref(o
);
1278 o
= pa_context_get_card_info_list(c
, get_card_info_callback
, NULL
);
1280 pa_operation_unref(o
);
1288 case MOVE_SINK_INPUT
:
1289 o
= pa_context_move_sink_input_by_name(c
, sink_input_idx
, sink_name
, simple_callback
, NULL
);
1292 case MOVE_SOURCE_OUTPUT
:
1293 o
= pa_context_move_source_output_by_name(c
, source_output_idx
, source_name
, simple_callback
, NULL
);
1297 o
= pa_context_load_module(c
, module_name
, module_args
, index_callback
, NULL
);
1302 o
= pa_context_get_module_info_list(c
, unload_module_by_name_callback
, NULL
);
1304 o
= pa_context_unload_module(c
, module_index
, simple_callback
, NULL
);
1309 o
= pa_context_suspend_sink_by_name(c
, sink_name
, suspend
, simple_callback
, NULL
);
1311 o
= pa_context_suspend_sink_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
);
1314 case SUSPEND_SOURCE
:
1316 o
= pa_context_suspend_source_by_name(c
, source_name
, suspend
, simple_callback
, NULL
);
1318 o
= pa_context_suspend_source_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
);
1321 case SET_CARD_PROFILE
:
1322 o
= pa_context_set_card_profile_by_name(c
, card_name
, profile_name
, simple_callback
, NULL
);
1326 o
= pa_context_set_sink_port_by_name(c
, sink_name
, port_name
, simple_callback
, NULL
);
1329 case SET_DEFAULT_SINK
:
1330 o
= pa_context_set_default_sink(c
, sink_name
, simple_callback
, NULL
);
1333 case SET_SOURCE_PORT
:
1334 o
= pa_context_set_source_port_by_name(c
, source_name
, port_name
, simple_callback
, NULL
);
1337 case SET_DEFAULT_SOURCE
:
1338 o
= pa_context_set_default_source(c
, source_name
, simple_callback
, NULL
);
1342 if (mute
== TOGGLE_MUTE
)
1343 o
= pa_context_get_sink_info_by_name(c
, sink_name
, sink_toggle_mute_callback
, NULL
);
1345 o
= pa_context_set_sink_mute_by_name(c
, sink_name
, mute
, simple_callback
, NULL
);
1348 case SET_SOURCE_MUTE
:
1349 if (mute
== TOGGLE_MUTE
)
1350 o
= pa_context_get_source_info_by_name(c
, source_name
, source_toggle_mute_callback
, NULL
);
1352 o
= pa_context_set_source_mute_by_name(c
, source_name
, mute
, simple_callback
, NULL
);
1355 case SET_SINK_INPUT_MUTE
:
1356 if (mute
== TOGGLE_MUTE
)
1357 o
= pa_context_get_sink_input_info(c
, sink_input_idx
, sink_input_toggle_mute_callback
, NULL
);
1359 o
= pa_context_set_sink_input_mute(c
, sink_input_idx
, mute
, simple_callback
, NULL
);
1362 case SET_SOURCE_OUTPUT_MUTE
:
1363 if (mute
== TOGGLE_MUTE
)
1364 o
= pa_context_get_source_output_info(c
, source_output_idx
, source_output_toggle_mute_callback
, NULL
);
1366 o
= pa_context_set_source_output_mute(c
, source_output_idx
, mute
, simple_callback
, NULL
);
1369 case SET_SINK_VOLUME
:
1370 if ((volume_flags
& VOL_RELATIVE
) == VOL_RELATIVE
) {
1371 o
= pa_context_get_sink_info_by_name(c
, sink_name
, get_sink_volume_callback
, NULL
);
1374 pa_cvolume_set(&v
, 1, volume
);
1375 o
= pa_context_set_sink_volume_by_name(c
, sink_name
, &v
, simple_callback
, NULL
);
1379 case SET_SOURCE_VOLUME
:
1380 if ((volume_flags
& VOL_RELATIVE
) == VOL_RELATIVE
) {
1381 o
= pa_context_get_source_info_by_name(c
, source_name
, get_source_volume_callback
, NULL
);
1384 pa_cvolume_set(&v
, 1, volume
);
1385 o
= pa_context_set_source_volume_by_name(c
, source_name
, &v
, simple_callback
, NULL
);
1389 case SET_SINK_INPUT_VOLUME
:
1390 if ((volume_flags
& VOL_RELATIVE
) == VOL_RELATIVE
) {
1391 o
= pa_context_get_sink_input_info(c
, sink_input_idx
, get_sink_input_volume_callback
, NULL
);
1394 pa_cvolume_set(&v
, 1, volume
);
1395 o
= pa_context_set_sink_input_volume(c
, sink_input_idx
, &v
, simple_callback
, NULL
);
1399 case SET_SOURCE_OUTPUT_VOLUME
:
1400 if ((volume_flags
& VOL_RELATIVE
) == VOL_RELATIVE
) {
1401 o
= pa_context_get_source_output_info(c
, source_output_idx
, get_source_output_volume_callback
, NULL
);
1404 pa_cvolume_set(&v
, 1, volume
);
1405 o
= pa_context_set_source_output_volume(c
, source_output_idx
, &v
, simple_callback
, NULL
);
1409 case SET_SINK_FORMATS
:
1410 set_sink_formats(c
, sink_idx
, formats
);
1413 case SET_PORT_LATENCY_OFFSET
:
1414 o
= pa_context_set_port_latency_offset(c
, card_name
, port_name
, latency_offset
, simple_callback
, NULL
);
1418 pa_context_set_subscribe_callback(c
, context_subscribe_callback
, NULL
);
1420 o
= pa_context_subscribe(c
,
1421 PA_SUBSCRIPTION_MASK_SINK
|
1422 PA_SUBSCRIPTION_MASK_SOURCE
|
1423 PA_SUBSCRIPTION_MASK_SINK_INPUT
|
1424 PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT
|
1425 PA_SUBSCRIPTION_MASK_MODULE
|
1426 PA_SUBSCRIPTION_MASK_CLIENT
|
1427 PA_SUBSCRIPTION_MASK_SAMPLE_CACHE
|
1428 PA_SUBSCRIPTION_MASK_SERVER
|
1429 PA_SUBSCRIPTION_MASK_CARD
,
1435 pa_assert_not_reached();
1439 pa_operation_unref(o
);
1444 pa_log("Operation failed: %s", pa_strerror(pa_context_errno(c
)));
1450 case PA_CONTEXT_TERMINATED
:
1454 case PA_CONTEXT_FAILED
:
1456 pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c
)));
1461 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
1462 pa_log(_("Got SIGINT, exiting."));
1466 static int parse_volume(const char *vol_spec
, pa_volume_t
*vol
, enum volume_flags
*vol_flags
) {
1470 pa_assert(vol_spec
);
1472 pa_assert(vol_flags
);
1474 vs
= pa_xstrdup(vol_spec
);
1476 *vol_flags
= (pa_startswith(vs
, "+") || pa_startswith(vs
, "-")) ? VOL_RELATIVE
: VOL_ABSOLUTE
;
1477 if (strchr(vs
, '.'))
1478 *vol_flags
|= VOL_LINEAR
;
1479 if (pa_endswith(vs
, "%")) {
1480 *vol_flags
|= VOL_PERCENT
;
1481 vs
[strlen(vs
)-1] = 0;
1483 if (pa_endswith(vs
, "db") || pa_endswith(vs
, "dB")) {
1484 *vol_flags
|= VOL_DECIBEL
;
1485 vs
[strlen(vs
)-2] = 0;
1488 if (pa_atod(vs
, &v
) < 0) {
1489 pa_log(_("Invalid volume specification"));
1496 if ((*vol_flags
& VOL_RELATIVE
) == VOL_RELATIVE
) {
1497 if ((*vol_flags
& 0x0F) == VOL_UINT
)
1498 v
+= (double) PA_VOLUME_NORM
;
1499 if ((*vol_flags
& 0x0F) == VOL_PERCENT
)
1501 if ((*vol_flags
& 0x0F) == VOL_LINEAR
)
1504 if ((*vol_flags
& 0x0F) == VOL_PERCENT
)
1505 v
= v
* (double) PA_VOLUME_NORM
/ 100;
1506 if ((*vol_flags
& 0x0F) == VOL_LINEAR
)
1507 v
= pa_sw_volume_from_linear(v
);
1508 if ((*vol_flags
& 0x0F) == VOL_DECIBEL
)
1509 v
= pa_sw_volume_from_dB(v
);
1511 if (!PA_VOLUME_IS_VALID((pa_volume_t
) v
)) {
1512 pa_log(_("Volume outside permissible range.\n"));
1516 *vol
= (pa_volume_t
) v
;
1521 static enum mute_flags
parse_mute(const char *mute_text
) {
1524 pa_assert(mute_text
);
1526 if (pa_streq("toggle", mute_text
))
1529 b
= pa_parse_boolean(mute_text
);
1536 return INVALID_MUTE
;
1540 static void help(const char *argv0
) {
1542 printf("%s %s %s\n", argv0
, _("[options]"), "stat [short]");
1543 printf("%s %s %s\n", argv0
, _("[options]"), "info");
1544 printf("%s %s %s %s\n", argv0
, _("[options]"), "list [short]", _("[TYPE]"));
1545 printf("%s %s %s\n", argv0
, _("[options]"), "exit");
1546 printf("%s %s %s %s\n", argv0
, _("[options]"), "upload-sample", _("FILENAME [NAME]"));
1547 printf("%s %s %s %s\n", argv0
, _("[options]"), "play-sample ", _("NAME [SINK]"));
1548 printf("%s %s %s %s\n", argv0
, _("[options]"), "remove-sample ", _("NAME"));
1549 printf("%s %s %s %s\n", argv0
, _("[options]"), "load-module ", _("NAME [ARGS ...]"));
1550 printf("%s %s %s %s\n", argv0
, _("[options]"), "unload-module ", _("NAME|#N"));
1551 printf("%s %s %s %s\n", argv0
, _("[options]"), "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
1552 printf("%s %s %s %s\n", argv0
, _("[options]"), "suspend-(sink|source)", _("NAME|#N 1|0"));
1553 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-card-profile ", _("CARD PROFILE"));
1554 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-default-(sink|source)", _("NAME"));
1555 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink|source)-port", _("NAME|#N PORT"));
1556 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink|source)-volume", _("NAME|#N VOLUME"));
1557 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink-input|source-output)-volume", _("#N VOLUME"));
1558 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink|source)-mute", _("NAME|#N 1|0|toggle"));
1559 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0|toggle"));
1560 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-sink-formats", _("#N FORMATS"));
1561 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
1562 printf("%s %s %s\n", argv0
, _("[options]"), "subscribe");
1563 printf(_("\nThe special names @DEFAULT_SINK@, @DEFAULT_SOURCE@ and @DEFAULT_MONITOR@\n"
1564 "can be used to specify the default sink, source and monitor.\n"));
1567 " -h, --help Show this help\n"
1568 " --version Show version\n\n"
1569 " -s, --server=SERVER The name of the server to connect to\n"
1570 " -n, --client-name=NAME How to call this client on the server\n"));
1577 int main(int argc
, char *argv
[]) {
1578 pa_mainloop
*m
= NULL
;
1580 char *server
= NULL
, *bn
;
1582 static const struct option long_options
[] = {
1583 {"server", 1, NULL
, 's'},
1584 {"client-name", 1, NULL
, 'n'},
1585 {"version", 0, NULL
, ARG_VERSION
},
1586 {"help", 0, NULL
, 'h'},
1590 setlocale(LC_ALL
, "");
1592 bindtextdomain(GETTEXT_PACKAGE
, PULSE_LOCALEDIR
);
1595 bn
= pa_path_get_filename(argv
[0]);
1597 proplist
= pa_proplist_new();
1599 while ((c
= getopt_long(argc
, argv
, "+s:n:h", long_options
, NULL
)) != -1) {
1607 printf(_("pactl %s\n"
1608 "Compiled with libpulse %s\n"
1609 "Linked with libpulse %s\n"),
1611 pa_get_headers_version(),
1612 pa_get_library_version());
1618 server
= pa_xstrdup(optarg
);
1624 if (!(t
= pa_locale_to_utf8(optarg
)) ||
1625 pa_proplist_sets(proplist
, PA_PROP_APPLICATION_NAME
, t
) < 0) {
1627 pa_log(_("Invalid client name '%s'"), t
? t
: optarg
);
1641 if (optind
< argc
) {
1642 if (pa_streq(argv
[optind
], "stat")) {
1644 short_list_format
= false;
1645 if (optind
+1 < argc
&& pa_streq(argv
[optind
+1], "short"))
1646 short_list_format
= true;
1648 } else if (pa_streq(argv
[optind
], "info"))
1651 else if (pa_streq(argv
[optind
], "exit"))
1654 else if (pa_streq(argv
[optind
], "list")) {
1657 for (int i
= optind
+1; i
< argc
; i
++) {
1658 if (pa_streq(argv
[i
], "modules") || pa_streq(argv
[i
], "clients") ||
1659 pa_streq(argv
[i
], "sinks") || pa_streq(argv
[i
], "sink-inputs") ||
1660 pa_streq(argv
[i
], "sources") || pa_streq(argv
[i
], "source-outputs") ||
1661 pa_streq(argv
[i
], "samples") || pa_streq(argv
[i
], "cards")) {
1662 list_type
= pa_xstrdup(argv
[i
]);
1663 } else if (pa_streq(argv
[i
], "short")) {
1664 short_list_format
= true;
1666 pa_log(_("Specify nothing, or one of: %s"), "modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards");
1671 } else if (pa_streq(argv
[optind
], "upload-sample")) {
1673 action
= UPLOAD_SAMPLE
;
1675 if (optind
+1 >= argc
) {
1676 pa_log(_("Please specify a sample file to load"));
1680 if (optind
+2 < argc
)
1681 sample_name
= pa_xstrdup(argv
[optind
+2]);
1683 char *f
= pa_path_get_filename(argv
[optind
+1]);
1684 sample_name
= pa_xstrndup(f
, strcspn(f
, "."));
1688 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfi
))) {
1689 pa_log(_("Failed to open sound file."));
1693 if (pa_sndfile_read_sample_spec(sndfile
, &sample_spec
) < 0) {
1694 pa_log(_("Failed to determine sample specification from file."));
1697 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
1699 if (pa_sndfile_read_channel_map(sndfile
, &channel_map
) < 0) {
1700 if (sample_spec
.channels
> 2)
1701 pa_log(_("Warning: Failed to determine sample specification from file."));
1702 pa_channel_map_init_extend(&channel_map
, sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1705 pa_assert(pa_channel_map_compatible(&channel_map
, &sample_spec
));
1706 sample_length
= (size_t) sfi
.frames
*pa_frame_size(&sample_spec
);
1708 } else if (pa_streq(argv
[optind
], "play-sample")) {
1709 action
= PLAY_SAMPLE
;
1710 if (argc
!= optind
+2 && argc
!= optind
+3) {
1711 pa_log(_("You have to specify a sample name to play"));
1715 sample_name
= pa_xstrdup(argv
[optind
+1]);
1717 if (optind
+2 < argc
)
1718 sink_name
= pa_xstrdup(argv
[optind
+2]);
1720 } else if (pa_streq(argv
[optind
], "remove-sample")) {
1721 action
= REMOVE_SAMPLE
;
1722 if (argc
!= optind
+2) {
1723 pa_log(_("You have to specify a sample name to remove"));
1727 sample_name
= pa_xstrdup(argv
[optind
+1]);
1729 } else if (pa_streq(argv
[optind
], "move-sink-input")) {
1730 action
= MOVE_SINK_INPUT
;
1731 if (argc
!= optind
+3) {
1732 pa_log(_("You have to specify a sink input index and a sink"));
1736 sink_input_idx
= (uint32_t) atoi(argv
[optind
+1]);
1737 sink_name
= pa_xstrdup(argv
[optind
+2]);
1739 } else if (pa_streq(argv
[optind
], "move-source-output")) {
1740 action
= MOVE_SOURCE_OUTPUT
;
1741 if (argc
!= optind
+3) {
1742 pa_log(_("You have to specify a source output index and a source"));
1746 source_output_idx
= (uint32_t) atoi(argv
[optind
+1]);
1747 source_name
= pa_xstrdup(argv
[optind
+2]);
1749 } else if (pa_streq(argv
[optind
], "load-module")) {
1754 action
= LOAD_MODULE
;
1756 if (argc
<= optind
+1) {
1757 pa_log(_("You have to specify a module name and arguments."));
1761 module_name
= argv
[optind
+1];
1763 for (i
= optind
+2; i
< argc
; i
++)
1764 n
+= strlen(argv
[i
])+1;
1767 p
= module_args
= pa_xmalloc(n
);
1769 for (i
= optind
+2; i
< argc
; i
++)
1770 p
+= sprintf(p
, "%s%s", p
== module_args
? "" : " ", argv
[i
]);
1773 } else if (pa_streq(argv
[optind
], "unload-module")) {
1774 action
= UNLOAD_MODULE
;
1776 if (argc
!= optind
+2) {
1777 pa_log(_("You have to specify a module index or name"));
1781 if (pa_atou(argv
[optind
+ 1], &module_index
) < 0)
1782 module_name
= argv
[optind
+ 1];
1784 } else if (pa_streq(argv
[optind
], "suspend-sink")) {
1787 action
= SUSPEND_SINK
;
1789 if (argc
> optind
+3 || optind
+1 >= argc
) {
1790 pa_log(_("You may not specify more than one sink. You have to specify a boolean value."));
1794 if ((b
= pa_parse_boolean(argv
[argc
-1])) < 0) {
1795 pa_log(_("Invalid suspend specification."));
1801 if (argc
> optind
+2)
1802 sink_name
= pa_xstrdup(argv
[optind
+1]);
1804 } else if (pa_streq(argv
[optind
], "suspend-source")) {
1807 action
= SUSPEND_SOURCE
;
1809 if (argc
> optind
+3 || optind
+1 >= argc
) {
1810 pa_log(_("You may not specify more than one source. You have to specify a boolean value."));
1814 if ((b
= pa_parse_boolean(argv
[argc
-1])) < 0) {
1815 pa_log(_("Invalid suspend specification."));
1821 if (argc
> optind
+2)
1822 source_name
= pa_xstrdup(argv
[optind
+1]);
1823 } else if (pa_streq(argv
[optind
], "set-card-profile")) {
1824 action
= SET_CARD_PROFILE
;
1826 if (argc
!= optind
+3) {
1827 pa_log(_("You have to specify a card name/index and a profile name"));
1831 card_name
= pa_xstrdup(argv
[optind
+1]);
1832 profile_name
= pa_xstrdup(argv
[optind
+2]);
1834 } else if (pa_streq(argv
[optind
], "set-sink-port")) {
1835 action
= SET_SINK_PORT
;
1837 if (argc
!= optind
+3) {
1838 pa_log(_("You have to specify a sink name/index and a port name"));
1842 sink_name
= pa_xstrdup(argv
[optind
+1]);
1843 port_name
= pa_xstrdup(argv
[optind
+2]);
1845 } else if (pa_streq(argv
[optind
], "set-default-sink")) {
1846 action
= SET_DEFAULT_SINK
;
1848 if (argc
!= optind
+2) {
1849 pa_log(_("You have to specify a sink name"));
1853 sink_name
= pa_xstrdup(argv
[optind
+1]);
1855 } else if (pa_streq(argv
[optind
], "set-source-port")) {
1856 action
= SET_SOURCE_PORT
;
1858 if (argc
!= optind
+3) {
1859 pa_log(_("You have to specify a source name/index and a port name"));
1863 source_name
= pa_xstrdup(argv
[optind
+1]);
1864 port_name
= pa_xstrdup(argv
[optind
+2]);
1866 } else if (pa_streq(argv
[optind
], "set-default-source")) {
1867 action
= SET_DEFAULT_SOURCE
;
1869 if (argc
!= optind
+2) {
1870 pa_log(_("You have to specify a source name"));
1874 source_name
= pa_xstrdup(argv
[optind
+1]);
1876 } else if (pa_streq(argv
[optind
], "set-sink-volume")) {
1877 action
= SET_SINK_VOLUME
;
1879 if (argc
!= optind
+3) {
1880 pa_log(_("You have to specify a sink name/index and a volume"));
1884 sink_name
= pa_xstrdup(argv
[optind
+1]);
1886 if (parse_volume(argv
[optind
+2], &volume
, &volume_flags
) < 0)
1889 } else if (pa_streq(argv
[optind
], "set-source-volume")) {
1890 action
= SET_SOURCE_VOLUME
;
1892 if (argc
!= optind
+3) {
1893 pa_log(_("You have to specify a source name/index and a volume"));
1897 source_name
= pa_xstrdup(argv
[optind
+1]);
1899 if (parse_volume(argv
[optind
+2], &volume
, &volume_flags
) < 0)
1902 } else if (pa_streq(argv
[optind
], "set-sink-input-volume")) {
1903 action
= SET_SINK_INPUT_VOLUME
;
1905 if (argc
!= optind
+3) {
1906 pa_log(_("You have to specify a sink input index and a volume"));
1910 if (pa_atou(argv
[optind
+1], &sink_input_idx
) < 0) {
1911 pa_log(_("Invalid sink input index"));
1915 if (parse_volume(argv
[optind
+2], &volume
, &volume_flags
) < 0)
1918 } else if (pa_streq(argv
[optind
], "set-source-output-volume")) {
1919 action
= SET_SOURCE_OUTPUT_VOLUME
;
1921 if (argc
!= optind
+3) {
1922 pa_log(_("You have to specify a source output index and a volume"));
1926 if (pa_atou(argv
[optind
+1], &source_output_idx
) < 0) {
1927 pa_log(_("Invalid source output index"));
1931 if (parse_volume(argv
[optind
+2], &volume
, &volume_flags
) < 0)
1934 } else if (pa_streq(argv
[optind
], "set-sink-mute")) {
1935 action
= SET_SINK_MUTE
;
1937 if (argc
!= optind
+3) {
1938 pa_log(_("You have to specify a sink name/index and a mute boolean"));
1942 if ((mute
= parse_mute(argv
[optind
+2])) == INVALID_MUTE
) {
1943 pa_log(_("Invalid mute specification"));
1947 sink_name
= pa_xstrdup(argv
[optind
+1]);
1949 } else if (pa_streq(argv
[optind
], "set-source-mute")) {
1950 action
= SET_SOURCE_MUTE
;
1952 if (argc
!= optind
+3) {
1953 pa_log(_("You have to specify a source name/index and a mute boolean"));
1957 if ((mute
= parse_mute(argv
[optind
+2])) == INVALID_MUTE
) {
1958 pa_log(_("Invalid mute specification"));
1962 source_name
= pa_xstrdup(argv
[optind
+1]);
1964 } else if (pa_streq(argv
[optind
], "set-sink-input-mute")) {
1965 action
= SET_SINK_INPUT_MUTE
;
1967 if (argc
!= optind
+3) {
1968 pa_log(_("You have to specify a sink input index and a mute boolean"));
1972 if (pa_atou(argv
[optind
+1], &sink_input_idx
) < 0) {
1973 pa_log(_("Invalid sink input index specification"));
1977 if ((mute
= parse_mute(argv
[optind
+2])) == INVALID_MUTE
) {
1978 pa_log(_("Invalid mute specification"));
1982 } else if (pa_streq(argv
[optind
], "set-source-output-mute")) {
1983 action
= SET_SOURCE_OUTPUT_MUTE
;
1985 if (argc
!= optind
+3) {
1986 pa_log(_("You have to specify a source output index and a mute boolean"));
1990 if (pa_atou(argv
[optind
+1], &source_output_idx
) < 0) {
1991 pa_log(_("Invalid source output index specification"));
1995 if ((mute
= parse_mute(argv
[optind
+2])) == INVALID_MUTE
) {
1996 pa_log(_("Invalid mute specification"));
2000 } else if (pa_streq(argv
[optind
], "subscribe"))
2004 else if (pa_streq(argv
[optind
], "set-sink-formats")) {
2007 if (argc
!= optind
+3 || pa_atoi(argv
[optind
+1], &tmp
) < 0) {
2008 pa_log(_("You have to specify a sink index and a semicolon-separated list of supported formats"));
2013 action
= SET_SINK_FORMATS
;
2014 formats
= pa_xstrdup(argv
[optind
+2]);
2016 } else if (pa_streq(argv
[optind
], "set-port-latency-offset")) {
2017 action
= SET_PORT_LATENCY_OFFSET
;
2019 if (argc
!= optind
+4) {
2020 pa_log(_("You have to specify a card name/index, a port name and a latency offset"));
2024 card_name
= pa_xstrdup(argv
[optind
+1]);
2025 port_name
= pa_xstrdup(argv
[optind
+2]);
2026 if (pa_atoi(argv
[optind
+ 3], &latency_offset
) < 0) {
2027 pa_log(_("Could not parse latency offset"));
2031 } else if (pa_streq(argv
[optind
], "help")) {
2038 if (action
== NONE
) {
2039 pa_log(_("No valid command specified."));
2043 if (!(m
= pa_mainloop_new())) {
2044 pa_log(_("pa_mainloop_new() failed."));
2048 mainloop_api
= pa_mainloop_get_api(m
);
2050 pa_assert_se(pa_signal_init(mainloop_api
) == 0);
2051 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
2052 pa_signal_new(SIGTERM
, exit_signal_callback
, NULL
);
2053 pa_disable_sigpipe();
2055 if (!(context
= pa_context_new_with_proplist(mainloop_api
, NULL
, proplist
))) {
2056 pa_log(_("pa_context_new() failed."));
2060 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
2061 if (pa_context_connect(context
, server
, 0, NULL
) < 0) {
2062 pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context
)));
2066 if (pa_mainloop_run(m
, &ret
) < 0) {
2067 pa_log(_("pa_mainloop_run() failed."));
2073 pa_stream_unref(sample_stream
);
2076 pa_context_unref(context
);
2080 pa_mainloop_free(m
);
2084 pa_xfree(list_type
);
2085 pa_xfree(sample_name
);
2086 pa_xfree(sink_name
);
2087 pa_xfree(source_name
);
2088 pa_xfree(module_args
);
2089 pa_xfree(card_name
);
2090 pa_xfree(profile_name
);
2091 pa_xfree(port_name
);
2098 pa_proplist_free(proplist
);