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