]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
74d4460de2c33f0484fdb0932333cad59a061f1e
[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         int full_design = -1,
44             remaining = -1,
45             present_rate = -1;
46         charging_status_t status = CS_DISCHARGING;
47
48         memset(statusbuf, '\0', sizeof(statusbuf));
49         memset(percentagebuf, '\0', sizeof(percentagebuf));
50         memset(remainingbuf, '\0', sizeof(remainingbuf));
51         memset(emptytimebuf, '\0', sizeof(emptytimebuf));
52         memset(consumptionbuf, '\0', sizeof(consumptionbuf));
53
54         INSTANCE(path);
55
56 #if defined(LINUX)
57         static char batpath[512];
58         sprintf(batpath, path, number);
59         if (!slurp(batpath, buf, sizeof(buf))) {
60                 OUTPUT_FULL_TEXT("No battery");
61                 return;
62         }
63
64         for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
65                 if (*walk == '\n') {
66                         last = walk+1;
67                         continue;
68                 }
69
70                 if (*walk != '=')
71                         continue;
72
73                 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
74                     BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
75                         remaining = atoi(walk+1);
76                 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
77                         present_rate = atoi(walk+1);
78                 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
79                         present_rate = atoi(walk+1);
80                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
81                         status = CS_CHARGING;
82                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
83                         status = CS_FULL;
84                 else {
85                         /* The only thing left is the full capacity */
86                         if (last_full_capacity) {
87                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
88                                     !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
89                                         continue;
90                         } else {
91                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
92                                     !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
93                                         continue;
94                         }
95
96                         full_design = atoi(walk+1);
97                 }
98         }
99
100         if ((full_design == -1) || (remaining == -1)) {
101                 OUTPUT_FULL_TEXT("No battery");
102                 return;
103         }
104
105         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
106
107         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
108                        (((float)remaining / (float)full_design) * 100));
109
110         if (present_rate > 0) {
111                 float remaining_time;
112                 int seconds, hours, minutes, seconds_remaining;
113                 if (status == CS_CHARGING)
114                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
115                 else if (status == CS_DISCHARGING)
116                         remaining_time = ((float)remaining / (float)present_rate);
117                 else remaining_time = 0;
118
119                 seconds_remaining = (int)(remaining_time * 3600.0);
120
121                 hours = seconds_remaining / 3600;
122                 seconds = seconds_remaining - (hours * 3600);
123                 minutes = seconds / 60;
124                 seconds -= (minutes * 60);
125
126                 if (status == CS_DISCHARGING && threshold > 0 && seconds_remaining < 60 * threshold)
127                         START_COLOR("color_bad");
128
129                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
130                         max(hours, 0), max(minutes, 0), max(seconds, 0));
131
132                 empty_time = time(NULL);
133                 empty_time += seconds_remaining;
134                 empty_tm = localtime(&empty_time);
135
136                 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
137                         max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
138
139                 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
140                         ((float)present_rate / 1000.0 / 1000.0));
141
142                 END_COLOR;
143         }
144 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
145         int state;
146         int sysctl_rslt;
147         size_t sysctl_size = sizeof(sysctl_rslt);
148
149         if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
150                 OUTPUT_FULL_TEXT("No battery");
151                 return;
152         }
153
154         present_rate = sysctl_rslt;
155         if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
156                 OUTPUT_FULL_TEXT("No battery");
157                 return;
158         }
159
160         remaining = sysctl_rslt;
161         if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
162                 OUTPUT_FULL_TEXT("No battery");
163                 return;
164         }
165
166         state = sysctl_rslt;
167         if (state == 0 && present_rate == 100)
168                 status = CS_FULL;
169         else if (state == 0 && present_rate < 100)
170                 status = CS_CHARGING;
171         else
172                 status = CS_DISCHARGING;
173
174         full_design = sysctl_rslt;
175
176         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
177
178         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
179                        present_rate);
180
181         if (state == 1) {
182                 int hours, minutes;
183                 minutes = remaining;
184                 hours = minutes / 60;
185                 minutes -= (hours * 60);
186                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
187                                max(hours, 0), max(minutes, 0));
188         }
189 #elif defined(__OpenBSD__)
190         /*
191          * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
192          * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
193          * probing acpi(4) devices.
194          */
195         struct apm_power_info apm_info;
196         int apm_fd, ac_status, charging;
197
198         apm_fd = open("/dev/apm", O_RDONLY);
199         if (apm_fd < 0) {
200                 OUTPUT_FULL_TEXT("can't open /dev/apm");
201                 return;
202         }
203         if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
204                 OUTPUT_FULL_TEXT("can't read power info");
205
206         close(apm_fd);
207
208         /* Don't bother to go further if there's no battery present. */
209         if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
210             (apm_info.battery_state == APM_BATT_UNKNOWN)) {
211                 OUTPUT_FULL_TEXT("No battery");
212                 return;
213         }
214
215         switch(apm_info.ac_state) {
216         case APM_AC_OFF:
217                 ac_status = CS_DISCHARGING;
218                 break;
219         case APM_AC_ON:
220                 ac_status = CS_CHARGING;
221                 break;
222         default:
223                 /* If we don't know what's going on, just assume we're discharging. */
224                 ac_status = CS_DISCHARGING;
225                 break;
226         }
227
228         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
229         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%", apm_info.battery_life);
230
231         /* Can't give a meaningful value for remaining minutes if we're charging. */
232         if (ac_status == CS_CHARGING)
233                 charging = 1;
234
235         (void)snprintf(remainingbuf, sizeof(remainingbuf), (charging ? "%s" : "%d"),
236                        (charging ? "(CHR)" : apm_info.minutes_left));
237 #endif
238
239         for (walk = format; *walk != '\0'; walk++) {
240                 if (*walk != '%') {
241                         *(outwalk++) = *walk;
242                         continue;
243                 }
244
245                 if (strncmp(walk+1, "status", strlen("status")) == 0) {
246                         outwalk += sprintf(outwalk, "%s", statusbuf);
247                         walk += strlen("status");
248                 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
249                         outwalk += sprintf(outwalk, "%s", percentagebuf);
250                         walk += strlen("percentage");
251                 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
252                         outwalk += sprintf(outwalk, "%s", remainingbuf);
253                         walk += strlen("remaining");
254                 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
255                         outwalk += sprintf(outwalk, "%s", emptytimebuf);
256                         walk += strlen("emptytime");
257                 } else if (strncmp(walk+1, "consumption", strlen("consumption")) == 0) {
258                         outwalk += sprintf(outwalk, "%s", consumptionbuf);
259                         walk += strlen("consumptionbuf");
260                 }
261         }
262
263         OUTPUT_FULL_TEXT(buffer);
264 }