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 #if defined(__NetBSD__)
26 #include <prop/proplib.h>
27 #include <sys/envsys.h>
30 #define BATT_STATUS_NAME(status) \
31 (status == CS_CHARGING ? "CHR" : \
32 (status == CS_DISCHARGING ? "BAT" : "FULL"))
34 * Get battery information from /sys. Note that it uses the design capacity to
35 * calculate the percentage, not the last full capacity, so you can see how
36 * worn off your battery is.
39 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) {
44 char percentagebuf[16];
45 char remainingbuf[256];
46 char emptytimebuf[256];
47 char consumptionbuf[256];
48 const char *walk, *last;
49 char *outwalk = buffer;
51 bool colorful_output = false;
56 charging_status_t status = CS_DISCHARGING;
58 memset(statusbuf, '\0', sizeof(statusbuf));
59 memset(percentagebuf, '\0', sizeof(percentagebuf));
60 memset(remainingbuf, '\0', sizeof(remainingbuf));
61 memset(emptytimebuf, '\0', sizeof(emptytimebuf));
62 memset(consumptionbuf, '\0', sizeof(consumptionbuf));
64 static char batpath[512];
65 sprintf(batpath, path, number);
69 if (!slurp(batpath, buf, sizeof(buf))) {
70 OUTPUT_FULL_TEXT(format_down);
74 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
83 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
85 remaining = atoi(walk+1);
87 else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
89 remaining = atoi(walk+1);
91 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
92 present_rate = abs(atoi(walk+1));
93 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
94 voltage = abs(atoi(walk+1));
95 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
96 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
97 * unit instead of μAh. We will calculate it as we need it
99 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
100 present_rate = abs(atoi(walk+1));
101 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
102 status = CS_CHARGING;
103 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
106 /* The only thing left is the full capacity */
107 if (last_full_capacity) {
108 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
109 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
112 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
113 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
117 full_design = atoi(walk+1);
121 /* the difference between POWER_SUPPLY_ENERGY_NOW and
122 * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
123 * given in mWh, the charge in mAh. So calculate every value given in
126 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
129 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
130 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
134 if ((full_design == -1) || (remaining == -1)) {
135 OUTPUT_FULL_TEXT(format_down);
139 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
141 float percentage_remaining = (((float)remaining / (float)full_design) * 100);
142 if (integer_battery_capacity) {
143 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00f%%", percentage_remaining);
145 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%", percentage_remaining);
148 if (present_rate > 0) {
149 float remaining_time;
150 int seconds, hours, minutes, seconds_remaining;
151 if (status == CS_CHARGING)
152 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
153 else if (status == CS_DISCHARGING)
154 remaining_time = ((float)remaining / (float)present_rate);
155 else remaining_time = 0;
157 seconds_remaining = (int)(remaining_time * 3600.0);
159 hours = seconds_remaining / 3600;
160 seconds = seconds_remaining - (hours * 3600);
161 minutes = seconds / 60;
162 seconds -= (minutes * 60);
164 if (status == CS_DISCHARGING && low_threshold > 0) {
165 if (strcasecmp(threshold_type, "percentage") == 0
166 && percentage_remaining < low_threshold) {
167 START_COLOR("color_bad");
168 colorful_output = true;
169 } else if (strcasecmp(threshold_type, "time") == 0
170 && seconds_remaining < 60 * low_threshold) {
171 START_COLOR("color_bad");
172 colorful_output = true;
174 colorful_output = false;
179 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d",
180 max(hours, 0), max(minutes, 0));
182 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
183 max(hours, 0), max(minutes, 0), max(seconds, 0));
185 empty_time = time(NULL);
186 empty_time += seconds_remaining;
187 empty_tm = localtime(&empty_time);
190 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d",
191 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
193 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
194 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
196 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
197 ((float)present_rate / 1000.0 / 1000.0));
199 /* On some systems, present_rate may not exist. Still, make sure
200 * we colorize the output if threshold_type is set to percentage
201 * (since we don't have any information on remaining time). */
202 if (status == CS_DISCHARGING && low_threshold > 0) {
203 if (strcasecmp(threshold_type, "percentage") == 0
204 && percentage_remaining < low_threshold) {
205 START_COLOR("color_bad");
206 colorful_output = true;
210 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
213 size_t sysctl_size = sizeof(sysctl_rslt);
215 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
216 OUTPUT_FULL_TEXT(format_down);
220 present_rate = sysctl_rslt;
221 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
222 OUTPUT_FULL_TEXT(format_down);
226 remaining = sysctl_rslt;
227 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
228 OUTPUT_FULL_TEXT(format_down);
233 if (state == 0 && present_rate == 100)
235 else if (state == 0 && present_rate < 100)
236 status = CS_CHARGING;
238 status = CS_DISCHARGING;
240 full_design = sysctl_rslt;
242 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
244 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
250 hours = minutes / 60;
251 minutes -= (hours * 60);
252 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
253 max(hours, 0), max(minutes, 0));
254 if (strcasecmp(threshold_type, "percentage") == 0
255 && present_rate < low_threshold) {
256 START_COLOR("color_bad");
257 colorful_output = true;
258 } else if (strcasecmp(threshold_type, "time") == 0
259 && remaining < (u_int) low_threshold) {
260 START_COLOR("color_bad");
261 colorful_output = true;
264 #elif defined(__OpenBSD__)
266 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
267 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
268 * probing acpi(4) devices.
270 struct apm_power_info apm_info;
273 apm_fd = open("/dev/apm", O_RDONLY);
275 OUTPUT_FULL_TEXT("can't open /dev/apm");
278 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
279 OUTPUT_FULL_TEXT("can't read power info");
283 /* Don't bother to go further if there's no battery present. */
284 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
285 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
286 OUTPUT_FULL_TEXT(format_down);
290 switch(apm_info.ac_state) {
292 status = CS_DISCHARGING;
295 status = CS_CHARGING;
298 /* If we don't know what's going on, just assume we're discharging. */
299 status = CS_DISCHARGING;
303 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
304 /* integer_battery_capacity is implied as battery_life is already in whole numbers. */
305 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00d%%", apm_info.battery_life);
307 if (status == CS_DISCHARGING && low_threshold > 0) {
308 if (strcasecmp(threshold_type, "percentage") == 0
309 && apm_info.battery_life < low_threshold) {
310 START_COLOR("color_bad");
311 colorful_output = true;
312 } else if (strcasecmp(threshold_type, "time") == 0
313 && apm_info.minutes_left < (u_int) low_threshold) {
314 START_COLOR("color_bad");
315 colorful_output = true;
319 /* Can't give a meaningful value for remaining minutes if we're charging. */
320 if (status != CS_CHARGING) {
321 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%d", apm_info.minutes_left);
323 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%s", "(CHR)");
328 #elif defined(__NetBSD__)
330 * Using envsys(4) via sysmon(4).
332 int fd, rval, last_full_cap;
333 bool is_found = false;
335 bool is_full = false;
337 prop_dictionary_t dict;
339 prop_object_iterator_t iter;
340 prop_object_iterator_t iter2;
341 prop_object_t obj, obj2, obj3, obj4, obj5;
343 asprintf(&sensor_desc, "acpibat%d", number);
345 fd = open("/dev/sysmon", O_RDONLY);
347 OUTPUT_FULL_TEXT("can't open /dev/sysmon");
351 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
357 if (prop_dictionary_count(dict) == 0) {
358 prop_object_release(dict);
363 iter = prop_dictionary_iterator(dict);
365 prop_object_release(dict);
369 /* iterate over the dictionary returned by the kernel */
370 while ((obj = prop_object_iterator_next(iter)) != NULL) {
371 /* skip this dict if it's not what we're looking for */
372 if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) == strlen(sensor_desc)) &&
373 (strncmp(sensor_desc,
374 prop_dictionary_keysym_cstring_nocopy(obj),
375 strlen(sensor_desc)) != 0))
380 array = prop_dictionary_get_keysym(dict, obj);
381 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
382 prop_object_iterator_release(iter);
383 prop_object_release(dict);
388 iter2 = prop_array_iterator(array);
390 prop_object_iterator_release(iter);
391 prop_object_release(dict);
396 /* iterate over array of dicts specific to target battery */
397 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
398 obj3 = prop_dictionary_get(obj2, "description");
401 strlen(prop_string_cstring_nocopy(obj3)) == 8 &&
403 prop_string_cstring_nocopy(obj3),
406 obj3 = prop_dictionary_get(obj2, "cur-value");
408 if (prop_number_integer_value(obj3))
409 status = CS_CHARGING;
411 status = CS_DISCHARGING;
417 strlen(prop_string_cstring_nocopy(obj3)) == 6 &&
419 prop_string_cstring_nocopy(obj3),
422 obj3 = prop_dictionary_get(obj2, "cur-value");
423 obj4 = prop_dictionary_get(obj2, "max-value");
424 obj5 = prop_dictionary_get(obj2, "type");
426 remaining = prop_number_integer_value(obj3);
427 full_design = prop_number_integer_value(obj4);
429 if (remaining == full_design)
432 if (strncmp("Ampere hour",
433 prop_string_cstring_nocopy(obj5),
435 watt_as_unit = false;
443 strlen(prop_string_cstring_nocopy(obj3)) == 14 &&
444 strncmp("discharge rate",
445 prop_string_cstring_nocopy(obj3),
448 obj3 = prop_dictionary_get(obj2, "cur-value");
449 present_rate = prop_number_integer_value(obj3);
454 strlen(prop_string_cstring_nocopy(obj3)) == 13 &&
455 strncmp("last full cap",
456 prop_string_cstring_nocopy(obj3),
459 obj3 = prop_dictionary_get(obj2, "cur-value");
460 last_full_cap = prop_number_integer_value(obj3);
465 strlen(prop_string_cstring_nocopy(obj3)) == 7 &&
467 prop_string_cstring_nocopy(obj3),
470 obj3 = prop_dictionary_get(obj2, "cur-value");
471 voltage = prop_number_integer_value(obj3);
475 prop_object_iterator_release(iter2);
478 prop_object_iterator_release(iter);
479 prop_object_release(dict);
483 OUTPUT_FULL_TEXT(format_down);
487 if (last_full_capacity)
488 full_design = last_full_cap;
490 if (! watt_as_unit) {
491 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
492 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
493 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
496 float percentage_remaining =
497 (((float)remaining / (float)full_design) * 100);
499 if (integer_battery_capacity)
500 (void)snprintf(percentagebuf,
501 sizeof(percentagebuf),
503 (int) percentage_remaining);
505 (void)snprintf(percentagebuf,
506 sizeof(percentagebuf),
508 percentage_remaining);
511 * Handle percentage low_threshold here, and time low_threshold when
514 if (status == CS_DISCHARGING && low_threshold > 0) {
515 if (strcasecmp(threshold_type, "percentage") == 0
516 && (((float)remaining / (float)full_design) * 100) < low_threshold) {
517 START_COLOR("color_bad");
518 colorful_output = true;
523 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(CS_FULL));
525 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
528 * The envsys(4) ACPI routines do not appear to provide a 'time
529 * remaining' figure, so we must deduce it.
531 float remaining_time;
532 int seconds, hours, minutes, seconds_remaining;
534 if (status == CS_CHARGING)
535 remaining_time = ((float)full_design - (float)remaining)
536 / (float)present_rate;
537 else if (status == CS_DISCHARGING)
538 remaining_time = ((float)remaining / (float)present_rate);
539 else remaining_time = 0;
541 seconds_remaining = (int)(remaining_time * 3600.0);
543 hours = seconds_remaining / 3600;
544 seconds = seconds_remaining - (hours * 3600);
545 minutes = seconds / 60;
546 seconds -= (minutes * 60);
548 if (status != CS_CHARGING) {
550 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d",
551 max(hours, 0), max(minutes, 0));
553 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
554 max(hours, 0), max(minutes, 0), max(seconds, 0));
556 if (low_threshold > 0) {
557 if (strcasecmp(threshold_type, "time") == 0
558 && ((float) seconds_remaining / 60.0) < (u_int) low_threshold) {
559 START_COLOR("color_bad");
560 colorful_output = true;
565 (void)snprintf(remainingbuf, sizeof(remainingbuf), "(%02d:%02d until full)",
566 max(hours, 0), max(minutes, 0));
568 (void)snprintf(remainingbuf, sizeof(remainingbuf), "(%02d:%02d:%02d until full)",
569 max(hours, 0), max(minutes, 0), max(seconds, 0));
572 empty_time = time(NULL);
573 empty_time += seconds_remaining;
574 empty_tm = localtime(&empty_time);
576 /* No need to show empty time if battery is charging */
577 if (status != CS_CHARGING) {
579 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d",
580 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
582 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
583 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
586 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
587 ((float)present_rate / 1000.0 / 1000.0));
590 #define EAT_SPACE_FROM_OUTPUT_IF_EMPTY(_buf) \
592 if (strlen(_buf) == 0) { \
593 if (outwalk > buffer && isspace(outwalk[-1])) \
595 else if (isspace(*(walk+1))) \
600 for (walk = format; *walk != '\0'; walk++) {
602 *(outwalk++) = *walk;
606 if (BEGINS_WITH(walk+1, "status")) {
607 outwalk += sprintf(outwalk, "%s", statusbuf);
608 walk += strlen("status");
609 } else if (BEGINS_WITH(walk+1, "percentage")) {
610 outwalk += sprintf(outwalk, "%s", percentagebuf);
611 walk += strlen("percentage");
612 } else if (BEGINS_WITH(walk+1, "remaining")) {
613 outwalk += sprintf(outwalk, "%s", remainingbuf);
614 walk += strlen("remaining");
615 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(remainingbuf);
616 } else if (BEGINS_WITH(walk+1, "emptytime")) {
617 outwalk += sprintf(outwalk, "%s", emptytimebuf);
618 walk += strlen("emptytime");
619 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(emptytimebuf);
620 } else if (BEGINS_WITH(walk+1, "consumption")) {
621 outwalk += sprintf(outwalk, "%s", consumptionbuf);
622 walk += strlen("consumption");
623 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf);
630 OUTPUT_FULL_TEXT(buffer);