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/i18n.h>
40 #include <pulse/pulseaudio.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/sndfile-util.h>
47 #define BUFSIZE (16*1024)
49 static pa_context
*context
= NULL
;
50 static pa_mainloop_api
*mainloop_api
= NULL
;
64 sink_input_idx
= PA_INVALID_INDEX
,
65 source_output_idx
= PA_INVALID_INDEX
;
67 static uint32_t module_index
;
68 static pa_bool_t suspend
;
70 static pa_proplist
*proplist
= NULL
;
72 static SNDFILE
*sndfile
= NULL
;
73 static pa_stream
*sample_stream
= NULL
;
74 static pa_sample_spec sample_spec
;
75 static pa_channel_map channel_map
;
76 static size_t sample_length
= 0;
78 static int actions
= 1;
80 static pa_bool_t nl
= FALSE
;
101 static void quit(int ret
) {
102 pa_assert(mainloop_api
);
103 mainloop_api
->quit(mainloop_api
, ret
);
106 static void context_drain_complete(pa_context
*c
, void *userdata
) {
107 pa_context_disconnect(c
);
110 static void drain(void) {
112 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
113 pa_context_disconnect(context
);
115 pa_operation_unref(o
);
118 static void complete_action(void) {
119 pa_assert(actions
> 0);
125 static void stat_callback(pa_context
*c
, const pa_stat_info
*i
, void *userdata
) {
128 pa_log(_("Failed to get statistics: %s\n"), pa_strerror(pa_context_errno(c
)));
133 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_total_size
);
134 printf(_("Currently in use: %u blocks containing %s bytes total.\n"), i
->memblock_total
, s
);
136 pa_bytes_snprint(s
, sizeof(s
), i
->memblock_allocated_size
);
137 printf(_("Allocated during whole lifetime: %u blocks containing %s bytes total.\n"), i
->memblock_allocated
, s
);
139 pa_bytes_snprint(s
, sizeof(s
), i
->scache_size
);
140 printf(_("Sample cache size: %s\n"), s
);
145 static void get_server_info_callback(pa_context
*c
, const pa_server_info
*i
, void *useerdata
) {
146 char ss
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
149 pa_log(_("Failed to get server information: %s\n"), pa_strerror(pa_context_errno(c
)));
154 pa_sample_spec_snprint(ss
, sizeof(ss
), &i
->sample_spec
);
155 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
);
157 printf(_("User name: %s\n"
160 "Server Version: %s\n"
161 "Default Sample Specification: %s\n"
162 "Default Channel Map: %s\n"
164 "Default Source: %s\n"
172 i
->default_sink_name
,
173 i
->default_source_name
,
179 static void get_sink_info_callback(pa_context
*c
, const pa_sink_info
*i
, int is_last
, void *userdata
) {
181 static const char *state_table
[] = {
182 [1+PA_SINK_INVALID_STATE
] = "n/a",
183 [1+PA_SINK_RUNNING
] = "RUNNING",
184 [1+PA_SINK_IDLE
] = "IDLE",
185 [1+PA_SINK_SUSPENDED
] = "SUSPENDED"
189 s
[PA_SAMPLE_SPEC_SNPRINT_MAX
],
190 cv
[PA_CVOLUME_SNPRINT_MAX
],
191 cvdb
[PA_SW_CVOLUME_SNPRINT_DB_MAX
],
192 v
[PA_VOLUME_SNPRINT_MAX
],
193 vdb
[PA_SW_VOLUME_SNPRINT_DB_MAX
],
194 cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
198 pa_log(_("Failed to get sink information: %s\n"), pa_strerror(pa_context_errno(c
)));
214 printf(_("Sink #%u\n"
217 "\tDescription: %s\n"
219 "\tSample Specification: %s\n"
220 "\tChannel Map: %s\n"
221 "\tOwner Module: %u\n"
225 "\tBase Volume: %s%s%s\n"
226 "\tMonitor Source: %s\n"
227 "\tLatency: %0.0f usec, configured %0.0f usec\n"
228 "\tFlags: %s%s%s%s%s%s\n"
229 "\tProperties:\n\t\t%s\n"),
231 state_table
[1+i
->state
],
233 pa_strnull(i
->description
),
234 pa_strnull(i
->driver
),
235 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
236 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
239 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
240 i
->flags
& PA_SINK_DECIBEL_VOLUME
? "\n\t " : "",
241 i
->flags
& PA_SINK_DECIBEL_VOLUME
? pa_sw_cvolume_snprint_dB(cvdb
, sizeof(cvdb
), &i
->volume
) : "",
242 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
243 pa_volume_snprint(v
, sizeof(v
), i
->base_volume
),
244 i
->flags
& PA_SINK_DECIBEL_VOLUME
? "\n\t " : "",
245 i
->flags
& PA_SINK_DECIBEL_VOLUME
? pa_sw_volume_snprint_dB(vdb
, sizeof(vdb
), i
->base_volume
) : "",
246 pa_strnull(i
->monitor_source_name
),
247 (double) i
->latency
, (double) i
->configured_latency
,
248 i
->flags
& PA_SINK_HARDWARE
? "HARDWARE " : "",
249 i
->flags
& PA_SINK_NETWORK
? "NETWORK " : "",
250 i
->flags
& PA_SINK_HW_MUTE_CTRL
? "HW_MUTE_CTRL " : "",
251 i
->flags
& PA_SINK_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
252 i
->flags
& PA_SINK_DECIBEL_VOLUME
? "DECIBEL_VOLUME " : "",
253 i
->flags
& PA_SINK_LATENCY
? "LATENCY " : "",
254 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
259 pa_sink_port_info
**p
;
261 printf(_("\tPorts:\n"));
262 for (p
= i
->ports
; *p
; p
++)
263 printf("\t\t%s: %s (priority. %u)\n", (*p
)->name
, (*p
)->description
, (*p
)->priority
);
267 printf(_("\tActive Port: %s\n"),
268 i
->active_port
->name
);
271 static void get_source_info_callback(pa_context
*c
, const pa_source_info
*i
, int is_last
, void *userdata
) {
273 static const char *state_table
[] = {
274 [1+PA_SOURCE_INVALID_STATE
] = "n/a",
275 [1+PA_SOURCE_RUNNING
] = "RUNNING",
276 [1+PA_SOURCE_IDLE
] = "IDLE",
277 [1+PA_SOURCE_SUSPENDED
] = "SUSPENDED"
281 s
[PA_SAMPLE_SPEC_SNPRINT_MAX
],
282 cv
[PA_CVOLUME_SNPRINT_MAX
],
283 cvdb
[PA_SW_CVOLUME_SNPRINT_DB_MAX
],
284 v
[PA_VOLUME_SNPRINT_MAX
],
285 vdb
[PA_SW_VOLUME_SNPRINT_DB_MAX
],
286 cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
290 pa_log(_("Failed to get source information: %s\n"), pa_strerror(pa_context_errno(c
)));
306 printf(_("Source #%u\n"
309 "\tDescription: %s\n"
311 "\tSample Specification: %s\n"
312 "\tChannel Map: %s\n"
313 "\tOwner Module: %u\n"
317 "\tBase Volume: %s%s%s\n"
318 "\tMonitor of Sink: %s\n"
319 "\tLatency: %0.0f usec, configured %0.0f usec\n"
320 "\tFlags: %s%s%s%s%s%s\n"
321 "\tProperties:\n\t\t%s\n"),
323 state_table
[1+i
->state
],
325 pa_strnull(i
->description
),
326 pa_strnull(i
->driver
),
327 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
328 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
331 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
332 i
->flags
& PA_SOURCE_DECIBEL_VOLUME
? "\n\t " : "",
333 i
->flags
& PA_SOURCE_DECIBEL_VOLUME
? pa_sw_cvolume_snprint_dB(cvdb
, sizeof(cvdb
), &i
->volume
) : "",
334 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
335 pa_volume_snprint(v
, sizeof(v
), i
->base_volume
),
336 i
->flags
& PA_SOURCE_DECIBEL_VOLUME
? "\n\t " : "",
337 i
->flags
& PA_SOURCE_DECIBEL_VOLUME
? pa_sw_volume_snprint_dB(vdb
, sizeof(vdb
), i
->base_volume
) : "",
338 i
->monitor_of_sink_name
? i
->monitor_of_sink_name
: _("n/a"),
339 (double) i
->latency
, (double) i
->configured_latency
,
340 i
->flags
& PA_SOURCE_HARDWARE
? "HARDWARE " : "",
341 i
->flags
& PA_SOURCE_NETWORK
? "NETWORK " : "",
342 i
->flags
& PA_SOURCE_HW_MUTE_CTRL
? "HW_MUTE_CTRL " : "",
343 i
->flags
& PA_SOURCE_HW_VOLUME_CTRL
? "HW_VOLUME_CTRL " : "",
344 i
->flags
& PA_SOURCE_DECIBEL_VOLUME
? "DECIBEL_VOLUME " : "",
345 i
->flags
& PA_SOURCE_LATENCY
? "LATENCY " : "",
346 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
351 pa_source_port_info
**p
;
353 printf(_("\tPorts:\n"));
354 for (p
= i
->ports
; *p
; p
++)
355 printf("\t\t%s: %s (priority. %u)\n", (*p
)->name
, (*p
)->description
, (*p
)->priority
);
359 printf(_("\tActive Port: %s\n"),
360 i
->active_port
->name
);
363 static void get_module_info_callback(pa_context
*c
, const pa_module_info
*i
, int is_last
, void *userdata
) {
368 pa_log(_("Failed to get module information: %s\n"), pa_strerror(pa_context_errno(c
)));
384 pa_snprintf(t
, sizeof(t
), "%u", i
->n_used
);
386 printf(_("Module #%u\n"
389 "\tUsage counter: %s\n"
390 "\tProperties:\n\t\t%s\n"),
393 i
->argument
? i
->argument
: "",
394 i
->n_used
!= PA_INVALID_INDEX
? t
: _("n/a"),
395 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
400 static void get_client_info_callback(pa_context
*c
, const pa_client_info
*i
, int is_last
, void *userdata
) {
405 pa_log(_("Failed to get client information: %s\n"), pa_strerror(pa_context_errno(c
)));
421 pa_snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
423 printf(_("Client #%u\n"
425 "\tOwner Module: %s\n"
426 "\tProperties:\n\t\t%s\n"),
428 pa_strnull(i
->driver
),
429 i
->owner_module
!= PA_INVALID_INDEX
? t
: _("n/a"),
430 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
435 static void get_card_info_callback(pa_context
*c
, const pa_card_info
*i
, int is_last
, void *userdata
) {
440 pa_log(_("Failed to get card information: %s\n"), pa_strerror(pa_context_errno(c
)));
456 pa_snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
458 printf(_("Card #%u\n"
461 "\tOwner Module: %s\n"
462 "\tProperties:\n\t\t%s\n"),
465 pa_strnull(i
->driver
),
466 i
->owner_module
!= PA_INVALID_INDEX
? t
: _("n/a"),
467 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
470 pa_card_profile_info
*p
;
472 printf(_("\tProfiles:\n"));
473 for (p
= i
->profiles
; p
->name
; p
++)
474 printf("\t\t%s: %s (sinks: %u, sources: %u, priority. %u)\n", p
->name
, p
->description
, p
->n_sinks
, p
->n_sources
, p
->priority
);
477 if (i
->active_profile
)
478 printf(_("\tActive Profile: %s\n"),
479 i
->active_profile
->name
);
484 static void get_sink_input_info_callback(pa_context
*c
, const pa_sink_input_info
*i
, int is_last
, void *userdata
) {
485 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cvdb
[PA_SW_CVOLUME_SNPRINT_DB_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
489 pa_log(_("Failed to get sink input information: %s\n"), pa_strerror(pa_context_errno(c
)));
505 pa_snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
506 pa_snprintf(k
, sizeof(k
), "%u", i
->client
);
508 printf(_("Sink Input #%u\n"
510 "\tOwner Module: %s\n"
513 "\tSample Specification: %s\n"
514 "\tChannel Map: %s\n"
519 "\tBuffer Latency: %0.0f usec\n"
520 "\tSink Latency: %0.0f usec\n"
521 "\tResample method: %s\n"
522 "\tProperties:\n\t\t%s\n"),
524 pa_strnull(i
->driver
),
525 i
->owner_module
!= PA_INVALID_INDEX
? t
: _("n/a"),
526 i
->client
!= PA_INVALID_INDEX
? k
: _("n/a"),
528 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
529 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
531 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
532 pa_sw_cvolume_snprint_dB(cvdb
, sizeof(cvdb
), &i
->volume
),
533 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
534 (double) i
->buffer_usec
,
535 (double) i
->sink_usec
,
536 i
->resample_method
? i
->resample_method
: _("n/a"),
537 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
542 static void get_source_output_info_callback(pa_context
*c
, const pa_source_output_info
*i
, int is_last
, void *userdata
) {
543 char t
[32], k
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
547 pa_log(_("Failed to get source output information: %s\n"), pa_strerror(pa_context_errno(c
)));
564 pa_snprintf(t
, sizeof(t
), "%u", i
->owner_module
);
565 pa_snprintf(k
, sizeof(k
), "%u", i
->client
);
567 printf(_("Source Output #%u\n"
569 "\tOwner Module: %s\n"
572 "\tSample Specification: %s\n"
573 "\tChannel Map: %s\n"
574 "\tBuffer Latency: %0.0f usec\n"
575 "\tSource Latency: %0.0f usec\n"
576 "\tResample method: %s\n"
577 "\tProperties:\n\t\t%s\n"),
579 pa_strnull(i
->driver
),
580 i
->owner_module
!= PA_INVALID_INDEX
? t
: _("n/a"),
581 i
->client
!= PA_INVALID_INDEX
? k
: _("n/a"),
583 pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
),
584 pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
),
585 (double) i
->buffer_usec
,
586 (double) i
->source_usec
,
587 i
->resample_method
? i
->resample_method
: _("n/a"),
588 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
593 static void get_sample_info_callback(pa_context
*c
, const pa_sample_info
*i
, int is_last
, void *userdata
) {
594 char t
[32], s
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cv
[PA_CVOLUME_SNPRINT_MAX
], cvdb
[PA_SW_CVOLUME_SNPRINT_DB_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
598 pa_log(_("Failed to get sample information: %s\n"), pa_strerror(pa_context_errno(c
)));
614 pa_bytes_snprint(t
, sizeof(t
), i
->bytes
);
616 printf(_("Sample #%u\n"
618 "\tSample Specification: %s\n"
619 "\tChannel Map: %s\n"
623 "\tDuration: %0.1fs\n"
627 "\tProperties:\n\t\t%s\n"),
630 pa_sample_spec_valid(&i
->sample_spec
) ? pa_sample_spec_snprint(s
, sizeof(s
), &i
->sample_spec
) : _("n/a"),
631 pa_sample_spec_valid(&i
->sample_spec
) ? pa_channel_map_snprint(cm
, sizeof(cm
), &i
->channel_map
) : _("n/a"),
632 pa_cvolume_snprint(cv
, sizeof(cv
), &i
->volume
),
633 pa_sw_cvolume_snprint_dB(cvdb
, sizeof(cvdb
), &i
->volume
),
634 pa_cvolume_get_balance(&i
->volume
, &i
->channel_map
),
635 (double) i
->duration
/1000000.0,
638 i
->filename
? i
->filename
: _("n/a"),
639 pl
= pa_proplist_to_string_sep(i
->proplist
, "\n\t\t"));
644 static void simple_callback(pa_context
*c
, int success
, void *userdata
) {
646 pa_log(_("Failure: %s\n"), pa_strerror(pa_context_errno(c
)));
654 static void index_callback(pa_context
*c
, uint32_t idx
, void *userdata
) {
655 if (idx
== PA_INVALID_INDEX
) {
656 pa_log(_("Failure: %s\n"), pa_strerror(pa_context_errno(c
)));
666 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
669 switch (pa_stream_get_state(s
)) {
670 case PA_STREAM_CREATING
:
671 case PA_STREAM_READY
:
674 case PA_STREAM_TERMINATED
:
678 case PA_STREAM_FAILED
:
680 pa_log(_("Failed to upload sample: %s\n"), pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
685 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
688 pa_assert(s
&& length
&& sndfile
);
690 d
= pa_xmalloc(length
);
692 pa_assert(sample_length
>= length
);
693 l
= (sf_count_t
) (length
/pa_frame_size(&sample_spec
));
695 if ((sf_readf_float(sndfile
, d
, l
)) != l
) {
697 pa_log(_("Premature end of file\n"));
702 pa_stream_write(s
, d
, length
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
704 sample_length
-= length
;
706 if (sample_length
<= 0) {
707 pa_stream_set_write_callback(sample_stream
, NULL
, NULL
);
708 pa_stream_finish_upload(sample_stream
);
712 static void context_state_callback(pa_context
*c
, void *userdata
) {
714 switch (pa_context_get_state(c
)) {
715 case PA_CONTEXT_CONNECTING
:
716 case PA_CONTEXT_AUTHORIZING
:
717 case PA_CONTEXT_SETTING_NAME
:
720 case PA_CONTEXT_READY
:
724 pa_operation_unref(pa_context_stat(c
, stat_callback
, NULL
));
725 pa_operation_unref(pa_context_get_server_info(c
, get_server_info_callback
, NULL
));
729 pa_operation_unref(pa_context_play_sample(c
, sample_name
, device
, PA_VOLUME_NORM
, simple_callback
, NULL
));
733 pa_operation_unref(pa_context_remove_sample(c
, sample_name
, simple_callback
, NULL
));
737 sample_stream
= pa_stream_new(c
, sample_name
, &sample_spec
, NULL
);
738 pa_assert(sample_stream
);
740 pa_stream_set_state_callback(sample_stream
, stream_state_callback
, NULL
);
741 pa_stream_set_write_callback(sample_stream
, stream_write_callback
, NULL
);
742 pa_stream_connect_upload(sample_stream
, sample_length
);
746 pa_operation_unref(pa_context_exit_daemon(c
, simple_callback
, NULL
));
751 pa_operation_unref(pa_context_get_module_info_list(c
, get_module_info_callback
, NULL
));
752 pa_operation_unref(pa_context_get_sink_info_list(c
, get_sink_info_callback
, NULL
));
753 pa_operation_unref(pa_context_get_source_info_list(c
, get_source_info_callback
, NULL
));
754 pa_operation_unref(pa_context_get_sink_input_info_list(c
, get_sink_input_info_callback
, NULL
));
755 pa_operation_unref(pa_context_get_source_output_info_list(c
, get_source_output_info_callback
, NULL
));
756 pa_operation_unref(pa_context_get_client_info_list(c
, get_client_info_callback
, NULL
));
757 pa_operation_unref(pa_context_get_sample_info_list(c
, get_sample_info_callback
, NULL
));
758 pa_operation_unref(pa_context_get_card_info_list(c
, get_card_info_callback
, NULL
));
761 case MOVE_SINK_INPUT
:
762 pa_operation_unref(pa_context_move_sink_input_by_name(c
, sink_input_idx
, sink_name
, simple_callback
, NULL
));
765 case MOVE_SOURCE_OUTPUT
:
766 pa_operation_unref(pa_context_move_source_output_by_name(c
, source_output_idx
, source_name
, simple_callback
, NULL
));
770 pa_operation_unref(pa_context_load_module(c
, module_name
, module_args
, index_callback
, NULL
));
774 pa_operation_unref(pa_context_unload_module(c
, module_index
, simple_callback
, NULL
));
779 pa_operation_unref(pa_context_suspend_sink_by_name(c
, sink_name
, suspend
, simple_callback
, NULL
));
781 pa_operation_unref(pa_context_suspend_sink_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
));
786 pa_operation_unref(pa_context_suspend_source_by_name(c
, source_name
, suspend
, simple_callback
, NULL
));
788 pa_operation_unref(pa_context_suspend_source_by_index(c
, PA_INVALID_INDEX
, suspend
, simple_callback
, NULL
));
791 case SET_CARD_PROFILE
:
792 pa_operation_unref(pa_context_set_card_profile_by_name(c
, card_name
, profile_name
, simple_callback
, NULL
));
796 pa_operation_unref(pa_context_set_sink_port_by_name(c
, sink_name
, port_name
, simple_callback
, NULL
));
799 case SET_SOURCE_PORT
:
800 pa_operation_unref(pa_context_set_source_port_by_name(c
, source_name
, port_name
, simple_callback
, NULL
));
804 pa_assert_not_reached();
808 case PA_CONTEXT_TERMINATED
:
812 case PA_CONTEXT_FAILED
:
814 pa_log(_("Connection failure: %s\n"), pa_strerror(pa_context_errno(c
)));
819 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
820 pa_log(_("Got SIGINT, exiting.\n"));
824 static void help(const char *argv0
) {
826 printf(_("%s [options] stat\n"
827 "%s [options] list\n"
828 "%s [options] exit\n"
829 "%s [options] upload-sample FILENAME [NAME]\n"
830 "%s [options] play-sample NAME [SINK]\n"
831 "%s [options] remove-sample NAME\n"
832 "%s [options] move-sink-input ID SINK\n"
833 "%s [options] move-source-output ID SOURCE\n"
834 "%s [options] load-module NAME [ARGS ...]\n"
835 "%s [options] unload-module ID\n"
836 "%s [options] suspend-sink [SINK] 1|0\n"
837 "%s [options] suspend-source [SOURCE] 1|0\n"
838 "%s [options] set-card-profile [CARD] [PROFILE] \n"
839 "%s [options] set-sink-port [SINK] [PORT] \n"
840 "%s [options] set-source-port [SOURCE] [PORT] \n\n"
841 " -h, --help Show this help\n"
842 " --version Show version\n\n"
843 " -s, --server=SERVER The name of the server to connect to\n"
844 " -n, --client-name=NAME How to call this client on the server\n"),
845 argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
, argv0
);
852 int main(int argc
, char *argv
[]) {
853 pa_mainloop
* m
= NULL
;
855 char *server
= NULL
, *bn
;
857 static const struct option long_options
[] = {
858 {"server", 1, NULL
, 's'},
859 {"client-name", 1, NULL
, 'n'},
860 {"version", 0, NULL
, ARG_VERSION
},
861 {"help", 0, NULL
, 'h'},
865 setlocale(LC_ALL
, "");
866 bindtextdomain(GETTEXT_PACKAGE
, PULSE_LOCALEDIR
);
868 bn
= pa_path_get_filename(argv
[0]);
870 proplist
= pa_proplist_new();
872 while ((c
= getopt_long(argc
, argv
, "s:n:h", long_options
, NULL
)) != -1) {
880 printf(_("pactl %s\n"
881 "Compiled with libpulse %s\n"
882 "Linked with libpulse %s\n"),
884 pa_get_headers_version(),
885 pa_get_library_version());
891 server
= pa_xstrdup(optarg
);
897 if (!(t
= pa_locale_to_utf8(optarg
)) ||
898 pa_proplist_sets(proplist
, PA_PROP_APPLICATION_NAME
, t
) < 0) {
900 pa_log(_("Invalid client name '%s'\n"), t
? t
: optarg
);
915 if (pa_streq(argv
[optind
], "stat"))
917 else if (pa_streq(argv
[optind
], "exit"))
919 else if (pa_streq(argv
[optind
], "list"))
921 else if (pa_streq(argv
[optind
], "upload-sample")) {
923 action
= UPLOAD_SAMPLE
;
925 if (optind
+1 >= argc
) {
926 pa_log(_("Please specify a sample file to load\n"));
931 sample_name
= pa_xstrdup(argv
[optind
+2]);
933 char *f
= pa_path_get_filename(argv
[optind
+1]);
934 sample_name
= pa_xstrndup(f
, strcspn(f
, "."));
938 if (!(sndfile
= sf_open(argv
[optind
+1], SFM_READ
, &sfi
))) {
939 pa_log(_("Failed to open sound file.\n"));
943 if (pa_sndfile_read_sample_spec(sndfile
, &sample_spec
) < 0) {
944 pa_log(_("Failed to determine sample specification from file.\n"));
947 sample_spec
.format
= PA_SAMPLE_FLOAT32
;
949 if (pa_sndfile_read_channel_map(sndfile
, &channel_map
) < 0) {
950 if (sample_spec
.channels
> 2)
951 pa_log(_("Warning: Failed to determine sample specification from file.\n"));
952 pa_channel_map_init_extend(&channel_map
, sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
955 pa_assert(pa_channel_map_compatible(&channel_map
, &sample_spec
));
956 sample_length
= (size_t) sfi
.frames
*pa_frame_size(&sample_spec
);
958 } else if (pa_streq(argv
[optind
], "play-sample")) {
959 action
= PLAY_SAMPLE
;
960 if (argc
!= optind
+2 && argc
!= optind
+3) {
961 pa_log(_("You have to specify a sample name to play\n"));
965 sample_name
= pa_xstrdup(argv
[optind
+1]);
968 device
= pa_xstrdup(argv
[optind
+2]);
970 } else if (pa_streq(argv
[optind
], "remove-sample")) {
971 action
= REMOVE_SAMPLE
;
972 if (argc
!= optind
+2) {
973 pa_log(_("You have to specify a sample name to remove\n"));
977 sample_name
= pa_xstrdup(argv
[optind
+1]);
979 } else if (pa_streq(argv
[optind
], "move-sink-input")) {
980 action
= MOVE_SINK_INPUT
;
981 if (argc
!= optind
+3) {
982 pa_log(_("You have to specify a sink input index and a sink\n"));
986 sink_input_idx
= (uint32_t) atoi(argv
[optind
+1]);
987 sink_name
= pa_xstrdup(argv
[optind
+2]);
989 } else if (pa_streq(argv
[optind
], "move-source-output")) {
990 action
= MOVE_SOURCE_OUTPUT
;
991 if (argc
!= optind
+3) {
992 pa_log(_("You have to specify a source output index and a source\n"));
996 source_output_idx
= (uint32_t) atoi(argv
[optind
+1]);
997 source_name
= pa_xstrdup(argv
[optind
+2]);
999 } else if (pa_streq(argv
[optind
], "load-module")) {
1004 action
= LOAD_MODULE
;
1006 if (argc
<= optind
+1) {
1007 pa_log(_("You have to specify a module name and arguments.\n"));
1011 module_name
= argv
[optind
+1];
1013 for (i
= optind
+2; i
< argc
; i
++)
1014 n
+= strlen(argv
[i
])+1;
1017 p
= module_args
= pa_xmalloc(n
);
1019 for (i
= optind
+2; i
< argc
; i
++)
1020 p
+= sprintf(p
, "%s%s", p
== module_args
? "" : " ", argv
[i
]);
1023 } else if (pa_streq(argv
[optind
], "unload-module")) {
1024 action
= UNLOAD_MODULE
;
1026 if (argc
!= optind
+2) {
1027 pa_log(_("You have to specify a module index\n"));
1031 module_index
= (uint32_t) atoi(argv
[optind
+1]);
1033 } else if (pa_streq(argv
[optind
], "suspend-sink")) {
1034 action
= SUSPEND_SINK
;
1036 if (argc
> optind
+3 || optind
+1 >= argc
) {
1037 pa_log(_("You may not specify more than one sink. You have to specify a boolean value.\n"));
1041 suspend
= pa_parse_boolean(argv
[argc
-1]);
1043 if (argc
> optind
+2)
1044 sink_name
= pa_xstrdup(argv
[optind
+1]);
1046 } else if (pa_streq(argv
[optind
], "suspend-source")) {
1047 action
= SUSPEND_SOURCE
;
1049 if (argc
> optind
+3 || optind
+1 >= argc
) {
1050 pa_log(_("You may not specify more than one source. You have to specify a boolean value.\n"));
1054 suspend
= pa_parse_boolean(argv
[argc
-1]);
1056 if (argc
> optind
+2)
1057 source_name
= pa_xstrdup(argv
[optind
+1]);
1058 } else if (pa_streq(argv
[optind
], "set-card-profile")) {
1059 action
= SET_CARD_PROFILE
;
1061 if (argc
!= optind
+3) {
1062 pa_log(_("You have to specify a card name/index and a profile name\n"));
1066 card_name
= pa_xstrdup(argv
[optind
+1]);
1067 profile_name
= pa_xstrdup(argv
[optind
+2]);
1069 } else if (pa_streq(argv
[optind
], "set-sink-port")) {
1070 action
= SET_SINK_PORT
;
1072 if (argc
!= optind
+3) {
1073 pa_log(_("You have to specify a sink name/index and a port name\n"));
1077 sink_name
= pa_xstrdup(argv
[optind
+1]);
1078 port_name
= pa_xstrdup(argv
[optind
+2]);
1080 } else if (pa_streq(argv
[optind
], "set-source-port")) {
1081 action
= SET_SOURCE_PORT
;
1083 if (argc
!= optind
+3) {
1084 pa_log(_("You have to specify a source name/index and a port name\n"));
1088 source_name
= pa_xstrdup(argv
[optind
+1]);
1089 port_name
= pa_xstrdup(argv
[optind
+2]);
1091 } else if (pa_streq(argv
[optind
], "help")) {
1098 if (action
== NONE
) {
1099 pa_log(_("No valid command specified.\n"));
1103 if (!(m
= pa_mainloop_new())) {
1104 pa_log(_("pa_mainloop_new() failed.\n"));
1108 mainloop_api
= pa_mainloop_get_api(m
);
1110 pa_assert_se(pa_signal_init(mainloop_api
) == 0);
1111 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
1112 pa_signal_new(SIGTERM
, exit_signal_callback
, NULL
);
1113 pa_disable_sigpipe();
1115 if (!(context
= pa_context_new_with_proplist(mainloop_api
, NULL
, proplist
))) {
1116 pa_log(_("pa_context_new() failed.\n"));
1120 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
1121 if (pa_context_connect(context
, server
, 0, NULL
) < 0) {
1122 pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context
)));
1126 if (pa_mainloop_run(m
, &ret
) < 0) {
1127 pa_log(_("pa_mainloop_run() failed.\n"));
1133 pa_stream_unref(sample_stream
);
1136 pa_context_unref(context
);
1140 pa_mainloop_free(m
);
1145 pa_xfree(sample_name
);
1146 pa_xfree(sink_name
);
1147 pa_xfree(source_name
);
1148 pa_xfree(module_args
);
1149 pa_xfree(card_name
);
1150 pa_xfree(profile_name
);
1156 pa_proplist_free(proplist
);