summaryrefslogtreecommitdiff
path: root/battery.c
blob: f19d1e8c7a10b8c010269e885ead0b4cb228cc8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <draw.h>
#include <event.h>

enum
{
	SEC = 1000,	/* ms */

	/* battery discharge levels and charging state */
	JLOW = 0,
	JMED,
	JHIG,
	JCHR,
	NBATTLVL,

	/* percentage text colors */
	PDIS = 0,
	PCHR,
	NPERC,

	/* color palette */
	CBG = 0,
	CFG,
	CJUICE,
	NCOLOR,

	Labspace = 2	/* room around the label */
};

Image *juicecolors[NBATTLVL], *textcolors[NPERC], *palette[NCOLOR];
int bfd, nbatt, lvl, ischarging;
char lvlstr[5];

char battfile[] = "/mnt/acpi/battery";

void
juicereflow(double ratio)
{
	Point sp;

	sp = Pt(screen->r.min.x, screen->r.max.y - Dy(screen->r)*ratio);
	draw(screen, Rpt(sp, screen->r.max), palette[CJUICE], nil, ZP);
}

void
label(Point p, char *text)
{
	string(screen, p, palette[CFG], ZP, font, text);
}

Image *
rgba(ulong c)
{
	Image *i;

	i = allocimage(display, Rect(0,0,1,1), screen->chan, 1, c);
	if(i == nil)
		sysfatal("allocimage: %r");
	return i;
}

void
redraw(void)
{
	Point p = Pt(((screen->r.min.x+screen->r.max.x)/2)-((font->width*strlen(lvlstr))/2),
		screen->r.max.y-(font->height+Labspace));

	draw(screen, screen->r, palette[CBG], nil, ZP);
	juicereflow(lvl/100.0);
	label(p, lvlstr);
}

void
update(void)
{
	char buf[256], idbuf[16], *s, *p;
	int n;

	if((n = pread(bfd, buf, sizeof(buf)-1, 0)) <= 0)
		sysfatal("pread: %r");
	buf[n] = 0;
	s = buf;
	nbatt = 0;
	lvl = strtol(s, &s, 0);
	do{
		if(isalpha(*s)){
			p = idbuf;
			while(isalpha(*s) && p < idbuf + sizeof idbuf - 1)
				*p++ = *s++;
			*p = 0;
			if(strcmp(idbuf, "charging") == 0)
				ischarging = 1;
			else if(strcmp(idbuf, "discharging") == 0)
				ischarging = 0;
		}
		if(*s == '\n'){
			nbatt++;
			if(isdigit(*++s))
				lvl += strtol(s, &s, 0);
		}
	}while(*s++);
	lvl /= nbatt;
	if(ischarging){
		palette[CFG] = textcolors[PCHR];
		palette[CJUICE] = juicecolors[JCHR];
	}else{
		palette[CFG] = textcolors[PDIS];
		palette[CJUICE] = lvl < 20 ? juicecolors[JLOW]:
				lvl < 60 ? juicecolors[JMED]:
					juicecolors[JHIG];
	}
	snprint(lvlstr, sizeof lvlstr, "%d%%", lvl);
}

void
eresized(int new)
{
	if(new && getwindow(display, Refnone) < 0)
		fprint(2, "can't reattach to window\n");
	redraw();
}

void
usage(void)
{
	fprint(2, "usage: %s\n", argv0);
	exits("usage");
}

void
main(int argc, char *argv[])
{
	Event e;
	int Etimer, key;

	ARGBEGIN{
	default: usage();
	}ARGEND;
	if(argc != 0)
		usage();
	bfd = open(battfile, OREAD);
	if(bfd < 0)
		sysfatal("open: %r");
	if(initdraw(0, 0, "battery") < 0)
		sysfatal("initdraw: %r");
	juicecolors[JLOW] = rgba(DRed);
	juicecolors[JMED] = rgba(DDarkyellow);
	juicecolors[JHIG] = rgba(DMedgreen);
	juicecolors[JCHR] = rgba(0x0088CCFF);
	textcolors[PDIS] = display->black;
	textcolors[PCHR] = rgba(DYellow);
	palette[CBG] = allocimagemix(display, 0x88FF88FF, DWhite);
	palette[CFG] = textcolors[PDIS];
	einit(Emouse);
	Etimer = etimer(0, 120*SEC);
	update();
	redraw();
	for(;;){
		key = event(&e);
		if(key == Etimer)
			update();
		redraw();
	}
}