]> code.delx.au - pulseaudio/blob - src/polypcore/cli-command.c
Cleaned up the includes after the restructuring. Indicate which headers are
[pulseaudio] / src / polypcore / cli-command.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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.
10
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.
15
16 You should have received a copy of the GNU Lesser 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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <errno.h>
31
32 #include <polypcore/module.h>
33 #include <polypcore/sink.h>
34 #include <polypcore/source.h>
35 #include <polypcore/client.h>
36 #include <polypcore/sink-input.h>
37 #include <polypcore/source-output.h>
38 #include <polypcore/tokenizer.h>
39 #include <polypcore/strbuf.h>
40 #include <polypcore/namereg.h>
41 #include <polypcore/cli-text.h>
42 #include <polypcore/core-scache.h>
43 #include <polypcore/sample-util.h>
44 #include <polypcore/sound-file.h>
45 #include <polypcore/play-memchunk.h>
46 #include <polypcore/autoload.h>
47 #include <polypcore/xmalloc.h>
48 #include <polypcore/sound-file-stream.h>
49 #include <polypcore/props.h>
50 #include <polypcore/util.h>
51
52 #include "cli-command.h"
53
54 struct command {
55 const char *name;
56 int (*proc) (pa_core *c, pa_tokenizer*t, pa_strbuf *buf, int *fail);
57 const char *help;
58 unsigned args;
59 };
60
61 #define INCLUDE_META ".include"
62 #define FAIL_META ".fail"
63 #define NOFAIL_META ".nofail"
64
65 /* Prototypes for all available commands */
66 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
67 static int pa_cli_command_help(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
68 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
69 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
70 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
71 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
72 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
73 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
74 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
75 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
76 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
77 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
78 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
79 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
80 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
81 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
82 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
83 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
84 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
85 static int pa_cli_command_scache_play(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
86 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
87 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
88 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
89 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
90 static int pa_cli_command_play_file(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
91 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
92 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
93 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
94 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
95 static int pa_cli_command_list_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
96
97
98 /* A method table for all available commands */
99
100 static const struct command commands[] = {
101 { "exit", pa_cli_command_exit, "Terminate the daemon", 1 },
102 { "help", pa_cli_command_help, "Show this help", 1 },
103 { "list-modules", pa_cli_command_modules, "List loaded modules", 1 },
104 { "list-sinks", pa_cli_command_sinks, "List loaded sinks", 1 },
105 { "list-sources", pa_cli_command_sources, "List loaded sources", 1 },
106 { "list-clients", pa_cli_command_clients, "List loaded clients", 1 },
107 { "list-sink-inputs", pa_cli_command_sink_inputs, "List sink inputs", 1 },
108 { "list-source-outputs", pa_cli_command_source_outputs, "List source outputs", 1 },
109 { "stat", pa_cli_command_stat, "Show memory block statistics", 1 },
110 { "info", pa_cli_command_info, "Show comprehensive status", 1 },
111 { "ls", pa_cli_command_info, NULL, 1 },
112 { "list", pa_cli_command_info, NULL, 1 },
113 { "load-module", pa_cli_command_load, "Load a module (args: name, arguments)", 3},
114 { "unload-module", pa_cli_command_unload, "Unload a module (args: index)", 2},
115 { "set-sink-volume", pa_cli_command_sink_volume, "Set the volume of a sink (args: index|name, volume)", 3},
116 { "set-sink-input-volume", pa_cli_command_sink_input_volume, "Set the volume of a sink input (args: index|name, volume)", 3},
117 { "set-default-sink", pa_cli_command_sink_default, "Set the default sink (args: index|name)", 2},
118 { "set-default-source", pa_cli_command_source_default, "Set the default source (args: index|name)", 2},
119 { "kill-client", pa_cli_command_kill_client, "Kill a client (args: index)", 2},
120 { "kill-sink-input", pa_cli_command_kill_sink_input, "Kill a sink input (args: index)", 2},
121 { "kill-source-output", pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
122 { "list-samples", pa_cli_command_scache_list, "List all entries in the sample cache", 1},
123 { "play-sample", pa_cli_command_scache_play, "Play a sample from the sample cache (args: name, sink|index)", 3},
124 { "remove-sample", pa_cli_command_scache_remove, "Remove a sample from the sample cache (args: name)", 2},
125 { "load-sample", pa_cli_command_scache_load, "Load a sound file into the sample cache (args: name, filename)", 3},
126 { "load-sample-lazy", pa_cli_command_scache_load, "Lazily load a sound file into the sample cache (args: name, filename)", 3},
127 { "load-sample-dir-lazy", pa_cli_command_scache_load_dir, "Lazily load all files in a directory into the sample cache (args: pathname)", 2},
128 { "play-file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3},
129 { "list-autoload", pa_cli_command_autoload_list, "List autoload entries", 1},
130 { "add-autoload-sink", pa_cli_command_autoload_add, "Add autoload entry for a sink (args: sink, module name, arguments)", 4},
131 { "add-autoload-source", pa_cli_command_autoload_add, "Add autoload entry for a source (args: source, module name, arguments)", 4},
132 { "remove-autoload-sink", pa_cli_command_autoload_remove, "Remove autoload entry for a sink (args: name)", 2},
133 { "remove-autoload-source", pa_cli_command_autoload_remove, "Remove autoload entry for a source (args: name)", 2},
134 { "dump", pa_cli_command_dump, "Dump daemon configuration", 1},
135 { "list-props", pa_cli_command_list_props, NULL, 1},
136 { NULL, NULL, NULL, 0 }
137 };
138
139 static const char whitespace[] = " \t\n\r";
140 static const char linebreak[] = "\n\r";
141
142 static uint32_t parse_index(const char *n) {
143 uint32_t idx;
144
145 if (pa_atou(n, &idx) < 0)
146 return (uint32_t) PA_IDXSET_INVALID;
147
148 return idx;
149 }
150
151 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, PA_GCC_UNUSED pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
152 assert(c && c->mainloop && t);
153 c->mainloop->quit(c->mainloop, 0);
154 return 0;
155 }
156
157 static int pa_cli_command_help(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
158 const struct command*command;
159 assert(c && t && buf);
160
161 pa_strbuf_puts(buf, "Available commands:\n");
162
163 for (command = commands; command->name; command++)
164 if (command->help)
165 pa_strbuf_printf(buf, " %-25s %s\n", command->name, command->help);
166 return 0;
167 }
168
169 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
170 char *s;
171 assert(c && t);
172 s = pa_module_list_to_string(c);
173 assert(s);
174 pa_strbuf_puts(buf, s);
175 pa_xfree(s);
176 return 0;
177 }
178
179 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
180 char *s;
181 assert(c && t);
182 s = pa_client_list_to_string(c);
183 assert(s);
184 pa_strbuf_puts(buf, s);
185 pa_xfree(s);
186 return 0;
187 }
188
189 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
190 char *s;
191 assert(c && t);
192 s = pa_sink_list_to_string(c);
193 assert(s);
194 pa_strbuf_puts(buf, s);
195 pa_xfree(s);
196 return 0;
197 }
198
199 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
200 char *s;
201 assert(c && t);
202 s = pa_source_list_to_string(c);
203 assert(s);
204 pa_strbuf_puts(buf, s);
205 pa_xfree(s);
206 return 0;
207 }
208
209 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
210 char *s;
211 assert(c && t);
212 s = pa_sink_input_list_to_string(c);
213 assert(s);
214 pa_strbuf_puts(buf, s);
215 pa_xfree(s);
216 return 0;
217 }
218
219 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
220 char *s;
221 assert(c && t);
222 s = pa_source_output_list_to_string(c);
223 assert(s);
224 pa_strbuf_puts(buf, s);
225 pa_xfree(s);
226 return 0;
227 }
228
229 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
230 char s[256];
231 assert(c && t);
232
233 pa_bytes_snprint(s, sizeof(s), c->memblock_stat->total_size);
234 pa_strbuf_printf(buf, "Memory blocks currently allocated: %u, size: %s.\n",
235 c->memblock_stat->total,
236 s);
237
238 pa_bytes_snprint(s, sizeof(s), c->memblock_stat->allocated_size);
239 pa_strbuf_printf(buf, "Memory blocks allocated during the whole lifetime: %u, size: %s.\n",
240 c->memblock_stat->allocated,
241 s);
242
243 pa_bytes_snprint(s, sizeof(s), pa_scache_total_size(c));
244 pa_strbuf_printf(buf, "Total sample cache size: %s.\n", s);
245
246 pa_sample_spec_snprint(s, sizeof(s), &c->default_sample_spec);
247 pa_strbuf_printf(buf, "Default sample spec: %s\n", s);
248
249 pa_strbuf_printf(buf, "Default sink name: %s\n"
250 "Default source name: %s\n",
251 pa_namereg_get_default_sink_name(c),
252 pa_namereg_get_default_source_name(c));
253
254 return 0;
255 }
256
257 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
258 assert(c && t);
259 pa_cli_command_stat(c, t, buf, fail);
260 pa_cli_command_modules(c, t, buf, fail);
261 pa_cli_command_sinks(c, t, buf, fail);
262 pa_cli_command_sources(c, t, buf, fail);
263 pa_cli_command_clients(c, t, buf, fail);
264 pa_cli_command_sink_inputs(c, t, buf, fail);
265 pa_cli_command_source_outputs(c, t, buf, fail);
266 pa_cli_command_scache_list(c, t, buf, fail);
267 pa_cli_command_autoload_list(c, t, buf, fail);
268 return 0;
269 }
270
271 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
272 pa_module *m;
273 const char *name;
274 assert(c && t);
275
276 if (!(name = pa_tokenizer_get(t, 1))) {
277 pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
278 return -1;
279 }
280
281 if (!(m = pa_module_load(c, name, pa_tokenizer_get(t, 2)))) {
282 pa_strbuf_puts(buf, "Module load failed.\n");
283 return -1;
284 }
285
286 return 0;
287 }
288
289 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
290 pa_module *m;
291 uint32_t idx;
292 const char *i;
293 char *e;
294 assert(c && t);
295
296 if (!(i = pa_tokenizer_get(t, 1))) {
297 pa_strbuf_puts(buf, "You need to specify the module index.\n");
298 return -1;
299 }
300
301 idx = (uint32_t) strtoul(i, &e, 10);
302 if (*e || !(m = pa_idxset_get_by_index(c->modules, idx))) {
303 pa_strbuf_puts(buf, "Invalid module index.\n");
304 return -1;
305 }
306
307 pa_module_unload_request(m);
308 return 0;
309 }
310
311 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
312 const char *n, *v;
313 pa_sink *sink;
314 uint32_t volume;
315 pa_cvolume cvolume;
316
317 if (!(n = pa_tokenizer_get(t, 1))) {
318 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
319 return -1;
320 }
321
322 if (!(v = pa_tokenizer_get(t, 2))) {
323 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
324 return -1;
325 }
326
327 if (pa_atou(v, &volume) < 0) {
328 pa_strbuf_puts(buf, "Failed to parse volume.\n");
329 return -1;
330 }
331
332 if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
333 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
334 return -1;
335 }
336
337 pa_cvolume_set(&cvolume, sink->sample_spec.channels, volume);
338 pa_sink_set_volume(sink, PA_MIXER_HARDWARE, &cvolume);
339 return 0;
340 }
341
342 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
343 const char *n, *v;
344 pa_sink_input *si;
345 pa_volume_t volume;
346 pa_cvolume cvolume;
347 uint32_t idx;
348
349 if (!(n = pa_tokenizer_get(t, 1))) {
350 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
351 return -1;
352 }
353
354 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
355 pa_strbuf_puts(buf, "Failed to parse index.\n");
356 return -1;
357 }
358
359 if (!(v = pa_tokenizer_get(t, 2))) {
360 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
361 return -1;
362 }
363
364 if (pa_atou(v, &volume) < 0) {
365 pa_strbuf_puts(buf, "Failed to parse volume.\n");
366 return -1;
367 }
368
369 if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) idx))) {
370 pa_strbuf_puts(buf, "No sink input found with this index.\n");
371 return -1;
372 }
373
374 pa_cvolume_set(&cvolume, si->sample_spec.channels, volume);
375 pa_sink_input_set_volume(si, &cvolume);
376 return 0;
377 }
378
379 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
380 const char *n;
381 assert(c && t);
382
383 if (!(n = pa_tokenizer_get(t, 1))) {
384 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
385 return -1;
386 }
387
388 pa_namereg_set_default(c, n, PA_NAMEREG_SINK);
389 return 0;
390 }
391
392 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
393 const char *n;
394 assert(c && t);
395
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");
398 return -1;
399 }
400
401 pa_namereg_set_default(c, n, PA_NAMEREG_SOURCE);
402 return 0;
403 }
404
405 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
406 const char *n;
407 pa_client *client;
408 uint32_t idx;
409 assert(c && t);
410
411 if (!(n = pa_tokenizer_get(t, 1))) {
412 pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
413 return -1;
414 }
415
416 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
417 pa_strbuf_puts(buf, "Failed to parse index.\n");
418 return -1;
419 }
420
421 if (!(client = pa_idxset_get_by_index(c->clients, idx))) {
422 pa_strbuf_puts(buf, "No client found by this index.\n");
423 return -1;
424 }
425
426 pa_client_kill(client);
427 return 0;
428 }
429
430 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
431 const char *n;
432 pa_sink_input *sink_input;
433 uint32_t idx;
434 assert(c && t);
435
436 if (!(n = pa_tokenizer_get(t, 1))) {
437 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
438 return -1;
439 }
440
441 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
442 pa_strbuf_puts(buf, "Failed to parse index.\n");
443 return -1;
444 }
445
446 if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx))) {
447 pa_strbuf_puts(buf, "No sink input found by this index.\n");
448 return -1;
449 }
450
451 pa_sink_input_kill(sink_input);
452 return 0;
453 }
454
455 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
456 const char *n;
457 pa_source_output *source_output;
458 uint32_t idx;
459 assert(c && t);
460
461 if (!(n = pa_tokenizer_get(t, 1))) {
462 pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
463 return -1;
464 }
465
466 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
467 pa_strbuf_puts(buf, "Failed to parse index.\n");
468 return -1;
469 }
470
471 if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx))) {
472 pa_strbuf_puts(buf, "No source output found by this index.\n");
473 return -1;
474 }
475
476 pa_source_output_kill(source_output);
477 return 0;
478 }
479
480 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
481 char *s;
482 assert(c && t);
483 s = pa_scache_list_to_string(c);
484 assert(s);
485 pa_strbuf_puts(buf, s);
486 pa_xfree(s);
487 return 0;
488 }
489
490 static int pa_cli_command_scache_play(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
491 const char *n, *sink_name;
492 pa_sink *sink;
493 assert(c && t && buf && fail);
494
495 if (!(n = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
496 pa_strbuf_puts(buf, "You need to specify a sample name and a sink name.\n");
497 return -1;
498 }
499
500 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
501 pa_strbuf_puts(buf, "No sink by that name.\n");
502 return -1;
503 }
504
505 if (pa_scache_play_item(c, n, sink, NULL) < 0) {
506 pa_strbuf_puts(buf, "Failed to play sample.\n");
507 return -1;
508 }
509
510 return 0;
511 }
512
513 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
514 const char *n;
515 assert(c && t && buf && fail);
516
517 if (!(n = pa_tokenizer_get(t, 1))) {
518 pa_strbuf_puts(buf, "You need to specify a sample name.\n");
519 return -1;
520 }
521
522 if (pa_scache_remove_item(c, n) < 0) {
523 pa_strbuf_puts(buf, "Failed to remove sample.\n");
524 return -1;
525 }
526
527 return 0;
528 }
529
530 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
531 const char *fname, *n;
532 int r;
533 assert(c && t && buf && fail);
534
535 if (!(fname = pa_tokenizer_get(t, 2)) || !(n = pa_tokenizer_get(t, 1))) {
536 pa_strbuf_puts(buf, "You need to specify a file name and a sample name.\n");
537 return -1;
538 }
539
540 if (strstr(pa_tokenizer_get(t, 0), "lazy"))
541 r = pa_scache_add_file_lazy(c, n, fname, NULL);
542 else
543 r = pa_scache_add_file(c, n, fname, NULL);
544
545 if (r < 0)
546 pa_strbuf_puts(buf, "Failed to load sound file.\n");
547
548 return 0;
549 }
550
551 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
552 const char *pname;
553 assert(c && t && buf && fail);
554
555 if (!(pname = pa_tokenizer_get(t, 1))) {
556 pa_strbuf_puts(buf, "You need to specify a path name.\n");
557 return -1;
558 }
559
560 if (pa_scache_add_directory_lazy(c, pname) < 0) {
561 pa_strbuf_puts(buf, "Failed to load directory.\n");
562 return -1;
563 }
564
565 return 0;
566 }
567
568 static int pa_cli_command_play_file(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
569 const char *fname, *sink_name;
570 pa_sink *sink;
571 assert(c && t && buf && fail);
572
573 if (!(fname = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
574 pa_strbuf_puts(buf, "You need to specify a file name and a sink name.\n");
575 return -1;
576 }
577
578 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
579 pa_strbuf_puts(buf, "No sink by that name.\n");
580 return -1;
581 }
582
583
584 return pa_play_file(sink, fname, NULL);
585 }
586
587 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
588 const char *a, *b;
589 assert(c && t && buf && fail);
590
591 if (!(a = pa_tokenizer_get(t, 1)) || !(b = pa_tokenizer_get(t, 2))) {
592 pa_strbuf_puts(buf, "You need to specify a device name, a filename or a module name and optionally module arguments\n");
593 return -1;
594 }
595
596 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);
597
598 return 0;
599 }
600
601 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
602 const char *name;
603 assert(c && t && buf && fail);
604
605 if (!(name = pa_tokenizer_get(t, 1))) {
606 pa_strbuf_puts(buf, "You need to specify a device name\n");
607 return -1;
608 }
609
610 if (pa_autoload_remove_by_name(c, name, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
611 pa_strbuf_puts(buf, "Failed to remove autload entry\n");
612 return -1;
613 }
614
615 return 0;
616 }
617
618 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
619 char *s;
620 assert(c && t);
621 s = pa_autoload_list_to_string(c);
622 assert(s);
623 pa_strbuf_puts(buf, s);
624 pa_xfree(s);
625 return 0;
626 }
627
628 static int pa_cli_command_list_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
629 assert(c && t);
630 pa_property_dump(c, buf);
631 return 0;
632 }
633
634 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
635 pa_module *m;
636 pa_sink *s;
637 int nl;
638 const char *p;
639 uint32_t idx;
640 char txt[256];
641 time_t now;
642 void *i;
643 pa_autoload_entry *a;
644
645 assert(c && t);
646
647 time(&now);
648
649 #ifdef HAVE_CTIME_R
650 pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime_r(&now, txt));
651 #else
652 pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime(&now));
653 #endif
654
655
656 for (m = pa_idxset_first(c->modules, &idx); m; m = pa_idxset_next(c->modules, &idx)) {
657 if (m->auto_unload)
658 continue;
659
660 pa_strbuf_printf(buf, "load-module %s", m->name);
661
662 if (m->argument)
663 pa_strbuf_printf(buf, " %s", m->argument);
664
665 pa_strbuf_puts(buf, "\n");
666 }
667
668 nl = 0;
669
670 for (s = pa_idxset_first(c->sinks, &idx); s; s = pa_idxset_next(c->sinks, &idx)) {
671 if (s->owner && s->owner->auto_unload)
672 continue;
673
674 if (!nl) {
675 pa_strbuf_puts(buf, "\n");
676 nl = 1;
677 }
678
679 pa_strbuf_printf(buf, "set-sink-volume %s 0x%03x\n", s->name, pa_cvolume_avg(pa_sink_get_volume(s, PA_MIXER_HARDWARE)));
680 }
681
682
683 if (c->autoload_hashmap) {
684 nl = 0;
685
686 i = NULL;
687 while ((a = pa_hashmap_iterate(c->autoload_hashmap, &i, NULL))) {
688
689 if (!nl) {
690 pa_strbuf_puts(buf, "\n");
691 nl = 1;
692 }
693
694 pa_strbuf_printf(buf, "add-autoload-%s %s %s", a->type == PA_NAMEREG_SINK ? "sink" : "source", a->name, a->module);
695
696 if (a->argument)
697 pa_strbuf_printf(buf, " %s", a->argument);
698
699 pa_strbuf_puts(buf, "\n");
700 }
701 }
702
703 nl = 0;
704
705 if ((p = pa_namereg_get_default_sink_name(c))) {
706 if (!nl) {
707 pa_strbuf_puts(buf, "\n");
708 nl = 1;
709 }
710 pa_strbuf_printf(buf, "set-default-sink %s\n", p);
711 }
712
713 if ((p = pa_namereg_get_default_source_name(c))) {
714 if (!nl) {
715 pa_strbuf_puts(buf, "\n");
716 nl = 1;
717 }
718 pa_strbuf_printf(buf, "set-default-source %s\n", p);
719 }
720
721 pa_strbuf_puts(buf, "\n### EOF\n");
722
723 return 0;
724 }
725
726
727 int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
728 const char *cs;
729
730 cs = s+strspn(s, whitespace);
731
732 if (*cs == '#' || !*cs)
733 return 0;
734 else if (*cs == '.') {
735 if (!strcmp(cs, FAIL_META))
736 *fail = 1;
737 else if (!strcmp(cs, NOFAIL_META))
738 *fail = 0;
739 else {
740 size_t l;
741 l = strcspn(cs, whitespace);
742
743 if (l == sizeof(INCLUDE_META)-1 && !strncmp(cs, INCLUDE_META, l)) {
744 const char *filename = cs+l+strspn(cs+l, whitespace);
745
746 if (pa_cli_command_execute_file(c, filename, buf, fail) < 0)
747 if (*fail) return -1;
748 } else {
749 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
750 if (*fail) return -1;
751 }
752 }
753 } else {
754 const struct command*command;
755 int unknown = 1;
756 size_t l;
757
758 l = strcspn(cs, whitespace);
759
760 for (command = commands; command->name; command++)
761 if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
762 int ret;
763 pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
764 assert(t);
765 ret = command->proc(c, t, buf, fail);
766 pa_tokenizer_free(t);
767 unknown = 0;
768
769 if (ret < 0 && *fail)
770 return -1;
771
772 break;
773 }
774
775 if (unknown) {
776 pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
777 if (*fail)
778 return -1;
779 }
780 }
781
782 return 0;
783 }
784
785 int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf, int *fail) {
786 char line[256];
787 FILE *f = NULL;
788 int ret = -1;
789 assert(c && fn && buf);
790
791 if (!(f = fopen(fn, "r"))) {
792 pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, strerror(errno));
793 if (!*fail)
794 ret = 0;
795 goto fail;
796 }
797
798 while (fgets(line, sizeof(line), f)) {
799 char *e = line + strcspn(line, linebreak);
800 *e = 0;
801
802 if (pa_cli_command_execute_line(c, line, buf, fail) < 0 && *fail)
803 goto fail;
804 }
805
806 ret = 0;
807
808 fail:
809 if (f)
810 fclose(f);
811
812 return ret;
813 }
814
815 int pa_cli_command_execute(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
816 const char *p;
817 assert(c && s && buf && fail);
818
819 p = s;
820 while (*p) {
821 size_t l = strcspn(p, linebreak);
822 char *line = pa_xstrndup(p, l);
823
824 if (pa_cli_command_execute_line(c, line, buf, fail) < 0&& *fail) {
825 pa_xfree(line);
826 return -1;
827 }
828 pa_xfree(line);
829
830 p += l;
831 p += strspn(p, linebreak);
832 }
833
834 return 0;
835 }