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