2 This file is part of PulseAudio.
4 Copyright 2008-2013 João Paulo Rechi Vita
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio 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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
27 #include <pulse/xmalloc.h>
28 #include <pulsecore/module.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/dbus-shared.h>
35 #include "bluez4-util.h"
37 PA_MODULE_AUTHOR("João Paulo Rechi Vita");
38 PA_MODULE_DESCRIPTION("Detect available BlueZ 4 Bluetooth audio devices and load BlueZ 4 Bluetooth audio drivers");
39 PA_MODULE_VERSION(PACKAGE_VERSION);
40 PA_MODULE_USAGE("sco_sink=<name of sink> "
41 "sco_source=<name of source> ");
42 PA_MODULE_LOAD_ONCE(true);
44 static const char* const valid_modargs[] = {
47 "async", /* deprecated */
55 pa_bluez4_discovery *discovery;
60 struct pa_module_info {
65 static pa_hook_result_t load_module_for_device(pa_bluez4_discovery *y, const pa_bluez4_device *d, struct userdata *u) {
66 struct pa_module_info *mi;
71 mi = pa_hashmap_get(u->hashmap, d->path);
73 if (pa_bluez4_device_any_audio_connected(d)) {
79 /* Oh, awesome, a new device has shown up and been connected! */
81 args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
83 if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
84 pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
87 tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
88 pa_modargs_get_value(u->modargs, "sco_sink", NULL),
89 pa_modargs_get_value(u->modargs, "sco_source", NULL));
94 pa_log_debug("Loading module-bluez4-device %s", args);
95 pa_module_load(&m, u->module->core, "module-bluez4-device", args);
99 mi = pa_xnew(struct pa_module_info, 1);
100 mi->module = m->index;
101 mi->path = pa_xstrdup(d->path);
103 pa_hashmap_put(u->hashmap, mi->path, mi);
105 pa_log_debug("Failed to load module for device %s", d->path);
112 /* Hmm, disconnection? Then the module unloads itself */
114 pa_log_debug("Unregistering module for %s", d->path);
115 pa_hashmap_remove(u->hashmap, mi->path);
124 int pa__init(pa_module* m) {
130 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
131 pa_log("Failed to parse module arguments");
135 if (pa_modargs_get_value(ma, "async", NULL))
136 pa_log_warn("The 'async' argument is deprecated and does nothing.");
138 m->userdata = u = pa_xnew0(struct userdata, 1);
142 u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
144 if (!(u->discovery = pa_bluez4_discovery_get(u->core)))
147 u->slot = pa_hook_connect(pa_bluez4_discovery_hook(u->discovery, PA_BLUEZ4_HOOK_DEVICE_CONNECTION_CHANGED),
148 PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
158 void pa__done(pa_module* m) {
163 if (!(u = m->userdata))
167 pa_hook_slot_free(u->slot);
170 pa_bluez4_discovery_unref(u->discovery);
173 struct pa_module_info *mi;
175 while ((mi = pa_hashmap_steal_first(u->hashmap))) {
180 pa_hashmap_free(u->hashmap);
184 pa_modargs_free(u->modargs);