diff options
Diffstat (limited to 'pavolctld.c')
-rw-r--r-- | pavolctld.c | 30 |
1 files changed, 10 insertions, 20 deletions
diff --git a/pavolctld.c b/pavolctld.c index a4e108b..b266ab0 100644 --- a/pavolctld.c +++ b/pavolctld.c @@ -81,37 +81,26 @@ static void context_state_callback(pa_context *c, void *userdata) { pa_context_get_sink_info_list(c, sink_info_volume_callback, userdata); // subscribe to sink & server events to listen to changes + // other events can be subscribed to if desired pa_context_subscribe(c, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SERVER, NULL, NULL); } } /* stdin commands */ static void sink_info_set_volume_callback(pa_context *c, const pa_sink_info *i, int eol, void *userdata) { - pa_cvolume *vol; - double input_vol; 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 if (eol) return; - vol = (pa_cvolume *)&i->volume; // disregard const qualifier, avoid warnings :) switch(cmd[1]) { - case '\n': - pa_context_get_sink_info_by_index(c, i->index, sink_info_volume_callback, userdata); - return; - case '+': - input_vol = atof(&cmd[2])/100; - vol = pa_cvolume_inc(vol, (pa_volume_t)(input_vol * PA_VOLUME_NORM)); - break; - case '-': - input_vol = atof(&cmd[2])/100; - vol = pa_cvolume_dec(vol, (pa_volume_t)(input_vol * PA_VOLUME_NORM)); - break; - default: - input_vol = atof(&cmd[1])/100; - vol = pa_cvolume_set(vol, i->volume.channels, (pa_volume_t)(input_vol * PA_VOLUME_NORM)); - break; + case '\n': pa_context_get_sink_info_by_index(c, i->index, sink_info_volume_callback, userdata); return; + 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 + default : cvol = pa_cvolume_set(cvol, i->volume.channels, vol); break; } - pa_context_set_sink_volume_by_index(c, i->index, vol, NULL, NULL); + pa_context_set_sink_volume_by_index(c, i->index, cvol, NULL, NULL); } /* handle stdin commands */ @@ -126,6 +115,7 @@ static void handle_command(pa_context *c, char *cmd) { /* main */ int main() { + // initialize threaded pulse mainloop pa_threaded_mainloop *mainloop = pa_threaded_mainloop_new(); pa_mainloop_api *mainloop_api = pa_threaded_mainloop_get_api(mainloop); pa_context *context = pa_context_new(mainloop_api, "pavolctld"); @@ -138,6 +128,7 @@ int main() { // main loop pa_threaded_mainloop_start(mainloop); + // listen for commands while (fgets(stdin_buffer, sizeof(stdin_buffer), stdin) != NULL) handle_command(context, stdin_buffer); @@ -148,4 +139,3 @@ int main() { return 0; } - |