From ffd7a89fc94f9224cafb21cfa2a671bfec2897da Mon Sep 17 00:00:00 2001 From: Sam Light Date: Wed, 10 Sep 2025 19:37:28 +0100 Subject: Updates to pulse --- src/modules/pulse.c | 55 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/modules/pulse.c b/src/modules/pulse.c index 7d4ac34..f039afd 100644 --- a/src/modules/pulse.c +++ b/src/modules/pulse.c @@ -6,12 +6,26 @@ #include #include #include +#include #include #include #include #include +#define VALUE_SIZE 16 + +struct pulse { + time_t last_updated; + pa_mainloop * ml; + pa_context * ctx; + char * default_sink; + + bool run_loop; + + char value[VALUE_SIZE]; +}; + static void run_loop(struct pulse * p) { @@ -66,8 +80,9 @@ pulse_new() { struct pulse * p = malloc(sizeof(struct pulse)); - memset(p->value, '\0', PULSE_VALUE_SIZE); + memset(p->value, '\0', VALUE_SIZE); + p->last_updated = 0; p->default_sink = NULL; p->run_loop = false; @@ -105,17 +120,16 @@ sink_info_cb(pa_context * ctx, const pa_sink_info * info, int eol, void * ud) stop_loop(p); } else { - if(info->mute) { - strncpy(p->value, "M", PULSE_VALUE_SIZE); - } - else { - vol = pa_cvolume_avg(&info->volume); - vol -= PA_VOLUME_MUTED; - norm = PA_VOLUME_NORM - PA_VOLUME_MUTED; - - vol = round((100.0f / norm) * vol); - snprintf(p->value, PULSE_VALUE_SIZE, "%d%%", vol); - } + vol = pa_cvolume_avg(&info->volume); + vol -= PA_VOLUME_MUTED; + norm = PA_VOLUME_NORM - PA_VOLUME_MUTED; + + vol = round((100.0f / norm) * vol); + snprintf( + p->value, VALUE_SIZE, + "%s% 4d%%", + info->mute ? "\uf6a9" : "\uf028", + vol); } } @@ -130,3 +144,20 @@ pulse_update(struct pulse * p) run_loop(p); } } + +const char * +pulse_get_val(struct pulse * p) +{ + return p->value; +} + +bool +pulse_should_update(struct pulse * p, time_t now, unsigned short limit) +{ + double time_diff = difftime(now, p->last_updated); + if(time_diff >= limit) { + p->last_updated = now; + return true; + } + return false; +} -- cgit v1.2.3