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