From 18e975fc5ef68a89875c77974570884b9214a009 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 20 Jul 2009 17:34:17 +0200 Subject: merged --- src/modules/bluetooth/module-bluetooth-discover.c | 108 ++++++++++++++-------- 1 file changed, 70 insertions(+), 38 deletions(-) (limited to 'src/modules/bluetooth/module-bluetooth-discover.c') diff --git a/src/modules/bluetooth/module-bluetooth-discover.c b/src/modules/bluetooth/module-bluetooth-discover.c index 4586d8ca..788fee00 100644 --- a/src/modules/bluetooth/module-bluetooth-discover.c +++ b/src/modules/bluetooth/module-bluetooth-discover.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include "module-bluetooth-discover-symdef.h" #include "bluetooth-util.h" @@ -42,13 +42,21 @@ PA_MODULE_AUTHOR("Joao Paulo Rechi Vita"); PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers"); PA_MODULE_VERSION(PACKAGE_VERSION); -PA_MODULE_USAGE("sco_sink= " - "sco_source=" - "async="); +PA_MODULE_USAGE("async="); +PA_MODULE_LOAD_ONCE(TRUE); + +/* +#ifdef NOKIA + "sco_sink= " + "sco_source=" +#endif +*/ static const char* const valid_modargs[] = { +#ifdef NOKIA "sco_sink", "sco_source", +#endif "async", NULL }; @@ -57,26 +65,45 @@ struct userdata { pa_module *module; pa_modargs *modargs; pa_core *core; - pa_dbus_connection *connection; pa_bluetooth_discovery *discovery; + pa_hook_slot *slot; + pa_hashmap *hashmap; }; -static void load_module_for_device(struct userdata *u, pa_bluetooth_device *d, pa_bool_t good) { +struct module_info { + char *path; + uint32_t module; +}; + +static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) { + struct module_info *mi; + pa_assert(u); pa_assert(d); - if (good && - d->device_connected > 0 && - (d->audio_sink_connected > 0 || d->headset_connected > 0)) { + mi = pa_hashmap_get(u->hashmap, d->path); + + if (!d->dead && + d->device_connected > 0 && d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED) { - if (((uint32_t) PA_PTR_TO_UINT(d->data))-1 == PA_INVALID_INDEX) { + if (!mi) { pa_module *m = NULL; char *args; /* Oh, awesome, a new device has shown up and been connected! */ - args = pa_sprintf_malloc("address=\"%s\" path=\"%s\" profile=\"%s\"", d->address, d->path, d->headset_connected ? "hsp" : "a2dp"); + args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path); +#if 0 + /* This is in case we have to use hsp immediately, without waiting for .Audio.State = Connected */ + if (d->headset_state >= PA_BT_AUDIO_STATE_CONNECTED && somecondition) { + char *tmp; + tmp = pa_sprintf_malloc("%s profile=\"hsp\"", args); + pa_xfree(args); + args = tmp; + } +#endif +#ifdef NOKIA if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) && pa_modargs_get_value(u->modargs, "sco_source", NULL)) { char *tmp; @@ -87,44 +114,38 @@ static void load_module_for_device(struct userdata *u, pa_bluetooth_device *d, p pa_xfree(args); args = tmp; } +#endif pa_log_debug("Loading module-bluetooth-device %s", args); m = pa_module_load(u->module->core, "module-bluetooth-device", args); pa_xfree(args); - if (m) - d->data = PA_UINT_TO_PTR((uint32_t) (m->index+1)); - else + if (m) { + mi = pa_xnew(struct module_info, 1); + mi->module = m->index; + mi->path = pa_xstrdup(d->path); + + pa_hashmap_put(u->hashmap, mi->path, mi); + } else pa_log_debug("Failed to load module for device %s", d->path); } } else { - if (((uint32_t) PA_PTR_TO_UINT(d->data))-1 != PA_INVALID_INDEX) { + if (mi) { /* Hmm, disconnection? Then let's unload our module */ pa_log_debug("Unloading module for %s", d->path); - pa_module_unload_request_by_index(u->core, (uint32_t) (PA_PTR_TO_UINT(d->data))-1, TRUE); - d->data = NULL; - } - } -} - -static int setup_dbus(struct userdata *u) { - DBusError err; + pa_module_unload_request_by_index(u->core, mi->module, TRUE); - dbus_error_init(&err); - - u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &err); - - if (dbus_error_is_set(&err) || !u->connection) { - pa_log("Failed to get D-Bus connection: %s", err.message); - dbus_error_free(&err); - return -1; + pa_hashmap_remove(u->hashmap, mi->path); + pa_xfree(mi->path); + pa_xfree(mi); + } } - return 0; + return PA_HOOK_OK; } int pa__init(pa_module* m) { @@ -149,12 +170,12 @@ int pa__init(pa_module* m) { u->core = m->core; u->modargs = ma; ma = NULL; + u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); - if (setup_dbus(u) < 0) + if (!(u->discovery = pa_bluetooth_discovery_get(u->core))) goto fail; - if (!(u->discovery = pa_bluetooth_discovery_new(pa_dbus_connection_get(u->connection), load_module_for_device, u))) - goto fail; + u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery), PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u); if (!async) pa_bluetooth_discovery_sync(u->discovery); @@ -178,11 +199,22 @@ void pa__done(pa_module* m) { if (!(u = m->userdata)) return; + if (u->slot) + pa_hook_slot_free(u->slot); + if (u->discovery) - pa_bluetooth_discovery_free(u->discovery); + pa_bluetooth_discovery_unref(u->discovery); + + if (u->hashmap) { + struct module_info *mi; - if (u->connection) - pa_dbus_connection_unref(u->connection); + while ((mi = pa_hashmap_steal_first(u->hashmap))) { + pa_xfree(mi->path); + pa_xfree(mi); + } + + pa_hashmap_free(u->hashmap, NULL, NULL); + } if (u->modargs) pa_modargs_free(u->modargs); -- cgit