]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
Merge pull request #140 from tommie/multibatt
[i3/i3status] / src / print_battery_info.c
1 // vim:ts=4:sw=4:expandtab
2 #include <ctype.h>
3 #include <time.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
9
10 #include "i3status.h"
11
12 #if defined(LINUX)
13 #include <errno.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #endif
17
18 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
19 #include <sys/types.h>
20 #include <sys/sysctl.h>
21 #include <dev/acpica/acpiio.h>
22 #endif
23
24 #if defined(__OpenBSD__)
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/fcntl.h>
28 #include <machine/apmvar.h>
29 #endif
30
31 #if defined(__NetBSD__)
32 #include <fcntl.h>
33 #include <prop/proplib.h>
34 #include <sys/envsys.h>
35 #endif
36
37 typedef enum {
38     CS_UNKNOWN,
39     CS_DISCHARGING,
40     CS_CHARGING,
41     CS_FULL,
42 } charging_status_t;
43
44 /* A description of the state of one or more batteries. */
45 struct battery_info {
46     /* measured properties */
47     int full_design;  /* in uAh */
48     int full_last;    /* in uAh */
49     int remaining;    /* in uAh */
50     int present_rate; /* in uA, always non-negative */
51
52     /* derived properties */
53     int seconds_remaining;
54     float percentage_remaining;
55     charging_status_t status;
56 };
57
58 #if defined(LINUX) || defined(__NetBSD__)
59 /*
60  * Add batt_info data to acc.
61  */
62 static void add_battery_info(struct battery_info *acc, const struct battery_info *batt_info) {
63     if (acc->remaining < 0) {
64         /* initialize accumulator so we can add to it */
65         acc->full_design = 0;
66         acc->full_last = 0;
67         acc->remaining = 0;
68         acc->present_rate = 0;
69     }
70
71     acc->full_design += batt_info->full_design;
72     acc->full_last += batt_info->full_last;
73     acc->remaining += batt_info->remaining;
74
75     /* make present_rate negative for discharging and positive for charging */
76     int present_rate = (acc->status == CS_DISCHARGING ? -1 : 1) * acc->present_rate;
77     present_rate += (batt_info->status == CS_DISCHARGING ? -1 : 1) * batt_info->present_rate;
78
79     /* merge status */
80     switch (acc->status) {
81         case CS_UNKNOWN:
82             acc->status = batt_info->status;
83             break;
84
85         case CS_DISCHARGING:
86             if (present_rate > 0)
87                 acc->status = CS_CHARGING;
88             /* else if batt_info is DISCHARGING: no conflict
89              * else if batt_info is CHARGING: present_rate should indicate that
90              * else if batt_info is FULL: but something else is discharging */
91             break;
92
93         case CS_CHARGING:
94             if (present_rate < 0)
95                 acc->status = CS_DISCHARGING;
96             /* else if batt_info is DISCHARGING: present_rate should indicate that
97              * else if batt_info is CHARGING: no conflict
98              * else if batt_info is FULL: but something else is charging */
99             break;
100
101         case CS_FULL:
102             if (batt_info->status != CS_UNKNOWN)
103                 acc->status = batt_info->status;
104             /* else: retain FULL, since it is more specific than UNKNOWN */
105             break;
106     }
107
108     acc->present_rate = abs(present_rate);
109 }
110 #endif
111
112 static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, int number, const char *path, const char *format_down) {
113     char *outwalk = buffer;
114
115 #if defined(LINUX)
116     char buf[1024];
117     const char *walk, *last;
118     bool watt_as_unit = false;
119     int voltage = -1;
120     char batpath[512];
121     sprintf(batpath, path, number);
122     INSTANCE(batpath);
123
124     if (!slurp(batpath, buf, sizeof(buf))) {
125         OUTPUT_FULL_TEXT(format_down);
126         return false;
127     }
128
129     for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
130         if (*walk == '\n') {
131             last = walk + 1;
132             continue;
133         }
134
135         if (*walk != '=')
136             continue;
137
138         if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW=")) {
139             watt_as_unit = true;
140             batt_info->remaining = atoi(walk + 1);
141         } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW=")) {
142             watt_as_unit = false;
143             batt_info->remaining = atoi(walk + 1);
144         } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW="))
145             batt_info->present_rate = abs(atoi(walk + 1));
146         else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW="))
147             voltage = abs(atoi(walk + 1));
148         /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
149          * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
150          * unit instead of μAh. We will calculate it as we need it
151          * later. */
152         else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW="))
153             batt_info->present_rate = abs(atoi(walk + 1));
154         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
155             batt_info->status = CS_CHARGING;
156         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
157             batt_info->status = CS_FULL;
158         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
159             batt_info->status = CS_DISCHARGING;
160         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
161             batt_info->status = CS_UNKNOWN;
162         else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") ||
163                  BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN="))
164             batt_info->full_design = atoi(walk + 1);
165         else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL=") ||
166                  BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL="))
167             batt_info->full_last = atoi(walk + 1);
168     }
169
170     /* the difference between POWER_SUPPLY_ENERGY_NOW and
171      * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
172      * given in mWh, the charge in mAh. So calculate every value given in
173      * ampere to watt */
174     if (!watt_as_unit && voltage != -1) {
175         batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0));
176         batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0));
177         batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.0));
178         batt_info->full_last = (((float)voltage / 1000.0) * ((float)batt_info->full_last / 1000.0));
179     }
180 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
181     int state;
182     int sysctl_rslt;
183     size_t sysctl_size = sizeof(sysctl_rslt);
184
185     if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
186         OUTPUT_FULL_TEXT(format_down);
187         return false;
188     }
189
190     batt_info->percentage_remaining = sysctl_rslt;
191     if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
192         OUTPUT_FULL_TEXT(format_down);
193         return false;
194     }
195
196     batt_info->seconds_remaining = sysctl_rslt * 60;
197     if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
198         OUTPUT_FULL_TEXT(format_down);
199         return false;
200     }
201
202     state = sysctl_rslt;
203     if (state == 0 && batt_info->percentage_remaining == 100)
204         batt_info->status = CS_FULL;
205     else if ((state & ACPI_BATT_STAT_CHARGING) && batt_info->percentage_remaining < 100)
206         batt_info->status = CS_CHARGING;
207     else
208         batt_info->status = CS_DISCHARGING;
209 #elif defined(__OpenBSD__)
210     /*
211          * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
212          * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
213          * probing acpi(4) devices.
214          */
215     struct apm_power_info apm_info;
216     int apm_fd;
217
218     apm_fd = open("/dev/apm", O_RDONLY);
219     if (apm_fd < 0) {
220         OUTPUT_FULL_TEXT("can't open /dev/apm");
221         return false;
222     }
223     if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
224         OUTPUT_FULL_TEXT("can't read power info");
225
226     close(apm_fd);
227
228     /* Don't bother to go further if there's no battery present. */
229     if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
230         (apm_info.battery_state == APM_BATT_UNKNOWN)) {
231         OUTPUT_FULL_TEXT(format_down);
232         return false;
233     }
234
235     switch (apm_info.ac_state) {
236         case APM_AC_OFF:
237             batt_info->status = CS_DISCHARGING;
238             break;
239         case APM_AC_ON:
240             batt_info->status = CS_CHARGING;
241             break;
242         default:
243             /* If we don't know what's going on, just assume we're discharging. */
244             batt_info->status = CS_DISCHARGING;
245             break;
246     }
247
248     batt_info->percentage_remaining = apm_info.battery_life;
249
250     /* Can't give a meaningful value for remaining minutes if we're charging. */
251     if (batt_info->status != CS_CHARGING) {
252         batt_info->seconds_remaining = apm_info.minutes_left * 60;
253     }
254 #elif defined(__NetBSD__)
255     /*
256      * Using envsys(4) via sysmon(4).
257      */
258     int fd, rval;
259     bool is_found = false;
260     char sensor_desc[16];
261
262     prop_dictionary_t dict;
263     prop_array_t array;
264     prop_object_iterator_t iter;
265     prop_object_iterator_t iter2;
266     prop_object_t obj, obj2, obj3, obj4, obj5;
267
268     if (number >= 0)
269         (void)snprintf(sensor_desc, sizeof(sensor_desc), "acpibat%d", number);
270
271     fd = open("/dev/sysmon", O_RDONLY);
272     if (fd < 0) {
273         OUTPUT_FULL_TEXT("can't open /dev/sysmon");
274         return false;
275     }
276
277     rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
278     if (rval == -1) {
279         close(fd);
280         return false;
281     }
282
283     if (prop_dictionary_count(dict) == 0) {
284         prop_object_release(dict);
285         close(fd);
286         return false;
287     }
288
289     iter = prop_dictionary_iterator(dict);
290     if (iter == NULL) {
291         prop_object_release(dict);
292         close(fd);
293     }
294
295     /* iterate over the dictionary returned by the kernel */
296     while ((obj = prop_object_iterator_next(iter)) != NULL) {
297         /* skip this dict if it's not what we're looking for */
298         if (number < 0) {
299             /* we want all batteries */
300             if (!BEGINS_WITH(prop_dictionary_keysym_cstring_nocopy(obj),
301                              "acpibat"))
302                 continue;
303         } else {
304             /* we want a specific battery */
305             if (strcmp(sensor_desc,
306                        prop_dictionary_keysym_cstring_nocopy(obj)) != 0)
307                 continue;
308         }
309
310         is_found = true;
311
312         array = prop_dictionary_get_keysym(dict, obj);
313         if (prop_object_type(array) != PROP_TYPE_ARRAY) {
314             prop_object_iterator_release(iter);
315             prop_object_release(dict);
316             close(fd);
317             return false;
318         }
319
320         iter2 = prop_array_iterator(array);
321         if (!iter2) {
322             prop_object_iterator_release(iter);
323             prop_object_release(dict);
324             close(fd);
325             return false;
326         }
327
328         struct battery_info batt_buf = {
329             .full_design = 0,
330             .full_last = 0,
331             .remaining = 0,
332             .present_rate = 0,
333             .status = CS_UNKNOWN,
334         };
335         int voltage = -1;
336         bool watt_as_unit = false;
337
338         /* iterate over array of dicts specific to target battery */
339         while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
340             obj3 = prop_dictionary_get(obj2, "description");
341
342             if (obj3 == NULL)
343                 continue;
344
345             if (strcmp("charging", prop_string_cstring_nocopy(obj3)) == 0) {
346                 obj3 = prop_dictionary_get(obj2, "cur-value");
347
348                 if (prop_number_integer_value(obj3))
349                     batt_buf.status = CS_CHARGING;
350                 else
351                     batt_buf.status = CS_DISCHARGING;
352             } else if (strcmp("charge", prop_string_cstring_nocopy(obj3)) == 0) {
353                 obj3 = prop_dictionary_get(obj2, "cur-value");
354                 obj4 = prop_dictionary_get(obj2, "max-value");
355                 obj5 = prop_dictionary_get(obj2, "type");
356
357                 batt_buf.remaining = prop_number_integer_value(obj3);
358                 batt_buf.full_design = prop_number_integer_value(obj4);
359
360                 if (strcmp("Ampere hour", prop_string_cstring_nocopy(obj5)) == 0)
361                     watt_as_unit = false;
362                 else
363                     watt_as_unit = true;
364             } else if (strcmp("discharge rate", prop_string_cstring_nocopy(obj3)) == 0) {
365                 obj3 = prop_dictionary_get(obj2, "cur-value");
366                 batt_buf.present_rate = prop_number_integer_value(obj3);
367             } else if (strcmp("charge rate", prop_string_cstring_nocopy(obj3)) == 0) {
368                 obj3 = prop_dictionary_get(obj2, "cur-value");
369                 batt_info->present_rate = prop_number_integer_value(obj3);
370             } else if (strcmp("last full cap", prop_string_cstring_nocopy(obj3)) == 0) {
371                 obj3 = prop_dictionary_get(obj2, "cur-value");
372                 batt_buf.full_last = prop_number_integer_value(obj3);
373             } else if (strcmp("voltage", prop_string_cstring_nocopy(obj3)) == 0) {
374                 obj3 = prop_dictionary_get(obj2, "cur-value");
375                 voltage = prop_number_integer_value(obj3);
376             }
377         }
378         prop_object_iterator_release(iter2);
379
380         if (!watt_as_unit && voltage != -1) {
381             batt_buf.present_rate = (((float)voltage / 1000.0) * ((float)batt_buf.present_rate / 1000.0));
382             batt_buf.remaining = (((float)voltage / 1000.0) * ((float)batt_buf.remaining / 1000.0));
383             batt_buf.full_design = (((float)voltage / 1000.0) * ((float)batt_buf.full_design / 1000.0));
384             batt_buf.full_last = (((float)voltage / 1000.0) * ((float)batt_buf.full_last / 1000.0));
385         }
386
387         if (batt_buf.remaining == batt_buf.full_design)
388             batt_buf.status = CS_FULL;
389
390         add_battery_info(batt_info, &batt_buf);
391     }
392
393     prop_object_iterator_release(iter);
394     prop_object_release(dict);
395     close(fd);
396
397     if (!is_found) {
398         OUTPUT_FULL_TEXT(format_down);
399         return false;
400     }
401
402     batt_info->present_rate = abs(batt_info->present_rate);
403 #endif
404
405     return true;
406 }
407
408 /*
409  * Populate batt_info with aggregate information about all batteries.
410  * Returns false on error, and an error message will have been written.
411  */
412 static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, const char *path, const char *format_down) {
413 #if defined(LINUX)
414     char *outwalk = buffer;
415     bool is_found = false;
416
417     /* 1,000 batteries should be enough for anyone */
418     for (int i = 0; i < 1000; i++) {
419         char batpath[1024];
420         (void)snprintf(batpath, sizeof(batpath), path, i);
421
422         if (!strcmp(batpath, path)) {
423             OUTPUT_FULL_TEXT("no '%d' in battery path");
424             return false;
425         }
426
427         /* Probe to see if there is such a battery. */
428         struct stat sb;
429         if (stat(batpath, &sb) != 0) {
430             /* No such file, then we are done, assuming sysfs files have sequential numbers. */
431             if (errno == ENOENT)
432                 break;
433
434             OUTPUT_FULL_TEXT(format_down);
435             return false;
436         }
437
438         struct battery_info batt_buf = {
439             .full_design = 0,
440             .full_last = 0,
441             .remaining = 0,
442             .present_rate = 0,
443             .status = CS_UNKNOWN,
444         };
445         if (!slurp_battery_info(&batt_buf, json_gen, buffer, i, path, format_down))
446             return false;
447
448         is_found = true;
449         add_battery_info(batt_info, &batt_buf);
450     }
451
452     if (!is_found) {
453         OUTPUT_FULL_TEXT(format_down);
454         return false;
455     }
456
457     batt_info->present_rate = abs(batt_info->present_rate);
458 #else
459     /* FreeBSD and OpenBSD only report aggregates. NetBSD always
460      * iterates through all batteries, so it's more efficient to
461      * aggregate in slurp_battery_info. */
462     return slurp_battery_info(batt_info, json_gen, buffer, -1, path, format_down);
463 #endif
464
465     return true;
466 }
467
468 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) {
469     const char *walk;
470     char *outwalk = buffer;
471     struct battery_info batt_info = {
472         .full_design = -1,
473         .full_last = -1,
474         .remaining = -1,
475         .present_rate = -1,
476         .seconds_remaining = -1,
477         .percentage_remaining = -1,
478         .status = CS_UNKNOWN,
479     };
480     bool colorful_output = false;
481
482 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
483     /* These OSes report battery stats in whole percent. */
484     integer_battery_capacity = true;
485 #endif
486 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
487     /* These OSes report battery time in minutes. */
488     hide_seconds = true;
489 #endif
490
491     if (number < 0) {
492         if (!slurp_all_batteries(&batt_info, json_gen, buffer, path, format_down))
493             return;
494     } else {
495         if (!slurp_battery_info(&batt_info, json_gen, buffer, number, path, format_down))
496             return;
497     }
498
499     int full = (last_full_capacity ? batt_info.full_last : batt_info.full_design);
500     if (full < 0 && batt_info.percentage_remaining < 0) {
501         /* We have no physical measurements and no estimates. Nothing
502          * much we can report, then. */
503         OUTPUT_FULL_TEXT(format_down);
504         return;
505     }
506
507     if (batt_info.percentage_remaining < 0) {
508         batt_info.percentage_remaining = (((float)batt_info.remaining / (float)full) * 100);
509         /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
510          * charged, even though that’s plainly wrong. For people who chose to see
511          * the percentage calculated based on the last full capacity, we clamp the
512          * value to 100%, as that makes more sense.
513          * See http://bugs.debian.org/785398 */
514         if (last_full_capacity && batt_info.percentage_remaining > 100) {
515             batt_info.percentage_remaining = 100;
516         }
517     }
518
519     if (batt_info.seconds_remaining < 0 && batt_info.present_rate > 0 && batt_info.status != CS_FULL) {
520         if (batt_info.status == CS_CHARGING)
521             batt_info.seconds_remaining = 3600.0 * (full - batt_info.remaining) / batt_info.present_rate;
522         else if (batt_info.status == CS_DISCHARGING)
523             batt_info.seconds_remaining = 3600.0 * batt_info.remaining / batt_info.present_rate;
524         else
525             batt_info.seconds_remaining = 0;
526     }
527
528     if (batt_info.status == CS_DISCHARGING && low_threshold > 0) {
529         if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) {
530             START_COLOR("color_bad");
531             colorful_output = true;
532         } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) {
533             START_COLOR("color_bad");
534             colorful_output = true;
535         }
536     }
537
538 #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT()                   \
539     do {                                                       \
540         if (outwalk == prevoutwalk) {                          \
541             if (outwalk > buffer && isspace((int)outwalk[-1])) \
542                 outwalk--;                                     \
543             else if (isspace((int)*(walk + 1)))                \
544                 walk++;                                        \
545         }                                                      \
546     } while (0)
547
548     for (walk = format; *walk != '\0'; walk++) {
549         char *prevoutwalk = outwalk;
550
551         if (*walk != '%') {
552             *(outwalk++) = *walk;
553             continue;
554         }
555
556         if (BEGINS_WITH(walk + 1, "status")) {
557             const char *statusstr;
558             switch (batt_info.status) {
559                 case CS_CHARGING:
560                     statusstr = status_chr;
561                     break;
562                 case CS_DISCHARGING:
563                     statusstr = status_bat;
564                     break;
565                 case CS_FULL:
566                     statusstr = status_full;
567                     break;
568                 default:
569                     statusstr = status_unk;
570             }
571
572             outwalk += sprintf(outwalk, "%s", statusstr);
573             walk += strlen("status");
574         } else if (BEGINS_WITH(walk + 1, "percentage")) {
575             if (integer_battery_capacity) {
576                 outwalk += sprintf(outwalk, "%.00f%s", batt_info.percentage_remaining, pct_mark);
577             } else {
578                 outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark);
579             }
580             walk += strlen("percentage");
581         } else if (BEGINS_WITH(walk + 1, "remaining")) {
582             if (batt_info.seconds_remaining >= 0) {
583                 int seconds, hours, minutes;
584
585                 hours = batt_info.seconds_remaining / 3600;
586                 seconds = batt_info.seconds_remaining - (hours * 3600);
587                 minutes = seconds / 60;
588                 seconds -= (minutes * 60);
589
590                 if (hide_seconds)
591                     outwalk += sprintf(outwalk, "%02d:%02d",
592                                        max(hours, 0), max(minutes, 0));
593                 else
594                     outwalk += sprintf(outwalk, "%02d:%02d:%02d",
595                                        max(hours, 0), max(minutes, 0), max(seconds, 0));
596             }
597             walk += strlen("remaining");
598             EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
599         } else if (BEGINS_WITH(walk + 1, "emptytime")) {
600             if (batt_info.seconds_remaining >= 0) {
601                 time_t empty_time = time(NULL) + batt_info.seconds_remaining;
602                 struct tm *empty_tm = localtime(&empty_time);
603
604                 if (hide_seconds)
605                     outwalk += sprintf(outwalk, "%02d:%02d",
606                                        max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
607                 else
608                     outwalk += sprintf(outwalk, "%02d:%02d:%02d",
609                                        max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
610             }
611             walk += strlen("emptytime");
612             EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
613         } else if (BEGINS_WITH(walk + 1, "consumption")) {
614             if (batt_info.present_rate >= 0)
615                 outwalk += sprintf(outwalk, "%1.2fW", batt_info.present_rate / 1e6);
616
617             walk += strlen("consumption");
618             EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
619         }
620     }
621
622     if (colorful_output)
623         END_COLOR;
624
625     OUTPUT_FULL_TEXT(buffer);
626 }