]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
Add battery capacity parsing (#216)
[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 <glob.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             batt_info->percentage_remaining = -1;
142         } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW=")) {
143             watt_as_unit = false;
144             batt_info->remaining = atoi(walk + 1);
145             batt_info->percentage_remaining = -1;
146         } else if (BEGINS_WITH(last, "POWER_SUPPLY_CAPACITY=") && batt_info->remaining == -1) {
147             batt_info->percentage_remaining = atoi(walk + 1);
148         } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW="))
149             batt_info->present_rate = abs(atoi(walk + 1));
150         else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW="))
151             voltage = abs(atoi(walk + 1));
152         /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
153          * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
154          * unit instead of μAh. We will calculate it as we need it
155          * later. */
156         else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW="))
157             batt_info->present_rate = abs(atoi(walk + 1));
158         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
159             batt_info->status = CS_CHARGING;
160         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
161             batt_info->status = CS_FULL;
162         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
163             batt_info->status = CS_DISCHARGING;
164         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
165             batt_info->status = CS_UNKNOWN;
166         else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") ||
167                  BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN="))
168             batt_info->full_design = atoi(walk + 1);
169         else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL=") ||
170                  BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL="))
171             batt_info->full_last = atoi(walk + 1);
172     }
173
174     /* the difference between POWER_SUPPLY_ENERGY_NOW and
175      * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
176      * given in mWh, the charge in mAh. So calculate every value given in
177      * ampere to watt */
178     if (!watt_as_unit && voltage != -1) {
179         batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0));
180         batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0));
181         batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.0));
182         batt_info->full_last = (((float)voltage / 1000.0) * ((float)batt_info->full_last / 1000.0));
183     }
184 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
185     int state;
186     int sysctl_rslt;
187     size_t sysctl_size = sizeof(sysctl_rslt);
188
189     if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
190         OUTPUT_FULL_TEXT(format_down);
191         return false;
192     }
193
194     batt_info->percentage_remaining = sysctl_rslt;
195     if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
196         OUTPUT_FULL_TEXT(format_down);
197         return false;
198     }
199
200     batt_info->seconds_remaining = sysctl_rslt * 60;
201     if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
202         OUTPUT_FULL_TEXT(format_down);
203         return false;
204     }
205
206     state = sysctl_rslt;
207     if (state == 0 && batt_info->percentage_remaining == 100)
208         batt_info->status = CS_FULL;
209     else if ((state & ACPI_BATT_STAT_CHARGING) && batt_info->percentage_remaining < 100)
210         batt_info->status = CS_CHARGING;
211     else
212         batt_info->status = CS_DISCHARGING;
213 #elif defined(__OpenBSD__)
214     /*
215          * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
216          * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
217          * probing acpi(4) devices.
218          */
219     struct apm_power_info apm_info;
220     int apm_fd;
221
222     apm_fd = open("/dev/apm", O_RDONLY);
223     if (apm_fd < 0) {
224         OUTPUT_FULL_TEXT("can't open /dev/apm");
225         return false;
226     }
227     if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
228         OUTPUT_FULL_TEXT("can't read power info");
229
230     close(apm_fd);
231
232     /* Don't bother to go further if there's no battery present. */
233     if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
234         (apm_info.battery_state == APM_BATT_UNKNOWN)) {
235         OUTPUT_FULL_TEXT(format_down);
236         return false;
237     }
238
239     switch (apm_info.ac_state) {
240         case APM_AC_OFF:
241             batt_info->status = CS_DISCHARGING;
242             break;
243         case APM_AC_ON:
244             batt_info->status = CS_CHARGING;
245             break;
246         default:
247             /* If we don't know what's going on, just assume we're discharging. */
248             batt_info->status = CS_DISCHARGING;
249             break;
250     }
251
252     batt_info->percentage_remaining = apm_info.battery_life;
253
254     /* Can't give a meaningful value for remaining minutes if we're charging. */
255     if (batt_info->status != CS_CHARGING) {
256         batt_info->seconds_remaining = apm_info.minutes_left * 60;
257     }
258 #elif defined(__NetBSD__)
259     /*
260      * Using envsys(4) via sysmon(4).
261      */
262     int fd, rval;
263     bool is_found = false;
264     char sensor_desc[16];
265
266     prop_dictionary_t dict;
267     prop_array_t array;
268     prop_object_iterator_t iter;
269     prop_object_iterator_t iter2;
270     prop_object_t obj, obj2, obj3, obj4, obj5;
271
272     if (number >= 0)
273         (void)snprintf(sensor_desc, sizeof(sensor_desc), "acpibat%d", number);
274
275     fd = open("/dev/sysmon", O_RDONLY);
276     if (fd < 0) {
277         OUTPUT_FULL_TEXT("can't open /dev/sysmon");
278         return false;
279     }
280
281     rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
282     if (rval == -1) {
283         close(fd);
284         return false;
285     }
286
287     if (prop_dictionary_count(dict) == 0) {
288         prop_object_release(dict);
289         close(fd);
290         return false;
291     }
292
293     iter = prop_dictionary_iterator(dict);
294     if (iter == NULL) {
295         prop_object_release(dict);
296         close(fd);
297     }
298
299     /* iterate over the dictionary returned by the kernel */
300     while ((obj = prop_object_iterator_next(iter)) != NULL) {
301         /* skip this dict if it's not what we're looking for */
302         if (number < 0) {
303             /* we want all batteries */
304             if (!BEGINS_WITH(prop_dictionary_keysym_cstring_nocopy(obj),
305                              "acpibat"))
306                 continue;
307         } else {
308             /* we want a specific battery */
309             if (strcmp(sensor_desc,
310                        prop_dictionary_keysym_cstring_nocopy(obj)) != 0)
311                 continue;
312         }
313
314         is_found = true;
315
316         array = prop_dictionary_get_keysym(dict, obj);
317         if (prop_object_type(array) != PROP_TYPE_ARRAY) {
318             prop_object_iterator_release(iter);
319             prop_object_release(dict);
320             close(fd);
321             return false;
322         }
323
324         iter2 = prop_array_iterator(array);
325         if (!iter2) {
326             prop_object_iterator_release(iter);
327             prop_object_release(dict);
328             close(fd);
329             return false;
330         }
331
332         struct battery_info batt_buf = {
333             .full_design = 0,
334             .full_last = 0,
335             .remaining = 0,
336             .present_rate = 0,
337             .status = CS_UNKNOWN,
338         };
339         int voltage = -1;
340         bool watt_as_unit = false;
341
342         /* iterate over array of dicts specific to target battery */
343         while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
344             obj3 = prop_dictionary_get(obj2, "description");
345
346             if (obj3 == NULL)
347                 continue;
348
349             if (strcmp("charging", prop_string_cstring_nocopy(obj3)) == 0) {
350                 obj3 = prop_dictionary_get(obj2, "cur-value");
351
352                 if (prop_number_integer_value(obj3))
353                     batt_buf.status = CS_CHARGING;
354                 else
355                     batt_buf.status = CS_DISCHARGING;
356             } else if (strcmp("charge", prop_string_cstring_nocopy(obj3)) == 0) {
357                 obj3 = prop_dictionary_get(obj2, "cur-value");
358                 obj4 = prop_dictionary_get(obj2, "max-value");
359                 obj5 = prop_dictionary_get(obj2, "type");
360
361                 batt_buf.remaining = prop_number_integer_value(obj3);
362                 batt_buf.full_design = prop_number_integer_value(obj4);
363
364                 if (strcmp("Ampere hour", prop_string_cstring_nocopy(obj5)) == 0)
365                     watt_as_unit = false;
366                 else
367                     watt_as_unit = true;
368             } else if (strcmp("discharge rate", prop_string_cstring_nocopy(obj3)) == 0) {
369                 obj3 = prop_dictionary_get(obj2, "cur-value");
370                 batt_buf.present_rate = prop_number_integer_value(obj3);
371             } else if (strcmp("charge rate", prop_string_cstring_nocopy(obj3)) == 0) {
372                 obj3 = prop_dictionary_get(obj2, "cur-value");
373                 batt_info->present_rate = prop_number_integer_value(obj3);
374             } else if (strcmp("last full cap", prop_string_cstring_nocopy(obj3)) == 0) {
375                 obj3 = prop_dictionary_get(obj2, "cur-value");
376                 batt_buf.full_last = prop_number_integer_value(obj3);
377             } else if (strcmp("voltage", prop_string_cstring_nocopy(obj3)) == 0) {
378                 obj3 = prop_dictionary_get(obj2, "cur-value");
379                 voltage = prop_number_integer_value(obj3);
380             }
381         }
382         prop_object_iterator_release(iter2);
383
384         if (!watt_as_unit && voltage != -1) {
385             batt_buf.present_rate = (((float)voltage / 1000.0) * ((float)batt_buf.present_rate / 1000.0));
386             batt_buf.remaining = (((float)voltage / 1000.0) * ((float)batt_buf.remaining / 1000.0));
387             batt_buf.full_design = (((float)voltage / 1000.0) * ((float)batt_buf.full_design / 1000.0));
388             batt_buf.full_last = (((float)voltage / 1000.0) * ((float)batt_buf.full_last / 1000.0));
389         }
390
391         if (batt_buf.remaining == batt_buf.full_design)
392             batt_buf.status = CS_FULL;
393
394         add_battery_info(batt_info, &batt_buf);
395     }
396
397     prop_object_iterator_release(iter);
398     prop_object_release(dict);
399     close(fd);
400
401     if (!is_found) {
402         OUTPUT_FULL_TEXT(format_down);
403         return false;
404     }
405
406     batt_info->present_rate = abs(batt_info->present_rate);
407 #endif
408
409     return true;
410 }
411
412 /*
413  * Populate batt_info with aggregate information about all batteries.
414  * Returns false on error, and an error message will have been written.
415  */
416 static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, const char *path, const char *format_down) {
417 #if defined(LINUX)
418     char *outwalk = buffer;
419     bool is_found = false;
420
421     char *placeholder;
422     char *globpath = sstrdup(path);
423     if ((placeholder = strstr(path, "%d")) != NULL) {
424         char *globplaceholder = globpath + (placeholder - path);
425         *globplaceholder = '*';
426         strcpy(globplaceholder + 1, placeholder + 2);
427     }
428
429     if (!strcmp(globpath, path)) {
430         OUTPUT_FULL_TEXT("no '%d' in battery path");
431         return false;
432     }
433
434     glob_t globbuf;
435     if (glob(globpath, 0, NULL, &globbuf) == 0) {
436         for (size_t i = 0; i < globbuf.gl_pathc; i++) {
437             /* Probe to see if there is such a battery. */
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, globbuf.gl_pathv[i], format_down))
446                 return false;
447
448             is_found = true;
449             add_battery_info(batt_info, &batt_buf);
450         }
451     }
452     globfree(&globbuf);
453     free(globpath);
454
455     if (!is_found) {
456         OUTPUT_FULL_TEXT(format_down);
457         return false;
458     }
459
460     batt_info->present_rate = abs(batt_info->present_rate);
461 #else
462     /* FreeBSD and OpenBSD only report aggregates. NetBSD always
463      * iterates through all batteries, so it's more efficient to
464      * aggregate in slurp_battery_info. */
465     return slurp_battery_info(batt_info, json_gen, buffer, -1, path, format_down);
466 #endif
467
468     return true;
469 }
470
471 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) {
472     const char *walk;
473     char *outwalk = buffer;
474     struct battery_info batt_info = {
475         .full_design = -1,
476         .full_last = -1,
477         .remaining = -1,
478         .present_rate = -1,
479         .seconds_remaining = -1,
480         .percentage_remaining = -1,
481         .status = CS_UNKNOWN,
482     };
483     bool colorful_output = false;
484
485 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
486     /* These OSes report battery stats in whole percent. */
487     integer_battery_capacity = true;
488 #endif
489 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
490     /* These OSes report battery time in minutes. */
491     hide_seconds = true;
492 #endif
493
494     if (number < 0) {
495         if (!slurp_all_batteries(&batt_info, json_gen, buffer, path, format_down))
496             return;
497     } else {
498         if (!slurp_battery_info(&batt_info, json_gen, buffer, number, path, format_down))
499             return;
500     }
501
502     int full = (last_full_capacity ? batt_info.full_last : batt_info.full_design);
503     if (full < 0 && batt_info.percentage_remaining < 0) {
504         /* We have no physical measurements and no estimates. Nothing
505          * much we can report, then. */
506         OUTPUT_FULL_TEXT(format_down);
507         return;
508     }
509
510     if (batt_info.percentage_remaining < 0) {
511         batt_info.percentage_remaining = (((float)batt_info.remaining / (float)full) * 100);
512         /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
513          * charged, even though that’s plainly wrong. For people who chose to see
514          * the percentage calculated based on the last full capacity, we clamp the
515          * value to 100%, as that makes more sense.
516          * See http://bugs.debian.org/785398 */
517         if (last_full_capacity && batt_info.percentage_remaining > 100) {
518             batt_info.percentage_remaining = 100;
519         }
520     }
521
522     if (batt_info.seconds_remaining < 0 && batt_info.present_rate > 0 && batt_info.status != CS_FULL) {
523         if (batt_info.status == CS_CHARGING)
524             batt_info.seconds_remaining = 3600.0 * (full - batt_info.remaining) / batt_info.present_rate;
525         else if (batt_info.status == CS_DISCHARGING)
526             batt_info.seconds_remaining = 3600.0 * batt_info.remaining / batt_info.present_rate;
527         else
528             batt_info.seconds_remaining = 0;
529     }
530
531     if (batt_info.status == CS_DISCHARGING && low_threshold > 0) {
532         if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) {
533             START_COLOR("color_bad");
534             colorful_output = true;
535         } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) {
536             START_COLOR("color_bad");
537             colorful_output = true;
538         }
539     }
540
541 #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT()                   \
542     do {                                                       \
543         if (outwalk == prevoutwalk) {                          \
544             if (outwalk > buffer && isspace((int)outwalk[-1])) \
545                 outwalk--;                                     \
546             else if (isspace((int)*(walk + 1)))                \
547                 walk++;                                        \
548         }                                                      \
549     } while (0)
550
551     for (walk = format; *walk != '\0'; walk++) {
552         char *prevoutwalk = outwalk;
553
554         if (*walk != '%') {
555             *(outwalk++) = *walk;
556             continue;
557         }
558
559         if (BEGINS_WITH(walk + 1, "status")) {
560             const char *statusstr;
561             switch (batt_info.status) {
562                 case CS_CHARGING:
563                     statusstr = status_chr;
564                     break;
565                 case CS_DISCHARGING:
566                     statusstr = status_bat;
567                     break;
568                 case CS_FULL:
569                     statusstr = status_full;
570                     break;
571                 default:
572                     statusstr = status_unk;
573             }
574
575             outwalk += sprintf(outwalk, "%s", statusstr);
576             walk += strlen("status");
577         } else if (BEGINS_WITH(walk + 1, "percentage")) {
578             if (integer_battery_capacity) {
579                 outwalk += sprintf(outwalk, "%.00f%s", batt_info.percentage_remaining, pct_mark);
580             } else {
581                 outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark);
582             }
583             walk += strlen("percentage");
584         } else if (BEGINS_WITH(walk + 1, "remaining")) {
585             if (batt_info.seconds_remaining >= 0) {
586                 int seconds, hours, minutes;
587
588                 hours = batt_info.seconds_remaining / 3600;
589                 seconds = batt_info.seconds_remaining - (hours * 3600);
590                 minutes = seconds / 60;
591                 seconds -= (minutes * 60);
592
593                 if (hide_seconds)
594                     outwalk += sprintf(outwalk, "%02d:%02d",
595                                        max(hours, 0), max(minutes, 0));
596                 else
597                     outwalk += sprintf(outwalk, "%02d:%02d:%02d",
598                                        max(hours, 0), max(minutes, 0), max(seconds, 0));
599             }
600             walk += strlen("remaining");
601             EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
602         } else if (BEGINS_WITH(walk + 1, "emptytime")) {
603             if (batt_info.seconds_remaining >= 0) {
604                 time_t empty_time = time(NULL) + batt_info.seconds_remaining;
605                 struct tm *empty_tm = localtime(&empty_time);
606
607                 if (hide_seconds)
608                     outwalk += sprintf(outwalk, "%02d:%02d",
609                                        max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
610                 else
611                     outwalk += sprintf(outwalk, "%02d:%02d:%02d",
612                                        max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
613             }
614             walk += strlen("emptytime");
615             EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
616         } else if (BEGINS_WITH(walk + 1, "consumption")) {
617             if (batt_info.present_rate >= 0)
618                 outwalk += sprintf(outwalk, "%1.2fW", batt_info.present_rate / 1e6);
619
620             walk += strlen("consumption");
621             EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
622         }
623     }
624
625     if (colorful_output)
626         END_COLOR;
627
628     OUTPUT_FULL_TEXT(buffer);
629 }