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;
return;
if (si->sink != sink)
- pa_sink_input_move_to(si, sink, TRUE);
+ pa_sink_input_move_to(si, sink, FALSE);
}
static pa_hook_result_t route_sink_inputs(struct userdata *u, pa_sink *ignore_sink) {
pa_assert(u);
pa_assert(u->do_routing);
+ if (so->save_source)
+ return;
+
if (so->direct_on_input)
return;
return;
if (so->source != source)
- pa_source_output_move_to(so, source, TRUE);
+ pa_source_output_move_to(so, source, FALSE);
}
static pa_hook_result_t route_source_outputs(struct userdata *u, pa_source* ignore_source) {
return PA_HOOK_OK;
if (new_data->sink)
- pa_log_debug("Overriding device for stream, even although it is already set. I am evil that way...");
-
- if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
- role_index = get_role_index("none");
- else
- role_index = get_role_index(role);
+ pa_log_debug("Not restoring device for stream because already set.");
+ else {
+ if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
+ role_index = get_role_index("none");
+ else
+ role_index = get_role_index(role);
- if (PA_INVALID_INDEX != role_index) {
- uint32_t device_index;
+ if (PA_INVALID_INDEX != role_index) {
+ uint32_t device_index;
- device_index = u->preferred_sinks[role_index];
- if (PA_INVALID_INDEX != device_index) {
- pa_sink *sink;
+ device_index = u->preferred_sinks[role_index];
+ if (PA_INVALID_INDEX != device_index) {
+ pa_sink *sink;
- if ((sink = pa_idxset_get_by_index(u->core->sinks, device_index))) {
- new_data->sink = sink;
+ if ((sink = pa_idxset_get_by_index(u->core->sinks, device_index))) {
+ new_data->sink = sink;
+ new_data->save_sink = FALSE;
+ }
}
}
}
return PA_HOOK_OK;
if (new_data->source)
- pa_log_debug("Overriding device for stream, even although it is already set. I am evil that way...");
-
- if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
- role_index = get_role_index("none");
- else
- role_index = get_role_index(role);
+ pa_log_debug("Not restoring device for stream because already set.");
+ else {
+ if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
+ role_index = get_role_index("none");
+ else
+ role_index = get_role_index(role);
- if (PA_INVALID_INDEX != role_index) {
- uint32_t device_index;
+ if (PA_INVALID_INDEX != role_index) {
+ uint32_t device_index;
- device_index = u->preferred_sources[role_index];
- if (PA_INVALID_INDEX != device_index) {
- pa_source *source;
+ device_index = u->preferred_sources[role_index];
+ if (PA_INVALID_INDEX != device_index) {
+ pa_source *source;
- if ((source = pa_idxset_get_by_index(u->core->sources, device_index))) {
- new_data->source = source;
+ if ((source = pa_idxset_get_by_index(u->core->sources, device_index))) {
+ new_data->source = source;
+ new_data->save_source = FALSE;
+ }
}
}
}
if ((e = read_entry(u, name))) {
uint32_t idx;
- char *devname;
- pa_bool_t available = FALSE;
+ char *device_name;
+ uint32_t found_index = PA_INVALID_INDEX;
- if ((devname = get_name(name, "sink:"))) {
+ if ((device_name = get_name(name, "sink:"))) {
pa_sink* s;
PA_IDXSET_FOREACH(s, u->core->sinks, idx) {
- if (strcmp(s->name, devname) == 0) {
- available = TRUE;
+ if (strcmp(s->name, device_name) == 0) {
+ found_index = s->index;
break;
}
}
- pa_xfree(devname);
- } else if ((devname = get_name(name, "source:"))) {
+ pa_xfree(device_name);
+ } else if ((device_name = get_name(name, "source:"))) {
pa_source* s;
PA_IDXSET_FOREACH(s, u->core->sources, idx) {
- if (strcmp(s->name, devname) == 0) {
- available = TRUE;
+ if (strcmp(s->name, device_name) == 0) {
+ found_index = s->index;
break;
}
}
- pa_xfree(devname);
+ pa_xfree(device_name);
}
pa_tagstruct_puts(reply, name);
pa_tagstruct_puts(reply, e->description);
pa_tagstruct_puts(reply, e->icon);
- pa_tagstruct_put_boolean(reply, available);
+ pa_tagstruct_putu32(reply, found_index);
pa_tagstruct_putu32(reply, NUM_ROLES);
for (uint32_t i = ROLE_NONE; i < NUM_ROLES; ++i) {
return PA_HOOK_OK;
}
+struct prioritised_indexes {
+ uint32_t index;
+ int32_t priority;
+};
+
int pa__init(pa_module*m) {
pa_modargs *ma = NULL;
struct userdata *u;
pa_source *source;
uint32_t idx;
pa_bool_t do_routing = FALSE, on_hotplug = TRUE, on_rescue = TRUE;
+ uint32_t total_devices;
pa_assert(m);
u->source_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_new_hook_callback, u);
/* The following slots are used to deal with routing */
- /* A little bit later than module-stream-restore, module-intended-roles */
- u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+15, (pa_hook_cb_t) sink_input_new_hook_callback, u);
- u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+15, (pa_hook_cb_t) source_output_new_hook_callback, u);
+ /* A little bit later than module-stream-restore, but before module-intended-roles */
+ u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+5, (pa_hook_cb_t) sink_input_new_hook_callback, u);
+ u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+5, (pa_hook_cb_t) source_output_new_hook_callback, u);
if (on_hotplug) {
- /* A little bit later than module-stream-restore, module-intended-roles */
- u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+15, (pa_hook_cb_t) sink_put_hook_callback, u);
- u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+15, (pa_hook_cb_t) source_put_hook_callback, u);
+ /* A little bit later than module-stream-restore, but before module-intended-roles */
+ u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+5, (pa_hook_cb_t) sink_put_hook_callback, u);
+ u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+5, (pa_hook_cb_t) source_put_hook_callback, u);
}
if (on_rescue) {
- /* A little bit later than module-stream-restore, module-intended-roles, a little bit earlier than module-rescue-streams, ... */
- u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+15, (pa_hook_cb_t) sink_unlink_hook_callback, u);
- u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+15, (pa_hook_cb_t) source_unlink_hook_callback, u);
+ /* A little bit later than module-stream-restore, a little bit earlier than module-intended-roles, module-rescue-streams, ... */
+ u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+5, (pa_hook_cb_t) sink_unlink_hook_callback, u);
+ u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+5, (pa_hook_cb_t) source_unlink_hook_callback, u);
}
if (!(fname = pa_state_path("device-manager", TRUE)))
pa_log_info("Sucessfully opened database file '%s'.", fname);
pa_xfree(fname);
- /* We cycle over all the available sinks so that they are added to our database if they are not in it yet */
- PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
- subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
+ /* Attempt to inject the devices into the list in priority order */
+ total_devices = PA_MAX(pa_idxset_size(m->core->sinks), pa_idxset_size(m->core->sources));
+ if (total_devices > 0 && total_devices < 128) {
+ uint32_t i;
+ struct prioritised_indexes p_i[128];
+
+ /* We cycle over all the available sinks so that they are added to our database if they are not in it yet */
+ i = 0;
+ PA_IDXSET_FOREACH(sink, m->core->sinks, idx) {
+ pa_log_debug("Found sink index %u", sink->index);
+ p_i[i ].index = sink->index;
+ p_i[i++].priority = sink->priority;
+ }
+ /* Bubble sort it (only really useful for first time creation) */
+ if (i > 1)
+ for (uint32_t j = 0; j < i; ++j)
+ for (uint32_t k = 0; k < i; ++k)
+ if (p_i[j].priority > p_i[k].priority) {
+ struct prioritised_indexes tmp_pi = p_i[k];
+ p_i[k] = p_i[j];
+ p_i[j] = tmp_pi;
+ }
+ /* Register it */
+ for (uint32_t j = 0; j < i; ++j)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, p_i[j].index, u);
+
+
+ /* We cycle over all the available sources so that they are added to our database if they are not in it yet */
+ i = 0;
+ PA_IDXSET_FOREACH(source, m->core->sources, idx) {
+ p_i[i ].index = source->index;
+ p_i[i++].priority = source->priority;
+ }
+ /* Bubble sort it (only really useful for first time creation) */
+ if (i > 1)
+ for (uint32_t j = 0; j < i; ++j)
+ for (uint32_t k = 0; k < i; ++k)
+ if (p_i[j].priority > p_i[k].priority) {
+ struct prioritised_indexes tmp_pi = p_i[k];
+ p_i[k] = p_i[j];
+ p_i[j] = tmp_pi;
+ }
+ /* Register it */
+ for (uint32_t j = 0; j < i; ++j)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, p_i[j].index, u);
+ }
+ else if (total_devices > 0) {
+ /* This user has a *lot* of devices... */
+ PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
- PA_IDXSET_FOREACH(source, m->core->sources, idx)
- subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
+ PA_IDXSET_FOREACH(source, m->core->sources, idx)
+ subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
+ }
/* Perform the routing (if it's enabled) which will update our priority list cache too */
for (uint32_t i = 0; i < NUM_ROLES; ++i) {