X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/870d68d44e33f9fb7a5402becbc0b5c363066bcc..832b21ca4b4ba866e010a6f52c0f84919c7123f2:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 8241451..993eba4 100644 --- a/slstatus.c +++ b/slstatus.c @@ -35,7 +35,9 @@ struct arg { static char *smprintf(const char *fmt, ...); static char *battery_perc(const char *bat); +static char *battery_power(const char *bat); static char *battery_state(const char *bat); +static char *cpu_freq(void); static char *cpu_perc(void); static char *datetime(const char *fmt); static char *disk_free(const char *mnt); @@ -118,6 +120,25 @@ battery_perc(const char *bat) return smprintf("%d%%", perc); } +static char * +battery_power(const char *bat) +{ + char path[PATH_MAX]; + FILE *fp; + int watts; + + snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now"); + fp = fopen(path, "r"); + if (fp == NULL) { + warn("Failed to open file %s", path); + return smprintf("%s", UNKNOWN_STR); + } + fscanf(fp, "%i", &watts); + fclose(fp); + + return smprintf("%d", (watts + 500000) / 1000000); +} + static char * battery_state(const char *bat) { @@ -147,6 +168,23 @@ battery_state(const char *bat) } } +static char * +cpu_freq(void) +{ + int freq; + FILE *fp; + + fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r"); + if (fp == NULL) { + warn("Failed to open file /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"); + return smprintf("%s", UNKNOWN_STR); + } + fscanf(fp, "%i", &freq); + fclose(fp); + + return smprintf("%d", (freq + 500) / 1000); +} + static char * cpu_perc(void) {