[wiki] [sites] another battery monitor || Thuban
commit 8bdd8de0d512ab98064d4b7e8fdf9150ae927437
Author: Thuban <thuban_AT_singularity.fr>
Date: Wed Jun 26 10:01:14 2013 +0200
another battery monitor
diff --git a/dwm.suckless.org/dwmstatus/batterystatus.c b/dwm.suckless.org/dwmstatus/batterystatus.c
new file mode 100644
index 0000000..9951fc3
--- /dev/null
+++ b/dwm.suckless.org/dwmstatus/batterystatus.c
_AT_@ -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
");
+ 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
", &lnum1);
+ fclose(fp);
+ fp = fopen(BATT_FULL, "r");
+ fscanf(fp, "%ld
", &lnum2);
+ fclose(fp);
+ fp = fopen(BATT_STATUS, "r");
+ fscanf(fp, "%s
", 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
index 7ba3cfd..6cf3a5e 100644
--- a/dwm.suckless.org/dwmstatus/index.md
+++ b/dwm.suckless.org/dwmstatus/index.md
_AT_@ -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
---------
Received on Wed Jun 26 2013 - 10:01:18 CEST
This archive was generated by hypermail 2.3.0
: Wed Jun 26 2013 - 10:12:14 CEST