From: Jo?o Paulo Rechi Vita <jprvita@xxxxxxxxxxxxx> --- src/modules/bluetooth/module-bluez5-device.c | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/modules/bluetooth/module-bluez5-device.c b/src/modules/bluetooth/module-bluez5-device.c index c1c3477..aa247f9 100644 --- a/src/modules/bluetooth/module-bluez5-device.c +++ b/src/modules/bluetooth/module-bluez5-device.c @@ -47,10 +47,32 @@ struct userdata { pa_core *core; pa_modargs *modargs; + pa_hook_slot *device_connection_changed_slot; + pa_bluetooth_discovery *discovery; pa_bluetooth_device *device; }; +/* Run from main thread */ +static pa_hook_result_t device_connection_changed_cb(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) { + pa_assert(d); + pa_assert(u); + + if (d != u->device) + return PA_HOOK_OK; + + if (d->dead) + pa_log_debug("Device %s removed: unloading module-bluez5-device", d->path); + else if (!pa_bluetooth_device_any_transport_connected(d)) + pa_log_debug("Unloading module, because device %s doesn't have any audio profiles connected anymore", d->path); + else + return PA_HOOK_OK; + + pa_module_unload(u->core, u->module, true); + + return PA_HOOK_OK; +} + int pa__init(pa_module* m) { struct userdata *u; pa_modargs *ma; @@ -81,6 +103,10 @@ int pa__init(pa_module* m) { goto fail; } + u->device_connection_changed_slot = + pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED), + PA_HOOK_NORMAL, (pa_hook_cb_t) device_connection_changed_cb, u); + return 0; fail: @@ -98,6 +124,9 @@ void pa__done(pa_module *m) { if (!(u = m->userdata)) return; + if (u->device_connection_changed_slot) + pa_hook_slot_free(u->device_connection_changed_slot); + if (u->modargs) pa_modargs_free(u->modargs); -- 1.7.11.7