10 #define POWER_BASEDIR "/sys/class/power_supply/"
14 void status_battery(GlobalData *g, char *batname)
17 char text[32] = { 0 };
27 int chargePercent = -1;
36 batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname);
37 if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) {
38 statusError("status_battery",
39 "batpath buffer too small",
43 strcpy(batpath, POWER_BASEDIR);
44 strcat(batpath, batname);
47 /* Is the battery present? */
48 if (access(batpath, F_OK)) {
49 //printf(" ^fg(grey)[%s] ", batname);
55 strcpy(&batpath[batpathlen], "/energy_now");
56 stlen = fileRead(stline, sizeof(stline), batpath);
58 chargeNow = atoi(stline);
61 strcpy(&batpath[batpathlen], "/energy_full");
62 stlen = fileRead(stline, sizeof(stline), batpath);
64 chargeFull = atoi(stline);
67 strcpy(&batpath[batpathlen], "/power_now");
68 stlen = fileRead(stline, sizeof(stline), batpath);
76 chargePercent = chargeNow / (chargeFull / 100);
79 if (chargePercent <= 40) {
80 if (chargePercent <= 25) {
81 if (chargePercent <= 10) {
82 s.color = "#FF0000"; // red
85 s.color = "#FFA500"; // orange
89 s.color = "#FFFF00"; // yellow
92 if (chargePercent > 70) {
93 s.color = "#FFFFFF"; // white
96 s.color = "#22FF22"; // green
100 battTime = (float)chargeNow / (float)battW;
103 // fully charged and not in use
104 snprintf(text, sizeof(text), "%s: %d%% _ _",
105 batname, chargePercent);
107 snprintf(text, sizeof(text), "%s: %d%% %.1fh %.1fW",
108 batname, chargePercent, battTime, (float)battW / 1000000.0);
111 line_append_item(g, &s);