2 * The Ursary Audio Controls
4 * (c) 2014 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>
27 * rotary red button green button
28 * 0 sink Ursarium mute select as default (or assign to client selected by touch)
29 * 1 sink Catarium mute dtto
30 * 2 sink Compress - dtto
32 * 4 MPD mute play/pause/stop
41 /*** Sink controls ***/
43 static double volume_from_pa(pa_volume_t vol)
45 return (double) vol / PA_VOLUME_NORM;
48 static pa_volume_t volume_to_pa(double vol)
50 return vol * PA_VOLUME_NORM + 0.0001;
53 static void update_ring_from_sink(int ring, const char *sink_name)
55 struct pulse_sink *s = pulse_sink_by_name(sink_name);
58 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
59 noct_set_button(ring, 0);
65 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
66 noct_set_button(ring, 1);
70 double vol = CLAMP(volume_from_pa(s->volume), 0, 1);
71 noct_set_ring(ring, RING_MODE_LEFT, CLAMP((int)(0x7f * vol), 12, 0x7f));
72 noct_set_button(ring, 0);
75 static void update_sink_from_rotary(int delta, const char *sink_name)
77 struct pulse_sink *s = pulse_sink_by_name(sink_name);
81 double vol = volume_from_pa(s->volume) + delta * 0.02;
82 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, 1));
83 if (pavol == s->volume)
86 pa_cvolume_set(&cvol, s->channels, pavol);
88 DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
89 pulse_sink_set_volume(s->idx, &cvol);
92 static void update_sink_mute_from_button(int on, const char *sink_name)
97 struct pulse_sink *s = pulse_sink_by_name(sink_name);
101 DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
102 pulse_sink_set_mute(s->idx, !s->mute);
105 /*** Client controls ***/
114 static struct client_map client_map[] = {
115 { 4, "Music Player Daemon", "albireo", 1.5 },
116 { 5, NULL, "albireo", 1.5 },
117 { 6, NULL, "ogion", 1.5 },
118 { 7, NULL, "ursula", 1.5 },
121 #define NUM_CLIENTS ARRAY_SIZE(client_map)
123 struct client_state {
128 static struct client_state client_state[NUM_CLIENTS];
130 static int find_client_by_rotary(int rotary)
133 for (i=0; i < NUM_CLIENTS; i++)
134 if (client_map[i].rotary == rotary)
139 static void calc_clients(void)
141 bzero(client_state, sizeof(client_state));
143 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
145 s->noct_client_idx = -1;
147 if (s->client_idx < 0 || s->sink_idx < 0)
150 struct pulse_client *c = pulse_client_by_idx(s->client_idx);
154 for (uns i=0; i < NUM_CLIENTS; i++)
156 struct client_map *cm = &client_map[i];
157 struct client_state *cs = &client_state[i];
158 if ((!cm->client || !strcmp(cm->client, c->name)) &&
159 (!cm->host || !strcmp(cm->host, c->host)))
161 // DBG("@@ Client #%d, sink input #%d -> rotary %d", s->client_idx, s->idx, cm->rotary);
162 s->noct_client_idx = i;
163 cs->volume = MAX(cs->volume, s->volume);
164 cs->have_muted[!!s->mute] = 1;
171 static void update_clients(void)
175 for (uns i=0; i < NUM_CLIENTS; i++)
177 struct client_map *cm = &client_map[i];
178 struct client_state *cs = &client_state[i];
179 if (!cs->have_muted[0] && !cs->have_muted[1])
181 noct_set_ring(cm->rotary, RING_MODE_LEFT, 0);
182 noct_set_button(cm->rotary, 0);
184 else if (!cs->have_muted[0])
186 noct_set_ring(cm->rotary, RING_MODE_SINGLE_ON, 0x7f);
187 noct_set_button(cm->rotary, 1);
191 double vol = CLAMP(volume_from_pa(cs->volume), 0, cm->range);
192 int val = 0x7f * vol / cm->range;
193 val = CLAMP(val, 12, 0x7f);
194 noct_set_ring(cm->rotary, RING_MODE_LEFT, val);
195 noct_set_button(cm->rotary, 0);
200 static void update_client_from_rotary(int rotary, int delta)
202 int i = find_client_by_rotary(rotary);
205 struct client_map *cm = &client_map[i];
206 struct client_state *cs = &client_state[i];
209 double vol = volume_from_pa(cs->volume) + delta*0.02;
210 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, cm->range));
212 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
214 if (s->noct_client_idx == i && s->volume != pavol)
216 DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
218 pa_cvolume_set(&cvol, s->channels, pavol);
219 pulse_sink_input_set_volume(s->idx, &cvol);
224 static void update_client_from_button(int button, int on)
226 if (button >= 8 || !on)
229 int i = find_client_by_rotary(button);
232 struct client_state *cs = &client_state[i];
235 if (!cs->have_muted[0] && !cs->have_muted[1])
237 uns mute = !cs->have_muted[1];
239 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
241 if (s->noct_client_idx == i)
243 DBG("@@ Client #%d, sink input #%d: setting mute=%u", s->client_idx, s->idx, mute);
244 pulse_sink_input_set_mute(s->idx, mute);
249 static int find_touched_client(void)
253 for (uns i=0; i < NUM_CLIENTS; i++)
254 if (noct_rotary_touched[client_map[i].rotary])
263 /*** Default sink controls ***/
265 static const char *get_client_sink(int i)
267 const char *sink = NULL;
269 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
270 if (s->noct_client_idx == i)
272 struct pulse_sink *sk = (s->sink_idx >= 0) ? pulse_sink_by_idx(s->sink_idx) : NULL;
273 const char *ss = sk ? sk->name : NULL;
276 else if (strcmp(sink, ss))
282 static void update_default_sink(void)
284 int i = find_touched_client();
287 sink = get_client_sink(i);
289 sink = pulse_default_sink_name ? : "?";
291 if (!strcmp(sink, "ursarium"))
293 noct_set_button(8, 1);
294 noct_set_button(9, 0);
295 noct_set_button(10, 0);
297 else if (!strcmp(sink, "catarium"))
299 noct_set_button(8, 0);
300 noct_set_button(9, 1);
301 noct_set_button(10, 0);
303 else if (!strcmp(sink, "compress"))
305 noct_set_button(8, 0);
306 noct_set_button(9, 0);
307 noct_set_button(10, 1);
311 noct_set_button(8, 0);
312 noct_set_button(9, 0);
313 noct_set_button(10, 0);
317 static void update_default_sink_from_button(int button, int on)
322 int i = find_touched_client();
325 sink = get_client_sink(i);
327 sink = pulse_default_sink_name ? : "?";
329 const char *switch_to = NULL;
332 if (!strcmp(sink, "ursarium"))
333 switch_to = "burrow";
335 switch_to = "ursarium";
337 else if (button == 9)
339 if (!strcmp(sink, "catarium"))
340 switch_to = "burrow";
342 switch_to = "catarium";
344 else if (button == 10)
346 if (!strcmp(sink, "compress"))
347 switch_to = "burrow";
349 switch_to = "compress";
357 struct pulse_sink *sk = pulse_sink_by_name(switch_to);
361 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
362 if (s->noct_client_idx == i)
364 DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
365 pulse_sink_input_move(s->idx, sk->idx);
370 DBG("Switching default sink to %s", switch_to);
371 pulse_server_set_default_sink(switch_to);
375 /*** MPD controls ***/
377 static bool mpd_flash_state;
379 static void mpd_flash_timeout(struct main_timer *t)
381 mpd_flash_state ^= 1;
382 noct_set_button(12, mpd_flash_state);
383 timer_add_rel(t, 500);
386 static struct main_timer mpd_flash_timer = {
387 .handler = mpd_flash_timeout,
390 static void update_mpd(void)
392 const char *state = mpd_get_player_state();
393 if (!strcmp(state, "play"))
395 noct_set_button(12, 1);
396 timer_del(&mpd_flash_timer);
398 else if (!strcmp(state, "pause"))
400 if (!timer_is_active(&mpd_flash_timer))
403 mpd_flash_timeout(&mpd_flash_timer);
408 noct_set_button(12, 0);
409 timer_del(&mpd_flash_timer);
413 static void mpd_button_timeout(struct main_timer *t)
420 static void update_mpd_from_button(int button UNUSED, int on)
422 static struct main_timer mpd_button_timer = {
423 .handler = mpd_button_timeout,
426 const char *state = mpd_get_player_state();
430 if (timer_is_active(&mpd_button_timer))
432 timer_del(&mpd_button_timer);
433 if (!strcmp(state, "play"))
438 else if (!strcmp(state, "pause"))
447 if (!strcmp(state, "stop"))
454 DBG("MPD starting button timer");
455 timer_add_rel(&mpd_button_timer, 1000);
459 /*** Main update routines ***/
461 static struct main_timer update_timer;
462 static timestamp_t last_touch_time;
471 static enum update_state update_state;
473 static bool want_sleep_p(void)
475 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
481 static void do_update(struct main_timer *t)
483 DBG("## UPDATE in state %u", update_state);
487 if (!noct_is_ready())
489 DBG("## UPDATE: Nocturn is not ready");
490 update_state = US_OFFLINE;
493 else if (update_state == US_OFFLINE)
495 DBG("## UPDATE: Going online");
496 update_state = US_ONLINE;
500 if (!pulse_is_ready())
502 DBG("## UPDATE: Pulse is not online");
503 if (update_state != US_PULSE_DEAD)
505 update_state = US_PULSE_DEAD;
507 for (int i=0; i<8; i++)
508 noct_set_button(i, 1);
512 else if (update_state == US_PULSE_DEAD)
514 DBG("## UPDATE: Waking up from the dead");
515 update_state = US_ONLINE;
524 bool want_sleep = want_sleep_p();
526 last_touch_time = main_get_now();
527 timestamp_t since_touch = last_touch_time ? main_get_now() - last_touch_time : 0;
528 timestamp_t sleep_in = 5000;
529 if (since_touch >= sleep_in)
531 DBG("UPDATE: Sleeping");
532 if (update_state == US_ONLINE)
534 update_state = US_SLEEPING;
536 noct_set_ring(8, RING_MODE_LEFT, 127);
542 if (update_state == US_SLEEPING)
544 DBG("UPDATE: Waking up");
545 update_state = US_ONLINE;
550 timestamp_t t = sleep_in - since_touch + 10;
551 DBG("UPDATE: Scheduling sleep in %d ms", (int) t);
552 timer_add_rel(&update_timer, t);
557 update_ring_from_sink(0, "ursarium");
558 update_ring_from_sink(1, "catarium");
560 update_default_sink();
564 void schedule_update(void)
566 timer_add_rel(&update_timer, 10);
569 static bool prepare_notify(void)
571 if (!pulse_is_ready())
573 DBG("## NOTIFY: Pulse is not online");
577 last_touch_time = main_get_now();
578 if (update_state == US_SLEEPING)
580 DBG("## NOTIFY: Scheduling wakeup");
587 void notify_rotary(int rotary, int delta)
589 if (!prepare_notify())
595 update_sink_from_rotary(delta, "ursarium");
598 update_sink_from_rotary(delta, "catarium");
601 update_sink_from_rotary(delta, "ursarium");
602 update_sink_from_rotary(delta, "catarium");
605 update_client_from_rotary(rotary, delta);
609 void notify_button(int button, int on)
611 if (!prepare_notify())
617 update_sink_mute_from_button(on, "ursarium");
620 update_sink_mute_from_button(on, "catarium");
625 update_default_sink_from_button(button, on);
628 update_mpd_from_button(button, on);
631 update_client_from_button(button, on);
635 void notify_touch(int rotary, int on UNUSED)
637 if (!prepare_notify())
640 // Rotary touches switch meaning of LEDs, this is handled inside display updates
641 if (rotary >= 4 && rotary < 8)
645 /*** Main entry point ***/
647 static void daemon_body(struct daemon_params *dp UNUSED)
650 update_timer.handler = do_update;
656 msg(L_INFO, "Ursary daemon starting");
662 static struct opt_section options = {
664 OPT_HELP("Control console for the Ursary"),
666 OPT_HELP("Options:"),
668 OPT_BOOL('d', "debug", debug, 0, "\tEnable debugging mode (no fork etc.)"),
673 int main(int argc UNUSED, char **argv)
675 opt_parse(&options, argv+1);
679 struct daemon_params dp = {
680 .flags = (debug ? DAEMON_FLAG_SIMULATE : 0),
681 .pid_file = "/run/ursaryd.pid",
682 .run_as_user = "ursary",
692 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
693 log_set_default_stream(ls);
696 daemon_run(&dp, daemon_body);