From: Mikel Astiz <mikel.astiz@xxxxxxxxxxxx> Handle availability changes in Bluetooth ports inside module-bluetooth-policy. The implemented behavior is similar to how module-switch-on-port-available behaves, but the conditions are more relaxed and thus more profile changes are triggered. --- src/modules/bluetooth/module-bluetooth-policy.c | 89 +++++++++++++++++++++++ 1 files changed, 89 insertions(+), 0 deletions(-) diff --git a/src/modules/bluetooth/module-bluetooth-policy.c b/src/modules/bluetooth/module-bluetooth-policy.c index 0e1ad34..6f8be27 100644 --- a/src/modules/bluetooth/module-bluetooth-policy.c +++ b/src/modules/bluetooth/module-bluetooth-policy.c @@ -56,6 +56,7 @@ struct userdata { pa_hook_slot *sink_put_slot; pa_hook_slot *source_state_changed_slot; pa_hook_slot *sink_state_changed_slot; + pa_hook_slot *port_available_changed_slot; pa_hashmap *loaded_modules; }; @@ -171,6 +172,86 @@ static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void *user return PA_HOOK_OK; } +static pa_device_port* find_best_port(pa_hashmap *ports) { + void *state; + pa_device_port *port; + pa_device_port *result = NULL; + + PA_HASHMAP_FOREACH(port, ports, state) { + if (port->available != PA_PORT_AVAILABLE_YES) + continue; + + if (result == NULL || port->priority > result->priority) + result = port; + } + + return result; +} + +static void set_port_profile(pa_card *card, pa_device_port *port) { + void *state; + pa_card_profile *profile; + + PA_HASHMAP_FOREACH(profile, port->profiles, state) { + pa_log_debug("Setting card '%s' to profile '%s'", card->name, profile->name); + + if (pa_card_set_profile(card, profile->name, FALSE) != 0) + pa_log_warn("Could not set profile '%s'", profile->name); + + return; + } +} + +static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port *port, void *userdata) { + pa_card *card; + const char *s; + uint32_t state; + pa_bool_t is_active_profile; + pa_device_port *port2; + + PA_IDXSET_FOREACH(card, c->cards, state) + if (port == pa_hashmap_get(card->ports, port->name)) + break; + + if (!card) { + pa_log_warn("Did not find port %s in array of cards", port->name); + return PA_HOOK_OK; + } + + /* Only consider bluetooth cards */ + s = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS); + if (!s || !pa_streq(s, "bluetooth")) + return PA_HOOK_OK; + + is_active_profile = card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name); + + if (is_active_profile && port->available == PA_PORT_AVAILABLE_YES) + return PA_HOOK_OK; + + if (!is_active_profile && port->available != PA_PORT_AVAILABLE_YES) + return PA_HOOK_OK; + + if ((port2 = find_best_port(card->ports)) == NULL) + return PA_HOOK_OK; + + set_port_profile(card, port2); + + return PA_HOOK_OK; +} + +static void handle_all_ports(pa_core *core) { + pa_card *card; + uint32_t state; + + PA_IDXSET_FOREACH(card, core->cards, state) { + pa_device_port *port; + void *state2; + + PA_HASHMAP_FOREACH(port, card->ports, state2) + port_available_hook_callback(core, port, NULL); + } +} + int pa__init(pa_module *m) { pa_modargs *ma; struct userdata *u; @@ -206,6 +287,11 @@ int pa__init(pa_module *m) { u->source_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_hook_callback, u); + u->port_available_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED], + PA_HOOK_NORMAL, (pa_hook_cb_t) port_available_hook_callback, u); + + handle_all_ports(m->core); + pa_modargs_free(ma); return 0; @@ -234,6 +320,9 @@ void pa__done(pa_module *m) { if (u->sink_state_changed_slot) pa_hook_slot_free(u->sink_state_changed_slot); + if (u->port_available_changed_slot) + pa_hook_slot_free(u->port_available_changed_slot); + pa_hashmap_free(u->loaded_modules, NULL, NULL); pa_xfree(u); } -- 1.7.7.6