]> git.sur5r.net Git - i3/i3status/blob - i3status.c
Support align and min_width module options
[i3/i3status] / i3status.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3status – Generates a status line for dzen2 or xmobar
5  *
6  * Copyright © 2008-2012 Michael Stapelberg and contributors
7  * Copyright © 2009 Thorsten Toepper <atsutane at freethoughts dot de>
8  * Copyright © 2010 Axel Wagner <mail at merovius dot de>
9  * Copyright © 2010 Fernando Tarlá Cardoso Lemos <fernandotcl at gmail dot com>
10  *
11  * See file LICENSE for license information.
12  *
13  */
14 #include <limits.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdbool.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <getopt.h>
23 #include <signal.h>
24 #include <confuse.h>
25 #include <glob.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <sys/time.h>
30 #include <locale.h>
31
32 #include <yajl/yajl_gen.h>
33 #include <yajl/yajl_version.h>
34
35 #include "i3status.h"
36
37 #define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }
38
39 #define CFG_CUSTOM_ALIGN_OPT \
40     CFG_STR_CB("align", NULL, CFGF_NONE, parse_align)
41
42 #define CFG_COLOR_OPTS(good, degraded, bad) \
43     CFG_STR("color_good", good, CFGF_NONE), \
44     CFG_STR("color_degraded", degraded, CFGF_NONE), \
45     CFG_STR("color_bad", bad, CFGF_NONE)
46
47 #define CFG_CUSTOM_COLOR_OPTS CFG_COLOR_OPTS(NULL, NULL, NULL)
48
49 #define CFG_CUSTOM_MIN_WIDTH_OPT \
50     CFG_PTR_CB("min_width", NULL, CFGF_NONE, parse_min_width, free)
51
52 /* socket file descriptor for general purposes */
53 int general_socket;
54
55 static bool exit_upon_signal = false;
56
57 cfg_t *cfg, *cfg_general, *cfg_section;
58
59 /*
60  * Set the exit_upon_signal flag, because one cannot do anything in a safe
61  * manner in a signal handler (e.g. fprintf, which we really want to do for
62  * debugging purposes), see
63  * https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers
64  *
65  */
66 void fatalsig(int signum) {
67         exit_upon_signal = true;
68 }
69
70 /*
71  * Do nothing upon SIGUSR1. Running this signal handler will nevertheless
72  * interrupt nanosleep() so that i3status immediately generates new output.
73  *
74  */
75 void sigusr1(int signum) {
76 }
77
78 /*
79  * Checks if the given path exists by calling stat().
80  *
81  */
82 static bool path_exists(const char *path) {
83         struct stat buf;
84         return (stat(path, &buf) == 0);
85 }
86
87 static void *scalloc(size_t size) {
88         void *result = calloc(size, 1);
89         exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
90         return result;
91 }
92
93 static char *sstrdup(const char *str) {
94         char *result = strdup(str);
95         exit_if_null(result, "Error: out of memory (strdup())\n");
96         return result;
97 }
98
99 /*
100  * Parses the "align" module option (to validate input).
101  */
102 static int parse_align(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
103         if (strcasecmp(value, "left") != 0 && strcasecmp(value,"right") != 0 && strcasecmp(value, "center") != 0)
104                 die("Invalid alignment attribute found in section %s, line %d: \"%s\"\n"
105                     "Valid attributes are: left, center, right\n", context->name, context->line, value);
106
107         char **cresult = result;
108         *cresult = sstrdup(value);
109
110         return 0;
111 }
112
113 /*
114  * Parses the "min_width" module option whose value can either be a string or an integer.
115  */
116 static int parse_min_width(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
117         char *end;
118         long num = strtol(value, &end, 10);
119
120         if (num < 0)
121                 die("Invalid min_width attribute found in section %s, line %d: %d\n"
122                     "Expected positive integer or string\n", context->name, context->line, num);
123         else if (num == LONG_MIN || num == LONG_MAX || (end && *end != '\0'))
124                 num = 0;
125
126         if (strlen(value) == 0)
127                 die("Empty min_width attribute found in section %s, line %d\n"
128                     "Expected positive integer or non-empty string\n", context->name, context->line);
129
130         if (strcmp(value, "0") == 0)
131                 die("Invalid min_width attribute found in section %s, line %d: \"%s\"\n"
132                     "Expected positive integer or string\n", context->name, context->line, value);
133
134         struct min_width *parsed = scalloc(sizeof(struct min_width));
135         parsed->num = num;
136
137         /* num is preferred, but if it’s 0 (i.e. not valid), store and use
138          * the raw string value */
139         if (num == 0)
140                 parsed->str = sstrdup(value);
141
142         struct min_width **cresult = result;
143         *cresult = parsed;
144
145         return 0;
146 }
147
148 /*
149  * Validates a color in "#RRGGBB" format
150  *
151  */
152 static int valid_color(const char *value)
153 {
154         if (strlen(value) != 7) return 0;
155         if (value[0] != '#') return 0;
156         for (int i = 1; i < 7; ++i) {
157                 if (value[i] >= '0' && value[i] <= '9') continue;
158                 if (value[i] >= 'a' && value[i] <= 'f') continue;
159                 if (value[i] >= 'A' && value[i] <= 'F') continue;
160                 return 0;
161         }
162         return 1;
163 }
164
165 /*
166  * This function resolves ~ in pathnames.
167  * It may resolve wildcards in the first part of the path, but if no match
168  * or multiple matches are found, it just returns a copy of path as given.
169  *
170  */
171 static char *resolve_tilde(const char *path) {
172         static glob_t globbuf;
173         char *head, *tail, *result = NULL;
174
175         tail = strchr(path, '/');
176         head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
177
178         int res = glob(head, GLOB_TILDE, NULL, &globbuf);
179         free(head);
180         /* no match, or many wildcard matches are bad */
181         if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
182                 result = sstrdup(path);
183         else if (res != 0) {
184                 die("glob() failed");
185         } else {
186                 head = globbuf.gl_pathv[0];
187                 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
188                 strncpy(result, head, strlen(head));
189                 if (tail)
190                         strncat(result, tail, strlen(tail));
191         }
192         globfree(&globbuf);
193
194         return result;
195 }
196
197 static char *get_config_path(void) {
198         char *xdg_config_home, *xdg_config_dirs, *config_path;
199
200         /* 1: check the traditional path under the home directory */
201         config_path = resolve_tilde("~/.i3status.conf");
202         if (path_exists(config_path))
203                 return config_path;
204
205         /* 2: check for $XDG_CONFIG_HOME/i3status/config */
206         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
207                 xdg_config_home = "~/.config";
208
209         xdg_config_home = resolve_tilde(xdg_config_home);
210         if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
211                 die("asprintf() failed");
212         free(xdg_config_home);
213
214         if (path_exists(config_path))
215                 return config_path;
216         free(config_path);
217
218         /* 3: check the traditional path under /etc */
219         config_path = SYSCONFDIR "/i3status.conf";
220         if (path_exists(config_path))
221             return sstrdup(config_path);
222
223         /* 4: check for $XDG_CONFIG_DIRS/i3status/config */
224         if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
225                 xdg_config_dirs = "/etc/xdg";
226
227         char *buf = strdup(xdg_config_dirs);
228         char *tok = strtok(buf, ":");
229         while (tok != NULL) {
230                 tok = resolve_tilde(tok);
231                 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
232                         die("asprintf() failed");
233                 free(tok);
234                 if (path_exists(config_path)) {
235                         free(buf);
236                         return config_path;
237                 }
238                 free(config_path);
239                 tok = strtok(NULL, ":");
240         }
241         free(buf);
242
243         die("Unable to find the configuration file (looked at "
244                 "~/.i3status.conf, $XDG_CONFIG_HOME/i3status/config, "
245                 "/etc/i3status.conf and $XDG_CONFIG_DIRS/i3status/config)");
246         return NULL;
247 }
248
249 /*
250  * Returns the default separator to use if no custom separator has been specified.
251  */
252 static char *get_default_separator() {
253         if (output_format == O_DZEN2)
254                 return "^p(5;-2)^ro(2)^p()^p(5)";
255         if (output_format == O_I3BAR)
256                 // anything besides the empty string indicates that the default separator should be used
257                 return "default";
258         return " | ";
259 }
260
261 int main(int argc, char *argv[]) {
262         unsigned int j;
263
264         cfg_opt_t general_opts[] = {
265                 CFG_STR("output_format", "auto", CFGF_NONE),
266                 CFG_BOOL("colors", 1, CFGF_NONE),
267                 CFG_STR("separator", "default", CFGF_NONE),
268                 CFG_STR("color_separator", "#333333", CFGF_NONE),
269                 CFG_INT("interval", 1, CFGF_NONE),
270                 CFG_COLOR_OPTS("#00FF00", "#FFFF00", "#FF0000"),
271                 CFG_END()
272         };
273
274         cfg_opt_t run_watch_opts[] = {
275                 CFG_STR("pidfile", NULL, CFGF_NONE),
276                 CFG_STR("format", "%title: %status", CFGF_NONE),
277                 CFG_CUSTOM_ALIGN_OPT,
278                 CFG_CUSTOM_COLOR_OPTS,
279                 CFG_CUSTOM_MIN_WIDTH_OPT,
280                 CFG_END()
281         };
282
283         cfg_opt_t path_exists_opts[] = {
284                 CFG_STR("path", NULL, CFGF_NONE),
285                 CFG_STR("format", "%title: %status", CFGF_NONE),
286                 CFG_CUSTOM_ALIGN_OPT,
287                 CFG_CUSTOM_COLOR_OPTS,
288                 CFG_CUSTOM_MIN_WIDTH_OPT,
289                 CFG_END()
290         };
291
292         cfg_opt_t wireless_opts[] = {
293                 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
294                 CFG_STR("format_down", "W: down", CFGF_NONE),
295                 CFG_CUSTOM_ALIGN_OPT,
296                 CFG_CUSTOM_COLOR_OPTS,
297                 CFG_CUSTOM_MIN_WIDTH_OPT,
298                 CFG_END()
299         };
300
301         cfg_opt_t ethernet_opts[] = {
302                 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
303                 CFG_STR("format_down", "E: down", CFGF_NONE),
304                 CFG_CUSTOM_ALIGN_OPT,
305                 CFG_CUSTOM_COLOR_OPTS,
306                 CFG_CUSTOM_MIN_WIDTH_OPT,
307                 CFG_END()
308         };
309
310         cfg_opt_t ipv6_opts[] = {
311                 CFG_STR("format_up", "%ip", CFGF_NONE),
312                 CFG_STR("format_down", "no IPv6", CFGF_NONE),
313                 CFG_CUSTOM_ALIGN_OPT,
314                 CFG_CUSTOM_COLOR_OPTS,
315                 CFG_CUSTOM_MIN_WIDTH_OPT,
316                 CFG_END()
317         };
318
319         cfg_opt_t battery_opts[] = {
320                 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
321                 CFG_STR("format_down", "No battery", CFGF_NONE),
322                 CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
323                 CFG_INT("low_threshold", 30, CFGF_NONE),
324                 CFG_STR("threshold_type", "time", CFGF_NONE),
325                 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
326                 CFG_BOOL("integer_battery_capacity", false, CFGF_NONE),
327                 CFG_BOOL("hide_seconds", false, CFGF_NONE),
328                 CFG_CUSTOM_ALIGN_OPT,
329                 CFG_CUSTOM_COLOR_OPTS,
330                 CFG_CUSTOM_MIN_WIDTH_OPT,
331                 CFG_END()
332         };
333
334         cfg_opt_t time_opts[] = {
335                 CFG_STR("format", "%Y-%m-%d %H:%M:%S", CFGF_NONE),
336                 CFG_CUSTOM_ALIGN_OPT,
337                 CFG_CUSTOM_MIN_WIDTH_OPT,
338                 CFG_END()
339         };
340
341         cfg_opt_t tztime_opts[] = {
342                 CFG_STR("format", "%Y-%m-%d %H:%M:%S %Z", CFGF_NONE),
343                 CFG_STR("timezone", "", CFGF_NONE),
344                 CFG_CUSTOM_ALIGN_OPT,
345                 CFG_CUSTOM_MIN_WIDTH_OPT,
346                 CFG_END()
347         };
348
349         cfg_opt_t ddate_opts[] = {
350                 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
351                 CFG_CUSTOM_ALIGN_OPT,
352                 CFG_CUSTOM_MIN_WIDTH_OPT,
353                 CFG_END()
354         };
355
356         cfg_opt_t load_opts[] = {
357                 CFG_STR("format", "%1min %5min %15min", CFGF_NONE),
358                 CFG_FLOAT("max_threshold", 5, CFGF_NONE),
359                 CFG_CUSTOM_ALIGN_OPT,
360                 CFG_CUSTOM_COLOR_OPTS,
361                 CFG_CUSTOM_MIN_WIDTH_OPT,
362                 CFG_END()
363         };
364
365         cfg_opt_t usage_opts[] = {
366                 CFG_STR("format", "%usage", CFGF_NONE),
367                 CFG_CUSTOM_ALIGN_OPT,
368                 CFG_CUSTOM_MIN_WIDTH_OPT,
369                 CFG_END()
370         };
371
372         cfg_opt_t temp_opts[] = {
373                 CFG_STR("format", "%degrees C", CFGF_NONE),
374                 CFG_STR("path", NULL, CFGF_NONE),
375                 CFG_INT("max_threshold", 75, CFGF_NONE),
376                 CFG_CUSTOM_ALIGN_OPT,
377                 CFG_CUSTOM_COLOR_OPTS,
378                 CFG_CUSTOM_MIN_WIDTH_OPT,
379                 CFG_END()
380         };
381
382         cfg_opt_t disk_opts[] = {
383                 CFG_STR("format", "%free", CFGF_NONE),
384                 CFG_STR("prefix_type", "binary", CFGF_NONE),
385                 CFG_CUSTOM_ALIGN_OPT,
386                 CFG_CUSTOM_MIN_WIDTH_OPT,
387                 CFG_END()
388         };
389
390         cfg_opt_t volume_opts[] = {
391                 CFG_STR("format", "♪: %volume", CFGF_NONE),
392                 CFG_STR("format_muted", "♪: 0%%", CFGF_NONE),
393                 CFG_STR("device", "default", CFGF_NONE),
394                 CFG_STR("mixer", "Master", CFGF_NONE),
395                 CFG_INT("mixer_idx", 0, CFGF_NONE),
396                 CFG_CUSTOM_ALIGN_OPT,
397                 CFG_CUSTOM_COLOR_OPTS,
398                 CFG_CUSTOM_MIN_WIDTH_OPT,
399                 CFG_END()
400         };
401
402         cfg_opt_t opts[] = {
403                 CFG_STR_LIST("order", "{}", CFGF_NONE),
404                 CFG_SEC("general", general_opts, CFGF_NONE),
405                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
406                 CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI),
407                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
408                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
409                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
410                 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
411                 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
412                 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
413                 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
414                 CFG_SEC("time", time_opts, CFGF_NONE),
415                 CFG_SEC("tztime", tztime_opts, CFGF_TITLE | CFGF_MULTI),
416                 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
417                 CFG_SEC("load", load_opts, CFGF_NONE),
418                 CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
419                 CFG_CUSTOM_COLOR_OPTS,
420                 CFG_END()
421         };
422
423         char *configfile = NULL;
424         int o, option_index = 0;
425         struct option long_options[] = {
426                 {"config", required_argument, 0, 'c'},
427                 {"help", no_argument, 0, 'h'},
428                 {"version", no_argument, 0, 'v'},
429                 {0, 0, 0, 0}
430         };
431
432         struct sigaction action;
433         memset(&action, 0, sizeof(struct sigaction));
434         action.sa_handler = fatalsig;
435
436         /* Exit upon SIGPIPE because when we have nowhere to write to, gathering system
437          * information is pointless. Also exit explicitly on SIGTERM and SIGINT because
438          * only this will trigger a reset of the cursor in the terminal output-format.
439          */
440         sigaction(SIGPIPE, &action, NULL);
441         sigaction(SIGTERM, &action, NULL);
442         sigaction(SIGINT, &action, NULL);
443
444         memset(&action, 0, sizeof(struct sigaction));
445         action.sa_handler = sigusr1;
446         sigaction(SIGUSR1, &action, NULL);
447
448         if (setlocale(LC_ALL, "") == NULL)
449                 die("Could not set locale. Please make sure all your LC_* / LANG settings are correct.");
450
451         while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
452                 if ((char)o == 'c')
453                         configfile = optarg;
454                 else if ((char)o == 'h') {
455                         printf("i3status " VERSION " © 2008-2012 Michael Stapelberg and contributors\n"
456                                 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
457                         return 0;
458                 } else if ((char)o == 'v') {
459                         printf("i3status " VERSION " © 2008-2012 Michael Stapelberg and contributors\n");
460                         return 0;
461                 }
462
463
464         if (configfile == NULL)
465                 configfile = get_config_path();
466
467         cfg = cfg_init(opts, CFGF_NOCASE);
468         if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
469                 return EXIT_FAILURE;
470
471         if (cfg_size(cfg, "order") == 0)
472                 die("Your 'order' array is empty. Please fix your config.\n");
473
474         cfg_general = cfg_getsec(cfg, "general");
475         if (cfg_general == NULL)
476                 die("Could not get section \"general\"\n");
477
478         char *output_str = cfg_getstr(cfg_general, "output_format");
479         if (strcasecmp(output_str, "auto") == 0) {
480                 fprintf(stderr, "i3status: trying to auto-detect output_format setting\n");
481                 output_str = auto_detect_format();
482                 if (!output_str) {
483                         output_str = "none";
484                         fprintf(stderr, "i3status: falling back to \"none\"\n");
485                 } else {
486                         fprintf(stderr, "i3status: auto-detected \"%s\"\n", output_str);
487                 }
488         }
489
490         if (strcasecmp(output_str, "dzen2") == 0)
491                 output_format = O_DZEN2;
492         else if (strcasecmp(output_str, "xmobar") == 0)
493                 output_format = O_XMOBAR;
494         else if (strcasecmp(output_str, "i3bar") == 0)
495                 output_format = O_I3BAR;
496         else if (strcasecmp(output_str, "term") == 0)
497                 output_format = O_TERM;
498         else if (strcasecmp(output_str, "none") == 0)
499                 output_format = O_NONE;
500         else die("Unknown output format: \"%s\"\n", output_str);
501
502         const char *separator = cfg_getstr(cfg_general, "separator");
503
504         // if no custom separator has been provided, use the default one
505         if (strcasecmp(separator, "default") == 0)
506                 separator = get_default_separator();
507
508         if (!valid_color(cfg_getstr(cfg_general, "color_good"))
509                         || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
510                         || !valid_color(cfg_getstr(cfg_general, "color_bad"))
511                         || !valid_color(cfg_getstr(cfg_general, "color_separator")))
512                die("Bad color format");
513
514 #if YAJL_MAJOR >= 2
515         yajl_gen json_gen = yajl_gen_alloc(NULL);
516 #else
517         yajl_gen json_gen = yajl_gen_alloc(NULL, NULL);
518 #endif
519
520         if (output_format == O_I3BAR) {
521                 /* Initialize the i3bar protocol. See i3/docs/i3bar-protocol
522                  * for details. */
523                 printf("{\"version\":1}\n[\n");
524                 fflush(stdout);
525                 yajl_gen_array_open(json_gen);
526                 yajl_gen_clear(json_gen);
527         }
528         if (output_format == O_TERM) {
529                 /* Save the cursor-position and hide the cursor */
530                 printf("\033[s\033[?25l");
531                 /* Undo at exit */
532                 atexit(&reset_cursor);
533         }
534
535         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
536                 die("Could not create socket\n");
537
538         int interval = cfg_getint(cfg_general, "interval");
539
540         /* One memory page which each plugin can use to buffer output.
541          * Even though it’s unclean, we just assume that the user will not
542          * specify a format string which expands to something longer than 4096
543          * bytes — given that the output of i3status is used to display
544          * information on screen, more than 1024 characters for the full line
545          * (!), not individual plugins, seem very unlikely. */
546         char buffer[4096];
547
548         while (1) {
549                 if (exit_upon_signal) {
550                         fprintf(stderr, "Exiting due to signal.\n");
551                         exit(1);
552                 }
553                 struct timeval tv;
554                 gettimeofday(&tv, NULL);
555                 if (output_format == O_I3BAR)
556                         yajl_gen_array_open(json_gen);
557                 else if (output_format == O_TERM)
558                         /* Restore the cursor-position, clear line */
559                         printf("\033[u\033[K");
560                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
561                         if (j > 0)
562                                 print_separator(separator);
563
564                         const char *current = cfg_getnstr(cfg, "order", j);
565
566                         CASE_SEC("ipv6") {
567                                 SEC_OPEN_MAP("ipv6");
568                                 print_ipv6_info(json_gen, buffer, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
569                                 SEC_CLOSE_MAP;
570                         }
571
572                         CASE_SEC_TITLE("wireless") {
573                                 SEC_OPEN_MAP("wireless");
574                                 print_wireless_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
575                                 SEC_CLOSE_MAP;
576                         }
577
578                         CASE_SEC_TITLE("ethernet") {
579                                 SEC_OPEN_MAP("ethernet");
580                                 print_eth_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
581                                 SEC_CLOSE_MAP;
582                         }
583
584                         CASE_SEC_TITLE("battery") {
585                                 SEC_OPEN_MAP("battery");
586                                 print_battery_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"), cfg_getint(sec, "low_threshold"), cfg_getstr(sec, "threshold_type"), cfg_getbool(sec, "last_full_capacity"), cfg_getbool(sec, "integer_battery_capacity"), cfg_getbool(sec, "hide_seconds"));
587                                 SEC_CLOSE_MAP;
588                         }
589
590                         CASE_SEC_TITLE("run_watch") {
591                                 SEC_OPEN_MAP("run_watch");
592                                 print_run_watch(json_gen, buffer, title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
593                                 SEC_CLOSE_MAP;
594                         }
595
596                         CASE_SEC_TITLE("path_exists") {
597                                 SEC_OPEN_MAP("path_exists");
598                                 print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
599                                 SEC_CLOSE_MAP;
600                         }
601
602                         CASE_SEC_TITLE("disk") {
603                                 SEC_OPEN_MAP("disk_info");
604                                 print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "prefix_type"));
605                                 SEC_CLOSE_MAP;
606                         }
607
608                         CASE_SEC("load") {
609                                 SEC_OPEN_MAP("load");
610                                 print_load(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getfloat(sec, "max_threshold"));
611                                 SEC_CLOSE_MAP;
612                         }
613
614                         CASE_SEC("time") {
615                                 SEC_OPEN_MAP("time");
616                                 print_time(json_gen, buffer, cfg_getstr(sec, "format"), NULL, tv.tv_sec);
617                                 SEC_CLOSE_MAP;
618                         }
619
620                         CASE_SEC_TITLE("tztime") {
621                                 SEC_OPEN_MAP("tztime");
622                                 print_time(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "timezone"), tv.tv_sec);
623                                 SEC_CLOSE_MAP;
624                         }
625
626                         CASE_SEC("ddate") {
627                                 SEC_OPEN_MAP("ddate");
628                                 print_ddate(json_gen, buffer, cfg_getstr(sec, "format"), tv.tv_sec);
629                                 SEC_CLOSE_MAP;
630                         }
631
632                         CASE_SEC_TITLE("volume") {
633                                 SEC_OPEN_MAP("volume");
634                                 print_volume(json_gen, buffer, cfg_getstr(sec, "format"),
635                                              cfg_getstr(sec, "format_muted"),
636                                              cfg_getstr(sec, "device"),
637                                              cfg_getstr(sec, "mixer"),
638                                              cfg_getint(sec, "mixer_idx"));
639                                 SEC_CLOSE_MAP;
640                         }
641
642                         CASE_SEC_TITLE("cpu_temperature") {
643                                 SEC_OPEN_MAP("cpu_temperature");
644                                 print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "max_threshold"));
645                                 SEC_CLOSE_MAP;
646                         }
647
648                         CASE_SEC("cpu_usage") {
649                                 SEC_OPEN_MAP("cpu_usage");
650                                 print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"));
651                                 SEC_CLOSE_MAP;
652                         }
653                 }
654                 if (output_format == O_I3BAR) {
655                         yajl_gen_array_close(json_gen);
656                         const unsigned char *buf;
657 #if YAJL_MAJOR >= 2
658                         size_t len;
659 #else
660                         unsigned int len;
661 #endif
662                         yajl_gen_get_buf(json_gen, &buf, &len);
663                         write(STDOUT_FILENO, buf, len);
664                         yajl_gen_clear(json_gen);
665                 }
666
667                 printf("\n");
668                 fflush(stdout);
669
670                 /* To provide updates on every full second (as good as possible)
671                  * we don’t use sleep(interval) but we sleep until the next
672                  * second (with microsecond precision) plus (interval-1)
673                  * seconds. We also align to 60 seconds modulo interval such
674                  * that we start with :00 on every new minute. */
675                 struct timeval current_timeval;
676                 gettimeofday(&current_timeval, NULL);
677                 struct timespec ts = {interval - 1 - (current_timeval.tv_sec % interval), (10e5 - current_timeval.tv_usec) * 1000};
678                 nanosleep(&ts, NULL);
679         }
680 }