shithub: bar

Download patch

ref: bbcc115caba646716d1845a79230e2b60492a16c
parent: b2c7b666c866e32e60134c5ef34ee51705dc3a4d
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Jan 13 03:24:50 EST 2021

battery update period: 1s → 10s

--- a/bar.c
+++ b/bar.c
@@ -16,6 +16,7 @@
 static int wctl, width, bottom, bat, minheight;
 static Image *cback, *ctext;
 static char *pos = "rb";
+static char bats[16];
 static Tzone *local;
 static Font *f;
 
@@ -34,7 +35,8 @@
 		return nsec() - xstart;
 
 	if(fasthz == 0){
-		if((fasthz = _tos->cyclefreq) == 0){
+		fasthz = _tos->cyclefreq;
+		if(fasthz == 0){
 			fasthz = ~0ULL;
 			xstart = nsec();
 			return 0;
@@ -97,7 +99,7 @@
 static void
 redraw(void)
 {
-	char bats[16], s[128], *t, tmp[16];
+	char s[128];
 	Rectangle r;
 	Tmfmt tf;
 	Point p;
@@ -108,14 +110,6 @@
 
 	draw(screen, r, cback, nil, ZP);
 
-	t = bat < 0 || pread(bat, tmp, 4, 0) < 4 ? nil : strchr(tmp, ' ');
-	if(t != nil){
-		*t = 0;
-		snprint(bats, sizeof(bats), pos[0] == 'l' || pos[1] == 'l' ? " │ %s%%" : "%s%% │ ", tmp);
-	}else{
-		bats[0] = 0;
-	}
-
 	tf = tmfmt(tmnow(&tm, local), "YYYY/MM/DD WW hh:mm:ss");
 	if(pos[0] == 'l' || pos[1] == 'l'){
 		snprint(s, sizeof(s), "%τ%s", tf, bats);
@@ -139,19 +133,41 @@
 }
 
 static void
+readbattery(void)
+{
+	char *s, tmp[16];
+
+	s = bat < 0 || pread(bat, tmp, 4, 0) < 4 ? nil : strchr(tmp, ' ');
+	if(s != nil){
+		*s = 0;
+		snprint(bats, sizeof(bats), pos[0] == 'l' || pos[1] == 'l' ? " │ %s%%" : "%s%% │ ", tmp);
+	}else{
+		bats[0] = 0;
+	}
+}
+
+static void
 updateproc(void *)
 {
-	uvlong t, n;
+	uvlong t1, t2, n;
 
-	t = nanosec();
+	t1 = nanosec();
+	t2 = t1;
 	for(;;){
 		sleep(250);
 		if(wctl < 0)
 			break;
 		fprint(wctl, bottom ? "bottom" : "top");
-		if((n = nanosec()) - t >= 1000000000ULL){
+
+		n = nanosec();
+
+		if(n - t1 >= 10000000000ULL){
+			readbattery();
+			t1 = n;
+		}
+		if(n - t2 >= 1000000000ULL){
 			redraw();
-			t = n;
+			t2 = n;
 		}
 	}
 
@@ -230,8 +246,9 @@
 	brgb = ~(brgb>>8 | brgb>>16 | brgb>>24);
 	brgb = brgb<<8 | brgb<<16 | brgb<<24 | 0xff;
 	ctext = allocimage(display, Rect(0,0,1,1), RGB24, 1, brgb);
-	redraw();
 
+	readbattery();
+	redraw();
 	proccreate(updateproc, nil, 4096);
 
 	for(;;){