2 * The Ursary Audio Controls
4 * (c) 2014 Martin Mares <mj@ucw.cz>
10 #include <ucw/clists.h>
11 #include <ucw/mainloop.h>
12 #include <ucw/stkstring.h>
23 * rotary red button green button
24 * 0 sink Ursarium mute select as default (or assign to client selected by touch)
25 * 1 sink Catarium mute dtto
28 * 4 MPD mute play/pause/stop
37 /*** Sink controls ***/
39 static double volume_from_pa(pa_volume_t vol)
41 return (double) vol / PA_VOLUME_NORM;
44 static pa_volume_t volume_to_pa(double vol)
46 return vol * PA_VOLUME_NORM + 0.0001;
49 static void update_ring_from_sink(int ring, const char *sink_name)
51 struct pulse_sink *s = pulse_sink_by_name(sink_name);
54 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
55 noct_set_button(ring, 0);
61 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
62 noct_set_button(ring, 1);
66 double vol = CLAMP(volume_from_pa(s->volume), 0, 1);
67 noct_set_ring(ring, RING_MODE_LEFT, CLAMP((int)(0x7f * vol), 12, 0x7f));
68 noct_set_button(ring, 0);
71 static void update_sink_from_rotary(int delta, const char *sink_name)
73 struct pulse_sink *s = pulse_sink_by_name(sink_name);
77 double vol = volume_from_pa(s->volume) + delta * 0.02;
78 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, 1));
79 if (pavol == s->volume)
82 pa_cvolume_set(&cvol, s->channels, pavol);
84 DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
85 pulse_sink_set_volume(s->idx, &cvol);
88 static void update_sink_mute_from_button(int on, const char *sink_name)
93 struct pulse_sink *s = pulse_sink_by_name(sink_name);
97 DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
98 pulse_sink_set_mute(s->idx, !s->mute);
101 /*** Client controls ***/
110 static struct client_map client_map[] = {
111 { 4, "Music Player Daemon", "albireo", 1.5 },
112 { 5, NULL, "albireo", 1.5 },
113 { 6, NULL, "ogion", 1.5 },
114 { 7, NULL, "ursula", 1.5 },
117 #define NUM_CLIENTS ARRAY_SIZE(client_map)
119 struct client_state {
124 static struct client_state client_state[NUM_CLIENTS];
126 static int find_client_by_rotary(int rotary)
129 for (i=0; i < NUM_CLIENTS; i++)
130 if (client_map[i].rotary == rotary)
135 static void calc_clients(void)
137 bzero(client_state, sizeof(client_state));
139 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
141 s->noct_client_idx = -1;
143 if (s->client_idx < 0 || s->sink_idx < 0)
146 struct pulse_client *c = pulse_client_by_idx(s->client_idx);
150 for (uns i=0; i < NUM_CLIENTS; i++)
152 struct client_map *cm = &client_map[i];
153 struct client_state *cs = &client_state[i];
154 if ((!cm->client || !strcmp(cm->client, c->name)) &&
155 (!cm->host || !strcmp(cm->host, c->host)))
157 // DBG("@@ Client #%d, sink input #%d -> rotary %d", s->client_idx, s->idx, cm->rotary);
158 s->noct_client_idx = i;
159 cs->volume = MAX(cs->volume, s->volume);
160 cs->have_muted[!!s->mute] = 1;
167 static void update_clients(void)
171 for (uns i=0; i < NUM_CLIENTS; i++)
173 struct client_map *cm = &client_map[i];
174 struct client_state *cs = &client_state[i];
175 if (!cs->have_muted[0] && !cs->have_muted[1])
177 noct_set_ring(cm->rotary, RING_MODE_LEFT, 0);
178 noct_set_button(cm->rotary, 0);
180 else if (!cs->have_muted[0])
182 noct_set_ring(cm->rotary, RING_MODE_SINGLE_ON, 0x7f);
183 noct_set_button(cm->rotary, 1);
187 double vol = CLAMP(volume_from_pa(cs->volume), 0, cm->range);
188 int val = 0x7f * vol / cm->range;
189 val = CLAMP(val, 12, 0x7f);
190 noct_set_ring(cm->rotary, RING_MODE_LEFT, val);
191 noct_set_button(cm->rotary, 0);
196 static void update_client_from_rotary(int rotary, int delta)
198 int i = find_client_by_rotary(rotary);
201 struct client_map *cm = &client_map[i];
202 struct client_state *cs = &client_state[i];
205 double vol = volume_from_pa(cs->volume) + delta*0.02;
206 pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, cm->range));
208 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
210 if (s->noct_client_idx == i && s->volume != pavol)
212 DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
214 pa_cvolume_set(&cvol, s->channels, pavol);
215 pulse_sink_input_set_volume(s->idx, &cvol);
220 static void update_client_from_button(int button, int on)
222 if (button >= 8 || !on)
225 int i = find_client_by_rotary(button);
228 struct client_state *cs = &client_state[i];
231 if (!cs->have_muted[0] && !cs->have_muted[1])
233 uns mute = !cs->have_muted[1];
235 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
237 if (s->noct_client_idx == i)
239 DBG("@@ Client #%d, sink input #%d: setting mute=%u", s->client_idx, s->idx, mute);
240 pulse_sink_input_set_mute(s->idx, mute);
245 static int find_touched_client(void)
249 for (uns i=0; i < NUM_CLIENTS; i++)
250 if (noct_rotary_touched[client_map[i].rotary])
259 /*** Default sink controls ***/
261 static const char *get_client_sink(int i)
263 const char *sink = NULL;
265 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
266 if (s->noct_client_idx == i)
268 struct pulse_sink *sk = (s->sink_idx >= 0) ? pulse_sink_by_idx(s->sink_idx) : NULL;
269 const char *ss = sk ? sk->name : NULL;
272 else if (strcmp(sink, ss))
278 static void update_default_sink(void)
280 int i = find_touched_client();
283 sink = get_client_sink(i);
285 sink = pulse_default_sink_name ? : "?";
287 if (!strcmp(sink, "ursarium"))
289 noct_set_button(8, 1);
290 noct_set_button(9, 0);
292 else if (!strcmp(sink, "catarium"))
294 noct_set_button(8, 0);
295 noct_set_button(9, 1);
299 noct_set_button(8, 0);
300 noct_set_button(9, 0);
304 static void update_default_sink_from_button(int button, int on)
309 int i = find_touched_client();
312 sink = get_client_sink(i);
314 sink = pulse_default_sink_name ? : "?";
316 const char *switch_to = NULL;
319 if (!strcmp(sink, "ursarium"))
320 switch_to = "burrow";
322 switch_to = "ursarium";
324 else if (button == 9)
326 if (!strcmp(sink, "catarium"))
327 switch_to = "burrow";
329 switch_to = "catarium";
337 struct pulse_sink *sk = pulse_sink_by_name(switch_to);
341 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
342 if (s->noct_client_idx == i)
344 DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
345 pulse_sink_input_move(s->idx, sk->idx);
350 DBG("Switching default sink to %s", switch_to);
351 pulse_server_set_default_sink(switch_to);
355 /*** MPD controls ***/
357 static bool mpd_flash_state;
359 static void mpd_flash_timeout(struct main_timer *t)
361 mpd_flash_state ^= 1;
362 noct_set_button(12, mpd_flash_state);
363 timer_add_rel(t, 500);
366 static struct main_timer mpd_flash_timer = {
367 .handler = mpd_flash_timeout,
370 static void update_mpd(void)
372 const char *state = mpd_get_player_state();
373 if (!strcmp(state, "play"))
375 noct_set_button(12, 1);
376 timer_del(&mpd_flash_timer);
378 else if (!strcmp(state, "pause"))
380 if (!timer_is_active(&mpd_flash_timer))
383 mpd_flash_timeout(&mpd_flash_timer);
388 noct_set_button(12, 0);
389 timer_del(&mpd_flash_timer);
393 static void mpd_button_timeout(struct main_timer *t)
400 static void update_mpd_from_button(int button UNUSED, int on)
402 static struct main_timer mpd_button_timer = {
403 .handler = mpd_button_timeout,
406 const char *state = mpd_get_player_state();
410 if (timer_is_active(&mpd_button_timer))
412 timer_del(&mpd_button_timer);
413 if (!strcmp(state, "play"))
418 else if (!strcmp(state, "pause"))
427 if (!strcmp(state, "stop"))
434 DBG("MPD starting button timer");
435 timer_add_rel(&mpd_button_timer, 1000);
439 /*** Main update routines ***/
441 static struct main_timer update_timer;
443 static void do_update(struct main_timer *t)
446 if (!noct_is_ready())
448 DBG("## UPDATE: Nocturn is not ready");
453 if (pulse_state != PS_ONLINE)
455 DBG("## UPDATE: Pulse is not online");
456 for (int i=0; i<=8; i++)
457 noct_set_ring(i, RING_MODE_LEFT, 0);
458 for (int i=0; i<8; i++)
460 noct_set_button(i, 1);
461 noct_set_button(i+8, 0);
468 DBG("## UPDATE: Waking up from the dead");
469 for (int i=0; i<=8; i++)
470 noct_set_ring(i, RING_MODE_LEFT, 0);
471 for (int i=0; i<16; i++)
472 noct_set_button(i, 0);
481 update_ring_from_sink(0, "ursarium");
482 update_ring_from_sink(1, "catarium");
484 update_default_sink();
488 void schedule_update(void)
490 timer_add_rel(&update_timer, 10);
493 void notify_rotary(int rotary, int delta)
495 if (pulse_state != PS_ONLINE)
497 DBG("## NOTIFY: Pulse is not online");
504 update_sink_from_rotary(delta, "ursarium");
507 update_sink_from_rotary(delta, "catarium");
510 update_sink_from_rotary(delta, "ursarium");
511 update_sink_from_rotary(delta, "catarium");
514 update_client_from_rotary(rotary, delta);
518 void notify_button(int button, int on)
520 if (pulse_state != PS_ONLINE)
522 DBG("## NOTIFY: Pulse is not online");
529 update_sink_mute_from_button(on, "ursarium");
532 update_sink_mute_from_button(on, "catarium");
536 update_default_sink_from_button(button, on);
539 update_mpd_from_button(button, on);
542 update_client_from_button(button, on);
546 void notify_touch(int rotary, int on UNUSED)
548 if (pulse_state != PS_ONLINE)
550 DBG("## NOTIFY: Pulse is not online");
554 // Rotary touches switch meaning of LEDs, this is handled inside display updates
555 if (rotary >= 4 && rotary < 8)
559 /*** Main entry point ***/
561 int main(int argc UNUSED, char **argv)
565 update_timer.handler = do_update;
571 msg(L_DEBUG, "Entering main loop");