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