]> code.delx.au - pulseaudio/blob - src/utils/pactl.c
Remove pa_bool_t and replace it with bool.
[pulseaudio] / src / utils / pactl.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <getopt.h>
34 #include <locale.h>
35 #include <ctype.h>
36
37 #include <sndfile.h>
38
39 #include <pulse/pulseaudio.h>
40 #include <pulse/ext-device-restore.h>
41
42 #include <pulsecore/i18n.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/sndfile-util.h>
47
48 static pa_context *context = NULL;
49 static pa_mainloop_api *mainloop_api = NULL;
50
51 static char
52 *list_type = NULL,
53 *sample_name = NULL,
54 *sink_name = NULL,
55 *source_name = NULL,
56 *module_name = NULL,
57 *module_args = NULL,
58 *card_name = NULL,
59 *profile_name = NULL,
60 *port_name = NULL,
61 *formats = NULL;
62
63 static uint32_t
64 sink_input_idx = PA_INVALID_INDEX,
65 source_output_idx = PA_INVALID_INDEX,
66 sink_idx = PA_INVALID_INDEX;
67
68 static bool short_list_format = false;
69 static uint32_t module_index;
70 static int32_t latency_offset;
71 static bool suspend;
72 static pa_volume_t volume;
73 static enum volume_flags {
74 VOL_UINT = 0,
75 VOL_PERCENT = 1,
76 VOL_LINEAR = 2,
77 VOL_DECIBEL = 3,
78 VOL_ABSOLUTE = 0 << 4,
79 VOL_RELATIVE = 1 << 4,
80 } volume_flags;
81
82 static enum mute_flags {
83 INVALID_MUTE = -1,
84 UNMUTE = 0,
85 MUTE = 1,
86 TOGGLE_MUTE = 2
87 } mute = INVALID_MUTE;
88
89 static pa_proplist *proplist = NULL;
90
91 static SNDFILE *sndfile = NULL;
92 static pa_stream *sample_stream = NULL;
93 static pa_sample_spec sample_spec;
94 static pa_channel_map channel_map;
95 static size_t sample_length = 0;
96 static int actions = 1;
97
98 static bool nl = false;
99
100 static enum {
101 NONE,
102 EXIT,
103 STAT,
104 INFO,
105 UPLOAD_SAMPLE,
106 PLAY_SAMPLE,
107 REMOVE_SAMPLE,
108 LIST,
109 MOVE_SINK_INPUT,
110 MOVE_SOURCE_OUTPUT,
111 LOAD_MODULE,
112 UNLOAD_MODULE,
113 SUSPEND_SINK,
114 SUSPEND_SOURCE,
115 SET_CARD_PROFILE,
116 SET_SINK_PORT,
117 SET_DEFAULT_SINK,
118 SET_SOURCE_PORT,
119 SET_DEFAULT_SOURCE,
120 SET_SINK_VOLUME,
121 SET_SOURCE_VOLUME,
122 SET_SINK_INPUT_VOLUME,
123 SET_SOURCE_OUTPUT_VOLUME,
124 SET_SINK_MUTE,
125 SET_SOURCE_MUTE,
126 SET_SINK_INPUT_MUTE,
127 SET_SOURCE_OUTPUT_MUTE,
128 SET_SINK_FORMATS,
129 SET_PORT_LATENCY_OFFSET,
130 SUBSCRIBE
131 } action = NONE;
132
133 static void quit(int ret) {
134 pa_assert(mainloop_api);
135 mainloop_api->quit(mainloop_api, ret);
136 }
137
138 static void context_drain_complete(pa_context *c, void *userdata) {
139 pa_context_disconnect(c);
140 }
141
142 static void drain(void) {
143 pa_operation *o;
144
145 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
146 pa_context_disconnect(context);
147 else
148 pa_operation_unref(o);
149 }
150
151 static void complete_action(void) {
152 pa_assert(actions > 0);
153
154 if (!(--actions))
155 drain();
156 }
157
158 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
159 char s[PA_BYTES_SNPRINT_MAX];
160 if (!i) {
161 pa_log(_("Failed to get statistics: %s"), pa_strerror(pa_context_errno(c)));
162 quit(1);
163 return;
164 }
165
166 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
167 printf(_("Currently in use: %u blocks containing %s bytes total.\n"), i->memblock_total, s);
168
169 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
170 printf(_("Allocated during whole lifetime: %u blocks containing %s bytes total.\n"), i->memblock_allocated, s);
171
172 pa_bytes_snprint(s, sizeof(s), i->scache_size);
173 printf(_("Sample cache size: %s\n"), s);
174
175 complete_action();
176 }
177
178 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
179 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
180
181 if (!i) {
182 pa_log(_("Failed to get server information: %s"), pa_strerror(pa_context_errno(c)));
183 quit(1);
184 return;
185 }
186
187 printf(_("Server String: %s\n"
188 "Library Protocol Version: %u\n"
189 "Server Protocol Version: %u\n"
190 "Is Local: %s\n"
191 "Client Index: %u\n"
192 "Tile Size: %zu\n"),
193 pa_context_get_server(c),
194 pa_context_get_protocol_version(c),
195 pa_context_get_server_protocol_version(c),
196 pa_yes_no(pa_context_is_local(c)),
197 pa_context_get_index(c),
198 pa_context_get_tile_size(c, NULL));
199
200 pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec);
201 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map);
202
203 printf(_("User Name: %s\n"
204 "Host Name: %s\n"
205 "Server Name: %s\n"
206 "Server Version: %s\n"
207 "Default Sample Specification: %s\n"
208 "Default Channel Map: %s\n"
209 "Default Sink: %s\n"
210 "Default Source: %s\n"
211 "Cookie: %04x:%04x\n"),
212 i->user_name,
213 i->host_name,
214 i->server_name,
215 i->server_version,
216 ss,
217 cm,
218 i->default_sink_name,
219 i->default_source_name,
220 i->cookie >> 16,
221 i->cookie & 0xFFFFU);
222
223 complete_action();
224 }
225
226 static const char* get_available_str_ynonly(int available) {
227 switch (available) {
228 case PA_PORT_AVAILABLE_YES: return ", available";
229 case PA_PORT_AVAILABLE_NO: return ", not available";
230 }
231 return "";
232 }
233
234 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
235
236 static const char *state_table[] = {
237 [1+PA_SINK_INVALID_STATE] = "n/a",
238 [1+PA_SINK_RUNNING] = "RUNNING",
239 [1+PA_SINK_IDLE] = "IDLE",
240 [1+PA_SINK_SUSPENDED] = "SUSPENDED"
241 };
242
243 char
244 s[PA_SAMPLE_SPEC_SNPRINT_MAX],
245 cv[PA_CVOLUME_SNPRINT_MAX],
246 cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX],
247 v[PA_VOLUME_SNPRINT_MAX],
248 vdb[PA_SW_VOLUME_SNPRINT_DB_MAX],
249 cm[PA_CHANNEL_MAP_SNPRINT_MAX],
250 f[PA_FORMAT_INFO_SNPRINT_MAX];
251 char *pl;
252
253 if (is_last < 0) {
254 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
255 quit(1);
256 return;
257 }
258
259 if (is_last) {
260 complete_action();
261 return;
262 }
263
264 pa_assert(i);
265
266 if (nl && !short_list_format)
267 printf("\n");
268 nl = true;
269
270 if (short_list_format) {
271 printf("%u\t%s\t%s\t%s\t%s\n",
272 i->index,
273 i->name,
274 pa_strnull(i->driver),
275 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
276 state_table[1+i->state]);
277 return;
278 }
279
280 printf(_("Sink #%u\n"
281 "\tState: %s\n"
282 "\tName: %s\n"
283 "\tDescription: %s\n"
284 "\tDriver: %s\n"
285 "\tSample Specification: %s\n"
286 "\tChannel Map: %s\n"
287 "\tOwner Module: %u\n"
288 "\tMute: %s\n"
289 "\tVolume: %s%s%s\n"
290 "\t balance %0.2f\n"
291 "\tBase Volume: %s%s%s\n"
292 "\tMonitor Source: %s\n"
293 "\tLatency: %0.0f usec, configured %0.0f usec\n"
294 "\tFlags: %s%s%s%s%s%s%s\n"
295 "\tProperties:\n\t\t%s\n"),
296 i->index,
297 state_table[1+i->state],
298 i->name,
299 pa_strnull(i->description),
300 pa_strnull(i->driver),
301 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
302 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
303 i->owner_module,
304 pa_yes_no(i->mute),
305 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
306 i->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t " : "",
307 i->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume) : "",
308 pa_cvolume_get_balance(&i->volume, &i->channel_map),
309 pa_volume_snprint(v, sizeof(v), i->base_volume),
310 i->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t " : "",
311 i->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), i->base_volume) : "",
312 pa_strnull(i->monitor_source_name),
313 (double) i->latency, (double) i->configured_latency,
314 i->flags & PA_SINK_HARDWARE ? "HARDWARE " : "",
315 i->flags & PA_SINK_NETWORK ? "NETWORK " : "",
316 i->flags & PA_SINK_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
317 i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
318 i->flags & PA_SINK_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
319 i->flags & PA_SINK_LATENCY ? "LATENCY " : "",
320 i->flags & PA_SINK_SET_FORMATS ? "SET_FORMATS " : "",
321 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
322
323 pa_xfree(pl);
324
325 if (i->ports) {
326 pa_sink_port_info **p;
327
328 printf(_("\tPorts:\n"));
329 for (p = i->ports; *p; p++)
330 printf("\t\t%s: %s (priority: %u%s)\n", (*p)->name, (*p)->description,
331 (*p)->priority, get_available_str_ynonly((*p)->available));
332 }
333
334 if (i->active_port)
335 printf(_("\tActive Port: %s\n"),
336 i->active_port->name);
337
338 if (i->formats) {
339 uint8_t j;
340
341 printf(_("\tFormats:\n"));
342 for (j = 0; j < i->n_formats; j++)
343 printf("\t\t%s\n", pa_format_info_snprint(f, sizeof(f), i->formats[j]));
344 }
345 }
346
347 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
348
349 static const char *state_table[] = {
350 [1+PA_SOURCE_INVALID_STATE] = "n/a",
351 [1+PA_SOURCE_RUNNING] = "RUNNING",
352 [1+PA_SOURCE_IDLE] = "IDLE",
353 [1+PA_SOURCE_SUSPENDED] = "SUSPENDED"
354 };
355
356 char
357 s[PA_SAMPLE_SPEC_SNPRINT_MAX],
358 cv[PA_CVOLUME_SNPRINT_MAX],
359 cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX],
360 v[PA_VOLUME_SNPRINT_MAX],
361 vdb[PA_SW_VOLUME_SNPRINT_DB_MAX],
362 cm[PA_CHANNEL_MAP_SNPRINT_MAX],
363 f[PA_FORMAT_INFO_SNPRINT_MAX];
364 char *pl;
365
366 if (is_last < 0) {
367 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
368 quit(1);
369 return;
370 }
371
372 if (is_last) {
373 complete_action();
374 return;
375 }
376
377 pa_assert(i);
378
379 if (nl && !short_list_format)
380 printf("\n");
381 nl = true;
382
383 if (short_list_format) {
384 printf("%u\t%s\t%s\t%s\t%s\n",
385 i->index,
386 i->name,
387 pa_strnull(i->driver),
388 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
389 state_table[1+i->state]);
390 return;
391 }
392
393 printf(_("Source #%u\n"
394 "\tState: %s\n"
395 "\tName: %s\n"
396 "\tDescription: %s\n"
397 "\tDriver: %s\n"
398 "\tSample Specification: %s\n"
399 "\tChannel Map: %s\n"
400 "\tOwner Module: %u\n"
401 "\tMute: %s\n"
402 "\tVolume: %s%s%s\n"
403 "\t balance %0.2f\n"
404 "\tBase Volume: %s%s%s\n"
405 "\tMonitor of Sink: %s\n"
406 "\tLatency: %0.0f usec, configured %0.0f usec\n"
407 "\tFlags: %s%s%s%s%s%s\n"
408 "\tProperties:\n\t\t%s\n"),
409 i->index,
410 state_table[1+i->state],
411 i->name,
412 pa_strnull(i->description),
413 pa_strnull(i->driver),
414 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
415 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
416 i->owner_module,
417 pa_yes_no(i->mute),
418 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
419 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "",
420 i->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume) : "",
421 pa_cvolume_get_balance(&i->volume, &i->channel_map),
422 pa_volume_snprint(v, sizeof(v), i->base_volume),
423 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "",
424 i->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), i->base_volume) : "",
425 i->monitor_of_sink_name ? i->monitor_of_sink_name : _("n/a"),
426 (double) i->latency, (double) i->configured_latency,
427 i->flags & PA_SOURCE_HARDWARE ? "HARDWARE " : "",
428 i->flags & PA_SOURCE_NETWORK ? "NETWORK " : "",
429 i->flags & PA_SOURCE_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
430 i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
431 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
432 i->flags & PA_SOURCE_LATENCY ? "LATENCY " : "",
433 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
434
435 pa_xfree(pl);
436
437 if (i->ports) {
438 pa_source_port_info **p;
439
440 printf(_("\tPorts:\n"));
441 for (p = i->ports; *p; p++)
442 printf("\t\t%s: %s (priority: %u%s)\n", (*p)->name, (*p)->description,
443 (*p)->priority, get_available_str_ynonly((*p)->available));
444 }
445
446 if (i->active_port)
447 printf(_("\tActive Port: %s\n"),
448 i->active_port->name);
449
450 if (i->formats) {
451 uint8_t j;
452
453 printf(_("\tFormats:\n"));
454 for (j = 0; j < i->n_formats; j++)
455 printf("\t\t%s\n", pa_format_info_snprint(f, sizeof(f), i->formats[j]));
456 }
457 }
458
459 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
460 char t[32];
461 char *pl;
462
463 if (is_last < 0) {
464 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
465 quit(1);
466 return;
467 }
468
469 if (is_last) {
470 complete_action();
471 return;
472 }
473
474 pa_assert(i);
475
476 if (nl && !short_list_format)
477 printf("\n");
478 nl = true;
479
480 pa_snprintf(t, sizeof(t), "%u", i->n_used);
481
482 if (short_list_format) {
483 printf("%u\t%s\t%s\t\n", i->index, i->name, i->argument ? i->argument : "");
484 return;
485 }
486
487 printf(_("Module #%u\n"
488 "\tName: %s\n"
489 "\tArgument: %s\n"
490 "\tUsage counter: %s\n"
491 "\tProperties:\n\t\t%s\n"),
492 i->index,
493 i->name,
494 i->argument ? i->argument : "",
495 i->n_used != PA_INVALID_INDEX ? t : _("n/a"),
496 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
497
498 pa_xfree(pl);
499 }
500
501 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
502 char t[32];
503 char *pl;
504
505 if (is_last < 0) {
506 pa_log(_("Failed to get client information: %s"), pa_strerror(pa_context_errno(c)));
507 quit(1);
508 return;
509 }
510
511 if (is_last) {
512 complete_action();
513 return;
514 }
515
516 pa_assert(i);
517
518 if (nl && !short_list_format)
519 printf("\n");
520 nl = true;
521
522 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
523
524 if (short_list_format) {
525 printf("%u\t%s\t%s\n",
526 i->index,
527 pa_strnull(i->driver),
528 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)));
529 return;
530 }
531
532 printf(_("Client #%u\n"
533 "\tDriver: %s\n"
534 "\tOwner Module: %s\n"
535 "\tProperties:\n\t\t%s\n"),
536 i->index,
537 pa_strnull(i->driver),
538 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
539 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
540
541 pa_xfree(pl);
542 }
543
544 static void get_card_info_callback(pa_context *c, const pa_card_info *i, int is_last, void *userdata) {
545 char t[32];
546 char *pl;
547
548 if (is_last < 0) {
549 pa_log(_("Failed to get card information: %s"), pa_strerror(pa_context_errno(c)));
550 complete_action();
551 return;
552 }
553
554 if (is_last) {
555 complete_action();
556 return;
557 }
558
559 pa_assert(i);
560
561 if (nl && !short_list_format)
562 printf("\n");
563 nl = true;
564
565 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
566
567 if (short_list_format) {
568 printf("%u\t%s\t%s\n", i->index, i->name, pa_strnull(i->driver));
569 return;
570 }
571
572 printf(_("Card #%u\n"
573 "\tName: %s\n"
574 "\tDriver: %s\n"
575 "\tOwner Module: %s\n"
576 "\tProperties:\n\t\t%s\n"),
577 i->index,
578 i->name,
579 pa_strnull(i->driver),
580 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
581 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
582
583 pa_xfree(pl);
584
585 if (i->profiles) {
586 pa_card_profile_info *p;
587
588 printf(_("\tProfiles:\n"));
589 for (p = i->profiles; p->name; p++)
590 printf("\t\t%s: %s (sinks: %u, sources: %u, priority: %u)\n", p->name, p->description, p->n_sinks, p->n_sources, p->priority);
591 }
592
593 if (i->active_profile)
594 printf(_("\tActive Profile: %s\n"),
595 i->active_profile->name);
596
597 if (i->ports) {
598 pa_card_port_info **p;
599
600 printf(_("\tPorts:\n"));
601 for (p = i->ports; *p; p++) {
602 pa_card_profile_info **pr = (*p)->profiles;
603 printf("\t\t%s: %s (priority: %u, latency offset: %" PRId64 " usec%s)\n", (*p)->name,
604 (*p)->description, (*p)->priority, (*p)->latency_offset,
605 get_available_str_ynonly((*p)->available));
606
607 if (!pa_proplist_isempty((*p)->proplist)) {
608 printf(_("\t\t\tProperties:\n\t\t\t\t%s\n"), pl = pa_proplist_to_string_sep((*p)->proplist, "\n\t\t\t\t"));
609 pa_xfree(pl);
610 }
611
612 if (pr) {
613 printf(_("\t\t\tPart of profile(s): %s"), pa_strnull((*pr)->name));
614 pr++;
615 while (*pr) {
616 printf(", %s", pa_strnull((*pr)->name));
617 pr++;
618 }
619 printf("\n");
620 }
621 }
622 }
623 }
624
625 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
626 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], f[PA_FORMAT_INFO_SNPRINT_MAX];
627 char *pl;
628
629 if (is_last < 0) {
630 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
631 quit(1);
632 return;
633 }
634
635 if (is_last) {
636 complete_action();
637 return;
638 }
639
640 pa_assert(i);
641
642 if (nl && !short_list_format)
643 printf("\n");
644 nl = true;
645
646 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
647 pa_snprintf(k, sizeof(k), "%u", i->client);
648
649 if (short_list_format) {
650 printf("%u\t%u\t%s\t%s\t%s\n",
651 i->index,
652 i->sink,
653 i->client != PA_INVALID_INDEX ? k : "-",
654 pa_strnull(i->driver),
655 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec));
656 return;
657 }
658
659 printf(_("Sink Input #%u\n"
660 "\tDriver: %s\n"
661 "\tOwner Module: %s\n"
662 "\tClient: %s\n"
663 "\tSink: %u\n"
664 "\tSample Specification: %s\n"
665 "\tChannel Map: %s\n"
666 "\tFormat: %s\n"
667 "\tCorked: %s\n"
668 "\tMute: %s\n"
669 "\tVolume: %s\n"
670 "\t %s\n"
671 "\t balance %0.2f\n"
672 "\tBuffer Latency: %0.0f usec\n"
673 "\tSink Latency: %0.0f usec\n"
674 "\tResample method: %s\n"
675 "\tProperties:\n\t\t%s\n"),
676 i->index,
677 pa_strnull(i->driver),
678 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
679 i->client != PA_INVALID_INDEX ? k : _("n/a"),
680 i->sink,
681 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
682 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
683 pa_format_info_snprint(f, sizeof(f), i->format),
684 pa_yes_no(i->corked),
685 pa_yes_no(i->mute),
686 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
687 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
688 pa_cvolume_get_balance(&i->volume, &i->channel_map),
689 (double) i->buffer_usec,
690 (double) i->sink_usec,
691 i->resample_method ? i->resample_method : _("n/a"),
692 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
693
694 pa_xfree(pl);
695 }
696
697 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
698 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], f[PA_FORMAT_INFO_SNPRINT_MAX];
699 char *pl;
700
701 if (is_last < 0) {
702 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
703 quit(1);
704 return;
705 }
706
707 if (is_last) {
708 complete_action();
709 return;
710 }
711
712 pa_assert(i);
713
714 if (nl && !short_list_format)
715 printf("\n");
716 nl = true;
717
718 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
719 pa_snprintf(k, sizeof(k), "%u", i->client);
720
721 if (short_list_format) {
722 printf("%u\t%u\t%s\t%s\t%s\n",
723 i->index,
724 i->source,
725 i->client != PA_INVALID_INDEX ? k : "-",
726 pa_strnull(i->driver),
727 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec));
728 return;
729 }
730
731 printf(_("Source Output #%u\n"
732 "\tDriver: %s\n"
733 "\tOwner Module: %s\n"
734 "\tClient: %s\n"
735 "\tSource: %u\n"
736 "\tSample Specification: %s\n"
737 "\tChannel Map: %s\n"
738 "\tFormat: %s\n"
739 "\tCorked: %s\n"
740 "\tMute: %s\n"
741 "\tVolume: %s\n"
742 "\t %s\n"
743 "\t balance %0.2f\n"
744 "\tBuffer Latency: %0.0f usec\n"
745 "\tSource Latency: %0.0f usec\n"
746 "\tResample method: %s\n"
747 "\tProperties:\n\t\t%s\n"),
748 i->index,
749 pa_strnull(i->driver),
750 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
751 i->client != PA_INVALID_INDEX ? k : _("n/a"),
752 i->source,
753 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
754 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
755 pa_format_info_snprint(f, sizeof(f), i->format),
756 pa_yes_no(i->corked),
757 pa_yes_no(i->mute),
758 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
759 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
760 pa_cvolume_get_balance(&i->volume, &i->channel_map),
761 (double) i->buffer_usec,
762 (double) i->source_usec,
763 i->resample_method ? i->resample_method : _("n/a"),
764 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
765
766 pa_xfree(pl);
767 }
768
769 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
770 char t[PA_BYTES_SNPRINT_MAX], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
771 char *pl;
772
773 if (is_last < 0) {
774 pa_log(_("Failed to get sample information: %s"), pa_strerror(pa_context_errno(c)));
775 quit(1);
776 return;
777 }
778
779 if (is_last) {
780 complete_action();
781 return;
782 }
783
784 pa_assert(i);
785
786 if (nl && !short_list_format)
787 printf("\n");
788 nl = true;
789
790 pa_bytes_snprint(t, sizeof(t), i->bytes);
791
792 if (short_list_format) {
793 printf("%u\t%s\t%s\t%0.3f\n",
794 i->index,
795 i->name,
796 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : "-",
797 (double) i->duration/1000000.0);
798 return;
799 }
800
801 printf(_("Sample #%u\n"
802 "\tName: %s\n"
803 "\tSample Specification: %s\n"
804 "\tChannel Map: %s\n"
805 "\tVolume: %s\n"
806 "\t %s\n"
807 "\t balance %0.2f\n"
808 "\tDuration: %0.1fs\n"
809 "\tSize: %s\n"
810 "\tLazy: %s\n"
811 "\tFilename: %s\n"
812 "\tProperties:\n\t\t%s\n"),
813 i->index,
814 i->name,
815 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : _("n/a"),
816 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : _("n/a"),
817 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
818 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
819 pa_cvolume_get_balance(&i->volume, &i->channel_map),
820 (double) i->duration/1000000.0,
821 t,
822 pa_yes_no(i->lazy),
823 i->filename ? i->filename : _("n/a"),
824 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
825
826 pa_xfree(pl);
827 }
828
829 static void simple_callback(pa_context *c, int success, void *userdata) {
830 if (!success) {
831 pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
832 quit(1);
833 return;
834 }
835
836 complete_action();
837 }
838
839 static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
840 if (idx == PA_INVALID_INDEX) {
841 pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c)));
842 quit(1);
843 return;
844 }
845
846 printf("%u\n", idx);
847
848 complete_action();
849 }
850
851 static void volume_relative_adjust(pa_cvolume *cv) {
852 pa_assert((volume_flags & VOL_RELATIVE) == VOL_RELATIVE);
853
854 /* Relative volume change is additive in case of UINT or PERCENT
855 * and multiplicative for LINEAR or DECIBEL */
856 if ((volume_flags & 0x0F) == VOL_UINT || (volume_flags & 0x0F) == VOL_PERCENT) {
857 pa_volume_t v = pa_cvolume_avg(cv);
858 v = v + volume < PA_VOLUME_NORM ? PA_VOLUME_MUTED : v + volume - PA_VOLUME_NORM;
859 pa_cvolume_set(cv, 1, v);
860 }
861 if ((volume_flags & 0x0F) == VOL_LINEAR || (volume_flags & 0x0F) == VOL_DECIBEL) {
862 pa_sw_cvolume_multiply_scalar(cv, cv, volume);
863 }
864 }
865
866 static void unload_module_by_name_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
867 static bool unloaded = false;
868
869 if (is_last < 0) {
870 pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
871 quit(1);
872 return;
873 }
874
875 if (is_last) {
876 if (unloaded == false)
877 pa_log(_("Failed to unload module: Module %s not loaded"), module_name);
878 complete_action();
879 return;
880 }
881
882 pa_assert(i);
883
884 if (pa_streq(module_name, i->name)) {
885 unloaded = true;
886 actions++;
887 pa_operation_unref(pa_context_unload_module(c, i->index, simple_callback, NULL));
888 }
889 }
890
891 static void get_sink_volume_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
892 pa_cvolume cv;
893
894 if (is_last < 0) {
895 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
896 quit(1);
897 return;
898 }
899
900 if (is_last)
901 return;
902
903 pa_assert(i);
904
905 cv = i->volume;
906 volume_relative_adjust(&cv);
907 pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &cv, simple_callback, NULL));
908 }
909
910 static void get_source_volume_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
911 pa_cvolume cv;
912
913 if (is_last < 0) {
914 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
915 quit(1);
916 return;
917 }
918
919 if (is_last)
920 return;
921
922 pa_assert(i);
923
924 cv = i->volume;
925 volume_relative_adjust(&cv);
926 pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &cv, simple_callback, NULL));
927 }
928
929 static void get_sink_input_volume_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
930 pa_cvolume cv;
931
932 if (is_last < 0) {
933 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
934 quit(1);
935 return;
936 }
937
938 if (is_last)
939 return;
940
941 pa_assert(i);
942
943 cv = i->volume;
944 volume_relative_adjust(&cv);
945 pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &cv, simple_callback, NULL));
946 }
947
948 static void get_source_output_volume_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
949 pa_cvolume cv;
950
951 if (is_last < 0) {
952 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
953 quit(1);
954 return;
955 }
956
957 if (is_last)
958 return;
959
960 pa_assert(o);
961
962 cv = o->volume;
963 volume_relative_adjust(&cv);
964 pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &cv, simple_callback, NULL));
965 }
966
967 static void sink_toggle_mute_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
968 if (is_last < 0) {
969 pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
970 quit(1);
971 return;
972 }
973
974 if (is_last)
975 return;
976
977 pa_assert(i);
978
979 pa_operation_unref(pa_context_set_sink_mute_by_name(c, i->name, !i->mute, simple_callback, NULL));
980 }
981
982 static void source_toggle_mute_callback(pa_context *c, const pa_source_info *o, int is_last, void *userdata) {
983 if (is_last < 0) {
984 pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
985 quit(1);
986 return;
987 }
988
989 if (is_last)
990 return;
991
992 pa_assert(o);
993
994 pa_operation_unref(pa_context_set_source_mute_by_name(c, o->name, !o->mute, simple_callback, NULL));
995 }
996
997 static void sink_input_toggle_mute_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
998 if (is_last < 0) {
999 pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
1000 quit(1);
1001 return;
1002 }
1003
1004 if (is_last)
1005 return;
1006
1007 pa_assert(i);
1008
1009 pa_operation_unref(pa_context_set_sink_input_mute(c, i->index, !i->mute, simple_callback, NULL));
1010 }
1011
1012 static void source_output_toggle_mute_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
1013 if (is_last < 0) {
1014 pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
1015 quit(1);
1016 return;
1017 }
1018
1019 if (is_last)
1020 return;
1021
1022 pa_assert(o);
1023
1024 pa_operation_unref(pa_context_set_source_output_mute(c, o->index, !o->mute, simple_callback, NULL));
1025 }
1026
1027 /* PA_MAX_FORMATS is defined in internal.h so we just define a sane value here */
1028 #define MAX_FORMATS 256
1029
1030 static void set_sink_formats(pa_context *c, uint32_t sink, const char *str) {
1031 pa_format_info *f_arr[MAX_FORMATS];
1032 char *format = NULL;
1033 const char *state = NULL;
1034 int i = 0;
1035
1036 while ((format = pa_split(str, ";", &state))) {
1037 pa_format_info *f = pa_format_info_from_string(pa_strip(format));
1038
1039 if (!f) {
1040 pa_log(_("Failed to set format: invalid format string %s"), format);
1041 goto error;
1042 }
1043
1044 f_arr[i++] = f;
1045 pa_xfree(format);
1046 }
1047
1048 pa_operation_unref(pa_ext_device_restore_save_formats(c, PA_DEVICE_TYPE_SINK, sink, i, f_arr, simple_callback, NULL));
1049
1050 done:
1051 if (format)
1052 pa_xfree(format);
1053 while(i--)
1054 pa_format_info_free(f_arr[i]);
1055
1056 return;
1057
1058 error:
1059 while(i--)
1060 pa_format_info_free(f_arr[i]);
1061 quit(1);
1062 goto done;
1063 }
1064
1065 static void stream_state_callback(pa_stream *s, void *userdata) {
1066 pa_assert(s);
1067
1068 switch (pa_stream_get_state(s)) {
1069 case PA_STREAM_CREATING:
1070 case PA_STREAM_READY:
1071 break;
1072
1073 case PA_STREAM_TERMINATED:
1074 drain();
1075 break;
1076
1077 case PA_STREAM_FAILED:
1078 default:
1079 pa_log(_("Failed to upload sample: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
1080 quit(1);
1081 }
1082 }
1083
1084 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
1085 sf_count_t l;
1086 float *d;
1087 pa_assert(s && length && sndfile);
1088
1089 d = pa_xmalloc(length);
1090
1091 pa_assert(sample_length >= length);
1092 l = (sf_count_t) (length/pa_frame_size(&sample_spec));
1093
1094 if ((sf_readf_float(sndfile, d, l)) != l) {
1095 pa_xfree(d);
1096 pa_log(_("Premature end of file"));
1097 quit(1);
1098 return;
1099 }
1100
1101 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
1102
1103 sample_length -= length;
1104
1105 if (sample_length <= 0) {
1106 pa_stream_set_write_callback(sample_stream, NULL, NULL);
1107 pa_stream_finish_upload(sample_stream);
1108 }
1109 }
1110
1111 static const char *subscription_event_type_to_string(pa_subscription_event_type_t t) {
1112
1113 switch (t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
1114
1115 case PA_SUBSCRIPTION_EVENT_NEW:
1116 return _("new");
1117
1118 case PA_SUBSCRIPTION_EVENT_CHANGE:
1119 return _("change");
1120
1121 case PA_SUBSCRIPTION_EVENT_REMOVE:
1122 return _("remove");
1123 }
1124
1125 return _("unknown");
1126 }
1127
1128 static const char *subscription_event_facility_to_string(pa_subscription_event_type_t t) {
1129
1130 switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
1131
1132 case PA_SUBSCRIPTION_EVENT_SINK:
1133 return _("sink");
1134
1135 case PA_SUBSCRIPTION_EVENT_SOURCE:
1136 return _("source");
1137
1138 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
1139 return _("sink-input");
1140
1141 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
1142 return _("source-output");
1143
1144 case PA_SUBSCRIPTION_EVENT_MODULE:
1145 return _("module");
1146
1147 case PA_SUBSCRIPTION_EVENT_CLIENT:
1148 return _("client");
1149
1150 case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE:
1151 return _("sample-cache");
1152
1153 case PA_SUBSCRIPTION_EVENT_SERVER:
1154 return _("server");
1155
1156 case PA_SUBSCRIPTION_EVENT_CARD:
1157 return _("server");
1158 }
1159
1160 return _("unknown");
1161 }
1162
1163 static void context_subscribe_callback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
1164 pa_assert(c);
1165
1166 printf(_("Event '%s' on %s #%u\n"),
1167 subscription_event_type_to_string(t),
1168 subscription_event_facility_to_string(t),
1169 idx);
1170 fflush(stdout);
1171 }
1172
1173 static void context_state_callback(pa_context *c, void *userdata) {
1174 pa_assert(c);
1175 switch (pa_context_get_state(c)) {
1176 case PA_CONTEXT_CONNECTING:
1177 case PA_CONTEXT_AUTHORIZING:
1178 case PA_CONTEXT_SETTING_NAME:
1179 break;
1180
1181 case PA_CONTEXT_READY:
1182 switch (action) {
1183 case STAT:
1184 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
1185 if (short_list_format)
1186 break;
1187 actions++;
1188
1189 case INFO:
1190 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
1191 break;
1192
1193 case PLAY_SAMPLE:
1194 pa_operation_unref(pa_context_play_sample(c, sample_name, sink_name, PA_VOLUME_NORM, simple_callback, NULL));
1195 break;
1196
1197 case REMOVE_SAMPLE:
1198 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
1199 break;
1200
1201 case UPLOAD_SAMPLE:
1202 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
1203 pa_assert(sample_stream);
1204
1205 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
1206 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
1207 pa_stream_connect_upload(sample_stream, sample_length);
1208 break;
1209
1210 case EXIT:
1211 pa_operation_unref(pa_context_exit_daemon(c, simple_callback, NULL));
1212 break;
1213
1214 case LIST:
1215 if (list_type) {
1216 if (pa_streq(list_type, "modules"))
1217 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
1218 else if (pa_streq(list_type, "sinks"))
1219 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
1220 else if (pa_streq(list_type, "sources"))
1221 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
1222 else if (pa_streq(list_type, "sink-inputs"))
1223 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
1224 else if (pa_streq(list_type, "source-outputs"))
1225 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
1226 else if (pa_streq(list_type, "clients"))
1227 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
1228 else if (pa_streq(list_type, "samples"))
1229 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
1230 else if (pa_streq(list_type, "cards"))
1231 pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL));
1232 else
1233 pa_assert_not_reached();
1234 } else {
1235 actions = 8;
1236 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
1237 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
1238 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
1239 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
1240 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
1241 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
1242 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
1243 pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL));
1244 }
1245 break;
1246
1247 case MOVE_SINK_INPUT:
1248 pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL));
1249 break;
1250
1251 case MOVE_SOURCE_OUTPUT:
1252 pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL));
1253 break;
1254
1255 case LOAD_MODULE:
1256 pa_operation_unref(pa_context_load_module(c, module_name, module_args, index_callback, NULL));
1257 break;
1258
1259 case UNLOAD_MODULE:
1260 if (module_name)
1261 pa_operation_unref(pa_context_get_module_info_list(c, unload_module_by_name_callback, NULL));
1262 else
1263 pa_operation_unref(pa_context_unload_module(c, module_index, simple_callback, NULL));
1264 break;
1265
1266 case SUSPEND_SINK:
1267 if (sink_name)
1268 pa_operation_unref(pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL));
1269 else
1270 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
1271 break;
1272
1273 case SUSPEND_SOURCE:
1274 if (source_name)
1275 pa_operation_unref(pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL));
1276 else
1277 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
1278 break;
1279
1280 case SET_CARD_PROFILE:
1281 pa_operation_unref(pa_context_set_card_profile_by_name(c, card_name, profile_name, simple_callback, NULL));
1282 break;
1283
1284 case SET_SINK_PORT:
1285 pa_operation_unref(pa_context_set_sink_port_by_name(c, sink_name, port_name, simple_callback, NULL));
1286 break;
1287
1288 case SET_DEFAULT_SINK:
1289 pa_operation_unref(pa_context_set_default_sink(c, sink_name, simple_callback, NULL));
1290 break;
1291
1292 case SET_SOURCE_PORT:
1293 pa_operation_unref(pa_context_set_source_port_by_name(c, source_name, port_name, simple_callback, NULL));
1294 break;
1295
1296 case SET_DEFAULT_SOURCE:
1297 pa_operation_unref(pa_context_set_default_source(c, source_name, simple_callback, NULL));
1298 break;
1299
1300 case SET_SINK_MUTE:
1301 if (mute == TOGGLE_MUTE)
1302 pa_operation_unref(pa_context_get_sink_info_by_name(c, sink_name, sink_toggle_mute_callback, NULL));
1303 else
1304 pa_operation_unref(pa_context_set_sink_mute_by_name(c, sink_name, mute, simple_callback, NULL));
1305 break;
1306
1307 case SET_SOURCE_MUTE:
1308 if (mute == TOGGLE_MUTE)
1309 pa_operation_unref(pa_context_get_source_info_by_name(c, source_name, source_toggle_mute_callback, NULL));
1310 else
1311 pa_operation_unref(pa_context_set_source_mute_by_name(c, source_name, mute, simple_callback, NULL));
1312 break;
1313
1314 case SET_SINK_INPUT_MUTE:
1315 if (mute == TOGGLE_MUTE)
1316 pa_operation_unref(pa_context_get_sink_input_info(c, sink_input_idx, sink_input_toggle_mute_callback, NULL));
1317 else
1318 pa_operation_unref(pa_context_set_sink_input_mute(c, sink_input_idx, mute, simple_callback, NULL));
1319 break;
1320
1321 case SET_SOURCE_OUTPUT_MUTE:
1322 if (mute == TOGGLE_MUTE)
1323 pa_operation_unref(pa_context_get_source_output_info(c, source_output_idx, source_output_toggle_mute_callback, NULL));
1324 else
1325 pa_operation_unref(pa_context_set_source_output_mute(c, source_output_idx, mute, simple_callback, NULL));
1326 break;
1327
1328 case SET_SINK_VOLUME:
1329 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1330 pa_operation_unref(pa_context_get_sink_info_by_name(c, sink_name, get_sink_volume_callback, NULL));
1331 } else {
1332 pa_cvolume v;
1333 pa_cvolume_set(&v, 1, volume);
1334 pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &v, simple_callback, NULL));
1335 }
1336 break;
1337
1338 case SET_SOURCE_VOLUME:
1339 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1340 pa_operation_unref(pa_context_get_source_info_by_name(c, source_name, get_source_volume_callback, NULL));
1341 } else {
1342 pa_cvolume v;
1343 pa_cvolume_set(&v, 1, volume);
1344 pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &v, simple_callback, NULL));
1345 }
1346 break;
1347
1348 case SET_SINK_INPUT_VOLUME:
1349 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1350 pa_operation_unref(pa_context_get_sink_input_info(c, sink_input_idx, get_sink_input_volume_callback, NULL));
1351 } else {
1352 pa_cvolume v;
1353 pa_cvolume_set(&v, 1, volume);
1354 pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &v, simple_callback, NULL));
1355 }
1356 break;
1357
1358 case SET_SOURCE_OUTPUT_VOLUME:
1359 if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1360 pa_operation_unref(pa_context_get_source_output_info(c, source_output_idx, get_source_output_volume_callback, NULL));
1361 } else {
1362 pa_cvolume v;
1363 pa_cvolume_set(&v, 1, volume);
1364 pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &v, simple_callback, NULL));
1365 }
1366 break;
1367
1368 case SET_SINK_FORMATS:
1369 set_sink_formats(c, sink_idx, formats);
1370 break;
1371
1372 case SET_PORT_LATENCY_OFFSET:
1373 pa_operation_unref(pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL));
1374 break;
1375
1376 case SUBSCRIBE:
1377 pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL);
1378
1379 pa_operation_unref(pa_context_subscribe(
1380 c,
1381 PA_SUBSCRIPTION_MASK_SINK|
1382 PA_SUBSCRIPTION_MASK_SOURCE|
1383 PA_SUBSCRIPTION_MASK_SINK_INPUT|
1384 PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT|
1385 PA_SUBSCRIPTION_MASK_MODULE|
1386 PA_SUBSCRIPTION_MASK_CLIENT|
1387 PA_SUBSCRIPTION_MASK_SAMPLE_CACHE|
1388 PA_SUBSCRIPTION_MASK_SERVER|
1389 PA_SUBSCRIPTION_MASK_CARD,
1390 NULL,
1391 NULL));
1392 break;
1393
1394 default:
1395 pa_assert_not_reached();
1396 }
1397 break;
1398
1399 case PA_CONTEXT_TERMINATED:
1400 quit(0);
1401 break;
1402
1403 case PA_CONTEXT_FAILED:
1404 default:
1405 pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c)));
1406 quit(1);
1407 }
1408 }
1409
1410 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
1411 pa_log(_("Got SIGINT, exiting."));
1412 quit(0);
1413 }
1414
1415 static int parse_volume(const char *vol_spec, pa_volume_t *vol, enum volume_flags *vol_flags) {
1416 double v;
1417 char *vs;
1418
1419 pa_assert(vol_spec);
1420 pa_assert(vol);
1421 pa_assert(vol_flags);
1422
1423 vs = pa_xstrdup(vol_spec);
1424
1425 *vol_flags = (pa_startswith(vs, "+") || pa_startswith(vs, "-")) ? VOL_RELATIVE : VOL_ABSOLUTE;
1426 if (strchr(vs, '.'))
1427 *vol_flags |= VOL_LINEAR;
1428 if (pa_endswith(vs, "%")) {
1429 *vol_flags |= VOL_PERCENT;
1430 vs[strlen(vs)-1] = 0;
1431 }
1432 if (pa_endswith(vs, "db") || pa_endswith(vs, "dB")) {
1433 *vol_flags |= VOL_DECIBEL;
1434 vs[strlen(vs)-2] = 0;
1435 }
1436
1437 if (pa_atod(vs, &v) < 0) {
1438 pa_log(_("Invalid volume specification"));
1439 pa_xfree(vs);
1440 return -1;
1441 }
1442
1443 pa_xfree(vs);
1444
1445 if ((*vol_flags & VOL_RELATIVE) == VOL_RELATIVE) {
1446 if ((*vol_flags & 0x0F) == VOL_UINT)
1447 v += (double) PA_VOLUME_NORM;
1448 if ((*vol_flags & 0x0F) == VOL_PERCENT)
1449 v += 100.0;
1450 if ((*vol_flags & 0x0F) == VOL_LINEAR)
1451 v += 1.0;
1452 }
1453 if ((*vol_flags & 0x0F) == VOL_PERCENT)
1454 v = v * (double) PA_VOLUME_NORM / 100;
1455 if ((*vol_flags & 0x0F) == VOL_LINEAR)
1456 v = pa_sw_volume_from_linear(v);
1457 if ((*vol_flags & 0x0F) == VOL_DECIBEL)
1458 v = pa_sw_volume_from_dB(v);
1459
1460 if (!PA_VOLUME_IS_VALID((pa_volume_t) v)) {
1461 pa_log(_("Volume outside permissible range.\n"));
1462 return -1;
1463 }
1464
1465 *vol = (pa_volume_t) v;
1466
1467 return 0;
1468 }
1469
1470 static enum mute_flags parse_mute(const char *mute_text) {
1471 int b;
1472
1473 pa_assert(mute_text);
1474
1475 if (pa_streq("toggle", mute_text))
1476 return TOGGLE_MUTE;
1477
1478 b = pa_parse_boolean(mute_text);
1479 switch (b) {
1480 case 0:
1481 return UNMUTE;
1482 case 1:
1483 return MUTE;
1484 default:
1485 return INVALID_MUTE;
1486 }
1487 }
1488
1489 static void help(const char *argv0) {
1490
1491 printf("%s %s %s\n", argv0, _("[options]"), "stat [short]");
1492 printf("%s %s %s\n", argv0, _("[options]"), "info");
1493 printf("%s %s %s %s\n", argv0, _("[options]"), "list [short]", _("[TYPE]"));
1494 printf("%s %s %s\n", argv0, _("[options]"), "exit");
1495 printf("%s %s %s %s\n", argv0, _("[options]"), "upload-sample", _("FILENAME [NAME]"));
1496 printf("%s %s %s %s\n", argv0, _("[options]"), "play-sample ", _("NAME [SINK]"));
1497 printf("%s %s %s %s\n", argv0, _("[options]"), "remove-sample ", _("NAME"));
1498 printf("%s %s %s %s\n", argv0, _("[options]"), "load-module ", _("NAME [ARGS ...]"));
1499 printf("%s %s %s %s\n", argv0, _("[options]"), "unload-module ", _("NAME|#N"));
1500 printf("%s %s %s %s\n", argv0, _("[options]"), "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
1501 printf("%s %s %s %s\n", argv0, _("[options]"), "suspend-(sink|source)", _("NAME|#N 1|0"));
1502 printf("%s %s %s %s\n", argv0, _("[options]"), "set-card-profile ", _("CARD PROFILE"));
1503 printf("%s %s %s %s\n", argv0, _("[options]"), "set-default-(sink|source)", _("NAME"));
1504 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-port", _("NAME|#N PORT"));
1505 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-volume", _("NAME|#N VOLUME"));
1506 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-volume", _("#N VOLUME"));
1507 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-mute", _("NAME|#N 1|0|toggle"));
1508 printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0|toggle"));
1509 printf("%s %s %s %s\n", argv0, _("[options]"), "set-sink-formats", _("#N FORMATS"));
1510 printf("%s %s %s %s\n", argv0, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
1511 printf("%s %s %s\n", argv0, _("[options]"), "subscribe");
1512 printf(_("\nThe special names @DEFAULT_SINK@, @DEFAULT_SOURCE@ and @DEFAULT_MONITOR@\n"
1513 "can be used to specify the default sink, source and monitor.\n"));
1514
1515 printf(_("\n"
1516 " -h, --help Show this help\n"
1517 " --version Show version\n\n"
1518 " -s, --server=SERVER The name of the server to connect to\n"
1519 " -n, --client-name=NAME How to call this client on the server\n"));
1520 }
1521
1522 enum {
1523 ARG_VERSION = 256
1524 };
1525
1526 int main(int argc, char *argv[]) {
1527 pa_mainloop *m = NULL;
1528 int ret = 1, c;
1529 char *server = NULL, *bn;
1530
1531 static const struct option long_options[] = {
1532 {"server", 1, NULL, 's'},
1533 {"client-name", 1, NULL, 'n'},
1534 {"version", 0, NULL, ARG_VERSION},
1535 {"help", 0, NULL, 'h'},
1536 {NULL, 0, NULL, 0}
1537 };
1538
1539 setlocale(LC_ALL, "");
1540 #ifdef ENABLE_NLS
1541 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
1542 #endif
1543
1544 bn = pa_path_get_filename(argv[0]);
1545
1546 proplist = pa_proplist_new();
1547
1548 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
1549 switch (c) {
1550 case 'h' :
1551 help(bn);
1552 ret = 0;
1553 goto quit;
1554
1555 case ARG_VERSION:
1556 printf(_("pactl %s\n"
1557 "Compiled with libpulse %s\n"
1558 "Linked with libpulse %s\n"),
1559 PACKAGE_VERSION,
1560 pa_get_headers_version(),
1561 pa_get_library_version());
1562 ret = 0;
1563 goto quit;
1564
1565 case 's':
1566 pa_xfree(server);
1567 server = pa_xstrdup(optarg);
1568 break;
1569
1570 case 'n': {
1571 char *t;
1572
1573 if (!(t = pa_locale_to_utf8(optarg)) ||
1574 pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t) < 0) {
1575
1576 pa_log(_("Invalid client name '%s'"), t ? t : optarg);
1577 pa_xfree(t);
1578 goto quit;
1579 }
1580
1581 pa_xfree(t);
1582 break;
1583 }
1584
1585 default:
1586 goto quit;
1587 }
1588 }
1589
1590 if (optind < argc) {
1591 if (pa_streq(argv[optind], "stat")) {
1592 action = STAT;
1593 short_list_format = false;
1594 if (optind+1 < argc && pa_streq(argv[optind+1], "short"))
1595 short_list_format = true;
1596
1597 } else if (pa_streq(argv[optind], "info"))
1598 action = INFO;
1599
1600 else if (pa_streq(argv[optind], "exit"))
1601 action = EXIT;
1602
1603 else if (pa_streq(argv[optind], "list")) {
1604 action = LIST;
1605
1606 for (int i = optind+1; i < argc; i++) {
1607 if (pa_streq(argv[i], "modules") || pa_streq(argv[i], "clients") ||
1608 pa_streq(argv[i], "sinks") || pa_streq(argv[i], "sink-inputs") ||
1609 pa_streq(argv[i], "sources") || pa_streq(argv[i], "source-outputs") ||
1610 pa_streq(argv[i], "samples") || pa_streq(argv[i], "cards")) {
1611 list_type = pa_xstrdup(argv[i]);
1612 } else if (pa_streq(argv[i], "short")) {
1613 short_list_format = true;
1614 } else {
1615 pa_log(_("Specify nothing, or one of: %s"), "modules, sinks, sources, sink-inputs, source-outputs, clients, samples, cards");
1616 goto quit;
1617 }
1618 }
1619
1620 } else if (pa_streq(argv[optind], "upload-sample")) {
1621 struct SF_INFO sfi;
1622 action = UPLOAD_SAMPLE;
1623
1624 if (optind+1 >= argc) {
1625 pa_log(_("Please specify a sample file to load"));
1626 goto quit;
1627 }
1628
1629 if (optind+2 < argc)
1630 sample_name = pa_xstrdup(argv[optind+2]);
1631 else {
1632 char *f = pa_path_get_filename(argv[optind+1]);
1633 sample_name = pa_xstrndup(f, strcspn(f, "."));
1634 }
1635
1636 pa_zero(sfi);
1637 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfi))) {
1638 pa_log(_("Failed to open sound file."));
1639 goto quit;
1640 }
1641
1642 if (pa_sndfile_read_sample_spec(sndfile, &sample_spec) < 0) {
1643 pa_log(_("Failed to determine sample specification from file."));
1644 goto quit;
1645 }
1646 sample_spec.format = PA_SAMPLE_FLOAT32;
1647
1648 if (pa_sndfile_read_channel_map(sndfile, &channel_map) < 0) {
1649 if (sample_spec.channels > 2)
1650 pa_log(_("Warning: Failed to determine sample specification from file."));
1651 pa_channel_map_init_extend(&channel_map, sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
1652 }
1653
1654 pa_assert(pa_channel_map_compatible(&channel_map, &sample_spec));
1655 sample_length = (size_t) sfi.frames*pa_frame_size(&sample_spec);
1656
1657 } else if (pa_streq(argv[optind], "play-sample")) {
1658 action = PLAY_SAMPLE;
1659 if (argc != optind+2 && argc != optind+3) {
1660 pa_log(_("You have to specify a sample name to play"));
1661 goto quit;
1662 }
1663
1664 sample_name = pa_xstrdup(argv[optind+1]);
1665
1666 if (optind+2 < argc)
1667 sink_name = pa_xstrdup(argv[optind+2]);
1668
1669 } else if (pa_streq(argv[optind], "remove-sample")) {
1670 action = REMOVE_SAMPLE;
1671 if (argc != optind+2) {
1672 pa_log(_("You have to specify a sample name to remove"));
1673 goto quit;
1674 }
1675
1676 sample_name = pa_xstrdup(argv[optind+1]);
1677
1678 } else if (pa_streq(argv[optind], "move-sink-input")) {
1679 action = MOVE_SINK_INPUT;
1680 if (argc != optind+3) {
1681 pa_log(_("You have to specify a sink input index and a sink"));
1682 goto quit;
1683 }
1684
1685 sink_input_idx = (uint32_t) atoi(argv[optind+1]);
1686 sink_name = pa_xstrdup(argv[optind+2]);
1687
1688 } else if (pa_streq(argv[optind], "move-source-output")) {
1689 action = MOVE_SOURCE_OUTPUT;
1690 if (argc != optind+3) {
1691 pa_log(_("You have to specify a source output index and a source"));
1692 goto quit;
1693 }
1694
1695 source_output_idx = (uint32_t) atoi(argv[optind+1]);
1696 source_name = pa_xstrdup(argv[optind+2]);
1697
1698 } else if (pa_streq(argv[optind], "load-module")) {
1699 int i;
1700 size_t n = 0;
1701 char *p;
1702
1703 action = LOAD_MODULE;
1704
1705 if (argc <= optind+1) {
1706 pa_log(_("You have to specify a module name and arguments."));
1707 goto quit;
1708 }
1709
1710 module_name = argv[optind+1];
1711
1712 for (i = optind+2; i < argc; i++)
1713 n += strlen(argv[i])+1;
1714
1715 if (n > 0) {
1716 p = module_args = pa_xmalloc(n);
1717
1718 for (i = optind+2; i < argc; i++)
1719 p += sprintf(p, "%s%s", p == module_args ? "" : " ", argv[i]);
1720 }
1721
1722 } else if (pa_streq(argv[optind], "unload-module")) {
1723 action = UNLOAD_MODULE;
1724
1725 if (argc != optind+2) {
1726 pa_log(_("You have to specify a module index or name"));
1727 goto quit;
1728 }
1729
1730 if (pa_atou(argv[optind + 1], &module_index) < 0)
1731 module_name = argv[optind + 1];
1732
1733 } else if (pa_streq(argv[optind], "suspend-sink")) {
1734 int b;
1735
1736 action = SUSPEND_SINK;
1737
1738 if (argc > optind+3 || optind+1 >= argc) {
1739 pa_log(_("You may not specify more than one sink. You have to specify a boolean value."));
1740 goto quit;
1741 }
1742
1743 if ((b = pa_parse_boolean(argv[argc-1])) < 0) {
1744 pa_log(_("Invalid suspend specification."));
1745 goto quit;
1746 }
1747
1748 suspend = !!b;
1749
1750 if (argc > optind+2)
1751 sink_name = pa_xstrdup(argv[optind+1]);
1752
1753 } else if (pa_streq(argv[optind], "suspend-source")) {
1754 int b;
1755
1756 action = SUSPEND_SOURCE;
1757
1758 if (argc > optind+3 || optind+1 >= argc) {
1759 pa_log(_("You may not specify more than one source. You have to specify a boolean value."));
1760 goto quit;
1761 }
1762
1763 if ((b = pa_parse_boolean(argv[argc-1])) < 0) {
1764 pa_log(_("Invalid suspend specification."));
1765 goto quit;
1766 }
1767
1768 suspend = !!b;
1769
1770 if (argc > optind+2)
1771 source_name = pa_xstrdup(argv[optind+1]);
1772 } else if (pa_streq(argv[optind], "set-card-profile")) {
1773 action = SET_CARD_PROFILE;
1774
1775 if (argc != optind+3) {
1776 pa_log(_("You have to specify a card name/index and a profile name"));
1777 goto quit;
1778 }
1779
1780 card_name = pa_xstrdup(argv[optind+1]);
1781 profile_name = pa_xstrdup(argv[optind+2]);
1782
1783 } else if (pa_streq(argv[optind], "set-sink-port")) {
1784 action = SET_SINK_PORT;
1785
1786 if (argc != optind+3) {
1787 pa_log(_("You have to specify a sink name/index and a port name"));
1788 goto quit;
1789 }
1790
1791 sink_name = pa_xstrdup(argv[optind+1]);
1792 port_name = pa_xstrdup(argv[optind+2]);
1793
1794 } else if (pa_streq(argv[optind], "set-default-sink")) {
1795 action = SET_DEFAULT_SINK;
1796
1797 if (argc != optind+2) {
1798 pa_log(_("You have to specify a sink name"));
1799 goto quit;
1800 }
1801
1802 sink_name = pa_xstrdup(argv[optind+1]);
1803
1804 } else if (pa_streq(argv[optind], "set-source-port")) {
1805 action = SET_SOURCE_PORT;
1806
1807 if (argc != optind+3) {
1808 pa_log(_("You have to specify a source name/index and a port name"));
1809 goto quit;
1810 }
1811
1812 source_name = pa_xstrdup(argv[optind+1]);
1813 port_name = pa_xstrdup(argv[optind+2]);
1814
1815 } else if (pa_streq(argv[optind], "set-default-source")) {
1816 action = SET_DEFAULT_SOURCE;
1817
1818 if (argc != optind+2) {
1819 pa_log(_("You have to specify a source name"));
1820 goto quit;
1821 }
1822
1823 source_name = pa_xstrdup(argv[optind+1]);
1824
1825 } else if (pa_streq(argv[optind], "set-sink-volume")) {
1826 action = SET_SINK_VOLUME;
1827
1828 if (argc != optind+3) {
1829 pa_log(_("You have to specify a sink name/index and a volume"));
1830 goto quit;
1831 }
1832
1833 sink_name = pa_xstrdup(argv[optind+1]);
1834
1835 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1836 goto quit;
1837
1838 } else if (pa_streq(argv[optind], "set-source-volume")) {
1839 action = SET_SOURCE_VOLUME;
1840
1841 if (argc != optind+3) {
1842 pa_log(_("You have to specify a source name/index and a volume"));
1843 goto quit;
1844 }
1845
1846 source_name = pa_xstrdup(argv[optind+1]);
1847
1848 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1849 goto quit;
1850
1851 } else if (pa_streq(argv[optind], "set-sink-input-volume")) {
1852 action = SET_SINK_INPUT_VOLUME;
1853
1854 if (argc != optind+3) {
1855 pa_log(_("You have to specify a sink input index and a volume"));
1856 goto quit;
1857 }
1858
1859 if (pa_atou(argv[optind+1], &sink_input_idx) < 0) {
1860 pa_log(_("Invalid sink input index"));
1861 goto quit;
1862 }
1863
1864 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1865 goto quit;
1866
1867 } else if (pa_streq(argv[optind], "set-source-output-volume")) {
1868 action = SET_SOURCE_OUTPUT_VOLUME;
1869
1870 if (argc != optind+3) {
1871 pa_log(_("You have to specify a source output index and a volume"));
1872 goto quit;
1873 }
1874
1875 if (pa_atou(argv[optind+1], &source_output_idx) < 0) {
1876 pa_log(_("Invalid source output index"));
1877 goto quit;
1878 }
1879
1880 if (parse_volume(argv[optind+2], &volume, &volume_flags) < 0)
1881 goto quit;
1882
1883 } else if (pa_streq(argv[optind], "set-sink-mute")) {
1884 action = SET_SINK_MUTE;
1885
1886 if (argc != optind+3) {
1887 pa_log(_("You have to specify a sink name/index and a mute boolean"));
1888 goto quit;
1889 }
1890
1891 if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
1892 pa_log(_("Invalid mute specification"));
1893 goto quit;
1894 }
1895
1896 sink_name = pa_xstrdup(argv[optind+1]);
1897
1898 } else if (pa_streq(argv[optind], "set-source-mute")) {
1899 action = SET_SOURCE_MUTE;
1900
1901 if (argc != optind+3) {
1902 pa_log(_("You have to specify a source name/index and a mute boolean"));
1903 goto quit;
1904 }
1905
1906 if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
1907 pa_log(_("Invalid mute specification"));
1908 goto quit;
1909 }
1910
1911 source_name = pa_xstrdup(argv[optind+1]);
1912
1913 } else if (pa_streq(argv[optind], "set-sink-input-mute")) {
1914 action = SET_SINK_INPUT_MUTE;
1915
1916 if (argc != optind+3) {
1917 pa_log(_("You have to specify a sink input index and a mute boolean"));
1918 goto quit;
1919 }
1920
1921 if (pa_atou(argv[optind+1], &sink_input_idx) < 0) {
1922 pa_log(_("Invalid sink input index specification"));
1923 goto quit;
1924 }
1925
1926 if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
1927 pa_log(_("Invalid mute specification"));
1928 goto quit;
1929 }
1930
1931 } else if (pa_streq(argv[optind], "set-source-output-mute")) {
1932 action = SET_SOURCE_OUTPUT_MUTE;
1933
1934 if (argc != optind+3) {
1935 pa_log(_("You have to specify a source output index and a mute boolean"));
1936 goto quit;
1937 }
1938
1939 if (pa_atou(argv[optind+1], &source_output_idx) < 0) {
1940 pa_log(_("Invalid source output index specification"));
1941 goto quit;
1942 }
1943
1944 if ((mute = parse_mute(argv[optind+2])) == INVALID_MUTE) {
1945 pa_log(_("Invalid mute specification"));
1946 goto quit;
1947 }
1948
1949 } else if (pa_streq(argv[optind], "subscribe"))
1950
1951 action = SUBSCRIBE;
1952
1953 else if (pa_streq(argv[optind], "set-sink-formats")) {
1954 int32_t tmp;
1955
1956 if (argc != optind+3 || pa_atoi(argv[optind+1], &tmp) < 0) {
1957 pa_log(_("You have to specify a sink index and a semicolon-separated list of supported formats"));
1958 goto quit;
1959 }
1960
1961 sink_idx = tmp;
1962 action = SET_SINK_FORMATS;
1963 formats = pa_xstrdup(argv[optind+2]);
1964
1965 } else if (pa_streq(argv[optind], "set-port-latency-offset")) {
1966 action = SET_PORT_LATENCY_OFFSET;
1967
1968 if (argc != optind+4) {
1969 pa_log(_("You have to specify a card name/index, a port name and a latency offset"));
1970 goto quit;
1971 }
1972
1973 card_name = pa_xstrdup(argv[optind+1]);
1974 port_name = pa_xstrdup(argv[optind+2]);
1975 if (pa_atoi(argv[optind + 3], &latency_offset) < 0) {
1976 pa_log(_("Could not parse latency offset"));
1977 goto quit;
1978 }
1979
1980 } else if (pa_streq(argv[optind], "help")) {
1981 help(bn);
1982 ret = 0;
1983 goto quit;
1984 }
1985 }
1986
1987 if (action == NONE) {
1988 pa_log(_("No valid command specified."));
1989 goto quit;
1990 }
1991
1992 if (!(m = pa_mainloop_new())) {
1993 pa_log(_("pa_mainloop_new() failed."));
1994 goto quit;
1995 }
1996
1997 mainloop_api = pa_mainloop_get_api(m);
1998
1999 pa_assert_se(pa_signal_init(mainloop_api) == 0);
2000 pa_signal_new(SIGINT, exit_signal_callback, NULL);
2001 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
2002 pa_disable_sigpipe();
2003
2004 if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) {
2005 pa_log(_("pa_context_new() failed."));
2006 goto quit;
2007 }
2008
2009 pa_context_set_state_callback(context, context_state_callback, NULL);
2010 if (pa_context_connect(context, server, 0, NULL) < 0) {
2011 pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
2012 goto quit;
2013 }
2014
2015 if (pa_mainloop_run(m, &ret) < 0) {
2016 pa_log(_("pa_mainloop_run() failed."));
2017 goto quit;
2018 }
2019
2020 quit:
2021 if (sample_stream)
2022 pa_stream_unref(sample_stream);
2023
2024 if (context)
2025 pa_context_unref(context);
2026
2027 if (m) {
2028 pa_signal_done();
2029 pa_mainloop_free(m);
2030 }
2031
2032 pa_xfree(server);
2033 pa_xfree(list_type);
2034 pa_xfree(sample_name);
2035 pa_xfree(sink_name);
2036 pa_xfree(source_name);
2037 pa_xfree(module_args);
2038 pa_xfree(card_name);
2039 pa_xfree(profile_name);
2040 pa_xfree(port_name);
2041 pa_xfree(formats);
2042
2043 if (sndfile)
2044 sf_close(sndfile);
2045
2046 if (proplist)
2047 pa_proplist_free(proplist);
2048
2049 return ret;
2050 }