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