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