/***
This file is part of PulseAudio.
- Copyright 2008 Joao Paulo Rechi Vita
+ Copyright 2008-2013 João Paulo Rechi Vita
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
+ published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
#include <pulse/xmalloc.h>
#include <pulsecore/module.h>
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/macro.h>
-#include <pulsecore/llist.h>
#include <pulsecore/core-util.h>
-#include <modules/dbus-util.h>
+#include <pulsecore/dbus-shared.h>
#include "module-bluetooth-discover-symdef.h"
#include "bluetooth-util.h"
-PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
-PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
+PA_MODULE_AUTHOR("João Paulo Rechi Vita");
+PA_MODULE_DESCRIPTION("Detect available BlueZ 4 Bluetooth audio devices and load BlueZ 4 Bluetooth audio drivers");
PA_MODULE_VERSION(PACKAGE_VERSION);
PA_MODULE_USAGE("sco_sink=<name of sink> "
- "sco_source=<name of source>"
- "async=<Asynchronous initialization?>");
+ "sco_source=<name of source> ");
+PA_MODULE_LOAD_ONCE(true);
static const char* const valid_modargs[] = {
"sco_sink",
"sco_source",
- "async",
+ "async", /* deprecated */
NULL
};
struct userdata {
pa_module *module;
- pa_modargs *ma;
+ 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 (((uint32_t) PA_PTR_TO_UINT(d->data))-1 == PA_INVALID_INDEX) {
+ if (pa_bluetooth_device_any_audio_connected(d)) {
+
+ if (!mi) {
pa_module *m = NULL;
char *args;
args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
- if (pa_modargs_get_value(u->ma, "sco_sink", NULL) &&
- pa_modargs_get_value(u->ma, "sco_source", NULL)) {
+ if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
+ pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
char *tmp;
- tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args, pa_modargs_get_value(u->ma, "sco_sink", NULL), pa_modargs_get_value(u->ma, "sco_source", NULL));
+ tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
+ pa_modargs_get_value(u->modargs, "sco_sink", NULL),
+ pa_modargs_get_value(u->modargs, "sco_source", NULL));
pa_xfree(args);
args = tmp;
}
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 */
+ /* Hmm, disconnection? Then the module unloads itself */
- 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;
+ pa_log_debug("Unregistering module for %s", d->path);
+ pa_hashmap_remove(u->hashmap, mi->path);
+ pa_xfree(mi->path);
+ pa_xfree(mi);
}
}
-}
-
-static int setup_dbus(struct userdata *u) {
- DBusError err;
-
- 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;
- }
- return 0;
+ return PA_HOOK_OK;
}
int pa__init(pa_module* m) {
struct userdata *u;
- pa_modargs *ma;
- pa_bool_t async = FALSE;
+ pa_modargs *ma = NULL;
pa_assert(m);
goto fail;
}
- if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
- pa_log("Failed to parse async argument.");
- goto fail;
- }
+ if (pa_modargs_get_value(ma, "async", NULL))
+ pa_log_warn("The 'async' argument is deprecated and does nothing.");
m->userdata = u = pa_xnew0(struct userdata, 1);
u->module = m;
u->core = m->core;
- u->ma = ma;
+ 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;
-
- if (!async)
- pa_bluetooth_discovery_sync(u->discovery);
+ u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED),
+ PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
return 0;
fail:
pa__done(m);
+ if (ma)
+ pa_modargs_free(ma);
+
return -1;
}
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);
+ }
- if (u->ma)
- pa_modargs_free(u->ma);
+ if (u->modargs)
+ pa_modargs_free(u->modargs);
pa_xfree(u);
}