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"
45 #include "sound-file.h"
46 #include "play-memchunk.h"
50 int (*proc) (struct pa_core *c, struct pa_tokenizer*t, struct pa_strbuf *buf, int *fail, int *verbose);
55 static int pa_cli_command_exit(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
56 static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
57 static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
58 static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
59 static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
60 static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
61 static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
62 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
63 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
64 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
65 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
66 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
67 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
68 static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
69 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
70 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
71 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
72 static int pa_cli_command_kill_sink_input(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
73 static int pa_cli_command_kill_source_output(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
74 static int pa_cli_command_scache_play(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
75 static int pa_cli_command_scache_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
76 static int pa_cli_command_scache_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
77 static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
78 static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
80 static const struct command commands[] = {
81 { "exit", pa_cli_command_exit, "Terminate the daemon", 1 },
82 { "help", pa_cli_command_help, "Show this help", 1 },
83 { "modules", pa_cli_command_modules, "List loaded modules", 1 },
84 { "sinks", pa_cli_command_sinks, "List loaded sinks", 1 },
85 { "sources", pa_cli_command_sources, "List loaded sources", 1 },
86 { "clients", pa_cli_command_clients, "List loaded clients", 1 },
87 { "sink_inputs", pa_cli_command_sink_inputs, "List sink inputs", 1 },
88 { "source_outputs", pa_cli_command_source_outputs, "List source outputs", 1 },
89 { "stat", pa_cli_command_stat, "Show memory block statistics", 1 },
90 { "info", pa_cli_command_info, "Show comprehensive status", 1 },
91 { "ls", pa_cli_command_info, NULL, 1 },
92 { "list", pa_cli_command_info, NULL, 1 },
93 { "load", pa_cli_command_load, "Load a module (args: name, arguments)", 3},
94 { "unload", pa_cli_command_unload, "Unload a module (args: index)", 2},
95 { "sink_volume", pa_cli_command_sink_volume, "Set the volume of a sink (args: index|name, volume)", 3},
96 { "sink_input_volume", pa_cli_command_sink_input_volume, "Set the volume of a sink input (args: index|name, volume)", 3},
97 { "sink_default", pa_cli_command_sink_default, "Set the default sink (args: index|name)", 2},
98 { "source_default", pa_cli_command_source_default, "Set the default source (args: index|name)", 2},
99 { "kill_client", pa_cli_command_kill_client, "Kill a client (args: index)", 2},
100 { "kill_sink_input", pa_cli_command_kill_sink_input, "Kill a sink input (args: index)", 2},
101 { "kill_source_output", pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
102 { "scache_list", pa_cli_command_scache_list, "List all entries in the sample cache", 2},
103 { "scache_play", pa_cli_command_scache_play, "Play a sample from the sample cache (args: name, sink|index)", 3},
104 { "scache_remove", pa_cli_command_scache_remove, "Remove a sample from the sample cache (args: name)", 2},
105 { "scache_load", pa_cli_command_scache_load, "Load a sound file into the sample cache (args: filename,name)", 3},
106 { "play_file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3},
107 { NULL, NULL, NULL, 0 }
110 static const char whitespace[] = " \t\n\r";
111 static const char linebreak[] = "\n\r";
113 static uint32_t parse_index(const char *n) {
116 index = strtol(n, &x, 0);
117 if (!x || *x != 0 || index < 0)
118 return (uint32_t) PA_IDXSET_INVALID;
120 return (uint32_t) index;
123 static int pa_cli_command_exit(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
124 assert(c && c->mainloop && t);
125 c->mainloop->quit(c->mainloop, 0);
129 static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
130 const struct command*command;
131 assert(c && t && buf);
133 pa_strbuf_puts(buf, "Available commands:\n");
135 for (command = commands; command->name; command++)
137 pa_strbuf_printf(buf, " %-20s %s\n", command->name, command->help);
141 static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
144 s = pa_module_list_to_string(c);
146 pa_strbuf_puts(buf, s);
151 static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
154 s = pa_client_list_to_string(c);
156 pa_strbuf_puts(buf, s);
161 static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
164 s = pa_sink_list_to_string(c);
166 pa_strbuf_puts(buf, s);
171 static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
174 s = pa_source_list_to_string(c);
176 pa_strbuf_puts(buf, s);
181 static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
184 s = pa_sink_input_list_to_string(c);
186 pa_strbuf_puts(buf, s);
191 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
194 s = pa_source_output_list_to_string(c);
196 pa_strbuf_puts(buf, s);
201 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
203 pa_strbuf_printf(buf, "Memory blocks allocated: %u, total size: %u bytes.\n", pa_memblock_get_count(), pa_memblock_get_total());
207 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
209 pa_cli_command_stat(c, t, buf, fail, verbose);
210 pa_cli_command_modules(c, t, buf, fail, verbose);
211 pa_cli_command_sinks(c, t, buf, fail, verbose);
212 pa_cli_command_sources(c, t, buf, fail, verbose);
213 pa_cli_command_clients(c, t, buf, fail, verbose);
214 pa_cli_command_sink_inputs(c, t, buf, fail, verbose);
215 pa_cli_command_source_outputs(c, t, buf, fail, verbose);
216 pa_cli_command_scache_list(c, t, buf, fail, verbose);
220 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
226 if (!(name = pa_tokenizer_get(t, 1))) {
227 pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
231 if (!(m = pa_module_load(c, name, pa_tokenizer_get(t, 2)))) {
232 pa_strbuf_puts(buf, "Module load failed.\n");
237 snprintf(txt, sizeof(txt), "Module successfully loaded, index: %u.\n", m->index);
238 pa_strbuf_puts(buf, txt);
243 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
250 if (!(i = pa_tokenizer_get(t, 1))) {
251 pa_strbuf_puts(buf, "You need to specify the module index.\n");
255 index = (uint32_t) strtoul(i, &e, 10);
256 if (*e || !(m = pa_idxset_get_by_index(c->modules, index))) {
257 pa_strbuf_puts(buf, "Invalid module index.\n");
261 pa_module_unload_request(c, m);
265 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
268 struct pa_sink *sink;
271 if (!(n = pa_tokenizer_get(t, 1))) {
272 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
276 if (!(v = pa_tokenizer_get(t, 2))) {
277 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
281 volume = strtol(v, &x, 0);
282 if (!x || *x != 0 || volume < 0) {
283 pa_strbuf_puts(buf, "Failed to parse volume.\n");
287 if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK))) {
288 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
292 sink->volume = (uint32_t) volume;
296 static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
298 struct pa_sink_input *si;
303 if (!(n = pa_tokenizer_get(t, 1))) {
304 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
308 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
309 pa_strbuf_puts(buf, "Failed to parse index.\n");
313 if (!(v = pa_tokenizer_get(t, 2))) {
314 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
319 volume = strtol(v, &x, 0);
320 if (!x || *x != 0 || volume < 0) {
321 pa_strbuf_puts(buf, "Failed to parse volume.\n");
325 if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) index))) {
326 pa_strbuf_puts(buf, "No sink input found with this index.\n");
330 si->volume = (uint32_t) volume;
334 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
336 struct pa_sink *sink;
339 if (!(n = pa_tokenizer_get(t, 1))) {
340 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
344 if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK))) {
345 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
349 c->default_sink_index = sink->index;
353 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
355 struct pa_source *source;
358 if (!(n = pa_tokenizer_get(t, 1))) {
359 pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
363 if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE))) {
364 pa_strbuf_puts(buf, "No source found by this name or index.\n");
368 c->default_source_index = source->index;
372 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
374 struct pa_client *client;
378 if (!(n = pa_tokenizer_get(t, 1))) {
379 pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
383 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
384 pa_strbuf_puts(buf, "Failed to parse index.\n");
388 if (!(client = pa_idxset_get_by_index(c->clients, index))) {
389 pa_strbuf_puts(buf, "No client found by this index.\n");
393 pa_client_kill(client);
397 static int pa_cli_command_kill_sink_input(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
399 struct pa_sink_input *sink_input;
403 if (!(n = pa_tokenizer_get(t, 1))) {
404 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
408 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
409 pa_strbuf_puts(buf, "Failed to parse index.\n");
413 if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, index))) {
414 pa_strbuf_puts(buf, "No sink input found by this index.\n");
418 pa_sink_input_kill(sink_input);
422 static int pa_cli_command_kill_source_output(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
424 struct pa_source_output *source_output;
428 if (!(n = pa_tokenizer_get(t, 1))) {
429 pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
433 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
434 pa_strbuf_puts(buf, "Failed to parse index.\n");
438 if (!(source_output = pa_idxset_get_by_index(c->source_outputs, index))) {
439 pa_strbuf_puts(buf, "No source output found by this index.\n");
443 pa_source_output_kill(source_output);
447 static int pa_cli_command_scache_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
450 s = pa_scache_list_to_string(c);
452 pa_strbuf_puts(buf, s);
457 static int pa_cli_command_scache_play(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
458 const char *n, *sink_name;
459 struct pa_sink *sink;
460 assert(c && t && buf && fail && verbose);
462 if (!(n = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
463 pa_strbuf_puts(buf, "You need to specify a sample name and a sink name.\n");
467 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK))) {
468 pa_strbuf_puts(buf, "No sink by that name.\n");
472 if (pa_scache_play_item(c, n, sink, PA_VOLUME_NORM) < 0) {
473 pa_strbuf_puts(buf, "Failed to play sample.\n");
480 static int pa_cli_command_scache_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
482 assert(c && t && buf && fail && verbose);
484 if (!(n = pa_tokenizer_get(t, 1))) {
485 pa_strbuf_puts(buf, "You need to specify a sample name.\n");
489 if (pa_scache_remove_item(c, n) < 0) {
490 pa_strbuf_puts(buf, "Failed to remove sample.\n");
497 static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
498 const char *fname, *n;
499 struct pa_memchunk chunk;
500 struct pa_sample_spec ss;
501 assert(c && t && buf && fail && verbose);
503 if (!(fname = pa_tokenizer_get(t, 1)) || !(n = pa_tokenizer_get(t, 2))) {
504 pa_strbuf_puts(buf, "You need to specify a file name and a sample name.\n");
508 if (pa_sound_file_load(fname, &ss, &chunk) < 0) {
509 pa_strbuf_puts(buf, "Failed to load sound file.\n");
513 pa_scache_add_item(c, n, &ss, &chunk, NULL);
514 pa_memblock_unref(chunk.memblock);
518 static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
519 const char *fname, *sink_name;
520 struct pa_memchunk chunk;
521 struct pa_sample_spec ss;
522 struct pa_sink *sink;
524 assert(c && t && buf && fail && verbose);
526 if (!(fname = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
527 pa_strbuf_puts(buf, "You need to specify a file name and a sink name.\n");
531 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK))) {
532 pa_strbuf_puts(buf, "No sink by that name.\n");
536 if (pa_sound_file_load(fname, &ss, &chunk) < 0) {
537 pa_strbuf_puts(buf, "Failed to load sound file.\n");
541 ret = pa_play_memchunk(sink, fname, &ss, &chunk, PA_VOLUME_NORM);
542 pa_memblock_unref(chunk.memblock);
546 int pa_cli_command_execute_line(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
549 cs = s+strspn(s, whitespace);
551 if (*cs == '#' || !*cs)
553 else if (*cs == '.') {
554 static const char fail_meta[] = ".fail";
555 static const char nofail_meta[] = ".nofail";
556 static const char verbose_meta[] = ".verbose";
557 static const char noverbose_meta[] = ".noverbose";
559 if (!strcmp(cs, verbose_meta))
561 else if (!strcmp(cs, noverbose_meta))
563 else if (!strcmp(cs, fail_meta))
565 else if (!strcmp(cs, nofail_meta))
569 static const char include_meta[] = ".include";
570 l = strcspn(cs, whitespace);
572 if (l == sizeof(include_meta)-1 && !strncmp(cs, include_meta, l)) {
573 const char *filename = cs+l+strspn(cs+l, whitespace);
575 if (pa_cli_command_execute_file(c, filename, buf, fail, verbose) < 0)
576 if (*fail) return -1;
578 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
579 if (*fail) return -1;
583 const struct command*command;
587 l = strcspn(cs, whitespace);
589 for (command = commands; command->name; command++)
590 if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
592 struct pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
594 ret = command->proc(c, t, buf, fail, verbose);
595 pa_tokenizer_free(t);
598 if (ret < 0 && *fail)
605 pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
614 int pa_cli_command_execute_file(struct pa_core *c, const char *fn, struct pa_strbuf *buf, int *fail, int *verbose) {
618 assert(c && fn && buf);
620 if (!(f = fopen(fn, "r"))) {
621 pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, strerror(errno));
628 pa_strbuf_printf(buf, "Executing file: '%s'\n", fn);
630 while (fgets(line, sizeof(line), f)) {
631 char *e = line + strcspn(line, linebreak);
634 if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0 && *fail)
639 pa_strbuf_printf(buf, "Executed file: '%s'\n", fn);
650 int pa_cli_command_execute(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
652 assert(c && s && buf && fail && verbose);
656 size_t l = strcspn(p, linebreak);
657 char *line = strndup(p, l);
660 if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0&& *fail) {
667 p += strspn(p, linebreak);