+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, s->channels, pavol);
+ 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)
+ {
+ DBG("## NOTIFY: Pulse is not online");
+ return;
+ }
+
+ switch (rotary)
+ {
+ case 0:
+ update_sink_from_rotary(delta, "ursarium");
+ break;
+ case 1:
+ update_sink_from_rotary(delta, "catarium");
+ break;
+ case 8:
+ update_sink_from_rotary(delta, "ursarium");
+ update_sink_from_rotary(delta, "catarium");
+ break;
+ default:
+ update_client_from_rotary(rotary, delta);
+ }
+}
+
+static void update_sink_mute_from_button(int on, const char *sink_name)
+{
+ if (!on)
+ return;
+
+ struct pulse_sink *s = pulse_sink_by_name(sink_name);
+ if (!s)
+ return;
+
+ DBG("## Setting mute of sink %s to %d", s->name, !s->mute);
+ 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)
+ {
+ DBG("## NOTIFY: Pulse is not online");
+ return;
+ }
+
+ switch (button)
+ {
+ case 0:
+ update_sink_mute_from_button(on, "ursarium");
+ break;
+ case 1:
+ update_sink_mute_from_button(on, "catarium");
+ break;
+ default:
+ update_client_from_button(button, on);
+ }