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