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