shithub: scripts

ref: bc762e9c9294990c0d2480295637c792363dcc8c
dir: /patches/stats.patch/

View raw version
9a10,154
> static char *
> readall(int f, int *osz)
> {
> 	int bufsz, sz, n;
> 	char *s;
> 
> 	bufsz = 1023;
> 	s = nil;
> 	for(sz = 0;; sz += n){
> 		if(bufsz-sz < 1024){
> 			bufsz *= 2;
> 			s = realloc(s, bufsz);
> 		}
> 		if((n = readn(f, s+sz, bufsz-sz-1)) < 1)
> 			break;
> 	}
> 	if(n < 0 || sz < 1){
> 		free(s);
> 		return nil;
> 	}
> 	s[sz] = 0;
> 	*osz = sz;
> 
> 	return s;
> }
> 
> enum {
> 	Colrioback,
> 
> 	/* the following group has to be in order, they are used by libframe */
> 	Colback,
> 	Colhigh,
> 	Colbord,
> 	Coltext,
> 	Colhtext,
> 
> 	Coltitle,
> 	Colltitle,
> 	Colhold,
> 	Collhold,
> 	Colpalehold,
> 	Colpaletext,
> 	Colsize,
> 
> 	/* menuhit */
> 	Colmenubar,
> 	Colmenuback,
> 	Colmenuhigh,
> 	Colmenubord,
> 	Colmenutext,
> 	Colmenuhtext,
> 
> 	Numcolors
> };
> 
> typedef struct Color Color;
> 
> struct Color {
> 	char *id;
> 	union {
> 		u32int rgb;
> 		char *path;
> 	};
> 	int flags;
> };
> 
> static Color theme[Numcolors] = {
> 	[Colrioback]   = {"rioback",   {0x777777}, 0},
> 	[Colback]      = {"back",      {0xffffff}, 0},
> 	[Colhigh]      = {"high",      {0xcccccc}, 0},
> 	[Colbord]      = {"border",    {0x999999}, 0},
> 	[Coltext]      = {"text",      {DBlack>>8}, 0},
> 	[Colhtext]     = {"htext",     {DBlack>>8}, 0},
> 	[Coltitle]     = {"title",     {DGreygreen>>8}, 0},
> 	[Colltitle]    = {"ltitle",    {DPalegreygreen>>8}, 0},
> 	[Colhold]      = {"hold",      {DMedblue>>8}, 0},
> 	[Collhold]     = {"lhold",     {DGreyblue>>8}, 0},
> 	[Colpalehold]  = {"palehold",  {DPalegreyblue>>8}, 0},
> 	[Colpaletext]  = {"paletext",  {0x666666}, 0},
> 	[Colsize]      = {"size",      {DRed>>8}, 0},
> 	[Colmenubar]   = {"menubar",   {DDarkgreen>>8}, 1},
> 	[Colmenuback]  = {"menuback",  {0xeaffea}, 1},
> 	[Colmenuhigh]  = {"menuhigh",  {DDarkgreen>>8}, 1},
> 	[Colmenubord]  = {"menubord",  {DMedgreen>>8}, 1},
> 	[Colmenutext]  = {"menutext",  {DBlack>>8}, 1},
> 	[Colmenuhtext] = {"menuhtext", {0xeaffea}, 1},
> };
> 
> u32int
> color2rgb(Color c, int scale)
> {
> 	return ((((c.rgb >> 16) & 0xff) / scale) << 24) |
> 			((((c.rgb >> 8) & 0xff) / scale) << 16) |
> 			(((c.rgb & 0xff) / scale) << 8) |
> 			0xff;
> }
> 
> void
> themeload(char *s, int n)
> {
> 	int i;
> 	char *t, *a[2], *e;
> 	Image *newc;
> 	u32int rgb;
> 
> 	if((t = malloc(n+1)) == nil)
> 		return;
> 	memmove(t, s, n);
> 	t[n] = 0;
> 
> 	for(s = t; s != nil && *s; s = e){
> 		if((e = strchr(s, '\n')) != nil)
> 			*e++ = 0;
> 		if(tokenize(s, a, 2) == 2){
> 			for(i = 0; i < nelem(theme); i++) {
> 				if(strcmp(theme[i].id, a[0]) == 0) {
> 					rgb = strtoul(a[1], nil, 16);
> 					if((newc = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, rgb<<8 | 0xff)) != nil) {
> 						theme[i].rgb = rgb;
> 					}
> 					break;
> 				}
> 			}
> 		}
> 	}
> 	free(t);
> }
> 
> char *
> themestring(int *n)
> {
> 	char *s, *t, *e;
> 	int i;
> 
> 	if((t = malloc(512)) != nil){
> 		s = t;
> 		e = s+512;
> 		for(i = 0; i < nelem(theme); i++)
> 			s = seprint(s, e, "%s\t%06ux\n", theme[i].id, theme[i].rgb);
> 		*n = s - t;
> 	}
> 
> 	return t;
> }
> 
29c174
< 	/* /dev/swap */
---
> 	/* old /dev/swap */
34,35c179
< 	Reclaim,
< 	Maxreclaim,
---
> 
52d195
< 
74c217
< 	uvlong		devswap[10];
---
> 	uvlong		devswap[8];
125d267
< 	Mreclaim,
150d291
< 	"add  reclaim ",
174d314
< 	reclaimval(Machine*, uvlong*, uvlong*, int),
200d339
< 	reclaimval,
214c353
< char	argchars[] = "8bcdeEfiIkmlnprstwz";
---
> char	argchars[] = "8bcdeEfiIkmlnpstwz";
222,224c361
< int	sleeptime = 1000;
< int	batteryperiod = 1000;
< int	tempperiod = 1000;
---
> int 	sleeptime = 1000;
227a365,366
> Image	*stext;
> 
278c417
< 	cols[i][0] = allocimagemix(display, c0, DWhite);
---
> 	cols[i][0] = allocimagemix(display, c0, DBlack);
287c426
< 	mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
---
> 	mkcol(0, color2rgb(theme[Coltitle], 1), color2rgb(theme[Colhold], 1), color2rgb(theme[Collhold], 1));
289c428
< 	mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
---
> 	mkcol(1, color2rgb(theme[Coltitle], 1), color2rgb(theme[Colhold], 1), color2rgb(theme[Collhold], 1));
291c430
< 	mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
---
> 	mkcol(2, color2rgb(theme[Coltitle], 1), color2rgb(theme[Colhold], 1), color2rgb(theme[Collhold], 1));
293c432
< 	mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
---
> 	mkcol(3, color2rgb(theme[Coltitle], 1), color2rgb(theme[Colhold], 1), color2rgb(theme[Collhold], 1));
295c434
< 	mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
---
> 	mkcol(4, color2rgb(theme[Coltitle], 1), color2rgb(theme[Colhold], 1), color2rgb(theme[Collhold], 1));
297,299c436,438
< 	cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
< 	cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
< 	cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
---
> 	cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, color2rgb(theme[Colhigh], 1));
> 	cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, color2rgb(theme[Coltext], 1));
> 	cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, color2rgb(theme[Collhold], 1));
342c481
< 		runestring(screen, p, display->black, ZP, font, r);
---
> 		runestring(screen, p, stext, ZP, font, r);
442c581
< 		string(screen, g->overtmp->r.min, display->black, ZP, font, buf);
---
> 		string(screen, g->overtmp->r.min, stext, ZP, font, buf);
476,477d614
< 	static int xxx = 0;
< 
481a619,622
> 		a[0] = a[3];
> 		a[1] = a[4];
> 		a[2] = a[5];
> 		a[3] = a[6];
483,499c624,625
< 		a[Mem] = a[3];
< 		a[Maxmem] = a[4];
< 		a[Swap] = a[5];
< 		a[Maxswap] = a[6];
< 
< 		a[Reclaim] = 0;
< 		a[Maxreclaim] = 0;
< 		if(m->bufp = strstr(m->buf, "reclaim")){
< 			while(m->bufp > m->buf && m->bufp[-1] != '\n')
< 				m->bufp--;
< 			a[Reclaim] = strtoull(m->bufp, &m->bufp, 10);
< 			while(*m->bufp++ == '/')
< 				a[Maxreclaim] = strtoull(m->bufp, &m->bufp, 10);
< 		}
< 
< 		a[Kern] = 0;
< 		a[Maxkern] = 0;
---
> 		a[4] = 0;
> 		a[5] = 0;
503c629
< 			a[Kern] = strtoull(m->bufp, &m->bufp, 10);
---
> 			a[4] = strtoull(m->bufp, &m->bufp, 10);
505c631
< 				a[Maxkern] = strtoull(m->bufp, &m->bufp, 10);
---
> 				a[5] = strtoull(m->bufp, &m->bufp, 10);
508,509c634,635
< 		a[Draw] = 0;
< 		a[Maxdraw] = 0;
---
> 		a[6] = 0;
> 		a[7] = 0;
513c639
< 			a[Draw] = strtoull(m->bufp, &m->bufp, 10);
---
> 			a[6] = strtoull(m->bufp, &m->bufp, 10);
515c641
< 				a[Maxdraw] = strtoull(m->bufp, &m->bufp, 10);
---
> 				a[7] = strtoull(m->bufp, &m->bufp, 10);
521,527c647,650
< 	a[Reclaim] = 0;
< 	a[Maxreclaim] = 0;
< 	a[Kern] = 0;
< 	a[Maxkern] = 0;
< 	a[Draw] = 0;
< 	a[Maxdraw] = 0;
< 
---
> 	a[4] = 0;
> 	a[5] = 0;
> 	a[6] = 0;
> 	a[7] = 0;
625c748
< 		snprint(buf, sizeof buf, "%s/mnt/pm/battery", mpt);
---
> 		snprint(buf, sizeof buf, "%s/mnt/acpi/battery", mpt);
630d752
< 		batteryperiod = 10000;
642,643c764
< 		tempperiod = 5000;
< 		snprint(buf, sizeof buf, "%s/mnt/pm/cputemp", mpt);
---
> 		snprint(buf, sizeof buf, "%s/mnt/acpi/cputemp", mpt);
665c786
< 	return init | present[Mmem] | present[Mswap] | present[Mreclaim] | present[Mkern] | present[Mdraw];
---
> 	return init | present[Mmem] | present[Mswap] | present[Mkern] | present[Mdraw];
686,693c807
< 	static uint step = 0;
< 
< 	if(++step*sleeptime >= batteryperiod){
< 		step = 0;
< 		return init | present[Mbattery];
< 	}
< 
< 	return 0;
---
> 	return init | present[Mbattery];
705,712c819
< 	static uint step = 0;
< 
< 	if(++step*sleeptime >= tempperiod){
< 		step = 0;
< 		return init | present[Mtemp];
< 	}
< 
< 	return 0;
---
> 	return init | present[Mtemp];
758,763c865,868
< 	if(needbattery(init)){
< 		if(loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
< 			memmove(m->batterystats, a, sizeof(m->batterystats));
< 		else if(loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
< 			memmove(m->batterystats, a, sizeof(m->batterystats));
< 	}
---
> 	if(needbattery(init) && loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
> 		memmove(m->batterystats, a, sizeof(m->batterystats));
> 	if(needbattery(init) && loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
> 		memmove(m->batterystats, a, sizeof(m->batterystats));
792,800d896
< reclaimval(Machine *m, uvlong *v, uvlong *vmax, int)
< {
< 	*v = m->devswap[Reclaim];
< 	*vmax = m->devswap[Maxreclaim];
< 	if(*vmax == 0)
< 		*vmax = 1;
< }
< 
< void
899c995
< etherval(Machine *m, uvlong *v, uvlong *vmax, int)
---
> etherval(Machine *m, uvlong *v, uvlong *vmax, int init)
902c998,1000
< 	*vmax = sleeptime;
---
> 	*vmax = sleeptime*m->nproc;
> 	if(init)
> 		*vmax = sleeptime;
906c1004
< etherinval(Machine *m, uvlong *v, uvlong *vmax, int)
---
> etherinval(Machine *m, uvlong *v, uvlong *vmax, int init)
909c1007,1009
< 	*vmax = sleeptime;
---
> 	*vmax = sleeptime*m->nproc;
> 	if(init)
> 		*vmax = sleeptime;
913c1013
< etheroutval(Machine *m, uvlong *v, uvlong *vmax, int)
---
> etheroutval(Machine *m, uvlong *v, uvlong *vmax, int init)
916c1016,1018
< 	*vmax = sleeptime;
---
> 	*vmax = sleeptime*m->nproc;
> 	if(init)
> 		*vmax = sleeptime;
920c1022
< ethererrval(Machine *m, uvlong *v, uvlong *vmax, int)
---
> ethererrval(Machine *m, uvlong *v, uvlong *vmax, int init)
926,927c1028,1031
< 		*v += m->netetherstats[i]-m->prevetherstats[i];
< 	*vmax = (sleeptime/1000)*10;
---
> 		*v += m->netetherstats[i];
> 	*vmax = (sleeptime/1000)*10*m->nproc;
> 	if(init)
> 		*vmax = (sleeptime/1000)*10;
962c1066
< 	*vmax = 100;
---
> 	*vmax = sleeptime;
967c1071
< 		*v = l;
---
> 		*v = (l-20)*27;
1109c1213
< 	draw(screen, screen->r, display->white, nil, ZP);
---
> 	draw(screen, screen->r, display->black, nil, ZP);
1119c1223
< 		draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
---
> 		draw(screen, Rect(x, y-1, screen->r.max.x, y), stext, nil, ZP);
1128c1232
< 		draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
---
> 		draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), stext, nil, ZP);
1138c1242
< 		string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP, font, buf);
---
> 		string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), stext, ZP, font, buf);
1146c1250
< 		if(wid < dx-10){
---
> 		if(wid < (maxx-startx)-30){
1149c1253
< 			draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), display->black, nil, ZP);
---
> 			draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), stext, nil, ZP);
1161c1265
< 					draw(screen, Rect(maxx+1, ly, maxx+1+Lx, ly+1), display->black, nil, ZP);
---
> 					draw(screen, Rect(maxx+1, ly, maxx+1+Lx, ly+1), stext, nil, ZP);
1163c1267
< 					string(screen, Pt(maxx+1+Lx, ly), display->black, ZP, font, labs[k]);
---
> 					string(screen, Pt(maxx+1+Lx, ly), stext, ZP, font, labs[k]);
1171,1173c1275,1277
< 		machr = Rect(startx+i*dx, starty, startx+(i+1)*dx - 1, screen->r.max.y);
< 		if(i == nmach-1)
< 			machr.max.x = maxx;
---
> 		machr = Rect(startx+i*dx, starty, maxx, screen->r.max.y);
> 		if(i < nmach-1)
> 			machr.max.x = startx+(i+1)*dx - 1;
1199,1200d1302
< 			if(vmax == 0)
< 				vmax = 1;
1375,1377d1476
< 	case 'r':
< 		addgraph(Mreclaim);
< 		break;
1412a1512,1524
> 
> 	// load theme
> 	int sz, f;
> 	char *s;
> 
> 	if((f = open("/dev/theme", OREAD|OCEXEC)) >= 0){
> 		if((s = readall(f, &sz)) != nil)
> 			themeload(s, sz);
> 		free(s);
> 		close(f);
> 	}
> 
> 	stext = allocimage(display, Rect(0,0,1,1), CMAP8, 1, color2rgb(theme[Coltitle], 1));
1429,1430d1540
< 			if(vmax == 0)
< 				vmax = 1;