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, 12, 0x7f);
386 noct_set_ring(ring, RING_MODE_LEFT, val);
387 noct_set_button(ring, 0);
397 static struct client_map client_map[] = {
398 { 4, "Music Player Daemon", "albireo", 1 },
399 { 5, "MPlayer", NULL, 1 },
400 { 6, NULL, "ogion", 1 },
403 #define NUM_CLIENTS ARRAY_SIZE(client_map)
405 struct client_state {
410 static struct client_state client_state[NUM_CLIENTS];
412 static int find_client_by_rotary(int rotary)
415 for (i=0; i < NUM_CLIENTS; i++)
416 if (client_map[i].rotary == rotary)
421 static bool client_match_sink_input(struct client_map *cm, struct pulse_sink_input *s)
423 if (s->client_idx < 0 || s->sink_idx < 0)
426 struct pulse_client *c = pulse_client_lookup(s->client_idx);
430 return ((!cm->client || !strcmp(cm->client, c->name)) &&
431 (!cm->host || !strcmp(cm->host, c->host)));
434 static void calc_clients(void)
436 bzero(client_state, sizeof(client_state));
438 HASH_FOR_ALL(pulse_sink_input, s)
440 for (uns i=0; i < NUM_CLIENTS; i++)
442 struct client_map *cm = &client_map[i];
443 struct client_state *cs = &client_state[i];
444 if (client_match_sink_input(cm, s))
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;
455 static void update_clients(void)
459 for (uns i=0; i < NUM_CLIENTS; i++)
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])
465 noct_set_ring(cm->rotary, RING_MODE_LEFT, 0);
466 noct_set_button(cm->rotary, 0);
468 else if (!cs->have_muted[0])
470 noct_set_ring(cm->rotary, RING_MODE_SINGLE_ON, 0x7f);
471 noct_set_button(cm->rotary, 1);
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);
485 static void do_update(struct main_timer *t)
488 if (pulse_state != PS_ONLINE)
490 DBG("## UPDATE: Pulse is not online");
493 if (!noct_is_ready())
495 DBG("## UPDATE: Nocturn is not ready");
502 update_ring_from_sink(0, "ursarium");
503 update_ring_from_sink(1, "catarium");
507 void schedule_update(void)
509 timer_add_rel(&update_timer, 10); // FIXME
512 static void update_sink_from_rotary(int delta, const char *sink_name)
514 struct pulse_sink *s = pulse_sink_by_name(sink_name);
518 double vol = pa_sw_volume_to_linear(s->volume);
520 vol = CLAMP(vol, 0, 1);
521 pa_volume_t pavol = pa_sw_volume_from_linear(vol);
522 if (pavol == s->volume)
525 pa_cvolume_set(&cvol, 2, pavol);
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);
531 static void update_client_from_rotary(int rotary, int delta)
533 int i = find_client_by_rotary(rotary);
536 struct client_map *cm = &client_map[i];
537 struct client_state *cs = &client_state[i];
540 double vol = pa_sw_volume_to_linear(cs->volume);
542 vol = CLAMP(vol, 0, cm->range);
543 pa_volume_t pavol = pa_sw_volume_from_linear(vol);
545 HASH_FOR_ALL(pulse_sink_input, s)
547 if (client_match_sink_input(cm, s) && s->volume != pavol)
549 DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
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);
558 void notify_rotary(int rotary, int delta)
560 if (pulse_state != PS_ONLINE)
562 DBG("## NOTIFY: Pulse is not inline");
569 update_sink_from_rotary(delta, "ursarium");
572 update_sink_from_rotary(delta, "catarium");
575 update_sink_from_rotary(delta, "ursarium");
576 update_sink_from_rotary(delta, "catarium");
579 update_client_from_rotary(rotary, delta);
583 static void update_sink_mute_from_button(int on, const char *sink_name)
588 struct pulse_sink *s = pulse_sink_by_name(sink_name);
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);
596 static void update_client_from_button(int button, int on)
598 if (button >= 8 || !on)
601 int i = find_client_by_rotary(button);
604 struct client_map *cm = &client_map[i];
605 struct client_state *cs = &client_state[i];
608 if (!cs->have_muted[0] && !cs->have_muted[1])
610 uns mute = !cs->have_muted[1];
612 HASH_FOR_ALL(pulse_sink_input, s)
614 if (client_match_sink_input(cm, s))
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);
623 void notify_button(int button, int on)
625 if (pulse_state != PS_ONLINE)
627 DBG("## NOTIFY: Pulse is not inline");
634 update_sink_mute_from_button(on, "ursarium");
637 update_sink_mute_from_button(on, "catarium");
640 update_client_from_button(button, on);
644 int main(int argc UNUSED, char **argv)
648 update_timer.handler = do_update;
652 msg(L_INFO, "Initializing PulseAudio");
655 msg(L_INFO, "Entering main loop");