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