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