4 This file is part of PulseAudio.
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 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
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/module.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/source.h>
37 #include <pulsecore/client.h>
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/source-output.h>
40 #include <pulsecore/tokenizer.h>
41 #include <pulsecore/strbuf.h>
42 #include <pulsecore/namereg.h>
43 #include <pulsecore/cli-text.h>
44 #include <pulsecore/core-scache.h>
45 #include <pulsecore/sample-util.h>
46 #include <pulsecore/sound-file.h>
47 #include <pulsecore/play-memchunk.h>
48 #include <pulsecore/autoload.h>
49 #include <pulsecore/sound-file-stream.h>
50 #include <pulsecore/props.h>
51 #include <pulsecore/core-util.h>
52 #include <pulsecore/core-error.h>
54 #include "cli-command.h"
58 int (*proc
) (pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
63 #define INCLUDE_META ".include"
64 #define FAIL_META ".fail"
65 #define NOFAIL_META ".nofail"
67 /* Prototypes for all available commands */
68 static int pa_cli_command_exit(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
69 static int pa_cli_command_help(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
70 static int pa_cli_command_modules(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
71 static int pa_cli_command_clients(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
72 static int pa_cli_command_sinks(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
73 static int pa_cli_command_sources(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
74 static int pa_cli_command_sink_inputs(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
75 static int pa_cli_command_source_outputs(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
76 static int pa_cli_command_stat(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
77 static int pa_cli_command_info(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
78 static int pa_cli_command_load(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
79 static int pa_cli_command_unload(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
80 static int pa_cli_command_sink_volume(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
81 static int pa_cli_command_sink_input_volume(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
82 static int pa_cli_command_source_volume(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
83 static int pa_cli_command_sink_mute(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
84 static int pa_cli_command_source_mute(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
85 static int pa_cli_command_sink_default(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
86 static int pa_cli_command_source_default(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
87 static int pa_cli_command_kill_client(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
88 static int pa_cli_command_kill_sink_input(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
89 static int pa_cli_command_kill_source_output(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
90 static int pa_cli_command_scache_play(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
91 static int pa_cli_command_scache_remove(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
92 static int pa_cli_command_scache_list(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
93 static int pa_cli_command_scache_load(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
94 static int pa_cli_command_scache_load_dir(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
95 static int pa_cli_command_play_file(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
96 static int pa_cli_command_autoload_list(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
97 static int pa_cli_command_autoload_add(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
98 static int pa_cli_command_autoload_remove(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
99 static int pa_cli_command_dump(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
100 static int pa_cli_command_list_props(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
101 static int pa_cli_command_move_sink_input(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
102 static int pa_cli_command_move_source_output(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
);
104 /* A method table for all available commands */
106 static const struct command commands
[] = {
107 { "exit", pa_cli_command_exit
, "Terminate the daemon", 1 },
108 { "help", pa_cli_command_help
, "Show this help", 1 },
109 { "list-modules", pa_cli_command_modules
, "List loaded modules", 1 },
110 { "list-sinks", pa_cli_command_sinks
, "List loaded sinks", 1 },
111 { "list-sources", pa_cli_command_sources
, "List loaded sources", 1 },
112 { "list-clients", pa_cli_command_clients
, "List loaded clients", 1 },
113 { "list-sink-inputs", pa_cli_command_sink_inputs
, "List sink inputs", 1 },
114 { "list-source-outputs", pa_cli_command_source_outputs
, "List source outputs", 1 },
115 { "stat", pa_cli_command_stat
, "Show memory block statistics", 1 },
116 { "info", pa_cli_command_info
, "Show comprehensive status", 1 },
117 { "ls", pa_cli_command_info
, NULL
, 1 },
118 { "list", pa_cli_command_info
, NULL
, 1 },
119 { "load-module", pa_cli_command_load
, "Load a module (args: name, arguments)", 3},
120 { "unload-module", pa_cli_command_unload
, "Unload a module (args: index)", 2},
121 { "set-sink-volume", pa_cli_command_sink_volume
, "Set the volume of a sink (args: index|name, volume)", 3},
122 { "set-sink-input-volume", pa_cli_command_sink_input_volume
, "Set the volume of a sink input (args: index|name, volume)", 3},
123 { "set-source-volume", pa_cli_command_source_volume
, "Set the volume of a source (args: index|name, volume)", 3},
124 { "set-sink-mute", pa_cli_command_sink_mute
, "Set the mute switch of a sink (args: index|name, mute)", 3},
125 { "set-source-mute", pa_cli_command_source_mute
, "Set the mute switch of a source (args: index|name, mute)", 3},
126 { "set-default-sink", pa_cli_command_sink_default
, "Set the default sink (args: index|name)", 2},
127 { "set-default-source", pa_cli_command_source_default
, "Set the default source (args: index|name)", 2},
128 { "kill-client", pa_cli_command_kill_client
, "Kill a client (args: index)", 2},
129 { "kill-sink-input", pa_cli_command_kill_sink_input
, "Kill a sink input (args: index)", 2},
130 { "kill-source-output", pa_cli_command_kill_source_output
, "Kill a source output (args: index)", 2},
131 { "list-samples", pa_cli_command_scache_list
, "List all entries in the sample cache", 1},
132 { "play-sample", pa_cli_command_scache_play
, "Play a sample from the sample cache (args: name, sink|index)", 3},
133 { "remove-sample", pa_cli_command_scache_remove
, "Remove a sample from the sample cache (args: name)", 2},
134 { "load-sample", pa_cli_command_scache_load
, "Load a sound file into the sample cache (args: name, filename)", 3},
135 { "load-sample-lazy", pa_cli_command_scache_load
, "Lazily load a sound file into the sample cache (args: name, filename)", 3},
136 { "load-sample-dir-lazy", pa_cli_command_scache_load_dir
, "Lazily load all files in a directory into the sample cache (args: pathname)", 2},
137 { "play-file", pa_cli_command_play_file
, "Play a sound file (args: filename, sink|index)", 3},
138 { "list-autoload", pa_cli_command_autoload_list
, "List autoload entries", 1},
139 { "add-autoload-sink", pa_cli_command_autoload_add
, "Add autoload entry for a sink (args: sink, module name, arguments)", 4},
140 { "add-autoload-source", pa_cli_command_autoload_add
, "Add autoload entry for a source (args: source, module name, arguments)", 4},
141 { "remove-autoload-sink", pa_cli_command_autoload_remove
, "Remove autoload entry for a sink (args: name)", 2},
142 { "remove-autoload-source", pa_cli_command_autoload_remove
, "Remove autoload entry for a source (args: name)", 2},
143 { "dump", pa_cli_command_dump
, "Dump daemon configuration", 1},
144 { "list-props", pa_cli_command_list_props
, NULL
, 1},
145 { "move-sink-input", pa_cli_command_move_sink_input
, "Move sink input to another sink (args: index, sink)", 3},
146 { "move-source-output", pa_cli_command_move_source_output
, "Move source output to another source (args: index, source)", 3},
147 { NULL
, NULL
, NULL
, 0 }
150 static const char whitespace
[] = " \t\n\r";
151 static const char linebreak
[] = "\n\r";
153 static uint32_t parse_index(const char *n
) {
156 if (pa_atou(n
, &idx
) < 0)
157 return (uint32_t) PA_IDXSET_INVALID
;
162 static int pa_cli_command_exit(pa_core
*c
, pa_tokenizer
*t
, PA_GCC_UNUSED pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
163 assert(c
&& c
->mainloop
&& t
);
164 c
->mainloop
->quit(c
->mainloop
, 0);
168 static int pa_cli_command_help(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
169 const struct command
*command
;
170 assert(c
&& t
&& buf
);
172 pa_strbuf_puts(buf
, "Available commands:\n");
174 for (command
= commands
; command
->name
; command
++)
176 pa_strbuf_printf(buf
, " %-25s %s\n", command
->name
, command
->help
);
180 static int pa_cli_command_modules(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
183 s
= pa_module_list_to_string(c
);
185 pa_strbuf_puts(buf
, s
);
190 static int pa_cli_command_clients(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
193 s
= pa_client_list_to_string(c
);
195 pa_strbuf_puts(buf
, s
);
200 static int pa_cli_command_sinks(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
203 s
= pa_sink_list_to_string(c
);
205 pa_strbuf_puts(buf
, s
);
210 static int pa_cli_command_sources(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
213 s
= pa_source_list_to_string(c
);
215 pa_strbuf_puts(buf
, s
);
220 static int pa_cli_command_sink_inputs(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
223 s
= pa_sink_input_list_to_string(c
);
225 pa_strbuf_puts(buf
, s
);
230 static int pa_cli_command_source_outputs(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
233 s
= pa_source_output_list_to_string(c
);
235 pa_strbuf_puts(buf
, s
);
240 static int pa_cli_command_stat(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
244 pa_bytes_snprint(s
, sizeof(s
), c
->memblock_stat
->total_size
);
245 pa_strbuf_printf(buf
, "Memory blocks currently allocated: %u, size: %s.\n",
246 c
->memblock_stat
->total
,
249 pa_bytes_snprint(s
, sizeof(s
), c
->memblock_stat
->allocated_size
);
250 pa_strbuf_printf(buf
, "Memory blocks allocated during the whole lifetime: %u, size: %s.\n",
251 c
->memblock_stat
->allocated
,
254 pa_bytes_snprint(s
, sizeof(s
), pa_scache_total_size(c
));
255 pa_strbuf_printf(buf
, "Total sample cache size: %s.\n", s
);
257 pa_sample_spec_snprint(s
, sizeof(s
), &c
->default_sample_spec
);
258 pa_strbuf_printf(buf
, "Default sample spec: %s\n", s
);
260 pa_strbuf_printf(buf
, "Default sink name: %s\n"
261 "Default source name: %s\n",
262 pa_namereg_get_default_sink_name(c
),
263 pa_namereg_get_default_source_name(c
));
268 static int pa_cli_command_info(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
270 pa_cli_command_stat(c
, t
, buf
, fail
);
271 pa_cli_command_modules(c
, t
, buf
, fail
);
272 pa_cli_command_sinks(c
, t
, buf
, fail
);
273 pa_cli_command_sources(c
, t
, buf
, fail
);
274 pa_cli_command_clients(c
, t
, buf
, fail
);
275 pa_cli_command_sink_inputs(c
, t
, buf
, fail
);
276 pa_cli_command_source_outputs(c
, t
, buf
, fail
);
277 pa_cli_command_scache_list(c
, t
, buf
, fail
);
278 pa_cli_command_autoload_list(c
, t
, buf
, fail
);
282 static int pa_cli_command_load(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
287 if (!(name
= pa_tokenizer_get(t
, 1))) {
288 pa_strbuf_puts(buf
, "You need to specify the module name and optionally arguments.\n");
292 if (!(m
= pa_module_load(c
, name
, pa_tokenizer_get(t
, 2)))) {
293 pa_strbuf_puts(buf
, "Module load failed.\n");
300 static int pa_cli_command_unload(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
307 if (!(i
= pa_tokenizer_get(t
, 1))) {
308 pa_strbuf_puts(buf
, "You need to specify the module index.\n");
312 idx
= (uint32_t) strtoul(i
, &e
, 10);
313 if (*e
|| !(m
= pa_idxset_get_by_index(c
->modules
, idx
))) {
314 pa_strbuf_puts(buf
, "Invalid module index.\n");
318 pa_module_unload_request(m
);
322 static int pa_cli_command_sink_volume(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
328 if (!(n
= pa_tokenizer_get(t
, 1))) {
329 pa_strbuf_puts(buf
, "You need to specify a sink either by its name or its index.\n");
333 if (!(v
= pa_tokenizer_get(t
, 2))) {
334 pa_strbuf_puts(buf
, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
338 if (pa_atou(v
, &volume
) < 0) {
339 pa_strbuf_puts(buf
, "Failed to parse volume.\n");
343 if (!(sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
, 1))) {
344 pa_strbuf_puts(buf
, "No sink found by this name or index.\n");
348 pa_cvolume_set(&cvolume
, sink
->sample_spec
.channels
, volume
);
349 pa_sink_set_volume(sink
, PA_MIXER_HARDWARE
, &cvolume
);
353 static int pa_cli_command_sink_input_volume(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
360 if (!(n
= pa_tokenizer_get(t
, 1))) {
361 pa_strbuf_puts(buf
, "You need to specify a sink input by its index.\n");
365 if ((idx
= parse_index(n
)) == PA_IDXSET_INVALID
) {
366 pa_strbuf_puts(buf
, "Failed to parse index.\n");
370 if (!(v
= pa_tokenizer_get(t
, 2))) {
371 pa_strbuf_puts(buf
, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
375 if (pa_atou(v
, &volume
) < 0) {
376 pa_strbuf_puts(buf
, "Failed to parse volume.\n");
380 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, (uint32_t) idx
))) {
381 pa_strbuf_puts(buf
, "No sink input found with this index.\n");
385 pa_cvolume_set(&cvolume
, si
->sample_spec
.channels
, volume
);
386 pa_sink_input_set_volume(si
, &cvolume
);
390 static int pa_cli_command_source_volume(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
396 if (!(n
= pa_tokenizer_get(t
, 1))) {
397 pa_strbuf_puts(buf
, "You need to specify a source either by its name or its index.\n");
401 if (!(v
= pa_tokenizer_get(t
, 2))) {
402 pa_strbuf_puts(buf
, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
406 if (pa_atou(v
, &volume
) < 0) {
407 pa_strbuf_puts(buf
, "Failed to parse volume.\n");
411 if (!(source
= pa_namereg_get(c
, n
, PA_NAMEREG_SOURCE
, 1))) {
412 pa_strbuf_puts(buf
, "No source found by this name or index.\n");
416 pa_cvolume_set(&cvolume
, source
->sample_spec
.channels
, volume
);
417 pa_source_set_volume(source
, PA_MIXER_HARDWARE
, &cvolume
);
421 static int pa_cli_command_sink_mute(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
426 if (!(n
= pa_tokenizer_get(t
, 1))) {
427 pa_strbuf_puts(buf
, "You need to specify a sink either by its name or its index.\n");
431 if (!(m
= pa_tokenizer_get(t
, 2))) {
432 pa_strbuf_puts(buf
, "You need to specify a mute switch setting (0/1).\n");
436 if (pa_atoi(m
, &mute
) < 0) {
437 pa_strbuf_puts(buf
, "Failed to parse mute switch.\n");
441 if (!(sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
, 1))) {
442 pa_strbuf_puts(buf
, "No sink found by this name or index.\n");
446 pa_sink_set_mute(sink
, PA_MIXER_HARDWARE
, mute
);
450 static int pa_cli_command_source_mute(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
455 if (!(n
= pa_tokenizer_get(t
, 1))) {
456 pa_strbuf_puts(buf
, "You need to specify a source either by its name or its index.\n");
460 if (!(m
= pa_tokenizer_get(t
, 2))) {
461 pa_strbuf_puts(buf
, "You need to specify a mute switch setting (0/1).\n");
465 if (pa_atoi(m
, &mute
) < 0) {
466 pa_strbuf_puts(buf
, "Failed to parse mute switch.\n");
470 if (!(source
= pa_namereg_get(c
, n
, PA_NAMEREG_SOURCE
, 1))) {
471 pa_strbuf_puts(buf
, "No sink found by this name or index.\n");
475 pa_source_set_mute(source
, PA_MIXER_HARDWARE
, mute
);
479 static int pa_cli_command_sink_default(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
483 if (!(n
= pa_tokenizer_get(t
, 1))) {
484 pa_strbuf_puts(buf
, "You need to specify a sink either by its name or its index.\n");
488 pa_namereg_set_default(c
, n
, PA_NAMEREG_SINK
);
492 static int pa_cli_command_source_default(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
496 if (!(n
= pa_tokenizer_get(t
, 1))) {
497 pa_strbuf_puts(buf
, "You need to specify a source either by its name or its index.\n");
501 pa_namereg_set_default(c
, n
, PA_NAMEREG_SOURCE
);
505 static int pa_cli_command_kill_client(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
511 if (!(n
= pa_tokenizer_get(t
, 1))) {
512 pa_strbuf_puts(buf
, "You need to specify a client by its index.\n");
516 if ((idx
= parse_index(n
)) == PA_IDXSET_INVALID
) {
517 pa_strbuf_puts(buf
, "Failed to parse index.\n");
521 if (!(client
= pa_idxset_get_by_index(c
->clients
, idx
))) {
522 pa_strbuf_puts(buf
, "No client found by this index.\n");
526 pa_client_kill(client
);
530 static int pa_cli_command_kill_sink_input(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
532 pa_sink_input
*sink_input
;
536 if (!(n
= pa_tokenizer_get(t
, 1))) {
537 pa_strbuf_puts(buf
, "You need to specify a sink input by its index.\n");
541 if ((idx
= parse_index(n
)) == PA_IDXSET_INVALID
) {
542 pa_strbuf_puts(buf
, "Failed to parse index.\n");
546 if (!(sink_input
= pa_idxset_get_by_index(c
->sink_inputs
, idx
))) {
547 pa_strbuf_puts(buf
, "No sink input found by this index.\n");
551 pa_sink_input_kill(sink_input
);
555 static int pa_cli_command_kill_source_output(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
557 pa_source_output
*source_output
;
561 if (!(n
= pa_tokenizer_get(t
, 1))) {
562 pa_strbuf_puts(buf
, "You need to specify a source output by its index.\n");
566 if ((idx
= parse_index(n
)) == PA_IDXSET_INVALID
) {
567 pa_strbuf_puts(buf
, "Failed to parse index.\n");
571 if (!(source_output
= pa_idxset_get_by_index(c
->source_outputs
, idx
))) {
572 pa_strbuf_puts(buf
, "No source output found by this index.\n");
576 pa_source_output_kill(source_output
);
580 static int pa_cli_command_scache_list(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
583 s
= pa_scache_list_to_string(c
);
585 pa_strbuf_puts(buf
, s
);
590 static int pa_cli_command_scache_play(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
591 const char *n
, *sink_name
;
593 assert(c
&& t
&& buf
&& fail
);
595 if (!(n
= pa_tokenizer_get(t
, 1)) || !(sink_name
= pa_tokenizer_get(t
, 2))) {
596 pa_strbuf_puts(buf
, "You need to specify a sample name and a sink name.\n");
600 if (!(sink
= pa_namereg_get(c
, sink_name
, PA_NAMEREG_SINK
, 1))) {
601 pa_strbuf_puts(buf
, "No sink by that name.\n");
605 if (pa_scache_play_item(c
, n
, sink
, PA_VOLUME_NORM
) < 0) {
606 pa_strbuf_puts(buf
, "Failed to play sample.\n");
613 static int pa_cli_command_scache_remove(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
615 assert(c
&& t
&& buf
&& fail
);
617 if (!(n
= pa_tokenizer_get(t
, 1))) {
618 pa_strbuf_puts(buf
, "You need to specify a sample name.\n");
622 if (pa_scache_remove_item(c
, n
) < 0) {
623 pa_strbuf_puts(buf
, "Failed to remove sample.\n");
630 static int pa_cli_command_scache_load(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
631 const char *fname
, *n
;
633 assert(c
&& t
&& buf
&& fail
);
635 if (!(fname
= pa_tokenizer_get(t
, 2)) || !(n
= pa_tokenizer_get(t
, 1))) {
636 pa_strbuf_puts(buf
, "You need to specify a file name and a sample name.\n");
640 if (strstr(pa_tokenizer_get(t
, 0), "lazy"))
641 r
= pa_scache_add_file_lazy(c
, n
, fname
, NULL
);
643 r
= pa_scache_add_file(c
, n
, fname
, NULL
);
646 pa_strbuf_puts(buf
, "Failed to load sound file.\n");
651 static int pa_cli_command_scache_load_dir(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
653 assert(c
&& t
&& buf
&& fail
);
655 if (!(pname
= pa_tokenizer_get(t
, 1))) {
656 pa_strbuf_puts(buf
, "You need to specify a path name.\n");
660 if (pa_scache_add_directory_lazy(c
, pname
) < 0) {
661 pa_strbuf_puts(buf
, "Failed to load directory.\n");
668 static int pa_cli_command_play_file(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
669 const char *fname
, *sink_name
;
671 assert(c
&& t
&& buf
&& fail
);
673 if (!(fname
= pa_tokenizer_get(t
, 1)) || !(sink_name
= pa_tokenizer_get(t
, 2))) {
674 pa_strbuf_puts(buf
, "You need to specify a file name and a sink name.\n");
678 if (!(sink
= pa_namereg_get(c
, sink_name
, PA_NAMEREG_SINK
, 1))) {
679 pa_strbuf_puts(buf
, "No sink by that name.\n");
684 return pa_play_file(sink
, fname
, NULL
);
687 static int pa_cli_command_autoload_add(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
689 assert(c
&& t
&& buf
&& fail
);
691 if (!(a
= pa_tokenizer_get(t
, 1)) || !(b
= pa_tokenizer_get(t
, 2))) {
692 pa_strbuf_puts(buf
, "You need to specify a device name, a filename or a module name and optionally module arguments\n");
696 pa_autoload_add(c
, a
, strstr(pa_tokenizer_get(t
, 0), "sink") ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
, b
, pa_tokenizer_get(t
, 3), NULL
);
701 static int pa_cli_command_autoload_remove(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
703 assert(c
&& t
&& buf
&& fail
);
705 if (!(name
= pa_tokenizer_get(t
, 1))) {
706 pa_strbuf_puts(buf
, "You need to specify a device name\n");
710 if (pa_autoload_remove_by_name(c
, name
, strstr(pa_tokenizer_get(t
, 0), "sink") ? PA_NAMEREG_SINK
: PA_NAMEREG_SOURCE
) < 0) {
711 pa_strbuf_puts(buf
, "Failed to remove autload entry\n");
718 static int pa_cli_command_autoload_list(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
721 s
= pa_autoload_list_to_string(c
);
723 pa_strbuf_puts(buf
, s
);
728 static int pa_cli_command_list_props(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
730 pa_property_dump(c
, buf
);
734 static int pa_cli_command_move_sink_input(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
740 if (!(n
= pa_tokenizer_get(t
, 1))) {
741 pa_strbuf_puts(buf
, "You need to specify a sink input by its index.\n");
745 if ((idx
= parse_index(n
)) == PA_IDXSET_INVALID
) {
746 pa_strbuf_puts(buf
, "Failed to parse index.\n");
750 if (!(k
= pa_tokenizer_get(t
, 2))) {
751 pa_strbuf_puts(buf
, "You need to specify a sink.\n");
755 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, (uint32_t) idx
))) {
756 pa_strbuf_puts(buf
, "No sink input found with this index.\n");
760 if (!(sink
= pa_namereg_get(c
, k
, PA_NAMEREG_SINK
, 1))) {
761 pa_strbuf_puts(buf
, "No sink found by this name or index.\n");
765 if (pa_sink_input_move_to(si
, sink
, 0) < 0) {
766 pa_strbuf_puts(buf
, "Moved failed.\n");
772 static int pa_cli_command_move_source_output(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, int *fail
) {
774 pa_source_output
*so
;
778 if (!(n
= pa_tokenizer_get(t
, 1))) {
779 pa_strbuf_puts(buf
, "You need to specify a source output by its index.\n");
783 if ((idx
= parse_index(n
)) == PA_IDXSET_INVALID
) {
784 pa_strbuf_puts(buf
, "Failed to parse index.\n");
788 if (!(k
= pa_tokenizer_get(t
, 2))) {
789 pa_strbuf_puts(buf
, "You need to specify a source.\n");
793 if (!(so
= pa_idxset_get_by_index(c
->source_outputs
, (uint32_t) idx
))) {
794 pa_strbuf_puts(buf
, "No source output found with this index.\n");
798 if (!(source
= pa_namereg_get(c
, k
, PA_NAMEREG_SOURCE
, 1))) {
799 pa_strbuf_puts(buf
, "No source found by this name or index.\n");
803 if (pa_source_output_move_to(so
, source
) < 0) {
804 pa_strbuf_puts(buf
, "Moved failed.\n");
810 static int pa_cli_command_dump(pa_core
*c
, pa_tokenizer
*t
, pa_strbuf
*buf
, PA_GCC_UNUSED
int *fail
) {
820 pa_autoload_entry
*a
;
827 pa_strbuf_printf(buf
, "### Configuration dump generated at %s\n", ctime_r(&now
, txt
));
829 pa_strbuf_printf(buf
, "### Configuration dump generated at %s\n", ctime(&now
));
833 for (m
= pa_idxset_first(c
->modules
, &idx
); m
; m
= pa_idxset_next(c
->modules
, &idx
)) {
837 pa_strbuf_printf(buf
, "load-module %s", m
->name
);
840 pa_strbuf_printf(buf
, " %s", m
->argument
);
842 pa_strbuf_puts(buf
, "\n");
847 for (sink
= pa_idxset_first(c
->sinks
, &idx
); sink
; sink
= pa_idxset_next(c
->sinks
, &idx
)) {
848 if (sink
->owner
&& sink
->owner
->auto_unload
)
852 pa_strbuf_puts(buf
, "\n");
856 pa_strbuf_printf(buf
, "set-sink-volume %s 0x%03x\n", sink
->name
, pa_cvolume_avg(pa_sink_get_volume(sink
, PA_MIXER_HARDWARE
)));
857 pa_strbuf_printf(buf
, "set-sink-mute %s %d\n", sink
->name
, pa_sink_get_mute(sink
, PA_MIXER_HARDWARE
));
860 for (source
= pa_idxset_first(c
->sources
, &idx
); source
; source
= pa_idxset_next(c
->sources
, &idx
)) {
861 if (source
->owner
&& source
->owner
->auto_unload
)
865 pa_strbuf_puts(buf
, "\n");
869 pa_strbuf_printf(buf
, "set-source-volume %s 0x%03x\n", source
->name
, pa_cvolume_avg(pa_source_get_volume(source
, PA_MIXER_HARDWARE
)));
870 pa_strbuf_printf(buf
, "set-source-mute %s %d\n", source
->name
, pa_source_get_mute(source
, PA_MIXER_HARDWARE
));
874 if (c
->autoload_hashmap
) {
878 while ((a
= pa_hashmap_iterate(c
->autoload_hashmap
, &i
, NULL
))) {
881 pa_strbuf_puts(buf
, "\n");
885 pa_strbuf_printf(buf
, "add-autoload-%s %s %s", a
->type
== PA_NAMEREG_SINK
? "sink" : "source", a
->name
, a
->module
);
888 pa_strbuf_printf(buf
, " %s", a
->argument
);
890 pa_strbuf_puts(buf
, "\n");
896 if ((p
= pa_namereg_get_default_sink_name(c
))) {
898 pa_strbuf_puts(buf
, "\n");
901 pa_strbuf_printf(buf
, "set-default-sink %s\n", p
);
904 if ((p
= pa_namereg_get_default_source_name(c
))) {
906 pa_strbuf_puts(buf
, "\n");
909 pa_strbuf_printf(buf
, "set-default-source %s\n", p
);
912 pa_strbuf_puts(buf
, "\n### EOF\n");
917 int pa_cli_command_execute_line(pa_core
*c
, const char *s
, pa_strbuf
*buf
, int *fail
) {
920 cs
= s
+strspn(s
, whitespace
);
922 if (*cs
== '#' || !*cs
)
924 else if (*cs
== '.') {
925 if (!strcmp(cs
, FAIL_META
))
927 else if (!strcmp(cs
, NOFAIL_META
))
931 l
= strcspn(cs
, whitespace
);
933 if (l
== sizeof(INCLUDE_META
)-1 && !strncmp(cs
, INCLUDE_META
, l
)) {
934 const char *filename
= cs
+l
+strspn(cs
+l
, whitespace
);
936 if (pa_cli_command_execute_file(c
, filename
, buf
, fail
) < 0)
937 if (*fail
) return -1;
939 pa_strbuf_printf(buf
, "Invalid meta command: %s\n", cs
);
940 if (*fail
) return -1;
944 const struct command
*command
;
948 l
= strcspn(cs
, whitespace
);
950 for (command
= commands
; command
->name
; command
++)
951 if (strlen(command
->name
) == l
&& !strncmp(cs
, command
->name
, l
)) {
953 pa_tokenizer
*t
= pa_tokenizer_new(cs
, command
->args
);
955 ret
= command
->proc(c
, t
, buf
, fail
);
956 pa_tokenizer_free(t
);
959 if (ret
< 0 && *fail
)
966 pa_strbuf_printf(buf
, "Unknown command: %s\n", cs
);
975 int pa_cli_command_execute_file(pa_core
*c
, const char *fn
, pa_strbuf
*buf
, int *fail
) {
979 assert(c
&& fn
&& buf
);
981 if (!(f
= fopen(fn
, "r"))) {
982 pa_strbuf_printf(buf
, "open('%s') failed: %s\n", fn
, pa_cstrerror(errno
));
988 while (fgets(line
, sizeof(line
), f
)) {
989 char *e
= line
+ strcspn(line
, linebreak
);
992 if (pa_cli_command_execute_line(c
, line
, buf
, fail
) < 0 && *fail
)
1005 int pa_cli_command_execute(pa_core
*c
, const char *s
, pa_strbuf
*buf
, int *fail
) {
1007 assert(c
&& s
&& buf
&& fail
);
1011 size_t l
= strcspn(p
, linebreak
);
1012 char *line
= pa_xstrndup(p
, l
);
1014 if (pa_cli_command_execute_line(c
, line
, buf
, fail
) < 0&& *fail
) {
1021 p
+= strspn(p
, linebreak
);