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