Clean up battery status, fix wrong time estimate
[sysstatus.git] / statuses / battery.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include "string.h"
5 #include "battery.h"
6
7 #ifndef POWER_BASEDIR
8         #define POWER_BASEDIR "/sys/class/power_supply/"
9 #endif
10
11
12 void status_battery(char *batname)
13 {
14         char batpath[256];
15         int batpathlen;
16
17         char stline[16];
18         ssize_t stlen;
19
20         int chargeNow = 0;
21         int chargeFull = -1;
22         int chargePercent = -1;
23         int battW = 1;
24         int battV = 0;
25         float battTime = -1;
26
27
28         // Prepare path
29         batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname);
30         if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath))
31         {
32                 statusError("status_battery",
33                         "batpath buffer too small",
34                         batname);
35                 return;
36         }
37         strcpy(batpath, POWER_BASEDIR);
38         strcat(batpath, batname);
39
40
41         // Is the battery present?
42         if (access(batpath, F_OK))
43         {
44                 //printf(" ^fg(grey)[%s] ", batname);
45                 return;
46         }
47
48
49         // Get info
50         strcpy(&batpath[batpathlen], "/energy_now");
51         stlen = fileRead(stline, sizeof(stline), batpath);
52         if (stlen > 0)
53                 chargeNow = atoi(stline);
54
55         strcpy(&batpath[batpathlen], "/energy_full");
56         stlen = fileRead(stline, sizeof(stline), batpath);
57         if (stlen > 0)
58                 chargeFull = atoi(stline);
59
60         strcpy(&batpath[batpathlen], "/power_now");
61         stlen = fileRead(stline, sizeof(stline), batpath);
62         if (stlen > 0)
63                 battW = atoi(stline);
64
65         strcpy(&batpath[batpathlen], "/voltage_now");
66         stlen = fileRead(stline, sizeof(stline), batpath);
67         if (stlen > 0)
68                 battV = atoi(stline);
69
70
71         // Prettyprint
72         if (chargeFull > 0)
73                 chargePercent = chargeNow / (chargeFull / 100);
74
75         if (chargePercent <= 40) // 40
76         {
77                 if (chargePercent <= 25) // 25
78                 {
79                         if (chargePercent <= 10) // 10
80                                 fputs("^fg(red)", stdout);
81                         else // 11-25%
82                                 fputs("^fg(orange)", stdout);
83                 }
84                 else // 26-40%
85                         fputs("^fg(yellow)", stdout);
86         }
87         else
88         {
89                 if (chargePercent > 70) // 70
90                         fputs("^fg(white)", stdout);
91                 else // 41-70%
92                         fputs("^fg(green)", stdout);
93         }
94
95         battTime = (float)chargeNow / (float)battW;
96
97         if (battW == 0) // fully charged and not in use
98                 printf(" %s: %d%% _ _ ",
99                         batname, chargePercent);
100         else
101                 printf(" %s: %d%% %.1fh %.1fW ",
102                         batname, chargePercent, battTime, (float)battW / 1000000.0);
103 }