summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Makefile.am19
-rw-r--r--src/modules/module-zeroconf-publish.c426
-rw-r--r--src/pulsecore/avahi-wrap.c188
-rw-r--r--src/pulsecore/avahi-wrap.h33
4 files changed, 537 insertions, 129 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 2bd3b871..8c8168d5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -667,6 +667,14 @@ modlibexec_LTLIBRARIES += \
libx11prop.la
endif
+if HAVE_AVAHI
+pulsecoreinclude_HEADERS += \
+ pulsecore/avahi-wrap.h
+
+modlibexec_LTLIBRARIES += \
+ libavahi-wrap.la
+endif
+
libprotocol_simple_la_SOURCES = pulsecore/protocol-simple.c pulsecore/protocol-simple.h
libprotocol_simple_la_LDFLAGS = -avoid-version
libprotocol_simple_la_LIBADD = $(AM_LIBADD) libpulsecore.la libsocket-server.la libiochannel.la
@@ -770,6 +778,13 @@ libx11prop_la_LDFLAGS = -avoid-version
libx11prop_la_CFLAGS = $(AM_CFLAGS) $(X_CFLAGS)
libx11prop_la_LIBADD = $(AM_LIBADD) $(X_PRE_LIBS) -lX11 $(X_LIBS) $(X_EXTRA_LIBS)
+# Avahi
+
+libavahi_wrap_la_SOURCES = pulsecore/avahi-wrap.c pulsecore/avahi-wrap.h
+libavahi_wrap_la_LDFLAGS = -avoid-version
+libavahi_wrap_la_CFLAGS = $(AM_CFLAGS) $(AVAHI_CFLAGS)
+libavahi_wrap_la_LIBADD = $(AM_LIBADD) $(AVAHI_CFLAGS) libpulsecore.la
+
###################################
# Plug-in libraries #
###################################
@@ -1105,8 +1120,8 @@ libhowl_wrap_la_CFLAGS = $(AM_CFLAGS) $(HOWL_CFLAGS)
module_zeroconf_publish_la_SOURCES = modules/module-zeroconf-publish.c
module_zeroconf_publish_la_LDFLAGS = -module -avoid-version
-module_zeroconf_publish_la_LIBADD = $(AM_LIBADD) $(HOWL_LIBS) libhowl-wrap.la libpulsecore.la
-module_zeroconf_publish_la_CFLAGS = $(AM_CFLAGS) $(HOWL_CFLAGS)
+module_zeroconf_publish_la_LIBADD = $(AM_LIBADD) $(AVAHI_LIBS) libavahi-wrap.la libpulsecore.la
+module_zeroconf_publish_la_CFLAGS = $(AM_CFLAGS) $(AVAHI_CFLAGS)
# LIRC
diff --git a/src/modules/module-zeroconf-publish.c b/src/modules/module-zeroconf-publish.c
index 32fb1f41..23a188b3 100644
--- a/src/modules/module-zeroconf-publish.c
+++ b/src/modules/module-zeroconf-publish.c
@@ -29,6 +29,11 @@
#include <string.h>
#include <unistd.h>
+#include <avahi-client/client.h>
+#include <avahi-client/publish.h>
+#include <avahi-common/alternative.h>
+#include <avahi-common/error.h>
+
#include <pulse/xmalloc.h>
#include <pulse/util.h>
@@ -41,10 +46,8 @@
#include <pulsecore/core-subscribe.h>
#include <pulsecore/dynarray.h>
#include <pulsecore/modargs.h>
-
-#include "../pulsecore/endianmacros.h"
-
-#include "howl-wrap.h"
+#include <pulsecore/avahi-wrap.h>
+#include <pulsecore/endianmacros.h>
#include "module-zeroconf-publish-symdef.h"
@@ -53,9 +56,9 @@ PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
PA_MODULE_VERSION(PACKAGE_VERSION)
PA_MODULE_USAGE("port=<IP port number>")
-#define SERVICE_NAME_SINK "_pulse-sink._tcp"
-#define SERVICE_NAME_SOURCE "_pulse-source._tcp"
-#define SERVICE_NAME_SERVER "_pulse-server._tcp"
+#define SERVICE_TYPE_SINK "_pulse-sink._tcp"
+#define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
+#define SERVICE_TYPE_SERVER "_pulse-server._tcp"
static const char* const valid_modargs[] = {
"port",
@@ -63,9 +66,11 @@ static const char* const valid_modargs[] = {
};
struct service {
- sw_discovery_oid oid;
+ struct userdata *userdata;
+ AvahiEntryGroup *entry_group;
+ char *service_name;
char *name;
- int published; /* 0 -> not yet registered, 1 -> registered with data from real device, 2 -> registered with data from autoload device */
+ enum { UNPUBLISHED, PUBLISHED_REAL, PUBLISHED_AUTOLOAD } published ;
struct {
int valid;
@@ -82,19 +87,18 @@ struct service {
struct userdata {
pa_core *core;
- pa_howl_wrapper *howl_wrapper;
+ AvahiPoll *avahi_poll;
+ AvahiClient *client;
pa_hashmap *services;
pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
pa_subscription *subscription;
+ char *service_name;
+
+ AvahiEntryGroup *main_entry_group;
uint16_t port;
- sw_discovery_oid server_oid;
};
-static sw_result publish_reply(sw_discovery discovery, sw_discovery_publish_status status, sw_discovery_oid oid, sw_opaque extra) {
- return SW_OKAY;
-}
-
static void get_service_data(struct userdata *u, struct service *s, pa_sample_spec *ret_ss, char **ret_description) {
assert(u && s && s->loaded.valid && ret_ss && ret_description);
@@ -112,109 +116,144 @@ static void get_service_data(struct userdata *u, struct service *s, pa_sample_sp
assert(0);
}
-static void txt_record_server_data(pa_core *c, sw_text_record t) {
- char s[256];
+static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
+ char s[128];
assert(c);
- sw_text_record_add_key_and_string_value(t, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
- sw_text_record_add_key_and_string_value(t, "user-name", pa_get_user_name(s, sizeof(s)));
- sw_text_record_add_key_and_string_value(t, "fqdn", pa_get_fqdn(s, sizeof(s)));
- snprintf(s, sizeof(s), "0x%08x", c->cookie);
- sw_text_record_add_key_and_string_value(t, "cookie", s);
+ l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
+ l = avahi_string_list_add_pair(l, "user-name", pa_get_user_name(s, sizeof(s)));
+ l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
+ l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
+
+ return l;
}
-static int publish_service(struct userdata *u, struct service *s) {
- char t[256];
- char hn[256];
- int r = -1;
- sw_text_record txt;
- int free_txt = 0;
- assert(u && s);
-
- if ((s->published == 1 && s->loaded.valid) ||
- (s->published == 2 && s->autoload.valid && !s->loaded.valid))
- return 0;
+static int publish_service(struct userdata *u, struct service *s);
- if (s->published) {
- sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
- s->published = 0;
- }
+static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
+ struct service *s = userdata;
- snprintf(t, sizeof(t), "%s on %s", s->name, pa_get_host_name(hn, sizeof(hn)));
+ if (state == AVAHI_ENTRY_GROUP_COLLISION) {
+ char *t;
- if (sw_text_record_init(&txt) != SW_OKAY) {
- pa_log(__FILE__": sw_text_record_init() failed");
- goto finish;
+ t = avahi_alternative_service_name(s->service_name);
+ pa_xfree(s->service_name);
+ s->service_name = t;
+
+ publish_service(s->userdata, s);
}
- free_txt = 1;
+}
+
+static int publish_service(struct userdata *u, struct service *s) {
+ int r = -1;
+ AvahiStringList *txt = NULL;
+
+ assert(u);
+ assert(s);
- sw_text_record_add_key_and_string_value(txt, "device", s->name);
+ if (!u->client || avahi_client_get_state(u->client) != AVAHI_CLIENT_S_RUNNING)
+ return 0;
+
+ if ((s->published == PUBLISHED_REAL && s->loaded.valid) ||
+ (s->published == PUBLISHED_AUTOLOAD && s->autoload.valid && !s->loaded.valid))
+ return 0;
- txt_record_server_data(u->core, txt);
+ if (s->published != UNPUBLISHED) {
+ avahi_entry_group_reset(s->entry_group);
+ s->published = UNPUBLISHED;
+ }
- if (s->loaded.valid) {
- char z[64], *description;
- pa_sample_spec ss;
+ if (s->loaded.valid || s->autoload.valid) {
+ pa_namereg_type_t type;
- get_service_data(u, s, &ss, &description);
+ if (!s->entry_group) {
+ if (!(s->entry_group = avahi_entry_group_new(u->client, service_entry_group_callback, s))) {
+ pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(u->client)));
+ goto finish;
+ }
+ }
+
+ txt = avahi_string_list_add_pair(txt, "device", s->name);
+ txt = txt_record_server_data(u->core, txt);
+
+ if (s->loaded.valid) {
+ char *description;
+ pa_sample_spec ss;
+
+ get_service_data(u, s, &ss, &description);
+
+ txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
+ txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
+ txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
+ txt = avahi_string_list_add_pair(txt, "description", description);
- snprintf(z, sizeof(z), "%u", ss.rate);
- sw_text_record_add_key_and_string_value(txt, "rate", z);
- snprintf(z, sizeof(z), "%u", ss.channels);
- sw_text_record_add_key_and_string_value(txt, "channels", z);
- sw_text_record_add_key_and_string_value(txt, "format", pa_sample_format_to_string(ss.format));
-
- sw_text_record_add_key_and_string_value(txt, "description", description);
-
- if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
- s->loaded.type == PA_NAMEREG_SINK ? SERVICE_NAME_SINK : SERVICE_NAME_SOURCE,
- NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
- publish_reply, s, &s->oid) != SW_OKAY) {
- pa_log(__FILE__": failed to register sink on zeroconf.");
+ type = s->loaded.type;
+ } else if (s->autoload.valid)
+ type = s->autoload.type;
+
+ if (avahi_entry_group_add_service_strlst(
+ s->entry_group,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ 0,
+ s->service_name,
+ type == PA_NAMEREG_SINK ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
+ NULL,
+ NULL,
+ u->port,
+ txt) < 0) {
+
+ pa_log(__FILE__": avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(u->client)));
goto finish;
}
-
- s->published = 1;
- } else if (s->autoload.valid) {
-
- if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
- s->autoload.type == PA_NAMEREG_SINK ? SERVICE_NAME_SINK : SERVICE_NAME_SOURCE,
- NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
- publish_reply, s, &s->oid) != SW_OKAY) {
- pa_log(__FILE__": failed to register sink on zeroconf.");
+
+ if (avahi_entry_group_commit(s->entry_group) < 0) {
+ pa_log(__FILE__": avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(u->client)));
goto finish;
}
-
- s->published = 2;
+
+ if (s->loaded.valid)
+ s->published = PUBLISHED_REAL;
+ else if (s->autoload.valid)
+ s->published = PUBLISHED_AUTOLOAD;
}
-
+
r = 0;
finish:
- if (!s->published) {
+ if (s->published == UNPUBLISHED) {
/* Remove this service */
+
+ if (s->entry_group) {
+ avahi_entry_group_free(s->entry_group);
+ }
+
pa_hashmap_remove(u->services, s->name);
pa_xfree(s->name);
+ pa_xfree(s->service_name);
pa_xfree(s);
}
- if (free_txt)
- sw_text_record_fina(txt);
+ if (txt)
+ avahi_string_list_free(txt);
return r;
}
static struct service *get_service(struct userdata *u, const char *name) {
struct service *s;
+ char hn[64];
if ((s = pa_hashmap_get(u->services, name)))
return s;
- s = pa_xmalloc(sizeof(struct service));
- s->published = 0;
+ s = pa_xnew(struct service, 1);
+ s->userdata = u;
+ s->entry_group = NULL;
+ s->published = UNPUBLISHED;
s->name = pa_xstrdup(name);
s->loaded.valid = s->autoload.valid = 0;
+ s->service_name = pa_sprintf_malloc("%s on %s", s->name, pa_get_host_name(hn, sizeof(hn)));
pa_hashmap_put(u->services, s->name, s);
@@ -227,7 +266,7 @@ static int publish_sink(struct userdata *u, pa_sink *s) {
svc = get_service(u, s->name);
if (svc->loaded.valid)
- return 0;
+ return publish_service(u, svc);
svc->loaded.valid = 1;
svc->loaded.type = PA_NAMEREG_SINK;
@@ -244,7 +283,7 @@ static int publish_source(struct userdata *u, pa_source *s) {
svc = get_service(u, s->name);
if (svc->loaded.valid)
- return 0;
+ return publish_service(u, svc);
svc->loaded.valid = 1;
svc->loaded.type = PA_NAMEREG_SOURCE;
@@ -261,7 +300,7 @@ static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
svc = get_service(u, s->name);
if (svc->autoload.valid)
- return 0;
+ return publish_service(u, svc);
svc->autoload.valid = 1;
svc->autoload.type = s->type;
@@ -381,16 +420,164 @@ fail:
}
}
-int pa__init(pa_core *c, pa_module*m) {
- struct userdata *u;
- uint32_t idx, port = PA_NATIVE_DEFAULT_PORT;
+static int publish_main_service(struct userdata *u);
+
+static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
+ struct userdata *u = userdata;
+ assert(u);
+
+ if (state == AVAHI_ENTRY_GROUP_COLLISION) {
+ char *t;
+
+ t = avahi_alternative_service_name(u->service_name);
+ pa_xfree(u->service_name);
+ u->service_name = t;
+
+ publish_main_service(u);
+ }
+}
+
+static int publish_main_service(struct userdata *u) {
+ AvahiStringList *txt = NULL;
+ int r = -1;
+
+ if (!u->main_entry_group) {
+ if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
+ pa_log(__FILE__": avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+ goto fail;
+ }
+ } else
+ avahi_entry_group_reset(u->main_entry_group);
+
+ txt = txt_record_server_data(u->core, NULL);
+
+ if (avahi_entry_group_add_service_strlst(
+ u->main_entry_group,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ 0,
+ u->service_name,
+ SERVICE_TYPE_SERVER,
+ NULL,
+ NULL,
+ u->port,
+ txt) < 0) {
+
+ pa_log(__FILE__": avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+ goto fail;
+ }
+
+ if (avahi_entry_group_commit(u->main_entry_group) < 0) {
+ pa_log(__FILE__": avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+ goto fail;
+ }
+
+ r = 0;
+
+fail:
+ avahi_string_list_free(txt);
+
+ return r;
+}
+
+static int publish_all_services(struct userdata *u) {
pa_sink *sink;
pa_source *source;
pa_autoload_entry *autoload;
+ int r = -1;
+ uint32_t idx;
+
+ assert(u);
+
+ pa_log_debug(__FILE__": Publishing services in Zeroconf");
+
+ for (sink = pa_idxset_first(u->core->sinks, &idx); sink; sink = pa_idxset_next(u->core->sinks, &idx))
+ if (publish_sink(u, sink) < 0)
+ goto fail;
+
+ for (source = pa_idxset_first(u->core->sources, &idx); source; source = pa_idxset_next(u->core->sources, &idx))
+ if (publish_source(u, source) < 0)
+ goto fail;
+
+ if (u->core->autoload_idxset)
+ for (autoload = pa_idxset_first(u->core->autoload_idxset, &idx); autoload; autoload = pa_idxset_next(u->core->autoload_idxset, &idx))
+ if (publish_autoload(u, autoload) < 0)
+ goto fail;
+
+ if (publish_main_service(u) < 0)
+ goto fail;
+
+ r = 0;
+
+fail:
+ return r;
+}
+
+static void unpublish_all_services(struct userdata *u, int rem) {
+ void *state = NULL;
+ struct service *s;
+
+ assert(u);
+
+ pa_log_debug(__FILE__": Unpublishing services in Zeroconf");
+
+ while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
+ if (s->entry_group) {
+ if (rem) {
+ avahi_entry_group_free(s->entry_group);
+ s->entry_group = NULL;
+ } else
+ avahi_entry_group_reset(s->entry_group);
+ }
+
+ s->published = UNPUBLISHED;
+ }
+
+ if (u->main_entry_group) {
+ if (rem) {
+ avahi_entry_group_free(u->main_entry_group);
+ u->main_entry_group = NULL;
+ } else
+ avahi_entry_group_reset(u->main_entry_group);
+ }
+}
+
+static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
+ struct userdata *u = userdata;
+ assert(c);
+
+ u->client = c;
+
+ switch (state) {
+ case AVAHI_CLIENT_S_RUNNING:
+ publish_all_services(u);
+ break;
+
+ case AVAHI_CLIENT_S_COLLISION:
+ unpublish_all_services(u, 0);
+ break;
+
+ case AVAHI_CLIENT_FAILURE:
+ if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
+ int error;
+ unpublish_all_services(u, 1);
+ avahi_client_free(u->client);
+
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error)))
+ pa_log(__FILE__": pa_avahi_client_new() failed: %s", avahi_strerror(error));
+ }
+
+ break;
+
+ default: ;
+ }
+}
+
+int pa__init(pa_core *c, pa_module*m) {
+ struct userdata *u;
+ uint32_t port = PA_NATIVE_DEFAULT_PORT;
pa_modargs *ma = NULL;
- char t[256], hn[256];
- int free_txt = 0;
- sw_text_record txt;
+ char hn[256];
+ int error;
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
pa_log(__FILE__": failed to parse module arguments.");
@@ -402,55 +589,31 @@ int pa__init(pa_core *c, pa_module*m) {
goto fail;
}
- m->userdata = u = pa_xmalloc(sizeof(struct userdata));
+ m->userdata = u = pa_xnew(struct userdata, 1);
u->core = c;
u->port = (uint16_t) port;
- if (!(u->howl_wrapper = pa_howl_wrapper_get(c)))
- goto fail;
-
+ u->avahi_poll = pa_avahi_poll_new(c->mainloop);
+
u->services = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
u->sink_dynarray = pa_dynarray_new();
u->source_dynarray = pa_dynarray_new();
u->autoload_dynarray = pa_dynarray_new();
-
+
u->subscription = pa_subscription_new(c,
PA_SUBSCRIPTION_MASK_SINK|
PA_SUBSCRIPTION_MASK_SOURCE|
PA_SUBSCRIPTION_MASK_AUTOLOAD, subscribe_callback, u);
- for (sink = pa_idxset_first(c->sinks, &idx); sink; sink = pa_idxset_next(c->sinks, &idx))
- if (publish_sink(u, sink) < 0)
- goto fail;
+ u->main_entry_group = NULL;
- for (source = pa_idxset_first(c->sources, &idx); source; source = pa_idxset_next(c->sources, &idx))
- if (publish_source(u, source) < 0)
- goto fail;
+ u->service_name = pa_xstrdup(pa_get_host_name(hn, sizeof(hn)));
- if (c->autoload_idxset)
- for (autoload = pa_idxset_first(c->autoload_idxset, &idx); autoload; autoload = pa_idxset_next(c->autoload_idxset, &idx))
- if (publish_autoload(u, autoload) < 0)
- goto fail;
-
- snprintf(t, sizeof(t), "%s", pa_get_host_name(hn, sizeof(hn)));
-
- if (sw_text_record_init(&txt) != SW_OKAY) {
- pa_log(__FILE__": sw_text_record_init() failed");
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
+ pa_log(__FILE__": pa_avahi_client_new() failed: %s", avahi_strerror(error));
goto fail;
}
- free_txt = 1;
- txt_record_server_data(u->core, txt);
-
- if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
- SERVICE_NAME_SERVER,
- NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
- publish_reply, u, &u->server_oid) != SW_OKAY) {
- pa_log(__FILE__": failed to register server on zeroconf.");
- goto fail;
- }
-
- sw_text_record_fina(txt);
pa_modargs_free(ma);
return 0;
@@ -460,9 +623,6 @@ fail:
if (ma)
pa_modargs_free(ma);
-
- if (free_txt)
- sw_text_record_fina(txt);
return -1;
}
@@ -470,8 +630,14 @@ fail:
static void service_free(void *p, void *userdata) {
struct service *s = p;
struct userdata *u = userdata;
- assert(s && u);
- sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
+
+ assert(s);
+ assert(u);
+
+ if (s->entry_group)
+ avahi_entry_group_free(s->entry_group);
+
+ pa_xfree(s->service_name);
pa_xfree(s->name);
pa_xfree(s);
}
@@ -495,11 +661,17 @@ void pa__done(pa_core *c, pa_module*m) {
if (u->subscription)
pa_subscription_free(u->subscription);
-
- if (u->howl_wrapper)
- pa_howl_wrapper_unref(u->howl_wrapper);
+ if (u->main_entry_group)
+ avahi_entry_group_free(u->main_entry_group);
+ if (u->client)
+ avahi_client_free(u->client);
+
+ if (u->avahi_poll)
+ pa_avahi_poll_free(u->avahi_poll);
+
+ pa_xfree(u->service_name);
pa_xfree(u);
}
diff --git a/src/pulsecore/avahi-wrap.c b/src/pulsecore/avahi-wrap.c
new file mode 100644
index 00000000..9da76558
--- /dev/null
+++ b/src/pulsecore/avahi-wrap.c
@@ -0,0 +1,188 @@
+/* $Id$ */
+
+/***
+ This file is part of PulseAudio.
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <assert.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/log.h>
+
+#include "avahi-wrap.h"
+
+typedef struct {
+ AvahiPoll api;
+ pa_mainloop_api *mainloop;
+} pa_avahi_poll;
+
+struct AvahiWatch {
+ pa_io_event *io_event;
+ pa_avahi_poll *avahi_poll;
+ AvahiWatchEvent current_event;
+ AvahiWatchCallback callback;
+ void *userdata;
+};
+
+static AvahiWatchEvent translate_io_flags_back(pa_io_event_flags_t e) {
+ return
+ (e & PA_IO_EVENT_INPUT ? AVAHI_WATCH_IN : 0) |
+ (e & PA_IO_EVENT_OUTPUT ? AVAHI_WATCH_OUT : 0) |
+ (e & PA_IO_EVENT_ERROR ? AVAHI_WATCH_ERR : 0) |
+ (e & PA_IO_EVENT_HANGUP ? AVAHI_WATCH_HUP : 0);
+}
+
+static pa_io_event_flags_t translate_io_flags(AvahiWatchEvent e) {
+ return
+ (e & AVAHI_WATCH_IN ? PA_IO_EVENT_INPUT : 0) |
+ (e & AVAHI_WATCH_OUT ? PA_IO_EVENT_OUTPUT : 0) |
+ (e & AVAHI_WATCH_ERR ? PA_IO_EVENT_ERROR : 0) |
+ (e & AVAHI_WATCH_HUP ? PA_IO_EVENT_HANGUP : 0);
+}
+
+static void watch_callback(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
+ AvahiWatch *w = userdata;
+
+ assert(a);
+ assert(e);
+ assert(w);
+
+ w->current_event = translate_io_flags_back(events);
+ w->callback(w, fd, w->current_event, w->userdata);
+ w->current_event = 0;
+}
+
+static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
+ pa_avahi_poll *p;
+ AvahiWatch *w;
+
+ assert(api);
+ assert(fd >= 0);
+ assert(callback);
+
+ p = api->userdata;
+ assert(p);
+
+ w = pa_xnew(AvahiWatch, 1);
+ w->avahi_poll = p;
+ w->current_event = 0;
+ w->callback = callback;
+ w->userdata = userdata;
+ w->io_event = p->mainloop->io_new(p->mainloop, fd, translate_io_flags(event), watch_callback, w);
+
+ return w;
+}
+
+static void watch_update(AvahiWatch *w, AvahiWatchEvent event) {
+ assert(w);
+
+ w->avahi_poll->mainloop->io_enable(w->io_event, translate_io_flags(event));
+}
+
+static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
+ assert(w);
+
+ return w->current_event;
+}
+
+static void watch_free(AvahiWatch *w) {
+ assert(w);
+
+ w->avahi_poll->mainloop->io_free(w->io_event);
+ pa_xfree(w);
+}
+
+struct AvahiTimeout {
+ pa_time_event *time_event;
+ pa_avahi_poll *avahi_poll;
+ AvahiTimeoutCallback callback;
+ void *userdata;
+};
+
+static void timeout_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
+ AvahiTimeout *t = userdata;
+
+ assert(a);
+ assert(e);
+ assert(t);
+
+ t->callback(t, t->userdata);
+}
+
+static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
+ pa_avahi_poll *p;
+ AvahiTimeout *t;
+
+ assert(api);
+ assert(callback);
+
+ p = api->userdata;
+ assert(p);
+
+ t = pa_xnew(AvahiTimeout, 1);
+ t->avahi_poll = p;
+ t->callback = callback;
+ t->userdata = userdata;
+ t->time_event = p->mainloop->time_new(p->mainloop, tv, timeout_callback, t);
+
+ return t;
+}
+
+static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
+ assert(t);
+
+ t->avahi_poll->mainloop->time_restart(t->time_event, tv);
+}
+
+static void timeout_free(AvahiTimeout *t) {
+ assert(t);
+
+ t->avahi_poll->mainloop->time_free(t->time_event);
+ pa_xfree(t);
+}
+
+AvahiPoll* pa_avahi_poll_new(pa_mainloop_api *m) {
+ pa_avahi_poll *p;
+
+ assert(m);
+
+ p = pa_xnew(pa_avahi_poll, 1);
+
+ p->api.userdata = p;
+ p->api.watch_new = watch_new;
+ p->api.watch_update = watch_update;
+ p->api.watch_get_events = watch_get_events;
+ p->api.watch_free = watch_free;
+ p->api.timeout_new = timeout_new;
+ p->api.timeout_update = timeout_update;
+ p->api.timeout_free = timeout_free;
+ p->mainloop = m;
+
+ return &p->api;
+}
+
+void pa_avahi_poll_free(AvahiPoll *api) {
+ pa_avahi_poll *p;
+ assert(api);
+ p = api->userdata;
+ assert(p);
+
+ pa_xfree(p);
+}
+
diff --git a/src/pulsecore/avahi-wrap.h b/src/pulsecore/avahi-wrap.h
new file mode 100644
index 00000000..97da11eb
--- /dev/null
+++ b/src/pulsecore/avahi-wrap.h
@@ -0,0 +1,33 @@
+#ifndef fooavahiwrapperhfoo
+#define fooavahiwrapperhfoo
+
+/* $Id$ */
+
+/***
+ This file is part of PulseAudio.
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <avahi-client/client.h>
+
+#include <pulse/mainloop-api.h>
+
+AvahiPoll* pa_avahi_poll_new(pa_mainloop_api *api);
+void pa_avahi_poll_free(AvahiPoll *p);
+
+
+#endif