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