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 static int find_touched_client(void)
295 for (uns i=0; i < NUM_GROUPS; i++)
296 if (group_config[i].enabled && noct_rotary_touched[i])
305 /*** Default sink controls ***/
307 static const char *get_client_sink(int i)
309 const char *sink = NULL;
311 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
312 if (s->noct_group_idx == i)
314 struct pulse_sink *sk = (s->sink_idx >= 0) ? pulse_sink_by_idx(s->sink_idx) : NULL;
315 const char *ss = sk ? sk->name : NULL;
318 else if (strcmp(sink, ss))
324 static void update_default_sink(void)
326 int i = find_touched_client();
329 sink = get_client_sink(i);
331 sink = pulse_default_sink_name ? : "?";
333 if (!strcmp(sink, "ursarium"))
335 noct_set_button(8, 1);
336 noct_set_button(9, 0);
337 noct_set_button(10, 0);
339 else if (!strcmp(sink, "catarium"))
341 noct_set_button(8, 0);
342 noct_set_button(9, 1);
343 noct_set_button(10, 0);
345 else if (!strcmp(sink, "compress"))
347 noct_set_button(8, 0);
348 noct_set_button(9, 0);
349 noct_set_button(10, 1);
353 noct_set_button(8, 0);
354 noct_set_button(9, 0);
355 noct_set_button(10, 0);
359 static void update_default_sink_from_button(int button, int on)
364 int i = find_touched_client();
367 sink = get_client_sink(i);
369 sink = pulse_default_sink_name ? : "?";
371 const char *switch_to = NULL;
374 if (!strcmp(sink, "ursarium"))
375 switch_to = "burrow";
377 switch_to = "ursarium";
379 else if (button == 9)
381 if (!strcmp(sink, "catarium"))
382 switch_to = "burrow";
384 switch_to = "catarium";
386 else if (button == 10)
388 if (!strcmp(sink, "compress"))
389 switch_to = "burrow";
391 switch_to = "compress";
399 struct pulse_sink *sk = pulse_sink_by_name(switch_to);
403 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
404 if (s->noct_group_idx == i)
406 DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
407 pulse_sink_input_move(s->idx, sk->idx);
412 DBG("Switching default sink to %s", switch_to);
413 pulse_server_set_default_sink(switch_to);
417 /*** MPD controls ***/
419 static bool mpd_flash_state;
421 static void mpd_flash_timeout(struct main_timer *t)
423 mpd_flash_state ^= 1;
424 noct_set_button(12, mpd_flash_state);
425 timer_add_rel(t, 500);
428 static struct main_timer mpd_flash_timer = {
429 .handler = mpd_flash_timeout,
432 static void update_mpd(void)
434 const char *state = mpd_get_player_state();
435 if (!strcmp(state, "play"))
437 noct_set_button(12, 1);
438 timer_del(&mpd_flash_timer);
440 else if (!strcmp(state, "pause"))
442 if (!timer_is_active(&mpd_flash_timer))
445 mpd_flash_timeout(&mpd_flash_timer);
450 noct_set_button(12, 0);
451 timer_del(&mpd_flash_timer);
455 static void mpd_button_timeout(struct main_timer *t)
462 static void update_mpd_from_button(int button UNUSED, int on)
464 static struct main_timer mpd_button_timer = {
465 .handler = mpd_button_timeout,
468 const char *state = mpd_get_player_state();
472 if (timer_is_active(&mpd_button_timer))
474 timer_del(&mpd_button_timer);
475 if (!strcmp(state, "play"))
480 else if (!strcmp(state, "pause"))
489 if (!strcmp(state, "stop"))
496 DBG("MPD starting button timer");
497 timer_add_rel(&mpd_button_timer, 1000);
501 /*** Main update routines ***/
503 static struct main_timer update_timer;
504 static timestamp_t last_touch_time;
513 static enum update_state update_state;
515 static bool want_sleep_p(void)
517 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
523 static void do_update(struct main_timer *t)
525 DBG("## UPDATE in state %u", update_state);
529 if (!noct_is_ready())
531 DBG("## UPDATE: Nocturn is not ready");
532 update_state = US_OFFLINE;
535 else if (update_state == US_OFFLINE)
537 DBG("## UPDATE: Going online");
538 update_state = US_ONLINE;
542 if (!pulse_is_ready())
544 DBG("## UPDATE: Pulse is not online");
545 if (update_state != US_PULSE_DEAD)
547 update_state = US_PULSE_DEAD;
549 for (int i=0; i<8; i++)
550 noct_set_button(i, 1);
554 else if (update_state == US_PULSE_DEAD)
556 DBG("## UPDATE: Waking up from the dead");
557 update_state = US_ONLINE;
566 bool want_sleep = want_sleep_p();
568 last_touch_time = main_get_now();
569 timestamp_t since_touch = last_touch_time ? main_get_now() - last_touch_time : 0;
570 timestamp_t sleep_in = 5000;
571 if (since_touch >= sleep_in)
573 DBG("UPDATE: Sleeping");
574 if (update_state == US_ONLINE)
576 update_state = US_SLEEPING;
578 noct_set_ring(8, RING_MODE_LEFT, 127);
584 if (update_state == US_SLEEPING)
586 DBG("UPDATE: Waking up");
587 update_state = US_ONLINE;
592 timestamp_t t = sleep_in - since_touch + 10;
593 DBG("UPDATE: Scheduling sleep in %d ms", (int) t);
594 timer_add_rel(&update_timer, t);
599 update_ring_from_sink(0, "alsa_output.brum.analog-stereo");
600 update_button_from_port(8, "alsa_output.brum.analog-stereo", "analog-output-headphones");
603 update_default_sink();
608 void schedule_update(void)
610 timer_add_rel(&update_timer, 10);
613 static bool prepare_notify(void)
615 if (!pulse_is_ready())
617 DBG("## NOTIFY: Pulse is not online");
621 last_touch_time = main_get_now();
622 if (update_state == US_SLEEPING)
624 DBG("## NOTIFY: Scheduling wakeup");
631 void notify_rotary(int rotary, int delta)
633 if (!prepare_notify())
639 update_sink_from_rotary(delta, "alsa_output.brum.analog-stereo");
642 update_group_from_rotary(rotary, delta);
646 void notify_button(int button, int on)
648 if (!prepare_notify())
654 update_sink_mute_from_button(on, "alsa_output.brum.analog-stereo");
657 update_port_from_button(on, "alsa_output.brum.analog-stereo", "analog-output-lineout", "analog-output-headphones");
662 update_default_sink_from_button(button, on);
666 update_mpd_from_button(button, on);
681 update_group_from_button(button, on);
685 void notify_touch(int rotary, int on UNUSED)
687 if (!prepare_notify())
690 // Rotary touches switch meaning of LEDs, this is handled inside display updates
691 if (rotary >= 4 && rotary < 8)
695 /*** Main entry point ***/
700 static struct opt_section options = {
702 OPT_HELP("Control console for the Ursary"),
704 OPT_HELP("Options:"),
706 OPT_BOOL('d', "debug", debug, 0, "\tEnable debugging mode (no fork etc.)"),
707 OPT_BOOL(0, "no-fork", no_fork, 0, "\tDo not fork\n"),
712 static void sigterm_handler(struct main_signal *ms UNUSED)
717 static void daemon_body(struct daemon_params *dp)
720 update_timer.handler = do_update;
726 static struct main_signal term_sig = {
728 .handler = sigterm_handler,
730 signal_add(&term_sig);
732 msg(L_INFO, "Ursary daemon starting");
734 msg(L_INFO, "Ursary daemon shut down");
738 int main(int argc UNUSED, char **argv)
740 opt_parse(&options, argv+1);
744 struct daemon_params dp = {
745 .flags = ((debug || no_fork) ? DAEMON_FLAG_SIMULATE : 0),
746 .pid_file = "/run/ursaryd.pid",
747 .run_as_user = "ursary",
754 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
755 ls->levels = ~(1U << L_DEBUG);
756 log_set_default_stream(ls);
759 daemon_run(&dp, daemon_body);