1 // vim:ts=4:sw=4:expandtab
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>
31 * Get battery information from /sys. Note that it uses the design capacity to
32 * calculate the percentage, not the last full capacity, so you can see how
33 * worn off your battery is.
36 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) {
41 char percentagebuf[16];
42 char remainingbuf[256];
43 char emptytimebuf[256];
44 char consumptionbuf[256];
45 const char *walk, *last;
46 char *outwalk = buffer;
47 bool watt_as_unit = false;
48 bool colorful_output = false;
53 charging_status_t status = CS_DISCHARGING;
55 memset(statusbuf, '\0', sizeof(statusbuf));
56 memset(percentagebuf, '\0', sizeof(percentagebuf));
57 memset(remainingbuf, '\0', sizeof(remainingbuf));
58 memset(emptytimebuf, '\0', sizeof(emptytimebuf));
59 memset(consumptionbuf, '\0', sizeof(consumptionbuf));
61 static char batpath[512];
62 sprintf(batpath, path, number);
65 #define BATT_STATUS_NAME(status) \
66 (status == CS_CHARGING ? status_chr : (status == CS_DISCHARGING ? status_bat : status_full))
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);
86 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
88 remaining = atoi(walk + 1);
89 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
90 present_rate = abs(atoi(walk + 1));
91 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
92 voltage = abs(atoi(walk + 1));
93 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
94 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
95 * unit instead of μAh. We will calculate it as we need it
97 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
98 present_rate = abs(atoi(walk + 1));
99 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
100 status = CS_CHARGING;
101 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
104 /* The only thing left is the full capacity */
105 if (last_full_capacity) {
106 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
107 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
110 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
111 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
115 full_design = atoi(walk + 1);
119 /* the difference between POWER_SUPPLY_ENERGY_NOW and
120 * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
121 * given in mWh, the charge in mAh. So calculate every value given in
124 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
127 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
128 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
132 if ((full_design == -1) || (remaining == -1)) {
133 OUTPUT_FULL_TEXT(format_down);
137 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
139 float percentage_remaining = (((float)remaining / (float)full_design) * 100);
140 if (integer_battery_capacity) {
141 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00f%%", percentage_remaining);
143 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%", percentage_remaining);
146 if (present_rate > 0) {
147 float remaining_time;
148 int seconds, hours, minutes, seconds_remaining;
149 if (status == CS_CHARGING)
150 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
151 else if (status == CS_DISCHARGING)
152 remaining_time = ((float)remaining / (float)present_rate);
156 seconds_remaining = (int)(remaining_time * 3600.0);
158 hours = seconds_remaining / 3600;
159 seconds = seconds_remaining - (hours * 3600);
160 minutes = seconds / 60;
161 seconds -= (minutes * 60);
163 if (status == CS_DISCHARGING && low_threshold > 0) {
164 if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
165 START_COLOR("color_bad");
166 colorful_output = true;
167 } else if (strcasecmp(threshold_type, "time") == 0 && seconds_remaining < 60 * low_threshold) {
168 START_COLOR("color_bad");
169 colorful_output = true;
171 colorful_output = false;
176 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d",
177 max(hours, 0), max(minutes, 0));
179 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
180 max(hours, 0), max(minutes, 0), max(seconds, 0));
182 empty_time = time(NULL);
183 empty_time += seconds_remaining;
184 empty_tm = localtime(&empty_time);
187 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d",
188 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
190 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
191 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
193 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
194 ((float)present_rate / 1000.0 / 1000.0));
196 /* On some systems, present_rate may not exist. Still, make sure
197 * we colorize the output if threshold_type is set to percentage
198 * (since we don't have any information on remaining time). */
199 if (status == CS_DISCHARGING && low_threshold > 0) {
200 if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
201 START_COLOR("color_bad");
202 colorful_output = true;
206 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
209 size_t sysctl_size = sizeof(sysctl_rslt);
211 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
212 OUTPUT_FULL_TEXT(format_down);
216 present_rate = sysctl_rslt;
217 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
218 OUTPUT_FULL_TEXT(format_down);
222 remaining = sysctl_rslt;
223 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
224 OUTPUT_FULL_TEXT(format_down);
229 if (state == 0 && present_rate == 100)
231 else if (state == 0 && present_rate < 100)
232 status = CS_CHARGING;
234 status = CS_DISCHARGING;
236 full_design = sysctl_rslt;
238 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
240 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
246 hours = minutes / 60;
247 minutes -= (hours * 60);
248 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
249 max(hours, 0), max(minutes, 0));
250 if (strcasecmp(threshold_type, "percentage") == 0 && present_rate < low_threshold) {
251 START_COLOR("color_bad");
252 colorful_output = true;
253 } else if (strcasecmp(threshold_type, "time") == 0 && remaining < (u_int)low_threshold) {
254 START_COLOR("color_bad");
255 colorful_output = true;
258 #elif defined(__OpenBSD__)
260 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
261 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
262 * probing acpi(4) devices.
264 struct apm_power_info apm_info;
267 apm_fd = open("/dev/apm", O_RDONLY);
269 OUTPUT_FULL_TEXT("can't open /dev/apm");
272 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
273 OUTPUT_FULL_TEXT("can't read power info");
277 /* Don't bother to go further if there's no battery present. */
278 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
279 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
280 OUTPUT_FULL_TEXT(format_down);
284 switch (apm_info.ac_state) {
286 status = CS_DISCHARGING;
289 status = CS_CHARGING;
292 /* If we don't know what's going on, just assume we're discharging. */
293 status = CS_DISCHARGING;
297 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
298 /* integer_battery_capacity is implied as battery_life is already in whole numbers. */
299 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00d%%", apm_info.battery_life);
301 if (status == CS_DISCHARGING && low_threshold > 0) {
302 if (strcasecmp(threshold_type, "percentage") == 0 && apm_info.battery_life < low_threshold) {
303 START_COLOR("color_bad");
304 colorful_output = true;
305 } else if (strcasecmp(threshold_type, "time") == 0 && apm_info.minutes_left < (u_int)low_threshold) {
306 START_COLOR("color_bad");
307 colorful_output = true;
311 /* Can't give a meaningful value for remaining minutes if we're charging. */
312 if (status != CS_CHARGING) {
313 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%d", apm_info.minutes_left);
315 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%s", "(CHR)");
320 #elif defined(__NetBSD__)
322 * Using envsys(4) via sysmon(4).
324 int fd, rval, last_full_cap;
325 bool is_found = false;
327 bool is_full = false;
329 prop_dictionary_t dict;
331 prop_object_iterator_t iter;
332 prop_object_iterator_t iter2;
333 prop_object_t obj, obj2, obj3, obj4, obj5;
335 asprintf(&sensor_desc, "acpibat%d", number);
337 fd = open("/dev/sysmon", O_RDONLY);
339 OUTPUT_FULL_TEXT("can't open /dev/sysmon");
343 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
349 if (prop_dictionary_count(dict) == 0) {
350 prop_object_release(dict);
355 iter = prop_dictionary_iterator(dict);
357 prop_object_release(dict);
361 /* iterate over the dictionary returned by the kernel */
362 while ((obj = prop_object_iterator_next(iter)) != NULL) {
363 /* skip this dict if it's not what we're looking for */
364 if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) == strlen(sensor_desc)) &&
365 (strncmp(sensor_desc,
366 prop_dictionary_keysym_cstring_nocopy(obj),
367 strlen(sensor_desc)) != 0))
372 array = prop_dictionary_get_keysym(dict, obj);
373 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
374 prop_object_iterator_release(iter);
375 prop_object_release(dict);
380 iter2 = prop_array_iterator(array);
382 prop_object_iterator_release(iter);
383 prop_object_release(dict);
388 /* iterate over array of dicts specific to target battery */
389 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
390 obj3 = prop_dictionary_get(obj2, "description");
393 strlen(prop_string_cstring_nocopy(obj3)) == 8 &&
395 prop_string_cstring_nocopy(obj3),
397 obj3 = prop_dictionary_get(obj2, "cur-value");
399 if (prop_number_integer_value(obj3))
400 status = CS_CHARGING;
402 status = CS_DISCHARGING;
408 strlen(prop_string_cstring_nocopy(obj3)) == 6 &&
410 prop_string_cstring_nocopy(obj3),
412 obj3 = prop_dictionary_get(obj2, "cur-value");
413 obj4 = prop_dictionary_get(obj2, "max-value");
414 obj5 = prop_dictionary_get(obj2, "type");
416 remaining = prop_number_integer_value(obj3);
417 full_design = prop_number_integer_value(obj4);
419 if (remaining == full_design)
422 if (strncmp("Ampere hour",
423 prop_string_cstring_nocopy(obj5),
425 watt_as_unit = false;
433 strlen(prop_string_cstring_nocopy(obj3)) == 14 &&
434 strncmp("discharge rate",
435 prop_string_cstring_nocopy(obj3),
437 obj3 = prop_dictionary_get(obj2, "cur-value");
438 present_rate = prop_number_integer_value(obj3);
443 strlen(prop_string_cstring_nocopy(obj3)) == 13 &&
444 strncmp("last full cap",
445 prop_string_cstring_nocopy(obj3),
447 obj3 = prop_dictionary_get(obj2, "cur-value");
448 last_full_cap = prop_number_integer_value(obj3);
453 strlen(prop_string_cstring_nocopy(obj3)) == 7 &&
455 prop_string_cstring_nocopy(obj3),
457 obj3 = prop_dictionary_get(obj2, "cur-value");
458 voltage = prop_number_integer_value(obj3);
462 prop_object_iterator_release(iter2);
465 prop_object_iterator_release(iter);
466 prop_object_release(dict);
470 OUTPUT_FULL_TEXT(format_down);
474 if (last_full_capacity)
475 full_design = last_full_cap;
478 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
479 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
480 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
483 float percentage_remaining =
484 (((float)remaining / (float)full_design) * 100);
486 if (integer_battery_capacity)
487 (void)snprintf(percentagebuf,
488 sizeof(percentagebuf),
490 (int)percentage_remaining);
492 (void)snprintf(percentagebuf,
493 sizeof(percentagebuf),
495 percentage_remaining);
498 * Handle percentage low_threshold here, and time low_threshold when
501 if (status == CS_DISCHARGING && low_threshold > 0) {
502 if (strcasecmp(threshold_type, "percentage") == 0 && (((float)remaining / (float)full_design) * 100) < low_threshold) {
503 START_COLOR("color_bad");
504 colorful_output = true;
509 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(CS_FULL));
511 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
514 * The envsys(4) ACPI routines do not appear to provide a 'time
515 * remaining' figure, so we must deduce it.
517 float remaining_time;
518 int seconds, hours, minutes, seconds_remaining;
520 if (status == CS_CHARGING)
521 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
522 else if (status == CS_DISCHARGING)
523 remaining_time = ((float)remaining / (float)present_rate);
527 seconds_remaining = (int)(remaining_time * 3600.0);
529 hours = seconds_remaining / 3600;
530 seconds = seconds_remaining - (hours * 3600);
531 minutes = seconds / 60;
532 seconds -= (minutes * 60);
534 if (status != CS_CHARGING) {
536 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d",
537 max(hours, 0), max(minutes, 0));
539 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
540 max(hours, 0), max(minutes, 0), max(seconds, 0));
542 if (low_threshold > 0) {
543 if (strcasecmp(threshold_type, "time") == 0 && ((float)seconds_remaining / 60.0) < (u_int)low_threshold) {
544 START_COLOR("color_bad");
545 colorful_output = true;
550 (void)snprintf(remainingbuf, sizeof(remainingbuf), "(%02d:%02d until full)",
551 max(hours, 0), max(minutes, 0));
553 (void)snprintf(remainingbuf, sizeof(remainingbuf), "(%02d:%02d:%02d until full)",
554 max(hours, 0), max(minutes, 0), max(seconds, 0));
557 empty_time = time(NULL);
558 empty_time += seconds_remaining;
559 empty_tm = localtime(&empty_time);
561 /* No need to show empty time if battery is charging */
562 if (status != CS_CHARGING) {
564 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d",
565 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
567 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
568 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
571 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
572 ((float)present_rate / 1000.0 / 1000.0));
575 #define EAT_SPACE_FROM_OUTPUT_IF_EMPTY(_buf) \
577 if (strlen(_buf) == 0) { \
578 if (outwalk > buffer && isspace(outwalk[-1])) \
580 else if (isspace(*(walk + 1))) \
585 for (walk = format; *walk != '\0'; walk++) {
587 *(outwalk++) = *walk;
591 if (BEGINS_WITH(walk + 1, "status")) {
592 outwalk += sprintf(outwalk, "%s", statusbuf);
593 walk += strlen("status");
594 } else if (BEGINS_WITH(walk + 1, "percentage")) {
595 outwalk += sprintf(outwalk, "%s", percentagebuf);
596 walk += strlen("percentage");
597 } else if (BEGINS_WITH(walk + 1, "remaining")) {
598 outwalk += sprintf(outwalk, "%s", remainingbuf);
599 walk += strlen("remaining");
600 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(remainingbuf);
601 } else if (BEGINS_WITH(walk + 1, "emptytime")) {
602 outwalk += sprintf(outwalk, "%s", emptytimebuf);
603 walk += strlen("emptytime");
604 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(emptytimebuf);
605 } else if (BEGINS_WITH(walk + 1, "consumption")) {
606 outwalk += sprintf(outwalk, "%s", consumptionbuf);
607 walk += strlen("consumption");
608 EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf);
615 OUTPUT_FULL_TEXT(buffer);