+static uint32_t get_role_index(const char* role) {
+ pa_assert(role);
+
+ for (uint32_t i = ROLE_NONE; i < NUM_ROLES; ++i)
+ if (pa_streq(role, role_names[i]))
+ return i;
+
+ return PA_INVALID_INDEX;
+}
+
+static void update_highest_priority_device_indexes(struct userdata *u, const char *prefix, void *ignore_device) {
+ role_indexes_t *indexes, highest_priority_available;
+ pa_datum key;
+ bool done, sink_mode;
+
+ pa_assert(u);
+ pa_assert(prefix);
+
+ sink_mode = pa_streq(prefix, "sink:");
+
+ if (sink_mode)
+ indexes = &u->preferred_sinks;
+ else
+ indexes = &u->preferred_sources;
+
+ for (uint32_t i = 0; i < NUM_ROLES; ++i) {
+ (*indexes)[i] = PA_INVALID_INDEX;
+ }
+ pa_zero(highest_priority_available);
+
+ done = !pa_database_first(u->database, &key, NULL);
+
+ /* Find all existing devices with the same prefix so we find the highest priority device for each role */
+ while (!done) {
+ pa_datum next_key;
+
+ done = !pa_database_next(u->database, &key, &next_key, NULL);
+
+ if (key.size > strlen(prefix) && strncmp(key.data, prefix, strlen(prefix)) == 0) {
+ char *name, *device_name;
+ struct entry *e;
+
+ name = pa_xstrndup(key.data, key.size);
+ pa_assert_se(device_name = get_name(name, prefix));
+
+ if ((e = entry_read(u, name))) {
+ for (uint32_t i = 0; i < NUM_ROLES; ++i) {
+ if (!highest_priority_available[i] || e->priority[i] < highest_priority_available[i]) {
+ /* We've found a device with a higher priority than that we've currently got,
+ so see if it is currently available or not and update our list */
+ uint32_t idx;
+ bool found = false;
+
+ if (sink_mode) {
+ pa_sink *sink;
+
+ PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
+ if ((pa_sink*) ignore_device == sink)
+ continue;
+ if (pa_streq(sink->name, device_name)) {
+ found = true;
+ idx = sink->index; /* Is this needed? */
+ break;
+ }
+ }
+ } else {
+ pa_source *source;
+
+ PA_IDXSET_FOREACH(source, u->core->sources, idx) {
+ if ((pa_source*) ignore_device == source)
+ continue;
+ if (pa_streq(source->name, device_name)) {
+ found = true;
+ idx = source->index; /* Is this needed? */
+ break;
+ }
+ }
+ }
+ if (found) {
+ highest_priority_available[i] = e->priority[i];
+ (*indexes)[i] = idx;
+ }
+
+ }
+ }
+
+ entry_free(e);
+ }
+
+ pa_xfree(name);
+ pa_xfree(device_name);
+ }
+
+ pa_datum_free(&key);
+ key = next_key;
+ }
+}
+
+static void route_sink_input(struct userdata *u, pa_sink_input *si) {
+ const char *role;
+ uint32_t role_index, device_index;
+ pa_sink *sink;
+
+ pa_assert(u);
+ pa_assert(u->do_routing);
+
+ if (si->save_sink)
+ return;
+
+ /* Skip this if it is already in the process of being moved anyway */
+ if (!si->sink)
+ return;
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
+ return;
+
+ if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
+ role_index = get_role_index("none");
+ else
+ role_index = get_role_index(role);
+
+ if (PA_INVALID_INDEX == role_index)
+ return;
+
+ device_index = u->preferred_sinks[role_index];
+ if (PA_INVALID_INDEX == device_index)
+ return;
+
+ if (!(sink = pa_idxset_get_by_index(u->core->sinks, device_index)))
+ return;
+
+ if (si->sink != sink)
+ pa_sink_input_move_to(si, sink, false);
+}
+
+static pa_hook_result_t route_sink_inputs(struct userdata *u, pa_sink *ignore_sink) {
+ pa_sink_input *si;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ if (!u->do_routing)
+ return PA_HOOK_OK;
+
+ update_highest_priority_device_indexes(u, "sink:", ignore_sink);
+
+ PA_IDXSET_FOREACH(si, u->core->sink_inputs, idx) {
+ route_sink_input(u, si);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static void route_source_output(struct userdata *u, pa_source_output *so) {
+ const char *role;
+ uint32_t role_index, device_index;
+ pa_source *source;
+
+ pa_assert(u);
+ pa_assert(u->do_routing);
+
+ if (so->save_source)
+ return;
+
+ if (so->direct_on_input)
+ return;
+
+ /* Skip this if it is already in the process of being moved anyway */
+ if (!so->source)
+ return;
+
+ /* It might happen that a stream and a source are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
+ return;
+
+ if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
+ role_index = get_role_index("none");
+ else
+ role_index = get_role_index(role);
+
+ if (PA_INVALID_INDEX == role_index)
+ return;
+
+ device_index = u->preferred_sources[role_index];
+ if (PA_INVALID_INDEX == device_index)
+ return;
+
+ if (!(source = pa_idxset_get_by_index(u->core->sources, device_index)))
+ return;
+
+ if (so->source != source)
+ pa_source_output_move_to(so, source, false);
+}
+
+static pa_hook_result_t route_source_outputs(struct userdata *u, pa_source* ignore_source) {
+ pa_source_output *so;
+ uint32_t idx;
+
+ pa_assert(u);
+
+ if (!u->do_routing)
+ return PA_HOOK_OK;
+
+ update_highest_priority_device_indexes(u, "source:", ignore_source);
+
+ PA_IDXSET_FOREACH(so, u->core->source_outputs, idx) {
+ route_source_output(u, so);
+ }
+
+ return PA_HOOK_OK;
+}
+