+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);
+ }
+ }
+}
+