]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
use correct units for battery calculation
[i3/i3status] / src / print_battery_info.c
1 // vim:ts=8:expandtab
2 #include <time.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
8
9 #include "i3status.h"
10
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
12 #include <sys/types.h>
13 #include <sys/sysctl.h>
14 #endif
15
16 #if defined(__OpenBSD__)
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/fcntl.h>
20 #include <machine/apmvar.h>
21 #endif
22
23 #define BATT_STATUS_NAME(status) \
24     (status == CS_CHARGING ? "CHR" : \
25         (status == CS_DISCHARGING ? "BAT" : "FULL"))
26 /*
27  * Get battery information from /sys. Note that it uses the design capacity to
28  * calculate the percentage, not the last full capacity, so you can see how
29  * worn off your battery is.
30  *
31  */
32 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int threshold, bool last_full_capacity) {
33         time_t empty_time;
34         struct tm *empty_tm;
35         char buf[1024];
36         char statusbuf[16];
37         char percentagebuf[16];
38         char remainingbuf[256];
39         char emptytimebuf[256];
40         char consumptionbuf[256];
41         const char *walk, *last;
42         char *outwalk = buffer;
43         bool watt_as_unit;
44         int full_design = -1,
45             remaining = -1,
46             present_rate = -1,
47             voltage = -1;
48         charging_status_t status = CS_DISCHARGING;
49
50         memset(statusbuf, '\0', sizeof(statusbuf));
51         memset(percentagebuf, '\0', sizeof(percentagebuf));
52         memset(remainingbuf, '\0', sizeof(remainingbuf));
53         memset(emptytimebuf, '\0', sizeof(emptytimebuf));
54         memset(consumptionbuf, '\0', sizeof(consumptionbuf));
55
56         INSTANCE(path);
57
58 #if defined(LINUX)
59         static char batpath[512];
60         sprintf(batpath, path, number);
61         if (!slurp(batpath, buf, sizeof(buf))) {
62                 OUTPUT_FULL_TEXT("No battery");
63                 return;
64         }
65
66         for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
67                 if (*walk == '\n') {
68                         last = walk+1;
69                         continue;
70                 }
71
72                 if (*walk != '=')
73                         continue;
74
75                 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
76                         watt_as_unit = true;
77                         remaining = atoi(walk+1);
78                 }
79                 else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
80                         watt_as_unit = false;
81                         remaining = atoi(walk+1);
82                 }
83                 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
84                         present_rate = atoi(walk+1);
85                 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
86                         voltage = atoi(walk+1);
87                 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
88                  * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
89                  * unit instead of μAh. We will calculate it as we need it
90                  * later. */
91                 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
92                         present_rate = atoi(walk+1);
93                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
94                         status = CS_CHARGING;
95                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
96                         status = CS_FULL;
97                 else {
98                         /* The only thing left is the full capacity */
99                         if (last_full_capacity) {
100                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
101                                     !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
102                                         continue;
103                         } else {
104                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
105                                     !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
106                                         continue;
107                         }
108
109                         full_design = atoi(walk+1);
110                 }
111         }
112
113         /* the difference between POWER_SUPPLY_ENERGY_NOW and
114          * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
115          * given in mWh, the charge in mAh. So calculate every value given in
116          * ampere to watt */
117         if (!watt_as_unit) {
118             present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
119             remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
120             full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
121         }
122
123         if ((full_design == -1) || (remaining == -1)) {
124                 OUTPUT_FULL_TEXT("No battery");
125                 return;
126         }
127
128         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
129
130         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
131                        (((float)remaining / (float)full_design) * 100));
132
133         if (present_rate > 0) {
134                 float remaining_time;
135                 int seconds, hours, minutes, seconds_remaining;
136                 if (status == CS_CHARGING)
137                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
138                 else if (status == CS_DISCHARGING)
139                         remaining_time = ((float)remaining / (float)present_rate);
140                 else remaining_time = 0;
141
142                 seconds_remaining = (int)(remaining_time * 3600.0);
143
144                 hours = seconds_remaining / 3600;
145                 seconds = seconds_remaining - (hours * 3600);
146                 minutes = seconds / 60;
147                 seconds -= (minutes * 60);
148
149                 if (status == CS_DISCHARGING && threshold > 0 && seconds_remaining < 60 * threshold)
150                         START_COLOR("color_bad");
151
152                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
153                         max(hours, 0), max(minutes, 0), max(seconds, 0));
154
155                 empty_time = time(NULL);
156                 empty_time += seconds_remaining;
157                 empty_tm = localtime(&empty_time);
158
159                 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
160                         max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
161
162                 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
163                         ((float)present_rate / 1000.0 / 1000.0));
164
165                 END_COLOR;
166         }
167 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
168         int state;
169         int sysctl_rslt;
170         size_t sysctl_size = sizeof(sysctl_rslt);
171
172         if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
173                 OUTPUT_FULL_TEXT("No battery");
174                 return;
175         }
176
177         present_rate = sysctl_rslt;
178         if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
179                 OUTPUT_FULL_TEXT("No battery");
180                 return;
181         }
182
183         remaining = sysctl_rslt;
184         if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
185                 OUTPUT_FULL_TEXT("No battery");
186                 return;
187         }
188
189         state = sysctl_rslt;
190         if (state == 0 && present_rate == 100)
191                 status = CS_FULL;
192         else if (state == 0 && present_rate < 100)
193                 status = CS_CHARGING;
194         else
195                 status = CS_DISCHARGING;
196
197         full_design = sysctl_rslt;
198
199         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
200
201         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
202                        present_rate);
203
204         if (state == 1) {
205                 int hours, minutes;
206                 minutes = remaining;
207                 hours = minutes / 60;
208                 minutes -= (hours * 60);
209                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
210                                max(hours, 0), max(minutes, 0));
211         }
212 #elif defined(__OpenBSD__)
213         /*
214          * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
215          * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
216          * probing acpi(4) devices.
217          */
218         struct apm_power_info apm_info;
219         int apm_fd, ac_status, charging;
220
221         apm_fd = open("/dev/apm", O_RDONLY);
222         if (apm_fd < 0) {
223                 OUTPUT_FULL_TEXT("can't open /dev/apm");
224                 return;
225         }
226         if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
227                 OUTPUT_FULL_TEXT("can't read power info");
228
229         close(apm_fd);
230
231         /* Don't bother to go further if there's no battery present. */
232         if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
233             (apm_info.battery_state == APM_BATT_UNKNOWN)) {
234                 OUTPUT_FULL_TEXT("No battery");
235                 return;
236         }
237
238         switch(apm_info.ac_state) {
239         case APM_AC_OFF:
240                 ac_status = CS_DISCHARGING;
241                 break;
242         case APM_AC_ON:
243                 ac_status = CS_CHARGING;
244                 break;
245         default:
246                 /* If we don't know what's going on, just assume we're discharging. */
247                 ac_status = CS_DISCHARGING;
248                 break;
249         }
250
251         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
252         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%", apm_info.battery_life);
253
254         /* Can't give a meaningful value for remaining minutes if we're charging. */
255         if (ac_status == CS_CHARGING)
256                 charging = 1;
257
258         (void)snprintf(remainingbuf, sizeof(remainingbuf), (charging ? "%s" : "%d"),
259                        (charging ? "(CHR)" : apm_info.minutes_left));
260 #endif
261
262         for (walk = format; *walk != '\0'; walk++) {
263                 if (*walk != '%') {
264                         *(outwalk++) = *walk;
265                         continue;
266                 }
267
268                 if (strncmp(walk+1, "status", strlen("status")) == 0) {
269                         outwalk += sprintf(outwalk, "%s", statusbuf);
270                         walk += strlen("status");
271                 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
272                         outwalk += sprintf(outwalk, "%s", percentagebuf);
273                         walk += strlen("percentage");
274                 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
275                         outwalk += sprintf(outwalk, "%s", remainingbuf);
276                         walk += strlen("remaining");
277                 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
278                         outwalk += sprintf(outwalk, "%s", emptytimebuf);
279                         walk += strlen("emptytime");
280                 } else if (strncmp(walk+1, "consumption", strlen("consumption")) == 0) {
281                         outwalk += sprintf(outwalk, "%s", consumptionbuf);
282                         walk += strlen("consumption");
283                 }
284         }
285
286         OUTPUT_FULL_TEXT(buffer);
287 }