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