]> mj.ucw.cz Git - ursary.git/blob - ursaryd.c
Map of controls
[ursary.git] / ursaryd.c
1 /*
2  *      The Ursary Audio Controls
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #define LOCAL_DEBUG
8
9 #include <ucw/lib.h>
10 #include <ucw/clists.h>
11 #include <ucw/mainloop.h>
12 #include <ucw/stkstring.h>
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17
18 #include "ursaryd.h"
19
20 /*
21  *      Map of all controls
22  *
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
26  *      2       -               -               -
27  *      3       -               -               -
28  *      4       MPD             mute            play/pause/stop
29  *      5       Albireo         mute            -
30  *      6       Ogion           mute            -
31  *      7       Ursula          mute            -
32  *
33  *      center  all sinks
34  *      slider  -
35  */
36
37 /*** Sink controls ***/
38
39 static double volume_from_pa(pa_volume_t vol)
40 {
41   return (double) vol / PA_VOLUME_NORM;
42 }
43
44 static pa_volume_t volume_to_pa(double vol)
45 {
46   return vol * PA_VOLUME_NORM + 0.0001;
47 }
48
49 static void update_ring_from_sink(int ring, const char *sink_name)
50 {
51   struct pulse_sink *s = pulse_sink_by_name(sink_name);
52   if (!s)
53     {
54       noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
55       noct_set_button(ring, 0);
56       return;
57     }
58
59   if (s->mute)
60     {
61       noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
62       noct_set_button(ring, 1);
63       return;
64     }
65
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);
69 }
70
71 static void update_sink_from_rotary(int delta, const char *sink_name)
72 {
73   struct pulse_sink *s = pulse_sink_by_name(sink_name);
74   if (!s)
75     return;
76
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)
80     return;
81   pa_cvolume cvol;
82   pa_cvolume_set(&cvol, s->channels, pavol);
83
84   DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
85   pulse_sink_set_volume(s->idx, &cvol);
86 }
87
88 static void update_sink_mute_from_button(int on, const char *sink_name)
89 {
90   if (!on)
91     return;
92
93   struct pulse_sink *s = pulse_sink_by_name(sink_name);
94   if (!s)
95     return;
96
97   DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
98   pulse_sink_set_mute(s->idx, !s->mute);
99 }
100
101 /*** Client controls ***/
102
103 struct client_map {
104   int rotary;
105   const char *client;
106   const char *host;
107   double range;
108 };
109
110 static struct client_map client_map[] = {
111   { 4, "Music Player Daemon",   "albireo",      1 },
112   { 5, "MPlayer",               NULL,           1 },
113   { 6, NULL,                    "ogion",        1 },
114   { 7, NULL,                    "ursula",       1 },
115 };
116
117 #define NUM_CLIENTS ARRAY_SIZE(client_map)
118
119 struct client_state {
120   double volume;
121   bool have_muted[2];
122 };
123
124 static struct client_state client_state[NUM_CLIENTS];
125
126 static int find_client_by_rotary(int rotary)
127 {
128   uns i;
129   for (i=0; i < NUM_CLIENTS; i++)
130     if (client_map[i].rotary == rotary)
131       return i;
132   return -1;
133 }
134
135 static void calc_clients(void)
136 {
137   bzero(client_state, sizeof(client_state));
138
139   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
140     {
141       s->noct_client_idx = -1;
142
143       if (s->client_idx < 0 || s->sink_idx < 0)
144         continue;
145
146       struct pulse_client *c = pulse_client_by_idx(s->client_idx);
147       if (!c)
148         continue;
149
150       for (uns i=0; i < NUM_CLIENTS; i++)
151         {
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)))
156             {
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;
161               break;
162             }
163         }
164     }
165 }
166
167 static void update_clients(void)
168 {
169   calc_clients();
170
171   for (uns i=0; i < NUM_CLIENTS; i++)
172     {
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])
176         {
177           noct_set_ring(cm->rotary, RING_MODE_LEFT, 0);
178           noct_set_button(cm->rotary, 0);
179         }
180       else if (!cs->have_muted[0])
181         {
182           noct_set_ring(cm->rotary, RING_MODE_SINGLE_ON, 0x7f);
183           noct_set_button(cm->rotary, 1);
184         }
185       else
186         {
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);
192         }
193     }
194 }
195
196 static void update_client_from_rotary(int rotary, int delta)
197 {
198   int i = find_client_by_rotary(rotary);
199   if (i < 0)
200     return;
201   struct client_map *cm = &client_map[i];
202   struct client_state *cs = &client_state[i];
203
204   calc_clients();
205   double vol = volume_from_pa(cs->volume) + delta*0.02;
206   pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, cm->range));
207
208   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
209     {
210       if (s->noct_client_idx == i && s->volume != pavol)
211         {
212           DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
213           pa_cvolume cvol;
214           pa_cvolume_set(&cvol, s->channels, pavol);
215           pulse_sink_input_set_volume(s->idx, &cvol);
216         }
217     }
218 }
219
220 static void update_client_from_button(int button, int on)
221 {
222   if (button >= 8 || !on)
223     return;
224
225   int i = find_client_by_rotary(button);
226   if (i < 0)
227     return;
228   struct client_state *cs = &client_state[i];
229
230   calc_clients();
231   if (!cs->have_muted[0] && !cs->have_muted[1])
232     return;
233   uns mute = !cs->have_muted[1];
234
235   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
236     {
237       if (s->noct_client_idx == i)
238         {
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);
241         }
242     }
243 }
244
245 static int find_touched_client(void)
246 {
247   int touched = -1;
248
249   for (uns i=0; i < NUM_CLIENTS; i++)
250     if (noct_rotary_touched[client_map[i].rotary])
251       {
252         if (touched >= 0)
253           return -1;
254         touched = i;
255       }
256   return touched;
257 }
258
259 /*** Default sink controls ***/
260
261 static const char *get_client_sink(int i)
262 {
263   const char *sink = NULL;
264
265   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
266     if (s->noct_client_idx == i)
267       {
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;
270         if (!sink)
271           sink = ss;
272         else if (strcmp(sink, ss))
273           sink = "?";
274       }
275   return sink ? : "?";
276 }
277
278 static void update_default_sink(void)
279 {
280   int i = find_touched_client();
281   const char *sink;
282   if (i >= 0)
283     sink = get_client_sink(i);
284   else
285     sink = pulse_default_sink_name ? : "?";
286
287   if (!strcmp(sink, "ursarium"))
288     {
289       noct_set_button(8, 1);
290       noct_set_button(9, 0);
291     }
292   else if (!strcmp(sink, "catarium"))
293     {
294       noct_set_button(8, 0);
295       noct_set_button(9, 1);
296     }
297   else
298     {
299       noct_set_button(8, 0);
300       noct_set_button(9, 0);
301     }
302 }
303
304 static void update_default_sink_from_button(int button, int on)
305 {
306   if (!on)
307     return;
308
309   int i = find_touched_client();
310   const char *sink;
311   if (i >= 0)
312     sink = get_client_sink(i);
313   else
314     sink = pulse_default_sink_name ? : "?";
315
316   const char *switch_to = NULL;
317   if (button == 8)
318     {
319       if (!strcmp(sink, "ursarium"))
320         switch_to = "burrow";
321       else
322         switch_to = "ursarium";
323     }
324   else if (button == 9)
325     {
326       if (!strcmp(sink, "catarium"))
327         switch_to = "burrow";
328       else
329         switch_to = "catarium";
330     }
331
332   if (!switch_to)
333     return;
334
335   if (i >= 0)
336     {
337       struct pulse_sink *sk = pulse_sink_by_name(switch_to);
338       if (!sk)
339         return;
340
341       CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
342         if (s->noct_client_idx == i)
343           {
344             DBG("Moving input #%d to sink #%d", s->idx, sk->idx);
345             pulse_sink_input_move(s->idx, sk->idx);
346           }
347     }
348   else
349     {
350       DBG("Switching default sink to %s", switch_to);
351       pulse_server_set_default_sink(switch_to);
352     }
353 }
354
355 /*** MPD controls ***/
356
357 static bool mpd_flash_state;
358
359 static void mpd_flash_timeout(struct main_timer *t)
360 {
361   mpd_flash_state ^= 1;
362   noct_set_button(12, mpd_flash_state);
363   timer_add_rel(t, 500);
364 }
365
366 static struct main_timer mpd_flash_timer = {
367   .handler = mpd_flash_timeout,
368 };
369
370 static void update_mpd(void)
371 {
372   const char *state = mpd_get_player_state();
373   if (!strcmp(state, "play"))
374     {
375       noct_set_button(12, 1);
376       timer_del(&mpd_flash_timer);
377     }
378   else if (!strcmp(state, "pause"))
379     {
380       if (!timer_is_active(&mpd_flash_timer))
381         {
382           mpd_flash_state = 1;
383           mpd_flash_timeout(&mpd_flash_timer);
384         }
385     }
386   else
387     {
388       noct_set_button(12, 0);
389       timer_del(&mpd_flash_timer);
390     }
391 }
392
393 static void mpd_button_timeout(struct main_timer *t)
394 {
395   DBG("MPD stop");
396   timer_del(t);
397   mpd_stop();
398 }
399
400 static void update_mpd_from_button(int button UNUSED, int on)
401 {
402   static struct main_timer mpd_button_timer = {
403     .handler = mpd_button_timeout,
404   };
405
406   const char *state = mpd_get_player_state();
407
408   if (!on)
409     {
410       if (timer_is_active(&mpd_button_timer))
411         {
412           timer_del(&mpd_button_timer);
413           if (!strcmp(state, "play"))
414             {
415               DBG("MPD pause");
416               mpd_pause(1);
417             }
418           else if (!strcmp(state, "pause"))
419             {
420               DBG("MPD resume");
421               mpd_pause(0);
422             }
423         }
424       return;
425     }
426
427   if (!strcmp(state, "stop"))
428     {
429       DBG("MPD play");
430       mpd_play();
431     }
432   else
433     {
434       DBG("MPD starting button timer");
435       timer_add_rel(&mpd_button_timer, 1000);
436     }
437 }
438
439 /*** Main update routines ***/
440
441 static struct main_timer update_timer;
442
443 static void do_update(struct main_timer *t)
444 {
445   timer_del(t);
446   if (!noct_is_ready())
447     {
448       DBG("## UPDATE: Nocturn is not ready");
449       return;
450     }
451
452   static bool dead;
453   if (pulse_state != PS_ONLINE)
454     {
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++)
459         {
460           noct_set_button(i, 1);
461           noct_set_button(i+8, 0);
462         }
463       dead = 1;
464       return;
465     }
466   if (dead)
467     {
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);
473       dead = 0;
474     }
475
476   DBG("## UPDATE");
477 #ifdef LOCAL_DEBUG
478   pulse_dump();
479 #endif
480
481   update_ring_from_sink(0, "ursarium");
482   update_ring_from_sink(1, "catarium");
483   update_clients();
484   update_default_sink();
485   update_mpd();
486 }
487
488 void schedule_update(void)
489 {
490   timer_add_rel(&update_timer, 10);
491 }
492
493 void notify_rotary(int rotary, int delta)
494 {
495   if (pulse_state != PS_ONLINE)
496     {
497       DBG("## NOTIFY: Pulse is not online");
498       return;
499     }
500
501   switch (rotary)
502     {
503     case 0:
504       update_sink_from_rotary(delta, "ursarium");
505       break;
506     case 1:
507       update_sink_from_rotary(delta, "catarium");
508       break;
509     case 8:
510       update_sink_from_rotary(delta, "ursarium");
511       update_sink_from_rotary(delta, "catarium");
512       break;
513     default:
514       update_client_from_rotary(rotary, delta);
515     }
516 }
517
518 void notify_button(int button, int on)
519 {
520   if (pulse_state != PS_ONLINE)
521     {
522       DBG("## NOTIFY: Pulse is not online");
523       return;
524     }
525
526   switch (button)
527     {
528     case 0:
529       update_sink_mute_from_button(on, "ursarium");
530       break;
531     case 1:
532       update_sink_mute_from_button(on, "catarium");
533       break;
534     case 8:
535     case 9:
536       update_default_sink_from_button(button, on);
537       break;
538     case 12:
539       update_mpd_from_button(button, on);
540       break;
541     default:
542       update_client_from_button(button, on);
543     }
544 }
545
546 void notify_touch(int rotary, int on UNUSED)
547 {
548   if (pulse_state != PS_ONLINE)
549     {
550       DBG("## NOTIFY: Pulse is not online");
551       return;
552     }
553
554   // Rotary touches switch meaning of LEDs, this is handled inside display updates
555   if (rotary >= 4 && rotary < 8)
556     schedule_update();
557 }
558
559 /*** Main entry point ***/
560
561 int main(int argc UNUSED, char **argv)
562 {
563   log_init(argv[0]);
564   main_init();
565   update_timer.handler = do_update;
566
567   noct_init();
568   pulse_init();
569   mpd_init();
570
571   msg(L_DEBUG, "Entering main loop");
572   main_loop();
573
574   return 0;
575 }