]> mj.ucw.cz Git - ursary.git/commitdiff
Headphone switch button
authorMartin Mares <mj@ucw.cz>
Sun, 14 Oct 2018 20:17:47 +0000 (22:17 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 14 Oct 2018 20:17:47 +0000 (22:17 +0200)
pulse.c
ursaryd.c
ursaryd.h

diff --git a/pulse.c b/pulse.c
index d6a26b613c28bf488b433c6f465cd44bf7f7684a..53478dcda6082d2097246c1998d7dd404c717f45 100644 (file)
--- a/pulse.c
+++ b/pulse.c
@@ -1,7 +1,7 @@
 /*
  *     Asynchronous Interface to PulseAudio
  *
- *     (c) 2014 Martin Mares <mj@ucw.cz>
+ *     (c) 2014--2018 Martin Mares <mj@ucw.cz>
  */
 
 #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
index 97791f2d0c2b352ecddc361ce7d1ff09f84c2eec..ddc896f8062b64fe58c878c8420aecb010312134 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
@@ -1,7 +1,7 @@
 /*
  *     The Ursary Audio Controls
  *
- *     (c) 2014 Martin Mares <mj@ucw.cz>
+ *     (c) 2014--2018 Martin Mares <mj@ucw.cz>
  */
 
 #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);
index 805e2b89fe06c8ea7c14f59aa81b9f4440fde90a..18abb1bf3154172f39fa3519bae804ada740206d 100644 (file)
--- 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);