diff options
Diffstat (limited to 'pavolctld.c')
-rw-r--r-- | pavolctld.c | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/pavolctld.c b/pavolctld.c index cbeefe3..3a2dd19 100644 --- a/pavolctld.c +++ b/pavolctld.c @@ -14,9 +14,10 @@ static int command_sink_index; // commands will modify this sink /* output callbacks */ static void sink_info_volume_callback(pa_context *c, const pa_sink_info *i, int eol, void *userdata) { - pa_volume_t vol = pa_cvolume_avg(&i->volume); + pa_volume_t vol; if (eol) return; + vol = pa_cvolume_avg(&i->volume); printf("v%d,%d,%.02f,%d\n", i->index, ROUND_POS(vol*100.0f/PA_VOLUME_NORM), pa_sw_volume_to_dB(vol), i->mute); fflush(stdout); } @@ -86,10 +87,41 @@ static void context_state_callback(pa_context *c, void *userdata) { } /* stdin commands */ +static void sink_info_change_command_sink_callback(pa_context *c, const pa_sink_info *i, int eol, void *userdata) { + int useridx = *(int *)userdata; + if (eol) { + // callbacks are called twice if successful, with eol set the 2nd time + // so this is the easiest way to check if cmd was successful + if (useridx != command_sink_index) { + printf("e0,sink %d does not exist\n", useridx); + fflush(stdout); + } + return; + } + + command_sink_index = i->index; +} + +static void change_command_sink(pa_context *c, char *cmd) { + int useridx; + + if (cmd[1] == '\n') { + command_sink_index = default_sink_index; + return; + } + + useridx = atoi(&cmd[1]); + + if (useridx == default_sink_index) + return; + + pa_context_get_sink_info_by_index(c, useridx, sink_info_change_command_sink_callback, (void *)&useridx); +} + static void sink_info_set_volume_callback(pa_context *c, const pa_sink_info *i, int eol, void *userdata) { char *cmd = (char *)userdata; - pa_cvolume *cvol = (pa_cvolume *)&i->volume; // disregard const qualifier, avoid warnings :) - pa_volume_t vol = (pa_volume_t)(atof(&cmd[1]) / 100 * PA_VOLUME_NORM); // sneaky substring, super safe + pa_cvolume *cvol; + pa_volume_t vol; if (eol) return; if (cmd[1] == '\n') { @@ -97,6 +129,9 @@ static void sink_info_set_volume_callback(pa_context *c, const pa_sink_info *i, return; } + cvol = (pa_cvolume *)&i->volume; // disregard const qualifier, avoid warnings :) + vol = (pa_volume_t)(atof(&cmd[1]) / 100 * PA_VOLUME_NORM); // sneaky substring, super safe + switch (cmd[1]) { case '+': cvol = pa_cvolume_inc(cvol, vol); break; case '-': cvol = pa_cvolume_dec(cvol, vol * -1); break; // should be no underflow w/ uint32 since 0dB is only 2^16 @@ -126,6 +161,7 @@ static void sink_info_set_mute_callback(pa_context *c, const pa_sink_info *i, in static void handle_command(pa_context *c, char *cmd) { // send the command to each callback using userdata switch (cmd[0]) { + case 's': change_command_sink(c, cmd); break; case 'v': pa_context_get_sink_info_by_index(c, command_sink_index, sink_info_set_volume_callback, (void *)cmd); break; case 'm': pa_context_get_sink_info_by_index(c, command_sink_index, sink_info_set_mute_callback, (void *)cmd); break; } |