]> mj.ucw.cz Git - misc.git/blob - ursaryd/ut.c
a1277441d00e4399fb9c5fce3eddd82e10fe0abc
[misc.git] / ursaryd / ut.c
1 #define LOCAL_DEBUG
2
3 #include <ucw/lib.h>
4 #include <ucw/clists.h>
5 #include <ucw/mainloop.h>
6 #include <ucw/stkstring.h>
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11
12 #include <pulse/pulseaudio.h>
13
14 #include "ursaryd.h"
15
16 /*** Interface to PulseAudio ***/
17
18 static pa_context *pulse_ctx;
19 static struct main_timer pulse_connect_timer;
20
21 static void pulse_dump(void);
22
23 enum pulse_state {
24   PS_OFFLINE,
25   PS_SUBSCRIBE,
26   PS_GET_CLIENTS,
27   PS_GET_SINKS,
28   PS_GET_SINK_INPUTS,
29   PS_ONLINE,
30 };
31
32 static enum pulse_state pulse_state;
33 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
34
35 // Tracking of currently running asynchronous operations
36 struct pulse_op {
37   cnode n;
38   pa_operation *o;
39   bool is_init;
40 };
41
42 static clist pulse_op_list;
43
44 static struct pulse_op *pulse_op_new(void)
45 {
46   struct pulse_op *op = xmalloc_zero(sizeof(*op));
47   clist_add_tail(&pulse_op_list, &op->n);
48   return op;
49 }
50
51 static void pulse_op_done(struct pulse_op *op)
52 {
53   if (op->o)
54     pa_operation_unref(op->o);
55   clist_remove(&op->n);
56   xfree(op);
57 }
58
59 static void pulse_op_cancel_all(void)
60 {
61   struct pulse_op *op;
62   while (op = (struct pulse_op *) clist_head(&pulse_op_list))
63     {
64       DBG("Pulse: Cancelling pending operation");
65       pa_operation_cancel(op->o);
66       pulse_op_done(op);
67     }
68 }
69
70 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(__VA_ARGS__, _op); } while (0)
71 #define PULSE_ASYNC_INIT_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->is_init = 1; _op->o = name(__VA_ARGS__, _op); } while (0)
72
73 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
74 {
75   if (!success)
76     msg(L_ERROR, "Pulse: Failure reported");
77   pulse_op_done(userdata);
78 }
79
80 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
81 {
82 #if 0
83   void *iterator = NULL;
84   const char *key;
85
86   while (key = pa_proplist_iterate(pl, &iterator))
87     {
88       const char *val = pa_proplist_gets(pl, key);
89       DBG("   %s = %s", key, val);
90     }
91 #endif
92 }
93
94 struct pulse_sink_input {
95   int idx;
96   char *name;
97   int client_idx;
98   int sink_idx;
99   uns channels;
100   uns volume;
101   uns mute;
102 };
103
104 #define HASH_NODE struct pulse_sink_input
105 #define HASH_PREFIX(x) pulse_sink_input_##x
106 #define HASH_KEY_ATOMIC idx
107 #define HASH_WANT_CLEANUP
108 #define HASH_WANT_LOOKUP
109 #define HASH_WANT_REMOVE
110 #define HASH_ZERO_FILL
111 #include <ucw/hashtable.h>
112
113 #define SET_STRING(_field, _val) do { if (!_field || strcmp(_field, _val)) { xfree(_field); _field = xstrdup(_val); } } while (0)
114
115 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
116 {
117   struct pulse_op *op = userdata;
118
119   if (eol)
120     {
121       if (op->is_init)
122         {
123           PULSE_STATE(PS_ONLINE);
124           schedule_update();
125         }
126       pulse_op_done(op);
127       return;
128     }
129
130   DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
131     i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
132   pulse_dump_proplist(i->proplist);
133
134   struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
135   SET_STRING(s->name, i->name);
136   s->client_idx = i->client;
137   s->sink_idx = i->sink;
138   s->channels = i->channel_map.channels;
139   s->volume = pa_cvolume_avg(&i->volume);
140   s->mute = i->mute;
141   schedule_update();
142 }
143
144 static void pulse_sink_input_gone(int idx)
145 {
146   DBG("Pulse: REMOVE SINK INPUT #%d", idx);
147   struct pulse_sink_input *s = pulse_sink_input_lookup(idx);
148   pulse_sink_input_remove(s);
149   schedule_update();
150 }
151
152 struct pulse_sink {
153   int idx;
154   char *name;
155   uns channels;
156   uns volume;
157   uns base_volume;
158   int mute;
159 };
160
161 #define HASH_NODE struct pulse_sink
162 #define HASH_PREFIX(x) pulse_sink_##x
163 #define HASH_KEY_ATOMIC idx
164 #define HASH_WANT_CLEANUP
165 #define HASH_WANT_LOOKUP
166 #define HASH_WANT_REMOVE
167 #define HASH_ZERO_FILL
168 #include <ucw/hashtable.h>
169
170 static void pulse_sink_cb(pa_context *ctx, const pa_sink_info *i, int eol, void *userdata)
171 {
172   struct pulse_op *op = userdata;
173
174   if (eol)
175     {
176       if (op->is_init)
177         {
178           PULSE_STATE(PS_GET_SINK_INPUTS);
179           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, ctx, pulse_sink_input_cb);
180         }
181       pulse_op_done(op);
182       return;
183     }
184
185   DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u",
186     i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state);
187   pulse_dump_proplist(i->proplist);
188
189   struct pulse_sink *s = pulse_sink_lookup(i->index);
190   SET_STRING(s->name, i->name);
191   s->channels = i->channel_map.channels;
192   s->volume = pa_cvolume_avg(&i->volume);
193   s->base_volume = i->base_volume;
194   s->mute = i->mute;
195   schedule_update();
196 }
197
198 static void pulse_sink_gone(int idx)
199 {
200   DBG("Pulse: REMOVE SINK #%d", idx);
201   struct pulse_sink *s = pulse_sink_lookup(idx);
202   pulse_sink_remove(s);
203   schedule_update();
204 }
205
206 static struct pulse_sink *pulse_sink_by_name(const char *name)
207 {
208   HASH_FOR_ALL(pulse_sink, s)
209     {
210       if (!strcmp(s->name, name))
211         return s;
212     }
213   HASH_END_FOR;
214   return NULL;
215 }
216
217 struct pulse_client {
218   int idx;
219   char *name;
220   char *host;
221 };
222
223 #define HASH_NODE struct pulse_client
224 #define HASH_PREFIX(x) pulse_client_##x
225 #define HASH_KEY_ATOMIC idx
226 #define HASH_WANT_CLEANUP
227 #define HASH_WANT_LOOKUP
228 #define HASH_WANT_REMOVE
229 #define HASH_ZERO_FILL
230 #include <ucw/hashtable.h>
231
232 static void pulse_client_cb(pa_context *ctx, const pa_client_info *i, int eol, void *userdata)
233 {
234   struct pulse_op *op = userdata;
235
236   if (eol)
237     {
238       if (op->is_init)
239         {
240           PULSE_STATE(PS_GET_SINKS);
241           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, ctx, pulse_sink_cb);
242         }
243       pulse_op_done(op);
244       return;
245     }
246
247   char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
248   DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
249     i->index, i->name, i->owner_module, i->driver, host);
250   pulse_dump_proplist(i->proplist);
251
252   struct pulse_client *c = pulse_client_lookup(i->index);
253   SET_STRING(c->name, i->name);
254   SET_STRING(c->host, host);
255   schedule_update();
256 }
257
258 static void pulse_client_gone(int idx)
259 {
260   DBG("Pulse: REMOVE CLIENT #%d", idx);
261   struct pulse_client *c = pulse_client_lookup(idx);
262   pulse_client_remove(c);
263   schedule_update();
264 }
265
266 static void pulse_shutdown(void)
267 {
268   DBG("Pulse: Shutting down");
269   pulse_client_cleanup();
270   pulse_sink_cleanup();
271   pulse_sink_input_cleanup();
272 }
273
274 static void pulse_subscribe_done_cb(pa_context *ctx, int success, void *userdata)
275 {
276   pulse_op_done(userdata);
277
278   if (!success)
279     msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
280
281   PULSE_STATE(PS_GET_CLIENTS);
282   PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, ctx, pulse_client_cb);
283 }
284
285 static void pulse_event_cb(pa_context *ctx, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
286 {
287   DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
288
289   uns object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
290   uns action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
291   switch (object)
292     {
293     case PA_SUBSCRIPTION_EVENT_CLIENT:
294       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
295         PULSE_ASYNC_RUN(pa_context_get_client_info, ctx, idx, pulse_client_cb);
296       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
297         pulse_client_gone(idx);
298       break;
299     case PA_SUBSCRIPTION_EVENT_SINK:
300       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
301         PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, ctx, idx, pulse_sink_cb);
302       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
303         pulse_sink_gone(idx);
304       break;
305     case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
306       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
307         PULSE_ASYNC_RUN(pa_context_get_sink_input_info, ctx, idx, pulse_sink_input_cb);
308       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
309         pulse_sink_input_gone(idx);
310       break;
311     }
312 }
313
314 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
315 {
316   int state = pa_context_get_state(ctx);
317   DBG("Pulse: State callback, new state = %d", state);
318   if (state == PA_CONTEXT_READY)
319     {
320       if (pulse_state == PS_OFFLINE)
321         {
322           PULSE_STATE(PS_SUBSCRIBE);
323           pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
324           PULSE_ASYNC_INIT_RUN(pa_context_subscribe, ctx, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
325         }
326     }
327   else
328     {
329       if (pulse_state != PS_OFFLINE)
330         {
331           PULSE_STATE(PS_OFFLINE);
332           pulse_op_cancel_all();
333           pulse_shutdown();
334           schedule_update();
335         }
336       if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
337         timer_add_rel(&pulse_connect_timer, 2000);
338     }
339 }
340
341 static void pulse_dump(void)
342 {
343   HASH_FOR_ALL(pulse_client, c)
344     {
345       DBG("## Client #%d: %s host=%s", c->idx, c->name, c->host);
346     }
347   HASH_END_FOR;
348
349   HASH_FOR_ALL(pulse_sink, s)
350     {
351       DBG("## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u",
352         s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute);
353     }
354   HASH_END_FOR;
355
356   HASH_FOR_ALL(pulse_sink_input, s)
357     {
358       DBG("## Sink input #%d: %s client=%d sink=%d channels=%u volume=%u mute=%u",
359         s->idx, s->name, s->client_idx, s->sink_idx, s->channels, s->volume, s->mute);
360     }
361   HASH_END_FOR;
362 }
363
364 static void pulse_connect(struct main_timer *t)
365 {
366   DBG("Pulse: Connecting");
367   timer_del(t);
368
369   clist_init(&pulse_op_list);
370   pulse_client_init();
371   pulse_sink_init();
372   pulse_sink_input_init();
373
374   if (pulse_ctx)
375     pa_context_unref(pulse_ctx);
376   pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
377
378   pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
379   pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
380 }
381
382 static void pulse_init(void)
383 {
384   pmain_init();
385
386   pulse_connect_timer.handler = pulse_connect;
387   timer_add_rel(&pulse_connect_timer, 0);
388 }
389
390 /*** High-level logic ***/
391
392 static struct main_timer update_timer;
393
394 static void update_ring_from_sink(int ring, const char *sink_name)
395 {
396   struct pulse_sink *s = pulse_sink_by_name(sink_name);
397   if (!s)
398     {
399       noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
400       noct_set_button(ring, 0);
401       return;
402     }
403
404   if (s->mute)
405     {
406       noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
407       noct_set_button(ring, 1);
408       return;
409     }
410
411   double vol = pa_sw_volume_to_linear(s->volume);
412   vol = CLAMP(vol, 0, 1);
413   int val = 0x7f * vol;
414   val = CLAMP(val, 12, 0x7f);
415   noct_set_ring(ring, RING_MODE_LEFT, val);
416   noct_set_button(ring, 0);
417 }
418
419 struct client_map {
420   int rotary;
421   const char *client;
422   const char *host;
423   double range;
424 };
425
426 static struct client_map client_map[] = {
427   { 4, "Music Player Daemon",   "albireo",      1 },
428   { 5, "MPlayer",               NULL,           1 },
429   { 6, NULL,                    "ogion",        1 },
430 };
431
432 #define NUM_CLIENTS ARRAY_SIZE(client_map)
433
434 struct client_state {
435   double volume;
436   bool have_muted[2];
437 };
438
439 static struct client_state client_state[NUM_CLIENTS];
440
441 static int find_client_by_rotary(int rotary)
442 {
443   uns i;
444   for (i=0; i < NUM_CLIENTS; i++)
445     if (client_map[i].rotary == rotary)
446       return i;
447   return -1;
448 }
449
450 static bool client_match_sink_input(struct client_map *cm, struct pulse_sink_input *s)
451 {
452   if (s->client_idx < 0 || s->sink_idx < 0)
453     return 0;
454
455   struct pulse_client *c = pulse_client_lookup(s->client_idx);
456   if (!c)
457     return 0;
458
459   return ((!cm->client || !strcmp(cm->client, c->name)) &&
460          (!cm->host || !strcmp(cm->host, c->host)));
461 }
462
463 static void calc_clients(void)
464 {
465   bzero(client_state, sizeof(client_state));
466
467   HASH_FOR_ALL(pulse_sink_input, s)
468     {
469       for (uns i=0; i < NUM_CLIENTS; i++)
470         {
471           struct client_map *cm = &client_map[i];
472           struct client_state *cs = &client_state[i];
473           if (client_match_sink_input(cm, s))
474             {
475               DBG("@@ Client #%d, sink input #%d -> rotary %d", s->client_idx, s->idx, cm->rotary);
476               cs->volume = MAX(cs->volume, s->volume);
477               cs->have_muted[!!s->mute] = 1;
478             }
479         }
480     }
481   HASH_END_FOR;
482 }
483
484 static void update_clients(void)
485 {
486   calc_clients();
487
488   for (uns i=0; i < NUM_CLIENTS; i++)
489     {
490       struct client_map *cm = &client_map[i];
491       struct client_state *cs = &client_state[i];
492       if (!cs->have_muted[0] && !cs->have_muted[1])
493         {
494           noct_set_ring(cm->rotary, RING_MODE_LEFT, 0);
495           noct_set_button(cm->rotary, 0);
496         }
497       else if (!cs->have_muted[0])
498         {
499           noct_set_ring(cm->rotary, RING_MODE_SINGLE_ON, 0x7f);
500           noct_set_button(cm->rotary, 1);
501         }
502       else
503         {
504           double vol = pa_sw_volume_to_linear(cs->volume);
505           vol = CLAMP(vol, 0, cm->range);
506           int val = 0x7f * vol / cm->range;
507           val = CLAMP(val, 12, 0x7f);
508           noct_set_ring(cm->rotary, RING_MODE_LEFT, val);
509           noct_set_button(cm->rotary, 0);
510         }
511     }
512 }
513
514 static void do_update(struct main_timer *t)
515 {
516   timer_del(t);
517   if (!noct_is_ready())
518     {
519       DBG("## UPDATE: Nocturn is not ready");
520       return;
521     }
522
523   static bool dead;
524   if (pulse_state != PS_ONLINE)
525     {
526       DBG("## UPDATE: Pulse is not online");
527       for (int i=0; i<=8; i++)
528         noct_set_ring(i, RING_MODE_LEFT, 0);
529       for (int i=0; i<8; i++)
530         {
531           noct_set_button(i, 1);
532           noct_set_button(i+8, 0);
533         }
534       dead = 1;
535       return;
536     }
537   if (dead)
538     {
539       DBG("## UPDATE: Waking up from the dead");
540       for (int i=0; i<=8; i++)
541         noct_set_ring(i, RING_MODE_LEFT, 0);
542       for (int i=0; i<16; i++)
543         noct_set_button(i, 0);
544       dead = 0;
545     }
546
547   DBG("## UPDATE");
548   pulse_dump();
549
550   update_ring_from_sink(0, "ursarium");
551   update_ring_from_sink(1, "catarium");
552   update_clients();
553 }
554
555 void schedule_update(void)
556 {
557   timer_add_rel(&update_timer, 10);
558 }
559
560 static void update_sink_from_rotary(int delta, const char *sink_name)
561 {
562   struct pulse_sink *s = pulse_sink_by_name(sink_name);
563   if (!s)
564     return;
565
566   double vol = pa_sw_volume_to_linear(s->volume);
567   vol += delta * 0.02;
568   vol = CLAMP(vol, 0, 1);
569   pa_volume_t pavol = pa_sw_volume_from_linear(vol);
570   if (pavol == s->volume)
571     return;
572   pa_cvolume cvol;
573   pa_cvolume_set(&cvol, s->channels, pavol);
574
575   DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
576   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, pulse_ctx, s->idx, &cvol, pulse_success_cb);
577 }
578
579 static void update_client_from_rotary(int rotary, int delta)
580 {
581   int i = find_client_by_rotary(rotary);
582   if (i < 0)
583     return;
584   struct client_map *cm = &client_map[i];
585   struct client_state *cs = &client_state[i];
586
587   calc_clients();
588   double vol = pa_sw_volume_to_linear(cs->volume);
589   vol += delta * 0.02;
590   vol = CLAMP(vol, 0, cm->range);
591   pa_volume_t pavol = pa_sw_volume_from_linear(vol);
592
593   HASH_FOR_ALL(pulse_sink_input, s)
594     {
595       if (client_match_sink_input(cm, s) && s->volume != pavol)
596         {
597           DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
598           pa_cvolume cvol;
599           pa_cvolume_set(&cvol, s->channels, pavol);
600           PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, pulse_ctx, s->idx, &cvol, pulse_success_cb);
601         }
602     }
603   HASH_END_FOR;
604 }
605
606 void notify_rotary(int rotary, int delta)
607 {
608   if (pulse_state != PS_ONLINE)
609     {
610       DBG("## NOTIFY: Pulse is not online");
611       return;
612     }
613
614   switch (rotary)
615     {
616     case 0:
617       update_sink_from_rotary(delta, "ursarium");
618       break;
619     case 1:
620       update_sink_from_rotary(delta, "catarium");
621       break;
622     case 8:
623       update_sink_from_rotary(delta, "ursarium");
624       update_sink_from_rotary(delta, "catarium");
625       break;
626     default:
627       update_client_from_rotary(rotary, delta);
628     }
629 }
630
631 static void update_sink_mute_from_button(int on, const char *sink_name)
632 {
633   if (!on)
634     return;
635
636   struct pulse_sink *s = pulse_sink_by_name(sink_name);
637   if (!s)
638     return;
639
640   DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
641   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, pulse_ctx, s->idx, !s->mute, pulse_success_cb);
642 }
643
644 static void update_client_from_button(int button, int on)
645 {
646   if (button >= 8 || !on)
647     return;
648
649   int i = find_client_by_rotary(button);
650   if (i < 0)
651     return;
652   struct client_map *cm = &client_map[i];
653   struct client_state *cs = &client_state[i];
654
655   calc_clients();
656   if (!cs->have_muted[0] && !cs->have_muted[1])
657     return;
658   uns mute = !cs->have_muted[1];
659
660   HASH_FOR_ALL(pulse_sink_input, s)
661     {
662       if (client_match_sink_input(cm, s))
663         {
664           DBG("@@ Client #%d, sink input #%d: setting mute=%u", s->client_idx, s->idx, mute);
665           PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, pulse_ctx, s->idx, mute, pulse_success_cb);
666         }
667     }
668   HASH_END_FOR;
669 }
670
671 void notify_button(int button, int on)
672 {
673   if (pulse_state != PS_ONLINE)
674     {
675       DBG("## NOTIFY: Pulse is not online");
676       return;
677     }
678
679   switch (button)
680     {
681     case 0:
682       update_sink_mute_from_button(on, "ursarium");
683       break;
684     case 1:
685       update_sink_mute_from_button(on, "catarium");
686       break;
687     default:
688       update_client_from_button(button, on);
689     }
690 }
691
692 int main(int argc UNUSED, char **argv)
693 {
694   log_init(argv[0]);
695   main_init();
696   update_timer.handler = do_update;
697
698   noct_init();
699
700   msg(L_INFO, "Initializing PulseAudio");
701   pulse_init();
702
703   msg(L_INFO, "Entering main loop");
704   main_loop();
705
706   return 0;
707 }