]> mj.ucw.cz Git - ursary.git/blobdiff - ursaryd.c
Document button assignment changes
[ursary.git] / ursaryd.c
index d5b498f2afe3dfa7db2d11c89bd26bfec1bd7df7..943348843535ad8a3ebd6acd4c3d06eea7b1c45d 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--2020 Martin Mares <mj@ucw.cz>
  */
 
 #undef LOCAL_DEBUG
  *     Map of all controls
  *
  *             rotary          red button      green button
- *     0       sink Ursarium   mute            select as default (or assign to client selected by touch)
- *     1       sink Catarium   mute            dtto
- *     2       sink Compress   -               dtto
+ *     0       sink PCH        mute            use headphones
+ *     1       -               -               -
+ *     2       -               -               -
  *     3       -               -               -
- *     4       MPD             mute            play/pause/stop
- *     5       Albireo         mute            -
- *     6       Ogion           mute            -
- *     7       Ursula/Havrana  mute            -
+ *     4       MPD             mute            MPD play/pause
+ *     5       Albireo MPV     mute            MPD stop
+ *     6       Albireo other   mute            MPD prev
+ *     7       other machines  mute            MPD next
  *
- *     center  all sinks
+ *     center  -
  *     slider  -
  */
 
+#define PCH_SINK "alsa_output.pci-0000_00_1f.3.analog-stereo"
+
 /*** Sink controls ***/
 
 static double volume_from_pa(pa_volume_t vol)
@@ -73,6 +75,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 +117,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 {
@@ -113,10 +144,9 @@ struct client_map {
 
 static struct client_map client_map[] = {
   { 4, "Music Player Daemon",  "albireo",      },
-  { 5, NULL,                   "albireo",      },
-  { 6, NULL,                   "ogion",        },
-  { 7, NULL,                   "ursula",       },
-  { 7, NULL,                   "havrana",      },
+  { 5, "mpv",                  "albireo",      },
+  { 6, NULL,                   "albireo",      },
+  { 7, NULL,                   NULL,           },
 };
 
 #define CLIENT_MAP_SIZE ARRAY_SIZE(client_map)
@@ -260,6 +290,8 @@ static void update_group_from_button(int i, int on)
     }
 }
 
+#if 0  // Not used at the moment
+
 static int find_touched_client(void)
 {
   int touched = -1;
@@ -274,8 +306,12 @@ static int find_touched_client(void)
   return touched;
 }
 
+#endif
+
 /*** Default sink controls ***/
 
+#if 0  // Not mapped to any button at the moment
+
 static const char *get_client_sink(int i)
 {
   const char *sink = NULL;
@@ -386,6 +422,8 @@ static void update_default_sink_from_button(int button, int on)
     }
 }
 
+#endif
+
 /*** MPD controls ***/
 
 static bool mpd_flash_state;
@@ -568,10 +606,12 @@ static void do_update(struct main_timer *t)
     }
 
   // Everything normal
-  update_ring_from_sink(0, "ursarium");
-  update_ring_from_sink(1, "catarium");
+  update_ring_from_sink(0, PCH_SINK);
+  update_button_from_port(8, PCH_SINK, "analog-output-headphones");
   update_groups();
+#if 0
   update_default_sink();
+#endif
   update_mpd();
 }
 
@@ -606,14 +646,7 @@ void notify_rotary(int rotary, int delta)
   switch (rotary)
     {
     case 0:
-      update_sink_from_rotary(delta, "ursarium");
-      break;
-    case 1:
-      update_sink_from_rotary(delta, "catarium");
-      break;
-    case 8:
-      update_sink_from_rotary(delta, "ursarium");
-      update_sink_from_rotary(delta, "catarium");
+      update_sink_from_rotary(delta, PCH_SINK);
       break;
     default:
       update_group_from_rotary(rotary, delta);
@@ -628,19 +661,32 @@ void notify_button(int button, int on)
   switch (button)
     {
     case 0:
-      update_sink_mute_from_button(on, "ursarium");
-      break;
-    case 1:
-      update_sink_mute_from_button(on, "catarium");
+      update_sink_mute_from_button(on, PCH_SINK);
       break;
     case 8:
+      update_port_from_button(on, PCH_SINK, "analog-output-lineout", "analog-output-headphones");
+      break;
+#if 0
     case 9:
     case 10:
       update_default_sink_from_button(button, on);
       break;
+#endif
     case 12:
       update_mpd_from_button(button, on);
       break;
+    case 13:
+      if (on)
+       mpd_stop();
+      break;
+    case 14:
+      if (on)
+       mpd_prev();
+      break;
+    case 15:
+      if (on)
+       mpd_next();
+      break;
     default:
       update_group_from_button(button, on);
     }
@@ -659,6 +705,7 @@ void notify_touch(int rotary, int on UNUSED)
 /*** Main entry point ***/
 
 static int debug;
+static int no_fork;
 
 static struct opt_section options = {
   OPT_ITEMS {
@@ -667,6 +714,7 @@ static struct opt_section options = {
     OPT_HELP("Options:"),
     OPT_HELP_OPTION,
     OPT_BOOL('d', "debug", debug, 0, "\tEnable debugging mode (no fork etc.)"),
+    OPT_BOOL(0, "no-fork", no_fork, 0, "\tDo not fork\n"),
     OPT_END
   }
 };
@@ -704,7 +752,7 @@ int main(int argc UNUSED, char **argv)
   unsetenv("HOME");
 
   struct daemon_params dp = {
-    .flags = (debug ? DAEMON_FLAG_SIMULATE : 0),
+    .flags = ((debug || no_fork) ? DAEMON_FLAG_SIMULATE : 0),
     .pid_file = "/run/ursaryd.pid",
     .run_as_user = "ursary",
   };