From 46285716b6b3c21620ac8e5d5a099c2271f08583 Mon Sep 17 00:00:00 2001 From: Timmy Keller Date: Tue, 24 Sep 2024 18:20:44 -0500 Subject: rename to pavolctrld --- pavolctrld.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 pavolctrld.c (limited to 'pavolctrld.c') diff --git a/pavolctrld.c b/pavolctrld.c new file mode 100644 index 0000000..02fd1e8 --- /dev/null +++ b/pavolctrld.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +//TODO pa_operation_unref(o); + +static int default_sink_index; + + +static void sink_info_volume_callback(pa_context *c, const pa_sink_info *i, int eol, void *userdata) { + pa_volume_t vol; + if (eol) return; + + vol = pa_cvolume_avg(&i->volume); + printf("%d,%.02fdB,%d,%d,%d\n", vol*100/PA_VOLUME_NORM, pa_sw_volume_to_dB(vol), i->mute, i->index, i->index == default_sink_index); + fflush(stdout); +} + +static void sink_info_default_index_callback(pa_context *c, const pa_sink_info *i, int eol, void *userdata) { + if (eol) return; + default_sink_index = i->index; + + // output volume levels for each sink + pa_context_get_sink_info_list(c, sink_info_volume_callback, userdata); +} + +static void server_info_get_default_sink_callback(pa_context *c, const pa_server_info *i, void *userdata) { + pa_context_get_sink_info_by_name(c, i->default_sink_name, sink_info_default_index_callback, userdata); +} + +static void subscription_callback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) { + switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) { + case PA_SUBSCRIPTION_EVENT_SINK: + pa_context_get_sink_info_by_index(c, idx, sink_info_volume_callback, userdata); + break; + case PA_SUBSCRIPTION_EVENT_SERVER: + pa_context_get_server_info(c, server_info_get_default_sink_callback, userdata); + break; + } +} + +static void context_state_callback(pa_context *c, void *userdata) { + if (pa_context_get_state(c) == PA_CONTEXT_READY) { + pa_context_set_subscribe_callback(c, subscription_callback, userdata); + + // get initial default sink info + pa_context_get_server_info(c, server_info_get_default_sink_callback, userdata); + + // subscribe to sink & server events to listen to changes + pa_context_subscribe(c, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SERVER, NULL, NULL); + } +} + +int main() { + pa_mainloop *mainloop = pa_mainloop_new(); + pa_mainloop_api *mainloop_api = pa_mainloop_get_api(mainloop); + pa_context *context = pa_context_new(mainloop_api, "pavold"); + + pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); + + pa_context_set_state_callback(context, context_state_callback, NULL); + + // main loop + pa_mainloop_run(mainloop, NULL); + + // clean up + pa_context_disconnect(context); + pa_context_unref(context); + pa_mainloop_free(mainloop); + + return 0; +} + -- cgit v1.2.3