]> code.delx.au - pulseaudio/blob - src/utils/pactl.c
add a few more gcc warning flags and fix quite a few problems found by doing so
[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 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 <limits.h>
34 #include <getopt.h>
35 #include <locale.h>
36
37 #include <sndfile.h>
38
39 #include <pulse/i18n.h>
40 #include <pulse/pulseaudio.h>
41 #include <pulsecore/core-util.h>
42
43 #define BUFSIZE 1024
44
45 static pa_context *context = NULL;
46 static pa_mainloop_api *mainloop_api = NULL;
47
48 static char *device = NULL, *sample_name = NULL, *sink_name = NULL, *source_name = NULL, *module_name = NULL, *module_args = NULL;
49 static uint32_t sink_input_idx = PA_INVALID_INDEX, source_output_idx = PA_INVALID_INDEX;
50 static uint32_t module_index;
51 static int suspend;
52
53 static SNDFILE *sndfile = NULL;
54 static pa_stream *sample_stream = NULL;
55 static pa_sample_spec sample_spec;
56 static size_t sample_length = 0;
57
58 static int actions = 1;
59
60 static int nl = 0;
61
62 static enum {
63 NONE,
64 EXIT,
65 STAT,
66 UPLOAD_SAMPLE,
67 PLAY_SAMPLE,
68 REMOVE_SAMPLE,
69 LIST,
70 MOVE_SINK_INPUT,
71 MOVE_SOURCE_OUTPUT,
72 LOAD_MODULE,
73 UNLOAD_MODULE,
74 SUSPEND_SINK,
75 SUSPEND_SOURCE,
76 } action = NONE;
77
78 static void quit(int ret) {
79 assert(mainloop_api);
80 mainloop_api->quit(mainloop_api, ret);
81 }
82
83
84 static void context_drain_complete(pa_context *c, void *userdata) {
85 pa_context_disconnect(c);
86 }
87
88 static void drain(void) {
89 pa_operation *o;
90 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
91 pa_context_disconnect(context);
92 else
93 pa_operation_unref(o);
94 }
95
96
97 static void complete_action(void) {
98 assert(actions > 0);
99
100 if (!(--actions))
101 drain();
102 }
103
104 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
105 char s[128];
106 if (!i) {
107 fprintf(stderr, _("Failed to get statistics: %s\n"), pa_strerror(pa_context_errno(c)));
108 quit(1);
109 return;
110 }
111
112 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
113 printf(_("Currently in use: %u blocks containing %s bytes total.\n"), i->memblock_total, s);
114
115 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
116 printf(_("Allocated during whole lifetime: %u blocks containing %s bytes total.\n"), i->memblock_allocated, s);
117
118 pa_bytes_snprint(s, sizeof(s), i->scache_size);
119 printf(_("Sample cache size: %s\n"), s);
120
121 complete_action();
122 }
123
124 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
125 char s[PA_SAMPLE_SPEC_SNPRINT_MAX];
126
127 if (!i) {
128 fprintf(stderr, _("Failed to get server information: %s\n"), pa_strerror(pa_context_errno(c)));
129 quit(1);
130 return;
131 }
132
133 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec);
134
135 printf(_("User name: %s\n"
136 "Host Name: %s\n"
137 "Server Name: %s\n"
138 "Server Version: %s\n"
139 "Default Sample Specification: %s\n"
140 "Default Sink: %s\n"
141 "Default Source: %s\n"
142 "Cookie: %08x\n"),
143 i->user_name,
144 i->host_name,
145 i->server_name,
146 i->server_version,
147 s,
148 i->default_sink_name,
149 i->default_source_name,
150 i->cookie);
151
152 complete_action();
153 }
154
155 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
156 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
157 char *pl;
158
159 if (is_last < 0) {
160 fprintf(stderr, _("Failed to get sink information: %s\n"), pa_strerror(pa_context_errno(c)));
161 quit(1);
162 return;
163 }
164
165 if (is_last) {
166 complete_action();
167 return;
168 }
169
170 assert(i);
171
172 if (nl)
173 printf("\n");
174 nl = 1;
175
176 printf(_("*** Sink #%u ***\n"
177 "Name: %s\n"
178 "Driver: %s\n"
179 "Sample Specification: %s\n"
180 "Channel Map: %s\n"
181 "Owner Module: %u\n"
182 "Volume: %s\n"
183 "Monitor Source: %s\n"
184 "Latency: %0.0f usec, configured %0.0f usec\n"
185 "Flags: %s%s%s%s%s%s\n"
186 "Properties:\n%s"),
187 i->index,
188 i->name,
189 pa_strnull(i->driver),
190 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
191 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
192 i->owner_module,
193 i->mute ? _("muted") : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
194 pa_strnull(i->monitor_source_name),
195 (double) i->latency, (double) i->configured_latency,
196 i->flags & PA_SINK_HARDWARE ? "HARDWARE " : "",
197 i->flags & PA_SINK_NETWORK ? "NETWORK " : "",
198 i->flags & PA_SINK_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
199 i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
200 i->flags & PA_SINK_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
201 i->flags & PA_SINK_LATENCY ? "LATENCY " : "",
202 pl = pa_proplist_to_string(i->proplist));
203
204 pa_xfree(pl);
205 }
206
207 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
208 char s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
209 char *pl;
210
211 if (is_last < 0) {
212 fprintf(stderr, _("Failed to get source information: %s\n"), pa_strerror(pa_context_errno(c)));
213 quit(1);
214 return;
215 }
216
217 if (is_last) {
218 complete_action();
219 return;
220 }
221
222 assert(i);
223
224 if (nl)
225 printf("\n");
226 nl = 1;
227
228 printf(_("*** Source #%u ***\n"
229 "Name: %s\n"
230 "Driver: %s\n"
231 "Sample Specification: %s\n"
232 "Channel Map: %s\n"
233 "Owner Module: %u\n"
234 "Volume: %s\n"
235 "Monitor of Sink: %s\n"
236 "Latency: %0.0f usec, configured %0.0f usec\n"
237 "Flags: %s%s%s%s%s%s\n"
238 "Properties:\n%s"),
239 i->index,
240 i->name,
241 pa_strnull(i->driver),
242 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
243 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
244 i->owner_module,
245 i->mute ? "muted" : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
246 i->monitor_of_sink_name ? i->monitor_of_sink_name : _("n/a"),
247 (double) i->latency, (double) i->configured_latency,
248 i->flags & PA_SOURCE_HARDWARE ? "HARDWARE " : "",
249 i->flags & PA_SOURCE_NETWORK ? "NETWORK " : "",
250 i->flags & PA_SOURCE_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
251 i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
252 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
253 i->flags & PA_SOURCE_LATENCY ? "LATENCY " : "",
254 pl = pa_proplist_to_string(i->proplist));
255
256 pa_xfree(pl);
257 }
258
259 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
260 char t[32];
261
262 if (is_last < 0) {
263 fprintf(stderr, _("Failed to get module information: %s\n"), pa_strerror(pa_context_errno(c)));
264 quit(1);
265 return;
266 }
267
268 if (is_last) {
269 complete_action();
270 return;
271 }
272
273 assert(i);
274
275 if (nl)
276 printf("\n");
277 nl = 1;
278
279 snprintf(t, sizeof(t), "%u", i->n_used);
280
281 printf(_("*** Module #%u ***\n"
282 "Name: %s\n"
283 "Argument: %s\n"
284 "Usage counter: %s\n"
285 "Auto unload: %s\n"),
286 i->index,
287 i->name,
288 i->argument ? i->argument : "",
289 i->n_used != PA_INVALID_INDEX ? t : _("n/a"),
290 pa_yes_no(i->auto_unload));
291 }
292
293 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
294 char t[32];
295 char *pl;
296
297 if (is_last < 0) {
298 fprintf(stderr, _("Failed to get client information: %s\n"), pa_strerror(pa_context_errno(c)));
299 quit(1);
300 return;
301 }
302
303 if (is_last) {
304 complete_action();
305 return;
306 }
307
308 assert(i);
309
310 if (nl)
311 printf("\n");
312 nl = 1;
313
314 snprintf(t, sizeof(t), "%u", i->owner_module);
315
316 printf(_("*** Client #%u ***\n"
317 "Driver: %s\n"
318 "Owner Module: %s\n"
319 "Properties:\n%s"),
320 i->index,
321 pa_strnull(i->driver),
322 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
323 pl = pa_proplist_to_string(i->proplist));
324
325 pa_xfree(pl);
326 }
327
328 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
329 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
330 char *pl;
331
332 if (is_last < 0) {
333 fprintf(stderr, _("Failed to get sink input information: %s\n"), pa_strerror(pa_context_errno(c)));
334 quit(1);
335 return;
336 }
337
338 if (is_last) {
339 complete_action();
340 return;
341 }
342
343 assert(i);
344
345 if (nl)
346 printf("\n");
347 nl = 1;
348
349 snprintf(t, sizeof(t), "%u", i->owner_module);
350 snprintf(k, sizeof(k), "%u", i->client);
351
352 printf(_("*** Sink Input #%u ***\n"
353 "Driver: %s\n"
354 "Owner Module: %s\n"
355 "Client: %s\n"
356 "Sink: %u\n"
357 "Sample Specification: %s\n"
358 "Channel Map: %s\n"
359 "Volume: %s\n"
360 "Buffer Latency: %0.0f usec\n"
361 "Sink Latency: %0.0f usec\n"
362 "Resample method: %s\n"
363 "Properties:\n%s"),
364 i->index,
365 pa_strnull(i->driver),
366 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
367 i->client != PA_INVALID_INDEX ? k : _("n/a"),
368 i->sink,
369 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
370 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
371 i->mute ? _("muted") : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
372 (double) i->buffer_usec,
373 (double) i->sink_usec,
374 i->resample_method ? i->resample_method : _("n/a"),
375 pl = pa_proplist_to_string(i->proplist));
376
377 pa_xfree(pl);
378 }
379
380 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
381 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
382 char *pl;
383
384 if (is_last < 0) {
385 fprintf(stderr, _("Failed to get source output information: %s\n"), pa_strerror(pa_context_errno(c)));
386 quit(1);
387 return;
388 }
389
390 if (is_last) {
391 complete_action();
392 return;
393 }
394
395 assert(i);
396
397 if (nl)
398 printf("\n");
399 nl = 1;
400
401
402 snprintf(t, sizeof(t), "%u", i->owner_module);
403 snprintf(k, sizeof(k), "%u", i->client);
404
405 printf(_("*** Source Output #%u ***\n"
406 "Driver: %s\n"
407 "Owner Module: %s\n"
408 "Client: %s\n"
409 "Source: %u\n"
410 "Sample Specification: %s\n"
411 "Channel Map: %s\n"
412 "Buffer Latency: %0.0f usec\n"
413 "Source Latency: %0.0f usec\n"
414 "Resample method: %s\n"
415 "Properties:\n%s"),
416 i->index,
417 pa_strnull(i->driver),
418 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
419 i->client != PA_INVALID_INDEX ? k : _("n/a"),
420 i->source,
421 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
422 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
423 (double) i->buffer_usec,
424 (double) i->source_usec,
425 i->resample_method ? i->resample_method : _("n/a"),
426 pl = pa_proplist_to_string(i->proplist));
427
428 pa_xfree(pl);
429 }
430
431 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
432 char t[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
433 char *pl;
434
435 if (is_last < 0) {
436 fprintf(stderr, _("Failed to get sample information: %s\n"), pa_strerror(pa_context_errno(c)));
437 quit(1);
438 return;
439 }
440
441 if (is_last) {
442 complete_action();
443 return;
444 }
445
446 assert(i);
447
448 if (nl)
449 printf("\n");
450 nl = 1;
451
452
453 pa_bytes_snprint(t, sizeof(t), i->bytes);
454
455 printf(_("*** Sample #%u ***\n"
456 "Name: %s\n"
457 "Volume: %s\n"
458 "Sample Specification: %s\n"
459 "Channel Map: %s\n"
460 "Duration: %0.1fs\n"
461 "Size: %s\n"
462 "Lazy: %s\n"
463 "Filename: %s\n"
464 "Properties:\n%s"),
465 i->index,
466 i->name,
467 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
468 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : _("n/a"),
469 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : _("n/a"),
470 (double) i->duration/1000000,
471 t,
472 pa_yes_no(i->lazy),
473 i->filename ? i->filename : _("n/a"),
474 pl = pa_proplist_to_string(i->proplist));
475
476 pa_xfree(pl);
477 }
478
479 static void get_autoload_info_callback(pa_context *c, const pa_autoload_info *i, int is_last, void *userdata) {
480 if (is_last < 0) {
481 fprintf(stderr, _("Failed to get autoload information: %s\n"), pa_strerror(pa_context_errno(c)));
482 quit(1);
483 return;
484 }
485
486 if (is_last) {
487 complete_action();
488 return;
489 }
490
491 assert(i);
492
493 if (nl)
494 printf("\n");
495 nl = 1;
496
497 printf(_("*** Autoload Entry #%u ***\n"
498 "Name: %s\n"
499 "Type: %s\n"
500 "Module: %s\n"
501 "Argument: %s\n"),
502 i->index,
503 i->name,
504 i->type == PA_AUTOLOAD_SINK ? _("sink") : _("source"),
505 i->module,
506 i->argument ? i->argument : "");
507 }
508
509 static void simple_callback(pa_context *c, int success, void *userdata) {
510 if (!success) {
511 fprintf(stderr, _("Failure: %s\n"), pa_strerror(pa_context_errno(c)));
512 quit(1);
513 return;
514 }
515
516 complete_action();
517 }
518
519 static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
520 if (idx == PA_INVALID_INDEX) {
521 fprintf(stderr, _("Failure: %s\n"), pa_strerror(pa_context_errno(c)));
522 quit(1);
523 return;
524 }
525
526 printf("%u\n", idx);
527
528 complete_action();
529 }
530
531 static void stream_state_callback(pa_stream *s, void *userdata) {
532 assert(s);
533
534 switch (pa_stream_get_state(s)) {
535 case PA_STREAM_CREATING:
536 case PA_STREAM_READY:
537 break;
538
539 case PA_STREAM_TERMINATED:
540 drain();
541 break;
542
543 case PA_STREAM_FAILED:
544 default:
545 fprintf(stderr, _("Failed to upload sample: %s\n"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
546 quit(1);
547 }
548 }
549
550 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
551 sf_count_t l;
552 float *d;
553 assert(s && length && sndfile);
554
555 d = pa_xmalloc(length);
556
557 assert(sample_length >= length);
558 l = (sf_count_t) (length/pa_frame_size(&sample_spec));
559
560 if ((sf_readf_float(sndfile, d, l)) != l) {
561 pa_xfree(d);
562 fprintf(stderr, _("Premature end of file\n"));
563 quit(1);
564 }
565
566 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
567
568 sample_length -= length;
569
570 if (sample_length <= 0) {
571 pa_stream_set_write_callback(sample_stream, NULL, NULL);
572 pa_stream_finish_upload(sample_stream);
573 }
574 }
575
576 static void context_state_callback(pa_context *c, void *userdata) {
577 assert(c);
578 switch (pa_context_get_state(c)) {
579 case PA_CONTEXT_CONNECTING:
580 case PA_CONTEXT_AUTHORIZING:
581 case PA_CONTEXT_SETTING_NAME:
582 break;
583
584 case PA_CONTEXT_READY:
585 switch (action) {
586 case STAT:
587 actions = 2;
588 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
589 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
590 break;
591
592 case PLAY_SAMPLE:
593 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
594 break;
595
596 case REMOVE_SAMPLE:
597 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
598 break;
599
600 case UPLOAD_SAMPLE:
601 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
602 assert(sample_stream);
603
604 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
605 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
606 pa_stream_connect_upload(sample_stream, sample_length);
607 break;
608
609 case EXIT:
610 pa_operation_unref(pa_context_exit_daemon(c, simple_callback, NULL));
611 break;
612
613 case LIST:
614 actions = 8;
615 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
616 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
617 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
618 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
619 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
620 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
621 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
622 pa_operation_unref(pa_context_get_autoload_info_list(c, get_autoload_info_callback, NULL));
623 break;
624
625 case MOVE_SINK_INPUT:
626 pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL));
627 break;
628
629 case MOVE_SOURCE_OUTPUT:
630 pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL));
631 break;
632
633 case LOAD_MODULE:
634 pa_operation_unref(pa_context_load_module(c, module_name, module_args, index_callback, NULL));
635 break;
636
637 case UNLOAD_MODULE:
638 pa_operation_unref(pa_context_unload_module(c, module_index, simple_callback, NULL));
639 break;
640
641 case SUSPEND_SINK:
642 if (sink_name)
643 pa_operation_unref(pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL));
644 else
645 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
646 break;
647
648 case SUSPEND_SOURCE:
649 if (source_name)
650 pa_operation_unref(pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL));
651 else
652 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
653 break;
654
655 default:
656 assert(0);
657 }
658 break;
659
660 case PA_CONTEXT_TERMINATED:
661 quit(0);
662 break;
663
664 case PA_CONTEXT_FAILED:
665 default:
666 fprintf(stderr, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
667 quit(1);
668 }
669 }
670
671 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
672 fprintf(stderr, _("Got SIGINT, exiting.\n"));
673 quit(0);
674 }
675
676 static void help(const char *argv0) {
677
678 printf(_("%s [options] stat\n"
679 "%s [options] list\n"
680 "%s [options] exit\n"
681 "%s [options] upload-sample FILENAME [NAME]\n"
682 "%s [options] play-sample NAME [SINK]\n"
683 "%s [options] remove-sample NAME\n"
684 "%s [options] move-sink-input ID SINK\n"
685 "%s [options] move-source-output ID SOURCE\n"
686 "%s [options] load-module NAME [ARGS ...]\n"
687 "%s [options] unload-module ID\n"
688 "%s [options] suspend-sink [SINK] 1|0\n"
689 "%s [options] suspend-source [SOURCE] 1|0\n\n"
690 " -h, --help Show this help\n"
691 " --version Show version\n\n"
692 " -s, --server=SERVER The name of the server to connect to\n"
693 " -n, --client-name=NAME How to call this client on the server\n"),
694 argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0);
695 }
696
697 enum { ARG_VERSION = 256 };
698
699 int main(int argc, char *argv[]) {
700 pa_mainloop* m = NULL;
701 char tmp[PATH_MAX];
702 int ret = 1, r, c;
703 char *server = NULL, *client_name = NULL, *bn;
704
705 static const struct option long_options[] = {
706 {"server", 1, NULL, 's'},
707 {"client-name", 1, NULL, 'n'},
708 {"version", 0, NULL, ARG_VERSION},
709 {"help", 0, NULL, 'h'},
710 {NULL, 0, NULL, 0}
711 };
712
713 setlocale(LC_ALL, "");
714 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
715
716 if (!(bn = strrchr(argv[0], '/')))
717 bn = argv[0];
718 else
719 bn++;
720
721 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
722 switch (c) {
723 case 'h' :
724 help(bn);
725 ret = 0;
726 goto quit;
727
728 case ARG_VERSION:
729 printf(_("pactl %s\n"
730 "Compiled with libpulse %s\n"
731 "Linked with libpulse %s\n"),
732 PACKAGE_VERSION,
733 pa_get_headers_version(),
734 pa_get_library_version());
735 ret = 0;
736 goto quit;
737
738 case 's':
739 pa_xfree(server);
740 server = pa_xstrdup(optarg);
741 break;
742
743 case 'n':
744 pa_xfree(client_name);
745 client_name = pa_xstrdup(optarg);
746 break;
747
748 default:
749 goto quit;
750 }
751 }
752
753 if (!client_name)
754 client_name = pa_xstrdup(bn);
755
756 if (optind < argc) {
757 if (!strcmp(argv[optind], "stat"))
758 action = STAT;
759 else if (!strcmp(argv[optind], "exit"))
760 action = EXIT;
761 else if (!strcmp(argv[optind], "list"))
762 action = LIST;
763 else if (!strcmp(argv[optind], "upload-sample")) {
764 struct SF_INFO sfinfo;
765 action = UPLOAD_SAMPLE;
766
767 if (optind+1 >= argc) {
768 fprintf(stderr, _("Please specify a sample file to load\n"));
769 goto quit;
770 }
771
772 if (optind+2 < argc)
773 sample_name = pa_xstrdup(argv[optind+2]);
774 else {
775 char *f = strrchr(argv[optind+1], '/');
776 size_t n;
777 if (f)
778 f++;
779 else
780 f = argv[optind];
781
782 n = strcspn(f, ".");
783 strncpy(tmp, f, n);
784 tmp[n] = 0;
785 sample_name = pa_xstrdup(tmp);
786 }
787
788 memset(&sfinfo, 0, sizeof(sfinfo));
789 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfinfo))) {
790 fprintf(stderr, _("Failed to open sound file.\n"));
791 goto quit;
792 }
793
794 sample_spec.format = PA_SAMPLE_FLOAT32;
795 sample_spec.rate = (uint32_t) sfinfo.samplerate;
796 sample_spec.channels = (uint8_t) sfinfo.channels;
797
798 sample_length = (size_t)sfinfo.frames*pa_frame_size(&sample_spec);
799 } else if (!strcmp(argv[optind], "play-sample")) {
800 action = PLAY_SAMPLE;
801 if (argc != optind+2 && argc != optind+3) {
802 fprintf(stderr, _("You have to specify a sample name to play\n"));
803 goto quit;
804 }
805
806 sample_name = pa_xstrdup(argv[optind+1]);
807
808 if (optind+2 < argc)
809 device = pa_xstrdup(argv[optind+2]);
810
811 } else if (!strcmp(argv[optind], "remove-sample")) {
812 action = REMOVE_SAMPLE;
813 if (argc != optind+2) {
814 fprintf(stderr, _("You have to specify a sample name to remove\n"));
815 goto quit;
816 }
817
818 sample_name = pa_xstrdup(argv[optind+1]);
819 } else if (!strcmp(argv[optind], "move-sink-input")) {
820 action = MOVE_SINK_INPUT;
821 if (argc != optind+3) {
822 fprintf(stderr, _("You have to specify a sink input index and a sink\n"));
823 goto quit;
824 }
825
826 sink_input_idx = (uint32_t) atoi(argv[optind+1]);
827 sink_name = pa_xstrdup(argv[optind+2]);
828 } else if (!strcmp(argv[optind], "move-source-output")) {
829 action = MOVE_SOURCE_OUTPUT;
830 if (argc != optind+3) {
831 fprintf(stderr, _("You have to specify a source output index and a source\n"));
832 goto quit;
833 }
834
835 source_output_idx = (uint32_t) atoi(argv[optind+1]);
836 source_name = pa_xstrdup(argv[optind+2]);
837 } else if (!strcmp(argv[optind], "load-module")) {
838 int i;
839 size_t n = 0;
840 char *p;
841
842 action = LOAD_MODULE;
843
844 if (argc <= optind+1) {
845 fprintf(stderr, _("You have to specify a module name and arguments.\n"));
846 goto quit;
847 }
848
849 module_name = argv[optind+1];
850
851 for (i = optind+2; i < argc; i++)
852 n += strlen(argv[i])+1;
853
854 if (n > 0) {
855 p = module_args = pa_xmalloc(n);
856
857 for (i = optind+2; i < argc; i++)
858 p += sprintf(p, "%s%s", p == module_args ? "" : " ", argv[i]);
859 }
860
861 } else if (!strcmp(argv[optind], "unload-module")) {
862 action = UNLOAD_MODULE;
863
864 if (argc != optind+2) {
865 fprintf(stderr, _("You have to specify a module index\n"));
866 goto quit;
867 }
868
869 module_index = (uint32_t) atoi(argv[optind+1]);
870
871 } else if (!strcmp(argv[optind], "suspend-sink")) {
872 action = SUSPEND_SINK;
873
874 if (argc > optind+3 || optind+1 >= argc) {
875 fprintf(stderr, _("You may not specify more than one sink. You have to specify at least one boolean value.\n"));
876 goto quit;
877 }
878
879 suspend = pa_parse_boolean(argv[argc-1]);
880
881 if (argc > optind+2)
882 sink_name = pa_xstrdup(argv[optind+1]);
883
884 } else if (!strcmp(argv[optind], "suspend-source")) {
885 action = SUSPEND_SOURCE;
886
887 if (argc > optind+3 || optind+1 >= argc) {
888 fprintf(stderr, _("You may not specify more than one source. You have to specify at least one boolean value.\n"));
889 goto quit;
890 }
891
892 suspend = pa_parse_boolean(argv[argc-1]);
893
894 if (argc > optind+2)
895 source_name = pa_xstrdup(argv[optind+1]);
896 } else if (!strcmp(argv[optind], "help")) {
897 help(bn);
898 ret = 0;
899 goto quit;
900 }
901 }
902
903 if (action == NONE) {
904 fprintf(stderr, _("No valid command specified.\n"));
905 goto quit;
906 }
907
908 if (!(m = pa_mainloop_new())) {
909 fprintf(stderr, _("pa_mainloop_new() failed.\n"));
910 goto quit;
911 }
912
913 mainloop_api = pa_mainloop_get_api(m);
914
915 r = pa_signal_init(mainloop_api);
916 assert(r == 0);
917 pa_signal_new(SIGINT, exit_signal_callback, NULL);
918 #ifdef SIGPIPE
919 signal(SIGPIPE, SIG_IGN);
920 #endif
921
922 if (!(context = pa_context_new(mainloop_api, client_name))) {
923 fprintf(stderr, _("pa_context_new() failed.\n"));
924 goto quit;
925 }
926
927 pa_context_set_state_callback(context, context_state_callback, NULL);
928 pa_context_connect(context, server, 0, NULL);
929
930 if (pa_mainloop_run(m, &ret) < 0) {
931 fprintf(stderr, _("pa_mainloop_run() failed.\n"));
932 goto quit;
933 }
934
935 quit:
936 if (sample_stream)
937 pa_stream_unref(sample_stream);
938
939 if (context)
940 pa_context_unref(context);
941
942 if (m) {
943 pa_signal_done();
944 pa_mainloop_free(m);
945 }
946
947 if (sndfile)
948 sf_close(sndfile);
949
950 pa_xfree(server);
951 pa_xfree(device);
952 pa_xfree(sample_name);
953 pa_xfree(sink_name);
954 pa_xfree(source_name);
955 pa_xfree(module_args);
956 pa_xfree(client_name);
957
958 return ret;
959 }