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