]> code.delx.au - pulseaudio/blob - src/utils/pactl.c
don't show autoload flag anymore since it is obsolete
[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 i->index,
286 i->name,
287 i->argument ? i->argument : "",
288 i->n_used != PA_INVALID_INDEX ? t : _("n/a"));
289 }
290
291 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
292 char t[32];
293 char *pl;
294
295 if (is_last < 0) {
296 fprintf(stderr, _("Failed to get client information: %s\n"), pa_strerror(pa_context_errno(c)));
297 quit(1);
298 return;
299 }
300
301 if (is_last) {
302 complete_action();
303 return;
304 }
305
306 assert(i);
307
308 if (nl)
309 printf("\n");
310 nl = 1;
311
312 snprintf(t, sizeof(t), "%u", i->owner_module);
313
314 printf(_("*** Client #%u ***\n"
315 "Driver: %s\n"
316 "Owner Module: %s\n"
317 "Properties:\n%s"),
318 i->index,
319 pa_strnull(i->driver),
320 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
321 pl = pa_proplist_to_string(i->proplist));
322
323 pa_xfree(pl);
324 }
325
326 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
327 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
328 char *pl;
329
330 if (is_last < 0) {
331 fprintf(stderr, _("Failed to get sink input information: %s\n"), pa_strerror(pa_context_errno(c)));
332 quit(1);
333 return;
334 }
335
336 if (is_last) {
337 complete_action();
338 return;
339 }
340
341 assert(i);
342
343 if (nl)
344 printf("\n");
345 nl = 1;
346
347 snprintf(t, sizeof(t), "%u", i->owner_module);
348 snprintf(k, sizeof(k), "%u", i->client);
349
350 printf(_("*** Sink Input #%u ***\n"
351 "Driver: %s\n"
352 "Owner Module: %s\n"
353 "Client: %s\n"
354 "Sink: %u\n"
355 "Sample Specification: %s\n"
356 "Channel Map: %s\n"
357 "Volume: %s\n"
358 "Buffer Latency: %0.0f usec\n"
359 "Sink Latency: %0.0f usec\n"
360 "Resample method: %s\n"
361 "Properties:\n%s"),
362 i->index,
363 pa_strnull(i->driver),
364 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
365 i->client != PA_INVALID_INDEX ? k : _("n/a"),
366 i->sink,
367 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
368 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
369 i->mute ? _("muted") : pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
370 (double) i->buffer_usec,
371 (double) i->sink_usec,
372 i->resample_method ? i->resample_method : _("n/a"),
373 pl = pa_proplist_to_string(i->proplist));
374
375 pa_xfree(pl);
376 }
377
378 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
379 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
380 char *pl;
381
382 if (is_last < 0) {
383 fprintf(stderr, _("Failed to get source output information: %s\n"), pa_strerror(pa_context_errno(c)));
384 quit(1);
385 return;
386 }
387
388 if (is_last) {
389 complete_action();
390 return;
391 }
392
393 assert(i);
394
395 if (nl)
396 printf("\n");
397 nl = 1;
398
399
400 snprintf(t, sizeof(t), "%u", i->owner_module);
401 snprintf(k, sizeof(k), "%u", i->client);
402
403 printf(_("*** Source Output #%u ***\n"
404 "Driver: %s\n"
405 "Owner Module: %s\n"
406 "Client: %s\n"
407 "Source: %u\n"
408 "Sample Specification: %s\n"
409 "Channel Map: %s\n"
410 "Buffer Latency: %0.0f usec\n"
411 "Source Latency: %0.0f usec\n"
412 "Resample method: %s\n"
413 "Properties:\n%s"),
414 i->index,
415 pa_strnull(i->driver),
416 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
417 i->client != PA_INVALID_INDEX ? k : _("n/a"),
418 i->source,
419 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
420 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
421 (double) i->buffer_usec,
422 (double) i->source_usec,
423 i->resample_method ? i->resample_method : _("n/a"),
424 pl = pa_proplist_to_string(i->proplist));
425
426 pa_xfree(pl);
427 }
428
429 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
430 char t[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
431 char *pl;
432
433 if (is_last < 0) {
434 fprintf(stderr, _("Failed to get sample information: %s\n"), pa_strerror(pa_context_errno(c)));
435 quit(1);
436 return;
437 }
438
439 if (is_last) {
440 complete_action();
441 return;
442 }
443
444 assert(i);
445
446 if (nl)
447 printf("\n");
448 nl = 1;
449
450
451 pa_bytes_snprint(t, sizeof(t), i->bytes);
452
453 printf(_("*** Sample #%u ***\n"
454 "Name: %s\n"
455 "Volume: %s\n"
456 "Sample Specification: %s\n"
457 "Channel Map: %s\n"
458 "Duration: %0.1fs\n"
459 "Size: %s\n"
460 "Lazy: %s\n"
461 "Filename: %s\n"
462 "Properties:\n%s"),
463 i->index,
464 i->name,
465 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
466 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : _("n/a"),
467 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : _("n/a"),
468 (double) i->duration/1000000,
469 t,
470 pa_yes_no(i->lazy),
471 i->filename ? i->filename : _("n/a"),
472 pl = pa_proplist_to_string(i->proplist));
473
474 pa_xfree(pl);
475 }
476
477 static void simple_callback(pa_context *c, int success, void *userdata) {
478 if (!success) {
479 fprintf(stderr, _("Failure: %s\n"), pa_strerror(pa_context_errno(c)));
480 quit(1);
481 return;
482 }
483
484 complete_action();
485 }
486
487 static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
488 if (idx == PA_INVALID_INDEX) {
489 fprintf(stderr, _("Failure: %s\n"), pa_strerror(pa_context_errno(c)));
490 quit(1);
491 return;
492 }
493
494 printf("%u\n", idx);
495
496 complete_action();
497 }
498
499 static void stream_state_callback(pa_stream *s, void *userdata) {
500 assert(s);
501
502 switch (pa_stream_get_state(s)) {
503 case PA_STREAM_CREATING:
504 case PA_STREAM_READY:
505 break;
506
507 case PA_STREAM_TERMINATED:
508 drain();
509 break;
510
511 case PA_STREAM_FAILED:
512 default:
513 fprintf(stderr, _("Failed to upload sample: %s\n"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
514 quit(1);
515 }
516 }
517
518 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
519 sf_count_t l;
520 float *d;
521 assert(s && length && sndfile);
522
523 d = pa_xmalloc(length);
524
525 assert(sample_length >= length);
526 l = (sf_count_t) (length/pa_frame_size(&sample_spec));
527
528 if ((sf_readf_float(sndfile, d, l)) != l) {
529 pa_xfree(d);
530 fprintf(stderr, _("Premature end of file\n"));
531 quit(1);
532 }
533
534 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
535
536 sample_length -= length;
537
538 if (sample_length <= 0) {
539 pa_stream_set_write_callback(sample_stream, NULL, NULL);
540 pa_stream_finish_upload(sample_stream);
541 }
542 }
543
544 static void context_state_callback(pa_context *c, void *userdata) {
545 assert(c);
546 switch (pa_context_get_state(c)) {
547 case PA_CONTEXT_CONNECTING:
548 case PA_CONTEXT_AUTHORIZING:
549 case PA_CONTEXT_SETTING_NAME:
550 break;
551
552 case PA_CONTEXT_READY:
553 switch (action) {
554 case STAT:
555 actions = 2;
556 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
557 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
558 break;
559
560 case PLAY_SAMPLE:
561 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
562 break;
563
564 case REMOVE_SAMPLE:
565 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
566 break;
567
568 case UPLOAD_SAMPLE:
569 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
570 assert(sample_stream);
571
572 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
573 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
574 pa_stream_connect_upload(sample_stream, sample_length);
575 break;
576
577 case EXIT:
578 pa_operation_unref(pa_context_exit_daemon(c, simple_callback, NULL));
579 break;
580
581 case LIST:
582 actions = 8;
583 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
584 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
585 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
586 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
587 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
588 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
589 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
590 break;
591
592 case MOVE_SINK_INPUT:
593 pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL));
594 break;
595
596 case MOVE_SOURCE_OUTPUT:
597 pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL));
598 break;
599
600 case LOAD_MODULE:
601 pa_operation_unref(pa_context_load_module(c, module_name, module_args, index_callback, NULL));
602 break;
603
604 case UNLOAD_MODULE:
605 pa_operation_unref(pa_context_unload_module(c, module_index, simple_callback, NULL));
606 break;
607
608 case SUSPEND_SINK:
609 if (sink_name)
610 pa_operation_unref(pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL));
611 else
612 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
613 break;
614
615 case SUSPEND_SOURCE:
616 if (source_name)
617 pa_operation_unref(pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL));
618 else
619 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
620 break;
621
622 default:
623 assert(0);
624 }
625 break;
626
627 case PA_CONTEXT_TERMINATED:
628 quit(0);
629 break;
630
631 case PA_CONTEXT_FAILED:
632 default:
633 fprintf(stderr, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
634 quit(1);
635 }
636 }
637
638 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
639 fprintf(stderr, _("Got SIGINT, exiting.\n"));
640 quit(0);
641 }
642
643 static void help(const char *argv0) {
644
645 printf(_("%s [options] stat\n"
646 "%s [options] list\n"
647 "%s [options] exit\n"
648 "%s [options] upload-sample FILENAME [NAME]\n"
649 "%s [options] play-sample NAME [SINK]\n"
650 "%s [options] remove-sample NAME\n"
651 "%s [options] move-sink-input ID SINK\n"
652 "%s [options] move-source-output ID SOURCE\n"
653 "%s [options] load-module NAME [ARGS ...]\n"
654 "%s [options] unload-module ID\n"
655 "%s [options] suspend-sink [SINK] 1|0\n"
656 "%s [options] suspend-source [SOURCE] 1|0\n\n"
657 " -h, --help Show this help\n"
658 " --version Show version\n\n"
659 " -s, --server=SERVER The name of the server to connect to\n"
660 " -n, --client-name=NAME How to call this client on the server\n"),
661 argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0);
662 }
663
664 enum { ARG_VERSION = 256 };
665
666 int main(int argc, char *argv[]) {
667 pa_mainloop* m = NULL;
668 char tmp[PATH_MAX];
669 int ret = 1, r, c;
670 char *server = NULL, *client_name = NULL, *bn;
671
672 static const struct option long_options[] = {
673 {"server", 1, NULL, 's'},
674 {"client-name", 1, NULL, 'n'},
675 {"version", 0, NULL, ARG_VERSION},
676 {"help", 0, NULL, 'h'},
677 {NULL, 0, NULL, 0}
678 };
679
680 setlocale(LC_ALL, "");
681 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
682
683 if (!(bn = strrchr(argv[0], '/')))
684 bn = argv[0];
685 else
686 bn++;
687
688 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
689 switch (c) {
690 case 'h' :
691 help(bn);
692 ret = 0;
693 goto quit;
694
695 case ARG_VERSION:
696 printf(_("pactl %s\n"
697 "Compiled with libpulse %s\n"
698 "Linked with libpulse %s\n"),
699 PACKAGE_VERSION,
700 pa_get_headers_version(),
701 pa_get_library_version());
702 ret = 0;
703 goto quit;
704
705 case 's':
706 pa_xfree(server);
707 server = pa_xstrdup(optarg);
708 break;
709
710 case 'n':
711 pa_xfree(client_name);
712 client_name = pa_xstrdup(optarg);
713 break;
714
715 default:
716 goto quit;
717 }
718 }
719
720 if (!client_name)
721 client_name = pa_xstrdup(bn);
722
723 if (optind < argc) {
724 if (!strcmp(argv[optind], "stat"))
725 action = STAT;
726 else if (!strcmp(argv[optind], "exit"))
727 action = EXIT;
728 else if (!strcmp(argv[optind], "list"))
729 action = LIST;
730 else if (!strcmp(argv[optind], "upload-sample")) {
731 struct SF_INFO sfinfo;
732 action = UPLOAD_SAMPLE;
733
734 if (optind+1 >= argc) {
735 fprintf(stderr, _("Please specify a sample file to load\n"));
736 goto quit;
737 }
738
739 if (optind+2 < argc)
740 sample_name = pa_xstrdup(argv[optind+2]);
741 else {
742 char *f = strrchr(argv[optind+1], '/');
743 size_t n;
744 if (f)
745 f++;
746 else
747 f = argv[optind];
748
749 n = strcspn(f, ".");
750 strncpy(tmp, f, n);
751 tmp[n] = 0;
752 sample_name = pa_xstrdup(tmp);
753 }
754
755 memset(&sfinfo, 0, sizeof(sfinfo));
756 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfinfo))) {
757 fprintf(stderr, _("Failed to open sound file.\n"));
758 goto quit;
759 }
760
761 sample_spec.format = PA_SAMPLE_FLOAT32;
762 sample_spec.rate = (uint32_t) sfinfo.samplerate;
763 sample_spec.channels = (uint8_t) sfinfo.channels;
764
765 sample_length = (size_t)sfinfo.frames*pa_frame_size(&sample_spec);
766 } else if (!strcmp(argv[optind], "play-sample")) {
767 action = PLAY_SAMPLE;
768 if (argc != optind+2 && argc != optind+3) {
769 fprintf(stderr, _("You have to specify a sample name to play\n"));
770 goto quit;
771 }
772
773 sample_name = pa_xstrdup(argv[optind+1]);
774
775 if (optind+2 < argc)
776 device = pa_xstrdup(argv[optind+2]);
777
778 } else if (!strcmp(argv[optind], "remove-sample")) {
779 action = REMOVE_SAMPLE;
780 if (argc != optind+2) {
781 fprintf(stderr, _("You have to specify a sample name to remove\n"));
782 goto quit;
783 }
784
785 sample_name = pa_xstrdup(argv[optind+1]);
786 } else if (!strcmp(argv[optind], "move-sink-input")) {
787 action = MOVE_SINK_INPUT;
788 if (argc != optind+3) {
789 fprintf(stderr, _("You have to specify a sink input index and a sink\n"));
790 goto quit;
791 }
792
793 sink_input_idx = (uint32_t) atoi(argv[optind+1]);
794 sink_name = pa_xstrdup(argv[optind+2]);
795 } else if (!strcmp(argv[optind], "move-source-output")) {
796 action = MOVE_SOURCE_OUTPUT;
797 if (argc != optind+3) {
798 fprintf(stderr, _("You have to specify a source output index and a source\n"));
799 goto quit;
800 }
801
802 source_output_idx = (uint32_t) atoi(argv[optind+1]);
803 source_name = pa_xstrdup(argv[optind+2]);
804 } else if (!strcmp(argv[optind], "load-module")) {
805 int i;
806 size_t n = 0;
807 char *p;
808
809 action = LOAD_MODULE;
810
811 if (argc <= optind+1) {
812 fprintf(stderr, _("You have to specify a module name and arguments.\n"));
813 goto quit;
814 }
815
816 module_name = argv[optind+1];
817
818 for (i = optind+2; i < argc; i++)
819 n += strlen(argv[i])+1;
820
821 if (n > 0) {
822 p = module_args = pa_xmalloc(n);
823
824 for (i = optind+2; i < argc; i++)
825 p += sprintf(p, "%s%s", p == module_args ? "" : " ", argv[i]);
826 }
827
828 } else if (!strcmp(argv[optind], "unload-module")) {
829 action = UNLOAD_MODULE;
830
831 if (argc != optind+2) {
832 fprintf(stderr, _("You have to specify a module index\n"));
833 goto quit;
834 }
835
836 module_index = (uint32_t) atoi(argv[optind+1]);
837
838 } else if (!strcmp(argv[optind], "suspend-sink")) {
839 action = SUSPEND_SINK;
840
841 if (argc > optind+3 || optind+1 >= argc) {
842 fprintf(stderr, _("You may not specify more than one sink. You have to specify at least one boolean value.\n"));
843 goto quit;
844 }
845
846 suspend = pa_parse_boolean(argv[argc-1]);
847
848 if (argc > optind+2)
849 sink_name = pa_xstrdup(argv[optind+1]);
850
851 } else if (!strcmp(argv[optind], "suspend-source")) {
852 action = SUSPEND_SOURCE;
853
854 if (argc > optind+3 || optind+1 >= argc) {
855 fprintf(stderr, _("You may not specify more than one source. You have to specify at least one boolean value.\n"));
856 goto quit;
857 }
858
859 suspend = pa_parse_boolean(argv[argc-1]);
860
861 if (argc > optind+2)
862 source_name = pa_xstrdup(argv[optind+1]);
863 } else if (!strcmp(argv[optind], "help")) {
864 help(bn);
865 ret = 0;
866 goto quit;
867 }
868 }
869
870 if (action == NONE) {
871 fprintf(stderr, _("No valid command specified.\n"));
872 goto quit;
873 }
874
875 if (!(m = pa_mainloop_new())) {
876 fprintf(stderr, _("pa_mainloop_new() failed.\n"));
877 goto quit;
878 }
879
880 mainloop_api = pa_mainloop_get_api(m);
881
882 r = pa_signal_init(mainloop_api);
883 assert(r == 0);
884 pa_signal_new(SIGINT, exit_signal_callback, NULL);
885 #ifdef SIGPIPE
886 signal(SIGPIPE, SIG_IGN);
887 #endif
888
889 if (!(context = pa_context_new(mainloop_api, client_name))) {
890 fprintf(stderr, _("pa_context_new() failed.\n"));
891 goto quit;
892 }
893
894 pa_context_set_state_callback(context, context_state_callback, NULL);
895 pa_context_connect(context, server, 0, NULL);
896
897 if (pa_mainloop_run(m, &ret) < 0) {
898 fprintf(stderr, _("pa_mainloop_run() failed.\n"));
899 goto quit;
900 }
901
902 quit:
903 if (sample_stream)
904 pa_stream_unref(sample_stream);
905
906 if (context)
907 pa_context_unref(context);
908
909 if (m) {
910 pa_signal_done();
911 pa_mainloop_free(m);
912 }
913
914 if (sndfile)
915 sf_close(sndfile);
916
917 pa_xfree(server);
918 pa_xfree(device);
919 pa_xfree(sample_name);
920 pa_xfree(sink_name);
921 pa_xfree(source_name);
922 pa_xfree(module_args);
923 pa_xfree(client_name);
924
925 return ret;
926 }