summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Wolff <janw@mailbox.org>2019-06-17 18:55:15 +0200
committerJan Wolff <janw@mailbox.org>2019-06-17 18:55:15 +0200
commit2be36d330a7116892bbd1d7f537b0e38b2807bad (patch)
treed750ce8666738902272804d8dee8adc64b37e552
parent17abaa8fbae9a32ea2d7e7cf801ea94153abe7ea (diff)
battery status (as works on my machine)
-rw-r--r--dwmstatus.c36
1 files changed, 32 insertions, 4 deletions
diff --git a/dwmstatus.c b/dwmstatus.c
index 47f8da0..3cc6be0 100644
--- a/dwmstatus.c
+++ b/dwmstatus.c
@@ -1,10 +1,13 @@
#include <time.h>
#include <stdio.h>
+#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
-#define STATUS_SIZE 256
+#define STATUS_SIZE 512
+#define BLOCK_SIZE 128
char status[STATUS_SIZE];
+char blocks[2][BLOCK_SIZE];
void print() {
#ifdef DEBUG
@@ -14,15 +17,40 @@ void print() {
#endif
}
-void update() {
+void update_battery() {
+ char buf_cur[32], buf_max[32], buf_status[1];
+ int cur, max;
+ FILE * fp;
+
+ fp = fopen("/sys/class/power_supply/BAT1/charge_now", "r");
+ fread(buf_cur, 32, 1, fp);
+ fclose(fp);
+
+ fp = fopen("/sys/class/power_supply/BAT1/charge_full", "r");
+ fread(buf_max, 32, 1, fp);
+ fclose(fp);
+
+ fp = fopen("/sys/class/power_supply/BAT1/status", "r");
+ fread(buf_status, 1, 1, fp);
+ fclose(fp);
+
+ cur = atoi(buf_cur);
+ max = atoi(buf_max);
+
+ sprintf(blocks[1], "%i%% %c", cur * 100 / max, buf_status[0]);
+}
+
+void update_time() {
time_t t = time(NULL);
struct tm * tm = localtime(&t);
- strftime(status, STATUS_SIZE, "%F %H:%M", tm);
+ strftime(blocks[0], BLOCK_SIZE, "%F %H:%M", tm);
}
int main() {
while(1) {
- update();
+ update_time();
+ update_battery();
+ snprintf(status, STATUS_SIZE, "%s | %s", blocks[1], blocks[0]);
print();
sleep(60);
}