]> code.delx.au - pulseaudio/blob - src/pulse/context.c
enable SHM support on the client side only if both the client and the server run...
[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->mempool = pa_mempool_new(1);
132 c->is_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_mempool_free(c->mempool);
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 /* Enable shared memory support if possible */
380 if (c->version >= 10 &&
381 pa_mempool_is_shared(c->mempool) &&
382 c->is_local) {
383
384 /* Only enable SHM if both sides are owned by the same
385 * user. This is a security measure because otherwise
386 * data private to the user might leak. */
387
388 const pa_creds *creds;
389 if ((creds = pa_pdispatch_creds(pd)))
390 if (getuid() == creds->uid)
391 pa_pstream_use_shm(c->pstream, 1);
392 }
393
394 reply = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
395 pa_tagstruct_puts(reply, c->name);
396 pa_pstream_send_tagstruct(c->pstream, reply);
397 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
398
399 pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);
400 break;
401 }
402
403 case PA_CONTEXT_SETTING_NAME :
404 pa_context_set_state(c, PA_CONTEXT_READY);
405 break;
406
407 default:
408 assert(0);
409 }
410
411 finish:
412 pa_context_unref(c);
413 }
414
415 static void setup_context(pa_context *c, pa_iochannel *io) {
416 pa_tagstruct *t;
417 uint32_t tag;
418
419 assert(c);
420 assert(io);
421
422 pa_context_ref(c);
423
424 assert(!c->pstream);
425 c->pstream = pa_pstream_new(c->mainloop, io, c->mempool);
426
427 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
428 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
429 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
430
431 assert(!c->pdispatch);
432 c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
433
434 if (!c->conf->cookie_valid) {
435 pa_context_fail(c, PA_ERR_AUTHKEY);
436 goto finish;
437 }
438
439 t = pa_tagstruct_command(c, PA_COMMAND_AUTH, &tag);
440 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
441 pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
442
443 #ifdef HAVE_CREDS
444 {
445 pa_creds ucred;
446
447 if (pa_iochannel_creds_supported(io))
448 pa_iochannel_creds_enable(io);
449
450 ucred.uid = getuid();
451 ucred.gid = getgid();
452
453 pa_pstream_send_tagstruct_with_creds(c->pstream, t, &ucred);
454 }
455 #else
456 pa_pstream_send_tagstruct(c->pstream, t, NULL);
457 #endif
458
459 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
460
461 pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);
462
463 finish:
464
465 pa_context_unref(c);
466 }
467
468 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata);
469
470 #ifndef OS_IS_WIN32
471
472 static int context_connect_spawn(pa_context *c) {
473 pid_t pid;
474 int status, r;
475 int fds[2] = { -1, -1} ;
476 pa_iochannel *io;
477
478 pa_context_ref(c);
479
480 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
481 pa_log("socketpair(): %s", pa_cstrerror(errno));
482 pa_context_fail(c, PA_ERR_INTERNAL);
483 goto fail;
484 }
485
486 pa_fd_set_cloexec(fds[0], 1);
487
488 pa_socket_low_delay(fds[0]);
489 pa_socket_low_delay(fds[1]);
490
491 if (c->spawn_api.prefork)
492 c->spawn_api.prefork();
493
494 if ((pid = fork()) < 0) {
495 pa_log("fork(): %s", pa_cstrerror(errno));
496 pa_context_fail(c, PA_ERR_INTERNAL);
497
498 if (c->spawn_api.postfork)
499 c->spawn_api.postfork();
500
501 goto fail;
502 } else if (!pid) {
503 /* Child */
504
505 char t[128];
506 const char *state = NULL;
507 #define MAX_ARGS 64
508 const char * argv[MAX_ARGS+1];
509 int n;
510
511 /* Not required, since fds[0] has CLOEXEC enabled anyway */
512 close(fds[0]);
513
514 if (c->spawn_api.atfork)
515 c->spawn_api.atfork();
516
517 /* Setup argv */
518
519 n = 0;
520
521 argv[n++] = c->conf->daemon_binary;
522 argv[n++] = "--daemonize=yes";
523
524 snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
525 argv[n++] = strdup(t);
526
527 while (n < MAX_ARGS) {
528 char *a;
529
530 if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
531 break;
532
533 argv[n++] = a;
534 }
535
536 argv[n++] = NULL;
537
538 execv(argv[0], (char * const *) argv);
539 _exit(1);
540 #undef MAX_ARGS
541 }
542
543 /* Parent */
544
545 r = waitpid(pid, &status, 0);
546
547 if (c->spawn_api.postfork)
548 c->spawn_api.postfork();
549
550 if (r < 0) {
551 pa_log("waitpid(): %s", pa_cstrerror(errno));
552 pa_context_fail(c, PA_ERR_INTERNAL);
553 goto fail;
554 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
555 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
556 goto fail;
557 }
558
559 close(fds[1]);
560
561 c->is_local = 1;
562
563 io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
564
565 setup_context(c, io);
566 unlock_autospawn_lock_file(c);
567
568 pa_context_unref(c);
569
570 return 0;
571
572 fail:
573 if (fds[0] != -1)
574 close(fds[0]);
575 if (fds[1] != -1)
576 close(fds[1]);
577
578 unlock_autospawn_lock_file(c);
579
580 pa_context_unref(c);
581
582 return -1;
583 }
584
585 #endif /* OS_IS_WIN32 */
586
587 static int try_next_connection(pa_context *c) {
588 char *u = NULL;
589 int r = -1;
590
591 assert(c);
592 assert(!c->client);
593
594 for (;;) {
595 pa_xfree(u);
596 u = NULL;
597
598 c->server_list = pa_strlist_pop(c->server_list, &u);
599
600 if (!u) {
601
602 #ifndef OS_IS_WIN32
603 if (c->do_autospawn) {
604 r = context_connect_spawn(c);
605 goto finish;
606 }
607 #endif
608
609 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
610 goto finish;
611 }
612
613 pa_log_debug("Trying to connect to %s...", u);
614
615 pa_xfree(c->server);
616 c->server = pa_xstrdup(u);
617
618 if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
619 continue;
620
621 c->is_local = pa_socket_client_is_local(c->client);
622 pa_socket_client_set_callback(c->client, on_connection, c);
623 break;
624 }
625
626 r = 0;
627
628 finish:
629 pa_xfree(u);
630
631 return r;
632 }
633
634 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata) {
635 pa_context *c = userdata;
636
637 assert(client);
638 assert(c);
639 assert(c->state == PA_CONTEXT_CONNECTING);
640
641 pa_context_ref(c);
642
643 pa_socket_client_unref(client);
644 c->client = NULL;
645
646 if (!io) {
647 /* Try the item in the list */
648 if (errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH) {
649 try_next_connection(c);
650 goto finish;
651 }
652
653 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
654 goto finish;
655 }
656
657 unlock_autospawn_lock_file(c);
658 setup_context(c, io);
659
660 finish:
661 pa_context_unref(c);
662 }
663
664 int pa_context_connect(
665 pa_context *c,
666 const char *server,
667 pa_context_flags_t flags,
668 const pa_spawn_api *api) {
669
670 int r = -1;
671
672 assert(c);
673 assert(c->ref >= 1);
674
675 PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
676 PA_CHECK_VALIDITY(c, !(flags & ~PA_CONTEXT_NOAUTOSPAWN), PA_ERR_INVALID);
677 PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID);
678
679 if (!server)
680 server = c->conf->default_server;
681
682 pa_context_ref(c);
683
684 assert(!c->server_list);
685
686 if (server) {
687 if (!(c->server_list = pa_strlist_parse(server))) {
688 pa_context_fail(c, PA_ERR_INVALIDSERVER);
689 goto finish;
690 }
691 } else {
692 char *d;
693 char ufn[PATH_MAX];
694
695 /* Prepend in reverse order */
696
697 if ((d = getenv("DISPLAY"))) {
698 char *e;
699 d = pa_xstrdup(d);
700 if ((e = strchr(d, ':')))
701 *e = 0;
702
703 if (*d)
704 c->server_list = pa_strlist_prepend(c->server_list, d);
705
706 pa_xfree(d);
707 }
708
709 c->server_list = pa_strlist_prepend(c->server_list, "tcp6:localhost");
710 c->server_list = pa_strlist_prepend(c->server_list, "tcp4:localhost");
711
712 /* The system wide instance */
713 c->server_list = pa_strlist_prepend(c->server_list, PA_SYSTEM_RUNTIME_PATH "/" PA_NATIVE_DEFAULT_UNIX_SOCKET);
714
715 /* The per-user instance */
716 c->server_list = pa_strlist_prepend(c->server_list, pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET, ufn, sizeof(ufn)));
717
718 /* Wrap the connection attempts in a single transaction for sane autospawn locking */
719 if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
720 char lf[PATH_MAX];
721
722 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
723 pa_make_secure_parent_dir(lf, 0700, (uid_t)-1, (gid_t)-1);
724 assert(c->autospawn_lock_fd <= 0);
725 c->autospawn_lock_fd = pa_lock_lockfile(lf);
726
727 if (api)
728 c->spawn_api = *api;
729 c->do_autospawn = 1;
730 }
731
732 }
733
734 pa_context_set_state(c, PA_CONTEXT_CONNECTING);
735 r = try_next_connection(c);
736
737 finish:
738 pa_context_unref(c);
739
740 return r;
741 }
742
743 void pa_context_disconnect(pa_context *c) {
744 assert(c);
745 assert(c->ref >= 1);
746
747 pa_context_set_state(c, PA_CONTEXT_TERMINATED);
748 }
749
750 pa_context_state_t pa_context_get_state(pa_context *c) {
751 assert(c);
752 assert(c->ref >= 1);
753
754 return c->state;
755 }
756
757 int pa_context_errno(pa_context *c) {
758 assert(c);
759 assert(c->ref >= 1);
760
761 return c->error;
762 }
763
764 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
765 assert(c);
766 assert(c->ref >= 1);
767
768 c->state_callback = cb;
769 c->state_userdata = userdata;
770 }
771
772 int pa_context_is_pending(pa_context *c) {
773 assert(c);
774 assert(c->ref >= 1);
775
776 PA_CHECK_VALIDITY(c,
777 c->state == PA_CONTEXT_CONNECTING ||
778 c->state == PA_CONTEXT_AUTHORIZING ||
779 c->state == PA_CONTEXT_SETTING_NAME ||
780 c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
781
782 return (c->pstream && pa_pstream_is_pending(c->pstream)) ||
783 (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) ||
784 c->client;
785 }
786
787 static void set_dispatch_callbacks(pa_operation *o);
788
789 static void pdispatch_drain_callback(PA_GCC_UNUSED pa_pdispatch*pd, void *userdata) {
790 set_dispatch_callbacks(userdata);
791 }
792
793 static void pstream_drain_callback(PA_GCC_UNUSED pa_pstream *s, void *userdata) {
794 set_dispatch_callbacks(userdata);
795 }
796
797 static void set_dispatch_callbacks(pa_operation *o) {
798 int done = 1;
799
800 assert(o);
801 assert(o->ref >= 1);
802 assert(o->context);
803 assert(o->context->ref >= 1);
804 assert(o->context->state == PA_CONTEXT_READY);
805
806 pa_pstream_set_drain_callback(o->context->pstream, NULL, NULL);
807 pa_pdispatch_set_drain_callback(o->context->pdispatch, NULL, NULL);
808
809 if (pa_pdispatch_is_pending(o->context->pdispatch)) {
810 pa_pdispatch_set_drain_callback(o->context->pdispatch, pdispatch_drain_callback, o);
811 done = 0;
812 }
813
814 if (pa_pstream_is_pending(o->context->pstream)) {
815 pa_pstream_set_drain_callback(o->context->pstream, pstream_drain_callback, o);
816 done = 0;
817 }
818
819 if (done) {
820 if (o->callback) {
821 pa_context_notify_cb_t cb = (pa_context_notify_cb_t) o->callback;
822 cb(o->context, o->userdata);
823 }
824
825 pa_operation_done(o);
826 pa_operation_unref(o);
827 }
828 }
829
830 pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
831 pa_operation *o;
832
833 assert(c);
834 assert(c->ref >= 1);
835
836 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
837 PA_CHECK_VALIDITY_RETURN_NULL(c, pa_context_is_pending(c), PA_ERR_BADSTATE);
838
839 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
840 set_dispatch_callbacks(pa_operation_ref(o));
841
842 return o;
843 }
844
845 void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
846 pa_operation *o = userdata;
847 int success = 1;
848
849 assert(pd);
850 assert(o);
851 assert(o->ref >= 1);
852
853 if (!o->context)
854 goto finish;
855
856 if (command != PA_COMMAND_REPLY) {
857 if (pa_context_handle_error(o->context, command, t) < 0)
858 goto finish;
859
860 success = 0;
861 } else if (!pa_tagstruct_eof(t)) {
862 pa_context_fail(o->context, PA_ERR_PROTOCOL);
863 goto finish;
864 }
865
866 if (o->callback) {
867 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
868 cb(o->context, success, o->userdata);
869 }
870
871 finish:
872 pa_operation_done(o);
873 pa_operation_unref(o);
874 }
875
876 pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata) {
877 pa_tagstruct *t;
878 pa_operation *o;
879 uint32_t tag;
880
881 assert(c);
882 assert(c->ref >= 1);
883
884 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
885
886 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
887
888 t = pa_tagstruct_command(c, PA_COMMAND_EXIT, &tag);
889 pa_pstream_send_tagstruct(c->pstream, t);
890 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);
891
892 return o;
893 }
894
895 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) {
896 pa_tagstruct *t;
897 pa_operation *o;
898 uint32_t tag;
899
900 assert(c);
901 assert(c->ref >= 1);
902
903 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
904
905 o = pa_operation_new(c, NULL, cb, userdata);
906
907 t = pa_tagstruct_command(c, command, &tag);
908 pa_pstream_send_tagstruct(c->pstream, t);
909 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, internal_cb, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
910
911 return o;
912 }
913
914 pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
915 pa_tagstruct *t;
916 pa_operation *o;
917 uint32_t tag;
918
919 assert(c);
920 assert(c->ref >= 1);
921
922 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
923
924 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
925
926 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SINK, &tag);
927 pa_tagstruct_puts(t, name);
928 pa_pstream_send_tagstruct(c->pstream, t);
929 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);
930
931 return o;
932 }
933
934 pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
935 pa_tagstruct *t;
936 pa_operation *o;
937 uint32_t tag;
938
939 assert(c);
940 assert(c->ref >= 1);
941
942 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
943
944 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
945
946 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SOURCE, &tag);
947 pa_tagstruct_puts(t, name);
948 pa_pstream_send_tagstruct(c->pstream, t);
949 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);
950
951 return o;
952 }
953
954 int pa_context_is_local(pa_context *c) {
955 assert(c);
956
957 return c->is_local;
958 }
959
960 pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
961 pa_tagstruct *t;
962 pa_operation *o;
963 uint32_t tag;
964
965 assert(c);
966 assert(c->ref >= 1);
967 assert(name);
968
969 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
970
971 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
972
973 t = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
974 pa_tagstruct_puts(t, name);
975 pa_pstream_send_tagstruct(c->pstream, t);
976 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);
977
978 return o;
979 }
980
981 const char* pa_get_library_version(void) {
982 return PACKAGE_VERSION;
983 }
984
985 const char* pa_context_get_server(pa_context *c) {
986 assert(c);
987 assert(c->ref >= 1);
988
989 if (!c->server)
990 return NULL;
991
992 if (*c->server == '{') {
993 char *e = strchr(c->server+1, '}');
994 return e ? e+1 : c->server;
995 }
996
997 return c->server;
998 }
999
1000 uint32_t pa_context_get_protocol_version(PA_GCC_UNUSED pa_context *c) {
1001 return PA_PROTOCOL_VERSION;
1002 }
1003
1004 uint32_t pa_context_get_server_protocol_version(pa_context *c) {
1005 assert(c);
1006 assert(c->ref >= 1);
1007
1008 return c->version;
1009 }
1010
1011 pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *tag) {
1012 pa_tagstruct *t;
1013
1014 assert(c);
1015 assert(tag);
1016
1017 t = pa_tagstruct_new(NULL, 0);
1018 pa_tagstruct_putu32(t, command);
1019 pa_tagstruct_putu32(t, *tag = c->ctag++);
1020
1021 return t;
1022 }