7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
12 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
17 #if defined(__OpenBSD__)
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/fcntl.h>
21 #include <machine/apmvar.h>
24 #define BATT_STATUS_NAME(status) \
25 (status == CS_CHARGING ? "CHR" : \
26 (status == CS_DISCHARGING ? "BAT" : "FULL"))
28 * Get battery information from /sys. Note that it uses the design capacity to
29 * calculate the percentage, not the last full capacity, so you can see how
30 * worn off your battery is.
33 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) {
38 char percentagebuf[16];
39 char remainingbuf[256];
40 char emptytimebuf[256];
41 char consumptionbuf[256];
42 const char *walk, *last;
43 char *outwalk = buffer;
45 bool colorful_output = false;
50 charging_status_t status = CS_DISCHARGING;
52 memset(statusbuf, '\0', sizeof(statusbuf));
53 memset(percentagebuf, '\0', sizeof(percentagebuf));
54 memset(remainingbuf, '\0', sizeof(remainingbuf));
55 memset(emptytimebuf, '\0', sizeof(emptytimebuf));
56 memset(consumptionbuf, '\0', sizeof(consumptionbuf));
58 static char batpath[512];
59 sprintf(batpath, path, number);
63 if (!slurp(batpath, buf, sizeof(buf))) {
64 OUTPUT_FULL_TEXT(format_down);
68 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
77 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
79 remaining = atoi(walk+1);
81 else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
83 remaining = atoi(walk+1);
85 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
86 present_rate = atoi(walk+1);
87 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
88 voltage = atoi(walk+1);
89 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
90 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
91 * unit instead of μAh. We will calculate it as we need it
93 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
94 present_rate = atoi(walk+1);
95 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
97 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
100 /* The only thing left is the full capacity */
101 if (last_full_capacity) {
102 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
103 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
106 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
107 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
111 full_design = atoi(walk+1);
115 /* the difference between POWER_SUPPLY_ENERGY_NOW and
116 * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
117 * given in mWh, the charge in mAh. So calculate every value given in
120 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
121 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
122 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
125 if ((full_design == -1) || (remaining == -1)) {
126 OUTPUT_FULL_TEXT(format_down);
130 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
132 float percentage_remaining = (((float)remaining / (float)full_design) * 100);
133 if (integer_battery_capacity) {
134 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00f%%", percentage_remaining);
136 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%", percentage_remaining);
139 if (present_rate > 0) {
140 float remaining_time;
141 int seconds, hours, minutes, seconds_remaining;
142 if (status == CS_CHARGING)
143 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
144 else if (status == CS_DISCHARGING)
145 remaining_time = ((float)remaining / (float)present_rate);
146 else remaining_time = 0;
148 seconds_remaining = (int)(remaining_time * 3600.0);
150 hours = seconds_remaining / 3600;
151 seconds = seconds_remaining - (hours * 3600);
152 minutes = seconds / 60;
153 seconds -= (minutes * 60);
155 if (status == CS_DISCHARGING && low_threshold > 0) {
156 if (strcasecmp(threshold_type, "percentage") == 0
157 && percentage_remaining < low_threshold) {
158 START_COLOR("color_bad");
159 colorful_output = true;
160 } else if (strcasecmp(threshold_type, "time") == 0
161 && seconds_remaining < 60 * low_threshold) {
162 START_COLOR("color_bad");
163 colorful_output = true;
165 colorful_output = false;
170 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d",
171 max(hours, 0), max(minutes, 0));
173 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
174 max(hours, 0), max(minutes, 0), max(seconds, 0));
176 empty_time = time(NULL);
177 empty_time += seconds_remaining;
178 empty_tm = localtime(&empty_time);
181 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d",
182 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
184 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
185 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
187 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
188 ((float)present_rate / 1000.0 / 1000.0));
190 /* On some systems, present_rate may not exist. Still, make sure
191 * we colorize the output if threshold_type is set to percentage
192 * (since we don't have any information on remaining time). */
193 if (status == CS_DISCHARGING && low_threshold > 0) {
194 if (strcasecmp(threshold_type, "percentage") == 0
195 && percentage_remaining < low_threshold) {
196 START_COLOR("color_bad");
197 colorful_output = true;
201 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
204 size_t sysctl_size = sizeof(sysctl_rslt);
206 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
207 OUTPUT_FULL_TEXT(format_down);
211 present_rate = sysctl_rslt;
212 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
213 OUTPUT_FULL_TEXT(format_down);
217 remaining = sysctl_rslt;
218 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
219 OUTPUT_FULL_TEXT(format_down);
224 if (state == 0 && present_rate == 100)
226 else if (state == 0 && present_rate < 100)
227 status = CS_CHARGING;
229 status = CS_DISCHARGING;
231 full_design = sysctl_rslt;
233 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
235 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
241 hours = minutes / 60;
242 minutes -= (hours * 60);
243 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
244 max(hours, 0), max(minutes, 0));
245 if (strcasecmp(threshold_type, "percentage") == 0
246 && present_rate < low_threshold) {
247 START_COLOR("color_bad");
248 colorful_output = true;
249 } else if (strcasecmp(threshold_type, "time") == 0
250 && remaining < (u_int) low_threshold) {
251 START_COLOR("color_bad");
252 colorful_output = true;
255 #elif defined(__OpenBSD__)
257 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
258 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
259 * probing acpi(4) devices.
261 struct apm_power_info apm_info;
264 apm_fd = open("/dev/apm", O_RDONLY);
266 OUTPUT_FULL_TEXT("can't open /dev/apm");
269 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
270 OUTPUT_FULL_TEXT("can't read power info");
274 /* Don't bother to go further if there's no battery present. */
275 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
276 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
277 OUTPUT_FULL_TEXT(format_down);
281 switch(apm_info.ac_state) {
283 status = CS_DISCHARGING;
286 status = CS_CHARGING;
289 /* If we don't know what's going on, just assume we're discharging. */
290 status = CS_DISCHARGING;
294 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
295 /* integer_battery_capacity is implied as battery_life is already in whole numbers. */
296 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00d%%", apm_info.battery_life);
298 if (status == CS_DISCHARGING && low_threshold > 0) {
299 if (strcasecmp(threshold_type, "percentage") == 0
300 && apm_info.battery_life < low_threshold) {
301 START_COLOR("color_bad");
302 colorful_output = true;
303 } else if (strcasecmp(threshold_type, "time") == 0
304 && apm_info.minutes_left < (u_int) low_threshold) {
305 START_COLOR("color_bad");
306 colorful_output = true;
310 /* Can't give a meaningful value for remaining minutes if we're charging. */
311 if (status != CS_CHARGING) {
312 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%d", apm_info.minutes_left);
314 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%s", "(CHR)");
321 #define EAT_SPACE_FROM_OUTPUT_IF_EMPTY(_buf) \
323 if (strlen(_buf) == 0) { \
324 if (outwalk > buffer && isspace(outwalk[-1])) \
326 else if (isspace(*(walk+1))) \
331 for (walk = format; *walk != '\0'; walk++) {
333 *(outwalk++) = *walk;
337 if (BEGINS_WITH(walk+1, "status")) {
338 outwalk += sprintf(outwalk, "%s", statusbuf);
339 walk += strlen("status");
340 } else if (BEGINS_WITH(walk+1, "percentage")) {
341 outwalk += sprintf(outwalk, "%s", percentagebuf);
342 walk += strlen("percentage");
343 } else if (BEGINS_WITH(walk+1, "remaining")) {
344 outwalk += sprintf(outwalk, "%s", remainingbuf);
345 walk += strlen("remaining");
346 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(remainingbuf);
347 } else if (BEGINS_WITH(walk+1, "emptytime")) {
348 outwalk += sprintf(outwalk, "%s", emptytimebuf);
349 walk += strlen("emptytime");
350 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(emptytimebuf);
351 } else if (BEGINS_WITH(walk+1, "consumption")) {
352 outwalk += sprintf(outwalk, "%s", consumptionbuf);
353 walk += strlen("consumption");
354 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf);
361 OUTPUT_FULL_TEXT(buffer);