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