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