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