don't hit an assert in the client if posix shm is not available
[pulseaudio.git] / src / pulse / context.c
bloba458c6b1082c801985525bf2e8ecaa33f7e3f8ff
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <signal.h>
35 #include <limits.h>
37 #ifdef HAVE_SYS_WAIT_H
38 #include <sys/wait.h>
39 #endif
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #ifdef HAVE_SYS_UN_H
45 #include <sys/un.h>
46 #endif
47 #ifdef HAVE_NETDB_H
48 #include <netdb.h>
49 #endif
51 #include "../pulsecore/winsock.h"
53 #include <pulsecore/core-error.h>
54 #include <pulse/version.h>
55 #include <pulse/xmalloc.h>
57 #include <pulsecore/native-common.h>
58 #include <pulsecore/pdispatch.h>
59 #include <pulsecore/pstream.h>
60 #include <pulsecore/dynarray.h>
61 #include <pulsecore/socket-client.h>
62 #include <pulsecore/pstream-util.h>
63 #include <pulsecore/core-util.h>
64 #include <pulsecore/log.h>
65 #include <pulsecore/socket-util.h>
66 #include <pulsecore/creds.h>
68 #include "internal.h"
70 #include "client-conf.h"
72 #ifdef HAVE_X11
73 #include "client-conf-x11.h"
74 #endif
76 #include "context.h"
78 #define AUTOSPAWN_LOCK "autospawn.lock"
80 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
81 [PA_COMMAND_REQUEST] = pa_command_request,
82 [PA_COMMAND_OVERFLOW] = pa_command_overflow_or_underflow,
83 [PA_COMMAND_UNDERFLOW] = pa_command_overflow_or_underflow,
84 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = pa_command_stream_killed,
85 [PA_COMMAND_RECORD_STREAM_KILLED] = pa_command_stream_killed,
86 [PA_COMMAND_SUBSCRIBE_EVENT] = pa_command_subscribe_event
89 static void unlock_autospawn_lock_file(pa_context *c) {
90 assert(c);
92 if (c->autospawn_lock_fd >= 0) {
93 char lf[PATH_MAX];
94 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
96 pa_unlock_lockfile(lf, c->autospawn_lock_fd);
97 c->autospawn_lock_fd = -1;
101 static void context_free(pa_context *c);
103 pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name) {
104 pa_context *c;
106 assert(mainloop);
107 assert(name);
109 c = pa_xnew(pa_context, 1);
110 c->ref = 1;
111 c->name = pa_xstrdup(name);
112 c->mainloop = mainloop;
113 c->client = NULL;
114 c->pstream = NULL;
115 c->pdispatch = NULL;
116 c->playback_streams = pa_dynarray_new();
117 c->record_streams = pa_dynarray_new();
119 PA_LLIST_HEAD_INIT(pa_stream, c->streams);
120 PA_LLIST_HEAD_INIT(pa_operation, c->operations);
122 c->error = PA_OK;
123 c->state = PA_CONTEXT_UNCONNECTED;
124 c->ctag = 0;
125 c->csyncid = 0;
127 c->state_callback = NULL;
128 c->state_userdata = NULL;
130 c->subscribe_callback = NULL;
131 c->subscribe_userdata = NULL;
133 c->is_local = -1;
134 c->server_list = NULL;
135 c->server = NULL;
136 c->autospawn_lock_fd = -1;
137 memset(&c->spawn_api, 0, sizeof(c->spawn_api));
138 c->do_autospawn = 0;
140 #ifndef MSG_NOSIGNAL
141 #ifdef SIGPIPE
142 pa_check_signal_is_blocked(SIGPIPE);
143 #endif
144 #endif
146 c->conf = pa_client_conf_new();
147 pa_client_conf_load(c->conf, NULL);
148 #ifdef HAVE_X11
149 pa_client_conf_from_x11(c->conf, NULL);
150 #endif
151 pa_client_conf_env(c->conf);
153 if (!(c->mempool = pa_mempool_new(!c->conf->disable_shm))) {
155 if (!c->conf->disable_shm)
156 c->mempool = pa_mempool_new(0);
158 if (!c->mempool) {
159 context_free(c);
160 return NULL;
164 return c;
167 static void context_free(pa_context *c) {
168 assert(c);
170 unlock_autospawn_lock_file(c);
172 while (c->operations)
173 pa_operation_cancel(c->operations);
175 while (c->streams)
176 pa_stream_set_state(c->streams, PA_STREAM_TERMINATED);
178 if (c->client)
179 pa_socket_client_unref(c->client);
180 if (c->pdispatch)
181 pa_pdispatch_unref(c->pdispatch);
182 if (c->pstream) {
183 pa_pstream_close(c->pstream);
184 pa_pstream_unref(c->pstream);
187 if (c->record_streams)
188 pa_dynarray_free(c->record_streams, NULL, NULL);
189 if (c->playback_streams)
190 pa_dynarray_free(c->playback_streams, NULL, NULL);
192 if (c->mempool)
193 pa_mempool_free(c->mempool);
195 if (c->conf)
196 pa_client_conf_free(c->conf);
198 pa_strlist_free(c->server_list);
200 pa_xfree(c->name);
201 pa_xfree(c->server);
202 pa_xfree(c);
205 pa_context* pa_context_ref(pa_context *c) {
206 assert(c);
207 assert(c->ref >= 1);
209 c->ref++;
210 return c;
213 void pa_context_unref(pa_context *c) {
214 assert(c);
215 assert(c->ref >= 1);
217 if (--c->ref <= 0)
218 context_free(c);
221 void pa_context_set_state(pa_context *c, pa_context_state_t st) {
222 assert(c);
223 assert(c->ref >= 1);
225 if (c->state == st)
226 return;
228 pa_context_ref(c);
230 c->state = st;
231 if (c->state_callback)
232 c->state_callback(c, c->state_userdata);
234 if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED) {
235 pa_stream *s;
237 s = c->streams ? pa_stream_ref(c->streams) : NULL;
238 while (s) {
239 pa_stream *n = s->next ? pa_stream_ref(s->next) : NULL;
240 pa_stream_set_state(s, st == PA_CONTEXT_FAILED ? PA_STREAM_FAILED : PA_STREAM_TERMINATED);
241 pa_stream_unref(s);
242 s = n;
245 if (c->pdispatch)
246 pa_pdispatch_unref(c->pdispatch);
247 c->pdispatch = NULL;
249 if (c->pstream) {
250 pa_pstream_close(c->pstream);
251 pa_pstream_unref(c->pstream);
253 c->pstream = NULL;
255 if (c->client)
256 pa_socket_client_unref(c->client);
257 c->client = NULL;
260 pa_context_unref(c);
263 void pa_context_fail(pa_context *c, int error) {
264 assert(c);
265 assert(c->ref >= 1);
267 pa_context_set_error(c, error);
268 pa_context_set_state(c, PA_CONTEXT_FAILED);
271 int pa_context_set_error(pa_context *c, int error) {
272 assert(error >= 0);
273 assert(error < PA_ERR_MAX);
275 if (c)
276 c->error = error;
278 return error;
281 static void pstream_die_callback(pa_pstream *p, void *userdata) {
282 pa_context *c = userdata;
284 assert(p);
285 assert(c);
287 pa_context_fail(c, PA_ERR_CONNECTIONTERMINATED);
290 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
291 pa_context *c = userdata;
293 assert(p);
294 assert(packet);
295 assert(c);
297 pa_context_ref(c);
299 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0)
300 pa_context_fail(c, PA_ERR_PROTOCOL);
302 pa_context_unref(c);
305 static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t offset, pa_seek_mode_t seek, const pa_memchunk *chunk, void *userdata) {
306 pa_context *c = userdata;
307 pa_stream *s;
309 assert(p);
310 assert(chunk);
311 assert(chunk->memblock);
312 assert(chunk->length);
313 assert(c);
314 assert(c->ref >= 1);
316 pa_context_ref(c);
318 if ((s = pa_dynarray_get(c->record_streams, channel))) {
320 assert(seek == PA_SEEK_RELATIVE && offset == 0);
322 pa_memblockq_seek(s->record_memblockq, offset, seek);
323 pa_memblockq_push_align(s->record_memblockq, chunk);
325 if (s->read_callback) {
326 size_t l;
328 if ((l = pa_memblockq_get_length(s->record_memblockq)) > 0)
329 s->read_callback(s, l, s->read_userdata);
333 pa_context_unref(c);
336 int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t) {
337 assert(c);
338 assert(c->ref >= 1);
340 if (command == PA_COMMAND_ERROR) {
341 assert(t);
343 if (pa_tagstruct_getu32(t, &c->error) < 0) {
344 pa_context_fail(c, PA_ERR_PROTOCOL);
345 return -1;
348 } else if (command == PA_COMMAND_TIMEOUT)
349 c->error = PA_ERR_TIMEOUT;
350 else {
351 pa_context_fail(c, PA_ERR_PROTOCOL);
352 return -1;
355 return 0;
358 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
359 pa_context *c = userdata;
361 assert(pd);
362 assert(c);
363 assert(c->state == PA_CONTEXT_AUTHORIZING || c->state == PA_CONTEXT_SETTING_NAME);
365 pa_context_ref(c);
367 if (command != PA_COMMAND_REPLY) {
369 if (pa_context_handle_error(c, command, t) < 0)
370 pa_context_fail(c, PA_ERR_PROTOCOL);
372 pa_context_fail(c, c->error);
373 goto finish;
376 switch(c->state) {
377 case PA_CONTEXT_AUTHORIZING: {
378 pa_tagstruct *reply;
380 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
381 !pa_tagstruct_eof(t)) {
382 pa_context_fail(c, PA_ERR_PROTOCOL);
383 goto finish;
386 /* Minimum supported version */
387 if (c->version < 8) {
388 pa_context_fail(c, PA_ERR_VERSION);
389 goto finish;
392 /* Enable shared memory support if possible */
393 if (c->version >= 10 &&
394 pa_mempool_is_shared(c->mempool) &&
395 c->is_local) {
397 /* Only enable SHM if both sides are owned by the same
398 * user. This is a security measure because otherwise
399 * data private to the user might leak. */
401 #ifdef HAVE_CREDS
402 const pa_creds *creds;
403 if ((creds = pa_pdispatch_creds(pd)))
404 if (getuid() == creds->uid)
405 pa_pstream_use_shm(c->pstream, 1);
406 #endif
409 reply = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
410 pa_tagstruct_puts(reply, c->name);
411 pa_pstream_send_tagstruct(c->pstream, reply);
412 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
414 pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);
415 break;
418 case PA_CONTEXT_SETTING_NAME :
419 pa_context_set_state(c, PA_CONTEXT_READY);
420 break;
422 default:
423 assert(0);
426 finish:
427 pa_context_unref(c);
430 static void setup_context(pa_context *c, pa_iochannel *io) {
431 pa_tagstruct *t;
432 uint32_t tag;
434 assert(c);
435 assert(io);
437 pa_context_ref(c);
439 assert(!c->pstream);
440 c->pstream = pa_pstream_new(c->mainloop, io, c->mempool);
442 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
443 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
444 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
446 assert(!c->pdispatch);
447 c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
449 if (!c->conf->cookie_valid)
450 pa_log_warn("No cookie loaded. Attempting to connect without.");
452 t = pa_tagstruct_command(c, PA_COMMAND_AUTH, &tag);
453 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
454 pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
456 #ifdef HAVE_CREDS
458 pa_creds ucred;
460 if (pa_iochannel_creds_supported(io))
461 pa_iochannel_creds_enable(io);
463 ucred.uid = getuid();
464 ucred.gid = getgid();
466 pa_pstream_send_tagstruct_with_creds(c->pstream, t, &ucred);
468 #else
469 pa_pstream_send_tagstruct(c->pstream, t);
470 #endif
472 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
474 pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);
476 pa_context_unref(c);
479 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata);
481 #ifndef OS_IS_WIN32
483 static int context_connect_spawn(pa_context *c) {
484 pid_t pid;
485 int status, r;
486 int fds[2] = { -1, -1} ;
487 pa_iochannel *io;
489 pa_context_ref(c);
491 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
492 pa_log("socketpair(): %s", pa_cstrerror(errno));
493 pa_context_fail(c, PA_ERR_INTERNAL);
494 goto fail;
497 pa_fd_set_cloexec(fds[0], 1);
499 pa_socket_low_delay(fds[0]);
500 pa_socket_low_delay(fds[1]);
502 if (c->spawn_api.prefork)
503 c->spawn_api.prefork();
505 if ((pid = fork()) < 0) {
506 pa_log("fork(): %s", pa_cstrerror(errno));
507 pa_context_fail(c, PA_ERR_INTERNAL);
509 if (c->spawn_api.postfork)
510 c->spawn_api.postfork();
512 goto fail;
513 } else if (!pid) {
514 /* Child */
516 char t[128];
517 const char *state = NULL;
518 #define MAX_ARGS 64
519 const char * argv[MAX_ARGS+1];
520 int n;
522 /* Not required, since fds[0] has CLOEXEC enabled anyway */
523 close(fds[0]);
525 if (c->spawn_api.atfork)
526 c->spawn_api.atfork();
528 /* Setup argv */
530 n = 0;
532 argv[n++] = c->conf->daemon_binary;
533 argv[n++] = "--daemonize=yes";
535 snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
536 argv[n++] = strdup(t);
538 while (n < MAX_ARGS) {
539 char *a;
541 if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
542 break;
544 argv[n++] = a;
547 argv[n++] = NULL;
549 execv(argv[0], (char * const *) argv);
550 _exit(1);
551 #undef MAX_ARGS
554 /* Parent */
556 r = waitpid(pid, &status, 0);
558 if (c->spawn_api.postfork)
559 c->spawn_api.postfork();
561 if (r < 0) {
562 pa_log("waitpid(): %s", pa_cstrerror(errno));
563 pa_context_fail(c, PA_ERR_INTERNAL);
564 goto fail;
565 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
566 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
567 goto fail;
570 close(fds[1]);
572 c->is_local = 1;
574 io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
576 setup_context(c, io);
577 unlock_autospawn_lock_file(c);
579 pa_context_unref(c);
581 return 0;
583 fail:
584 if (fds[0] != -1)
585 close(fds[0]);
586 if (fds[1] != -1)
587 close(fds[1]);
589 unlock_autospawn_lock_file(c);
591 pa_context_unref(c);
593 return -1;
596 #endif /* OS_IS_WIN32 */
598 static int try_next_connection(pa_context *c) {
599 char *u = NULL;
600 int r = -1;
602 assert(c);
603 assert(!c->client);
605 for (;;) {
606 pa_xfree(u);
607 u = NULL;
609 c->server_list = pa_strlist_pop(c->server_list, &u);
611 if (!u) {
613 #ifndef OS_IS_WIN32
614 if (c->do_autospawn) {
615 r = context_connect_spawn(c);
616 goto finish;
618 #endif
620 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
621 goto finish;
624 pa_log_debug("Trying to connect to %s...", u);
626 pa_xfree(c->server);
627 c->server = pa_xstrdup(u);
629 if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
630 continue;
632 c->is_local = pa_socket_client_is_local(c->client);
633 pa_socket_client_set_callback(c->client, on_connection, c);
634 break;
637 r = 0;
639 finish:
640 pa_xfree(u);
642 return r;
645 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata) {
646 pa_context *c = userdata;
648 assert(client);
649 assert(c);
650 assert(c->state == PA_CONTEXT_CONNECTING);
652 pa_context_ref(c);
654 pa_socket_client_unref(client);
655 c->client = NULL;
657 if (!io) {
658 /* Try the item in the list */
659 if (errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH) {
660 try_next_connection(c);
661 goto finish;
664 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
665 goto finish;
668 unlock_autospawn_lock_file(c);
669 setup_context(c, io);
671 finish:
672 pa_context_unref(c);
675 int pa_context_connect(
676 pa_context *c,
677 const char *server,
678 pa_context_flags_t flags,
679 const pa_spawn_api *api) {
681 int r = -1;
683 assert(c);
684 assert(c->ref >= 1);
686 PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
687 PA_CHECK_VALIDITY(c, !(flags & ~PA_CONTEXT_NOAUTOSPAWN), PA_ERR_INVALID);
688 PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID);
690 if (!server)
691 server = c->conf->default_server;
693 pa_context_ref(c);
695 assert(!c->server_list);
697 if (server) {
698 if (!(c->server_list = pa_strlist_parse(server))) {
699 pa_context_fail(c, PA_ERR_INVALIDSERVER);
700 goto finish;
702 } else {
703 char *d;
704 char ufn[PATH_MAX];
706 /* Prepend in reverse order */
708 if ((d = getenv("DISPLAY"))) {
709 char *e;
710 d = pa_xstrdup(d);
711 if ((e = strchr(d, ':')))
712 *e = 0;
714 if (*d)
715 c->server_list = pa_strlist_prepend(c->server_list, d);
717 pa_xfree(d);
720 c->server_list = pa_strlist_prepend(c->server_list, "tcp6:localhost");
721 c->server_list = pa_strlist_prepend(c->server_list, "tcp4:localhost");
723 /* The system wide instance */
724 c->server_list = pa_strlist_prepend(c->server_list, PA_SYSTEM_RUNTIME_PATH "/" PA_NATIVE_DEFAULT_UNIX_SOCKET);
726 /* The per-user instance */
727 c->server_list = pa_strlist_prepend(c->server_list, pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET, ufn, sizeof(ufn)));
729 /* Wrap the connection attempts in a single transaction for sane autospawn locking */
730 if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
731 char lf[PATH_MAX];
733 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
734 pa_make_secure_parent_dir(lf, 0700, (uid_t)-1, (gid_t)-1);
735 assert(c->autospawn_lock_fd <= 0);
736 c->autospawn_lock_fd = pa_lock_lockfile(lf);
738 if (api)
739 c->spawn_api = *api;
740 c->do_autospawn = 1;
745 pa_context_set_state(c, PA_CONTEXT_CONNECTING);
746 r = try_next_connection(c);
748 finish:
749 pa_context_unref(c);
751 return r;
754 void pa_context_disconnect(pa_context *c) {
755 assert(c);
756 assert(c->ref >= 1);
758 pa_context_set_state(c, PA_CONTEXT_TERMINATED);
761 pa_context_state_t pa_context_get_state(pa_context *c) {
762 assert(c);
763 assert(c->ref >= 1);
765 return c->state;
768 int pa_context_errno(pa_context *c) {
769 assert(c);
770 assert(c->ref >= 1);
772 return c->error;
775 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
776 assert(c);
777 assert(c->ref >= 1);
779 c->state_callback = cb;
780 c->state_userdata = userdata;
783 int pa_context_is_pending(pa_context *c) {
784 assert(c);
785 assert(c->ref >= 1);
787 PA_CHECK_VALIDITY(c,
788 c->state == PA_CONTEXT_CONNECTING ||
789 c->state == PA_CONTEXT_AUTHORIZING ||
790 c->state == PA_CONTEXT_SETTING_NAME ||
791 c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
793 return (c->pstream && pa_pstream_is_pending(c->pstream)) ||
794 (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) ||
795 c->client;
798 static void set_dispatch_callbacks(pa_operation *o);
800 static void pdispatch_drain_callback(PA_GCC_UNUSED pa_pdispatch*pd, void *userdata) {
801 set_dispatch_callbacks(userdata);
804 static void pstream_drain_callback(PA_GCC_UNUSED pa_pstream *s, void *userdata) {
805 set_dispatch_callbacks(userdata);
808 static void set_dispatch_callbacks(pa_operation *o) {
809 int done = 1;
811 assert(o);
812 assert(o->ref >= 1);
813 assert(o->context);
814 assert(o->context->ref >= 1);
815 assert(o->context->state == PA_CONTEXT_READY);
817 pa_pstream_set_drain_callback(o->context->pstream, NULL, NULL);
818 pa_pdispatch_set_drain_callback(o->context->pdispatch, NULL, NULL);
820 if (pa_pdispatch_is_pending(o->context->pdispatch)) {
821 pa_pdispatch_set_drain_callback(o->context->pdispatch, pdispatch_drain_callback, o);
822 done = 0;
825 if (pa_pstream_is_pending(o->context->pstream)) {
826 pa_pstream_set_drain_callback(o->context->pstream, pstream_drain_callback, o);
827 done = 0;
830 if (done) {
831 if (o->callback) {
832 pa_context_notify_cb_t cb = (pa_context_notify_cb_t) o->callback;
833 cb(o->context, o->userdata);
836 pa_operation_done(o);
837 pa_operation_unref(o);
841 pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
842 pa_operation *o;
844 assert(c);
845 assert(c->ref >= 1);
847 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
848 PA_CHECK_VALIDITY_RETURN_NULL(c, pa_context_is_pending(c), PA_ERR_BADSTATE);
850 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
851 set_dispatch_callbacks(pa_operation_ref(o));
853 return o;
856 void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
857 pa_operation *o = userdata;
858 int success = 1;
860 assert(pd);
861 assert(o);
862 assert(o->ref >= 1);
864 if (!o->context)
865 goto finish;
867 if (command != PA_COMMAND_REPLY) {
868 if (pa_context_handle_error(o->context, command, t) < 0)
869 goto finish;
871 success = 0;
872 } else if (!pa_tagstruct_eof(t)) {
873 pa_context_fail(o->context, PA_ERR_PROTOCOL);
874 goto finish;
877 if (o->callback) {
878 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
879 cb(o->context, success, o->userdata);
882 finish:
883 pa_operation_done(o);
884 pa_operation_unref(o);
887 pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata) {
888 pa_tagstruct *t;
889 pa_operation *o;
890 uint32_t tag;
892 assert(c);
893 assert(c->ref >= 1);
895 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
897 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
899 t = pa_tagstruct_command(c, PA_COMMAND_EXIT, &tag);
900 pa_pstream_send_tagstruct(c->pstream, t);
901 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
903 return o;
906 pa_operation* pa_context_send_simple_command(pa_context *c, uint32_t command, pa_pdispatch_cb_t internal_cb, pa_operation_cb_t cb, void *userdata) {
907 pa_tagstruct *t;
908 pa_operation *o;
909 uint32_t tag;
911 assert(c);
912 assert(c->ref >= 1);
914 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
916 o = pa_operation_new(c, NULL, cb, userdata);
918 t = pa_tagstruct_command(c, command, &tag);
919 pa_pstream_send_tagstruct(c->pstream, t);
920 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, internal_cb, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
922 return o;
925 pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
926 pa_tagstruct *t;
927 pa_operation *o;
928 uint32_t tag;
930 assert(c);
931 assert(c->ref >= 1);
933 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
935 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
937 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SINK, &tag);
938 pa_tagstruct_puts(t, name);
939 pa_pstream_send_tagstruct(c->pstream, t);
940 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
942 return o;
945 pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
946 pa_tagstruct *t;
947 pa_operation *o;
948 uint32_t tag;
950 assert(c);
951 assert(c->ref >= 1);
953 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
955 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
957 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SOURCE, &tag);
958 pa_tagstruct_puts(t, name);
959 pa_pstream_send_tagstruct(c->pstream, t);
960 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
962 return o;
965 int pa_context_is_local(pa_context *c) {
966 assert(c);
968 return c->is_local;
971 pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
972 pa_tagstruct *t;
973 pa_operation *o;
974 uint32_t tag;
976 assert(c);
977 assert(c->ref >= 1);
978 assert(name);
980 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
982 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
984 t = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
985 pa_tagstruct_puts(t, name);
986 pa_pstream_send_tagstruct(c->pstream, t);
987 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
989 return o;
992 const char* pa_get_library_version(void) {
993 return PACKAGE_VERSION;
996 const char* pa_context_get_server(pa_context *c) {
997 assert(c);
998 assert(c->ref >= 1);
1000 if (!c->server)
1001 return NULL;
1003 if (*c->server == '{') {
1004 char *e = strchr(c->server+1, '}');
1005 return e ? e+1 : c->server;
1008 return c->server;
1011 uint32_t pa_context_get_protocol_version(PA_GCC_UNUSED pa_context *c) {
1012 return PA_PROTOCOL_VERSION;
1015 uint32_t pa_context_get_server_protocol_version(pa_context *c) {
1016 assert(c);
1017 assert(c->ref >= 1);
1019 return c->version;
1022 pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *tag) {
1023 pa_tagstruct *t;
1025 assert(c);
1026 assert(tag);
1028 t = pa_tagstruct_new(NULL, 0);
1029 pa_tagstruct_putu32(t, command);
1030 pa_tagstruct_putu32(t, *tag = c->ctag++);
1032 return t;