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