]> code.delx.au - pulseaudio/blob - polyp/polyplib-context.c
* some commenting work
[pulseaudio] / polyp / polyplib-context.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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 <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netdb.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <sys/wait.h>
37 #include <signal.h>
38
39 #include "polyplib-internal.h"
40 #include "polyplib-context.h"
41 #include "native-common.h"
42 #include "pdispatch.h"
43 #include "pstream.h"
44 #include "dynarray.h"
45 #include "socket-client.h"
46 #include "pstream-util.h"
47 #include "util.h"
48 #include "xmalloc.h"
49 #include "log.h"
50 #include "client-conf.h"
51 #include "socket-util.h"
52
53 #ifdef HAVE_X11
54 #include "client-conf-x11.h"
55 #endif
56
57 #define AUTOSPAWN_LOCK "autospawn.lock"
58
59 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
60 [PA_COMMAND_REQUEST] = { pa_command_request },
61 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = { pa_command_stream_killed },
62 [PA_COMMAND_RECORD_STREAM_KILLED] = { pa_command_stream_killed },
63 [PA_COMMAND_SUBSCRIBE_EVENT] = { pa_command_subscribe_event },
64 };
65
66 static void unlock_autospawn_lock_file(struct pa_context *c) {
67 assert(c);
68
69 if (c->autospawn_lock_fd >= 0) {
70 pa_unlock_lockfile(c->autospawn_lock_fd);
71 c->autospawn_lock_fd = -1;
72 }
73 }
74
75 struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name) {
76 struct pa_context *c;
77 assert(mainloop && name);
78
79 c = pa_xmalloc(sizeof(struct pa_context));
80 c->ref = 1;
81 c->name = pa_xstrdup(name);
82 c->mainloop = mainloop;
83 c->client = NULL;
84 c->pstream = NULL;
85 c->pdispatch = NULL;
86 c->playback_streams = pa_dynarray_new();
87 c->record_streams = pa_dynarray_new();
88 assert(c->playback_streams && c->record_streams);
89
90 PA_LLIST_HEAD_INIT(struct pa_stream, c->streams);
91 PA_LLIST_HEAD_INIT(struct pa_operation, c->operations);
92
93 c->error = PA_ERROR_OK;
94 c->state = PA_CONTEXT_UNCONNECTED;
95 c->ctag = 0;
96
97 c->state_callback = NULL;
98 c->state_userdata = NULL;
99
100 c->subscribe_callback = NULL;
101 c->subscribe_userdata = NULL;
102
103 c->memblock_stat = pa_memblock_stat_new();
104 c->local = -1;
105 c->server_list = NULL;
106 c->server = NULL;
107 c->autospawn_lock_fd = -1;
108 memset(&c->spawn_api, 0, sizeof(c->spawn_api));
109 c->do_autospawn = 0;
110
111 pa_check_signal_is_blocked(SIGPIPE);
112
113 c->conf = pa_client_conf_new();
114 pa_client_conf_load(c->conf, NULL);
115 #ifdef HAVE_X11
116 pa_client_conf_from_x11(c->conf, NULL);
117 #endif
118 pa_client_conf_env(c->conf);
119
120 return c;
121 }
122
123 static void context_free(struct pa_context *c) {
124 assert(c);
125
126 unlock_autospawn_lock_file(c);
127
128 while (c->operations)
129 pa_operation_cancel(c->operations);
130
131 while (c->streams)
132 pa_stream_set_state(c->streams, PA_STREAM_TERMINATED);
133
134 if (c->client)
135 pa_socket_client_unref(c->client);
136 if (c->pdispatch)
137 pa_pdispatch_unref(c->pdispatch);
138 if (c->pstream) {
139 pa_pstream_close(c->pstream);
140 pa_pstream_unref(c->pstream);
141 }
142
143 if (c->record_streams)
144 pa_dynarray_free(c->record_streams, NULL, NULL);
145 if (c->playback_streams)
146 pa_dynarray_free(c->playback_streams, NULL, NULL);
147
148 pa_memblock_stat_unref(c->memblock_stat);
149
150 if (c->conf)
151 pa_client_conf_free(c->conf);
152
153 pa_strlist_free(c->server_list);
154
155 pa_xfree(c->name);
156 pa_xfree(c->server);
157 pa_xfree(c);
158 }
159
160 struct pa_context* pa_context_ref(struct pa_context *c) {
161 assert(c && c->ref >= 1);
162 c->ref++;
163 return c;
164 }
165
166 void pa_context_unref(struct pa_context *c) {
167 assert(c && c->ref >= 1);
168
169 if ((--(c->ref)) == 0)
170 context_free(c);
171 }
172
173 void pa_context_set_state(struct pa_context *c, enum pa_context_state st) {
174 assert(c);
175
176 if (c->state == st)
177 return;
178
179 pa_context_ref(c);
180
181 if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED) {
182 struct pa_stream *s;
183
184 s = c->streams ? pa_stream_ref(c->streams) : NULL;
185 while (s) {
186 struct pa_stream *n = s->next ? pa_stream_ref(s->next) : NULL;
187 pa_stream_set_state(s, st == PA_CONTEXT_FAILED ? PA_STREAM_FAILED : PA_STREAM_TERMINATED);
188 pa_stream_unref(s);
189 s = n;
190 }
191
192 if (c->pdispatch)
193 pa_pdispatch_unref(c->pdispatch);
194 c->pdispatch = NULL;
195
196 if (c->pstream) {
197 pa_pstream_close(c->pstream);
198 pa_pstream_unref(c->pstream);
199 }
200 c->pstream = NULL;
201
202 if (c->client)
203 pa_socket_client_unref(c->client);
204 c->client = NULL;
205 }
206
207 c->state = st;
208 if (c->state_callback)
209 c->state_callback(c, c->state_userdata);
210
211 pa_context_unref(c);
212 }
213
214 void pa_context_fail(struct pa_context *c, int error) {
215 assert(c);
216 c->error = error;
217 pa_context_set_state(c, PA_CONTEXT_FAILED);
218 }
219
220 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
221 struct pa_context *c = userdata;
222 assert(p && c);
223 pa_context_fail(c, PA_ERROR_CONNECTIONTERMINATED);
224 }
225
226 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
227 struct pa_context *c = userdata;
228 assert(p && packet && c);
229
230 pa_context_ref(c);
231
232 if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
233 pa_log(__FILE__": invalid packet.\n");
234 pa_context_fail(c, PA_ERROR_PROTOCOL);
235 }
236
237 pa_context_unref(c);
238 }
239
240 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata) {
241 struct pa_context *c = userdata;
242 struct pa_stream *s;
243 assert(p && chunk && c && chunk->memblock && chunk->memblock->data);
244
245 pa_context_ref(c);
246
247 if ((s = pa_dynarray_get(c->record_streams, channel))) {
248 pa_mcalign_push(s->mcalign, chunk);
249
250 for (;;) {
251 struct pa_memchunk t;
252
253 if (pa_mcalign_pop(s->mcalign, &t) < 0)
254 break;
255
256 if (s->read_callback) {
257 s->read_callback(s, (uint8_t*) t.memblock->data + t.index, t.length, s->read_userdata);
258 s->counter += chunk->length;
259 }
260
261 pa_memblock_unref(t.memblock);
262 }
263 }
264
265 pa_context_unref(c);
266 }
267
268 int pa_context_handle_error(struct pa_context *c, uint32_t command, struct pa_tagstruct *t) {
269 assert(c);
270
271 if (command == PA_COMMAND_ERROR) {
272 assert(t);
273
274 if (pa_tagstruct_getu32(t, &c->error) < 0) {
275 pa_context_fail(c, PA_ERROR_PROTOCOL);
276 return -1;
277
278 }
279 } else if (command == PA_COMMAND_TIMEOUT)
280 c->error = PA_ERROR_TIMEOUT;
281 else {
282 pa_context_fail(c, PA_ERROR_PROTOCOL);
283 return -1;
284 }
285
286 return 0;
287 }
288
289 static void setup_complete_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
290 struct pa_context *c = userdata;
291 assert(pd && c && (c->state == PA_CONTEXT_AUTHORIZING || c->state == PA_CONTEXT_SETTING_NAME));
292
293 pa_context_ref(c);
294
295 if (command != PA_COMMAND_REPLY) {
296
297 if (pa_context_handle_error(c, command, t) < 0)
298 pa_context_fail(c, PA_ERROR_PROTOCOL);
299
300 pa_context_fail(c, c->error);
301 goto finish;
302 }
303
304 switch(c->state) {
305 case PA_CONTEXT_AUTHORIZING: {
306 struct pa_tagstruct *t;
307 t = pa_tagstruct_new(NULL, 0);
308 assert(t);
309 pa_tagstruct_putu32(t, PA_COMMAND_SET_CLIENT_NAME);
310 pa_tagstruct_putu32(t, tag = c->ctag++);
311 pa_tagstruct_puts(t, c->name);
312 pa_pstream_send_tagstruct(c->pstream, t);
313 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c);
314
315 pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);
316 break;
317 }
318
319 case PA_CONTEXT_SETTING_NAME :
320 pa_context_set_state(c, PA_CONTEXT_READY);
321 break;
322
323 default:
324 assert(0);
325 }
326
327 finish:
328 pa_context_unref(c);
329 }
330
331 static void setup_context(struct pa_context *c, struct pa_iochannel *io) {
332 struct pa_tagstruct *t;
333 uint32_t tag;
334 assert(c && io);
335
336 pa_context_ref(c);
337
338 assert(!c->pstream);
339 c->pstream = pa_pstream_new(c->mainloop, io, c->memblock_stat);
340 assert(c->pstream);
341
342 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
343 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
344 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
345
346 assert(!c->pdispatch);
347 c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
348 assert(c->pdispatch);
349
350 if (!c->conf->cookie_valid) {
351 pa_context_fail(c, PA_ERROR_AUTHKEY);
352 goto finish;
353 }
354
355 t = pa_tagstruct_new(NULL, 0);
356 assert(t);
357 pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
358 pa_tagstruct_putu32(t, tag = c->ctag++);
359 pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
360 pa_pstream_send_tagstruct(c->pstream, t);
361 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c);
362
363 pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);
364
365 finish:
366
367 pa_context_unref(c);
368 }
369
370 static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata);
371
372 static int context_connect_spawn(struct pa_context *c) {
373 pid_t pid;
374 int status, r;
375 int fds[2] = { -1, -1} ;
376 struct pa_iochannel *io;
377
378 pa_context_ref(c);
379
380 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
381 pa_log(__FILE__": socketpair() failed: %s\n", strerror(errno));
382 pa_context_fail(c, PA_ERROR_INTERNAL);
383 goto fail;
384 }
385
386 pa_fd_set_cloexec(fds[0], 1);
387
388 pa_socket_low_delay(fds[0]);
389 pa_socket_low_delay(fds[1]);
390
391 if (c->spawn_api.prefork)
392 c->spawn_api.prefork();
393
394 if ((pid = fork()) < 0) {
395 pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
396 pa_context_fail(c, PA_ERROR_INTERNAL);
397
398 if (c->spawn_api.postfork)
399 c->spawn_api.postfork();
400
401 goto fail;
402 } else if (!pid) {
403 /* Child */
404
405 char t[128];
406 const char *state = NULL;
407 #define MAX_ARGS 64
408 char *argv[MAX_ARGS+1];
409 int n;
410
411 /* Not required, since fds[0] has CLOEXEC enabled anyway */
412 close(fds[0]);
413
414 if (c->spawn_api.atfork)
415 c->spawn_api.atfork();
416
417 /* Setup argv */
418
419 n = 0;
420
421 argv[n++] = c->conf->daemon_binary;
422 argv[n++] = "--daemonize=yes";
423
424 snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
425 argv[n++] = strdup(t);
426
427 while (n < MAX_ARGS) {
428 char *a;
429
430 if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
431 break;
432
433 argv[n++] = a;
434 }
435
436 argv[n++] = NULL;
437
438 execv(argv[0], argv);
439 _exit(1);
440 #undef MAX_ARGS
441 }
442
443 /* Parent */
444
445 r = waitpid(pid, &status, 0);
446
447 if (c->spawn_api.postfork)
448 c->spawn_api.postfork();
449
450 if (r < 0) {
451 pa_log(__FILE__": waitpid() failed: %s\n", strerror(errno));
452 pa_context_fail(c, PA_ERROR_INTERNAL);
453 goto fail;
454 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
455 pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
456 goto fail;
457 }
458
459 close(fds[1]);
460
461 c->local = 1;
462
463 io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
464
465 setup_context(c, io);
466 unlock_autospawn_lock_file(c);
467
468 pa_context_unref(c);
469
470 return 0;
471
472 fail:
473 if (fds[0] != -1)
474 close(fds[0]);
475 if (fds[1] != -1)
476 close(fds[1]);
477
478 unlock_autospawn_lock_file(c);
479
480 pa_context_unref(c);
481
482 return -1;
483 }
484
485 static int try_next_connection(struct pa_context *c) {
486 char *u = NULL;
487 int r = -1;
488 assert(c && !c->client);
489
490 for (;;) {
491 if (u)
492 pa_xfree(u);
493 u = NULL;
494
495 c->server_list = pa_strlist_pop(c->server_list, &u);
496
497 if (!u) {
498
499 if (c->do_autospawn) {
500 r = context_connect_spawn(c);
501 goto finish;
502 }
503
504 pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
505 goto finish;
506 }
507
508 /* pa_log(__FILE__": Trying to connect to %s...\n", u); */
509
510 pa_xfree(c->server);
511 c->server = pa_xstrdup(u);
512
513 if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
514 continue;
515
516 c->local = pa_socket_client_is_local(c->client);
517 pa_socket_client_set_callback(c->client, on_connection, c);
518 break;
519 }
520
521 r = 0;
522
523 finish:
524 if (u)
525 pa_xfree(u);
526
527 return r;
528 }
529
530 static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata) {
531 struct pa_context *c = userdata;
532 assert(client && c && c->state == PA_CONTEXT_CONNECTING);
533
534 pa_context_ref(c);
535
536 pa_socket_client_unref(client);
537 c->client = NULL;
538
539 if (!io) {
540 /* Try the item in the list */
541 if (errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH) {
542 try_next_connection(c);
543 goto finish;
544 }
545
546 pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
547 goto finish;
548 }
549
550 unlock_autospawn_lock_file(c);
551 setup_context(c, io);
552
553 finish:
554 pa_context_unref(c);
555 }
556
557 int pa_context_connect(struct pa_context *c, const char *server, int spawn, const struct pa_spawn_api *api) {
558 int r = -1;
559 assert(c && c->ref >= 1 && c->state == PA_CONTEXT_UNCONNECTED);
560
561 if (!server)
562 server = c->conf->default_server;
563
564
565 pa_context_ref(c);
566
567 assert(!c->server_list);
568
569 if (server) {
570 if (!(c->server_list = pa_strlist_parse(server))) {
571 pa_context_fail(c, PA_ERROR_INVALIDSERVER);
572 goto finish;
573 }
574 } else {
575 char *d;
576 char ufn[PATH_MAX];
577
578 /* Prepend in reverse order */
579
580 if ((d = getenv("DISPLAY")))
581 c->server_list = pa_strlist_prepend(c->server_list, d);
582
583 c->server_list = pa_strlist_prepend(c->server_list, "tcp6:localhost");
584 c->server_list = pa_strlist_prepend(c->server_list, "localhost");
585 c->server_list = pa_strlist_prepend(c->server_list, pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET, ufn, sizeof(ufn)));
586
587 /* Wrap the connection attempts in a single transaction for sane autospwan locking */
588 if (spawn && c->conf->autospawn) {
589 char lf[PATH_MAX];
590
591 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
592 assert(c->autospawn_lock_fd <= 0);
593 c->autospawn_lock_fd = pa_lock_lockfile(lf);
594
595 if (api)
596 c->spawn_api = *api;
597 c->do_autospawn = 1;
598 }
599
600 }
601
602 pa_context_set_state(c, PA_CONTEXT_CONNECTING);
603 r = try_next_connection(c);
604
605 finish:
606 pa_context_unref(c);
607
608 return r;
609 }
610
611 void pa_context_disconnect(struct pa_context *c) {
612 assert(c);
613 pa_context_set_state(c, PA_CONTEXT_TERMINATED);
614 }
615
616 enum pa_context_state pa_context_get_state(struct pa_context *c) {
617 assert(c && c->ref >= 1);
618 return c->state;
619 }
620
621 int pa_context_errno(struct pa_context *c) {
622 assert(c && c->ref >= 1);
623 return c->error;
624 }
625
626 void pa_context_set_state_callback(struct pa_context *c, void (*cb)(struct pa_context *c, void *userdata), void *userdata) {
627 assert(c && c->ref >= 1);
628 c->state_callback = cb;
629 c->state_userdata = userdata;
630 }
631
632 int pa_context_is_pending(struct pa_context *c) {
633 assert(c && c->ref >= 1);
634
635 /* pa_log("pstream: %i\n", pa_pstream_is_pending(c->pstream)); */
636 /* pa_log("pdispatch: %i\n", pa_pdispatch_is_pending(c->pdispatch)); */
637
638 return (c->pstream && pa_pstream_is_pending(c->pstream)) || (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) || c->client;
639 }
640
641 static void set_dispatch_callbacks(struct pa_operation *o);
642
643 static void pdispatch_drain_callback(struct pa_pdispatch*pd, void *userdata) {
644 set_dispatch_callbacks(userdata);
645 }
646
647 static void pstream_drain_callback(struct pa_pstream *s, void *userdata) {
648 set_dispatch_callbacks(userdata);
649 }
650
651 static void set_dispatch_callbacks(struct pa_operation *o) {
652 int done = 1;
653 assert(o && o->context && o->context->ref >= 1 && o->ref >= 1 && o->context->state == PA_CONTEXT_READY);
654
655 pa_pstream_set_drain_callback(o->context->pstream, NULL, NULL);
656 pa_pdispatch_set_drain_callback(o->context->pdispatch, NULL, NULL);
657
658 if (pa_pdispatch_is_pending(o->context->pdispatch)) {
659 pa_pdispatch_set_drain_callback(o->context->pdispatch, pdispatch_drain_callback, o);
660 done = 0;
661 }
662
663 if (pa_pstream_is_pending(o->context->pstream)) {
664 pa_pstream_set_drain_callback(o->context->pstream, pstream_drain_callback, o);
665 done = 0;
666 }
667
668 if (!done)
669 pa_operation_ref(o);
670 else {
671 if (o->callback) {
672 void (*cb)(struct pa_context *c, void *userdata);
673 cb = (void (*)(struct pa_context*, void*)) o->callback;
674 cb(o->context, o->userdata);
675 }
676
677 pa_operation_done(o);
678 }
679
680 pa_operation_unref(o);
681 }
682
683 struct pa_operation* pa_context_drain(struct pa_context *c, void (*cb) (struct pa_context*c, void *userdata), void *userdata) {
684 struct pa_operation *o;
685 assert(c && c->ref >= 1);
686
687 if (c->state != PA_CONTEXT_READY)
688 return NULL;
689
690 if (!pa_context_is_pending(c))
691 return NULL;
692
693 o = pa_operation_new(c, NULL);
694 assert(o);
695 o->callback = cb;
696 o->userdata = userdata;
697
698 set_dispatch_callbacks(pa_operation_ref(o));
699
700 return o;
701 }
702
703 void pa_context_exit_daemon(struct pa_context *c) {
704 struct pa_tagstruct *t;
705 assert(c && c->ref >= 1);
706
707 t = pa_tagstruct_new(NULL, 0);
708 assert(t);
709 pa_tagstruct_putu32(t, PA_COMMAND_EXIT);
710 pa_tagstruct_putu32(t, c->ctag++);
711 pa_pstream_send_tagstruct(c->pstream, t);
712 }
713
714 void pa_context_simple_ack_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
715 struct pa_operation *o = userdata;
716 int success = 1;
717 assert(pd && o && o->context && o->ref >= 1);
718
719 if (command != PA_COMMAND_REPLY) {
720 if (pa_context_handle_error(o->context, command, t) < 0)
721 goto finish;
722
723 success = 0;
724 } else if (!pa_tagstruct_eof(t)) {
725 pa_context_fail(o->context, PA_ERROR_PROTOCOL);
726 goto finish;
727 }
728
729 if (o->callback) {
730 void (*cb)(struct pa_context *c, int success, void *userdata) = o->callback;
731 cb(o->context, success, o->userdata);
732 }
733
734 finish:
735 pa_operation_done(o);
736 pa_operation_unref(o);
737 }
738
739 struct pa_operation* pa_context_send_simple_command(struct pa_context *c, uint32_t command, void (*internal_callback)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata), void (*cb)(), void *userdata) {
740 struct pa_tagstruct *t;
741 struct pa_operation *o;
742 uint32_t tag;
743 assert(c && cb);
744
745 o = pa_operation_new(c, NULL);
746 o->callback = cb;
747 o->userdata = userdata;
748
749 t = pa_tagstruct_new(NULL, 0);
750 pa_tagstruct_putu32(t, command);
751 pa_tagstruct_putu32(t, tag = c->ctag++);
752 pa_pstream_send_tagstruct(c->pstream, t);
753 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, internal_callback, o);
754
755 return pa_operation_ref(o);
756 }
757
758 struct pa_operation* pa_context_set_default_sink(struct pa_context *c, const char *name, void(*cb)(struct pa_context*c, int success, void *userdata), void *userdata) {
759 struct pa_tagstruct *t;
760 struct pa_operation *o;
761 uint32_t tag;
762 assert(c && cb);
763
764 o = pa_operation_new(c, NULL);
765 o->callback = cb;
766 o->userdata = userdata;
767
768 t = pa_tagstruct_new(NULL, 0);
769 pa_tagstruct_putu32(t, PA_COMMAND_SET_DEFAULT_SINK);
770 pa_tagstruct_putu32(t, tag = c->ctag++);
771 pa_tagstruct_puts(t, name);
772 pa_pstream_send_tagstruct(c->pstream, t);
773 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, o);
774
775 return pa_operation_ref(o);
776 }
777
778 struct pa_operation* pa_context_set_default_source(struct pa_context *c, const char *name, void(*cb)(struct pa_context*c, int success, void *userdata), void *userdata) {
779 struct pa_tagstruct *t;
780 struct pa_operation *o;
781 uint32_t tag;
782 assert(c && cb);
783
784 o = pa_operation_new(c, NULL);
785 o->callback = cb;
786 o->userdata = userdata;
787
788 t = pa_tagstruct_new(NULL, 0);
789 pa_tagstruct_putu32(t, PA_COMMAND_SET_DEFAULT_SOURCE);
790 pa_tagstruct_putu32(t, tag = c->ctag++);
791 pa_tagstruct_puts(t, name);
792 pa_pstream_send_tagstruct(c->pstream, t);
793 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, o);
794
795 return pa_operation_ref(o);
796 }
797
798 int pa_context_is_local(struct pa_context *c) {
799 assert(c);
800 return c->local;
801 }
802
803 struct pa_operation* pa_context_set_name(struct pa_context *c, const char *name, void(*cb)(struct pa_context*c, int success, void *userdata), void *userdata) {
804 struct pa_tagstruct *t;
805 struct pa_operation *o;
806 uint32_t tag;
807 assert(c && name && cb);
808
809 o = pa_operation_new(c, NULL);
810 o->callback = cb;
811 o->userdata = userdata;
812
813 t = pa_tagstruct_new(NULL, 0);
814 pa_tagstruct_putu32(t, PA_COMMAND_SET_CLIENT_NAME);
815 pa_tagstruct_putu32(t, tag = c->ctag++);
816 pa_tagstruct_puts(t, name);
817 pa_pstream_send_tagstruct(c->pstream, t);
818 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, o);
819
820 return pa_operation_ref(o);
821 }
822
823 const char* pa_get_library_version(void) {
824 return PACKAGE_VERSION;
825 }
826
827 const char* pa_context_get_server(struct pa_context *c) {
828
829 if (!c->server)
830 return NULL;
831
832 if (*c->server == '{') {
833 char *e = strchr(c->server+1, '}');
834 return e ? e+1 : c->server;
835 }
836
837 return c->server;
838 }