]> code.delx.au - pulseaudio/blob - src/pulsecore/cli-command.c
Merge branch 'master' of git://git.0pointer.de/pulseaudio
[pulseaudio] / src / pulsecore / cli-command.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <ltdl.h>
33
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/module.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/source.h>
39 #include <pulsecore/client.h>
40 #include <pulsecore/sink-input.h>
41 #include <pulsecore/source-output.h>
42 #include <pulsecore/tokenizer.h>
43 #include <pulsecore/strbuf.h>
44 #include <pulsecore/namereg.h>
45 #include <pulsecore/cli-text.h>
46 #include <pulsecore/core-scache.h>
47 #include <pulsecore/sample-util.h>
48 #include <pulsecore/sound-file.h>
49 #include <pulsecore/play-memchunk.h>
50 #include <pulsecore/autoload.h>
51 #include <pulsecore/sound-file-stream.h>
52 #include <pulsecore/shared.h>
53 #include <pulsecore/core-util.h>
54 #include <pulsecore/core-error.h>
55
56 #include "cli-command.h"
57
58 struct command {
59 const char *name;
60 int (*proc) (pa_core *c, pa_tokenizer*t, pa_strbuf *buf, pa_bool_t *fail);
61 const char *help;
62 unsigned args;
63 };
64
65 #define META_INCLUDE ".include"
66 #define META_FAIL ".fail"
67 #define META_NOFAIL ".nofail"
68 #define META_IFEXISTS ".ifexists"
69 #define META_ELSE ".else"
70 #define META_ENDIF ".endif"
71
72 enum {
73 IFSTATE_NONE = -1,
74 IFSTATE_FALSE = 0,
75 IFSTATE_TRUE = 1,
76 };
77
78 /* Prototypes for all available commands */
79 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
80 static int pa_cli_command_help(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
81 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
82 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
83 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
84 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
85 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
86 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
87 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
88 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
89 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
90 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
91 static int pa_cli_command_describe(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
92 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
93 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
94 static int pa_cli_command_source_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
95 static int pa_cli_command_sink_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
96 static int pa_cli_command_source_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
97 static int pa_cli_command_sink_input_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
98 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
99 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
100 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
101 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
102 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
103 static int pa_cli_command_scache_play(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
104 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
105 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
106 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
107 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
108 static int pa_cli_command_play_file(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
109 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
110 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
111 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
112 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
113 static int pa_cli_command_list_shared_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
114 static int pa_cli_command_move_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
115 static int pa_cli_command_move_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
116 static int pa_cli_command_vacuum(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
117 static int pa_cli_command_suspend_sink(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
118 static int pa_cli_command_suspend_source(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
119 static int pa_cli_command_suspend(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail);
120
121 /* A method table for all available commands */
122
123 static const struct command commands[] = {
124 { "exit", pa_cli_command_exit, "Terminate the daemon", 1 },
125 { "help", pa_cli_command_help, "Show this help", 1 },
126 { "list-modules", pa_cli_command_modules, "List loaded modules", 1 },
127 { "list-sinks", pa_cli_command_sinks, "List loaded sinks", 1 },
128 { "list-sources", pa_cli_command_sources, "List loaded sources", 1 },
129 { "list-clients", pa_cli_command_clients, "List loaded clients", 1 },
130 { "list-sink-inputs", pa_cli_command_sink_inputs, "List sink inputs", 1 },
131 { "list-source-outputs", pa_cli_command_source_outputs, "List source outputs", 1 },
132 { "stat", pa_cli_command_stat, "Show memory block statistics", 1 },
133 { "info", pa_cli_command_info, "Show comprehensive status", 1 },
134 { "ls", pa_cli_command_info, NULL, 1 },
135 { "list", pa_cli_command_info, NULL, 1 },
136 { "load-module", pa_cli_command_load, "Load a module (args: name, arguments)", 3},
137 { "unload-module", pa_cli_command_unload, "Unload a module (args: index)", 2},
138 { "describe-module", pa_cli_command_describe, "Describe a module (arg: name)", 2},
139 { "set-sink-volume", pa_cli_command_sink_volume, "Set the volume of a sink (args: index|name, volume)", 3},
140 { "set-sink-input-volume", pa_cli_command_sink_input_volume, "Set the volume of a sink input (args: index, volume)", 3},
141 { "set-source-volume", pa_cli_command_source_volume, "Set the volume of a source (args: index|name, volume)", 3},
142 { "set-sink-mute", pa_cli_command_sink_mute, "Set the mute switch of a sink (args: index|name, bool)", 3},
143 { "set-sink-input-mute", pa_cli_command_sink_input_mute, "Set the mute switch of a sink input (args: index, bool)", 3},
144 { "set-source-mute", pa_cli_command_source_mute, "Set the mute switch of a source (args: index|name, bool)", 3},
145 { "set-default-sink", pa_cli_command_sink_default, "Set the default sink (args: index|name)", 2},
146 { "set-default-source", pa_cli_command_source_default, "Set the default source (args: index|name)", 2},
147 { "kill-client", pa_cli_command_kill_client, "Kill a client (args: index)", 2},
148 { "kill-sink-input", pa_cli_command_kill_sink_input, "Kill a sink input (args: index)", 2},
149 { "kill-source-output", pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
150 { "list-samples", pa_cli_command_scache_list, "List all entries in the sample cache", 1},
151 { "play-sample", pa_cli_command_scache_play, "Play a sample from the sample cache (args: name, sink|index)", 3},
152 { "remove-sample", pa_cli_command_scache_remove, "Remove a sample from the sample cache (args: name)", 2},
153 { "load-sample", pa_cli_command_scache_load, "Load a sound file into the sample cache (args: name, filename)", 3},
154 { "load-sample-lazy", pa_cli_command_scache_load, "Lazily load a sound file into the sample cache (args: name, filename)", 3},
155 { "load-sample-dir-lazy", pa_cli_command_scache_load_dir, "Lazily load all files in a directory into the sample cache (args: pathname)", 2},
156 { "play-file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3},
157 { "list-autoload", pa_cli_command_autoload_list, "List autoload entries", 1},
158 { "add-autoload-sink", pa_cli_command_autoload_add, NULL /*"Add autoload entry for a sink (args: sink, module name, arguments)"*/, 4},
159 { "add-autoload-source", pa_cli_command_autoload_add, NULL /*"Add autoload entry for a source (args: source, module name, arguments)"*/, 4},
160 { "remove-autoload-sink", pa_cli_command_autoload_remove, NULL /*"Remove autoload entry for a sink (args: name)"*/, 2},
161 { "remove-autoload-source", pa_cli_command_autoload_remove, NULL /*"Remove autoload entry for a source (args: name)"*/, 2},
162 { "dump", pa_cli_command_dump, "Dump daemon configuration", 1},
163 { "shared", pa_cli_command_list_shared_props, NULL, 1},
164 { "move-sink-input", pa_cli_command_move_sink_input, "Move sink input to another sink (args: index, sink)", 3},
165 { "move-source-output", pa_cli_command_move_source_output, "Move source output to another source (args: index, source)", 3},
166 { "vacuum", pa_cli_command_vacuum, NULL, 1},
167 { "suspend-sink", pa_cli_command_suspend_sink, "Suspend sink (args: index|name, bool)", 3},
168 { "suspend-source", pa_cli_command_suspend_source, "Suspend source (args: index|name, bool)", 3},
169 { "suspend", pa_cli_command_suspend, "Suspend all sinks and all sources (args: bool)", 2},
170 { NULL, NULL, NULL, 0 }
171 };
172
173 static const char whitespace[] = " \t\n\r";
174 static const char linebreak[] = "\n\r";
175
176 static uint32_t parse_index(const char *n) {
177 uint32_t idx;
178
179 if (pa_atou(n, &idx) < 0)
180 return (uint32_t) PA_IDXSET_INVALID;
181
182 return idx;
183 }
184
185 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
186 pa_core_assert_ref(c);
187 pa_assert(t);
188 pa_assert(buf);
189 pa_assert(fail);
190
191 if (pa_core_exit(c, FALSE, 0) < 0)
192 pa_strbuf_puts(buf, "Not allowed to terminate daemon.\n");
193
194 return 0;
195 }
196
197 static int pa_cli_command_help(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
198 const struct command*command;
199
200 pa_core_assert_ref(c);
201 pa_assert(t);
202 pa_assert(buf);
203 pa_assert(fail);
204
205 pa_strbuf_puts(buf, "Available commands:\n");
206
207 for (command = commands; command->name; command++)
208 if (command->help)
209 pa_strbuf_printf(buf, " %-25s %s\n", command->name, command->help);
210 return 0;
211 }
212
213 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
214 char *s;
215
216 pa_core_assert_ref(c);
217 pa_assert(t);
218 pa_assert(buf);
219 pa_assert(fail);
220
221 pa_assert_se(s = pa_module_list_to_string(c));
222 pa_strbuf_puts(buf, s);
223 pa_xfree(s);
224 return 0;
225 }
226
227 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
228 char *s;
229
230 pa_core_assert_ref(c);
231 pa_assert(t);
232 pa_assert(buf);
233 pa_assert(fail);
234
235 pa_assert_se(s = pa_client_list_to_string(c));
236 pa_strbuf_puts(buf, s);
237 pa_xfree(s);
238 return 0;
239 }
240
241 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
242 char *s;
243
244 pa_core_assert_ref(c);
245 pa_assert(t);
246 pa_assert(buf);
247 pa_assert(fail);
248
249 pa_assert_se(s = pa_sink_list_to_string(c));
250 pa_strbuf_puts(buf, s);
251 pa_xfree(s);
252 return 0;
253 }
254
255 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
256 char *s;
257
258 pa_core_assert_ref(c);
259 pa_assert(t);
260 pa_assert(buf);
261 pa_assert(fail);
262
263 pa_assert_se(s = pa_source_list_to_string(c));
264 pa_strbuf_puts(buf, s);
265 pa_xfree(s);
266 return 0;
267 }
268
269 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
270 char *s;
271
272 pa_core_assert_ref(c);
273 pa_assert(t);
274 pa_assert(buf);
275 pa_assert(fail);
276
277 pa_assert_se(s = pa_sink_input_list_to_string(c));
278 pa_strbuf_puts(buf, s);
279 pa_xfree(s);
280 return 0;
281 }
282
283 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
284 char *s;
285
286 pa_core_assert_ref(c);
287 pa_assert(t);
288 pa_assert(buf);
289 pa_assert(fail);
290
291 pa_assert_se(s = pa_source_output_list_to_string(c));
292 pa_strbuf_puts(buf, s);
293 pa_xfree(s);
294 return 0;
295 }
296
297 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
298 char s[256];
299 const pa_mempool_stat *stat;
300 unsigned k;
301 const char *def_sink, *def_source;
302
303 static const char* const type_table[PA_MEMBLOCK_TYPE_MAX] = {
304 [PA_MEMBLOCK_POOL] = "POOL",
305 [PA_MEMBLOCK_POOL_EXTERNAL] = "POOL_EXTERNAL",
306 [PA_MEMBLOCK_APPENDED] = "APPENDED",
307 [PA_MEMBLOCK_USER] = "USER",
308 [PA_MEMBLOCK_FIXED] = "FIXED",
309 [PA_MEMBLOCK_IMPORTED] = "IMPORTED",
310 };
311
312 pa_core_assert_ref(c);
313 pa_assert(t);
314 pa_assert(buf);
315 pa_assert(fail);
316
317 stat = pa_mempool_get_stat(c->mempool);
318
319 pa_strbuf_printf(buf, "Memory blocks currently allocated: %u, size: %s.\n",
320 (unsigned) pa_atomic_load(&stat->n_allocated),
321 pa_bytes_snprint(s, sizeof(s), (unsigned) pa_atomic_load(&stat->allocated_size)));
322
323 pa_strbuf_printf(buf, "Memory blocks allocated during the whole lifetime: %u, size: %s.\n",
324 (unsigned) pa_atomic_load(&stat->n_accumulated),
325 pa_bytes_snprint(s, sizeof(s), (unsigned) pa_atomic_load(&stat->accumulated_size)));
326
327 pa_strbuf_printf(buf, "Memory blocks imported from other processes: %u, size: %s.\n",
328 (unsigned) pa_atomic_load(&stat->n_imported),
329 pa_bytes_snprint(s, sizeof(s), (unsigned) pa_atomic_load(&stat->imported_size)));
330
331 pa_strbuf_printf(buf, "Memory blocks exported to other processes: %u, size: %s.\n",
332 (unsigned) pa_atomic_load(&stat->n_exported),
333 pa_bytes_snprint(s, sizeof(s), (unsigned) pa_atomic_load(&stat->exported_size)));
334
335 pa_strbuf_printf(buf, "Total sample cache size: %s.\n",
336 pa_bytes_snprint(s, sizeof(s), (unsigned) pa_scache_total_size(c)));
337
338 pa_strbuf_printf(buf, "Default sample spec: %s\n",
339 pa_sample_spec_snprint(s, sizeof(s), &c->default_sample_spec));
340
341 def_sink = pa_namereg_get_default_sink_name(c);
342 def_source = pa_namereg_get_default_source_name(c);
343 pa_strbuf_printf(buf, "Default sink name: %s\n"
344 "Default source name: %s\n",
345 def_sink ? def_sink : "none",
346 def_source ? def_source : "none");
347
348 for (k = 0; k < PA_MEMBLOCK_TYPE_MAX; k++)
349 pa_strbuf_printf(buf,
350 "Memory blocks of type %s: %u allocated/%u accumulated.\n",
351 type_table[k],
352 (unsigned) pa_atomic_load(&stat->n_allocated_by_type[k]),
353 (unsigned) pa_atomic_load(&stat->n_accumulated_by_type[k]));
354
355 return 0;
356 }
357
358 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
359 pa_core_assert_ref(c);
360 pa_assert(t);
361 pa_assert(buf);
362 pa_assert(fail);
363
364 pa_cli_command_stat(c, t, buf, fail);
365 pa_cli_command_modules(c, t, buf, fail);
366 pa_cli_command_sinks(c, t, buf, fail);
367 pa_cli_command_sources(c, t, buf, fail);
368 pa_cli_command_clients(c, t, buf, fail);
369 pa_cli_command_sink_inputs(c, t, buf, fail);
370 pa_cli_command_source_outputs(c, t, buf, fail);
371 pa_cli_command_scache_list(c, t, buf, fail);
372 /* pa_cli_command_autoload_list(c, t, buf, fail); */
373 return 0;
374 }
375
376 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
377 pa_module *m;
378 const char *name;
379
380 pa_core_assert_ref(c);
381 pa_assert(t);
382 pa_assert(buf);
383 pa_assert(fail);
384
385 if (!(name = pa_tokenizer_get(t, 1))) {
386 pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
387 return -1;
388 }
389
390 if (!(m = pa_module_load(c, name, pa_tokenizer_get(t, 2)))) {
391 pa_strbuf_puts(buf, "Module load failed.\n");
392 return -1;
393 }
394
395 return 0;
396 }
397
398 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
399 pa_module *m;
400 uint32_t idx;
401 const char *i;
402 char *e;
403
404 pa_core_assert_ref(c);
405 pa_assert(t);
406 pa_assert(buf);
407 pa_assert(fail);
408
409 if (!(i = pa_tokenizer_get(t, 1))) {
410 pa_strbuf_puts(buf, "You need to specify the module index.\n");
411 return -1;
412 }
413
414 idx = (uint32_t) strtoul(i, &e, 10);
415 if (*e || !(m = pa_idxset_get_by_index(c->modules, idx))) {
416 pa_strbuf_puts(buf, "Invalid module index.\n");
417 return -1;
418 }
419
420 pa_module_unload_request(m, FALSE);
421 return 0;
422 }
423
424 static int pa_cli_command_describe(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
425 const char *name;
426 pa_modinfo *i;
427
428 pa_core_assert_ref(c);
429 pa_assert(t);
430 pa_assert(buf);
431 pa_assert(fail);
432
433 if (!(name = pa_tokenizer_get(t, 1))) {
434 pa_strbuf_puts(buf, "You need to specify the module name.\n");
435 return -1;
436 }
437
438 if ((i = pa_modinfo_get_by_name(name))) {
439
440 pa_strbuf_printf(buf, "Name: %s\n", name);
441
442 if (!i->description && !i->version && !i->author && !i->usage)
443 pa_strbuf_printf(buf, "No module information available\n");
444 else {
445 if (i->version)
446 pa_strbuf_printf(buf, "Version: %s\n", i->version);
447 if (i->description)
448 pa_strbuf_printf(buf, "Description: %s\n", i->description);
449 if (i->author)
450 pa_strbuf_printf(buf, "Author: %s\n", i->author);
451 if (i->usage)
452 pa_strbuf_printf(buf, "Usage: %s\n", i->usage);
453 pa_strbuf_printf(buf, "Load Once: %s\n", pa_yes_no(i->load_once));
454 }
455
456 pa_modinfo_free(i);
457 } else
458 pa_strbuf_puts(buf, "Failed to open module.\n");
459
460 return 0;
461 }
462
463 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
464 const char *n, *v;
465 pa_sink *sink;
466 uint32_t volume;
467 pa_cvolume cvolume;
468
469 pa_core_assert_ref(c);
470 pa_assert(t);
471 pa_assert(buf);
472 pa_assert(fail);
473
474 if (!(n = pa_tokenizer_get(t, 1))) {
475 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
476 return -1;
477 }
478
479 if (!(v = pa_tokenizer_get(t, 2))) {
480 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x10000 is normal volume)\n");
481 return -1;
482 }
483
484 if (pa_atou(v, &volume) < 0) {
485 pa_strbuf_puts(buf, "Failed to parse volume.\n");
486 return -1;
487 }
488
489 if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
490 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
491 return -1;
492 }
493
494 pa_cvolume_set(&cvolume, sink->sample_spec.channels, volume);
495 pa_sink_set_volume(sink, &cvolume);
496 return 0;
497 }
498
499 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
500 const char *n, *v;
501 pa_sink_input *si;
502 pa_volume_t volume;
503 pa_cvolume cvolume;
504 uint32_t idx;
505
506 pa_core_assert_ref(c);
507 pa_assert(t);
508 pa_assert(buf);
509 pa_assert(fail);
510
511 if (!(n = pa_tokenizer_get(t, 1))) {
512 pa_strbuf_puts(buf, "You need to specify a sink input 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 (!(v = pa_tokenizer_get(t, 2))) {
522 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x10000 is normal volume)\n");
523 return -1;
524 }
525
526 if (pa_atou(v, &volume) < 0) {
527 pa_strbuf_puts(buf, "Failed to parse volume.\n");
528 return -1;
529 }
530
531 if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) idx))) {
532 pa_strbuf_puts(buf, "No sink input found with this index.\n");
533 return -1;
534 }
535
536 pa_cvolume_set(&cvolume, si->sample_spec.channels, volume);
537 pa_sink_input_set_volume(si, &cvolume);
538 return 0;
539 }
540
541 static int pa_cli_command_source_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
542 const char *n, *v;
543 pa_source *source;
544 uint32_t volume;
545 pa_cvolume cvolume;
546
547 pa_core_assert_ref(c);
548 pa_assert(t);
549 pa_assert(buf);
550 pa_assert(fail);
551
552 if (!(n = pa_tokenizer_get(t, 1))) {
553 pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
554 return -1;
555 }
556
557 if (!(v = pa_tokenizer_get(t, 2))) {
558 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x10000 is normal volume)\n");
559 return -1;
560 }
561
562 if (pa_atou(v, &volume) < 0) {
563 pa_strbuf_puts(buf, "Failed to parse volume.\n");
564 return -1;
565 }
566
567 if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE, 1))) {
568 pa_strbuf_puts(buf, "No source found by this name or index.\n");
569 return -1;
570 }
571
572 pa_cvolume_set(&cvolume, source->sample_spec.channels, volume);
573 pa_source_set_volume(source, &cvolume);
574 return 0;
575 }
576
577 static int pa_cli_command_sink_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
578 const char *n, *m;
579 pa_sink *sink;
580 int mute;
581
582 pa_core_assert_ref(c);
583 pa_assert(t);
584 pa_assert(buf);
585 pa_assert(fail);
586
587 if (!(n = pa_tokenizer_get(t, 1))) {
588 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
589 return -1;
590 }
591
592 if (!(m = pa_tokenizer_get(t, 2))) {
593 pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
594 return -1;
595 }
596
597 if ((mute = pa_parse_boolean(m)) < 0) {
598 pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
599 return -1;
600 }
601
602 if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
603 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
604 return -1;
605 }
606
607 pa_sink_set_mute(sink, mute);
608 return 0;
609 }
610
611 static int pa_cli_command_source_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
612 const char *n, *m;
613 pa_source *source;
614 int mute;
615
616 pa_core_assert_ref(c);
617 pa_assert(t);
618 pa_assert(buf);
619 pa_assert(fail);
620
621 if (!(n = pa_tokenizer_get(t, 1))) {
622 pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
623 return -1;
624 }
625
626 if (!(m = pa_tokenizer_get(t, 2))) {
627 pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
628 return -1;
629 }
630
631 if ((mute = pa_parse_boolean(m)) < 0) {
632 pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
633 return -1;
634 }
635
636 if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE, 1))) {
637 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
638 return -1;
639 }
640
641 pa_source_set_mute(source, mute);
642 return 0;
643 }
644
645 static int pa_cli_command_sink_input_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
646 const char *n, *v;
647 pa_sink_input *si;
648 uint32_t idx;
649 int mute;
650
651 pa_core_assert_ref(c);
652 pa_assert(t);
653 pa_assert(buf);
654 pa_assert(fail);
655
656 if (!(n = pa_tokenizer_get(t, 1))) {
657 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
658 return -1;
659 }
660
661 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
662 pa_strbuf_puts(buf, "Failed to parse index.\n");
663 return -1;
664 }
665
666 if (!(v = pa_tokenizer_get(t, 2))) {
667 pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
668 return -1;
669 }
670
671 if ((mute = pa_parse_boolean(v)) < 0) {
672 pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
673 return -1;
674 }
675
676 if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) idx))) {
677 pa_strbuf_puts(buf, "No sink input found with this index.\n");
678 return -1;
679 }
680
681 pa_sink_input_set_mute(si, mute);
682 return 0;
683 }
684
685 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
686 const char *n;
687
688 pa_core_assert_ref(c);
689 pa_assert(t);
690 pa_assert(buf);
691 pa_assert(fail);
692
693 if (!(n = pa_tokenizer_get(t, 1))) {
694 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
695 return -1;
696 }
697
698 pa_namereg_set_default(c, n, PA_NAMEREG_SINK);
699 return 0;
700 }
701
702 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
703 const char *n;
704
705 pa_core_assert_ref(c);
706 pa_assert(t);
707 pa_assert(buf);
708 pa_assert(fail);
709
710 if (!(n = pa_tokenizer_get(t, 1))) {
711 pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
712 return -1;
713 }
714
715 pa_namereg_set_default(c, n, PA_NAMEREG_SOURCE);
716 return 0;
717 }
718
719 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
720 const char *n;
721 pa_client *client;
722 uint32_t idx;
723
724 pa_core_assert_ref(c);
725 pa_assert(t);
726 pa_assert(buf);
727 pa_assert(fail);
728
729 if (!(n = pa_tokenizer_get(t, 1))) {
730 pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
731 return -1;
732 }
733
734 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
735 pa_strbuf_puts(buf, "Failed to parse index.\n");
736 return -1;
737 }
738
739 if (!(client = pa_idxset_get_by_index(c->clients, idx))) {
740 pa_strbuf_puts(buf, "No client found by this index.\n");
741 return -1;
742 }
743
744 pa_client_kill(client);
745 return 0;
746 }
747
748 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
749 const char *n;
750 pa_sink_input *sink_input;
751 uint32_t idx;
752
753 pa_core_assert_ref(c);
754 pa_assert(t);
755 pa_assert(buf);
756 pa_assert(fail);
757
758 if (!(n = pa_tokenizer_get(t, 1))) {
759 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
760 return -1;
761 }
762
763 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
764 pa_strbuf_puts(buf, "Failed to parse index.\n");
765 return -1;
766 }
767
768 if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx))) {
769 pa_strbuf_puts(buf, "No sink input found by this index.\n");
770 return -1;
771 }
772
773 pa_sink_input_kill(sink_input);
774 return 0;
775 }
776
777 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
778 const char *n;
779 pa_source_output *source_output;
780 uint32_t idx;
781
782 pa_core_assert_ref(c);
783 pa_assert(t);
784 pa_assert(buf);
785 pa_assert(fail);
786
787 if (!(n = pa_tokenizer_get(t, 1))) {
788 pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
789 return -1;
790 }
791
792 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
793 pa_strbuf_puts(buf, "Failed to parse index.\n");
794 return -1;
795 }
796
797 if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx))) {
798 pa_strbuf_puts(buf, "No source output found by this index.\n");
799 return -1;
800 }
801
802 pa_source_output_kill(source_output);
803 return 0;
804 }
805
806 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
807 char *s;
808
809 pa_core_assert_ref(c);
810 pa_assert(t);
811 pa_assert(buf);
812 pa_assert(fail);
813
814 pa_assert_se(s = pa_scache_list_to_string(c));
815 pa_strbuf_puts(buf, s);
816 pa_xfree(s);
817
818 return 0;
819 }
820
821 static int pa_cli_command_scache_play(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
822 const char *n, *sink_name;
823 pa_sink *sink;
824 uint32_t idx;
825
826 pa_core_assert_ref(c);
827 pa_assert(t);
828 pa_assert(buf);
829 pa_assert(fail);
830
831 if (!(n = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
832 pa_strbuf_puts(buf, "You need to specify a sample name and a sink name.\n");
833 return -1;
834 }
835
836 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
837 pa_strbuf_puts(buf, "No sink by that name.\n");
838 return -1;
839 }
840
841 if (pa_scache_play_item(c, n, sink, PA_VOLUME_NORM, NULL, &idx) < 0) {
842 pa_strbuf_puts(buf, "Failed to play sample.\n");
843 return -1;
844 }
845
846 pa_strbuf_printf(buf, "Playing on sink input #%i\n", idx);
847
848 return 0;
849 }
850
851 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
852 const char *n;
853
854 pa_core_assert_ref(c);
855 pa_assert(t);
856 pa_assert(buf);
857 pa_assert(fail);
858
859 if (!(n = pa_tokenizer_get(t, 1))) {
860 pa_strbuf_puts(buf, "You need to specify a sample name.\n");
861 return -1;
862 }
863
864 if (pa_scache_remove_item(c, n) < 0) {
865 pa_strbuf_puts(buf, "Failed to remove sample.\n");
866 return -1;
867 }
868
869 return 0;
870 }
871
872 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
873 const char *fname, *n;
874 int r;
875
876 pa_core_assert_ref(c);
877 pa_assert(t);
878 pa_assert(buf);
879 pa_assert(fail);
880
881 if (!(fname = pa_tokenizer_get(t, 2)) || !(n = pa_tokenizer_get(t, 1))) {
882 pa_strbuf_puts(buf, "You need to specify a file name and a sample name.\n");
883 return -1;
884 }
885
886 if (strstr(pa_tokenizer_get(t, 0), "lazy"))
887 r = pa_scache_add_file_lazy(c, n, fname, NULL);
888 else
889 r = pa_scache_add_file(c, n, fname, NULL);
890
891 if (r < 0)
892 pa_strbuf_puts(buf, "Failed to load sound file.\n");
893
894 return 0;
895 }
896
897 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
898 const char *pname;
899
900 pa_core_assert_ref(c);
901 pa_assert(t);
902 pa_assert(buf);
903 pa_assert(fail);
904
905 if (!(pname = pa_tokenizer_get(t, 1))) {
906 pa_strbuf_puts(buf, "You need to specify a path name.\n");
907 return -1;
908 }
909
910 if (pa_scache_add_directory_lazy(c, pname) < 0) {
911 pa_strbuf_puts(buf, "Failed to load directory.\n");
912 return -1;
913 }
914
915 return 0;
916 }
917
918 static int pa_cli_command_play_file(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
919 const char *fname, *sink_name;
920 pa_sink *sink;
921
922 pa_core_assert_ref(c);
923 pa_assert(t);
924 pa_assert(buf);
925 pa_assert(fail);
926
927 if (!(fname = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
928 pa_strbuf_puts(buf, "You need to specify a file name and a sink name.\n");
929 return -1;
930 }
931
932 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
933 pa_strbuf_puts(buf, "No sink by that name.\n");
934 return -1;
935 }
936
937
938 return pa_play_file(sink, fname, NULL);
939 }
940
941 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
942 const char *a, *b;
943
944 pa_core_assert_ref(c);
945 pa_assert(t);
946 pa_assert(buf);
947 pa_assert(fail);
948
949 pa_log_warn("Autoload will no longer be implemented by future versions of the PulseAudio server.");
950
951 if (!(a = pa_tokenizer_get(t, 1)) || !(b = pa_tokenizer_get(t, 2))) {
952 pa_strbuf_puts(buf, "You need to specify a device name, a filename or a module name and optionally module arguments\n");
953 return -1;
954 }
955
956 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);
957
958 return 0;
959 }
960
961 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
962 const char *name;
963
964 pa_core_assert_ref(c);
965 pa_assert(t);
966 pa_assert(buf);
967 pa_assert(fail);
968
969 pa_log_warn("Autoload will no longer be implemented by future versions of the PulseAudio server.");
970
971 if (!(name = pa_tokenizer_get(t, 1))) {
972 pa_strbuf_puts(buf, "You need to specify a device name\n");
973 return -1;
974 }
975
976 if (pa_autoload_remove_by_name(c, name, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
977 pa_strbuf_puts(buf, "Failed to remove autload entry\n");
978 return -1;
979 }
980
981 return 0;
982 }
983
984 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
985 char *s;
986
987 pa_core_assert_ref(c);
988 pa_assert(t);
989 pa_assert(buf);
990 pa_assert(fail);
991
992 pa_log_warn("Autoload will no longer be implemented by future versions of the PulseAudio server.");
993
994 pa_assert_se(s = pa_autoload_list_to_string(c));
995 pa_strbuf_puts(buf, s);
996 pa_xfree(s);
997
998 return 0;
999 }
1000
1001 static int pa_cli_command_list_shared_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
1002 pa_core_assert_ref(c);
1003 pa_assert(t);
1004 pa_assert(buf);
1005 pa_assert(fail);
1006
1007 pa_shared_dump(c, buf);
1008 return 0;
1009 }
1010
1011 static int pa_cli_command_vacuum(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
1012 pa_core_assert_ref(c);
1013 pa_assert(t);
1014 pa_assert(buf);
1015 pa_assert(fail);
1016
1017 pa_mempool_vacuum(c->mempool);
1018
1019 return 0;
1020 }
1021
1022 static int pa_cli_command_move_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
1023 const char *n, *k;
1024 pa_sink_input *si;
1025 pa_sink *sink;
1026 uint32_t idx;
1027
1028 pa_core_assert_ref(c);
1029 pa_assert(t);
1030 pa_assert(buf);
1031 pa_assert(fail);
1032
1033 if (!(n = pa_tokenizer_get(t, 1))) {
1034 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
1035 return -1;
1036 }
1037
1038 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
1039 pa_strbuf_puts(buf, "Failed to parse index.\n");
1040 return -1;
1041 }
1042
1043 if (!(k = pa_tokenizer_get(t, 2))) {
1044 pa_strbuf_puts(buf, "You need to specify a sink.\n");
1045 return -1;
1046 }
1047
1048 if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) idx))) {
1049 pa_strbuf_puts(buf, "No sink input found with this index.\n");
1050 return -1;
1051 }
1052
1053 if (!(sink = pa_namereg_get(c, k, PA_NAMEREG_SINK, 1))) {
1054 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
1055 return -1;
1056 }
1057
1058 if (pa_sink_input_move_to(si, sink) < 0) {
1059 pa_strbuf_puts(buf, "Moved failed.\n");
1060 return -1;
1061 }
1062 return 0;
1063 }
1064
1065 static int pa_cli_command_move_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
1066 const char *n, *k;
1067 pa_source_output *so;
1068 pa_source *source;
1069 uint32_t idx;
1070
1071 pa_core_assert_ref(c);
1072 pa_assert(t);
1073 pa_assert(buf);
1074 pa_assert(fail);
1075
1076 if (!(n = pa_tokenizer_get(t, 1))) {
1077 pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
1078 return -1;
1079 }
1080
1081 if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
1082 pa_strbuf_puts(buf, "Failed to parse index.\n");
1083 return -1;
1084 }
1085
1086 if (!(k = pa_tokenizer_get(t, 2))) {
1087 pa_strbuf_puts(buf, "You need to specify a source.\n");
1088 return -1;
1089 }
1090
1091 if (!(so = pa_idxset_get_by_index(c->source_outputs, (uint32_t) idx))) {
1092 pa_strbuf_puts(buf, "No source output found with this index.\n");
1093 return -1;
1094 }
1095
1096 if (!(source = pa_namereg_get(c, k, PA_NAMEREG_SOURCE, 1))) {
1097 pa_strbuf_puts(buf, "No source found by this name or index.\n");
1098 return -1;
1099 }
1100
1101 if (pa_source_output_move_to(so, source) < 0) {
1102 pa_strbuf_puts(buf, "Moved failed.\n");
1103 return -1;
1104 }
1105 return 0;
1106 }
1107
1108 static int pa_cli_command_suspend_sink(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
1109 const char *n, *m;
1110 pa_sink *sink;
1111 int suspend;
1112
1113 pa_core_assert_ref(c);
1114 pa_assert(t);
1115 pa_assert(buf);
1116 pa_assert(fail);
1117
1118 if (!(n = pa_tokenizer_get(t, 1))) {
1119 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
1120 return -1;
1121 }
1122
1123 if (!(m = pa_tokenizer_get(t, 2))) {
1124 pa_strbuf_puts(buf, "You need to specify a suspend switch setting (0/1).\n");
1125 return -1;
1126 }
1127
1128 if ((suspend = pa_parse_boolean(m)) < 0) {
1129 pa_strbuf_puts(buf, "Failed to parse suspend switch.\n");
1130 return -1;
1131 }
1132
1133 if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
1134 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
1135 return -1;
1136 }
1137
1138 pa_sink_suspend(sink, suspend);
1139 return 0;
1140 }
1141
1142 static int pa_cli_command_suspend_source(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
1143 const char *n, *m;
1144 pa_source *source;
1145 int suspend;
1146
1147 pa_core_assert_ref(c);
1148 pa_assert(t);
1149 pa_assert(buf);
1150 pa_assert(fail);
1151
1152 if (!(n = pa_tokenizer_get(t, 1))) {
1153 pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
1154 return -1;
1155 }
1156
1157 if (!(m = pa_tokenizer_get(t, 2))) {
1158 pa_strbuf_puts(buf, "You need to specify a suspend switch setting (0/1).\n");
1159 return -1;
1160 }
1161
1162 if ((suspend = pa_parse_boolean(m)) < 0) {
1163 pa_strbuf_puts(buf, "Failed to parse suspend switch.\n");
1164 return -1;
1165 }
1166
1167 if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE, 1))) {
1168 pa_strbuf_puts(buf, "No source found by this name or index.\n");
1169 return -1;
1170 }
1171
1172 pa_source_suspend(source, suspend);
1173 return 0;
1174 }
1175
1176 static int pa_cli_command_suspend(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
1177 const char *m;
1178 int suspend;
1179 int ret;
1180
1181 pa_core_assert_ref(c);
1182 pa_assert(t);
1183 pa_assert(buf);
1184 pa_assert(fail);
1185
1186 if (!(m = pa_tokenizer_get(t, 1))) {
1187 pa_strbuf_puts(buf, "You need to specify a suspend switch setting (0/1).\n");
1188 return -1;
1189 }
1190
1191 if ((suspend = pa_parse_boolean(m)) < 0) {
1192 pa_strbuf_puts(buf, "Failed to parse suspend switch.\n");
1193 return -1;
1194 }
1195
1196 ret = - (pa_sink_suspend_all(c, suspend) < 0);
1197 if (pa_source_suspend_all(c, suspend) < 0)
1198 ret = -1;
1199
1200 if (ret < 0)
1201 pa_strbuf_puts(buf, "Failed to resume/suspend all sinks/sources.\n");
1202
1203 return 0;
1204 }
1205
1206 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
1207 pa_module *m;
1208 pa_sink *sink;
1209 pa_source *source;
1210 int nl;
1211 const char *p;
1212 uint32_t idx;
1213 char txt[256];
1214 time_t now;
1215 void *i;
1216 pa_autoload_entry *a;
1217
1218 pa_core_assert_ref(c);
1219 pa_assert(t);
1220 pa_assert(buf);
1221 pa_assert(fail);
1222
1223 time(&now);
1224
1225 #ifdef HAVE_CTIME_R
1226 pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime_r(&now, txt));
1227 #else
1228 pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime(&now));
1229 #endif
1230
1231 for (m = pa_idxset_first(c->modules, &idx); m; m = pa_idxset_next(c->modules, &idx)) {
1232 if (m->auto_unload)
1233 continue;
1234
1235 pa_strbuf_printf(buf, "load-module %s", m->name);
1236
1237 if (m->argument)
1238 pa_strbuf_printf(buf, " %s", m->argument);
1239
1240 pa_strbuf_puts(buf, "\n");
1241 }
1242
1243 nl = 0;
1244
1245 for (sink = pa_idxset_first(c->sinks, &idx); sink; sink = pa_idxset_next(c->sinks, &idx)) {
1246 if (sink->module && sink->module->auto_unload)
1247 continue;
1248
1249 if (!nl) {
1250 pa_strbuf_puts(buf, "\n");
1251 nl = 1;
1252 }
1253
1254 pa_strbuf_printf(buf, "set-sink-volume %s 0x%03x\n", sink->name, pa_cvolume_avg(pa_sink_get_volume(sink, FALSE)));
1255 pa_strbuf_printf(buf, "set-sink-mute %s %s\n", sink->name, pa_yes_no(pa_sink_get_mute(sink, FALSE)));
1256 pa_strbuf_printf(buf, "suspend-sink %s %s\n", sink->name, pa_yes_no(pa_sink_get_state(sink) == PA_SINK_SUSPENDED));
1257 }
1258
1259 for (source = pa_idxset_first(c->sources, &idx); source; source = pa_idxset_next(c->sources, &idx)) {
1260 if (source->module && source->module->auto_unload)
1261 continue;
1262
1263 if (!nl) {
1264 pa_strbuf_puts(buf, "\n");
1265 nl = 1;
1266 }
1267
1268 pa_strbuf_printf(buf, "set-source-volume %s 0x%03x\n", source->name, pa_cvolume_avg(pa_source_get_volume(source, FALSE)));
1269 pa_strbuf_printf(buf, "set-source-mute %s %s\n", source->name, pa_yes_no(pa_source_get_mute(source, FALSE)));
1270 pa_strbuf_printf(buf, "suspend-source %s %s\n", source->name, pa_yes_no(pa_source_get_state(source) == PA_SOURCE_SUSPENDED));
1271 }
1272
1273
1274 if (c->autoload_hashmap) {
1275 nl = 0;
1276
1277 i = NULL;
1278 while ((a = pa_hashmap_iterate(c->autoload_hashmap, &i, NULL))) {
1279
1280 if (!nl) {
1281 pa_strbuf_puts(buf, "\n");
1282 nl = 1;
1283 }
1284
1285 pa_strbuf_printf(buf, "add-autoload-%s %s %s", a->type == PA_NAMEREG_SINK ? "sink" : "source", a->name, a->module);
1286
1287 if (a->argument)
1288 pa_strbuf_printf(buf, " %s", a->argument);
1289
1290 pa_strbuf_puts(buf, "\n");
1291 }
1292 }
1293
1294 nl = 0;
1295
1296 if ((p = pa_namereg_get_default_sink_name(c))) {
1297 if (!nl) {
1298 pa_strbuf_puts(buf, "\n");
1299 nl = 1;
1300 }
1301 pa_strbuf_printf(buf, "set-default-sink %s\n", p);
1302 }
1303
1304 if ((p = pa_namereg_get_default_source_name(c))) {
1305 if (!nl) {
1306 pa_strbuf_puts(buf, "\n");
1307 nl = 1;
1308 }
1309 pa_strbuf_printf(buf, "set-default-source %s\n", p);
1310 }
1311
1312 pa_strbuf_puts(buf, "\n### EOF\n");
1313
1314 return 0;
1315 }
1316
1317 int pa_cli_command_execute_line_stateful(pa_core *c, const char *s, pa_strbuf *buf, pa_bool_t *fail, int *ifstate) {
1318 const char *cs;
1319
1320 pa_assert(c);
1321 pa_assert(s);
1322 pa_assert(buf);
1323
1324 cs = s+strspn(s, whitespace);
1325
1326 if (*cs == '#' || !*cs)
1327 return 0;
1328 else if (*cs == '.') {
1329 if (!strcmp(cs, META_ELSE)) {
1330 if (!ifstate || *ifstate == IFSTATE_NONE) {
1331 pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
1332 return -1;
1333 } else if (*ifstate == IFSTATE_TRUE)
1334 *ifstate = IFSTATE_FALSE;
1335 else
1336 *ifstate = IFSTATE_TRUE;
1337 return 0;
1338 } else if (!strcmp(cs, META_ENDIF)) {
1339 if (!ifstate || *ifstate == IFSTATE_NONE) {
1340 pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
1341 return -1;
1342 } else
1343 *ifstate = IFSTATE_NONE;
1344 return 0;
1345 }
1346 if (ifstate && *ifstate == IFSTATE_FALSE)
1347 return 0;
1348 if (!strcmp(cs, META_FAIL))
1349 *fail = TRUE;
1350 else if (!strcmp(cs, META_NOFAIL))
1351 *fail = FALSE;
1352 else {
1353 size_t l;
1354 l = strcspn(cs, whitespace);
1355
1356 if (l == sizeof(META_INCLUDE)-1 && !strncmp(cs, META_INCLUDE, l)) {
1357 const char *filename = cs+l+strspn(cs+l, whitespace);
1358 if (pa_cli_command_execute_file(c, filename, buf, fail) < 0)
1359 if (*fail)
1360 return -1;
1361 } else if (l == sizeof(META_IFEXISTS)-1 && !strncmp(cs, META_IFEXISTS, l)) {
1362 if (!ifstate) {
1363 pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
1364 return -1;
1365 } else if (*ifstate != IFSTATE_NONE) {
1366 pa_strbuf_printf(buf, "Nested %s commands not supported\n", cs);
1367 return -1;
1368 } else {
1369 const char *filename = cs+l+strspn(cs+l, whitespace);
1370
1371 /* Search DL_SEARCH_PATH unless the filename is absolute */
1372 if (filename[0] == PA_PATH_SEP_CHAR) {
1373
1374 *ifstate = access(filename, F_OK) == 0 ? IFSTATE_TRUE : IFSTATE_FALSE;
1375 pa_log_debug("Checking for existance of '%s': %s", filename, *ifstate == IFSTATE_TRUE ? "success" : "failure");
1376
1377 } else {
1378 const char *paths, *state = NULL;
1379 char *p;
1380
1381 if (!(paths = lt_dlgetsearchpath()))
1382 return -1;
1383
1384 while ((p = pa_split(paths, ":", &state))) {
1385 char *pathname;
1386
1387 pathname = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", p, filename);
1388 pa_xfree(p);
1389
1390 *ifstate = access(pathname, F_OK) == 0 ? IFSTATE_TRUE : IFSTATE_FALSE;
1391 pa_log_debug("Checking for existance of '%s': %s", pathname, *ifstate == IFSTATE_TRUE ? "success" : "failure");
1392
1393 pa_xfree(pathname);
1394
1395 if (*ifstate == IFSTATE_TRUE)
1396 break;
1397 }
1398 }
1399
1400 }
1401 } else {
1402 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
1403 if (*fail) return -1;
1404 }
1405 }
1406 } else {
1407 const struct command*command;
1408 int unknown = 1;
1409 size_t l;
1410
1411 if (ifstate && *ifstate == IFSTATE_FALSE)
1412 return 0;
1413
1414 l = strcspn(cs, whitespace);
1415
1416 for (command = commands; command->name; command++)
1417 if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
1418 int ret;
1419 pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
1420 pa_assert(t);
1421 ret = command->proc(c, t, buf, fail);
1422 pa_tokenizer_free(t);
1423 unknown = 0;
1424
1425 if (ret < 0 && *fail)
1426 return -1;
1427
1428 break;
1429 }
1430
1431 if (unknown) {
1432 pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
1433 if (*fail)
1434 return -1;
1435 }
1436 }
1437
1438 return 0;
1439 }
1440
1441 int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf, pa_bool_t *fail) {
1442 return pa_cli_command_execute_line_stateful(c, s, buf, fail, NULL);
1443 }
1444
1445 int pa_cli_command_execute_file_stream(pa_core *c, FILE *f, pa_strbuf *buf, pa_bool_t *fail) {
1446 char line[1024];
1447 int ifstate = IFSTATE_NONE;
1448 int ret = -1;
1449 pa_bool_t _fail = TRUE;
1450
1451 pa_assert(c);
1452 pa_assert(f);
1453 pa_assert(buf);
1454
1455 if (!fail)
1456 fail = &_fail;
1457
1458 while (fgets(line, sizeof(line), f)) {
1459 pa_strip_nl(line);
1460
1461 if (pa_cli_command_execute_line_stateful(c, line, buf, fail, &ifstate) < 0 && *fail)
1462 goto fail;
1463 }
1464
1465 ret = 0;
1466
1467 fail:
1468
1469 return ret;
1470 }
1471
1472 int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf, pa_bool_t *fail) {
1473 FILE *f = NULL;
1474 int ret = -1;
1475 pa_bool_t _fail = TRUE;
1476
1477 pa_assert(c);
1478 pa_assert(fn);
1479 pa_assert(buf);
1480
1481 if (!fail)
1482 fail = &_fail;
1483
1484 if (!(f = fopen(fn, "r"))) {
1485 pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, pa_cstrerror(errno));
1486 if (!*fail)
1487 ret = 0;
1488 goto fail;
1489 }
1490
1491 ret = pa_cli_command_execute_file_stream(c, f, buf, fail);
1492
1493 ret = 0;
1494
1495 fail:
1496 if (f)
1497 fclose(f);
1498
1499 return ret;
1500 }
1501
1502 int pa_cli_command_execute(pa_core *c, const char *s, pa_strbuf *buf, pa_bool_t *fail) {
1503 const char *p;
1504 int ifstate = IFSTATE_NONE;
1505 pa_bool_t _fail = TRUE;
1506
1507 pa_assert(c);
1508 pa_assert(s);
1509 pa_assert(buf);
1510
1511 if (!fail)
1512 fail = &_fail;
1513
1514 p = s;
1515 while (*p) {
1516 size_t l = strcspn(p, linebreak);
1517 char *line = pa_xstrndup(p, l);
1518
1519 if (pa_cli_command_execute_line_stateful(c, line, buf, fail, &ifstate) < 0 && *fail) {
1520 pa_xfree(line);
1521 return -1;
1522 }
1523 pa_xfree(line);
1524
1525 p += l;
1526 p += strspn(p, linebreak);
1527 }
1528
1529 return 0;
1530 }