From cbd8da6e464203575c9b83bc5fb86390a25122fd Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 14 Oct 2018 22:17:47 +0200 Subject: [PATCH] Headphone switch button --- pulse.c | 17 ++++++++++++----- ursaryd.c | 36 ++++++++++++++++++++++++++++++++++-- ursaryd.h | 2 ++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/pulse.c b/pulse.c index d6a26b6..53478dc 100644 --- a/pulse.c +++ b/pulse.c @@ -1,7 +1,7 @@ /* * Asynchronous Interface to PulseAudio * - * (c) 2014 Martin Mares + * (c) 2014--2018 Martin Mares */ #undef LOCAL_DEBUG @@ -91,8 +91,8 @@ void pulse_dump(void) msg(L_DEBUG, "## Client #%d: %s host=%s", c->idx, c->name, c->host); CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list) - msg(L_DEBUG, "## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u suspended=%u", - s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute, s->suspended); + msg(L_DEBUG, "## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u suspended=%u port=%s", + s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute, s->suspended, s->active_port); CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list) msg(L_DEBUG, "## Sink input #%d: %s client=%d sink=%d channels=%u volume=%u mute=%u", @@ -236,8 +236,9 @@ static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol return; } - DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u", - i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state); + DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u port=%s", + i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state, + (i->active_port ? i->active_port->name : "none")); pulse_dump_proplist(i->proplist); struct pulse_sink *s = pulse_sink_lookup(i->index); @@ -249,6 +250,7 @@ static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol s->base_volume = i->base_volume; s->mute = i->mute; s->suspended = (i->state == PA_SINK_SUSPENDED); + SET_STRING(s->active_port, (i->active_port ? i->active_port->name : "none")); schedule_update(); } @@ -284,6 +286,11 @@ void pulse_sink_set_mute(int idx, bool mute) PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb); } +void pulse_sink_set_port(int idx, const char *port) +{ + PULSE_ASYNC_RUN(pa_context_set_sink_port_by_index, idx, port, pulse_success_cb); +} + /*** Clients ***/ #define HASH_NODE struct pulse_client diff --git a/ursaryd.c b/ursaryd.c index 97791f2..ddc896f 100644 --- a/ursaryd.c +++ b/ursaryd.c @@ -1,7 +1,7 @@ /* * The Ursary Audio Controls * - * (c) 2014 Martin Mares + * (c) 2014--2018 Martin Mares */ #undef LOCAL_DEBUG @@ -73,6 +73,18 @@ static void update_ring_from_sink(int ring, const char *sink_name) noct_set_button(ring, 0); } +static void update_button_from_port(int button, const char *sink_name, const char *port_name) +{ + struct pulse_sink *s = pulse_sink_by_name(sink_name); + if (!s) + { + noct_set_button(button, 0); + return; + } + + noct_set_button(button, !strcmp(s->active_port, port_name)); +} + static void update_sink_from_rotary(int delta, const char *sink_name) { struct pulse_sink *s = pulse_sink_by_name(sink_name); @@ -103,6 +115,23 @@ static void update_sink_mute_from_button(int on, const char *sink_name) pulse_sink_set_mute(s->idx, !s->mute); } +static void update_port_from_button(int on, const char *sink_name, const char *port1, const char *port2) +{ + if (!on) + return; + + struct pulse_sink *s = pulse_sink_by_name(sink_name); + if (!s) + return; + + const char *port = port1; + if (!strcmp(s->active_port, port1)) + port = port2; + + DBG("## Setting port of sink %s to %s", s->name, port); + pulse_sink_set_port(s->idx, port); +} + /*** Client controls ***/ struct client_map { @@ -568,6 +597,7 @@ static void do_update(struct main_timer *t) // Everything normal update_ring_from_sink(0, "alsa_output.brum.analog-stereo"); + update_button_from_port(8, "alsa_output.brum.analog-stereo", "analog-output-headphones"); update_groups(); #if 0 update_default_sink(); @@ -623,8 +653,10 @@ void notify_button(int button, int on) case 0: update_sink_mute_from_button(on, "alsa_output.brum.analog-stereo"); break; -#if 0 case 8: + update_port_from_button(on, "alsa_output.brum.analog-stereo", "analog-output-lineout", "analog-output-headphones"); + break; +#if 0 case 9: case 10: update_default_sink_from_button(button, on); diff --git a/ursaryd.h b/ursaryd.h index 805e2b8..18abb1b 100644 --- a/ursaryd.h +++ b/ursaryd.h @@ -56,6 +56,7 @@ struct pulse_sink { uint base_volume; bool mute; bool suspended; + char *active_port; }; struct pulse_sink_input { @@ -79,6 +80,7 @@ struct pulse_sink *pulse_sink_by_name(const char *name); struct pulse_sink *pulse_sink_by_idx(int idx); void pulse_sink_set_volume(int idx, pa_cvolume *cvol); void pulse_sink_set_mute(int idx, bool mute); +void pulse_sink_set_port(int idx, const char *port); void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol); void pulse_sink_input_set_mute(int idx, bool mute); void pulse_sink_input_move(int input_idx, int sink_idx); -- 2.39.2