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