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