From 420b4ecd444e1fef29ac0925961b41e6e387c5c6 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 9 Nov 2014 16:35:17 +0100 Subject: [PATCH] Ursary: Clients --- ursaryd/ut.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 159 insertions(+), 2 deletions(-) diff --git a/ursaryd/ut.c b/ursaryd/ut.c index 0468955..9495b1a 100644 --- a/ursaryd/ut.c +++ b/ursaryd/ut.c @@ -382,11 +382,106 @@ static void update_ring_from_sink(int ring, const char *sink_name) double vol = pa_sw_volume_to_linear(s->volume); vol = CLAMP(vol, 0, 1); int val = 0x7f * vol; - val = CLAMP(val, 0, 0x7f); + val = CLAMP(val, 12, 0x7f); noct_set_ring(ring, RING_MODE_LEFT, val); noct_set_button(ring, 0); } +struct client_map { + int rotary; + const char *client; + const char *host; + double range; +}; + +static struct client_map client_map[] = { + { 4, "Music Player Daemon", "albireo", 1 }, + { 5, "MPlayer", NULL, 1 }, + { 6, NULL, "ogion", 1 }, +}; + +#define NUM_CLIENTS ARRAY_SIZE(client_map) + +struct client_state { + double volume; + bool have_muted[2]; +}; + +static struct client_state client_state[NUM_CLIENTS]; + +static int find_client_by_rotary(int rotary) +{ + uns i; + for (i=0; i < NUM_CLIENTS; i++) + if (client_map[i].rotary == rotary) + return i; + return -1; +} + +static bool client_match_sink_input(struct client_map *cm, struct pulse_sink_input *s) +{ + if (s->client_idx < 0 || s->sink_idx < 0) + return 0; + + struct pulse_client *c = pulse_client_lookup(s->client_idx); + if (!c) + return 0; + + return ((!cm->client || !strcmp(cm->client, c->name)) && + (!cm->host || !strcmp(cm->host, c->host))); +} + +static void calc_clients(void) +{ + bzero(client_state, sizeof(client_state)); + + HASH_FOR_ALL(pulse_sink_input, s) + { + for (uns i=0; i < NUM_CLIENTS; i++) + { + struct client_map *cm = &client_map[i]; + struct client_state *cs = &client_state[i]; + if (client_match_sink_input(cm, s)) + { + DBG("@@ Client #%d, sink input #%d -> rotary %d", s->client_idx, s->idx, cm->rotary); + cs->volume = MAX(cs->volume, s->volume); + cs->have_muted[!!s->mute] = 1; + } + } + } + HASH_END_FOR; +} + +static void update_clients(void) +{ + calc_clients(); + + for (uns i=0; i < NUM_CLIENTS; i++) + { + struct client_map *cm = &client_map[i]; + struct client_state *cs = &client_state[i]; + if (!cs->have_muted[0] && !cs->have_muted[1]) + { + noct_set_ring(cm->rotary, RING_MODE_LEFT, 0); + noct_set_button(cm->rotary, 0); + } + else if (!cs->have_muted[0]) + { + noct_set_ring(cm->rotary, RING_MODE_SINGLE_ON, 0x7f); + noct_set_button(cm->rotary, 1); + } + else + { + double vol = pa_sw_volume_to_linear(cs->volume); + vol = CLAMP(vol, 0, cm->range); + int val = 0x7f * vol / cm->range; + val = CLAMP(val, 12, 0x7f); + noct_set_ring(cm->rotary, RING_MODE_LEFT, val); + noct_set_button(cm->rotary, 0); + } + } +} + static void do_update(struct main_timer *t) { timer_del(t); @@ -406,6 +501,7 @@ static void do_update(struct main_timer *t) update_ring_from_sink(0, "ursarium"); update_ring_from_sink(1, "catarium"); + update_clients(); } void schedule_update(void) @@ -422,13 +518,43 @@ static void update_sink_from_rotary(int delta, const char *sink_name) double vol = pa_sw_volume_to_linear(s->volume); vol += delta * 0.02; vol = CLAMP(vol, 0, 1); + pa_volume_t pavol = pa_sw_volume_from_linear(vol); + if (pavol == s->volume) + return; pa_cvolume cvol; - pa_cvolume_set(&cvol, 2, pa_sw_volume_from_linear(vol)); + pa_cvolume_set(&cvol, 2, pavol); DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]); PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, pulse_ctx, s->idx, &cvol, pulse_success_cb); } +static void update_client_from_rotary(int rotary, int delta) +{ + int i = find_client_by_rotary(rotary); + if (i < 0) + return; + struct client_map *cm = &client_map[i]; + struct client_state *cs = &client_state[i]; + + calc_clients(); + double vol = pa_sw_volume_to_linear(cs->volume); + vol += delta * 0.02; + vol = CLAMP(vol, 0, cm->range); + pa_volume_t pavol = pa_sw_volume_from_linear(vol); + + HASH_FOR_ALL(pulse_sink_input, s) + { + if (client_match_sink_input(cm, s) && s->volume != pavol) + { + DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol); + pa_cvolume cvol; + pa_cvolume_set(&cvol, 2, pavol); // FIXME: #channels + PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, pulse_ctx, s->idx, &cvol, pulse_success_cb); + } + } + HASH_END_FOR; +} + void notify_rotary(int rotary, int delta) { if (pulse_state != PS_ONLINE) @@ -449,6 +575,8 @@ void notify_rotary(int rotary, int delta) update_sink_from_rotary(delta, "ursarium"); update_sink_from_rotary(delta, "catarium"); break; + default: + update_client_from_rotary(rotary, delta); } } @@ -465,6 +593,33 @@ static void update_sink_mute_from_button(int on, const char *sink_name) PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, pulse_ctx, s->idx, !s->mute, pulse_success_cb); } +static void update_client_from_button(int button, int on) +{ + if (button >= 8 || !on) + return; + + int i = find_client_by_rotary(button); + if (i < 0) + return; + struct client_map *cm = &client_map[i]; + struct client_state *cs = &client_state[i]; + + calc_clients(); + if (!cs->have_muted[0] && !cs->have_muted[1]) + return; + uns mute = !cs->have_muted[1]; + + HASH_FOR_ALL(pulse_sink_input, s) + { + if (client_match_sink_input(cm, s)) + { + DBG("@@ Client #%d, sink input #%d: setting mute=%u", s->client_idx, s->idx, mute); + PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, pulse_ctx, s->idx, mute, pulse_success_cb); + } + } + HASH_END_FOR; +} + void notify_button(int button, int on) { if (pulse_state != PS_ONLINE) @@ -481,6 +636,8 @@ void notify_button(int button, int on) case 1: update_sink_mute_from_button(on, "catarium"); break; + default: + update_client_from_button(button, on); } } -- 2.39.2