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