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