]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
cc7c5fef580a4c36e557757c52d015493fddc350
[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, int threshold, 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                 if (threshold > 0 && seconds_remaining < 60 * threshold)
124                         START_COLOR("color_bad");
125
126                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
127                         max(hours, 0), max(minutes, 0), max(seconds, 0));
128
129                 empty_time = time(NULL);
130                 empty_time += seconds_remaining;
131                 empty_tm = localtime(&empty_time);
132
133                 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
134                         max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
135
136                 END_COLOR;
137         }
138 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
139         int state;
140         int sysctl_rslt;
141         size_t sysctl_size = sizeof(sysctl_rslt);
142
143         if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
144                 OUTPUT_FULL_TEXT("No battery");
145                 return;
146         }
147
148         present_rate = sysctl_rslt;
149         if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
150                 OUTPUT_FULL_TEXT("No battery");
151                 return;
152         }
153
154         remaining = sysctl_rslt;
155         if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
156                 OUTPUT_FULL_TEXT("No battery");
157                 return;
158         }
159
160         state = sysctl_rslt;
161         if (state == 0 && present_rate == 100)
162                 status = CS_FULL;
163         else if (state == 0 && present_rate < 100)
164                 status = CS_CHARGING;
165         else
166                 status = CS_DISCHARGING;
167
168         full_design = sysctl_rslt;
169
170         (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
171                         (status == CS_CHARGING ? "CHR" :
172                          (status == CS_DISCHARGING ? "BAT" : "FULL")));
173
174         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
175                        present_rate);
176
177         if (state == 1) {
178                 int hours, minutes;
179                 minutes = remaining;
180                 hours = minutes / 60;
181                 minutes -= (hours * 60);
182                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
183                                max(hours, 0), max(minutes, 0));
184         }
185 #elif defined(__OpenBSD__)
186         /*
187          * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
188          * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
189          * probing acpi(4) devices.
190          */
191         struct apm_power_info apm_info;
192         int apm_fd, ac_status, charging;
193
194         apm_fd = open("/dev/apm", O_RDONLY);
195         if (apm_fd < 0) {
196                 OUTPUT_FULL_TEXT("can't open /dev/apm");
197                 return;
198         }
199         if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
200                 OUTPUT_FULL_TEXT("can't read power info");
201
202         close(apm_fd);
203
204         /* Don't bother to go further if there's no battery present. */
205         if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
206             (apm_info.battery_state == APM_BATT_UNKNOWN)) {
207                 OUTPUT_FULL_TEXT("No battery");
208                 return;
209         }
210
211         switch(apm_info.ac_state) {
212         case APM_AC_OFF:
213                 ac_status = CS_DISCHARGING;
214                 break;
215         case APM_AC_ON:
216                 ac_status = CS_CHARGING;
217                 break;
218         default:
219                 /* If we don't know what's going on, just assume we're discharging. */
220                 ac_status = CS_DISCHARGING;
221                 break;
222         }
223
224         (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
225                        (ac_status == CS_CHARGING ? "CHR" :
226                         (ac_status == CS_DISCHARGING ? "BAT" : "FULL")));
227
228         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%", apm_info.battery_life);
229
230         /* Can't give a meaningful value for remaining minutes if we're charging. */
231         if (ac_status == CS_CHARGING)
232                 charging = 1;
233
234         (void)snprintf(remainingbuf, sizeof(remainingbuf), (charging ? "%s" : "%d"),
235                        (charging ? "(CHR)" : apm_info.minutes_left));
236 #endif
237
238         for (walk = format; *walk != '\0'; walk++) {
239                 if (*walk != '%') {
240                         *(outwalk++) = *walk;
241                         continue;
242                 }
243
244                 if (strncmp(walk+1, "status", strlen("status")) == 0) {
245                         outwalk += sprintf(outwalk, "%s", statusbuf);
246                         walk += strlen("status");
247                 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
248                         outwalk += sprintf(outwalk, "%s", percentagebuf);
249                         walk += strlen("percentage");
250                 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
251                         outwalk += sprintf(outwalk, "%s", remainingbuf);
252                         walk += strlen("remaining");
253                 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
254                         outwalk += sprintf(outwalk, "%s", emptytimebuf);
255                         walk += strlen("emptytime");
256                 }
257         }
258
259         OUTPUT_FULL_TEXT(buffer);
260 }