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 | |
parent | b14e039639ed28005fbb8bddeb5b5fa0c93475ac (diff) |
Patches:
- Pulseaudio volume
- Battery warning
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | components/battery.c | 5 | ||||
-rw-r--r-- | components/custom.c | 74 | ||||
-rw-r--r-- | config.h | 95 | ||||
-rw-r--r-- | config.mk | 10 | ||||
-rw-r--r-- | slstatus.h | 3 |
6 files changed, 184 insertions, 6 deletions
@@ -26,7 +26,8 @@ COM =\ components/uptime\ components/user\ components/volume\ - components/wifi + components/wifi\ + components/custom all: slstatus 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"); +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..78e9563 --- /dev/null +++ b/config.h @@ -0,0 +1,95 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <stdlib.h> + +/* interval between updates (in ms) */ +const unsigned int interval = 1000; + +/* text to show if no value can be retrieved */ +static const char unknown_str[] = "n/a"; + +static const char batter_w[] = "notify-send \"LOW BATTERY\" \"Only %d percent\""; + +void batt_hook(int perc) { + static int last = 100; + if (perc > 25) { + last = 100; + return; + } + + if (perc > last-5) { + return; + } + + last = perc; + + // Libnotify probably works better + char cmdbuff[sizeof(batter_w) + 10]; + snprintf(cmdbuff, sizeof(cmdbuff), batter_w, perc); + + system(cmdbuff); +} + +/* maximum output string length */ +#define MAXLEN 2048 + +/* + * function description argument (example) + * + * battery_perc battery percentage battery name (BAT0) + * NULL on OpenBSD/FreeBSD + * battery_state battery charging state battery name (BAT0) + * NULL on OpenBSD/FreeBSD + * battery_remaining battery remaining HH:MM battery name (BAT0) + * NULL on OpenBSD/FreeBSD + * cpu_perc cpu usage in percent NULL + * cpu_freq cpu frequency in MHz NULL + * datetime date and time format string (%F %T) + * disk_free free disk space in GB mountpoint path (/) + * disk_perc disk usage in percent mountpoint path (/) + * disk_total total disk space in GB mountpoint path (/") + * disk_used used disk space in GB mountpoint path (/) + * entropy available entropy NULL + * gid GID of current user NULL + * hostname hostname NULL + * ipv4 IPv4 address interface name (eth0) + * ipv6 IPv6 address interface name (eth0) + * kernel_release `uname -r` NULL + * keyboard_indicators caps/num lock indicators format string (c?n?) + * see keyboard_indicators.c + * keymap layout (variant) of current NULL + * keymap + * load_avg load average NULL + * netspeed_rx receive network speed interface name (wlan0) + * netspeed_tx transfer network speed interface name (wlan0) + * num_files number of files in a directory path + * (/home/foo/Inbox/cur) + * ram_free free memory in GB NULL + * ram_perc memory usage in percent NULL + * ram_total total memory size in GB NULL + * ram_used used memory in GB NULL + * run_command custom shell command command (echo foo) + * swap_free free swap in GB NULL + * swap_perc swap usage in percent NULL + * swap_total total swap size in GB NULL + * swap_used used swap in GB NULL + * temp temperature in degree celsius sensor file + * (/sys/class/thermal/...) + * NULL on OpenBSD + * thermal zone on FreeBSD + * (tz0, tz1, etc.) + * uid UID of current user NULL + * uptime system uptime NULL + * username username of current user NULL + * vol_perc OSS/ALSA volume in percent mixer file (/dev/mixer) + * wifi_perc WiFi signal in percent interface name (wlan0) + * wifi_essid WiFi ESSID interface name (wlan0) + */ +static const struct arg args[] = { + /* function format argument */ + { vol_pulse, "TIK [V:%s] ", NULL}, + { ram_free, "[F:%s] ", NULL }, + { battery_perc, "[B:%s,", "BAT0" }, + { battery_state, "%s] ", "BAT0" }, + { datetime, "%s", "%F %T" }, +}; @@ -11,10 +11,10 @@ X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib # flags -CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE -CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os -LDFLAGS = -L$(X11LIB) -s -LDLIBS = -lX11 +CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE -ggdb +CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os -ggdb +LDFLAGS = -L$(X11LIB) -s -ggdb +LDLIBS = -lX11 -lasound # compiler and linker -CC = cc +CC = gcc @@ -79,3 +79,6 @@ const char *vol_perc(const char *card); /* wifi */ const char *wifi_perc(const char *interface); const char *wifi_essid(const char *interface); + +/* CUSTOM */ +const char *vol_pulse(void); |