4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include "cli-command.h"
37 #include "sink-input.h"
38 #include "source-output.h"
39 #include "tokenizer.h"
44 #include "sample-util.h"
48 int (*proc
) (struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
53 static int pa_cli_command_exit(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
54 static int pa_cli_command_help(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
55 static int pa_cli_command_modules(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
56 static int pa_cli_command_clients(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
57 static int pa_cli_command_sinks(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
58 static int pa_cli_command_sources(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
59 static int pa_cli_command_sink_inputs(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
60 static int pa_cli_command_source_outputs(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
61 static int pa_cli_command_stat(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
62 static int pa_cli_command_info(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
63 static int pa_cli_command_load(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
64 static int pa_cli_command_unload(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
65 static int pa_cli_command_sink_volume(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
66 static int pa_cli_command_sink_input_volume(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
67 static int pa_cli_command_sink_default(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
68 static int pa_cli_command_source_default(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
69 static int pa_cli_command_kill_client(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
70 static int pa_cli_command_kill_sink_input(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
71 static int pa_cli_command_kill_source_output(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
72 static int pa_cli_command_scache_play(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
73 static int pa_cli_command_scache_remove(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
74 static int pa_cli_command_scache_list(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
76 static const struct command commands
[] = {
77 { "exit", pa_cli_command_exit
, "Terminate the daemon", 1 },
78 { "help", pa_cli_command_help
, "Show this help", 1 },
79 { "modules", pa_cli_command_modules
, "List loaded modules", 1 },
80 { "sinks", pa_cli_command_sinks
, "List loaded sinks", 1 },
81 { "sources", pa_cli_command_sources
, "List loaded sources", 1 },
82 { "clients", pa_cli_command_clients
, "List loaded clients", 1 },
83 { "sink_inputs", pa_cli_command_sink_inputs
, "List sink inputs", 1 },
84 { "source_outputs", pa_cli_command_source_outputs
, "List source outputs", 1 },
85 { "stat", pa_cli_command_stat
, "Show memory block statistics", 1 },
86 { "info", pa_cli_command_info
, "Show comprehensive status", 1 },
87 { "ls", pa_cli_command_info
, NULL
, 1 },
88 { "list", pa_cli_command_info
, NULL
, 1 },
89 { "load", pa_cli_command_load
, "Load a module (args: name, arguments)", 3},
90 { "unload", pa_cli_command_unload
, "Unload a module (args: index)", 2},
91 { "sink_volume", pa_cli_command_sink_volume
, "Set the volume of a sink (args: index|name, volume)", 3},
92 { "sink_input_volume", pa_cli_command_sink_input_volume
, "Set the volume of a sink input (args: index|name, volume)", 3},
93 { "sink_default", pa_cli_command_sink_default
, "Set the default sink (args: index|name)", 2},
94 { "source_default", pa_cli_command_source_default
, "Set the default source (args: index|name)", 2},
95 { "kill_client", pa_cli_command_kill_client
, "Kill a client (args: index)", 2},
96 { "kill_sink_input", pa_cli_command_kill_sink_input
, "Kill a sink input (args: index)", 2},
97 { "kill_source_output", pa_cli_command_kill_source_output
, "Kill a source output (args: index)", 2},
98 { "scache_list", pa_cli_command_scache_list
, "List all entries in the sample cache", 2},
99 { "scache_play", pa_cli_command_scache_play
, "Play a sample from the sample cache (args: name, sink|index)", 3},
100 { "scache_remove", pa_cli_command_scache_remove
, "Remove a sample from the sample cache (args: name)", 2},
101 { NULL
, NULL
, NULL
, 0 }
104 static const char whitespace
[] = " \t\n\r";
105 static const char linebreak
[] = "\n\r";
107 static uint32_t parse_index(const char *n
) {
110 index
= strtol(n
, &x
, 0);
111 if (!x
|| *x
!= 0 || index
< 0)
112 return (uint32_t) PA_IDXSET_INVALID
;
114 return (uint32_t) index
;
117 static int pa_cli_command_exit(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
118 assert(c
&& c
->mainloop
&& t
);
119 c
->mainloop
->quit(c
->mainloop
, 0);
123 static int pa_cli_command_help(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
124 const struct command
*command
;
125 assert(c
&& t
&& buf
);
127 pa_strbuf_puts(buf
, "Available commands:\n");
129 for (command
= commands
; command
->name
; command
++)
131 pa_strbuf_printf(buf
, " %-20s %s\n", command
->name
, command
->help
);
135 static int pa_cli_command_modules(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
138 s
= pa_module_list_to_string(c
);
140 pa_strbuf_puts(buf
, s
);
145 static int pa_cli_command_clients(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
148 s
= pa_client_list_to_string(c
);
150 pa_strbuf_puts(buf
, s
);
155 static int pa_cli_command_sinks(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
158 s
= pa_sink_list_to_string(c
);
160 pa_strbuf_puts(buf
, s
);
165 static int pa_cli_command_sources(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
168 s
= pa_source_list_to_string(c
);
170 pa_strbuf_puts(buf
, s
);
175 static int pa_cli_command_sink_inputs(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
178 s
= pa_sink_input_list_to_string(c
);
180 pa_strbuf_puts(buf
, s
);
185 static int pa_cli_command_source_outputs(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
188 s
= pa_source_output_list_to_string(c
);
190 pa_strbuf_puts(buf
, s
);
195 static int pa_cli_command_stat(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
197 pa_strbuf_printf(buf
, "Memory blocks allocated: %u, total size: %u bytes.\n", pa_memblock_get_count(), pa_memblock_get_total());
201 static int pa_cli_command_info(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
203 pa_cli_command_stat(c
, t
, buf
, fail
, verbose
);
204 pa_cli_command_modules(c
, t
, buf
, fail
, verbose
);
205 pa_cli_command_sinks(c
, t
, buf
, fail
, verbose
);
206 pa_cli_command_sources(c
, t
, buf
, fail
, verbose
);
207 pa_cli_command_clients(c
, t
, buf
, fail
, verbose
);
208 pa_cli_command_sink_inputs(c
, t
, buf
, fail
, verbose
);
209 pa_cli_command_source_outputs(c
, t
, buf
, fail
, verbose
);
210 pa_cli_command_scache_list(c
, t
, buf
, fail
, verbose
);
214 static int pa_cli_command_load(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
220 if (!(name
= pa_tokenizer_get(t
, 1))) {
221 pa_strbuf_puts(buf
, "You need to specify the module name and optionally arguments.\n");
225 if (!(m
= pa_module_load(c
, name
, pa_tokenizer_get(t
, 2)))) {
226 pa_strbuf_puts(buf
, "Module load failed.\n");
231 snprintf(txt
, sizeof(txt
), "Module successfully loaded, index: %u.\n", m
->index
);
232 pa_strbuf_puts(buf
, txt
);
237 static int pa_cli_command_unload(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
244 if (!(i
= pa_tokenizer_get(t
, 1))) {
245 pa_strbuf_puts(buf
, "You need to specify the module index.\n");
249 index
= (uint32_t) strtoul(i
, &e
, 10);
250 if (*e
|| !(m
= pa_idxset_get_by_index(c
->modules
, index
))) {
251 pa_strbuf_puts(buf
, "Invalid module index.\n");
255 pa_module_unload_request(c
, m
);
259 static int pa_cli_command_sink_volume(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
262 struct pa_sink
*sink
;
265 if (!(n
= pa_tokenizer_get(t
, 1))) {
266 pa_strbuf_puts(buf
, "You need to specify a sink either by its name or its index.\n");
270 if (!(v
= pa_tokenizer_get(t
, 2))) {
271 pa_strbuf_puts(buf
, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
275 volume
= strtol(v
, &x
, 0);
276 if (!x
|| *x
!= 0 || volume
< 0) {
277 pa_strbuf_puts(buf
, "Failed to parse volume.\n");
281 if (!(sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
))) {
282 pa_strbuf_puts(buf
, "No sink found by this name or index.\n");
286 sink
->volume
= (uint32_t) volume
;
290 static int pa_cli_command_sink_input_volume(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
292 struct pa_sink_input
*si
;
297 if (!(n
= pa_tokenizer_get(t
, 1))) {
298 pa_strbuf_puts(buf
, "You need to specify a sink input by its index.\n");
302 if ((index
= parse_index(n
)) == PA_IDXSET_INVALID
) {
303 pa_strbuf_puts(buf
, "Failed to parse index.\n");
307 if (!(v
= pa_tokenizer_get(t
, 2))) {
308 pa_strbuf_puts(buf
, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
313 volume
= strtol(v
, &x
, 0);
314 if (!x
|| *x
!= 0 || volume
< 0) {
315 pa_strbuf_puts(buf
, "Failed to parse volume.\n");
319 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, (uint32_t) index
))) {
320 pa_strbuf_puts(buf
, "No sink input found with this index.\n");
324 si
->volume
= (uint32_t) volume
;
328 static int pa_cli_command_sink_default(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
330 struct pa_sink
*sink
;
333 if (!(n
= pa_tokenizer_get(t
, 1))) {
334 pa_strbuf_puts(buf
, "You need to specify a sink either by its name or its index.\n");
338 if (!(sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
))) {
339 pa_strbuf_puts(buf
, "No sink found by this name or index.\n");
343 c
->default_sink_index
= sink
->index
;
347 static int pa_cli_command_source_default(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
349 struct pa_source
*source
;
352 if (!(n
= pa_tokenizer_get(t
, 1))) {
353 pa_strbuf_puts(buf
, "You need to specify a source either by its name or its index.\n");
357 if (!(source
= pa_namereg_get(c
, n
, PA_NAMEREG_SOURCE
))) {
358 pa_strbuf_puts(buf
, "No source found by this name or index.\n");
362 c
->default_source_index
= source
->index
;
366 static int pa_cli_command_kill_client(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
368 struct pa_client
*client
;
372 if (!(n
= pa_tokenizer_get(t
, 1))) {
373 pa_strbuf_puts(buf
, "You need to specify a client by its index.\n");
377 if ((index
= parse_index(n
)) == PA_IDXSET_INVALID
) {
378 pa_strbuf_puts(buf
, "Failed to parse index.\n");
382 if (!(client
= pa_idxset_get_by_index(c
->clients
, index
))) {
383 pa_strbuf_puts(buf
, "No client found by this index.\n");
387 pa_client_kill(client
);
391 static int pa_cli_command_kill_sink_input(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
393 struct pa_sink_input
*sink_input
;
397 if (!(n
= pa_tokenizer_get(t
, 1))) {
398 pa_strbuf_puts(buf
, "You need to specify a sink input by its index.\n");
402 if ((index
= parse_index(n
)) == PA_IDXSET_INVALID
) {
403 pa_strbuf_puts(buf
, "Failed to parse index.\n");
407 if (!(sink_input
= pa_idxset_get_by_index(c
->sink_inputs
, index
))) {
408 pa_strbuf_puts(buf
, "No sink input found by this index.\n");
412 pa_sink_input_kill(sink_input
);
416 static int pa_cli_command_kill_source_output(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
418 struct pa_source_output
*source_output
;
422 if (!(n
= pa_tokenizer_get(t
, 1))) {
423 pa_strbuf_puts(buf
, "You need to specify a source output by its index.\n");
427 if ((index
= parse_index(n
)) == PA_IDXSET_INVALID
) {
428 pa_strbuf_puts(buf
, "Failed to parse index.\n");
432 if (!(source_output
= pa_idxset_get_by_index(c
->source_outputs
, index
))) {
433 pa_strbuf_puts(buf
, "No source output found by this index.\n");
437 pa_source_output_kill(source_output
);
441 static int pa_cli_command_scache_list(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
444 s
= pa_scache_list_to_string(c
);
446 pa_strbuf_puts(buf
, s
);
451 static int pa_cli_command_scache_play(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
452 const char *n
, *sink_name
;
453 struct pa_sink
*sink
;
454 assert(c
&& t
&& buf
&& fail
&& verbose
);
456 if (!(n
= pa_tokenizer_get(t
, 1)) || !(sink_name
= pa_tokenizer_get(t
, 2))) {
457 pa_strbuf_puts(buf
, "You need to specify a sample name and a sink name.\n");
461 if (!(sink
= pa_namereg_get(c
, sink_name
, PA_NAMEREG_SINK
))) {
462 pa_strbuf_puts(buf
, "No sink by that name.\n");
466 if (pa_scache_play_item(c
, n
, sink
, PA_VOLUME_NORM
) < 0) {
467 pa_strbuf_puts(buf
, "Failed to play sample.\n");
474 static int pa_cli_command_scache_remove(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
476 assert(c
&& t
&& buf
&& fail
&& verbose
);
478 if (!(n
= pa_tokenizer_get(t
, 1))) {
479 pa_strbuf_puts(buf
, "You need to specify a sample name.\n");
483 if (pa_scache_remove_item(c
, n
) < 0) {
484 pa_strbuf_puts(buf
, "Failed to remove sample.\n");
491 int pa_cli_command_execute_line(struct pa_core
*c
, const char *s
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
494 cs
= s
+strspn(s
, whitespace
);
496 if (*cs
== '#' || !*cs
)
498 else if (*cs
== '.') {
499 static const char fail_meta
[] = ".fail";
500 static const char nofail_meta
[] = ".nofail";
501 static const char verbose_meta
[] = ".verbose";
502 static const char noverbose_meta
[] = ".noverbose";
504 if (!strcmp(cs
, verbose_meta
))
506 else if (!strcmp(cs
, noverbose_meta
))
508 else if (!strcmp(cs
, fail_meta
))
510 else if (!strcmp(cs
, nofail_meta
))
514 static const char include_meta
[] = ".include";
515 l
= strcspn(cs
, whitespace
);
517 if (l
== sizeof(include_meta
)-1 && !strncmp(cs
, include_meta
, l
)) {
518 const char *filename
= cs
+l
+strspn(cs
+l
, whitespace
);
520 if (pa_cli_command_execute_file(c
, filename
, buf
, fail
, verbose
) < 0)
521 if (*fail
) return -1;
523 pa_strbuf_printf(buf
, "Invalid meta command: %s\n", cs
);
524 if (*fail
) return -1;
528 const struct command
*command
;
532 l
= strcspn(cs
, whitespace
);
534 for (command
= commands
; command
->name
; command
++)
535 if (strlen(command
->name
) == l
&& !strncmp(cs
, command
->name
, l
)) {
537 struct pa_tokenizer
*t
= pa_tokenizer_new(cs
, command
->args
);
539 ret
= command
->proc(c
, t
, buf
, fail
, verbose
);
540 pa_tokenizer_free(t
);
543 if (ret
< 0 && *fail
)
550 pa_strbuf_printf(buf
, "Unknown command: %s\n", cs
);
559 int pa_cli_command_execute_file(struct pa_core
*c
, const char *fn
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
563 assert(c
&& fn
&& buf
);
565 if (!(f
= fopen(fn
, "r"))) {
566 pa_strbuf_printf(buf
, "open('%s') failed: %s\n", fn
, strerror(errno
));
573 pa_strbuf_printf(buf
, "Executing file: '%s'\n", fn
);
575 while (fgets(line
, sizeof(line
), f
)) {
576 char *e
= line
+ strcspn(line
, linebreak
);
579 if (pa_cli_command_execute_line(c
, line
, buf
, fail
, verbose
) < 0 && *fail
)
584 pa_strbuf_printf(buf
, "Executed file: '%s'\n", fn
);
595 int pa_cli_command_execute(struct pa_core
*c
, const char *s
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
597 assert(c
&& s
&& buf
&& fail
&& verbose
);
601 size_t l
= strcspn(p
, linebreak
);
602 char *line
= strndup(p
, l
);
605 if (pa_cli_command_execute_line(c
, line
, buf
, fail
, verbose
) < 0&& *fail
) {
612 p
+= strspn(p
, linebreak
);