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