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_cvolume 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
) {
846 for (i
= 0; i
< cv
->channels
; i
++) {
847 if (cv
->values
[i
] + volume
.values
[i
] < PA_VOLUME_NORM
)
848 cv
->values
[i
] = PA_VOLUME_MUTED
;
850 cv
->values
[i
] = cv
->values
[i
] + volume
.values
[i
] - PA_VOLUME_NORM
;
853 if ((volume_flags
& 0x0F) == VOL_LINEAR
|| (volume_flags
& 0x0F) == VOL_DECIBEL
)
854 pa_sw_cvolume_multiply(cv
, cv
, &volume
);
857 static void unload_module_by_name_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
858 static bool unloaded
= false;
861 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c
)));
867 if (unloaded
== false)
868 pa_log(_("Failed to unload module: Module %s not loaded"), module_name
);
875 if (pa_streq(module_name
, i
->name
)) {
878 pa_operation_unref(pa_context_unload_module(c
, i
->index
, simple_callback
, NULL
));
882 static void fill_volume(pa_cvolume
*cv
, unsigned supported
) {
883 if (volume
.channels
== 1) {
884 pa_cvolume_set(&volume
, supported
, volume
.values
[0]);
885 } else if (volume
.channels
!= supported
) {
886 pa_log(_("Failed to set volume: You tried to set volumes for %d channels, whereas channel/s supported = %d\n"),
887 volume
.channels
, supported
);
892 if (volume_flags
& VOL_RELATIVE
)
893 volume_relative_adjust(cv
);
898 static void get_sink_volume_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
902 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c
)));
913 fill_volume(&cv
, i
->channel_map
.channels
);
915 pa_operation_unref(pa_context_set_sink_volume_by_name(c
, sink_name
, &cv
, simple_callback
, NULL
));
918 static void get_source_volume_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
922 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c
)));
933 fill_volume(&cv
, i
->channel_map
.channels
);
935 pa_operation_unref(pa_context_set_source_volume_by_name(c
, source_name
, &cv
, simple_callback
, NULL
));
938 static void get_sink_input_volume_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
942 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c
)));
953 fill_volume(&cv
, i
->channel_map
.channels
);
955 pa_operation_unref(pa_context_set_sink_input_volume(c
, sink_input_idx
, &cv
, simple_callback
, NULL
));
958 static void get_source_output_volume_callback(pa_context
*c
, const pa_source_output_info
*o
, int is_last
, void *userdata
) {
962 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c
)));
973 fill_volume(&cv
, o
->channel_map
.channels
);
975 pa_operation_unref(pa_context_set_source_output_volume(c
, source_output_idx
, &cv
, simple_callback
, NULL
));
978 static void sink_toggle_mute_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
980 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c
)));
990 pa_operation_unref(pa_context_set_sink_mute_by_name(c
, i
->name
, !i
->mute
, simple_callback
, NULL
));
993 static void source_toggle_mute_callback(pa_context
*c
, const pa_source_info
*o
, int is_last
, void *userdata
) {
995 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c
)));
1005 pa_operation_unref(pa_context_set_source_mute_by_name(c
, o
->name
, !o
->mute
, simple_callback
, NULL
));
1008 static void sink_input_toggle_mute_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
1010 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c
)));
1020 pa_operation_unref(pa_context_set_sink_input_mute(c
, i
->index
, !i
->mute
, simple_callback
, NULL
));
1023 static void source_output_toggle_mute_callback(pa_context
*c
, const pa_source_output_info
*o
, int is_last
, void *userdata
) {
1025 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c
)));
1035 pa_operation_unref(pa_context_set_source_output_mute(c
, o
->index
, !o
->mute
, simple_callback
, NULL
));
1038 /* PA_MAX_FORMATS is defined in internal.h so we just define a sane value here */
1039 #define MAX_FORMATS 256
1041 static void set_sink_formats(pa_context
*c
, uint32_t sink
, const char *str
) {
1042 pa_format_info
*f_arr
[MAX_FORMATS
];
1043 char *format
= NULL
;
1044 const char *state
= NULL
;
1046 pa_operation
*o
= NULL
;
1048 while ((format
= pa_split(str
, ";", &state
))) {
1049 pa_format_info
*f
= pa_format_info_from_string(pa_strip(format
));
1052 pa_log(_("Failed to set format: invalid format string %s"), format
);
1060 o
= pa_ext_device_restore_save_formats(c
, PA_DEVICE_TYPE_SINK
, sink
, i
, f_arr
, simple_callback
, NULL
);
1062 pa_operation_unref(o
);
1070 pa_format_info_free(f_arr
[i
]);
1076 pa_format_info_free(f_arr
[i
]);
1081 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
1084 switch (pa_stream_get_state(s
)) {
1085 case PA_STREAM_CREATING
:
1086 case PA_STREAM_READY
:
1089 case PA_STREAM_TERMINATED
:
1093 case PA_STREAM_FAILED
:
1095 pa_log(_("Failed to upload sample: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
1100 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
1103 pa_assert(s
&& length
&& sndfile
);
1105 d
= pa_xmalloc(length
);
1107 pa_assert(sample_length
>= length
);
1108 l
= (sf_count_t
) (length
/pa_frame_size(&sample_spec
));
1110 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
1112 pa_log(_("Premature end of file"));
1117 pa_stream_write(s
, d
, length
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
1119 sample_length
-= length
;
1121 if (sample_length
<= 0) {
1122 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
1123 pa_stream_finish_upload(sample_stream
);
1127 static const char *subscription_event_type_to_string(pa_subscription_event_type_t t
) {
1129 switch (t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) {
1131 case PA_SUBSCRIPTION_EVENT_NEW
:
1134 case PA_SUBSCRIPTION_EVENT_CHANGE
:
1137 case PA_SUBSCRIPTION_EVENT_REMOVE
:
1141 return _("unknown");
1144 static const char *subscription_event_facility_to_string(pa_subscription_event_type_t t
) {
1146 switch (t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) {
1148 case PA_SUBSCRIPTION_EVENT_SINK
:
1151 case PA_SUBSCRIPTION_EVENT_SOURCE
:
1154 case PA_SUBSCRIPTION_EVENT_SINK_INPUT
:
1155 return _("sink-input");
1157 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
:
1158 return _("source-output");
1160 case PA_SUBSCRIPTION_EVENT_MODULE
:
1163 case PA_SUBSCRIPTION_EVENT_CLIENT
:
1166 case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
:
1167 return _("sample-cache");
1169 case PA_SUBSCRIPTION_EVENT_SERVER
:
1172 case PA_SUBSCRIPTION_EVENT_CARD
:
1176 return _("unknown");
1179 static void context_subscribe_callback(pa_context
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
1182 printf(_("Event '%s' on %s #%u\n"),
1183 subscription_event_type_to_string(t
),
1184 subscription_event_facility_to_string(t
),
1189 static void context_state_callback(pa_context
*c
, void *userdata
) {
1190 pa_operation
*o
= NULL
;
1194 switch (pa_context_get_state(c
)) {
1195 case PA_CONTEXT_CONNECTING
:
1196 case PA_CONTEXT_AUTHORIZING
:
1197 case PA_CONTEXT_SETTING_NAME
:
1200 case PA_CONTEXT_READY
:
1203 o
= pa_context_stat(c
, stat_callback
, NULL
);
1204 if (short_list_format
)
1208 pa_operation_unref(o
);
1214 o
= pa_context_get_server_info(c
, get_server_info_callback
, NULL
);
1218 o
= pa_context_play_sample(c
, sample_name
, sink_name
, PA_VOLUME_NORM
, simple_callback
, NULL
);
1222 o
= pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
);
1226 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
1227 pa_assert(sample_stream
);
1229 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
1230 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
1231 pa_stream_connect_upload(sample_stream
, sample_length
);
1236 o
= pa_context_exit_daemon(c
, simple_callback
, NULL
);
1241 if (pa_streq(list_type
, "modules"))
1242 o
= pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
);
1243 else if (pa_streq(list_type
, "sinks"))
1244 o
= pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
);
1245 else if (pa_streq(list_type
, "sources"))
1246 o
= pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
);
1247 else if (pa_streq(list_type
, "sink-inputs"))
1248 o
= pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
);
1249 else if (pa_streq(list_type
, "source-outputs"))
1250 o
= pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
);
1251 else if (pa_streq(list_type
, "clients"))
1252 o
= pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
);
1253 else if (pa_streq(list_type
, "samples"))
1254 o
= pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
);
1255 else if (pa_streq(list_type
, "cards"))
1256 o
= pa_context_get_card_info_list(c
, get_card_info_callback
, NULL
);
1258 pa_assert_not_reached();
1260 o
= pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
);
1262 pa_operation_unref(o
);
1266 o
= pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
);
1268 pa_operation_unref(o
);
1272 o
= pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
);
1274 pa_operation_unref(o
);
1277 o
= pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
);
1279 pa_operation_unref(o
);
1283 o
= pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
);
1285 pa_operation_unref(o
);
1289 o
= pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
);
1291 pa_operation_unref(o
);
1295 o
= pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
);
1297 pa_operation_unref(o
);
1301 o
= pa_context_get_card_info_list(c
, get_card_info_callback
, NULL
);
1303 pa_operation_unref(o
);
1311 case MOVE_SINK_INPUT
:
1312 o
= pa_context_move_sink_input_by_name(c
, sink_input_idx
, sink_name
, simple_callback
, NULL
);
1315 case MOVE_SOURCE_OUTPUT
:
1316 o
= pa_context_move_source_output_by_name(c
, source_output_idx
, source_name
, simple_callback
, NULL
);
1320 o
= pa_context_load_module(c
, module_name
, module_args
, index_callback
, NULL
);
1325 o
= pa_context_get_module_info_list(c
, unload_module_by_name_callback
, NULL
);
1327 o
= pa_context_unload_module(c
, module_index
, simple_callback
, NULL
);
1332 o
= pa_context_suspend_sink_by_name(c
, sink_name
, suspend
, simple_callback
, NULL
);
1334 o
= pa_context_suspend_sink_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
);
1337 case SUSPEND_SOURCE
:
1339 o
= pa_context_suspend_source_by_name(c
, source_name
, suspend
, simple_callback
, NULL
);
1341 o
= pa_context_suspend_source_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
);
1344 case SET_CARD_PROFILE
:
1345 o
= pa_context_set_card_profile_by_name(c
, card_name
, profile_name
, simple_callback
, NULL
);
1349 o
= pa_context_set_sink_port_by_name(c
, sink_name
, port_name
, simple_callback
, NULL
);
1352 case SET_DEFAULT_SINK
:
1353 o
= pa_context_set_default_sink(c
, sink_name
, simple_callback
, NULL
);
1356 case SET_SOURCE_PORT
:
1357 o
= pa_context_set_source_port_by_name(c
, source_name
, port_name
, simple_callback
, NULL
);
1360 case SET_DEFAULT_SOURCE
:
1361 o
= pa_context_set_default_source(c
, source_name
, simple_callback
, NULL
);
1365 if (mute
== TOGGLE_MUTE
)
1366 o
= pa_context_get_sink_info_by_name(c
, sink_name
, sink_toggle_mute_callback
, NULL
);
1368 o
= pa_context_set_sink_mute_by_name(c
, sink_name
, mute
, simple_callback
, NULL
);
1371 case SET_SOURCE_MUTE
:
1372 if (mute
== TOGGLE_MUTE
)
1373 o
= pa_context_get_source_info_by_name(c
, source_name
, source_toggle_mute_callback
, NULL
);
1375 o
= pa_context_set_source_mute_by_name(c
, source_name
, mute
, simple_callback
, NULL
);
1378 case SET_SINK_INPUT_MUTE
:
1379 if (mute
== TOGGLE_MUTE
)
1380 o
= pa_context_get_sink_input_info(c
, sink_input_idx
, sink_input_toggle_mute_callback
, NULL
);
1382 o
= pa_context_set_sink_input_mute(c
, sink_input_idx
, mute
, simple_callback
, NULL
);
1385 case SET_SOURCE_OUTPUT_MUTE
:
1386 if (mute
== TOGGLE_MUTE
)
1387 o
= pa_context_get_source_output_info(c
, source_output_idx
, source_output_toggle_mute_callback
, NULL
);
1389 o
= pa_context_set_source_output_mute(c
, source_output_idx
, mute
, simple_callback
, NULL
);
1392 case SET_SINK_VOLUME
:
1393 o
= pa_context_get_sink_info_by_name(c
, sink_name
, get_sink_volume_callback
, NULL
);
1396 case SET_SOURCE_VOLUME
:
1397 o
= pa_context_get_source_info_by_name(c
, source_name
, get_source_volume_callback
, NULL
);
1400 case SET_SINK_INPUT_VOLUME
:
1401 o
= pa_context_get_sink_input_info(c
, sink_input_idx
, get_sink_input_volume_callback
, NULL
);
1404 case SET_SOURCE_OUTPUT_VOLUME
:
1405 o
= pa_context_get_source_output_info(c
, source_output_idx
, get_source_output_volume_callback
, NULL
);
1408 case SET_SINK_FORMATS
:
1409 set_sink_formats(c
, sink_idx
, formats
);
1412 case SET_PORT_LATENCY_OFFSET
:
1413 o
= pa_context_set_port_latency_offset(c
, card_name
, port_name
, latency_offset
, simple_callback
, NULL
);
1417 pa_context_set_subscribe_callback(c
, context_subscribe_callback
, NULL
);
1419 o
= pa_context_subscribe(c
,
1420 PA_SUBSCRIPTION_MASK_SINK
|
1421 PA_SUBSCRIPTION_MASK_SOURCE
|
1422 PA_SUBSCRIPTION_MASK_SINK_INPUT
|
1423 PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT
|
1424 PA_SUBSCRIPTION_MASK_MODULE
|
1425 PA_SUBSCRIPTION_MASK_CLIENT
|
1426 PA_SUBSCRIPTION_MASK_SAMPLE_CACHE
|
1427 PA_SUBSCRIPTION_MASK_SERVER
|
1428 PA_SUBSCRIPTION_MASK_CARD
,
1434 pa_assert_not_reached();
1438 pa_operation_unref(o
);
1443 pa_log("Operation failed: %s", pa_strerror(pa_context_errno(c
)));
1449 case PA_CONTEXT_TERMINATED
:
1453 case PA_CONTEXT_FAILED
:
1455 pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c
)));
1460 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
1461 pa_log(_("Got SIGINT, exiting."));
1465 static int parse_volume(const char *vol_spec
, pa_volume_t
*vol
, enum volume_flags
*vol_flags
) {
1469 pa_assert(vol_spec
);
1471 pa_assert(vol_flags
);
1473 vs
= pa_xstrdup(vol_spec
);
1475 *vol_flags
= (pa_startswith(vs
, "+") || pa_startswith(vs
, "-")) ? VOL_RELATIVE
: VOL_ABSOLUTE
;
1476 if (strchr(vs
, '.'))
1477 *vol_flags
|= VOL_LINEAR
;
1478 if (pa_endswith(vs
, "%")) {
1479 *vol_flags
|= VOL_PERCENT
;
1480 vs
[strlen(vs
)-1] = 0;
1482 if (pa_endswith(vs
, "db") || pa_endswith(vs
, "dB")) {
1483 *vol_flags
|= VOL_DECIBEL
;
1484 vs
[strlen(vs
)-2] = 0;
1487 if (pa_atod(vs
, &v
) < 0) {
1488 pa_log(_("Invalid volume specification"));
1495 if ((*vol_flags
& VOL_RELATIVE
) == VOL_RELATIVE
) {
1496 if ((*vol_flags
& 0x0F) == VOL_UINT
)
1497 v
+= (double) PA_VOLUME_NORM
;
1498 if ((*vol_flags
& 0x0F) == VOL_PERCENT
)
1500 if ((*vol_flags
& 0x0F) == VOL_LINEAR
)
1503 if ((*vol_flags
& 0x0F) == VOL_PERCENT
)
1504 v
= v
* (double) PA_VOLUME_NORM
/ 100;
1505 if ((*vol_flags
& 0x0F) == VOL_LINEAR
)
1506 v
= pa_sw_volume_from_linear(v
);
1507 if ((*vol_flags
& 0x0F) == VOL_DECIBEL
)
1508 v
= pa_sw_volume_from_dB(v
);
1510 if (!PA_VOLUME_IS_VALID((pa_volume_t
) v
)) {
1511 pa_log(_("Volume outside permissible range.\n"));
1515 *vol
= (pa_volume_t
) v
;
1520 static int parse_volumes(char *args
[], unsigned n
) {
1523 if (n
>= PA_CHANNELS_MAX
) {
1524 pa_log(_("Invalid number of volume specifications.\n"));
1528 volume
.channels
= n
;
1529 for (i
= 0; i
< volume
.channels
; i
++) {
1530 enum volume_flags flags
;
1532 if (parse_volume(args
[i
], &volume
.values
[i
], &flags
) < 0)
1535 if (i
> 0 && flags
!= volume_flags
) {
1536 pa_log(_("Inconsistent volume specification.\n"));
1539 volume_flags
= flags
;
1545 static enum mute_flags
parse_mute(const char *mute_text
) {
1548 pa_assert(mute_text
);
1550 if (pa_streq("toggle", mute_text
))
1553 b
= pa_parse_boolean(mute_text
);
1560 return INVALID_MUTE
;
1564 static void help(const char *argv0
) {
1566 printf("%s %s %s\n", argv0
, _("[options]"), "stat [short]");
1567 printf("%s %s %s\n", argv0
, _("[options]"), "info");
1568 printf("%s %s %s %s\n", argv0
, _("[options]"), "list [short]", _("[TYPE]"));
1569 printf("%s %s %s\n", argv0
, _("[options]"), "exit");
1570 printf("%s %s %s %s\n", argv0
, _("[options]"), "upload-sample", _("FILENAME [NAME]"));
1571 printf("%s %s %s %s\n", argv0
, _("[options]"), "play-sample ", _("NAME [SINK]"));
1572 printf("%s %s %s %s\n", argv0
, _("[options]"), "remove-sample ", _("NAME"));
1573 printf("%s %s %s %s\n", argv0
, _("[options]"), "load-module ", _("NAME [ARGS ...]"));
1574 printf("%s %s %s %s\n", argv0
, _("[options]"), "unload-module ", _("NAME|#N"));
1575 printf("%s %s %s %s\n", argv0
, _("[options]"), "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
1576 printf("%s %s %s %s\n", argv0
, _("[options]"), "suspend-(sink|source)", _("NAME|#N 1|0"));
1577 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-card-profile ", _("CARD PROFILE"));
1578 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-default-(sink|source)", _("NAME"));
1579 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink|source)-port", _("NAME|#N PORT"));
1580 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink|source)-volume", _("NAME|#N VOLUME"));
1581 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink-input|source-output)-volume", _("#N VOLUME"));
1582 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink|source)-mute", _("NAME|#N 1|0|toggle"));
1583 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0|toggle"));
1584 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-sink-formats", _("#N FORMATS"));
1585 printf("%s %s %s %s\n", argv0
, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
1586 printf("%s %s %s\n", argv0
, _("[options]"), "subscribe");
1587 printf(_("\nThe special names @DEFAULT_SINK@, @DEFAULT_SOURCE@ and @DEFAULT_MONITOR@\n"
1588 "can be used to specify the default sink, source and monitor.\n"));
1591 " -h, --help Show this help\n"
1592 " --version Show version\n\n"
1593 " -s, --server=SERVER The name of the server to connect to\n"
1594 " -n, --client-name=NAME How to call this client on the server\n"));
1601 int main(int argc
, char *argv
[]) {
1602 pa_mainloop
*m
= NULL
;
1604 char *server
= NULL
, *bn
;
1606 static const struct option long_options
[] = {
1607 {"server", 1, NULL
, 's'},
1608 {"client-name", 1, NULL
, 'n'},
1609 {"version", 0, NULL
, ARG_VERSION
},
1610 {"help", 0, NULL
, 'h'},
1614 setlocale(LC_ALL
, "");
1616 bindtextdomain(GETTEXT_PACKAGE
, PULSE_LOCALEDIR
);
1619 bn
= pa_path_get_filename(argv
[0]);
1621 proplist
= pa_proplist_new();
1623 while ((c
= getopt_long(argc
, argv
, "+s:n:h", long_options
, NULL
)) != -1) {
1631 printf(_("pactl %s\n"
1632 "Compiled with libpulse %s\n"
1633 "Linked with libpulse %s\n"),
1635 pa_get_headers_version(),
1636 pa_get_library_version());
1642 server
= pa_xstrdup(optarg
);
1648 if (!(t
= pa_locale_to_utf8(optarg
)) ||
1649 pa_proplist_sets(proplist
, PA_PROP_APPLICATION_NAME
, t
) < 0) {
1651 pa_log(_("Invalid client name '%s'"), t
? t
: optarg
);
1665 if (optind
< argc
) {
1666 if (pa_streq(argv
[optind
], "stat")) {
1668 short_list_format
= false;
1669 if (optind
+1 < argc
&& pa_streq(argv
[optind
+1], "short"))
1670 short_list_format
= true;
1672 } else if (pa_streq(argv
[optind
], "info"))
1675 else if (pa_streq(argv
[optind
], "exit"))
1678 else if (pa_streq(argv
[optind
], "list")) {
1681 for (int i
= optind
+1; i
< argc
; i
++) {
1682 if (pa_streq(argv
[i
], "modules") || pa_streq(argv
[i
], "clients") ||
1683 pa_streq(argv
[i
], "sinks") || pa_streq(argv
[i
], "sink-inputs") ||
1684 pa_streq(argv
[i
], "sources") || pa_streq(argv
[i
], "source-outputs") ||
1685 pa_streq(argv
[i
], "samples") || pa_streq(argv
[i
], "cards")) {
1686 list_type
= pa_xstrdup(argv
[i
]);
1687 } else if (pa_streq(argv
[i
], "short")) {
1688 short_list_format
= true;
1690 pa_log(_("Specify nothing, or one of: %s"), "modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards");
1695 } else if (pa_streq(argv
[optind
], "upload-sample")) {
1697 action
= UPLOAD_SAMPLE
;
1699 if (optind
+1 >= argc
) {
1700 pa_log(_("Please specify a sample file to load"));
1704 if (optind
+2 < argc
)
1705 sample_name
= pa_xstrdup(argv
[optind
+2]);
1707 char *f
= pa_path_get_filename(argv
[optind
+1]);
1708 sample_name
= pa_xstrndup(f
, strcspn(f
, "."));
1712 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfi
))) {
1713 pa_log(_("Failed to open sound file."));
1717 if (pa_sndfile_read_sample_spec(sndfile
, &sample_spec
) < 0) {
1718 pa_log(_("Failed to determine sample specification from file."));
1721 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
1723 if (pa_sndfile_read_channel_map(sndfile
, &channel_map
) < 0) {
1724 if (sample_spec
.channels
> 2)
1725 pa_log(_("Warning: Failed to determine sample specification from file."));
1726 pa_channel_map_init_extend(&channel_map
, sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1729 pa_assert(pa_channel_map_compatible(&channel_map
, &sample_spec
));
1730 sample_length
= (size_t) sfi
.frames
*pa_frame_size(&sample_spec
);
1732 } else if (pa_streq(argv
[optind
], "play-sample")) {
1733 action
= PLAY_SAMPLE
;
1734 if (argc
!= optind
+2 && argc
!= optind
+3) {
1735 pa_log(_("You have to specify a sample name to play"));
1739 sample_name
= pa_xstrdup(argv
[optind
+1]);
1741 if (optind
+2 < argc
)
1742 sink_name
= pa_xstrdup(argv
[optind
+2]);
1744 } else if (pa_streq(argv
[optind
], "remove-sample")) {
1745 action
= REMOVE_SAMPLE
;
1746 if (argc
!= optind
+2) {
1747 pa_log(_("You have to specify a sample name to remove"));
1751 sample_name
= pa_xstrdup(argv
[optind
+1]);
1753 } else if (pa_streq(argv
[optind
], "move-sink-input")) {
1754 action
= MOVE_SINK_INPUT
;
1755 if (argc
!= optind
+3) {
1756 pa_log(_("You have to specify a sink input index and a sink"));
1760 sink_input_idx
= (uint32_t) atoi(argv
[optind
+1]);
1761 sink_name
= pa_xstrdup(argv
[optind
+2]);
1763 } else if (pa_streq(argv
[optind
], "move-source-output")) {
1764 action
= MOVE_SOURCE_OUTPUT
;
1765 if (argc
!= optind
+3) {
1766 pa_log(_("You have to specify a source output index and a source"));
1770 source_output_idx
= (uint32_t) atoi(argv
[optind
+1]);
1771 source_name
= pa_xstrdup(argv
[optind
+2]);
1773 } else if (pa_streq(argv
[optind
], "load-module")) {
1778 action
= LOAD_MODULE
;
1780 if (argc
<= optind
+1) {
1781 pa_log(_("You have to specify a module name and arguments."));
1785 module_name
= argv
[optind
+1];
1787 for (i
= optind
+2; i
< argc
; i
++)
1788 n
+= strlen(argv
[i
])+1;
1791 p
= module_args
= pa_xmalloc(n
);
1793 for (i
= optind
+2; i
< argc
; i
++)
1794 p
+= sprintf(p
, "%s%s", p
== module_args
? "" : " ", argv
[i
]);
1797 } else if (pa_streq(argv
[optind
], "unload-module")) {
1798 action
= UNLOAD_MODULE
;
1800 if (argc
!= optind
+2) {
1801 pa_log(_("You have to specify a module index or name"));
1805 if (pa_atou(argv
[optind
+ 1], &module_index
) < 0)
1806 module_name
= argv
[optind
+ 1];
1808 } else if (pa_streq(argv
[optind
], "suspend-sink")) {
1811 action
= SUSPEND_SINK
;
1813 if (argc
> optind
+3 || optind
+1 >= argc
) {
1814 pa_log(_("You may not specify more than one sink. You have to specify a boolean value."));
1818 if ((b
= pa_parse_boolean(argv
[argc
-1])) < 0) {
1819 pa_log(_("Invalid suspend specification."));
1825 if (argc
> optind
+2)
1826 sink_name
= pa_xstrdup(argv
[optind
+1]);
1828 } else if (pa_streq(argv
[optind
], "suspend-source")) {
1831 action
= SUSPEND_SOURCE
;
1833 if (argc
> optind
+3 || optind
+1 >= argc
) {
1834 pa_log(_("You may not specify more than one source. You have to specify a boolean value."));
1838 if ((b
= pa_parse_boolean(argv
[argc
-1])) < 0) {
1839 pa_log(_("Invalid suspend specification."));
1845 if (argc
> optind
+2)
1846 source_name
= pa_xstrdup(argv
[optind
+1]);
1847 } else if (pa_streq(argv
[optind
], "set-card-profile")) {
1848 action
= SET_CARD_PROFILE
;
1850 if (argc
!= optind
+3) {
1851 pa_log(_("You have to specify a card name/index and a profile name"));
1855 card_name
= pa_xstrdup(argv
[optind
+1]);
1856 profile_name
= pa_xstrdup(argv
[optind
+2]);
1858 } else if (pa_streq(argv
[optind
], "set-sink-port")) {
1859 action
= SET_SINK_PORT
;
1861 if (argc
!= optind
+3) {
1862 pa_log(_("You have to specify a sink name/index and a port name"));
1866 sink_name
= pa_xstrdup(argv
[optind
+1]);
1867 port_name
= pa_xstrdup(argv
[optind
+2]);
1869 } else if (pa_streq(argv
[optind
], "set-default-sink")) {
1870 action
= SET_DEFAULT_SINK
;
1872 if (argc
!= optind
+2) {
1873 pa_log(_("You have to specify a sink name"));
1877 sink_name
= pa_xstrdup(argv
[optind
+1]);
1879 } else if (pa_streq(argv
[optind
], "set-source-port")) {
1880 action
= SET_SOURCE_PORT
;
1882 if (argc
!= optind
+3) {
1883 pa_log(_("You have to specify a source name/index and a port name"));
1887 source_name
= pa_xstrdup(argv
[optind
+1]);
1888 port_name
= pa_xstrdup(argv
[optind
+2]);
1890 } else if (pa_streq(argv
[optind
], "set-default-source")) {
1891 action
= SET_DEFAULT_SOURCE
;
1893 if (argc
!= optind
+2) {
1894 pa_log(_("You have to specify a source name"));
1898 source_name
= pa_xstrdup(argv
[optind
+1]);
1900 } else if (pa_streq(argv
[optind
], "set-sink-volume")) {
1901 action
= SET_SINK_VOLUME
;
1903 if (argc
< optind
+3) {
1904 pa_log(_("You have to specify a sink name/index and a volume"));
1908 sink_name
= pa_xstrdup(argv
[optind
+1]);
1910 if (parse_volumes(argv
+optind
+2, argc
-3) < 0)
1913 } else if (pa_streq(argv
[optind
], "set-source-volume")) {
1914 action
= SET_SOURCE_VOLUME
;
1916 if (argc
< optind
+3) {
1917 pa_log(_("You have to specify a source name/index and a volume"));
1921 source_name
= pa_xstrdup(argv
[optind
+1]);
1923 if (parse_volumes(argv
+optind
+2, argc
-3) < 0)
1926 } else if (pa_streq(argv
[optind
], "set-sink-input-volume")) {
1927 action
= SET_SINK_INPUT_VOLUME
;
1929 if (argc
< optind
+3) {
1930 pa_log(_("You have to specify a sink input index and a volume"));
1934 if (pa_atou(argv
[optind
+1], &sink_input_idx
) < 0) {
1935 pa_log(_("Invalid sink input index"));
1939 if (parse_volumes(argv
+optind
+2, argc
-3) < 0)
1942 } else if (pa_streq(argv
[optind
], "set-source-output-volume")) {
1943 action
= SET_SOURCE_OUTPUT_VOLUME
;
1945 if (argc
< optind
+3) {
1946 pa_log(_("You have to specify a source output index and a volume"));
1950 if (pa_atou(argv
[optind
+1], &source_output_idx
) < 0) {
1951 pa_log(_("Invalid source output index"));
1955 if (parse_volumes(argv
+optind
+2, argc
-3) < 0)
1958 } else if (pa_streq(argv
[optind
], "set-sink-mute")) {
1959 action
= SET_SINK_MUTE
;
1961 if (argc
!= optind
+3) {
1962 pa_log(_("You have to specify a sink name/index and a mute boolean"));
1966 if ((mute
= parse_mute(argv
[optind
+2])) == INVALID_MUTE
) {
1967 pa_log(_("Invalid mute specification"));
1971 sink_name
= pa_xstrdup(argv
[optind
+1]);
1973 } else if (pa_streq(argv
[optind
], "set-source-mute")) {
1974 action
= SET_SOURCE_MUTE
;
1976 if (argc
!= optind
+3) {
1977 pa_log(_("You have to specify a source name/index and a mute boolean"));
1981 if ((mute
= parse_mute(argv
[optind
+2])) == INVALID_MUTE
) {
1982 pa_log(_("Invalid mute specification"));
1986 source_name
= pa_xstrdup(argv
[optind
+1]);
1988 } else if (pa_streq(argv
[optind
], "set-sink-input-mute")) {
1989 action
= SET_SINK_INPUT_MUTE
;
1991 if (argc
!= optind
+3) {
1992 pa_log(_("You have to specify a sink input index and a mute boolean"));
1996 if (pa_atou(argv
[optind
+1], &sink_input_idx
) < 0) {
1997 pa_log(_("Invalid sink input index specification"));
2001 if ((mute
= parse_mute(argv
[optind
+2])) == INVALID_MUTE
) {
2002 pa_log(_("Invalid mute specification"));
2006 } else if (pa_streq(argv
[optind
], "set-source-output-mute")) {
2007 action
= SET_SOURCE_OUTPUT_MUTE
;
2009 if (argc
!= optind
+3) {
2010 pa_log(_("You have to specify a source output index and a mute boolean"));
2014 if (pa_atou(argv
[optind
+1], &source_output_idx
) < 0) {
2015 pa_log(_("Invalid source output index specification"));
2019 if ((mute
= parse_mute(argv
[optind
+2])) == INVALID_MUTE
) {
2020 pa_log(_("Invalid mute specification"));
2024 } else if (pa_streq(argv
[optind
], "subscribe"))
2028 else if (pa_streq(argv
[optind
], "set-sink-formats")) {
2031 if (argc
!= optind
+3 || pa_atoi(argv
[optind
+1], &tmp
) < 0) {
2032 pa_log(_("You have to specify a sink index and a semicolon-separated list of supported formats"));
2037 action
= SET_SINK_FORMATS
;
2038 formats
= pa_xstrdup(argv
[optind
+2]);
2040 } else if (pa_streq(argv
[optind
], "set-port-latency-offset")) {
2041 action
= SET_PORT_LATENCY_OFFSET
;
2043 if (argc
!= optind
+4) {
2044 pa_log(_("You have to specify a card name/index, a port name and a latency offset"));
2048 card_name
= pa_xstrdup(argv
[optind
+1]);
2049 port_name
= pa_xstrdup(argv
[optind
+2]);
2050 if (pa_atoi(argv
[optind
+ 3], &latency_offset
) < 0) {
2051 pa_log(_("Could not parse latency offset"));
2055 } else if (pa_streq(argv
[optind
], "help")) {
2062 if (action
== NONE
) {
2063 pa_log(_("No valid command specified."));
2067 if (!(m
= pa_mainloop_new())) {
2068 pa_log(_("pa_mainloop_new() failed."));
2072 mainloop_api
= pa_mainloop_get_api(m
);
2074 pa_assert_se(pa_signal_init(mainloop_api
) == 0);
2075 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
2076 pa_signal_new(SIGTERM
, exit_signal_callback
, NULL
);
2077 pa_disable_sigpipe();
2079 if (!(context
= pa_context_new_with_proplist(mainloop_api
, NULL
, proplist
))) {
2080 pa_log(_("pa_context_new() failed."));
2084 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
2085 if (pa_context_connect(context
, server
, 0, NULL
) < 0) {
2086 pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context
)));
2090 if (pa_mainloop_run(m
, &ret
) < 0) {
2091 pa_log(_("pa_mainloop_run() failed."));
2097 pa_stream_unref(sample_stream
);
2100 pa_context_unref(context
);
2104 pa_mainloop_free(m
);
2108 pa_xfree(list_type
);
2109 pa_xfree(sample_name
);
2110 pa_xfree(sink_name
);
2111 pa_xfree(source_name
);
2112 pa_xfree(module_args
);
2113 pa_xfree(card_name
);
2114 pa_xfree(profile_name
);
2115 pa_xfree(port_name
);
2122 pa_proplist_free(proplist
);