4 #include <ucw/clists.h>
5 #include <ucw/mainloop.h>
6 #include <ucw/stkstring.h>
12 #include <pulse/pulseaudio.h>
16 /*** Interface to PulseAudio ***/
18 static pa_context *pulse_ctx;
20 static void pulse_dump(void);
31 static enum pulse_state pulse_state;
32 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
34 // Tracking of currently running asynchronous operations
41 static clist pulse_op_list;
43 static struct pulse_op *pulse_op_new(void)
45 struct pulse_op *op = xmalloc_zero(sizeof(*op));
46 clist_add_tail(&pulse_op_list, &op->n);
50 static void pulse_op_done(struct pulse_op *op)
53 pa_operation_unref(op->o);
58 static void pulse_op_cancel_all(void)
61 while (op = (struct pulse_op *) clist_head(&pulse_op_list))
63 DBG("Pulse: Cancelling pending operation");
64 pa_operation_cancel(op->o);
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)
72 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
75 msg(L_ERROR, "Pulse: Failure reported");
76 pulse_op_done(userdata);
79 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
82 void *iterator = NULL;
85 while (key = pa_proplist_iterate(pl, &iterator))
87 const char *val = pa_proplist_gets(pl, key);
88 DBG(" %s = %s", key, val);
93 struct pulse_sink_input {
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>
111 #define SET_STRING(_field, _val) do { if (!_field || strcmp(_field, _val)) { xfree(_field); _field = xstrdup(_val); } } while (0)
113 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
115 struct pulse_op *op = userdata;
121 PULSE_STATE(PS_ONLINE);
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);
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);
141 static void pulse_sink_input_gone(int idx)
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);
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>
166 static void pulse_sink_cb(pa_context *ctx, const pa_sink_info *i, int eol, void *userdata)
168 struct pulse_op *op = userdata;
174 PULSE_STATE(PS_GET_SINK_INPUTS);
175 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, ctx, pulse_sink_input_cb);
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);
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;
193 static void pulse_sink_gone(int idx)
195 DBG("Pulse: REMOVE SINK #%d", idx);
196 struct pulse_sink *s = pulse_sink_lookup(idx);
197 pulse_sink_remove(s);
201 static struct pulse_sink *pulse_sink_by_name(const char *name)
203 HASH_FOR_ALL(pulse_sink, s)
205 if (!strcmp(s->name, name))
212 struct pulse_client {
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>
227 static void pulse_client_cb(pa_context *ctx, const pa_client_info *i, int eol, void *userdata)
229 struct pulse_op *op = userdata;
235 PULSE_STATE(PS_GET_SINKS);
236 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, ctx, pulse_sink_cb);
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);
247 struct pulse_client *c = pulse_client_lookup(i->index);
248 SET_STRING(c->name, i->name);
249 SET_STRING(c->host, host);
253 static void pulse_client_gone(int idx)
255 DBG("Pulse: REMOVE CLIENT #%d", idx);
256 struct pulse_client *c = pulse_client_lookup(idx);
257 pulse_client_remove(c);
261 static void pulse_subscribe_done_cb(pa_context *ctx, int success, void *userdata)
263 pulse_op_done(userdata);
266 msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
268 PULSE_STATE(PS_GET_CLIENTS);
269 PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, ctx, pulse_client_cb);
272 static void pulse_event_cb(pa_context *ctx, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
274 DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
276 uns object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
277 uns action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
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);
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);
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);
301 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
303 int state = pa_context_get_state(ctx);
304 DBG("Pulse: State callback, new state = %d", state);
305 if (state == PA_CONTEXT_READY)
307 if (pulse_state == PS_OFFLINE)
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);
316 if (pulse_state != PS_OFFLINE)
318 PULSE_STATE(PS_OFFLINE);
319 pulse_op_cancel_all();
320 // FIXME: Reset all data structures
325 static void pulse_dump(void)
327 HASH_FOR_ALL(pulse_client, c)
329 DBG("## Client #%d: %s host=%s", c->idx, c->name, c->host);
333 HASH_FOR_ALL(pulse_sink, s)
335 DBG("## Sink #%d: %s volume=%u base_vol=%u mute=%u",
336 s->idx, s->name, s->volume, s->base_volume, s->mute);
340 HASH_FOR_ALL(pulse_sink_input, s)
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);
348 static void pulse_init(void)
351 clist_init(&pulse_op_list);
354 pulse_sink_input_init();
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);
361 /*** High-level logic ***/
363 static struct main_timer update_timer;
365 static void update_ring_from_sink(int ring, const char *sink_name)
367 struct pulse_sink *s = pulse_sink_by_name(sink_name);
370 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
371 noct_set_button(ring, 0);
377 noct_set_ring(ring, RING_MODE_SINGLE_ON, 0x7f);
378 noct_set_button(ring, 1);
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);
390 static void do_update(struct main_timer *t)
393 if (pulse_state != PS_ONLINE)
395 DBG("## UPDATE: Pulse is not online");
398 if (!noct_is_ready())
400 DBG("## UPDATE: Nocturn is not ready");
407 update_ring_from_sink(0, "ursarium");
408 update_ring_from_sink(1, "catarium");
411 void schedule_update(void)
413 timer_add_rel(&update_timer, 10); // FIXME
416 static void update_sink_from_rotary(int delta, const char *sink_name)
418 struct pulse_sink *s = pulse_sink_by_name(sink_name);
422 double vol = pa_sw_volume_to_linear(s->volume);
424 vol = CLAMP(vol, 0, 1);
426 pa_cvolume_set(&cvol, 2, pa_sw_volume_from_linear(vol));
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);
432 void notify_rotary(int rotary, int delta)
434 if (pulse_state != PS_ONLINE)
436 DBG("## NOTIFY: Pulse is not inline");
443 update_sink_from_rotary(delta, "ursarium");
446 update_sink_from_rotary(delta, "catarium");
449 update_sink_from_rotary(delta, "ursarium");
450 update_sink_from_rotary(delta, "catarium");
455 static void update_sink_mute_from_button(int on, const char *sink_name)
460 struct pulse_sink *s = pulse_sink_by_name(sink_name);
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);
468 void notify_button(int button, int on)
470 if (pulse_state != PS_ONLINE)
472 DBG("## NOTIFY: Pulse is not inline");
479 update_sink_mute_from_button(on, "ursarium");
482 update_sink_mute_from_button(on, "catarium");
487 int main(int argc UNUSED, char **argv)
491 update_timer.handler = do_update;
495 msg(L_INFO, "Initializing PulseAudio");
498 msg(L_INFO, "Entering main loop");