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