]> mj.ucw.cz Git - misc.git/blob - ursaryd/ut.c
0468955ed61ba778ed923c44659be3225c406ada
[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
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, 0, 0x7f);
386   noct_set_ring(ring, RING_MODE_LEFT, val);
387   noct_set_button(ring, 0);
388 }
389
390 static void do_update(struct main_timer *t)
391 {
392   timer_del(t);
393   if (pulse_state != PS_ONLINE)
394     {
395       DBG("## UPDATE: Pulse is not online");
396       return;
397     }
398   if (!noct_is_ready())
399     {
400       DBG("## UPDATE: Nocturn is not ready");
401       return;
402     }
403
404   DBG("## UPDATE");
405   pulse_dump();
406
407   update_ring_from_sink(0, "ursarium");
408   update_ring_from_sink(1, "catarium");
409 }
410
411 void schedule_update(void)
412 {
413   timer_add_rel(&update_timer, 10);     // FIXME
414 }
415
416 static void update_sink_from_rotary(int delta, const char *sink_name)
417 {
418   struct pulse_sink *s = pulse_sink_by_name(sink_name);
419   if (!s)
420     return;
421
422   double vol = pa_sw_volume_to_linear(s->volume);
423   vol += delta * 0.02;
424   vol = CLAMP(vol, 0, 1);
425   pa_cvolume cvol;
426   pa_cvolume_set(&cvol, 2, pa_sw_volume_from_linear(vol));
427
428   DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
429   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, pulse_ctx, s->idx, &cvol, pulse_success_cb);
430 }
431
432 void notify_rotary(int rotary, int delta)
433 {
434   if (pulse_state != PS_ONLINE)
435     {
436       DBG("## NOTIFY: Pulse is not inline");
437       return;
438     }
439
440   switch (rotary)
441     {
442     case 0:
443       update_sink_from_rotary(delta, "ursarium");
444       break;
445     case 1:
446       update_sink_from_rotary(delta, "catarium");
447       break;
448     case 8:
449       update_sink_from_rotary(delta, "ursarium");
450       update_sink_from_rotary(delta, "catarium");
451       break;
452     }
453 }
454
455 static void update_sink_mute_from_button(int on, const char *sink_name)
456 {
457   if (!on)
458     return;
459
460   struct pulse_sink *s = pulse_sink_by_name(sink_name);
461   if (!s)
462     return;
463
464   DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
465   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, pulse_ctx, s->idx, !s->mute, pulse_success_cb);
466 }
467
468 void notify_button(int button, int on)
469 {
470   if (pulse_state != PS_ONLINE)
471     {
472       DBG("## NOTIFY: Pulse is not inline");
473       return;
474     }
475
476   switch (button)
477     {
478     case 0:
479       update_sink_mute_from_button(on, "ursarium");
480       break;
481     case 1:
482       update_sink_mute_from_button(on, "catarium");
483       break;
484     }
485 }
486
487 int main(int argc UNUSED, char **argv)
488 {
489   log_init(argv[0]);
490   main_init();
491   update_timer.handler = do_update;
492
493   noct_init();
494
495   msg(L_INFO, "Initializing PulseAudio");
496   pulse_init();
497
498   msg(L_INFO, "Entering main loop");
499   main_loop();
500
501   return 0;
502 }