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 /*** Sink controls ***/
44 static double volume_from_pa(pa_volume_t vol)
46 return (double) vol / PA_VOLUME_NORM;
49 static pa_volume_t volume_to_pa(double vol)
51 return vol * PA_VOLUME_NORM + 0.0001;
54 static void update_ring_from_sink(int ring, const char *sink_name)
56 struct pulse_sink *s = pulse_sink_by_name(sink_name);
59 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
60 noct_set_button(ring, 0);
66 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
67 noct_set_button(ring, 1);
71 double vol = CLAMP(volume_from_pa(s->volume), 0, 1);
72 noct_set_ring(ring, RING_MODE_LEFT, CLAMP((int)(0x7f * vol), 12, 0x7f));
73 noct_set_button(ring, 0);
76 static void update_button_from_port(int button, const char *sink_name, const char *port_name)
78 struct pulse_sink *s = pulse_sink_by_name(sink_name);
81 noct_set_button(button, 0);
85 noct_set_button(button, !strcmp(s->active_port, port_name));
88 static void update_sink_from_rotary(int delta, const char *sink_name)
90 struct pulse_sink *s = pulse_sink_by_name(sink_name);
94 double vol = volume_from_pa(s->volume) + delta * 0.02;
95 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, 1));
96 if (pavol == s->volume)
99 pa_cvolume_set(&cvol, s->channels, pavol);
101 DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
102 pulse_sink_set_volume(s->idx, &cvol);
105 static void update_sink_mute_from_button(int on, const char *sink_name)
110 struct pulse_sink *s = pulse_sink_by_name(sink_name);
114 DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
115 pulse_sink_set_mute(s->idx, !s->mute);
118 static void update_port_from_button(int on, const char *sink_name, const char *port1, const char *port2)
123 struct pulse_sink *s = pulse_sink_by_name(sink_name);
127 const char *port = port1;
128 if (!strcmp(s->active_port, port1))
131 DBG("## Setting port of sink %s to %s", s->name, port);
132 pulse_sink_set_port(s->idx, port);
135 /*** Client controls ***/
143 static struct client_map client_map[] = {
144 { 4, "Music Player Daemon", "albireo", },
145 { 5, "mpv", "albireo", },
146 { 6, NULL, "albireo", },
150 #define CLIENT_MAP_SIZE ARRAY_SIZE(client_map)
152 struct group_config {
164 static struct group_config group_config[NUM_GROUPS] = {
165 [4] = { .enabled = 1, .range = 1.5 },
166 [5] = { .enabled = 1, .range = 1.5 },
167 [6] = { .enabled = 1, .range = 1.5 },
168 [7] = { .enabled = 1, .range = 1.5 },
171 static struct group_state group_state[NUM_GROUPS];
173 static void calc_groups(void)
175 bzero(group_state, sizeof(group_state));
177 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
179 s->noct_group_idx = -1;
181 if (s->client_idx < 0 || s->sink_idx < 0)
184 struct pulse_client *c = pulse_client_by_idx(s->client_idx);
188 for (uns i=0; i < CLIENT_MAP_SIZE; i++)
190 struct client_map *cm = &client_map[i];
191 if ((!cm->client || !strcmp(cm->client, c->name)) &&
192 (!cm->host || !strcmp(cm->host, c->host)))
195 struct group_state *gs = &group_state[g];
196 DBG("@@ Client #%d, sink input #%d -> group %d", s->client_idx, s->idx, g);
197 s->noct_group_idx = g;
198 gs->volume = MAX(gs->volume, s->volume);
199 gs->have_muted[!!s->mute] = 1;
206 static void update_groups(void)
210 for (uns i=0; i < NUM_GROUPS; i++)
212 struct group_config *gc = &group_config[i];
213 struct group_state *gs = &group_state[i];
217 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);
219 if (!gs->have_muted[0] && !gs->have_muted[1])
221 noct_set_ring(i, RING_MODE_LEFT, 0);
222 noct_set_button(i, 0);
224 else if (!gs->have_muted[0])
226 noct_set_ring(i, RING_MODE_SINGLE_ON, 0x7f);
227 noct_set_button(i, 1);
231 double vol = CLAMP(volume_from_pa(gs->volume), 0, gc->range);
232 int val = 0x7f * vol / gc->range;
233 val = CLAMP(val, 12, 0x7f);
234 noct_set_ring(i, RING_MODE_LEFT, val);
235 noct_set_button(i, 0);
240 static void update_group_from_rotary(int i, int delta)
244 struct group_config *gc = &group_config[i];
245 struct group_state *gs = &group_state[i];
250 double vol = volume_from_pa(gs->volume) + delta*0.02;
251 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, gc->range));
253 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
255 if (s->noct_group_idx == i && s->volume != pavol)
257 DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
259 pa_cvolume_set(&cvol, s->channels, pavol);
260 pulse_sink_input_set_volume(s->idx, &cvol);
265 static void update_group_from_button(int i, int on)
271 struct group_config *gc = &group_config[i];
272 struct group_state *gs = &group_state[i];
277 if (!gs->have_muted[0] && !gs->have_muted[1])
279 uns mute = !gs->have_muted[1];
281 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
283 if (s->noct_group_idx == i)
285 DBG("@@ Client #%d, sink input #%d: setting mute=%u", s->client_idx, s->idx, mute);
286 pulse_sink_input_set_mute(s->idx, mute);
291 #if 0 // Not used at the moment
293 static int find_touched_client(void)
297 for (uns i=0; i < NUM_GROUPS; i++)
298 if (group_config[i].enabled && noct_rotary_touched[i])
309 /*** Default sink controls ***/
311 #if 0 // Not mapped to any button at the moment
313 static const char *get_client_sink(int i)
315 const char *sink = NULL;
317 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
318 if (s->noct_group_idx == i)
320 struct pulse_sink *sk = (s->sink_idx >= 0) ? pulse_sink_by_idx(s->sink_idx) : NULL;
321 const char *ss = sk ? sk->name : NULL;
324 else if (strcmp(sink, ss))
330 static void update_default_sink(void)
332 int i = find_touched_client();
335 sink = get_client_sink(i);
337 sink = pulse_default_sink_name ? : "?";
339 if (!strcmp(sink, "ursarium"))
341 noct_set_button(8, 1);
342 noct_set_button(9, 0);
343 noct_set_button(10, 0);
345 else if (!strcmp(sink, "catarium"))
347 noct_set_button(8, 0);
348 noct_set_button(9, 1);
349 noct_set_button(10, 0);
351 else if (!strcmp(sink, "compress"))
353 noct_set_button(8, 0);
354 noct_set_button(9, 0);
355 noct_set_button(10, 1);
359 noct_set_button(8, 0);
360 noct_set_button(9, 0);
361 noct_set_button(10, 0);
365 static void update_default_sink_from_button(int button, int on)
370 int i = find_touched_client();
373 sink = get_client_sink(i);
375 sink = pulse_default_sink_name ? : "?";
377 const char *switch_to = NULL;
380 if (!strcmp(sink, "ursarium"))
381 switch_to = "burrow";
383 switch_to = "ursarium";
385 else if (button == 9)
387 if (!strcmp(sink, "catarium"))
388 switch_to = "burrow";
390 switch_to = "catarium";
392 else if (button == 10)
394 if (!strcmp(sink, "compress"))
395 switch_to = "burrow";
397 switch_to = "compress";
405 struct pulse_sink *sk = pulse_sink_by_name(switch_to);
409 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
410 if (s->noct_group_idx == i)
412 DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
413 pulse_sink_input_move(s->idx, sk->idx);
418 DBG("Switching default sink to %s", switch_to);
419 pulse_server_set_default_sink(switch_to);
425 /*** MPD controls ***/
427 static bool mpd_flash_state;
429 static void mpd_flash_timeout(struct main_timer *t)
431 mpd_flash_state ^= 1;
432 noct_set_button(12, mpd_flash_state);
433 timer_add_rel(t, 500);
436 static struct main_timer mpd_flash_timer = {
437 .handler = mpd_flash_timeout,
440 static void update_mpd(void)
442 const char *state = mpd_get_player_state();
443 if (!strcmp(state, "play"))
445 noct_set_button(12, 1);
446 timer_del(&mpd_flash_timer);
448 else if (!strcmp(state, "pause"))
450 if (!timer_is_active(&mpd_flash_timer))
453 mpd_flash_timeout(&mpd_flash_timer);
458 noct_set_button(12, 0);
459 timer_del(&mpd_flash_timer);
463 static void mpd_button_timeout(struct main_timer *t)
470 static void update_mpd_from_button(int button UNUSED, int on)
472 static struct main_timer mpd_button_timer = {
473 .handler = mpd_button_timeout,
476 const char *state = mpd_get_player_state();
480 if (timer_is_active(&mpd_button_timer))
482 timer_del(&mpd_button_timer);
483 if (!strcmp(state, "play"))
488 else if (!strcmp(state, "pause"))
497 if (!strcmp(state, "stop"))
504 DBG("MPD starting button timer");
505 timer_add_rel(&mpd_button_timer, 1000);
509 /*** Main update routines ***/
511 static struct main_timer update_timer;
512 static timestamp_t last_touch_time;
521 static enum update_state update_state;
523 static bool want_sleep_p(void)
525 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
531 static void do_update(struct main_timer *t)
533 DBG("## UPDATE in state %u", update_state);
537 if (!noct_is_ready())
539 DBG("## UPDATE: Nocturn is not ready");
540 update_state = US_OFFLINE;
543 else if (update_state == US_OFFLINE)
545 DBG("## UPDATE: Going online");
546 update_state = US_ONLINE;
550 if (!pulse_is_ready())
552 DBG("## UPDATE: Pulse is not online");
553 if (update_state != US_PULSE_DEAD)
555 update_state = US_PULSE_DEAD;
557 for (int i=0; i<8; i++)
558 noct_set_button(i, 1);
562 else if (update_state == US_PULSE_DEAD)
564 DBG("## UPDATE: Waking up from the dead");
565 update_state = US_ONLINE;
574 bool want_sleep = want_sleep_p();
576 last_touch_time = main_get_now();
577 timestamp_t since_touch = last_touch_time ? main_get_now() - last_touch_time : 0;
578 timestamp_t sleep_in = 5000;
579 if (since_touch >= sleep_in)
581 DBG("UPDATE: Sleeping");
582 if (update_state == US_ONLINE)
584 update_state = US_SLEEPING;
586 noct_set_ring(8, RING_MODE_LEFT, 127);
592 if (update_state == US_SLEEPING)
594 DBG("UPDATE: Waking up");
595 update_state = US_ONLINE;
600 timestamp_t t = sleep_in - since_touch + 10;
601 DBG("UPDATE: Scheduling sleep in %d ms", (int) t);
602 timer_add_rel(&update_timer, t);
607 update_ring_from_sink(0, "alsa_output.brum.analog-stereo");
608 update_button_from_port(8, "alsa_output.brum.analog-stereo", "analog-output-headphones");
611 update_default_sink();
616 void schedule_update(void)
618 timer_add_rel(&update_timer, 10);
621 static bool prepare_notify(void)
623 if (!pulse_is_ready())
625 DBG("## NOTIFY: Pulse is not online");
629 last_touch_time = main_get_now();
630 if (update_state == US_SLEEPING)
632 DBG("## NOTIFY: Scheduling wakeup");
639 void notify_rotary(int rotary, int delta)
641 if (!prepare_notify())
647 update_sink_from_rotary(delta, "alsa_output.brum.analog-stereo");
650 update_group_from_rotary(rotary, delta);
654 void notify_button(int button, int on)
656 if (!prepare_notify())
662 update_sink_mute_from_button(on, "alsa_output.brum.analog-stereo");
665 update_port_from_button(on, "alsa_output.brum.analog-stereo", "analog-output-lineout", "analog-output-headphones");
670 update_default_sink_from_button(button, on);
674 update_mpd_from_button(button, on);
689 update_group_from_button(button, on);
693 void notify_touch(int rotary, int on UNUSED)
695 if (!prepare_notify())
698 // Rotary touches switch meaning of LEDs, this is handled inside display updates
699 if (rotary >= 4 && rotary < 8)
703 /*** Main entry point ***/
708 static struct opt_section options = {
710 OPT_HELP("Control console for the Ursary"),
712 OPT_HELP("Options:"),
714 OPT_BOOL('d', "debug", debug, 0, "\tEnable debugging mode (no fork etc.)"),
715 OPT_BOOL(0, "no-fork", no_fork, 0, "\tDo not fork\n"),
720 static void sigterm_handler(struct main_signal *ms UNUSED)
725 static void daemon_body(struct daemon_params *dp)
728 update_timer.handler = do_update;
734 static struct main_signal term_sig = {
736 .handler = sigterm_handler,
738 signal_add(&term_sig);
740 msg(L_INFO, "Ursary daemon starting");
742 msg(L_INFO, "Ursary daemon shut down");
746 int main(int argc UNUSED, char **argv)
748 opt_parse(&options, argv+1);
752 struct daemon_params dp = {
753 .flags = ((debug || no_fork) ? DAEMON_FLAG_SIMULATE : 0),
754 .pid_file = "/run/ursaryd.pid",
755 .run_as_user = "ursary",
762 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
763 ls->levels = ~(1U << L_DEBUG);
764 log_set_default_stream(ls);
767 daemon_run(&dp, daemon_body);