summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rw-r--r--components/battery.c5
-rw-r--r--components/custom.c74
2 files changed, 79 insertions, 0 deletions
diff --git a/components/battery.c b/components/battery.c
index 07b6ac1..f54db93 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -4,6 +4,9 @@
#include "../util.h"
+// Used for battery warning
+extern void batt_hook(int perc);
+
#if defined(__linux__)
#include <limits.h>
#include <stdint.h>
@@ -40,6 +43,8 @@
return NULL;
}
+ batt_hook(perc);
+
return bprintf("%d", perc);
}
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");
+}