2 * The Ursary Audio Controls
4 * (c) 2014--2020 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>
30 * rotary red button green button
31 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 * 0 sink PCH mute switch to PCH
33 * 1 sink BT mute switch to BT
35 * 3 desk brightness - desk lights on
36 * 4 MPD mute MPD play/pause
37 * 5 Albireo MPV mute MPD stop
38 * 6 Albireo other mute MPD prev
39 * 7 other machines mute MPD next
42 * slider light color temperature
45 #define PCH_SINK "alsa_output.pci-0000_00_1f.3.analog-stereo"
46 #define BT_SINK "bluez_sink.CC_98_8B_D0_8C_06.a2dp_sink"
48 /*** Sink controls ***/
50 static double volume_from_pa(pa_volume_t vol)
52 return (double) vol / PA_VOLUME_NORM;
55 static pa_volume_t volume_to_pa(double vol)
57 return vol * PA_VOLUME_NORM + 0.0001;
60 static void update_ring_from_sink(int ring, const char *sink_name)
62 struct pulse_sink *s = pulse_sink_by_name(sink_name);
65 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
66 noct_set_button(ring, 0);
72 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
73 noct_set_button(ring, 1);
77 double vol = CLAMP(volume_from_pa(s->volume), 0, 1);
78 noct_set_ring(ring, RING_MODE_LEFT, CLAMP((int)(0x7f * vol), 12, 0x7f));
79 noct_set_button(ring, 0);
82 static void update_sink_from_rotary(int delta, const char *sink_name)
84 struct pulse_sink *s = pulse_sink_by_name(sink_name);
88 double vol = volume_from_pa(s->volume) + delta * 0.02;
89 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, 1));
90 if (pavol == s->volume)
93 pa_cvolume_set(&cvol, s->channels, pavol);
95 DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
96 pulse_sink_set_volume(s->idx, &cvol);
99 static void update_sink_mute_from_button(int on, const char *sink_name)
104 struct pulse_sink *s = pulse_sink_by_name(sink_name);
108 DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
109 pulse_sink_set_mute(s->idx, !s->mute);
112 /*** Client controls ***/
120 static struct client_map client_map[] = {
121 { 4, "Music Player Daemon", "albireo", },
122 { 5, "mpv", "albireo", },
123 { 6, NULL, "albireo", },
127 #define CLIENT_MAP_SIZE ARRAY_SIZE(client_map)
129 struct group_config {
141 static struct group_config group_config[NUM_GROUPS] = {
142 [4] = { .enabled = 1, .range = 1.5 },
143 [5] = { .enabled = 1, .range = 1.5 },
144 [6] = { .enabled = 1, .range = 1.5 },
145 [7] = { .enabled = 1, .range = 1.5 },
148 static struct group_state group_state[NUM_GROUPS];
150 static void calc_groups(void)
152 bzero(group_state, sizeof(group_state));
154 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
156 s->noct_group_idx = -1;
158 if (s->client_idx < 0 || s->sink_idx < 0)
161 struct pulse_client *c = pulse_client_by_idx(s->client_idx);
165 for (uns i=0; i < CLIENT_MAP_SIZE; i++)
167 struct client_map *cm = &client_map[i];
168 if ((!cm->client || !strcmp(cm->client, c->name)) &&
169 (!cm->host || !strcmp(cm->host, c->host)))
172 struct group_state *gs = &group_state[g];
173 DBG("@@ Client #%d, sink input #%d -> group %d", s->client_idx, s->idx, g);
174 s->noct_group_idx = g;
175 gs->volume = MAX(gs->volume, s->volume);
176 gs->have_muted[!!s->mute] = 1;
183 static void update_groups(void)
187 for (uns i=0; i < NUM_GROUPS; i++)
189 struct group_config *gc = &group_config[i];
190 struct group_state *gs = &group_state[i];
194 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);
196 if (!gs->have_muted[0] && !gs->have_muted[1])
198 noct_set_ring(i, RING_MODE_LEFT, 0);
199 noct_set_button(i, 0);
201 else if (!gs->have_muted[0])
203 noct_set_ring(i, RING_MODE_SINGLE_ON, 0x7f);
204 noct_set_button(i, 1);
208 double vol = CLAMP(volume_from_pa(gs->volume), 0, gc->range);
209 int val = 0x7f * vol / gc->range;
210 val = CLAMP(val, 12, 0x7f);
211 noct_set_ring(i, RING_MODE_LEFT, val);
212 noct_set_button(i, 0);
217 static void update_group_from_rotary(int i, int delta)
221 struct group_config *gc = &group_config[i];
222 struct group_state *gs = &group_state[i];
227 double vol = volume_from_pa(gs->volume) + delta*0.02;
228 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, gc->range));
230 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
232 if (s->noct_group_idx == i && s->volume != pavol)
234 DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
236 pa_cvolume_set(&cvol, s->channels, pavol);
237 pulse_sink_input_set_volume(s->idx, &cvol);
242 static void update_group_from_button(int i, int on)
248 struct group_config *gc = &group_config[i];
249 struct group_state *gs = &group_state[i];
254 if (!gs->have_muted[0] && !gs->have_muted[1])
256 uns mute = !gs->have_muted[1];
258 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
260 if (s->noct_group_idx == i)
262 DBG("@@ Client #%d, sink input #%d: setting mute=%u", s->client_idx, s->idx, mute);
263 pulse_sink_input_set_mute(s->idx, mute);
268 static int find_touched_client(void)
272 for (uns i=0; i < NUM_GROUPS; i++)
273 if (group_config[i].enabled && noct_rotary_touched[i])
282 /*** Default sink controls ***/
284 static const char *get_client_sink(int i)
286 const char *sink = NULL;
288 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
289 if (s->noct_group_idx == i)
291 struct pulse_sink *sk = (s->sink_idx >= 0) ? pulse_sink_by_idx(s->sink_idx) : NULL;
292 const char *ss = sk ? sk->name : NULL;
295 else if (strcmp(sink, ss))
301 static void update_default_sink(void)
303 int i = find_touched_client();
306 sink = get_client_sink(i);
308 sink = pulse_default_sink_name ? : "?";
310 if (!strcmp(sink, PCH_SINK))
312 noct_set_button(8, 1);
313 noct_set_button(9, 0);
315 else if (!strcmp(sink, BT_SINK))
317 noct_set_button(8, 0);
318 noct_set_button(9, 1);
322 noct_set_button(8, 0);
323 noct_set_button(9, 0);
327 static void update_default_sink_from_button(int button, int on)
332 int i = find_touched_client();
336 sink = get_client_sink(i);
338 sink = pulse_default_sink_name ? : "?";
341 const char *switch_to = NULL;
343 switch_to = PCH_SINK;
344 else if (button == 9)
352 struct pulse_sink *sk = pulse_sink_by_name(switch_to);
356 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
357 if (s->noct_group_idx == i)
359 DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
360 pulse_sink_input_move(s->idx, sk->idx);
365 DBG("Switching default sink to %s", switch_to);
366 pulse_server_set_default_sink(switch_to);
370 /*** MPD controls ***/
372 static bool mpd_flash_state;
374 static void mpd_flash_timeout(struct main_timer *t)
376 mpd_flash_state ^= 1;
377 noct_set_button(12, mpd_flash_state);
378 timer_add_rel(t, 500);
381 static struct main_timer mpd_flash_timer = {
382 .handler = mpd_flash_timeout,
385 static void update_mpd(void)
387 const char *state = mpd_get_player_state();
388 if (!strcmp(state, "play"))
390 noct_set_button(12, 1);
391 timer_del(&mpd_flash_timer);
393 else if (!strcmp(state, "pause"))
395 if (!timer_is_active(&mpd_flash_timer))
398 mpd_flash_timeout(&mpd_flash_timer);
403 noct_set_button(12, 0);
404 timer_del(&mpd_flash_timer);
408 static void mpd_button_timeout(struct main_timer *t)
415 static void update_mpd_from_button(int button UNUSED, int on)
417 static struct main_timer mpd_button_timer = {
418 .handler = mpd_button_timeout,
421 const char *state = mpd_get_player_state();
425 if (timer_is_active(&mpd_button_timer))
427 timer_del(&mpd_button_timer);
428 if (!strcmp(state, "play"))
433 else if (!strcmp(state, "pause"))
442 if (!strcmp(state, "stop"))
449 DBG("MPD starting button timer");
450 timer_add_rel(&mpd_button_timer, 1000);
456 static bool lights_on[2];
457 static double lights_brightness[2];
458 static double lights_temperature[2];
460 static void update_lights(void)
464 noct_set_ring(3, RING_MODE_SINGLE_ON, 0x7f);
465 noct_set_button(11, 0);
469 for (uint i=0; i<1; i++)
474 noct_set_ring(3, RING_MODE_LEFT, lights_brightness[i] * 127);
475 noct_set_button(11, 1);
477 double x = (exp(r*lights_brightness[i]) - 1) / (exp(r) - 1);
478 double t = lights_temperature[i];
479 double w = 2*x*(1-t);
481 warm = CLAMP((int)(255*w), 0, 255);
482 cold = CLAMP((int)(255*c), 0, 255);
486 noct_set_ring(3, RING_MODE_LEFT, 0);
487 noct_set_button(11, 0);
490 DBG("Lights[%d]: on=%d bri=%.3f temp=%.3f -> warm=%d cold=%d", i, lights_on[i], lights_brightness[i], lights_temperature[i], warm, cold);
491 dmx_set_pwm(2*i, warm);
492 dmx_set_pwm(2*i+1, cold);
496 static void update_lights_from_rotary(int ch, int delta)
499 lights_brightness[ch] = CLAMP(lights_brightness[ch] + 0.015*delta*abs(delta), 0., 1.);
503 static void update_lights_from_slider(int value)
505 lights_temperature[0] = value / 127.;
509 static void lights_button_timeout(struct main_timer *t)
511 int ch = (uintptr_t) t->data;
512 DBG("Lights[%d]: Full throttle!", ch);
515 lights_brightness[ch] = 1;
519 static void update_lights_from_button(int ch, int on)
521 static struct main_timer lights_button_timer[2] = {{
522 .handler = lights_button_timeout,
523 .data = (void *)(uintptr_t) 0,
525 .handler = lights_button_timeout,
526 .data = (void *)(uintptr_t) 1,
531 lights_on[ch] = !lights_on[ch];
533 timer_add_rel(&lights_button_timer[ch], 1000);
536 timer_del(&lights_button_timer[ch]);
539 /*** Main update routines ***/
541 static struct main_timer update_timer;
542 static timestamp_t last_touch_time;
551 static enum update_state update_state;
553 static bool want_sleep_p(void)
555 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
561 static void do_update(struct main_timer *t)
563 DBG("## UPDATE in state %u", update_state);
567 if (!noct_is_ready())
569 DBG("## UPDATE: Nocturn is not ready");
570 update_state = US_OFFLINE;
573 else if (update_state == US_OFFLINE)
575 DBG("## UPDATE: Going online");
576 update_state = US_ONLINE;
580 if (!pulse_is_ready())
582 DBG("## UPDATE: Pulse is not online");
583 if (update_state != US_PULSE_DEAD)
585 update_state = US_PULSE_DEAD;
587 for (int i=0; i<8; i++)
588 noct_set_button(i, 1);
592 else if (update_state == US_PULSE_DEAD)
594 DBG("## UPDATE: Waking up from the dead");
595 update_state = US_ONLINE;
604 bool want_sleep = want_sleep_p();
606 last_touch_time = main_get_now();
607 timestamp_t since_touch = last_touch_time ? main_get_now() - last_touch_time : 0;
608 timestamp_t sleep_in = 30000;
609 if (since_touch >= sleep_in)
611 DBG("UPDATE: Sleeping");
612 if (update_state == US_ONLINE)
614 update_state = US_SLEEPING;
616 noct_set_ring(8, RING_MODE_LEFT, 127);
622 if (update_state == US_SLEEPING)
624 DBG("UPDATE: Waking up");
625 update_state = US_ONLINE;
630 timestamp_t t = sleep_in - since_touch + 10;
631 DBG("UPDATE: Scheduling sleep in %d ms", (int) t);
632 timer_add_rel(&update_timer, t);
637 update_ring_from_sink(0, PCH_SINK);
638 update_ring_from_sink(1, BT_SINK);
640 update_default_sink();
645 void schedule_update(void)
647 timer_add_rel(&update_timer, 10);
650 static bool prepare_notify(void)
652 if (!pulse_is_ready())
654 DBG("## NOTIFY: Pulse is not online");
658 last_touch_time = main_get_now();
659 if (update_state == US_SLEEPING)
661 DBG("## NOTIFY: Scheduling wakeup");
668 void notify_rotary(int rotary, int delta)
670 if (!prepare_notify())
676 update_sink_from_rotary(delta, PCH_SINK);
679 update_sink_from_rotary(delta, BT_SINK);
682 update_lights_from_rotary(0, delta);
685 update_lights_from_slider(delta);
688 update_group_from_rotary(rotary, delta);
692 void notify_button(int button, int on)
694 if (!prepare_notify())
700 update_sink_mute_from_button(on, PCH_SINK);
703 update_sink_mute_from_button(on, BT_SINK);
707 update_default_sink_from_button(button, on);
710 update_lights_from_button(0, on);
713 update_mpd_from_button(button, on);
728 update_group_from_button(button, on);
732 void notify_touch(int rotary UNUSED, int on UNUSED)
734 if (!prepare_notify())
737 // Rotary touches switch meaning of LEDs, this is handled inside display updates
738 if (rotary >= 4 && rotary < 8)
742 /*** Main entry point ***/
747 static struct opt_section options = {
749 OPT_HELP("Control console for the Ursary"),
751 OPT_HELP("Options:"),
753 OPT_BOOL('d', "debug", debug, 0, "\tEnable debugging mode (no fork etc.)"),
754 OPT_BOOL(0, "no-fork", no_fork, 0, "\tDo not fork\n"),
759 static void sigterm_handler(struct main_signal *ms UNUSED)
764 static void daemon_body(struct daemon_params *dp)
767 update_timer.handler = do_update;
775 static struct main_signal term_sig = {
777 .handler = sigterm_handler,
779 signal_add(&term_sig);
781 msg(L_INFO, "Ursary daemon starting");
783 msg(L_INFO, "Ursary daemon shut down");
787 int main(int argc UNUSED, char **argv)
789 opt_parse(&options, argv+1);
793 struct daemon_params dp = {
794 .flags = ((debug || no_fork) ? DAEMON_FLAG_SIMULATE : 0),
795 .pid_file = "/run/ursaryd.pid",
796 .run_as_user = "ursary",
803 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
804 ls->levels = ~(1U << L_DEBUG);
805 log_set_default_stream(ls);
808 daemon_run(&dp, daemon_body);