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