]> mj.ucw.cz Git - ursary.git/blobdiff - ursaryd.c
Better IR control of lights
[ursary.git] / ursaryd.c
index 857ca5f87525e61997f7260cfcbc200a95cee0af..1d0e5ff4ed56ea8881a1506f44f1a4908d5f048b 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
@@ -1,7 +1,7 @@
 /*
- *     The Ursary Audio Controls
+ *     The Ursary Control Panel
  *
- *     (c) 2014 Martin Mares <mj@ucw.cz>
+ *     (c) 2014-2023 Martin Mares <mj@ucw.cz>
  */
 
 #undef LOCAL_DEBUG
@@ -13,7 +13,9 @@
 #include <ucw/mainloop.h>
 #include <ucw/opt.h>
 #include <ucw/stkstring.h>
+#include <ucw/string.h>
 
+#include <math.h>
 #include <signal.h>
 #include <stdio.h>
 #include <string.h>
 #include <syslog.h>
 
 #include "ursaryd.h"
+#include "usb.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       sink Compress   -               dtto
- *     3       -               -               -
- *     4       MPD             mute            play/pause/stop
- *     5       Albireo         mute            -
- *     6       Ogion           mute            -
- *     7       Ursula          mute            -
+ *             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *     0       sink PCH        mute            switch to PCH
+ *     1       sink BT         mute            switch to BT
+ *     2       ceil brightness mic non-mute    ceiling lights on
+ *     3       desk brightness -               desk lights on
+ *     4       Albireo MPD     mute            MPD play/pause
+ *     5       Albireo MPV     mute            MPD stop
+ *     6       Albireo Zoom    mute            MPD prev
+ *     7       eveyrhing else  mute            MPD next
  *
- *     center  all sinks
- *     slider  -
+ *     center  rainbow brightness
+ *     slider  light color temperature
  */
 
+#define PCH_SINK "alsa_output.pci-0000_07_00.6.analog-stereo"
+#define BT_SINK "bluez_sink.CC_98_8B_D0_8C_06.a2dp_sink"
+#define LOGI_SOURCE "alsa_input.usb-046d_Logitech_Webcam_C925e_EF163C5F-02.analog-stereo"
+
 /*** Sink controls ***/
 
 static double volume_from_pa(pa_volume_t vol)
@@ -106,44 +114,48 @@ static void update_sink_mute_from_button(int on, const char *sink_name)
 /*** Client controls ***/
 
 struct client_map {
-  int rotary;
+  int group;
   const char *client;
   const char *host;
-  double range;
 };
 
 static struct client_map client_map[] = {
-  { 4, "Music Player Daemon",  "albireo",      1.5 },
-  { 5, NULL,                   "albireo",      1.5 },
-  { 6, NULL,                   "ogion",        1.5 },
-  { 7, NULL,                   "ursula",       1.5 },
+  { 4, "Music Player Daemon",  "albireo",      },
+  { 5, "mpv",                  "albireo",      },
+  { 6, "ZOOM VoiceEngine",     "albireo",      },
+  { 7, NULL,                   NULL,           },
 };
 
-#define NUM_CLIENTS ARRAY_SIZE(client_map)
+#define CLIENT_MAP_SIZE ARRAY_SIZE(client_map)
+
+struct group_config {
+  bool enabled;
+  double range;
+};
 
-struct client_state {
+struct group_state {
   double volume;
   bool have_muted[2];
 };
 
-static struct client_state client_state[NUM_CLIENTS];
+#define NUM_GROUPS 9
 
-static int find_client_by_rotary(int rotary)
-{
-  uns i;
-  for (i=0; i < NUM_CLIENTS; i++)
-    if (client_map[i].rotary == rotary)
-      return i;
-  return -1;
-}
+static struct group_config group_config[NUM_GROUPS] = {
+  [4] = { .enabled = 1, .range = 1.5 },
+  [5] = { .enabled = 1, .range = 1.5 },
+  [6] = { .enabled = 1, .range = 1.5 },
+  [7] = { .enabled = 1, .range = 1.5 },
+};
+
+static struct group_state group_state[NUM_GROUPS];
 
-static void calc_clients(void)
+static void calc_groups(void)
 {
-  bzero(client_state, sizeof(client_state));
+  bzero(group_state, sizeof(group_state));
 
   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
     {
-      s->noct_client_idx = -1;
+      s->noct_group_idx = -1;
 
       if (s->client_idx < 0 || s->sink_idx < 0)
        continue;
@@ -152,67 +164,74 @@ static void calc_clients(void)
       if (!c)
        continue;
 
-      for (uns i=0; i < NUM_CLIENTS; i++)
+      for (uns i=0; i < CLIENT_MAP_SIZE; i++)
        {
          struct client_map *cm = &client_map[i];
-         struct client_state *cs = &client_state[i];
          if ((!cm->client || !strcmp(cm->client, c->name)) &&
              (!cm->host || !strcmp(cm->host, c->host)))
            {
-             // DBG("@@ Client #%d, sink input #%d -> rotary %d", s->client_idx, s->idx, cm->rotary);
-             s->noct_client_idx = i;
-             cs->volume = MAX(cs->volume, s->volume);
-             cs->have_muted[!!s->mute] = 1;
+             int g = cm->group;
+             struct group_state *gs = &group_state[g];
+             DBG("@@ Client #%d, sink input #%d -> group %d", s->client_idx, s->idx, g);
+             s->noct_group_idx = g;
+             gs->volume = MAX(gs->volume, s->volume);
+             gs->have_muted[!!s->mute] = 1;
              break;
            }
        }
     }
 }
 
-static void update_clients(void)
+static void update_groups(void)
 {
-  calc_clients();
+  calc_groups();
 
-  for (uns i=0; i < NUM_CLIENTS; i++)
+  for (uns i=0; i < NUM_GROUPS; i++)
     {
-      struct client_map *cm = &client_map[i];
-      struct client_state *cs = &client_state[i];
-      if (!cs->have_muted[0] && !cs->have_muted[1])
+      struct group_config *gc = &group_config[i];
+      struct group_state *gs = &group_state[i];
+      if (!gc->enabled)
+       continue;
+
+      DBG("@@ Group #%d: mute=%d/%d volume=%.3f/%.3f", i, gs->have_muted[0], gs->have_muted[1], volume_from_pa(gs->volume), gc->range);
+
+      if (!gs->have_muted[0] && !gs->have_muted[1])
        {
-         noct_set_ring(cm->rotary, RING_MODE_LEFT, 0);
-         noct_set_button(cm->rotary, 0);
+         noct_set_ring(i, RING_MODE_LEFT, 0);
+         noct_set_button(i, 0);
        }
-      else if (!cs->have_muted[0])
+      else if (!gs->have_muted[0])
        {
-         noct_set_ring(cm->rotary, RING_MODE_SINGLE_ON, 0x7f);
-         noct_set_button(cm->rotary, 1);
+         noct_set_ring(i, RING_MODE_SINGLE_ON, 0x7f);
+         noct_set_button(i, 1);
        }
       else
        {
-         double vol = CLAMP(volume_from_pa(cs->volume), 0, cm->range);
-         int val = 0x7f * vol / cm->range;
+         double vol = CLAMP(volume_from_pa(gs->volume), 0, gc->range);
+         int val = 0x7f * vol / gc->range;
          val = CLAMP(val, 12, 0x7f);
-         noct_set_ring(cm->rotary, RING_MODE_LEFT, val);
-         noct_set_button(cm->rotary, 0);
+         noct_set_ring(i, RING_MODE_LEFT, val);
+         noct_set_button(i, 0);
        }
     }
 }
 
-static void update_client_from_rotary(int rotary, int delta)
+static void update_group_from_rotary(int i, int delta)
 {
-  int i = find_client_by_rotary(rotary);
-  if (i < 0)
+  if (i >= NUM_GROUPS)
+    return;
+  struct group_config *gc = &group_config[i];
+  struct group_state *gs = &group_state[i];
+  if (!gc->enabled)
     return;
-  struct client_map *cm = &client_map[i];
-  struct client_state *cs = &client_state[i];
 
-  calc_clients();
-  double vol = volume_from_pa(cs->volume) + delta*0.02;
-  pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, cm->range));
+  calc_groups();
+  double vol = volume_from_pa(gs->volume) + delta*0.02;
+  pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, gc->range));
 
   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
     {
-      if (s->noct_client_idx == i && s->volume != pavol)
+      if (s->noct_group_idx == i && s->volume != pavol)
        {
          DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
          pa_cvolume cvol;
@@ -222,24 +241,25 @@ static void update_client_from_rotary(int rotary, int delta)
     }
 }
 
-static void update_client_from_button(int button, int on)
+static void update_group_from_button(int i, int on)
 {
-  if (button >= 8 || !on)
+  if (!on)
     return;
-
-  int i = find_client_by_rotary(button);
-  if (i < 0)
+  if (i >= NUM_GROUPS)
+    return;
+  struct group_config *gc = &group_config[i];
+  struct group_state *gs = &group_state[i];
+  if (!gc->enabled)
     return;
-  struct client_state *cs = &client_state[i];
 
-  calc_clients();
-  if (!cs->have_muted[0] && !cs->have_muted[1])
+  calc_groups();
+  if (!gs->have_muted[0] && !gs->have_muted[1])
     return;
-  uns mute = !cs->have_muted[1];
+  uns mute = !gs->have_muted[1];
 
   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
     {
-      if (s->noct_client_idx == i)
+      if (s->noct_group_idx == i)
        {
          DBG("@@ Client #%d, sink input #%d: setting mute=%u", s->client_idx, s->idx, mute);
          pulse_sink_input_set_mute(s->idx, mute);
@@ -251,8 +271,8 @@ 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])
+  for (uns i=0; i < NUM_GROUPS; i++)
+    if (group_config[i].enabled && noct_rotary_touched[i])
       {
        if (touched >= 0)
          return -1;
@@ -268,7 +288,7 @@ 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)
+    if (s->noct_group_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;
@@ -289,29 +309,20 @@ static void update_default_sink(void)
   else
     sink = pulse_default_sink_name ? : "?";
 
-  if (!strcmp(sink, "ursarium"))
+  if (!strcmp(sink, PCH_SINK))
     {
       noct_set_button(8, 1);
       noct_set_button(9, 0);
-      noct_set_button(10, 0);
     }
-  else if (!strcmp(sink, "catarium"))
+  else if (!strcmp(sink, BT_SINK))
     {
       noct_set_button(8, 0);
       noct_set_button(9, 1);
-      noct_set_button(10, 0);
-    }
-  else if (!strcmp(sink, "compress"))
-    {
-      noct_set_button(8, 0);
-      noct_set_button(9, 0);
-      noct_set_button(10, 1);
     }
   else
     {
       noct_set_button(8, 0);
       noct_set_button(9, 0);
-      noct_set_button(10, 0);
     }
 }
 
@@ -321,34 +332,19 @@ static void update_default_sink_from_button(int button, int on)
     return;
 
   int i = find_touched_client();
+#if 0
   const char *sink;
   if (i >= 0)
     sink = get_client_sink(i);
   else
     sink = pulse_default_sink_name ? : "?";
+#endif
 
   const char *switch_to = NULL;
   if (button == 8)
-    {
-      if (!strcmp(sink, "ursarium"))
-       switch_to = "burrow";
-      else
-       switch_to = "ursarium";
-    }
+    switch_to = PCH_SINK;
   else if (button == 9)
-    {
-      if (!strcmp(sink, "catarium"))
-       switch_to = "burrow";
-      else
-       switch_to = "catarium";
-    }
-  else if (button == 10)
-    {
-      if (!strcmp(sink, "compress"))
-       switch_to = "burrow";
-      else
-       switch_to = "compress";
-    }
+    switch_to = BT_SINK;
 
   if (!switch_to)
     return;
@@ -360,7 +356,7 @@ static void update_default_sink_from_button(int button, int on)
        return;
 
       CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
-        if (s->noct_client_idx == i)
+        if (s->noct_group_idx == i)
          {
            DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
            pulse_sink_input_move(s->idx, sk->idx);
@@ -373,6 +369,38 @@ static void update_default_sink_from_button(int button, int on)
     }
 }
 
+/*** Source mute controls ***/
+
+static void update_source_buttons(void)
+{
+  struct pulse_source *source = pulse_source_by_name(LOGI_SOURCE);
+  if (!source)
+    return;
+
+#if 0  // Disabled for now
+  // if (source->suspended || source->mute)
+  if (source->mute)
+    noct_set_button(2, 0);
+  else
+    noct_set_button(2, 1);
+#endif
+}
+
+static void update_source_mute_from_button(int on, const char *source_name)
+{
+  if (!on)
+    return;
+
+  struct pulse_source *s = pulse_source_by_name(source_name);
+  if (!s)
+    return;
+
+#if 0
+  DBG("## Setting mute of source %s to %d", s->name, !s->mute);
+  pulse_source_set_mute(s->idx, !s->mute);
+#endif
+}
+
 /*** MPD controls ***/
 
 static bool mpd_flash_state;
@@ -457,6 +485,154 @@ static void update_mpd_from_button(int button UNUSED, int on)
     }
 }
 
+/*** Lights ***/
+
+static bool lights_on[2];
+static double lights_brightness[2];
+static double lights_temperature[2];
+static timestamp_t lights_last_update[2];
+
+static void update_lights(void)
+{
+  for (uint ch=0; ch < 2; ch++)
+    {
+      if (lights_on[ch])
+       {
+         noct_set_ring(3-ch, RING_MODE_LEFT, lights_brightness[ch] * 127);
+         noct_set_button(11-ch, 1);
+       }
+      else
+       {
+         noct_set_ring(3-ch, RING_MODE_LEFT, 0);
+         noct_set_button(11-ch, 0);
+       }
+    }
+}
+
+static void send_lights(int ch)
+{
+  DBG("Lights[%d]: on=%d bri=%.3f temp=%.3f", ch, lights_on[ch], lights_brightness[ch], lights_temperature[ch]);
+  double b = lights_on[ch] ? lights_brightness[ch] : 0;
+  double t = lights_on[ch] ? lights_temperature[ch] : 0;
+  char topic[100], val[100];
+  snprintf(topic, sizeof(topic), "burrow/lights/catarium/%s", (ch ? "top" : "bottom"));
+  snprintf(val, sizeof(val), "%.3f %.3f", b, t);
+  mqtt_publish(topic, val);
+  lights_last_update[ch] = main_get_now();
+  update_lights();
+}
+
+static void update_lights_from_rotary(int ch, int delta)
+{
+  if (lights_on[ch])
+    lights_brightness[ch] = CLAMP(lights_brightness[ch] + 0.015*delta*abs(delta), 0., 1.);
+  send_lights(ch);
+}
+
+static void update_lights_from_slider(int value)
+{
+  lights_temperature[0] = value / 127.;
+  lights_temperature[1] = value / 127.;
+  send_lights(0);
+  send_lights(1);
+}
+
+static void lights_button_timeout(struct main_timer *t)
+{
+  int ch = (uintptr_t) t->data;
+  DBG("Lights[%d]: Full throttle!", ch);
+  timer_del(t);
+  lights_on[ch] = 1;
+  lights_brightness[ch] = 1;
+  send_lights(ch);
+}
+
+static void update_lights_from_button(int ch, int on)
+{
+  static struct main_timer lights_button_timer[2] = {{
+    .handler = lights_button_timeout,
+    .data = (void *)(uintptr_t) 0,
+  },{
+    .handler = lights_button_timeout,
+    .data = (void *)(uintptr_t) 1,
+  }};
+
+  if (on)
+    timer_add_rel(&lights_button_timer[ch], 500);
+  else if (timer_is_active(&lights_button_timer[ch]))
+    {
+      timer_del(&lights_button_timer[ch]);
+      lights_on[ch] = !lights_on[ch];
+      send_lights(ch);
+    }
+}
+
+static void update_lights_from_ir(int ch, int dir)
+{
+  if (lights_on[ch])
+    lights_brightness[ch] = CLAMP(lights_brightness[ch] + 0.07*dir, 0., 1.);
+  else if (dir > 0)
+    {
+      lights_on[ch] = 1;
+      lights_brightness[ch] = 1;
+    }
+  else
+    {
+      lights_on[ch] = 1;
+      lights_brightness[ch] = 0.05;
+    }
+  send_lights(ch);
+}
+
+static void update_lights_on_off_ir(int ch)
+{
+  lights_on[ch] ^= 1;
+  send_lights(ch);
+}
+
+static void update_lights_temp_ir(void)
+{
+  if (!lights_on[0] && !lights_on[1])
+    return;
+
+  double t = (lights_temperature[0] + lights_temperature[1]) / 2;
+  if (t >= 0.66)
+    t = 0;
+  else if (t < 0.33)
+    t = 0.5;
+  else
+    t = 1;
+  lights_temperature[0] = lights_temperature[1] = t;
+
+  send_lights(0);
+  send_lights(1);
+}
+
+/*** Rainbow ***/
+
+static double rainbow_brightness;
+static timestamp_t rainbow_last_update;
+
+static void update_rainbow(void)
+{
+  noct_set_ring(8, RING_MODE_LEFT, rainbow_brightness * 127);
+}
+
+static void send_rainbow(void)
+{
+  char val[100];
+  snprintf(val, sizeof(val), "%.3f", rainbow_brightness);
+  mqtt_publish("burrow/lights/rainbow/brightness", val);
+  rainbow_last_update = main_get_now();
+  update_rainbow();
+}
+
+static void update_rainbow_from_rotary(int delta)
+{
+  rainbow_brightness = CLAMP(rainbow_brightness + 0.015*delta*abs(delta), 0., 1.);
+  send_rainbow();
+}
+
 /*** Main update routines ***/
 
 static struct main_timer update_timer;
@@ -526,7 +702,7 @@ static void do_update(struct main_timer *t)
   if (!want_sleep)
     last_touch_time = main_get_now();
   timestamp_t since_touch = last_touch_time ? main_get_now() - last_touch_time : 0;
-  timestamp_t sleep_in = 5000;
+  timestamp_t sleep_in = 30000;
   if (since_touch >= sleep_in)
     {
       DBG("UPDATE: Sleeping");
@@ -555,11 +731,14 @@ static void do_update(struct main_timer *t)
     }
 
   // Everything normal
-  update_ring_from_sink(0, "ursarium");
-  update_ring_from_sink(1, "catarium");
-  update_clients();
+  update_ring_from_sink(0, PCH_SINK);
+  update_ring_from_sink(1, BT_SINK);
+  update_groups();
   update_default_sink();
+  update_source_buttons();
   update_mpd();
+  update_lights();
+  update_rainbow();
 }
 
 void schedule_update(void)
@@ -593,17 +772,25 @@ void notify_rotary(int rotary, int delta)
   switch (rotary)
     {
     case 0:
-      update_sink_from_rotary(delta, "ursarium");
+      update_sink_from_rotary(delta, PCH_SINK);
       break;
     case 1:
-      update_sink_from_rotary(delta, "catarium");
+      update_sink_from_rotary(delta, BT_SINK);
+      break;
+    case 2:
+      update_lights_from_rotary(1, delta);
+      break;
+    case 3:
+      update_lights_from_rotary(0, delta);
       break;
     case 8:
-      update_sink_from_rotary(delta, "ursarium");
-      update_sink_from_rotary(delta, "catarium");
+      update_rainbow_from_rotary(delta);
+      break;
+    case 9:
+      update_lights_from_slider(delta);
       break;
     default:
-      update_client_from_rotary(rotary, delta);
+      update_group_from_rotary(rotary, delta);
     }
 }
 
@@ -615,25 +802,45 @@ void notify_button(int button, int on)
   switch (button)
     {
     case 0:
-      update_sink_mute_from_button(on, "ursarium");
+      update_sink_mute_from_button(on, PCH_SINK);
       break;
     case 1:
-      update_sink_mute_from_button(on, "catarium");
+      update_sink_mute_from_button(on, BT_SINK);
+      break;
+    case 2:
+      update_source_mute_from_button(on, LOGI_SOURCE);
       break;
     case 8:
     case 9:
-    case 10:
       update_default_sink_from_button(button, on);
       break;
+    case 10:
+      update_lights_from_button(1, on);
+      break;
+    case 11:
+      update_lights_from_button(0, on);
+      break;
     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_client_from_button(button, on);
+      update_group_from_button(button, on);
     }
 }
 
-void notify_touch(int rotary, int on UNUSED)
+void notify_touch(int rotary UNUSED, int on UNUSED)
 {
   if (!prepare_notify())
     return;
@@ -643,9 +850,100 @@ void notify_touch(int rotary, int on UNUSED)
     schedule_update();
 }
 
+static void notify_ir(const char *key)
+{
+  DBG("Received IR key %s", key);
+
+  // Lights
+  if (!strcmp(key, "preset+"))
+    update_lights_from_ir(1, 1);
+  else if (!strcmp(key, "preset-"))
+    update_lights_from_ir(1, -1);
+  else if (!strcmp(key, "tuning-up"))
+    update_lights_from_ir(0, 1);
+  else if (!strcmp(key, "tuning-down"))
+    update_lights_from_ir(0, -1);
+  else if (!strcmp(key, "band"))
+    update_lights_on_off_ir(1);
+  else if (!strcmp(key, "fm-mode"))
+    update_lights_on_off_ir(0);
+  else if (!strcmp(key, "dimmer"))
+    update_lights_temp_ir();
+
+  // Player
+  else if (!strcmp(key, "play"))
+    mpd_play();
+  else if (!strcmp(key, "stop"))
+    mpd_stop();
+  else if (!strcmp(key, "pause"))
+    mpd_pause(1);
+  else if (!strcmp(key, "prev-song"))
+    mpd_prev();
+  else if (!strcmp(key, "next-song"))
+    mpd_next();
+  else if (!strcmp(key, "rewind"))
+    update_sink_from_rotary(-2, PCH_SINK);
+  else if (!strcmp(key, "ffwd"))
+    update_sink_from_rotary(2, PCH_SINK);
+}
+
+void notify_mqtt(const char *topic, const char *val)
+{
+  const char blc[] = "burrow/lights/catarium/";
+  if (str_has_prefix(topic, blc))
+    {
+      topic += strlen(blc);
+      int ch;
+      if (!strcmp(topic, "top"))
+       ch = 1;
+      else if (!strcmp(topic, "bottom"))
+       ch = 0;
+      else
+       return;
+
+      double b, t;
+      if (sscanf(val, "%lf %lf", &b, &t) != 2)
+       return;
+
+      timestamp_t now = main_get_now();
+      if (!lights_last_update[ch] || lights_last_update[ch] + 1000 < now)
+       {
+         DBG("Received foreign light settings");
+         if (!b)
+           lights_on[ch] = 0;
+         else
+           {
+             lights_on[ch] = 1;
+             lights_brightness[ch] = b;
+             lights_temperature[ch] = t;
+           }
+         update_lights();
+       }
+    }
+
+  if (!strcmp(topic, "burrow/lights/rainbow/brightness"))
+    {
+      double b;
+      if (sscanf(val, "%lf", &b) == 1 && b >= 0 && b <= 1)
+       {
+         timestamp_t now = main_get_now();
+         if (!rainbow_last_update || rainbow_last_update + 1000 < now)
+           {
+             DBG("Received foreign rainbow settings");
+             rainbow_brightness = b;
+             update_rainbow();
+           }
+       }
+    }
+
+  if (!strcmp(topic, "burrow/control/catarium-ir"))
+    notify_ir(val);
+}
+
 /*** Main entry point ***/
 
 static int debug;
+static int no_fork;
 
 static struct opt_section options = {
   OPT_ITEMS {
@@ -654,6 +952,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
   }
 };
@@ -668,9 +967,11 @@ static void daemon_body(struct daemon_params *dp)
   main_init();
   update_timer.handler = do_update;
 
+  usb_init();
   noct_init();
   pulse_init();
   mpd_init();
+  mqtt_init();
 
   static struct main_signal term_sig = {
     .signum = SIGTERM,
@@ -691,7 +992,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",
   };