paswitch: add robustness

This commit is contained in:
Přemysl Eric Janouch 2018-10-28 19:16:22 +01:00
parent 1f36351ab7
commit da2899f721
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 21 additions and 7 deletions

View File

@ -196,6 +196,15 @@ app_context_free (struct app_context *self)
poller_free (&self->poller); poller_free (&self->poller);
} }
static struct sink *
current_sink (struct app_context *ctx)
{
LIST_FOR_EACH (struct sink, iter, ctx->sinks)
if (iter->index == ctx->selected_sink)
return iter;
return NULL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#define VOLUME_PERCENT(x) (((x) * 100 + PA_VOLUME_NORM / 2) / PA_VOLUME_NORM) #define VOLUME_PERCENT(x) (((x) * 100 + PA_VOLUME_NORM / 2) / PA_VOLUME_NORM)
@ -260,9 +269,14 @@ on_sink_info (pa_context *context, const pa_sink_info *info, int eol,
} }
if (!info || eol) if (!info || eol)
{ {
// TODO: handle the case of when sinks disappear struct sink *sink = current_sink (ctx);
if (ctx->selected_sink == PA_INVALID_INDEX && ctx->sinks) if (!sink && ctx->sinks)
{
ctx->selected_sink = ctx->sinks->index; ctx->selected_sink = ctx->sinks->index;
ctx->selected_port = -1;
}
else if (sink && ctx->selected_port >= (ssize_t) sink->ports_len)
ctx->selected_port = -1;
poller_idle_set (&ctx->redraw_event); poller_idle_set (&ctx->redraw_event);
ctx->reset_sinks = true; ctx->reset_sinks = true;
@ -304,6 +318,8 @@ update_sinks (struct app_context *ctx)
(ctx->context, on_sink_info, ctx)); (ctx->context, on_sink_info, ctx));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void static void
forget_sink_inputs (struct app_context *ctx) forget_sink_inputs (struct app_context *ctx)
{ {
@ -358,6 +374,8 @@ on_server_info (pa_context *context, const struct pa_server_info *info,
cstr_set (&ctx->default_sink, NULL); cstr_set (&ctx->default_sink, NULL);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void static void
update_server_info (struct app_context *ctx) update_server_info (struct app_context *ctx)
{ {
@ -600,11 +618,7 @@ on_action (struct app_context *ctx, enum action action)
{ {
poller_idle_set (&ctx->redraw_event); poller_idle_set (&ctx->redraw_event);
struct sink *sink = NULL; struct sink *sink = current_sink (ctx);
LIST_FOR_EACH (struct sink, iter, ctx->sinks)
if (iter->index == ctx->selected_sink)
sink = iter;
switch (action) switch (action)
{ {
case ACTION_UP: case ACTION_UP: