]> mj.ucw.cz Git - ursary.git/blob - pulse.c
Better IR control of lights
[ursary.git] / pulse.c
1 /*
2  *      Asynchronous Interface to PulseAudio
3  *
4  *      (c) 2014--2020 Martin Mares <mj@ucw.cz>
5  */
6
7 #undef 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 enum pulse_state {
21   PS_OFFLINE,
22   PS_SUBSCRIBE,
23   PS_GET_CLIENTS,
24   PS_GET_SINKS,
25   PS_GET_SINK_INPUTS,
26   PS_GET_SOURCES,
27   PS_GET_SERVER,
28   PS_ONLINE,
29 };
30
31 enum pulse_state pulse_state;
32 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
33
34 clist pulse_client_list, pulse_source_list, pulse_sink_list, pulse_sink_input_list;
35
36 static pa_context *pulse_ctx;
37 static struct main_timer pulse_connect_timer;
38
39 /*** Tracking of currently running asynchronous operations ***/
40
41 struct pulse_op {
42   cnode n;
43   pa_operation *o;
44   bool is_init;
45 };
46
47 static clist pulse_op_list;
48
49 static struct pulse_op *pulse_op_new(void)
50 {
51   struct pulse_op *op = xmalloc_zero(sizeof(*op));
52   clist_add_tail(&pulse_op_list, &op->n);
53   return op;
54 }
55
56 static void pulse_op_done(struct pulse_op *op)
57 {
58   if (op->o)
59     pa_operation_unref(op->o);
60   clist_remove(&op->n);
61   xfree(op);
62 }
63
64 static void pulse_op_cancel_all(void)
65 {
66   struct pulse_op *op;
67   while (op = (struct pulse_op *) clist_head(&pulse_op_list))
68     {
69       DBG("Pulse: Cancelling pending operation");
70       pa_operation_cancel(op->o);
71       pulse_op_done(op);
72     }
73 }
74
75 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(pulse_ctx, __VA_ARGS__, _op); } while (0)
76 #define PULSE_ASYNC_INIT_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->is_init = 1; _op->o = name(pulse_ctx, __VA_ARGS__, _op); } while (0)
77
78 static void pulse_success_cb(pa_context *ctx, int success, void *userdata)
79 {
80   if (!success)
81     msg(L_ERROR, "Pulse: Failure reported: %s", pa_strerror(pa_context_errno(ctx)));
82   pulse_op_done(userdata);
83 }
84
85 /*** Debugging dumps ***/
86
87 void pulse_dump(void)
88 {
89   msg(L_DEBUG, "## Server: default_sink=%s", pulse_default_sink_name);
90
91   CLIST_FOR_EACH(struct pulse_client *, c, pulse_client_list)
92     msg(L_DEBUG, "## Client #%d: %s host=%s", c->idx, c->name, c->host);
93
94   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
95     msg(L_DEBUG, "## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u suspended=%u port=%s",
96         s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute, s->suspended, s->active_port);
97
98   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
99     msg(L_DEBUG, "## Sink input #%d: %s client=%d sink=%d channels=%u volume=%u mute=%u",
100         s->idx, s->name, s->client_idx, s->sink_idx, s->channels, s->volume, s->mute);
101 }
102
103 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
104 {
105 #if 0
106   void *iterator = NULL;
107   const char *key;
108
109   while (key = pa_proplist_iterate(pl, &iterator))
110     {
111       const char *val = pa_proplist_gets(pl, key);
112       DBG("   %s = %s", key, val);
113     }
114 #endif
115 }
116
117 /*** Server state ***/
118
119 char *pulse_default_sink_name;
120
121 static void pulse_server_cb(pa_context *ctx UNUSED, const pa_server_info *i, void *userdata)
122 {
123   struct pulse_op *op = userdata;
124
125   DBG("Pulse: SERVER default_sink=%s", i->default_sink_name);
126   SET_STRING(pulse_default_sink_name, i->default_sink_name);
127
128   if (op->is_init)
129     {
130       PULSE_STATE(PS_ONLINE);
131       msg(L_INFO, "PulseAudio is ready");
132     }
133   pulse_op_done(op);
134   schedule_update();
135 }
136
137 void pulse_server_set_default_sink(const char *name)
138 {
139   PULSE_ASYNC_RUN(pa_context_set_default_sink, name, pulse_success_cb);
140 }
141
142 /*** Sources ***/
143
144 #define HASH_NODE struct pulse_source
145 #define HASH_PREFIX(x) pulse_source_##x
146 #define HASH_KEY_ATOMIC idx
147 #define HASH_WANT_CLEANUP
148 #define HASH_WANT_LOOKUP
149 #define HASH_WANT_REMOVE
150 #define HASH_ZERO_FILL
151 #include <ucw/hashtable.h>
152
153 static void pulse_source_cb(pa_context *ctx UNUSED, const pa_source_info *i, int eol, void *userdata)
154 {
155   struct pulse_op *op = userdata;
156
157   if (eol)
158     {
159       if (op->is_init)
160         {
161           PULSE_STATE(PS_GET_SERVER);
162           PULSE_ASYNC_INIT_RUN(pa_context_get_server_info, pulse_server_cb);
163         }
164       pulse_op_done(op);
165       return;
166     }
167
168   DBG("Pulse: SOURCE #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u port=%s",
169     i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state,
170     (i->active_port ? i->active_port->name : "none"));
171   pulse_dump_proplist(i->proplist);
172
173   struct pulse_source *s = pulse_source_lookup(i->index);
174   if (!clist_is_linked(&s->n))
175     clist_add_tail(&pulse_source_list, &s->n);
176   SET_STRING(s->name, i->name);
177   s->channels = i->channel_map.channels;
178   s->volume = pa_cvolume_avg(&i->volume);
179   s->base_volume = i->base_volume;
180   s->mute = i->mute;
181   s->suspended = (i->state == PA_SOURCE_SUSPENDED);
182   SET_STRING(s->active_port, (i->active_port ? i->active_port->name : "none"));
183   schedule_update();
184 }
185
186 static void pulse_source_gone(int idx)
187 {
188   DBG("Pulse: REMOVE SOURCE #%d", idx);
189   struct pulse_source *s = pulse_source_lookup(idx);
190   clist_remove(&s->n);
191   pulse_source_remove(s);
192   schedule_update();
193 }
194
195 struct pulse_source *pulse_source_by_name(const char *name)
196 {
197   CLIST_FOR_EACH(struct pulse_source *, s, pulse_source_list)
198     if (!strcmp(s->name, name))
199       return s;
200   return NULL;
201 }
202
203 struct pulse_source *pulse_source_by_idx(int idx)
204 {
205   return pulse_source_lookup(idx);
206 }
207
208 void pulse_source_set_volume(int idx, pa_cvolume *cvol)
209 {
210   PULSE_ASYNC_RUN(pa_context_set_source_volume_by_index, idx, cvol, pulse_success_cb);
211 }
212
213 void pulse_source_set_mute(int idx, bool mute)
214 {
215   PULSE_ASYNC_RUN(pa_context_set_source_mute_by_index, idx, mute, pulse_success_cb);
216 }
217
218 void pulse_source_set_port(int idx, const char *port)
219 {
220   PULSE_ASYNC_RUN(pa_context_set_source_port_by_index, idx, port, pulse_success_cb);
221 }
222
223 /*** Sink inputs ***/
224
225 #define HASH_NODE struct pulse_sink_input
226 #define HASH_PREFIX(x) pulse_sink_input_##x
227 #define HASH_KEY_ATOMIC idx
228 #define HASH_WANT_CLEANUP
229 #define HASH_WANT_FIND
230 #define HASH_WANT_LOOKUP
231 #define HASH_WANT_REMOVE
232 #define HASH_ZERO_FILL
233 #include <ucw/hashtable.h>
234
235 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
236 {
237   struct pulse_op *op = userdata;
238
239   if (eol)
240     {
241       if (op->is_init)
242         {
243           PULSE_STATE(PS_GET_SOURCES);
244           PULSE_ASYNC_INIT_RUN(pa_context_get_source_info_list, pulse_source_cb);
245         }
246       pulse_op_done(op);
247       return;
248     }
249
250   DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
251     i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
252   pulse_dump_proplist(i->proplist);
253
254   struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
255   if (!clist_is_linked(&s->n))
256     clist_add_tail(&pulse_sink_input_list, &s->n);
257   SET_STRING(s->name, i->name);
258   s->client_idx = i->client;
259   s->sink_idx = i->sink;
260   s->channels = i->channel_map.channels;
261   s->volume = pa_cvolume_avg(&i->volume);
262   s->mute = i->mute;
263   schedule_update();
264 }
265
266 static void pulse_sink_input_gone(int idx)
267 {
268   DBG("Pulse: REMOVE SINK INPUT #%d", idx);
269   struct pulse_sink_input *s = pulse_sink_input_find(idx);
270   if (s)
271     {
272       clist_remove(&s->n);
273       pulse_sink_input_remove(s);
274     }
275   else
276     DBG("Pulse: Removing sink which does not exist");
277   schedule_update();
278 }
279
280 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
281 {
282   PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
283 }
284
285 void pulse_sink_input_set_mute(int idx, bool mute)
286 {
287   PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
288 }
289
290 void pulse_sink_input_move(int input_idx, int sink_idx)
291 {
292   PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
293 }
294
295 /*** Sinks ***/
296
297 #define HASH_NODE struct pulse_sink
298 #define HASH_PREFIX(x) pulse_sink_##x
299 #define HASH_KEY_ATOMIC idx
300 #define HASH_WANT_CLEANUP
301 #define HASH_WANT_LOOKUP
302 #define HASH_WANT_REMOVE
303 #define HASH_ZERO_FILL
304 #include <ucw/hashtable.h>
305
306 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
307 {
308   struct pulse_op *op = userdata;
309
310   if (eol)
311     {
312       if (op->is_init)
313         {
314           PULSE_STATE(PS_GET_SINK_INPUTS);
315           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
316         }
317       pulse_op_done(op);
318       return;
319     }
320
321   DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u port=%s",
322     i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state,
323     (i->active_port ? i->active_port->name : "none"));
324   pulse_dump_proplist(i->proplist);
325
326   struct pulse_sink *s = pulse_sink_lookup(i->index);
327   if (!clist_is_linked(&s->n))
328     clist_add_tail(&pulse_sink_list, &s->n);
329   SET_STRING(s->name, i->name);
330   s->channels = i->channel_map.channels;
331   s->volume = pa_cvolume_avg(&i->volume);
332   s->base_volume = i->base_volume;
333   s->mute = i->mute;
334   s->suspended = (i->state == PA_SINK_SUSPENDED);
335   SET_STRING(s->active_port, (i->active_port ? i->active_port->name : "none"));
336   schedule_update();
337 }
338
339 static void pulse_sink_gone(int idx)
340 {
341   DBG("Pulse: REMOVE SINK #%d", idx);
342   struct pulse_sink *s = pulse_sink_lookup(idx);
343   clist_remove(&s->n);
344   pulse_sink_remove(s);
345   schedule_update();
346 }
347
348 struct pulse_sink *pulse_sink_by_name(const char *name)
349 {
350   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
351     if (!strcmp(s->name, name))
352       return s;
353   return NULL;
354 }
355
356 struct pulse_sink *pulse_sink_by_idx(int idx)
357 {
358   return pulse_sink_lookup(idx);
359 }
360
361 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
362 {
363   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
364 }
365
366 void pulse_sink_set_mute(int idx, bool mute)
367 {
368   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
369 }
370
371 void pulse_sink_set_port(int idx, const char *port)
372 {
373   PULSE_ASYNC_RUN(pa_context_set_sink_port_by_index, idx, port, pulse_success_cb);
374 }
375
376 /*** Clients ***/
377
378 #define HASH_NODE struct pulse_client
379 #define HASH_PREFIX(x) pulse_client_##x
380 #define HASH_KEY_ATOMIC idx
381 #define HASH_WANT_CLEANUP
382 #define HASH_WANT_FIND
383 #define HASH_WANT_LOOKUP
384 #define HASH_WANT_REMOVE
385 #define HASH_ZERO_FILL
386 #include <ucw/hashtable.h>
387
388 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
389 {
390   struct pulse_op *op = userdata;
391
392   if (eol)
393     {
394       if (op->is_init)
395         {
396           PULSE_STATE(PS_GET_SINKS);
397           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
398         }
399       pulse_op_done(op);
400       return;
401     }
402
403   char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
404   DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
405     i->index, i->name, i->owner_module, i->driver, host);
406   pulse_dump_proplist(i->proplist);
407
408   struct pulse_client *c = pulse_client_lookup(i->index);
409   if (!clist_is_linked(&c->n))
410     clist_add_tail(&pulse_client_list, &c->n);
411   SET_STRING(c->name, i->name);
412   SET_STRING(c->host, host);
413   schedule_update();
414 }
415
416 static void pulse_client_gone(int idx)
417 {
418   DBG("Pulse: REMOVE CLIENT #%d", idx);
419   struct pulse_client *c = pulse_client_find(idx);
420   if (c)
421     {
422       clist_remove(&c->n);
423       pulse_client_remove(c);
424       schedule_update();
425     }
426 }
427
428 struct pulse_client *pulse_client_by_idx(int idx)
429 {
430   return pulse_client_find(idx);
431 }
432
433 /*** Events ***/
434
435 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
436 {
437   pulse_op_done(userdata);
438
439   if (!success)
440     msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
441
442   PULSE_STATE(PS_GET_CLIENTS);
443   PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
444 }
445
446 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
447 {
448   DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
449
450   uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
451   uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
452   switch (object)
453     {
454     case PA_SUBSCRIPTION_EVENT_CLIENT:
455       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
456         PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
457       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
458         pulse_client_gone(idx);
459       break;
460     case PA_SUBSCRIPTION_EVENT_SOURCE:
461       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
462         PULSE_ASYNC_RUN(pa_context_get_source_info_by_index, idx, pulse_source_cb);
463       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
464         pulse_source_gone(idx);
465       break;
466     case PA_SUBSCRIPTION_EVENT_SINK:
467       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
468         PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
469       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
470         pulse_sink_gone(idx);
471       break;
472     case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
473       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
474         PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
475       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
476         pulse_sink_input_gone(idx);
477       break;
478     case PA_SUBSCRIPTION_EVENT_SERVER:
479       if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
480         PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
481       break;
482     }
483 }
484
485 /*** Server state ***/
486
487 static void pulse_shutdown(void)
488 {
489   DBG("Pulse: Shutting down");
490   pulse_client_cleanup();
491   pulse_source_cleanup();
492   pulse_sink_cleanup();
493   pulse_sink_input_cleanup();
494 }
495
496 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
497 {
498   int state = pa_context_get_state(ctx);
499   DBG("Pulse: State callback, new state = %d", state);
500   if (state == PA_CONTEXT_READY)
501     {
502       if (pulse_state == PS_OFFLINE)
503         {
504           PULSE_STATE(PS_SUBSCRIBE);
505           pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
506           PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
507         }
508     }
509   else
510     {
511       if (pulse_state != PS_OFFLINE)
512         {
513           msg(L_INFO, "Lost connection to PulseAudio");
514           PULSE_STATE(PS_OFFLINE);
515           pulse_op_cancel_all();
516           pulse_shutdown();
517           schedule_update();
518         }
519       if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
520         timer_add_rel(&pulse_connect_timer, 2000);
521     }
522 }
523
524 static void pulse_connect(struct main_timer *t)
525 {
526   msg(L_DEBUG, "Connecting to PulseAudio");
527   timer_del(t);
528
529   clist_init(&pulse_op_list);
530   clist_init(&pulse_client_list);
531   clist_init(&pulse_source_list);
532   clist_init(&pulse_sink_list);
533   clist_init(&pulse_sink_input_list);
534   pulse_client_init();
535   pulse_source_init();
536   pulse_sink_init();
537   pulse_sink_input_init();
538
539   if (pulse_ctx)
540     pa_context_unref(pulse_ctx);
541   pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
542
543   pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
544   pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
545 }
546
547 bool pulse_is_ready(void)
548 {
549   return (pulse_state == PS_ONLINE);
550 }
551
552 void pulse_init(void)
553 {
554   pmain_init();
555
556   pulse_connect_timer.handler = pulse_connect;
557   timer_add_rel(&pulse_connect_timer, 0);
558 }