]> code.delx.au - pulseaudio/blob - src/polyp/context.c
Do WSAStartup() in the DLL entry routine instead of at context creation.
[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 c->state = st;
211 if (c->state_callback)
212 c->state_callback(c, c->state_userdata);
213
214 if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED) {
215 pa_stream *s;
216
217 s = c->streams ? pa_stream_ref(c->streams) : NULL;
218 while (s) {
219 pa_stream *n = s->next ? pa_stream_ref(s->next) : NULL;
220 pa_stream_set_state(s, st == PA_CONTEXT_FAILED ? PA_STREAM_FAILED : PA_STREAM_TERMINATED);
221 pa_stream_unref(s);
222 s = n;
223 }
224
225 if (c->pdispatch)
226 pa_pdispatch_unref(c->pdispatch);
227 c->pdispatch = NULL;
228
229 if (c->pstream) {
230 pa_pstream_close(c->pstream);
231 pa_pstream_unref(c->pstream);
232 }
233 c->pstream = NULL;
234
235 if (c->client)
236 pa_socket_client_unref(c->client);
237 c->client = NULL;
238 }
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, const void *creds, 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, creds, 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 assert(seek == PA_SEEK_RELATIVE && offset == 0);
301
302 pa_memblockq_seek(s->record_memblockq, offset, seek);
303 pa_memblockq_push_align(s->record_memblockq, chunk);
304
305 if (s->read_callback) {
306 size_t l;
307
308 if ((l = pa_memblockq_get_length(s->record_memblockq)) > 0)
309 s->read_callback(s, l, s->read_userdata);
310 }
311 }
312
313 pa_context_unref(c);
314 }
315
316 int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t) {
317 assert(c);
318 assert(c->ref >= 1);
319
320 if (command == PA_COMMAND_ERROR) {
321 assert(t);
322
323 if (pa_tagstruct_getu32(t, &c->error) < 0) {
324 pa_context_fail(c, PA_ERR_PROTOCOL);
325 return -1;
326
327 }
328 } else if (command == PA_COMMAND_TIMEOUT)
329 c->error = PA_ERR_TIMEOUT;
330 else {
331 pa_context_fail(c, PA_ERR_PROTOCOL);
332 return -1;
333 }
334
335 return 0;
336 }
337
338 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
339 pa_context *c = userdata;
340
341 assert(pd);
342 assert(c);
343 assert(c->state == PA_CONTEXT_AUTHORIZING || c->state == PA_CONTEXT_SETTING_NAME);
344
345 pa_context_ref(c);
346
347 if (command != PA_COMMAND_REPLY) {
348
349 if (pa_context_handle_error(c, command, t) < 0)
350 pa_context_fail(c, PA_ERR_PROTOCOL);
351
352 pa_context_fail(c, c->error);
353 goto finish;
354 }
355
356 switch(c->state) {
357 case PA_CONTEXT_AUTHORIZING: {
358 pa_tagstruct *reply;
359
360 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
361 !pa_tagstruct_eof(t)) {
362 pa_context_fail(c, PA_ERR_PROTOCOL);
363 goto finish;
364 }
365
366 /* Minimum supported version */
367 if (c->version < 8) {
368 pa_context_fail(c, PA_ERR_VERSION);
369 goto finish;
370 }
371
372 reply = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
373 pa_tagstruct_puts(reply, c->name);
374 pa_pstream_send_tagstruct(c->pstream, reply);
375 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
376
377 pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);
378 break;
379 }
380
381 case PA_CONTEXT_SETTING_NAME :
382 pa_context_set_state(c, PA_CONTEXT_READY);
383 break;
384
385 default:
386 assert(0);
387 }
388
389 finish:
390 pa_context_unref(c);
391 }
392
393 static void setup_context(pa_context *c, pa_iochannel *io) {
394 pa_tagstruct *t;
395 uint32_t tag;
396
397 assert(c);
398 assert(io);
399
400 pa_context_ref(c);
401
402 assert(!c->pstream);
403 c->pstream = pa_pstream_new(c->mainloop, io, c->memblock_stat);
404
405 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
406 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
407 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
408
409 assert(!c->pdispatch);
410 c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
411
412 if (!c->conf->cookie_valid) {
413 pa_context_fail(c, PA_ERR_AUTHKEY);
414 goto finish;
415 }
416
417 t = pa_tagstruct_command(c, PA_COMMAND_AUTH, &tag);
418 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
419 pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
420 pa_pstream_send_tagstruct_with_creds(c->pstream, t, 1);
421 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
422
423 pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);
424
425 finish:
426
427 pa_context_unref(c);
428 }
429
430 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata);
431
432 #ifndef OS_IS_WIN32
433
434 static int context_connect_spawn(pa_context *c) {
435 pid_t pid;
436 int status, r;
437 int fds[2] = { -1, -1} ;
438 pa_iochannel *io;
439
440 pa_context_ref(c);
441
442 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
443 pa_log(__FILE__": socketpair() failed: %s", strerror(errno));
444 pa_context_fail(c, PA_ERR_INTERNAL);
445 goto fail;
446 }
447
448 pa_fd_set_cloexec(fds[0], 1);
449
450 pa_socket_low_delay(fds[0]);
451 pa_socket_low_delay(fds[1]);
452
453 if (c->spawn_api.prefork)
454 c->spawn_api.prefork();
455
456 if ((pid = fork()) < 0) {
457 pa_log(__FILE__": fork() failed: %s", strerror(errno));
458 pa_context_fail(c, PA_ERR_INTERNAL);
459
460 if (c->spawn_api.postfork)
461 c->spawn_api.postfork();
462
463 goto fail;
464 } else if (!pid) {
465 /* Child */
466
467 char t[128];
468 const char *state = NULL;
469 #define MAX_ARGS 64
470 const char * argv[MAX_ARGS+1];
471 int n;
472
473 /* Not required, since fds[0] has CLOEXEC enabled anyway */
474 close(fds[0]);
475
476 if (c->spawn_api.atfork)
477 c->spawn_api.atfork();
478
479 /* Setup argv */
480
481 n = 0;
482
483 argv[n++] = c->conf->daemon_binary;
484 argv[n++] = "--daemonize=yes";
485
486 snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
487 argv[n++] = strdup(t);
488
489 while (n < MAX_ARGS) {
490 char *a;
491
492 if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
493 break;
494
495 argv[n++] = a;
496 }
497
498 argv[n++] = NULL;
499
500 execv(argv[0], (char * const *) argv);
501 _exit(1);
502 #undef MAX_ARGS
503 }
504
505 /* Parent */
506
507 r = waitpid(pid, &status, 0);
508
509 if (c->spawn_api.postfork)
510 c->spawn_api.postfork();
511
512 if (r < 0) {
513 pa_log(__FILE__": waitpid() failed: %s", strerror(errno));
514 pa_context_fail(c, PA_ERR_INTERNAL);
515 goto fail;
516 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
517 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
518 goto fail;
519 }
520
521 close(fds[1]);
522
523 c->local = 1;
524
525 io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
526
527 setup_context(c, io);
528 unlock_autospawn_lock_file(c);
529
530 pa_context_unref(c);
531
532 return 0;
533
534 fail:
535 if (fds[0] != -1)
536 close(fds[0]);
537 if (fds[1] != -1)
538 close(fds[1]);
539
540 unlock_autospawn_lock_file(c);
541
542 pa_context_unref(c);
543
544 return -1;
545 }
546
547 #endif /* OS_IS_WIN32 */
548
549 static int try_next_connection(pa_context *c) {
550 char *u = NULL;
551 int r = -1;
552
553 assert(c);
554 assert(!c->client);
555
556 for (;;) {
557 pa_xfree(u);
558 u = NULL;
559
560 c->server_list = pa_strlist_pop(c->server_list, &u);
561
562 if (!u) {
563
564 #ifndef OS_IS_WIN32
565 if (c->do_autospawn) {
566 r = context_connect_spawn(c);
567 goto finish;
568 }
569 #endif
570
571 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
572 goto finish;
573 }
574
575 pa_log_debug(__FILE__": Trying to connect to %s...", u);
576
577 pa_xfree(c->server);
578 c->server = pa_xstrdup(u);
579
580 if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
581 continue;
582
583 c->local = pa_socket_client_is_local(c->client);
584 pa_socket_client_set_callback(c->client, on_connection, c);
585 break;
586 }
587
588 r = 0;
589
590 finish:
591 pa_xfree(u);
592
593 return r;
594 }
595
596 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata) {
597 pa_context *c = userdata;
598
599 assert(client);
600 assert(c);
601 assert(c->state == PA_CONTEXT_CONNECTING);
602
603 pa_context_ref(c);
604
605 pa_socket_client_unref(client);
606 c->client = NULL;
607
608 if (!io) {
609 /* Try the item in the list */
610 if (errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH) {
611 try_next_connection(c);
612 goto finish;
613 }
614
615 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
616 goto finish;
617 }
618
619 unlock_autospawn_lock_file(c);
620 setup_context(c, io);
621
622 finish:
623 pa_context_unref(c);
624 }
625
626 int pa_context_connect(
627 pa_context *c,
628 const char *server,
629 pa_context_flags_t flags,
630 const pa_spawn_api *api) {
631
632 int r = -1;
633
634 assert(c);
635 assert(c->ref >= 1);
636
637 PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
638 PA_CHECK_VALIDITY(c, !(flags & ~PA_CONTEXT_NOAUTOSPAWN), PA_ERR_INVALID);
639 PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID);
640
641 if (!server)
642 server = c->conf->default_server;
643
644 pa_context_ref(c);
645
646 assert(!c->server_list);
647
648 if (server) {
649 if (!(c->server_list = pa_strlist_parse(server))) {
650 pa_context_fail(c, PA_ERR_INVALIDSERVER);
651 goto finish;
652 }
653 } else {
654 char *d;
655 char ufn[PATH_MAX];
656
657 /* Prepend in reverse order */
658
659 if ((d = getenv("DISPLAY"))) {
660 char *e;
661 d = pa_xstrdup(d);
662 if ((e = strchr(d, ':')))
663 *e = 0;
664
665 if (*d)
666 c->server_list = pa_strlist_prepend(c->server_list, d);
667
668 pa_xfree(d);
669 }
670
671 c->server_list = pa_strlist_prepend(c->server_list, "tcp6:localhost");
672 c->server_list = pa_strlist_prepend(c->server_list, "localhost");
673 c->server_list = pa_strlist_prepend(c->server_list, pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET, ufn, sizeof(ufn)));
674
675 /* Wrap the connection attempts in a single transaction for sane autospawn locking */
676 if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
677 char lf[PATH_MAX];
678
679 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
680 pa_make_secure_parent_dir(lf);
681 assert(c->autospawn_lock_fd <= 0);
682 c->autospawn_lock_fd = pa_lock_lockfile(lf);
683
684 if (api)
685 c->spawn_api = *api;
686 c->do_autospawn = 1;
687 }
688
689 }
690
691 pa_context_set_state(c, PA_CONTEXT_CONNECTING);
692 r = try_next_connection(c);
693
694 finish:
695 pa_context_unref(c);
696
697 return r;
698 }
699
700 void pa_context_disconnect(pa_context *c) {
701 assert(c);
702 assert(c->ref >= 1);
703
704 pa_context_set_state(c, PA_CONTEXT_TERMINATED);
705 }
706
707 pa_context_state_t pa_context_get_state(pa_context *c) {
708 assert(c);
709 assert(c->ref >= 1);
710
711 return c->state;
712 }
713
714 int pa_context_errno(pa_context *c) {
715 assert(c);
716 assert(c->ref >= 1);
717
718 return c->error;
719 }
720
721 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
722 assert(c);
723 assert(c->ref >= 1);
724
725 c->state_callback = cb;
726 c->state_userdata = userdata;
727 }
728
729 int pa_context_is_pending(pa_context *c) {
730 assert(c);
731 assert(c->ref >= 1);
732
733 PA_CHECK_VALIDITY(c,
734 c->state == PA_CONTEXT_CONNECTING ||
735 c->state == PA_CONTEXT_AUTHORIZING ||
736 c->state == PA_CONTEXT_SETTING_NAME ||
737 c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
738
739 return (c->pstream && pa_pstream_is_pending(c->pstream)) ||
740 (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) ||
741 c->client;
742 }
743
744 static void set_dispatch_callbacks(pa_operation *o);
745
746 static void pdispatch_drain_callback(PA_GCC_UNUSED pa_pdispatch*pd, void *userdata) {
747 set_dispatch_callbacks(userdata);
748 }
749
750 static void pstream_drain_callback(PA_GCC_UNUSED pa_pstream *s, void *userdata) {
751 set_dispatch_callbacks(userdata);
752 }
753
754 static void set_dispatch_callbacks(pa_operation *o) {
755 int done = 1;
756
757 assert(o);
758 assert(o->ref >= 1);
759 assert(o->context);
760 assert(o->context->ref >= 1);
761 assert(o->context->state == PA_CONTEXT_READY);
762
763 pa_pstream_set_drain_callback(o->context->pstream, NULL, NULL);
764 pa_pdispatch_set_drain_callback(o->context->pdispatch, NULL, NULL);
765
766 if (pa_pdispatch_is_pending(o->context->pdispatch)) {
767 pa_pdispatch_set_drain_callback(o->context->pdispatch, pdispatch_drain_callback, o);
768 done = 0;
769 }
770
771 if (pa_pstream_is_pending(o->context->pstream)) {
772 pa_pstream_set_drain_callback(o->context->pstream, pstream_drain_callback, o);
773 done = 0;
774 }
775
776 if (done) {
777 if (o->callback) {
778 pa_context_notify_cb_t cb = (pa_context_notify_cb_t) o->callback;
779 cb(o->context, o->userdata);
780 }
781
782 pa_operation_done(o);
783 pa_operation_unref(o);
784 }
785 }
786
787 pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
788 pa_operation *o;
789
790 assert(c);
791 assert(c->ref >= 1);
792
793 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
794 PA_CHECK_VALIDITY_RETURN_NULL(c, pa_context_is_pending(c), PA_ERR_BADSTATE);
795
796 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
797 set_dispatch_callbacks(pa_operation_ref(o));
798
799 return o;
800 }
801
802 void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
803 pa_operation *o = userdata;
804 int success = 1;
805
806 assert(pd);
807 assert(o);
808 assert(o->ref >= 1);
809
810 if (!o->context)
811 goto finish;
812
813 if (command != PA_COMMAND_REPLY) {
814 if (pa_context_handle_error(o->context, command, t) < 0)
815 goto finish;
816
817 success = 0;
818 } else if (!pa_tagstruct_eof(t)) {
819 pa_context_fail(o->context, PA_ERR_PROTOCOL);
820 goto finish;
821 }
822
823 if (o->callback) {
824 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
825 cb(o->context, success, o->userdata);
826 }
827
828 finish:
829 pa_operation_done(o);
830 pa_operation_unref(o);
831 }
832
833 pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata) {
834 pa_tagstruct *t;
835 pa_operation *o;
836 uint32_t tag;
837
838 assert(c);
839 assert(c->ref >= 1);
840
841 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
842
843 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
844
845 t = pa_tagstruct_command(c, PA_COMMAND_EXIT, &tag);
846 pa_pstream_send_tagstruct(c->pstream, t);
847 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);
848
849 return o;
850 }
851
852 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) {
853 pa_tagstruct *t;
854 pa_operation *o;
855 uint32_t tag;
856
857 assert(c);
858 assert(c->ref >= 1);
859
860 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
861
862 o = pa_operation_new(c, NULL, cb, userdata);
863
864 t = pa_tagstruct_command(c, command, &tag);
865 pa_pstream_send_tagstruct(c->pstream, t);
866 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, internal_cb, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
867
868 return o;
869 }
870
871 pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
872 pa_tagstruct *t;
873 pa_operation *o;
874 uint32_t tag;
875
876 assert(c);
877 assert(c->ref >= 1);
878
879 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
880
881 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
882
883 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SINK, &tag);
884 pa_tagstruct_puts(t, name);
885 pa_pstream_send_tagstruct(c->pstream, t);
886 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);
887
888 return o;
889 }
890
891 pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
892 pa_tagstruct *t;
893 pa_operation *o;
894 uint32_t tag;
895
896 assert(c);
897 assert(c->ref >= 1);
898
899 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
900
901 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
902
903 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SOURCE, &tag);
904 pa_tagstruct_puts(t, name);
905 pa_pstream_send_tagstruct(c->pstream, t);
906 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);
907
908 return o;
909 }
910
911 int pa_context_is_local(pa_context *c) {
912 assert(c);
913
914 return c->local;
915 }
916
917 pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
918 pa_tagstruct *t;
919 pa_operation *o;
920 uint32_t tag;
921
922 assert(c);
923 assert(c->ref >= 1);
924 assert(name);
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_CLIENT_NAME, &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 const char* pa_get_library_version(void) {
939 return PACKAGE_VERSION;
940 }
941
942 const char* pa_context_get_server(pa_context *c) {
943 assert(c);
944 assert(c->ref >= 1);
945
946 if (!c->server)
947 return NULL;
948
949 if (*c->server == '{') {
950 char *e = strchr(c->server+1, '}');
951 return e ? e+1 : c->server;
952 }
953
954 return c->server;
955 }
956
957 uint32_t pa_context_get_protocol_version(PA_GCC_UNUSED pa_context *c) {
958 return PA_PROTOCOL_VERSION;
959 }
960
961 uint32_t pa_context_get_server_protocol_version(pa_context *c) {
962 assert(c);
963 assert(c->ref >= 1);
964
965 return c->version;
966 }
967
968 pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *tag) {
969 pa_tagstruct *t;
970
971 assert(c);
972 assert(tag);
973
974 t = pa_tagstruct_new(NULL, 0);
975 pa_tagstruct_putu32(t, command);
976 pa_tagstruct_putu32(t, *tag = c->ctag++);
977
978 return t;
979 }