]> mj.ucw.cz Git - ursary.git/blob - pulse.c
Document button assignment changes
[ursary.git] / pulse.c
1 /*
2  *      Asynchronous Interface to PulseAudio
3  *
4  *      (c) 2014--2018 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_SERVER,
27   PS_ONLINE,
28 };
29
30 enum pulse_state pulse_state;
31 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
32
33 clist pulse_client_list, pulse_sink_list, pulse_sink_input_list;
34
35 static pa_context *pulse_ctx;
36 static struct main_timer pulse_connect_timer;
37
38 /*** Tracking of currently running asynchronous operations ***/
39
40 struct pulse_op {
41   cnode n;
42   pa_operation *o;
43   bool is_init;
44 };
45
46 static clist pulse_op_list;
47
48 static struct pulse_op *pulse_op_new(void)
49 {
50   struct pulse_op *op = xmalloc_zero(sizeof(*op));
51   clist_add_tail(&pulse_op_list, &op->n);
52   return op;
53 }
54
55 static void pulse_op_done(struct pulse_op *op)
56 {
57   if (op->o)
58     pa_operation_unref(op->o);
59   clist_remove(&op->n);
60   xfree(op);
61 }
62
63 static void pulse_op_cancel_all(void)
64 {
65   struct pulse_op *op;
66   while (op = (struct pulse_op *) clist_head(&pulse_op_list))
67     {
68       DBG("Pulse: Cancelling pending operation");
69       pa_operation_cancel(op->o);
70       pulse_op_done(op);
71     }
72 }
73
74 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(pulse_ctx, __VA_ARGS__, _op); } while (0)
75 #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)
76
77 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
78 {
79   if (!success)
80     msg(L_ERROR, "Pulse: Failure reported");
81   pulse_op_done(userdata);
82 }
83
84 /*** Debugging dumps ***/
85
86 void pulse_dump(void)
87 {
88   msg(L_DEBUG, "## Server: default_sink=%s", pulse_default_sink_name);
89
90   CLIST_FOR_EACH(struct pulse_client *, c, pulse_client_list)
91     msg(L_DEBUG, "## Client #%d: %s host=%s", c->idx, c->name, c->host);
92
93   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
94     msg(L_DEBUG, "## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u suspended=%u port=%s",
95         s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute, s->suspended, s->active_port);
96
97   CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
98     msg(L_DEBUG, "## Sink input #%d: %s client=%d sink=%d channels=%u volume=%u mute=%u",
99         s->idx, s->name, s->client_idx, s->sink_idx, s->channels, s->volume, s->mute);
100 }
101
102 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
103 {
104 #if 0
105   void *iterator = NULL;
106   const char *key;
107
108   while (key = pa_proplist_iterate(pl, &iterator))
109     {
110       const char *val = pa_proplist_gets(pl, key);
111       DBG("   %s = %s", key, val);
112     }
113 #endif
114 }
115
116 /*** Server state ***/
117
118 char *pulse_default_sink_name;
119
120 static void pulse_server_cb(pa_context *ctx UNUSED, const pa_server_info *i, void *userdata)
121 {
122   struct pulse_op *op = userdata;
123
124   DBG("Pulse: SERVER default_sink=%s", i->default_sink_name);
125   SET_STRING(pulse_default_sink_name, i->default_sink_name);
126
127   if (op->is_init)
128     {
129       PULSE_STATE(PS_ONLINE);
130       msg(L_INFO, "PulseAudio is ready");
131     }
132   pulse_op_done(op);
133   schedule_update();
134 }
135
136 void pulse_server_set_default_sink(const char *name)
137 {
138   PULSE_ASYNC_RUN(pa_context_set_default_sink, name, pulse_success_cb);
139 }
140
141 /*** Sink inputs ***/
142
143 #define HASH_NODE struct pulse_sink_input
144 #define HASH_PREFIX(x) pulse_sink_input_##x
145 #define HASH_KEY_ATOMIC idx
146 #define HASH_WANT_CLEANUP
147 #define HASH_WANT_FIND
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_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_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: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
169     i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
170   pulse_dump_proplist(i->proplist);
171
172   struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
173   if (!clist_is_linked(&s->n))
174     clist_add_tail(&pulse_sink_input_list, &s->n);
175   SET_STRING(s->name, i->name);
176   s->client_idx = i->client;
177   s->sink_idx = i->sink;
178   s->channels = i->channel_map.channels;
179   s->volume = pa_cvolume_avg(&i->volume);
180   s->mute = i->mute;
181   schedule_update();
182 }
183
184 static void pulse_sink_input_gone(int idx)
185 {
186   DBG("Pulse: REMOVE SINK INPUT #%d", idx);
187   struct pulse_sink_input *s = pulse_sink_input_find(idx);
188   if (s)
189     {
190       clist_remove(&s->n);
191       pulse_sink_input_remove(s);
192     }
193   else
194     DBG("Pulse: Removing sink which does not exist");
195   schedule_update();
196 }
197
198 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
199 {
200   PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
201 }
202
203 void pulse_sink_input_set_mute(int idx, bool mute)
204 {
205   PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
206 }
207
208 void pulse_sink_input_move(int input_idx, int sink_idx)
209 {
210   PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
211 }
212
213 /*** Sinks ***/
214
215 #define HASH_NODE struct pulse_sink
216 #define HASH_PREFIX(x) pulse_sink_##x
217 #define HASH_KEY_ATOMIC idx
218 #define HASH_WANT_CLEANUP
219 #define HASH_WANT_LOOKUP
220 #define HASH_WANT_REMOVE
221 #define HASH_ZERO_FILL
222 #include <ucw/hashtable.h>
223
224 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
225 {
226   struct pulse_op *op = userdata;
227
228   if (eol)
229     {
230       if (op->is_init)
231         {
232           PULSE_STATE(PS_GET_SINK_INPUTS);
233           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
234         }
235       pulse_op_done(op);
236       return;
237     }
238
239   DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u port=%s",
240     i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state,
241     (i->active_port ? i->active_port->name : "none"));
242   pulse_dump_proplist(i->proplist);
243
244   struct pulse_sink *s = pulse_sink_lookup(i->index);
245   if (!clist_is_linked(&s->n))
246     clist_add_tail(&pulse_sink_list, &s->n);
247   SET_STRING(s->name, i->name);
248   s->channels = i->channel_map.channels;
249   s->volume = pa_cvolume_avg(&i->volume);
250   s->base_volume = i->base_volume;
251   s->mute = i->mute;
252   s->suspended = (i->state == PA_SINK_SUSPENDED);
253   SET_STRING(s->active_port, (i->active_port ? i->active_port->name : "none"));
254   schedule_update();
255 }
256
257 static void pulse_sink_gone(int idx)
258 {
259   DBG("Pulse: REMOVE SINK #%d", idx);
260   struct pulse_sink *s = pulse_sink_lookup(idx);
261   clist_remove(&s->n);
262   pulse_sink_remove(s);
263   schedule_update();
264 }
265
266 struct pulse_sink *pulse_sink_by_name(const char *name)
267 {
268   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
269     if (!strcmp(s->name, name))
270       return s;
271   return NULL;
272 }
273
274 struct pulse_sink *pulse_sink_by_idx(int idx)
275 {
276   return pulse_sink_lookup(idx);
277 }
278
279 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
280 {
281   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
282 }
283
284 void pulse_sink_set_mute(int idx, bool mute)
285 {
286   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
287 }
288
289 void pulse_sink_set_port(int idx, const char *port)
290 {
291   PULSE_ASYNC_RUN(pa_context_set_sink_port_by_index, idx, port, pulse_success_cb);
292 }
293
294 /*** Clients ***/
295
296 #define HASH_NODE struct pulse_client
297 #define HASH_PREFIX(x) pulse_client_##x
298 #define HASH_KEY_ATOMIC idx
299 #define HASH_WANT_CLEANUP
300 #define HASH_WANT_FIND
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_client_cb(pa_context *ctx UNUSED, const pa_client_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_SINKS);
315           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
316         }
317       pulse_op_done(op);
318       return;
319     }
320
321   char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
322   DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
323     i->index, i->name, i->owner_module, i->driver, host);
324   pulse_dump_proplist(i->proplist);
325
326   struct pulse_client *c = pulse_client_lookup(i->index);
327   if (!clist_is_linked(&c->n))
328     clist_add_tail(&pulse_client_list, &c->n);
329   SET_STRING(c->name, i->name);
330   SET_STRING(c->host, host);
331   schedule_update();
332 }
333
334 static void pulse_client_gone(int idx)
335 {
336   DBG("Pulse: REMOVE CLIENT #%d", idx);
337   struct pulse_client *c = pulse_client_find(idx);
338   if (c)
339     {
340       clist_remove(&c->n);
341       pulse_client_remove(c);
342       schedule_update();
343     }
344 }
345
346 struct pulse_client *pulse_client_by_idx(int idx)
347 {
348   return pulse_client_find(idx);
349 }
350
351 /*** Events ***/
352
353 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
354 {
355   pulse_op_done(userdata);
356
357   if (!success)
358     msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
359
360   PULSE_STATE(PS_GET_CLIENTS);
361   PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
362 }
363
364 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
365 {
366   DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
367
368   uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
369   uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
370   switch (object)
371     {
372     case PA_SUBSCRIPTION_EVENT_CLIENT:
373       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
374         PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
375       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
376         pulse_client_gone(idx);
377       break;
378     case PA_SUBSCRIPTION_EVENT_SINK:
379       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
380         PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
381       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
382         pulse_sink_gone(idx);
383       break;
384     case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
385       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
386         PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
387       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
388         pulse_sink_input_gone(idx);
389       break;
390     case PA_SUBSCRIPTION_EVENT_SERVER:
391       if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
392         PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
393       break;
394     }
395 }
396
397 /*** Server state ***/
398
399 static void pulse_shutdown(void)
400 {
401   DBG("Pulse: Shutting down");
402   pulse_client_cleanup();
403   pulse_sink_cleanup();
404   pulse_sink_input_cleanup();
405 }
406
407 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
408 {
409   int state = pa_context_get_state(ctx);
410   DBG("Pulse: State callback, new state = %d", state);
411   if (state == PA_CONTEXT_READY)
412     {
413       if (pulse_state == PS_OFFLINE)
414         {
415           PULSE_STATE(PS_SUBSCRIBE);
416           pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
417           PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
418         }
419     }
420   else
421     {
422       if (pulse_state != PS_OFFLINE)
423         {
424           msg(L_INFO, "Lost connection to PulseAudio");
425           PULSE_STATE(PS_OFFLINE);
426           pulse_op_cancel_all();
427           pulse_shutdown();
428           schedule_update();
429         }
430       if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
431         timer_add_rel(&pulse_connect_timer, 2000);
432     }
433 }
434
435 static void pulse_connect(struct main_timer *t)
436 {
437   msg(L_DEBUG, "Connecting to PulseAudio");
438   timer_del(t);
439
440   clist_init(&pulse_op_list);
441   clist_init(&pulse_client_list);
442   clist_init(&pulse_sink_list);
443   clist_init(&pulse_sink_input_list);
444   pulse_client_init();
445   pulse_sink_init();
446   pulse_sink_input_init();
447
448   if (pulse_ctx)
449     pa_context_unref(pulse_ctx);
450   pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
451
452   pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
453   pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
454 }
455
456 bool pulse_is_ready(void)
457 {
458   return (pulse_state == PS_ONLINE);
459 }
460
461 void pulse_init(void)
462 {
463   pmain_init();
464
465   pulse_connect_timer.handler = pulse_connect;
466   timer_add_rel(&pulse_connect_timer, 0);
467 }