2 * The Ursary Audio Controls
4 * (c) 2014--2018 Martin Mares <mj@ucw.cz>
10 #include <ucw/clists.h>
11 #include <ucw/daemon.h>
13 #include <ucw/mainloop.h>
15 #include <ucw/stkstring.h>
28 * rotary red button green button
33 * 4 MPD mute play/pause/stop
34 * 5 Albireo MPV mute -
35 * 6 Albireo other mute -
36 * 7 other machines mute -
42 #define PCH_SINK "alsa_output.pci-0000_00_1f.3.analog-stereo"
44 /*** Sink controls ***/
46 static double volume_from_pa(pa_volume_t vol)
48 return (double) vol / PA_VOLUME_NORM;
51 static pa_volume_t volume_to_pa(double vol)
53 return vol * PA_VOLUME_NORM + 0.0001;
56 static void update_ring_from_sink(int ring, const char *sink_name)
58 struct pulse_sink *s = pulse_sink_by_name(sink_name);
61 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
62 noct_set_button(ring, 0);
68 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
69 noct_set_button(ring, 1);
73 double vol = CLAMP(volume_from_pa(s->volume), 0, 1);
74 noct_set_ring(ring, RING_MODE_LEFT, CLAMP((int)(0x7f * vol), 12, 0x7f));
75 noct_set_button(ring, 0);
78 static void update_button_from_port(int button, const char *sink_name, const char *port_name)
80 struct pulse_sink *s = pulse_sink_by_name(sink_name);
83 noct_set_button(button, 0);
87 noct_set_button(button, !strcmp(s->active_port, port_name));
90 static void update_sink_from_rotary(int delta, const char *sink_name)
92 struct pulse_sink *s = pulse_sink_by_name(sink_name);
96 double vol = volume_from_pa(s->volume) + delta * 0.02;
97 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, 1));
98 if (pavol == s->volume)
101 pa_cvolume_set(&cvol, s->channels, pavol);
103 DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
104 pulse_sink_set_volume(s->idx, &cvol);
107 static void update_sink_mute_from_button(int on, const char *sink_name)
112 struct pulse_sink *s = pulse_sink_by_name(sink_name);
116 DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
117 pulse_sink_set_mute(s->idx, !s->mute);
120 static void update_port_from_button(int on, const char *sink_name, const char *port1, const char *port2)
125 struct pulse_sink *s = pulse_sink_by_name(sink_name);
129 const char *port = port1;
130 if (!strcmp(s->active_port, port1))
133 DBG("## Setting port of sink %s to %s", s->name, port);
134 pulse_sink_set_port(s->idx, port);
137 /*** Client controls ***/
145 static struct client_map client_map[] = {
146 { 4, "Music Player Daemon", "albireo", },
147 { 5, "mpv", "albireo", },
148 { 6, NULL, "albireo", },
152 #define CLIENT_MAP_SIZE ARRAY_SIZE(client_map)
154 struct group_config {
166 static struct group_config group_config[NUM_GROUPS] = {
167 [4] = { .enabled = 1, .range = 1.5 },
168 [5] = { .enabled = 1, .range = 1.5 },
169 [6] = { .enabled = 1, .range = 1.5 },
170 [7] = { .enabled = 1, .range = 1.5 },
173 static struct group_state group_state[NUM_GROUPS];
175 static void calc_groups(void)
177 bzero(group_state, sizeof(group_state));
179 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
181 s->noct_group_idx = -1;
183 if (s->client_idx < 0 || s->sink_idx < 0)
186 struct pulse_client *c = pulse_client_by_idx(s->client_idx);
190 for (uns i=0; i < CLIENT_MAP_SIZE; i++)
192 struct client_map *cm = &client_map[i];
193 if ((!cm->client || !strcmp(cm->client, c->name)) &&
194 (!cm->host || !strcmp(cm->host, c->host)))
197 struct group_state *gs = &group_state[g];
198 DBG("@@ Client #%d, sink input #%d -> group %d", s->client_idx, s->idx, g);
199 s->noct_group_idx = g;
200 gs->volume = MAX(gs->volume, s->volume);
201 gs->have_muted[!!s->mute] = 1;
208 static void update_groups(void)
212 for (uns i=0; i < NUM_GROUPS; i++)
214 struct group_config *gc = &group_config[i];
215 struct group_state *gs = &group_state[i];
219 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);
221 if (!gs->have_muted[0] && !gs->have_muted[1])
223 noct_set_ring(i, RING_MODE_LEFT, 0);
224 noct_set_button(i, 0);
226 else if (!gs->have_muted[0])
228 noct_set_ring(i, RING_MODE_SINGLE_ON, 0x7f);
229 noct_set_button(i, 1);
233 double vol = CLAMP(volume_from_pa(gs->volume), 0, gc->range);
234 int val = 0x7f * vol / gc->range;
235 val = CLAMP(val, 12, 0x7f);
236 noct_set_ring(i, RING_MODE_LEFT, val);
237 noct_set_button(i, 0);
242 static void update_group_from_rotary(int i, int delta)
246 struct group_config *gc = &group_config[i];
247 struct group_state *gs = &group_state[i];
252 double vol = volume_from_pa(gs->volume) + delta*0.02;
253 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, gc->range));
255 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
257 if (s->noct_group_idx == i && s->volume != pavol)
259 DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
261 pa_cvolume_set(&cvol, s->channels, pavol);
262 pulse_sink_input_set_volume(s->idx, &cvol);
267 static void update_group_from_button(int i, int on)
273 struct group_config *gc = &group_config[i];
274 struct group_state *gs = &group_state[i];
279 if (!gs->have_muted[0] && !gs->have_muted[1])
281 uns mute = !gs->have_muted[1];
283 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
285 if (s->noct_group_idx == i)
287 DBG("@@ Client #%d, sink input #%d: setting mute=%u", s->client_idx, s->idx, mute);
288 pulse_sink_input_set_mute(s->idx, mute);
293 #if 0 // Not used at the moment
295 static int find_touched_client(void)
299 for (uns i=0; i < NUM_GROUPS; i++)
300 if (group_config[i].enabled && noct_rotary_touched[i])
311 /*** Default sink controls ***/
313 #if 0 // Not mapped to any button at the moment
315 static const char *get_client_sink(int i)
317 const char *sink = NULL;
319 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
320 if (s->noct_group_idx == i)
322 struct pulse_sink *sk = (s->sink_idx >= 0) ? pulse_sink_by_idx(s->sink_idx) : NULL;
323 const char *ss = sk ? sk->name : NULL;
326 else if (strcmp(sink, ss))
332 static void update_default_sink(void)
334 int i = find_touched_client();
337 sink = get_client_sink(i);
339 sink = pulse_default_sink_name ? : "?";
341 if (!strcmp(sink, "ursarium"))
343 noct_set_button(8, 1);
344 noct_set_button(9, 0);
345 noct_set_button(10, 0);
347 else if (!strcmp(sink, "catarium"))
349 noct_set_button(8, 0);
350 noct_set_button(9, 1);
351 noct_set_button(10, 0);
353 else if (!strcmp(sink, "compress"))
355 noct_set_button(8, 0);
356 noct_set_button(9, 0);
357 noct_set_button(10, 1);
361 noct_set_button(8, 0);
362 noct_set_button(9, 0);
363 noct_set_button(10, 0);
367 static void update_default_sink_from_button(int button, int on)
372 int i = find_touched_client();
375 sink = get_client_sink(i);
377 sink = pulse_default_sink_name ? : "?";
379 const char *switch_to = NULL;
382 if (!strcmp(sink, "ursarium"))
383 switch_to = "burrow";
385 switch_to = "ursarium";
387 else if (button == 9)
389 if (!strcmp(sink, "catarium"))
390 switch_to = "burrow";
392 switch_to = "catarium";
394 else if (button == 10)
396 if (!strcmp(sink, "compress"))
397 switch_to = "burrow";
399 switch_to = "compress";
407 struct pulse_sink *sk = pulse_sink_by_name(switch_to);
411 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
412 if (s->noct_group_idx == i)
414 DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
415 pulse_sink_input_move(s->idx, sk->idx);
420 DBG("Switching default sink to %s", switch_to);
421 pulse_server_set_default_sink(switch_to);
427 /*** MPD controls ***/
429 static bool mpd_flash_state;
431 static void mpd_flash_timeout(struct main_timer *t)
433 mpd_flash_state ^= 1;
434 noct_set_button(12, mpd_flash_state);
435 timer_add_rel(t, 500);
438 static struct main_timer mpd_flash_timer = {
439 .handler = mpd_flash_timeout,
442 static void update_mpd(void)
444 const char *state = mpd_get_player_state();
445 if (!strcmp(state, "play"))
447 noct_set_button(12, 1);
448 timer_del(&mpd_flash_timer);
450 else if (!strcmp(state, "pause"))
452 if (!timer_is_active(&mpd_flash_timer))
455 mpd_flash_timeout(&mpd_flash_timer);
460 noct_set_button(12, 0);
461 timer_del(&mpd_flash_timer);
465 static void mpd_button_timeout(struct main_timer *t)
472 static void update_mpd_from_button(int button UNUSED, int on)
474 static struct main_timer mpd_button_timer = {
475 .handler = mpd_button_timeout,
478 const char *state = mpd_get_player_state();
482 if (timer_is_active(&mpd_button_timer))
484 timer_del(&mpd_button_timer);
485 if (!strcmp(state, "play"))
490 else if (!strcmp(state, "pause"))
499 if (!strcmp(state, "stop"))
506 DBG("MPD starting button timer");
507 timer_add_rel(&mpd_button_timer, 1000);
511 /*** Main update routines ***/
513 static struct main_timer update_timer;
514 static timestamp_t last_touch_time;
523 static enum update_state update_state;
525 static bool want_sleep_p(void)
527 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
533 static void do_update(struct main_timer *t)
535 DBG("## UPDATE in state %u", update_state);
539 if (!noct_is_ready())
541 DBG("## UPDATE: Nocturn is not ready");
542 update_state = US_OFFLINE;
545 else if (update_state == US_OFFLINE)
547 DBG("## UPDATE: Going online");
548 update_state = US_ONLINE;
552 if (!pulse_is_ready())
554 DBG("## UPDATE: Pulse is not online");
555 if (update_state != US_PULSE_DEAD)
557 update_state = US_PULSE_DEAD;
559 for (int i=0; i<8; i++)
560 noct_set_button(i, 1);
564 else if (update_state == US_PULSE_DEAD)
566 DBG("## UPDATE: Waking up from the dead");
567 update_state = US_ONLINE;
576 bool want_sleep = want_sleep_p();
578 last_touch_time = main_get_now();
579 timestamp_t since_touch = last_touch_time ? main_get_now() - last_touch_time : 0;
580 timestamp_t sleep_in = 5000;
581 if (since_touch >= sleep_in)
583 DBG("UPDATE: Sleeping");
584 if (update_state == US_ONLINE)
586 update_state = US_SLEEPING;
588 noct_set_ring(8, RING_MODE_LEFT, 127);
594 if (update_state == US_SLEEPING)
596 DBG("UPDATE: Waking up");
597 update_state = US_ONLINE;
602 timestamp_t t = sleep_in - since_touch + 10;
603 DBG("UPDATE: Scheduling sleep in %d ms", (int) t);
604 timer_add_rel(&update_timer, t);
609 update_ring_from_sink(0, PCH_SINK);
610 update_button_from_port(8, PCH_SINK, "analog-output-headphones");
613 update_default_sink();
618 void schedule_update(void)
620 timer_add_rel(&update_timer, 10);
623 static bool prepare_notify(void)
625 if (!pulse_is_ready())
627 DBG("## NOTIFY: Pulse is not online");
631 last_touch_time = main_get_now();
632 if (update_state == US_SLEEPING)
634 DBG("## NOTIFY: Scheduling wakeup");
641 void notify_rotary(int rotary, int delta)
643 if (!prepare_notify())
649 update_sink_from_rotary(delta, PCH_SINK);
652 update_group_from_rotary(rotary, delta);
656 void notify_button(int button, int on)
658 if (!prepare_notify())
664 update_sink_mute_from_button(on, PCH_SINK);
667 update_port_from_button(on, PCH_SINK, "analog-output-lineout", "analog-output-headphones");
672 update_default_sink_from_button(button, on);
676 update_mpd_from_button(button, on);
691 update_group_from_button(button, on);
695 void notify_touch(int rotary, int on UNUSED)
697 if (!prepare_notify())
700 // Rotary touches switch meaning of LEDs, this is handled inside display updates
701 if (rotary >= 4 && rotary < 8)
705 /*** Main entry point ***/
710 static struct opt_section options = {
712 OPT_HELP("Control console for the Ursary"),
714 OPT_HELP("Options:"),
716 OPT_BOOL('d', "debug", debug, 0, "\tEnable debugging mode (no fork etc.)"),
717 OPT_BOOL(0, "no-fork", no_fork, 0, "\tDo not fork\n"),
722 static void sigterm_handler(struct main_signal *ms UNUSED)
727 static void daemon_body(struct daemon_params *dp)
730 update_timer.handler = do_update;
736 static struct main_signal term_sig = {
738 .handler = sigterm_handler,
740 signal_add(&term_sig);
742 msg(L_INFO, "Ursary daemon starting");
744 msg(L_INFO, "Ursary daemon shut down");
748 int main(int argc UNUSED, char **argv)
750 opt_parse(&options, argv+1);
754 struct daemon_params dp = {
755 .flags = ((debug || no_fork) ? DAEMON_FLAG_SIMULATE : 0),
756 .pid_file = "/run/ursaryd.pid",
757 .run_as_user = "ursary",
764 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
765 ls->levels = ~(1U << L_DEBUG);
766 log_set_default_stream(ls);
769 daemon_run(&dp, daemon_body);