]> mj.ucw.cz Git - ursary.git/blobdiff - ursaryd.c
Nocturn: Do not write to a missing USB device
[ursary.git] / ursaryd.c
index c8a87a2ed3fda4d207ff47470214a6ea51d9a15e..eaa56542b46eec0f4ce81333ec81dcc414d8bce2 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
 
 #include "ursaryd.h"
 
+/*
+ *     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       -               -               -
+ *     3       -               -               -
+ *     4       MPD             mute            play/pause/stop
+ *     5       Albireo         mute            -
+ *     6       Ogion           mute            -
+ *     7       Ursula          mute            -
+ *
+ *     center  all sinks
+ *     slider  -
+ */
+
 /*** Sink controls ***/
 
 static double volume_from_pa(pa_volume_t vol)
@@ -225,6 +242,200 @@ static void update_client_from_button(int button, int on)
     }
 }
 
+static int find_touched_client(void)
+{
+  int touched = -1;
+
+  for (uns i=0; i < NUM_CLIENTS; i++)
+    if (noct_rotary_touched[client_map[i].rotary])
+      {
+       if (touched >= 0)
+         return -1;
+       touched = i;
+      }
+  return touched;
+}
+
+/*** Default sink controls ***/
+
+static const char *get_client_sink(int i)
+{
+  const char *sink = NULL;
+
+  CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
+    if (s->noct_client_idx == i)
+      {
+       struct pulse_sink *sk = (s->sink_idx >= 0) ? pulse_sink_by_idx(s->sink_idx) : NULL;
+       const char *ss = sk ? sk->name : NULL;
+       if (!sink)
+         sink = ss;
+       else if (strcmp(sink, ss))
+         sink = "?";
+      }
+  return sink ? : "?";
+}
+
+static void update_default_sink(void)
+{
+  int i = find_touched_client();
+  const char *sink;
+  if (i >= 0)
+    sink = get_client_sink(i);
+  else
+    sink = pulse_default_sink_name ? : "?";
+
+  if (!strcmp(sink, "ursarium"))
+    {
+      noct_set_button(8, 1);
+      noct_set_button(9, 0);
+    }
+  else if (!strcmp(sink, "catarium"))
+    {
+      noct_set_button(8, 0);
+      noct_set_button(9, 1);
+    }
+  else
+    {
+      noct_set_button(8, 0);
+      noct_set_button(9, 0);
+    }
+}
+
+static void update_default_sink_from_button(int button, int on)
+{
+  if (!on)
+    return;
+
+  int i = find_touched_client();
+  const char *sink;
+  if (i >= 0)
+    sink = get_client_sink(i);
+  else
+    sink = pulse_default_sink_name ? : "?";
+
+  const char *switch_to = NULL;
+  if (button == 8)
+    {
+      if (!strcmp(sink, "ursarium"))
+       switch_to = "burrow";
+      else
+       switch_to = "ursarium";
+    }
+  else if (button == 9)
+    {
+      if (!strcmp(sink, "catarium"))
+       switch_to = "burrow";
+      else
+       switch_to = "catarium";
+    }
+
+  if (!switch_to)
+    return;
+
+  if (i >= 0)
+    {
+      struct pulse_sink *sk = pulse_sink_by_name(switch_to);
+      if (!sk)
+       return;
+
+      CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
+        if (s->noct_client_idx == i)
+         {
+           DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
+           pulse_sink_input_move(s->idx, sk->idx);
+         }
+    }
+  else
+    {
+      DBG("Switching default sink to %s", switch_to);
+      pulse_server_set_default_sink(switch_to);
+    }
+}
+
+/*** MPD controls ***/
+
+static bool mpd_flash_state;
+
+static void mpd_flash_timeout(struct main_timer *t)
+{
+  mpd_flash_state ^= 1;
+  noct_set_button(12, mpd_flash_state);
+  timer_add_rel(t, 500);
+}
+
+static struct main_timer mpd_flash_timer = {
+  .handler = mpd_flash_timeout,
+};
+
+static void update_mpd(void)
+{
+  const char *state = mpd_get_player_state();
+  if (!strcmp(state, "play"))
+    {
+      noct_set_button(12, 1);
+      timer_del(&mpd_flash_timer);
+    }
+  else if (!strcmp(state, "pause"))
+    {
+      if (!timer_is_active(&mpd_flash_timer))
+       {
+         mpd_flash_state = 1;
+         mpd_flash_timeout(&mpd_flash_timer);
+       }
+    }
+  else
+    {
+      noct_set_button(12, 0);
+      timer_del(&mpd_flash_timer);
+    }
+}
+
+static void mpd_button_timeout(struct main_timer *t)
+{
+  DBG("MPD stop");
+  timer_del(t);
+  mpd_stop();
+}
+
+static void update_mpd_from_button(int button UNUSED, int on)
+{
+  static struct main_timer mpd_button_timer = {
+    .handler = mpd_button_timeout,
+  };
+
+  const char *state = mpd_get_player_state();
+
+  if (!on)
+    {
+      if (timer_is_active(&mpd_button_timer))
+       {
+         timer_del(&mpd_button_timer);
+         if (!strcmp(state, "play"))
+           {
+             DBG("MPD pause");
+             mpd_pause(1);
+           }
+         else if (!strcmp(state, "pause"))
+           {
+             DBG("MPD resume");
+             mpd_pause(0);
+           }
+       }
+      return;
+    }
+
+  if (!strcmp(state, "stop"))
+    {
+      DBG("MPD play");
+      mpd_play();
+    }
+  else
+    {
+      DBG("MPD starting button timer");
+      timer_add_rel(&mpd_button_timer, 1000);
+    }
+}
+
 /*** Main update routines ***/
 
 static struct main_timer update_timer;
@@ -263,11 +474,15 @@ static void do_update(struct main_timer *t)
     }
 
   DBG("## UPDATE");
+#ifdef LOCAL_DEBUG
   pulse_dump();
+#endif
 
   update_ring_from_sink(0, "ursarium");
   update_ring_from_sink(1, "catarium");
   update_clients();
+  update_default_sink();
+  update_mpd();
 }
 
 void schedule_update(void)
@@ -316,11 +531,31 @@ void notify_button(int button, int on)
     case 1:
       update_sink_mute_from_button(on, "catarium");
       break;
+    case 8:
+    case 9:
+      update_default_sink_from_button(button, on);
+      break;
+    case 12:
+      update_mpd_from_button(button, on);
+      break;
     default:
       update_client_from_button(button, on);
     }
 }
 
+void notify_touch(int rotary, int on UNUSED)
+{
+  if (pulse_state != PS_ONLINE)
+    {
+      DBG("## NOTIFY: Pulse is not online");
+      return;
+    }
+
+  // Rotary touches switch meaning of LEDs, this is handled inside display updates
+  if (rotary >= 4 && rotary < 8)
+    schedule_update();
+}
+
 /*** Main entry point ***/
 
 int main(int argc UNUSED, char **argv)
@@ -331,6 +566,7 @@ int main(int argc UNUSED, char **argv)
 
   noct_init();
   pulse_init();
+  mpd_init();
 
   msg(L_DEBUG, "Entering main loop");
   main_loop();