]> code.delx.au - pulseaudio/blob - src/pulsecore/cli-command.c
f74258d3c70d4c3331b692b5718962bdd5cf64de
[pulseaudio] / src / pulsecore / cli-command.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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.
10
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.
15
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
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 <pulse/xmalloc.h>
33
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>
53
54 #include "cli-command.h"
55
56 struct command {
57 const char *name;
58 int (*proc) (pa_core *c, pa_tokenizer*t, pa_strbuf *buf, int *fail);
59 const char *help;
60 unsigned args;
61 };
62
63 #define INCLUDE_META ".include"
64 #define FAIL_META ".fail"
65 #define NOFAIL_META ".nofail"
66
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);
103
104 /* A method table for all available commands */
105
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 }
148 };
149
150 static const char whitespace[] = " \t\n\r";
151 static const char linebreak[] = "\n\r";
152
153 static uint32_t parse_index(const char *n) {
154 uint32_t idx;
155
156 if (pa_atou(n, &idx) < 0)
157 return (uint32_t) PA_IDXSET_INVALID;
158
159 return idx;
160 }
161
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);
165 return 0;
166 }
167
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);
171
172 pa_strbuf_puts(buf, "Available commands:\n");
173
174 for (command = commands; command->name; command++)
175 if (command->help)
176 pa_strbuf_printf(buf, " %-25s %s\n", command->name, command->help);
177 return 0;
178 }
179
180 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
181 char *s;
182 assert(c && t);
183 s = pa_module_list_to_string(c);
184 assert(s);
185 pa_strbuf_puts(buf, s);
186 pa_xfree(s);
187 return 0;
188 }
189
190 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
191 char *s;
192 assert(c && t);
193 s = pa_client_list_to_string(c);
194 assert(s);
195 pa_strbuf_puts(buf, s);
196 pa_xfree(s);
197 return 0;
198 }
199
200 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
201 char *s;
202 assert(c && t);
203 s = pa_sink_list_to_string(c);
204 assert(s);
205 pa_strbuf_puts(buf, s);
206 pa_xfree(s);
207 return 0;
208 }
209
210 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
211 char *s;
212 assert(c && t);
213 s = pa_source_list_to_string(c);
214 assert(s);
215 pa_strbuf_puts(buf, s);
216 pa_xfree(s);
217 return 0;
218 }
219
220 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
221 char *s;
222 assert(c && t);
223 s = pa_sink_input_list_to_string(c);
224 assert(s);
225 pa_strbuf_puts(buf, s);
226 pa_xfree(s);
227 return 0;
228 }
229
230 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
231 char *s;
232 assert(c && t);
233 s = pa_source_output_list_to_string(c);
234 assert(s);
235 pa_strbuf_puts(buf, s);
236 pa_xfree(s);
237 return 0;
238 }
239
240 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
241 char s[256];
242 assert(c && t);
243
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,
247 s);
248
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,
252 s);
253
254 pa_bytes_snprint(s, sizeof(s), pa_scache_total_size(c));
255 pa_strbuf_printf(buf, "Total sample cache size: %s.\n", s);
256
257 pa_sample_spec_snprint(s, sizeof(s), &c->default_sample_spec);
258 pa_strbuf_printf(buf, "Default sample spec: %s\n", s);
259
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));
264
265 return 0;
266 }
267
268 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
269 assert(c && t);
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);
279 return 0;
280 }
281
282 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
283 pa_module *m;
284 const char *name;
285 assert(c && t);
286
287 if (!(name = pa_tokenizer_get(t, 1))) {
288 pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
289 return -1;
290 }
291
292 if (!(m = pa_module_load(c, name, pa_tokenizer_get(t, 2)))) {
293 pa_strbuf_puts(buf, "Module load failed.\n");
294 return -1;
295 }
296
297 return 0;
298 }
299
300 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
301 pa_module *m;
302 uint32_t idx;
303 const char *i;
304 char *e;
305 assert(c && t);
306
307 if (!(i = pa_tokenizer_get(t, 1))) {
308 pa_strbuf_puts(buf, "You need to specify the module index.\n");
309 return -1;
310 }
311
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");
315 return -1;
316 }
317
318 pa_module_unload_request(m);
319 return 0;
320 }
321
322 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
323 const char *n, *v;
324 pa_sink *sink;
325 uint32_t volume;
326 pa_cvolume cvolume;
327
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");
330 return -1;
331 }
332
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");
335 return -1;
336 }
337
338 if (pa_atou(v, &volume) < 0) {
339 pa_strbuf_puts(buf, "Failed to parse volume.\n");
340 return -1;
341 }
342
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");
345 return -1;
346 }
347
348 pa_cvolume_set(&cvolume, sink->sample_spec.channels, volume);
349 pa_sink_set_volume(sink, PA_MIXER_HARDWARE, &cvolume);
350 return 0;
351 }
352
353 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
354 const char *n, *v;
355 pa_sink_input *si;
356 pa_volume_t volume;
357 pa_cvolume cvolume;
358 uint32_t idx;
359
360 if (!(n = pa_tokenizer_get(t, 1))) {
361 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
362 return -1;
363 }
364
365 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
366 pa_strbuf_puts(buf, "Failed to parse index.\n");
367 return -1;
368 }
369
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");
372 return -1;
373 }
374
375 if (pa_atou(v, &volume) < 0) {
376 pa_strbuf_puts(buf, "Failed to parse volume.\n");
377 return -1;
378 }
379
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");
382 return -1;
383 }
384
385 pa_cvolume_set(&cvolume, si->sample_spec.channels, volume);
386 pa_sink_input_set_volume(si, &cvolume);
387 return 0;
388 }
389
390 static int pa_cli_command_source_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
391 const char *n, *v;
392 pa_source *source;
393 uint32_t volume;
394 pa_cvolume cvolume;
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 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");
403 return -1;
404 }
405
406 if (pa_atou(v, &volume) < 0) {
407 pa_strbuf_puts(buf, "Failed to parse volume.\n");
408 return -1;
409 }
410
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");
413 return -1;
414 }
415
416 pa_cvolume_set(&cvolume, source->sample_spec.channels, volume);
417 pa_source_set_volume(source, PA_MIXER_HARDWARE, &cvolume);
418 return 0;
419 }
420
421 static int pa_cli_command_sink_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
422 const char *n, *m;
423 pa_sink *sink;
424 int mute;
425
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");
428 return -1;
429 }
430
431 if (!(m = pa_tokenizer_get(t, 2))) {
432 pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
433 return -1;
434 }
435
436 if (pa_atoi(m, &mute) < 0) {
437 pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
438 return -1;
439 }
440
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");
443 return -1;
444 }
445
446 pa_sink_set_mute(sink, PA_MIXER_HARDWARE, mute);
447 return 0;
448 }
449
450 static int pa_cli_command_source_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
451 const char *n, *m;
452 pa_source *source;
453 int mute;
454
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");
457 return -1;
458 }
459
460 if (!(m = pa_tokenizer_get(t, 2))) {
461 pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
462 return -1;
463 }
464
465 if (pa_atoi(m, &mute) < 0) {
466 pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
467 return -1;
468 }
469
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");
472 return -1;
473 }
474
475 pa_source_set_mute(source, PA_MIXER_HARDWARE, mute);
476 return 0;
477 }
478
479 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
480 const char *n;
481 assert(c && t);
482
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");
485 return -1;
486 }
487
488 pa_namereg_set_default(c, n, PA_NAMEREG_SINK);
489 return 0;
490 }
491
492 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
493 const char *n;
494 assert(c && t);
495
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");
498 return -1;
499 }
500
501 pa_namereg_set_default(c, n, PA_NAMEREG_SOURCE);
502 return 0;
503 }
504
505 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
506 const char *n;
507 pa_client *client;
508 uint32_t idx;
509 assert(c && t);
510
511 if (!(n = pa_tokenizer_get(t, 1))) {
512 pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
513 return -1;
514 }
515
516 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
517 pa_strbuf_puts(buf, "Failed to parse index.\n");
518 return -1;
519 }
520
521 if (!(client = pa_idxset_get_by_index(c->clients, idx))) {
522 pa_strbuf_puts(buf, "No client found by this index.\n");
523 return -1;
524 }
525
526 pa_client_kill(client);
527 return 0;
528 }
529
530 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
531 const char *n;
532 pa_sink_input *sink_input;
533 uint32_t idx;
534 assert(c && t);
535
536 if (!(n = pa_tokenizer_get(t, 1))) {
537 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
538 return -1;
539 }
540
541 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
542 pa_strbuf_puts(buf, "Failed to parse index.\n");
543 return -1;
544 }
545
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");
548 return -1;
549 }
550
551 pa_sink_input_kill(sink_input);
552 return 0;
553 }
554
555 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
556 const char *n;
557 pa_source_output *source_output;
558 uint32_t idx;
559 assert(c && t);
560
561 if (!(n = pa_tokenizer_get(t, 1))) {
562 pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
563 return -1;
564 }
565
566 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
567 pa_strbuf_puts(buf, "Failed to parse index.\n");
568 return -1;
569 }
570
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");
573 return -1;
574 }
575
576 pa_source_output_kill(source_output);
577 return 0;
578 }
579
580 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
581 char *s;
582 assert(c && t);
583 s = pa_scache_list_to_string(c);
584 assert(s);
585 pa_strbuf_puts(buf, s);
586 pa_xfree(s);
587 return 0;
588 }
589
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;
592 pa_sink *sink;
593 assert(c && t && buf && fail);
594
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");
597 return -1;
598 }
599
600 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
601 pa_strbuf_puts(buf, "No sink by that name.\n");
602 return -1;
603 }
604
605 if (pa_scache_play_item(c, n, sink, PA_VOLUME_NORM) < 0) {
606 pa_strbuf_puts(buf, "Failed to play sample.\n");
607 return -1;
608 }
609
610 return 0;
611 }
612
613 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
614 const char *n;
615 assert(c && t && buf && fail);
616
617 if (!(n = pa_tokenizer_get(t, 1))) {
618 pa_strbuf_puts(buf, "You need to specify a sample name.\n");
619 return -1;
620 }
621
622 if (pa_scache_remove_item(c, n) < 0) {
623 pa_strbuf_puts(buf, "Failed to remove sample.\n");
624 return -1;
625 }
626
627 return 0;
628 }
629
630 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
631 const char *fname, *n;
632 int r;
633 assert(c && t && buf && fail);
634
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");
637 return -1;
638 }
639
640 if (strstr(pa_tokenizer_get(t, 0), "lazy"))
641 r = pa_scache_add_file_lazy(c, n, fname, NULL);
642 else
643 r = pa_scache_add_file(c, n, fname, NULL);
644
645 if (r < 0)
646 pa_strbuf_puts(buf, "Failed to load sound file.\n");
647
648 return 0;
649 }
650
651 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
652 const char *pname;
653 assert(c && t && buf && fail);
654
655 if (!(pname = pa_tokenizer_get(t, 1))) {
656 pa_strbuf_puts(buf, "You need to specify a path name.\n");
657 return -1;
658 }
659
660 if (pa_scache_add_directory_lazy(c, pname) < 0) {
661 pa_strbuf_puts(buf, "Failed to load directory.\n");
662 return -1;
663 }
664
665 return 0;
666 }
667
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;
670 pa_sink *sink;
671 assert(c && t && buf && fail);
672
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");
675 return -1;
676 }
677
678 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
679 pa_strbuf_puts(buf, "No sink by that name.\n");
680 return -1;
681 }
682
683
684 return pa_play_file(sink, fname, NULL);
685 }
686
687 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
688 const char *a, *b;
689 assert(c && t && buf && fail);
690
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");
693 return -1;
694 }
695
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);
697
698 return 0;
699 }
700
701 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
702 const char *name;
703 assert(c && t && buf && fail);
704
705 if (!(name = pa_tokenizer_get(t, 1))) {
706 pa_strbuf_puts(buf, "You need to specify a device name\n");
707 return -1;
708 }
709
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");
712 return -1;
713 }
714
715 return 0;
716 }
717
718 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
719 char *s;
720 assert(c && t);
721 s = pa_autoload_list_to_string(c);
722 assert(s);
723 pa_strbuf_puts(buf, s);
724 pa_xfree(s);
725 return 0;
726 }
727
728 static int pa_cli_command_list_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
729 assert(c && t);
730 pa_property_dump(c, buf);
731 return 0;
732 }
733
734 static int pa_cli_command_move_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
735 const char *n, *k;
736 pa_sink_input *si;
737 pa_sink *sink;
738 uint32_t idx;
739
740 if (!(n = pa_tokenizer_get(t, 1))) {
741 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
742 return -1;
743 }
744
745 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
746 pa_strbuf_puts(buf, "Failed to parse index.\n");
747 return -1;
748 }
749
750 if (!(k = pa_tokenizer_get(t, 2))) {
751 pa_strbuf_puts(buf, "You need to specify a sink.\n");
752 return -1;
753 }
754
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");
757 return -1;
758 }
759
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");
762 return -1;
763 }
764
765 if (pa_sink_input_move_to(si, sink, 0) < 0) {
766 pa_strbuf_puts(buf, "Moved failed.\n");
767 return -1;
768 }
769 return 0;
770 }
771
772 static int pa_cli_command_move_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
773 const char *n, *k;
774 pa_source_output *so;
775 pa_source *source;
776 uint32_t idx;
777
778 if (!(n = pa_tokenizer_get(t, 1))) {
779 pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
780 return -1;
781 }
782
783 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
784 pa_strbuf_puts(buf, "Failed to parse index.\n");
785 return -1;
786 }
787
788 if (!(k = pa_tokenizer_get(t, 2))) {
789 pa_strbuf_puts(buf, "You need to specify a source.\n");
790 return -1;
791 }
792
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");
795 return -1;
796 }
797
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");
800 return -1;
801 }
802
803 if (pa_source_output_move_to(so, source) < 0) {
804 pa_strbuf_puts(buf, "Moved failed.\n");
805 return -1;
806 }
807 return 0;
808 }
809
810 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
811 pa_module *m;
812 pa_sink *sink;
813 pa_source *source;
814 int nl;
815 const char *p;
816 uint32_t idx;
817 char txt[256];
818 time_t now;
819 void *i;
820 pa_autoload_entry *a;
821
822 assert(c && t);
823
824 time(&now);
825
826 #ifdef HAVE_CTIME_R
827 pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime_r(&now, txt));
828 #else
829 pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime(&now));
830 #endif
831
832
833 for (m = pa_idxset_first(c->modules, &idx); m; m = pa_idxset_next(c->modules, &idx)) {
834 if (m->auto_unload)
835 continue;
836
837 pa_strbuf_printf(buf, "load-module %s", m->name);
838
839 if (m->argument)
840 pa_strbuf_printf(buf, " %s", m->argument);
841
842 pa_strbuf_puts(buf, "\n");
843 }
844
845 nl = 0;
846
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)
849 continue;
850
851 if (!nl) {
852 pa_strbuf_puts(buf, "\n");
853 nl = 1;
854 }
855
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));
858 }
859
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)
862 continue;
863
864 if (!nl) {
865 pa_strbuf_puts(buf, "\n");
866 nl = 1;
867 }
868
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));
871 }
872
873
874 if (c->autoload_hashmap) {
875 nl = 0;
876
877 i = NULL;
878 while ((a = pa_hashmap_iterate(c->autoload_hashmap, &i, NULL))) {
879
880 if (!nl) {
881 pa_strbuf_puts(buf, "\n");
882 nl = 1;
883 }
884
885 pa_strbuf_printf(buf, "add-autoload-%s %s %s", a->type == PA_NAMEREG_SINK ? "sink" : "source", a->name, a->module);
886
887 if (a->argument)
888 pa_strbuf_printf(buf, " %s", a->argument);
889
890 pa_strbuf_puts(buf, "\n");
891 }
892 }
893
894 nl = 0;
895
896 if ((p = pa_namereg_get_default_sink_name(c))) {
897 if (!nl) {
898 pa_strbuf_puts(buf, "\n");
899 nl = 1;
900 }
901 pa_strbuf_printf(buf, "set-default-sink %s\n", p);
902 }
903
904 if ((p = pa_namereg_get_default_source_name(c))) {
905 if (!nl) {
906 pa_strbuf_puts(buf, "\n");
907 nl = 1;
908 }
909 pa_strbuf_printf(buf, "set-default-source %s\n", p);
910 }
911
912 pa_strbuf_puts(buf, "\n### EOF\n");
913
914 return 0;
915 }
916
917 int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
918 const char *cs;
919
920 cs = s+strspn(s, whitespace);
921
922 if (*cs == '#' || !*cs)
923 return 0;
924 else if (*cs == '.') {
925 if (!strcmp(cs, FAIL_META))
926 *fail = 1;
927 else if (!strcmp(cs, NOFAIL_META))
928 *fail = 0;
929 else {
930 size_t l;
931 l = strcspn(cs, whitespace);
932
933 if (l == sizeof(INCLUDE_META)-1 && !strncmp(cs, INCLUDE_META, l)) {
934 const char *filename = cs+l+strspn(cs+l, whitespace);
935
936 if (pa_cli_command_execute_file(c, filename, buf, fail) < 0)
937 if (*fail) return -1;
938 } else {
939 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
940 if (*fail) return -1;
941 }
942 }
943 } else {
944 const struct command*command;
945 int unknown = 1;
946 size_t l;
947
948 l = strcspn(cs, whitespace);
949
950 for (command = commands; command->name; command++)
951 if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
952 int ret;
953 pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
954 assert(t);
955 ret = command->proc(c, t, buf, fail);
956 pa_tokenizer_free(t);
957 unknown = 0;
958
959 if (ret < 0 && *fail)
960 return -1;
961
962 break;
963 }
964
965 if (unknown) {
966 pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
967 if (*fail)
968 return -1;
969 }
970 }
971
972 return 0;
973 }
974
975 int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf, int *fail) {
976 char line[256];
977 FILE *f = NULL;
978 int ret = -1;
979 assert(c && fn && buf);
980
981 if (!(f = fopen(fn, "r"))) {
982 pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, pa_cstrerror(errno));
983 if (!*fail)
984 ret = 0;
985 goto fail;
986 }
987
988 while (fgets(line, sizeof(line), f)) {
989 char *e = line + strcspn(line, linebreak);
990 *e = 0;
991
992 if (pa_cli_command_execute_line(c, line, buf, fail) < 0 && *fail)
993 goto fail;
994 }
995
996 ret = 0;
997
998 fail:
999 if (f)
1000 fclose(f);
1001
1002 return ret;
1003 }
1004
1005 int pa_cli_command_execute(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
1006 const char *p;
1007 assert(c && s && buf && fail);
1008
1009 p = s;
1010 while (*p) {
1011 size_t l = strcspn(p, linebreak);
1012 char *line = pa_xstrndup(p, l);
1013
1014 if (pa_cli_command_execute_line(c, line, buf, fail) < 0&& *fail) {
1015 pa_xfree(line);
1016 return -1;
1017 }
1018 pa_xfree(line);
1019
1020 p += l;
1021 p += strspn(p, linebreak);
1022 }
1023
1024 return 0;
1025 }