- for (walk = format; *walk != '\0'; walk++) {
- if (*walk != '%') {
- putchar(*walk);
- continue;
- }
-
- if (strncmp(walk+1, "status", strlen("status")) == 0) {
- printf("%s", statusbuf);
- walk += strlen("status");
- } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
- printf("%s", percentagebuf);
- walk += strlen("percentage");
- } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
- printf("%s", remainingbuf);
- walk += strlen("remaining");
- } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
- printf("%s", emptytimebuf);
- walk += strlen("emptytime");
- }
+ return true;
+}
+
+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_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) {
+ const char *walk;
+ char *outwalk = buffer;
+ struct battery_info batt_info = {
+ .full_design = -1,
+ .full_last = -1,
+ .remaining = -1,
+ .present_rate = -1,
+ .seconds_remaining = -1,
+ .percentage_remaining = -1,
+ .status = CS_UNKNOWN,
+ };
+ bool colorful_output = false;
+
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
+ /* These OSes report battery stats in whole percent. */
+ integer_battery_capacity = true;
+#endif
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
+ /* These OSes report battery time in minutes. */
+ hide_seconds = true;
+#endif
+
+ if (number < 0) {
+ if (!slurp_all_batteries(&batt_info, json_gen, buffer, path, format_down))
+ return;
+ } else {
+ if (!slurp_battery_info(&batt_info, json_gen, buffer, number, path, format_down))
+ return;
+ }
+
+ // *Choose* a measure of the 'full' battery. It is whichever is better of
+ // the battery's (hardware-given) design capacity (batt_info.full_design)
+ // and the battery's last known good charge (batt_info.full_last).
+ // We prefer the design capacity, but use the last capacity if we don't have it,
+ // or if we are asked to (last_full_capacity == true); but similarly we use
+ // the design capacity if we don't have the last capacity.
+ // If we don't have either then both full_design and full_last <= 0,
+ // which implies full <= 0, which bails out on the following line.
+ int full = batt_info.full_design;
+ if (full <= 0 || (last_full_capacity && batt_info.full_last > 0)) {
+ full = batt_info.full_last;
+ }
+ if (full <= 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) {
+ /* We have no physical measurements and no estimates. Nothing
+ * much we can report, then. */
+ OUTPUT_FULL_TEXT(format_down);
+ return;
+ }
+
+ if (batt_info.percentage_remaining < 0) {
+ batt_info.percentage_remaining = (((float)batt_info.remaining / (float)full) * 100);
+ /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
+ * charged, even though that’s plainly wrong. For people who chose to see
+ * the percentage calculated based on the last full capacity, we clamp the
+ * value to 100%, as that makes more sense.
+ * See http://bugs.debian.org/785398 */
+ if (last_full_capacity && batt_info.percentage_remaining > 100) {
+ batt_info.percentage_remaining = 100;
+ }
+ }
+
+ if (batt_info.seconds_remaining < 0 && batt_info.present_rate > 0 && batt_info.status != CS_FULL) {
+ if (batt_info.status == CS_CHARGING)
+ batt_info.seconds_remaining = 3600.0 * (full - batt_info.remaining) / batt_info.present_rate;
+ else if (batt_info.status == CS_DISCHARGING)
+ batt_info.seconds_remaining = 3600.0 * batt_info.remaining / batt_info.present_rate;
+ else
+ batt_info.seconds_remaining = 0;
+ }
+
+ if (batt_info.status == CS_DISCHARGING && low_threshold > 0) {
+ if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) {
+ START_COLOR("color_bad");
+ colorful_output = true;
+ } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) {
+ START_COLOR("color_bad");
+ colorful_output = true;
+ }
+ }
+
+#define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
+ do { \
+ if (outwalk == prevoutwalk) { \
+ if (outwalk > buffer && isspace((int)outwalk[-1])) \
+ outwalk--; \
+ else if (isspace((int)*(walk + 1))) \
+ walk++; \
+ } \
+ } while (0)
+
+ for (walk = format; *walk != '\0'; walk++) {
+ char *prevoutwalk = outwalk;
+
+ if (*walk != '%') {
+ *(outwalk++) = *walk;
+ continue;