shithub: bar

Download patch

ref: 55b6520c25a63e64653c2ce93bda6a4c049d9040
parent: a75dbcaded5dc4a53ae0ed872a0f7da20502610d
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Fri Nov 20 05:36:17 EST 2020

simplify, make battery status appear on the left or right depending on the corner

--- a/bar.c
+++ b/bar.c
@@ -1,7 +1,11 @@
-#include "theme.c"
-#include "nanosec.c"
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <draw.h>
 #include <keyboard.h>
 #include <mouse.h>
+#include <thread.h>
+#include <tos.h>
 
 #define MAX(a,b) ((a)>=(b)?(a):(b))
 
@@ -9,21 +13,50 @@
 	Off = 4,
 };
 
-static Font *f;
-static struct {
-	int w, h;
-}scr;
-
-static char *pos = "rb";
 static int wctl, width, bottom, bat;
+static Image *cback, *ctext;
+static char *pos = "rb";
 static Tzone *local;
+static Font *f;
 
+/*
+ * nsec() is wallclock and can be adjusted by timesync
+ * so need to use cycles() instead, but fall back to
+ * nsec() in case we can't
+ */
+static uvlong
+nanosec(void)
+{
+	static uvlong fasthz, xstart;
+	uvlong x, div;
+
+	if(fasthz == ~0ULL)
+		return nsec() - xstart;
+
+	if(fasthz == 0){
+		if((fasthz = _tos->cyclefreq) == 0){
+			fasthz = ~0ULL;
+			xstart = nsec();
+			return 0;
+		}else{
+			cycles(&xstart);
+		}
+	}
+	cycles(&x);
+	x -= xstart;
+
+	/* this is ugly */
+	for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
+
+	return x / (fasthz / div);
+}
+
 static void
 place(void)
 {
-	static int ow, oh;
-	char t[61], *a[5];
 	int fd, n, w, h, minx, miny, maxx, maxy;
+	char t[61], *a[5];
+	static int ow, oh;
 
 	if((fd = open("/dev/screen", OREAD)) < 0)
 		return;
@@ -45,9 +78,9 @@
 		}
 		if(pos[0] == 'l' || pos[1] == 'l'){
 			minx = 0;
-			maxx = MAX(100, Borderwidth+width+Borderwidth);
+			maxx = MAX(100, Borderwidth+Off+width+Off+Borderwidth);
 		}else{
-			minx = MAX(100, w-Borderwidth-width-Borderwidth);
+			minx = MAX(100, w-(Borderwidth+Off+width+Off+Borderwidth));
 			maxx = w;
 		}
 		fprint(wctl, "resize -r %d %d %d %d", minx, miny, maxx, maxy);
@@ -59,32 +92,41 @@
 static void
 redraw(void)
 {
-	Tm tm;
-	char bats[16], s[128], *t;
-	Point p;
+	char bats[16], s[128], *t, tmp[16];
 	Rectangle r;
+	Tmfmt tf;
+	Point p;
+	Tm tm;
 
 	lockdisplay(display);
 	r = screen->r;
 
-	draw(screen, r, colors[Dback].im, nil, ZP);
+	draw(screen, r, cback, nil, ZP);
 
-	if(bat < 0 || pread(bat, bats, 4, 0) < 4)
+	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;
-	else{
-		t = strchr(bats, ' ');
-		strcpy(t, "% | ");
 	}
 
-	snprint(s, sizeof(s), "%s%τ", bats, tmfmt(tmnow(&tm, local), "YYYY/MM/DD WW hh:mm:ss"));
-	width = Off + stringwidth(f, s) + Off;
-	p.x = r.max.x - width + Off;
+	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);
+		p.x = r.min.x + Off;
+	}else{
+		snprint(s, sizeof(s), "%s%τ", bats, tf);
+		p.x = r.max.x - (stringwidth(f, s) + Off);
+	}
 	p.y = (pos[0] == 't' || pos[1] == 't') ? r.max.y - (f->height + Off) : r.min.y + Off;
-	string(screen, p, colors[Dfhigh].im, ZP, f, s);
+	string(screen, p, ctext, ZP, f, s);
 
 	flushimage(display, 1);
 	unlockdisplay(display);
 
+	snprint(s, sizeof(s), "%s%τ", bats[0] ? "100% | " : "", tf);
+	width = stringwidth(f, s);
 	place();
 }
 
@@ -108,12 +150,6 @@
 	threadexits(nil);
 }
 
-void
-themechanged(void)
-{
-	redraw();
-}
-
 static void
 usage(void)
 {
@@ -126,6 +162,9 @@
 {
 	Keyboardctl *kctl;
 	Mousectl *mctl;
+	char *s, *v[3];
+	u32int brgb;
+	Biobuf *b;
 	Rune key;
 	Mouse m;
 	Alt a[] =
@@ -167,9 +206,21 @@
 	a[1].c = mctl->resizec;
 	a[2].c = kctl->c;
 
-	nanosec();
-	themeinit();
-	redraw();
+	brgb = DPalegreygreen;
+	if((b = Bopen("/dev/theme", OREAD)) != nil){
+		while((s = Brdline(b, '\n')) != nil){
+			s[Blinelen(b)-1] = 0;
+			if(tokenize(s, v, nelem(v)) > 1 && strcmp(v[0], "ltitle") == 0){
+				brgb = strtoul(v[1], nil, 16)<<8 | 0xff;
+				break;
+			}
+		}
+		Bterm(b);
+	}
+	cback = allocimage(display, Rect(0,0,1,1), RGB24, 1, brgb);
+	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);
 
 	proccreate(updateproc, nil, 4096);
 
--- a/mkfile
+++ b/mkfile
@@ -2,9 +2,6 @@
 
 BIN=/$objtype/bin
 TARG=bar
-HFILES=\
-	nanosec.c\
-	theme.c\
 
 default:V: all
 
--- a/nanosec.c
+++ /dev/null
@@ -1,35 +1,0 @@
-#include <tos.h>
-
-/*
- * nsec() is wallclock and can be adjusted by timesync
- * so need to use cycles() instead, but fall back to
- * nsec() in case we can't
- */
-uvlong
-nanosec(void)
-{
-	static uvlong fasthz, xstart;
-	uvlong x, div;
-
-	if(fasthz == ~0ULL)
-		return nsec() - xstart;
-
-	if(fasthz == 0){
-		if((fasthz = _tos->cyclefreq) == 0){
-			fasthz = ~0ULL;
-			xstart = nsec();
-			fprint(2, "cyclefreq not available, falling back to nsec()\n");
-			fprint(2, "you might want to disable aux/timesync\n");
-			return 0;
-		}else{
-			cycles(&xstart);
-		}
-	}
-	cycles(&x);
-	x -= xstart;
-
-	/* this is ugly */
-	for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
-
-	return x / (fasthz / div);
-}
--- a/theme.c
+++ /dev/null
@@ -1,162 +1,0 @@
-#include <u.h>
-#include <libc.h>
-#include <plumb.h>
-#include <draw.h>
-#include <bio.h>
-#include <thread.h>
-#include "theme.h"
-
-ThemeColor colors[Numcolors] = {
-	[Dback]   = {"background", 0x999999},
-	[Dfhigh]  = {"f_high",     0xffffff},
-	[Dltitle] = {"ltitle",     DPalegreygreen>>8},
-};
-
-void themechanged(void);
-
-static char *themeplumb;
-
-static void
-runpicker(void *x)
-{
-	int *p, f;
-	char tmp[32];
-
-	snprint(tmp, sizeof(tmp), "-pid %d -dx %d -dy %d", getpid(), 384, 320);
-	newwindow(tmp);
-
-	p = x;
-	dup(*p, 0); dup(*p, 1); close(*p);
-	close(p[1]);
-	close(p[2]);
-	dup(f = open("/dev/null", OWRITE), 2); close(f);
-	execl("/bin/picker", "picker", nil);
-
-	threadexits("exec: %r");
-}
-
-void
-themeproc(void *fd)
-{
-	Biobuf *b;
-	char *s, *v[3];
-	int p[3], n, i;
-	static int pid;
-
-	threadsetname("themeproc");
-	pipe(p);
-	p[2] = fd != nil ? *(int*)fd : -1;
-	postnote(PNGROUP, pid, "interrupt");
-	pid = threadpid(procrfork(runpicker, p, 4096, RFFDG|RFNAMEG|RFNOTEG));
-	close(p[0]);
-	b = Bfdopen(p[1], OREAD);
-
-	for(i = 0; i < nelem(colors); i++)
-		fprint(p[1], "%s\t%06ux\n", colors[i].id, colors[i].rgb);
-
-	for(;;){
-		if((s = Brdstr(b, '\n', 1)) == nil)
-			break;
-		if((n = tokenize(s, v, nelem(v))) >= 2){
-			for(i = 0; i < nelem(colors); i++){
-				if(strcmp(colors[i].id, v[0]) == 0){
-					if(display->locking)
-						lockdisplay(display);
-					freeimage(colors[i].im);
-					colors[i].rgb = strtoul(v[1], nil, 16);
-					colors[i].im = allocimage(display, Rect(0,0,1,1), RGB24, 1, colors[i].rgb<<8 | 0xff);
-					if(display->locking)
-						unlockdisplay(display);
-					themechanged();
-					break;
-				}
-			}
-		}
-		free(s);
-		if(n != 2)
-			break;
-	}
-	Bterm(b);
-	postnote(PNGROUP, pid, "interrupt");
-
-	threadexits(nil);
-}
-
-static int
-loadtheme(char *filename, int init)
-{
-	Biobuf *in;
-	char *s, *v[3];
-	int i, n;
-
-	if ((in = Bopen(filename, OREAD)) != nil) {
-		if(display->locking && !init)
-			lockdisplay(display);
-		for(;;){
-			if((s = Brdstr(in, '\n', 1)) == nil)
-				break;
-			if((n = tokenize(s, v, nelem(v))) == 2){
-				for(i = 0; i < nelem(colors); i++){
-					if(strcmp(colors[i].id, v[0]) == 0){
-						freeimage(colors[i].im);
-						colors[i].rgb = strtoul(v[1], nil, 16);
-						colors[i].im = allocimage(display, Rect(0,0,1,1), RGB24, 1, colors[i].rgb<<8 | 0xff);
-						break;
-					}
-				}
-			}
-			free(s);
-			if(n != 2)
-				break;
-		}
-		if(display->locking && !init)
-			unlockdisplay(display);
-		Bterm(in);
-		if(!init)
-			themechanged();
-		return 0;
-	}
-
-	return -1;
-}
-
-static void
-plumbproc(void *)
-{
-	int f;
-	Plumbmsg *m;
-
-	threadsetname("theme/plumb");
-	if ((f = plumbopen(themeplumb, OREAD)) >= 0) {
-		while ((m = plumbrecv(f)) != nil) {
-			loadtheme(m->data, 0);
-			themechanged();
-			plumbfree(m);
-		}
-	}
-
-	threadexits(nil);
-}
-
-void
-themeinit(void)
-{
-	char *s;
-	int i;
-
-	loadtheme("/dev/theme", 1);
-	colors[Dback].rgb = colors[Dltitle].rgb;
-	colors[Dfhigh].rgb = ~(colors[Dback].rgb | colors[Dback].rgb>>8 | colors[Dback].rgb>>16);
-
-	if((s = getenv("theme")) != nil){
-		if(loadtheme(s, 1) != 0)
-			sysfatal("theme load failed: %r");
-		free(s);
-	}
-	for(i = 0; i < Numcolors; i++){
-		if(colors[i].im == nil)
-			colors[i].im = allocimage(display, Rect(0,0,1,1), RGB24, 1, colors[i].rgb<<8 | 0xff);
-	}
-	if((themeplumb = getenv("themeplumb")) != nil)
-		proccreate(plumbproc, nil, 4096);
-}
--- a/theme.h
+++ /dev/null
@@ -1,19 +1,0 @@
-enum {
-	Dback = 0,
-	Dfhigh,
-	Dltitle,
-	Numcolors,
-};
-
-typedef struct ThemeColor ThemeColor;
-
-struct ThemeColor {
-	char *id;
-	u32int rgb;
-	Image *im;
-};
-
-extern ThemeColor colors[Numcolors];
-
-void themeproc(void *fd);
-void themeinit(void);