diff options
| author | Sam Light <sam@lightscale.co.uk> | 2025-09-10 19:37:28 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2025-09-10 19:37:28 +0100 |
| commit | ffd7a89fc94f9224cafb21cfa2a671bfec2897da (patch) | |
| tree | f4c118153f199c0e9397313cd49dd2aac1f05866 | |
| parent | 4d3e462531d2172cb13319c7f79c0e2228e652fa (diff) | |
Updates to pulse
| -rw-r--r-- | src/modules/pulse.c | 55 |
1 files 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 <pulse/context.h> #include <pulse/introspect.h> #include <pulse/volume.h> +#include <pulse/subscribe.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <tgmath.h> +#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; +} |
