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