4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <sys/types.h>
39 #ifdef HAVE_SYS_WAIT_H
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
53 #include "../pulsecore/winsock.h"
55 #include <pulsecore/core-error.h>
56 #include <pulse/version.h>
57 #include <pulse/xmalloc.h>
59 #include <pulsecore/native-common.h>
60 #include <pulsecore/pdispatch.h>
61 #include <pulsecore/pstream.h>
62 #include <pulsecore/dynarray.h>
63 #include <pulsecore/socket-client.h>
64 #include <pulsecore/pstream-util.h>
65 #include <pulsecore/core-util.h>
66 #include <pulsecore/log.h>
67 #include <pulsecore/socket-util.h>
68 #include <pulsecore/creds.h>
69 #include <pulsecore/macro.h>
73 #include "client-conf.h"
76 #include "client-conf-x11.h"
81 #define AUTOSPAWN_LOCK "autospawn.lock"
83 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
84 [PA_COMMAND_REQUEST] = pa_command_request,
85 [PA_COMMAND_OVERFLOW] = pa_command_overflow_or_underflow,
86 [PA_COMMAND_UNDERFLOW] = pa_command_overflow_or_underflow,
87 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = pa_command_stream_killed,
88 [PA_COMMAND_RECORD_STREAM_KILLED] = pa_command_stream_killed,
89 [PA_COMMAND_SUBSCRIBE_EVENT] = pa_command_subscribe_event
92 static void unlock_autospawn_lock_file(pa_context *c) {
95 if (c->autospawn_lock_fd >= 0) {
97 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
99 pa_unlock_lockfile(lf, c->autospawn_lock_fd);
100 c->autospawn_lock_fd = -1;
104 static void context_free(pa_context *c);
106 pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name) {
112 c = pa_xnew(pa_context, 1);
114 c->name = pa_xstrdup(name);
115 c->mainloop = mainloop;
119 c->playback_streams = pa_dynarray_new();
120 c->record_streams = pa_dynarray_new();
122 PA_LLIST_HEAD_INIT(pa_stream, c->streams);
123 PA_LLIST_HEAD_INIT(pa_operation, c->operations);
126 c->state = PA_CONTEXT_UNCONNECTED;
130 c->state_callback = NULL;
131 c->state_userdata = NULL;
133 c->subscribe_callback = NULL;
134 c->subscribe_userdata = NULL;
137 c->server_list = NULL;
139 c->autospawn_lock_fd = -1;
140 memset(&c->spawn_api, 0, sizeof(c->spawn_api));
145 pa_check_signal_is_blocked(SIGPIPE);
149 c->conf = pa_client_conf_new();
150 pa_client_conf_load(c->conf, NULL);
152 pa_client_conf_from_x11(c->conf, NULL);
154 pa_client_conf_env(c->conf);
156 if (!(c->mempool = pa_mempool_new(!c->conf->disable_shm))) {
158 if (!c->conf->disable_shm)
159 c->mempool = pa_mempool_new(0);
170 static void context_free(pa_context *c) {
173 unlock_autospawn_lock_file(c);
175 while (c->operations)
176 pa_operation_cancel(c->operations);
179 pa_stream_set_state(c->streams, PA_STREAM_TERMINATED);
182 pa_socket_client_unref(c->client);
184 pa_pdispatch_unref(c->pdispatch);
186 pa_pstream_unlink(c->pstream);
187 pa_pstream_unref(c->pstream);
190 if (c->record_streams)
191 pa_dynarray_free(c->record_streams, NULL, NULL);
192 if (c->playback_streams)
193 pa_dynarray_free(c->playback_streams, NULL, NULL);
196 pa_mempool_free(c->mempool);
199 pa_client_conf_free(c->conf);
201 pa_strlist_free(c->server_list);
208 pa_context* pa_context_ref(pa_context *c) {
210 pa_assert(PA_REFCNT_VALUE(c) >= 1);
216 void pa_context_unref(pa_context *c) {
218 pa_assert(PA_REFCNT_VALUE(c) >= 1);
220 if (PA_REFCNT_DEC(c) <= 0)
224 void pa_context_set_state(pa_context *c, pa_context_state_t st) {
226 pa_assert(PA_REFCNT_VALUE(c) >= 1);
234 if (c->state_callback)
235 c->state_callback(c, c->state_userdata);
237 if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED) {
240 s = c->streams ? pa_stream_ref(c->streams) : NULL;
242 pa_stream *n = s->next ? pa_stream_ref(s->next) : NULL;
243 pa_stream_set_state(s, st == PA_CONTEXT_FAILED ? PA_STREAM_FAILED : PA_STREAM_TERMINATED);
249 pa_pdispatch_unref(c->pdispatch);
253 pa_pstream_unlink(c->pstream);
254 pa_pstream_unref(c->pstream);
259 pa_socket_client_unref(c->client);
266 void pa_context_fail(pa_context *c, int error) {
268 pa_assert(PA_REFCNT_VALUE(c) >= 1);
270 pa_context_set_error(c, error);
271 pa_context_set_state(c, PA_CONTEXT_FAILED);
274 int pa_context_set_error(pa_context *c, int error) {
275 pa_assert(error >= 0);
276 pa_assert(error < PA_ERR_MAX);
284 static void pstream_die_callback(pa_pstream *p, void *userdata) {
285 pa_context *c = userdata;
290 pa_context_fail(c, PA_ERR_CONNECTIONTERMINATED);
293 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
294 pa_context *c = userdata;
302 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0)
303 pa_context_fail(c, PA_ERR_PROTOCOL);
308 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) {
309 pa_context *c = userdata;
314 pa_assert(chunk->memblock);
315 pa_assert(chunk->length);
317 pa_assert(PA_REFCNT_VALUE(c) >= 1);
321 if ((s = pa_dynarray_get(c->record_streams, channel))) {
323 pa_assert(seek == PA_SEEK_RELATIVE);
324 pa_assert(offset == 0);
326 pa_memblockq_seek(s->record_memblockq, offset, seek);
327 pa_memblockq_push_align(s->record_memblockq, chunk);
329 if (s->read_callback) {
332 if ((l = pa_memblockq_get_length(s->record_memblockq)) > 0)
333 s->read_callback(s, l, s->read_userdata);
340 int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t) {
342 pa_assert(PA_REFCNT_VALUE(c) >= 1);
344 if (command == PA_COMMAND_ERROR) {
347 if (pa_tagstruct_getu32(t, &c->error) < 0) {
348 pa_context_fail(c, PA_ERR_PROTOCOL);
352 } else if (command == PA_COMMAND_TIMEOUT)
353 c->error = PA_ERR_TIMEOUT;
355 pa_context_fail(c, PA_ERR_PROTOCOL);
362 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
363 pa_context *c = userdata;
367 pa_assert(c->state == PA_CONTEXT_AUTHORIZING || c->state == PA_CONTEXT_SETTING_NAME);
371 if (command != PA_COMMAND_REPLY) {
373 if (pa_context_handle_error(c, command, t) < 0)
374 pa_context_fail(c, PA_ERR_PROTOCOL);
376 pa_context_fail(c, c->error);
381 case PA_CONTEXT_AUTHORIZING: {
384 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
385 !pa_tagstruct_eof(t)) {
386 pa_context_fail(c, PA_ERR_PROTOCOL);
390 /* Minimum supported version */
391 if (c->version < 8) {
392 pa_context_fail(c, PA_ERR_VERSION);
396 /* Enable shared memory support if possible */
397 if (c->version >= 10 &&
398 pa_mempool_is_shared(c->mempool) &&
401 /* Only enable SHM if both sides are owned by the same
402 * user. This is a security measure because otherwise
403 * data private to the user might leak. */
406 const pa_creds *creds;
407 if ((creds = pa_pdispatch_creds(pd)))
408 if (getuid() == creds->uid)
409 pa_pstream_use_shm(c->pstream, 1);
413 reply = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
414 pa_tagstruct_puts(reply, c->name);
415 pa_pstream_send_tagstruct(c->pstream, reply);
416 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
418 pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);
422 case PA_CONTEXT_SETTING_NAME :
423 pa_context_set_state(c, PA_CONTEXT_READY);
434 static void setup_context(pa_context *c, pa_iochannel *io) {
443 pa_assert(!c->pstream);
444 c->pstream = pa_pstream_new(c->mainloop, io, c->mempool);
446 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
447 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
448 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
450 pa_assert(!c->pdispatch);
451 c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
453 if (!c->conf->cookie_valid)
454 pa_log_warn("No cookie loaded. Attempting to connect without.");
456 t = pa_tagstruct_command(c, PA_COMMAND_AUTH, &tag);
457 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
458 pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
464 if (pa_iochannel_creds_supported(io))
465 pa_iochannel_creds_enable(io);
467 ucred.uid = getuid();
468 ucred.gid = getgid();
470 pa_pstream_send_tagstruct_with_creds(c->pstream, t, &ucred);
473 pa_pstream_send_tagstruct(c->pstream, t);
476 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
478 pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);
483 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata);
487 static int context_connect_spawn(pa_context *c) {
490 int fds[2] = { -1, -1} ;
495 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
496 pa_log("socketpair(): %s", pa_cstrerror(errno));
497 pa_context_fail(c, PA_ERR_INTERNAL);
501 pa_fd_set_cloexec(fds[0], 1);
503 pa_socket_low_delay(fds[0]);
504 pa_socket_low_delay(fds[1]);
506 if (c->spawn_api.prefork)
507 c->spawn_api.prefork();
509 if ((pid = fork()) < 0) {
510 pa_log("fork(): %s", pa_cstrerror(errno));
511 pa_context_fail(c, PA_ERR_INTERNAL);
513 if (c->spawn_api.postfork)
514 c->spawn_api.postfork();
521 const char *state = NULL;
523 const char * argv[MAX_ARGS+1];
526 /* Not required, since fds[0] has CLOEXEC enabled anyway */
529 if (c->spawn_api.atfork)
530 c->spawn_api.atfork();
536 argv[n++] = c->conf->daemon_binary;
537 argv[n++] = "--daemonize=yes";
539 pa_snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
540 argv[n++] = strdup(t);
542 while (n < MAX_ARGS) {
545 if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
553 execv(argv[0], (char * const *) argv);
560 r = waitpid(pid, &status, 0);
562 if (c->spawn_api.postfork)
563 c->spawn_api.postfork();
566 pa_log("waitpid(): %s", pa_cstrerror(errno));
567 pa_context_fail(c, PA_ERR_INTERNAL);
569 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
570 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
578 io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
580 setup_context(c, io);
581 unlock_autospawn_lock_file(c);
593 unlock_autospawn_lock_file(c);
600 #endif /* OS_IS_WIN32 */
602 static int try_next_connection(pa_context *c) {
607 pa_assert(!c->client);
613 c->server_list = pa_strlist_pop(c->server_list, &u);
618 if (c->do_autospawn) {
619 r = context_connect_spawn(c);
624 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
628 pa_log_debug("Trying to connect to %s...", u);
631 c->server = pa_xstrdup(u);
633 if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
636 c->is_local = pa_socket_client_is_local(c->client);
637 pa_socket_client_set_callback(c->client, on_connection, c);
649 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata) {
650 pa_context *c = userdata;
654 pa_assert(c->state == PA_CONTEXT_CONNECTING);
658 pa_socket_client_unref(client);
662 /* Try the item in the list */
663 if (errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH) {
664 try_next_connection(c);
668 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
672 unlock_autospawn_lock_file(c);
673 setup_context(c, io);
679 int pa_context_connect(
682 pa_context_flags_t flags,
683 const pa_spawn_api *api) {
688 pa_assert(PA_REFCNT_VALUE(c) >= 1);
690 PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
691 PA_CHECK_VALIDITY(c, !(flags & ~PA_CONTEXT_NOAUTOSPAWN), PA_ERR_INVALID);
692 PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID);
695 server = c->conf->default_server;
699 pa_assert(!c->server_list);
702 if (!(c->server_list = pa_strlist_parse(server))) {
703 pa_context_fail(c, PA_ERR_INVALIDSERVER);
710 /* Prepend in reverse order */
712 if ((d = getenv("DISPLAY"))) {
715 if ((e = strchr(d, ':')))
719 c->server_list = pa_strlist_prepend(c->server_list, d);
724 c->server_list = pa_strlist_prepend(c->server_list, "tcp6:localhost");
725 c->server_list = pa_strlist_prepend(c->server_list, "tcp4:localhost");
727 /* The system wide instance */
728 c->server_list = pa_strlist_prepend(c->server_list, PA_SYSTEM_RUNTIME_PATH "/" PA_NATIVE_DEFAULT_UNIX_SOCKET);
730 /* The per-user instance */
731 c->server_list = pa_strlist_prepend(c->server_list, pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET, ufn, sizeof(ufn)));
733 /* Wrap the connection attempts in a single transaction for sane autospawn locking */
734 if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
737 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
738 pa_make_secure_parent_dir(lf, 0700, (uid_t)-1, (gid_t)-1);
739 pa_assert(c->autospawn_lock_fd <= 0);
740 c->autospawn_lock_fd = pa_lock_lockfile(lf);
749 pa_context_set_state(c, PA_CONTEXT_CONNECTING);
750 r = try_next_connection(c);
758 void pa_context_disconnect(pa_context *c) {
760 pa_assert(PA_REFCNT_VALUE(c) >= 1);
762 pa_context_set_state(c, PA_CONTEXT_TERMINATED);
765 pa_context_state_t pa_context_get_state(pa_context *c) {
767 pa_assert(PA_REFCNT_VALUE(c) >= 1);
772 int pa_context_errno(pa_context *c) {
774 pa_assert(PA_REFCNT_VALUE(c) >= 1);
779 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
781 pa_assert(PA_REFCNT_VALUE(c) >= 1);
783 c->state_callback = cb;
784 c->state_userdata = userdata;
787 int pa_context_is_pending(pa_context *c) {
789 pa_assert(PA_REFCNT_VALUE(c) >= 1);
792 c->state == PA_CONTEXT_CONNECTING ||
793 c->state == PA_CONTEXT_AUTHORIZING ||
794 c->state == PA_CONTEXT_SETTING_NAME ||
795 c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
797 return (c->pstream && pa_pstream_is_pending(c->pstream)) ||
798 (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) ||
802 static void set_dispatch_callbacks(pa_operation *o);
804 static void pdispatch_drain_callback(PA_GCC_UNUSED pa_pdispatch*pd, void *userdata) {
805 set_dispatch_callbacks(userdata);
808 static void pstream_drain_callback(PA_GCC_UNUSED pa_pstream *s, void *userdata) {
809 set_dispatch_callbacks(userdata);
812 static void set_dispatch_callbacks(pa_operation *o) {
816 pa_assert(PA_REFCNT_VALUE(o) >= 1);
817 pa_assert(o->context);
818 pa_assert(PA_REFCNT_VALUE(o->context) >= 1);
819 pa_assert(o->context->state == PA_CONTEXT_READY);
821 pa_pstream_set_drain_callback(o->context->pstream, NULL, NULL);
822 pa_pdispatch_set_drain_callback(o->context->pdispatch, NULL, NULL);
824 if (pa_pdispatch_is_pending(o->context->pdispatch)) {
825 pa_pdispatch_set_drain_callback(o->context->pdispatch, pdispatch_drain_callback, o);
829 if (pa_pstream_is_pending(o->context->pstream)) {
830 pa_pstream_set_drain_callback(o->context->pstream, pstream_drain_callback, o);
836 pa_context_notify_cb_t cb = (pa_context_notify_cb_t) o->callback;
837 cb(o->context, o->userdata);
840 pa_operation_done(o);
841 pa_operation_unref(o);
845 pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
849 pa_assert(PA_REFCNT_VALUE(c) >= 1);
851 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
852 PA_CHECK_VALIDITY_RETURN_NULL(c, pa_context_is_pending(c), PA_ERR_BADSTATE);
854 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
855 set_dispatch_callbacks(pa_operation_ref(o));
860 void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
861 pa_operation *o = userdata;
866 pa_assert(PA_REFCNT_VALUE(o) >= 1);
871 if (command != PA_COMMAND_REPLY) {
872 if (pa_context_handle_error(o->context, command, t) < 0)
876 } else if (!pa_tagstruct_eof(t)) {
877 pa_context_fail(o->context, PA_ERR_PROTOCOL);
882 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
883 cb(o->context, success, o->userdata);
887 pa_operation_done(o);
888 pa_operation_unref(o);
891 pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata) {
897 pa_assert(PA_REFCNT_VALUE(c) >= 1);
899 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
901 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
903 t = pa_tagstruct_command(c, PA_COMMAND_EXIT, &tag);
904 pa_pstream_send_tagstruct(c->pstream, t);
905 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);
910 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) {
916 pa_assert(PA_REFCNT_VALUE(c) >= 1);
918 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
920 o = pa_operation_new(c, NULL, cb, userdata);
922 t = pa_tagstruct_command(c, command, &tag);
923 pa_pstream_send_tagstruct(c->pstream, t);
924 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, internal_cb, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
929 pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
935 pa_assert(PA_REFCNT_VALUE(c) >= 1);
937 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
939 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
941 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SINK, &tag);
942 pa_tagstruct_puts(t, name);
943 pa_pstream_send_tagstruct(c->pstream, t);
944 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);
949 pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
955 pa_assert(PA_REFCNT_VALUE(c) >= 1);
957 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
959 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
961 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SOURCE, &tag);
962 pa_tagstruct_puts(t, name);
963 pa_pstream_send_tagstruct(c->pstream, t);
964 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);
969 int pa_context_is_local(pa_context *c) {
975 pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
981 pa_assert(PA_REFCNT_VALUE(c) >= 1);
984 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
986 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
988 t = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
989 pa_tagstruct_puts(t, name);
990 pa_pstream_send_tagstruct(c->pstream, t);
991 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);
996 const char* pa_get_library_version(void) {
997 return PACKAGE_VERSION;
1000 const char* pa_context_get_server(pa_context *c) {
1002 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1007 if (*c->server == '{') {
1008 char *e = strchr(c->server+1, '}');
1009 return e ? e+1 : c->server;
1015 uint32_t pa_context_get_protocol_version(PA_GCC_UNUSED pa_context *c) {
1016 return PA_PROTOCOL_VERSION;
1019 uint32_t pa_context_get_server_protocol_version(pa_context *c) {
1021 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1026 pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *tag) {
1032 t = pa_tagstruct_new(NULL, 0);
1033 pa_tagstruct_putu32(t, command);
1034 pa_tagstruct_putu32(t, *tag = c->ctag++);