sites

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 8bdd8de0d512ab98064d4b7e8fdf9150ae927437
parent aff4fc19a4664d7f8a3418e432723693554e1801
Author: Thuban <thuban@singularity.fr>
Date:   Wed, 26 Jun 2013 10:01:14 +0200

another battery monitor

Diffstat:
Adwm.suckless.org/dwmstatus/batterystatus.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdwm.suckless.org/dwmstatus/index.md | 2++
2 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/dwm.suckless.org/dwmstatus/batterystatus.c b/dwm.suckless.org/dwmstatus/batterystatus.c @@ -0,0 +1,53 @@ +#define BATT_NOW "/sys/class/power_supply/BAT0/charge_now" +#define BATT_FULL "/sys/class/power_supply/BAT0/charge_full" +#define BATT_STATUS "/sys/class/power_supply/BAT0/status" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +char * +smprintf(char *fmt, ...) +{ + va_list fmtargs; + char *buf = NULL; + + va_start(fmtargs, fmt); + if (vasprintf(&buf, fmt, fmtargs) == -1){ + fprintf(stderr, "malloc vasprintf\n"); + exit(1); + } + va_end(fmtargs); + + return buf; +} + +char * +getbattery(){ + long lnum1, lnum2 = 0; + char *status = malloc(sizeof(char)*12); + char s = '?'; + FILE *fp = NULL; + if ((fp = fopen(BATT_NOW, "r"))) { + fscanf(fp, "%ld\n", &lnum1); + fclose(fp); + fp = fopen(BATT_FULL, "r"); + fscanf(fp, "%ld\n", &lnum2); + fclose(fp); + fp = fopen(BATT_STATUS, "r"); + fscanf(fp, "%s\n", status); + fclose(fp); + if (strcmp(status,"Charging") == 0) + s = '+'; + if (strcmp(status,"Discharging") == 0) + s = '-'; + if (strcmp(status,"Full") == 0) + s = '='; + return smprintf("%c%ld%%", s,(lnum1/(lnum2/100))); + } + else return smprintf(""); +} + + + diff --git a/dwm.suckless.org/dwmstatus/index.md b/dwm.suckless.org/dwmstatus/index.md @@ -44,6 +44,8 @@ add them here as file or as code example. * [Simple function to have mpd title/artist info](mpdstatus.c) * [Print any info in dwm status bar via fifo](fifo.c). Replaces dynamic_info. +* [Battery function](batterystatus.c) : Battery percentage and status. + if + charging, - if discharging, = if full. Questions ---------