diff options
author | Julian T <julian@jtle.dk> | 2020-10-31 16:51:20 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-10-31 16:51:20 +0100 |
commit | a20c17de66b417c133883e9b983db6315e532c0a (patch) | |
tree | f0ded360ca2a84f64ec37ea521df65cfb263d566 /components/custom.c | |
parent | b14e039639ed28005fbb8bddeb5b5fa0c93475ac (diff) |
Patches:
- Pulseaudio volume
- Battery warning
Diffstat (limited to 'components/custom.c')
-rw-r--r-- | components/custom.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/components/custom.c b/components/custom.c new file mode 100644 index 0000000..d68f506 --- /dev/null +++ b/components/custom.c @@ -0,0 +1,74 @@ + +#include <alsa/asoundlib.h> +#include <alsa/mixer.h> +#include <limits.h> + +#include "../util.h" + +// https://stackoverflow.com/questions/7657624/get-master-sound-volume-in-c-in-linux +const char *vol_pulse(void) +{ + snd_mixer_t *handle; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *sid; + + static const char *mix_name = "Master"; + static const char *card = "default"; + static int mix_index = 0; + + static const char *notfound = "n/a"; + + snd_mixer_selem_id_alloca(&sid); + + snd_mixer_selem_id_set_index(sid, mix_index); + snd_mixer_selem_id_set_name(sid, mix_name); + + if ((snd_mixer_open(&handle, 0)) < 0) { + fprintf(stderr, "could not open mixer\n"); + return notfound; + } + if ((snd_mixer_attach(handle, card)) < 0) { + snd_mixer_close(handle); + fprintf(stderr, "could not open card %s\n", card); + return notfound; + } + if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) { + snd_mixer_close(handle); + fprintf(stderr, "could not register selem\n"); + return notfound; + } + int ret = snd_mixer_load(handle); + if (ret < 0) { + snd_mixer_close(handle); + fprintf(stderr, "could not load mixer\n"); + return notfound; + } + elem = snd_mixer_find_selem(handle, sid); + if (!elem) { + snd_mixer_close(handle); + fprintf(stderr, "could not find selem with index=%d, name=%s\n", mix_index, mix_name); + return notfound; + } + + long minv, maxv; + long outvol; + int muted; + + snd_mixer_selem_get_playback_volume_range(elem, &minv, &maxv); + + if(snd_mixer_selem_get_playback_volume(elem, 0, &outvol) < 0) { + fprintf(stderr, "could not read output volume\n"); + snd_mixer_close(handle); + return notfound; + } + + if(snd_mixer_selem_get_playback_switch(elem, 0, &muted) < 0) { + fprintf(stderr, "could not read mute output\n"); + snd_mixer_close(handle); + return notfound; + } + + snd_mixer_close(handle); + + return bprintf("%0.0f%s", ((float)outvol / (float)maxv) * 100, muted ? "" : "M"); +} |