* (c) 2014 Martin Mares <mj@ucw.cz>
*/
-#define LOCAL_DEBUG
+#undef LOCAL_DEBUG
#include <ucw/lib.h>
#include <ucw/clists.h>
+#include <ucw/daemon.h>
+#include <ucw/log.h>
#include <ucw/mainloop.h>
+#include <ucw/opt.h>
#include <ucw/stkstring.h>
+#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <syslog.h>
#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 sink Compress - dtto
+ * 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)
};
static struct client_map client_map[] = {
- { 4, "Music Player Daemon", "albireo", 1 },
- { 5, "MPlayer", NULL, 1 },
- { 6, NULL, "ogion", 1 },
- { 7, NULL, "ursula", 1 },
+ { 4, "Music Player Daemon", "albireo", 1.5 },
+ { 5, NULL, "albireo", 1.5 },
+ { 6, NULL, "ogion", 1.5 },
+ { 7, NULL, "ursula", 1.5 },
};
#define NUM_CLIENTS ARRAY_SIZE(client_map)
}
}
+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);
+ noct_set_button(10, 0);
+ }
+ else if (!strcmp(sink, "catarium"))
+ {
+ 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);
+ }
+}
+
+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";
+ }
+ else if (button == 10)
+ {
+ if (!strcmp(sink, "compress"))
+ switch_to = "burrow";
+ else
+ switch_to = "compress";
+ }
+
+ 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;
+static timestamp_t last_touch_time;
+
+enum update_state {
+ US_OFFLINE,
+ US_ONLINE,
+ US_SLEEPING,
+ US_PULSE_DEAD,
+};
+
+static enum update_state update_state;
+
+static bool want_sleep_p(void)
+{
+ CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
+ if (!s->suspended)
+ return 0;
+ return 1;
+}
static void do_update(struct main_timer *t)
{
+ DBG("## UPDATE in state %u", update_state);
timer_del(t);
+
+ // Nocturn dead?
if (!noct_is_ready())
{
DBG("## UPDATE: Nocturn is not ready");
+ update_state = US_OFFLINE;
return;
}
+ else if (update_state == US_OFFLINE)
+ {
+ DBG("## UPDATE: Going online");
+ update_state = US_ONLINE;
+ }
- static bool dead;
- if (pulse_state != PS_ONLINE)
+ // Pulse dead?
+ if (!pulse_is_ready())
{
DBG("## UPDATE: Pulse is not online");
- for (int i=0; i<=8; i++)
- noct_set_ring(i, RING_MODE_LEFT, 0);
- for (int i=0; i<8; i++)
+ if (update_state != US_PULSE_DEAD)
{
- noct_set_button(i, 1);
- noct_set_button(i+8, 0);
+ update_state = US_PULSE_DEAD;
+ noct_clear();
+ for (int i=0; i<8; i++)
+ noct_set_button(i, 1);
}
- dead = 1;
return;
}
- if (dead)
+ else if (update_state == US_PULSE_DEAD)
{
DBG("## UPDATE: Waking up from the dead");
- for (int i=0; i<=8; i++)
- noct_set_ring(i, RING_MODE_LEFT, 0);
- for (int i=0; i<16; i++)
- noct_set_button(i, 0);
- dead = 0;
+ update_state = US_ONLINE;
+ noct_clear();
}
- DBG("## UPDATE");
+#ifdef LOCAL_DEBUG
pulse_dump();
+#endif
+
+ // Sleeping?
+ bool want_sleep = want_sleep_p();
+ 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;
+ if (since_touch >= sleep_in)
+ {
+ DBG("UPDATE: Sleeping");
+ if (update_state == US_ONLINE)
+ {
+ update_state = US_SLEEPING;
+ noct_clear();
+ noct_set_ring(8, RING_MODE_LEFT, 127);
+ }
+ return;
+ }
+ else
+ {
+ if (update_state == US_SLEEPING)
+ {
+ DBG("UPDATE: Waking up");
+ update_state = US_ONLINE;
+ noct_clear();
+ }
+ if (want_sleep)
+ {
+ timestamp_t t = sleep_in - since_touch + 10;
+ DBG("UPDATE: Scheduling sleep in %d ms", (int) t);
+ timer_add_rel(&update_timer, t);
+ }
+ }
+ // Everything normal
update_ring_from_sink(0, "ursarium");
update_ring_from_sink(1, "catarium");
update_clients();
+ update_default_sink();
+ update_mpd();
}
void schedule_update(void)
timer_add_rel(&update_timer, 10);
}
-void notify_rotary(int rotary, int delta)
+static bool prepare_notify(void)
{
- if (pulse_state != PS_ONLINE)
+ if (!pulse_is_ready())
{
DBG("## NOTIFY: Pulse is not online");
- return;
+ return 0;
+ }
+
+ last_touch_time = main_get_now();
+ if (update_state == US_SLEEPING)
+ {
+ DBG("## NOTIFY: Scheduling wakeup");
+ schedule_update();
}
+ return 1;
+}
+
+void notify_rotary(int rotary, int delta)
+{
+ if (!prepare_notify())
+ return;
+
switch (rotary)
{
case 0:
void notify_button(int button, int on)
{
- if (pulse_state != PS_ONLINE)
- {
- DBG("## NOTIFY: Pulse is not online");
- return;
- }
+ if (!prepare_notify())
+ return;
switch (button)
{
case 1:
update_sink_mute_from_button(on, "catarium");
break;
+ case 8:
+ case 9:
+ case 10:
+ 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 (!prepare_notify())
+ 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)
+static int debug;
+
+static struct opt_section options = {
+ OPT_ITEMS {
+ OPT_HELP("Control console for the Ursary"),
+ OPT_HELP(""),
+ OPT_HELP("Options:"),
+ OPT_HELP_OPTION,
+ OPT_BOOL('d', "debug", debug, 0, "\tEnable debugging mode (no fork etc.)"),
+ OPT_END
+ }
+};
+
+static void sigterm_handler(struct main_signal *ms UNUSED)
+{
+ main_shut_down();
+}
+
+static void daemon_body(struct daemon_params *dp)
{
- log_init(argv[0]);
main_init();
update_timer.handler = do_update;
noct_init();
pulse_init();
+ mpd_init();
+
+ static struct main_signal term_sig = {
+ .signum = SIGTERM,
+ .handler = sigterm_handler,
+ };
+ signal_add(&term_sig);
- msg(L_DEBUG, "Entering main loop");
+ msg(L_INFO, "Ursary daemon starting");
main_loop();
+ msg(L_INFO, "Ursary daemon shut down");
+ daemon_exit(dp);
+}
+
+int main(int argc UNUSED, char **argv)
+{
+ opt_parse(&options, argv+1);
+ unsetenv("DISPLAY");
+ unsetenv("HOME");
+
+ struct daemon_params dp = {
+ .flags = (debug ? DAEMON_FLAG_SIMULATE : 0),
+ .pid_file = "/run/ursaryd.pid",
+ .run_as_user = "ursary",
+ };
+ daemon_init(&dp);
+
+ log_init(argv[0]);
+ if (!debug)
+ {
+ struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
+ ls->levels = ~(1U << L_DEBUG);
+ log_set_default_stream(ls);
+ }
+ daemon_run(&dp, daemon_body);
return 0;
}