Commit 14b9e136 authored by Arnaud Ferraris's avatar Arnaud Ferraris
Browse files

cad-pulse: stop tracking the "default" port

Tracking the default port (which is actually the non-speaker active 
output port) can be tedious and prone to errors.

Instead of trying to track which port was in use when we switched to 
speaker, we can simply check, when it's time to switch back, which 
available port has higher priority and use this one.

This will also help solve corner cases: switch to call mode (earpiece), 
enable speaker, plug in headphones, then disable speaker: the current 
code will automatically switch back to headphones instead of the 
earpiece, which sounds like the good thing to do in that case.
parent ea3d21d5
......@@ -33,7 +33,6 @@ struct _CadPulse
int source_id;
gchar *speaker_port;
gchar *default_port;
};
G_DEFINE_TYPE(CadPulse, cad_pulse, G_TYPE_OBJECT);
......@@ -66,7 +65,6 @@ static void process_new_source(CadPulse *self, const pa_source_info *info)
static void process_sink_ports(CadPulse *self, const pa_sink_info *info)
{
pa_sink_port_info *active = info->active_port;
int i;
for (i = 0; i < info->n_ports; i++) {
......@@ -79,19 +77,9 @@ static void process_sink_ports(CadPulse *self, const pa_sink_info *info)
} else if (!self->speaker_port) {
self->speaker_port = g_strdup(port->name);
}
if (port == active && self->default_port) {
g_free(self->default_port);
self->default_port = NULL;
}
} else if (port == active) {
self->default_port = g_strdup(port->name);
}
}
g_debug("SINK: speaker_port='%s'", self->speaker_port);
if (self->default_port)
g_debug("SINK: default_port='%s'", self->default_port);
}
static void process_new_sink(CadPulse *self, const pa_sink_info *info)
......@@ -276,8 +264,6 @@ static void dispose(GObject *object)
if (self->speaker_port)
g_free(self->speaker_port);
if (self->default_port)
g_free(self->default_port);
if (self->ctx) {
pa_context_disconnect(self->ctx);
......@@ -381,6 +367,7 @@ static void set_speaker_enable(pa_context *ctx, const pa_sink_info *info, int eo
CadPulseOperation *operation = data;
pa_sink_port_info *port;
pa_operation *op = NULL;
const gchar *target_port;
if (eol == 1)
return;
......@@ -391,21 +378,32 @@ static void set_speaker_enable(pa_context *ctx, const pa_sink_info *info, int eo
if (info->card != operation->pulse->card_id || info->index != operation->pulse->sink_id)
return;
target_port = operation->pulse->speaker_port;
port = info->active_port;
if (strcmp(port->name, operation->pulse->speaker_port) == 0 && !operation->value) {
if (operation->pulse->default_port) {
if (strcmp(port->name, target_port) == 0 && !operation->value) {
pa_sink_port_info *available_port = NULL;
guint i;
// Look for the highest priority available port which isn't target port
for (i = 0; i < info->n_ports; i++) {
port = info->ports[i];
if (port->available != PA_PORT_AVAILABLE_NO &&
strcmp(port->name, target_port) != 0 &&
(!available_port || port->priority > available_port->priority)) {
available_port = port;
}
}
if (available_port) {
op = pa_context_set_sink_port_by_index(ctx, operation->pulse->sink_id,
operation->pulse->default_port,
available_port->name,
operation_complete_cb, operation);
}
} else if (strcmp(port->name, operation->pulse->speaker_port) != 0 && operation->value) {
if (operation->pulse->default_port)
g_free(operation->pulse->default_port);
operation->pulse->default_port = g_strdup(port->name);
}
} else if (strcmp(port->name, target_port) != 0 && operation->value) {
op = pa_context_set_sink_port_by_index(ctx, operation->pulse->sink_id,
operation->pulse->speaker_port,
target_port,
operation_complete_cb, operation);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment