2 * vim:ts=4:sw=4:expandtab
4 * i3status – Generates a status line for dzen2 or xmobar
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>
11 * See file LICENSE for license information.
20 #include <sys/socket.h>
21 #include <netinet/in.h>
27 #include <sys/types.h>
32 #include <yajl/yajl_gen.h>
33 #include <yajl/yajl_version.h>
37 #define exit_if_null(pointer, ...) \
39 if (pointer == NULL) \
43 #define CFG_CUSTOM_ALIGN_OPT \
44 CFG_STR_CB("align", NULL, CFGF_NONE, parse_align)
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)
51 #define CFG_CUSTOM_COLOR_OPTS CFG_COLOR_OPTS(NULL, NULL, NULL)
53 #define CFG_CUSTOM_MIN_WIDTH_OPT \
54 CFG_PTR_CB("min_width", NULL, CFGF_NONE, parse_min_width, free)
56 #define CFG_CUSTOM_SEPARATOR_OPT \
57 CFG_BOOL("separator", 0, CFGF_NODEFAULT)
59 #define CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT \
60 CFG_INT("separator_block_width", 0, CFGF_NODEFAULT)
62 /* socket file descriptor for general purposes */
65 static bool exit_upon_signal = false;
66 static bool run_once = false;
68 cfg_t *cfg, *cfg_general, *cfg_section;
72 pthread_t main_thread;
74 markup_format_t markup_format;
75 output_format_t output_format;
80 * Set the exit_upon_signal flag, because one cannot do anything in a safe
81 * manner in a signal handler (e.g. fprintf, which we really want to do for
82 * debugging purposes), see
83 * https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers
86 void fatalsig(int signum) {
87 exit_upon_signal = true;
91 * Do nothing upon SIGUSR1. Running this signal handler will nevertheless
92 * interrupt nanosleep() so that i3status immediately generates new output.
95 void sigusr1(int signum) {
99 * Checks if the given path exists by calling stat().
102 static bool path_exists(const char *path) {
104 return (stat(path, &buf) == 0);
107 static void *scalloc(size_t size) {
108 void *result = calloc(size, 1);
109 exit_if_null(result, "Error: out of memory (calloc(%zu))\n", size);
113 char *sstrdup(const char *str) {
117 char *result = strdup(str);
118 exit_if_null(result, "Error: out of memory (strdup())\n");
123 * Parses the "align" module option (to validate input).
125 static int parse_align(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
126 if (strcasecmp(value, "left") != 0 && strcasecmp(value, "right") != 0 && strcasecmp(value, "center") != 0)
127 die("Invalid alignment attribute found in section %s, line %d: \"%s\"\n"
128 "Valid attributes are: left, center, right\n",
129 context->name, context->line, value);
131 const char **cresult = result;
138 * Parses the "min_width" module option whose value can either be a string or an integer.
140 static int parse_min_width(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
142 long num = strtol(value, &end, 10);
145 die("Invalid min_width attribute found in section %s, line %d: %ld\n"
146 "Expected positive integer or string\n",
147 context->name, context->line, num);
148 else if (num == LONG_MIN || num == LONG_MAX || (end && *end != '\0'))
151 if (strlen(value) == 0)
152 die("Empty min_width attribute found in section %s, line %d\n"
153 "Expected positive integer or non-empty string\n",
154 context->name, context->line);
156 if (strcmp(value, "0") == 0)
157 die("Invalid min_width attribute found in section %s, line %d: \"%s\"\n"
158 "Expected positive integer or string\n",
159 context->name, context->line, value);
161 struct min_width *parsed = scalloc(sizeof(struct min_width));
164 /* num is preferred, but if it’s 0 (i.e. not valid), store and use
165 * the raw string value */
167 parsed->str = sstrdup(value);
169 struct min_width **cresult = result;
176 * Validates a color in "#RRGGBB" format
179 static int valid_color(const char *value) {
180 const int len = strlen(value);
182 if (output_format == O_LEMONBAR) {
183 /* lemonbar supports an optional alpha channel */
184 if (len != strlen("#rrggbb") && len != strlen("#aarrggbb")) {
188 if (len != strlen("#rrggbb")) {
194 for (int i = 1; i < len; ++i) {
195 if (value[i] >= '0' && value[i] <= '9')
197 if (value[i] >= 'a' && value[i] <= 'f')
199 if (value[i] >= 'A' && value[i] <= 'F')
207 * This function resolves ~ in pathnames.
208 * It may resolve wildcards in the first part of the path, but if no match
209 * or multiple matches are found, it just returns a copy of path as given.
212 static char *resolve_tilde(const char *path) {
213 static glob_t globbuf;
214 char *head, *tail, *result = NULL;
216 tail = strchr(path, '/');
217 head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
219 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
221 /* no match, or many wildcard matches are bad */
222 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
223 result = sstrdup(path);
225 die("glob() failed");
227 head = globbuf.gl_pathv[0];
228 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
229 strncpy(result, head, strlen(head));
231 strncat(result, tail, strlen(tail));
238 static char *get_config_path(void) {
239 char *xdg_config_home, *xdg_config_dirs, *config_path;
241 /* 1: check for $XDG_CONFIG_HOME/i3status/config */
242 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
243 xdg_config_home = "~/.config";
245 xdg_config_home = resolve_tilde(xdg_config_home);
246 if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
247 die("asprintf() failed");
248 free(xdg_config_home);
250 if (path_exists(config_path))
254 /* 2: check for $XDG_CONFIG_DIRS/i3status/config */
255 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
256 xdg_config_dirs = "/etc/xdg";
258 /* 3: check the traditional path under the home directory */
259 config_path = resolve_tilde("~/.i3status.conf");
260 if (path_exists(config_path))
262 char *buf = strdup(xdg_config_dirs);
263 char *tok = strtok(buf, ":");
264 while (tok != NULL) {
265 tok = resolve_tilde(tok);
266 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
267 die("asprintf() failed");
269 if (path_exists(config_path)) {
274 tok = strtok(NULL, ":");
278 /* 4: check the traditional path under /etc */
279 config_path = SYSCONFDIR "/i3status.conf";
280 if (path_exists(config_path))
281 return sstrdup(config_path);
283 die("Unable to find the configuration file (looked at "
284 "~/.i3status.conf, $XDG_CONFIG_HOME/i3status/config, "
285 "/etc/i3status.conf and $XDG_CONFIG_DIRS/i3status/config)");
290 * Returns the default separator to use if no custom separator has been specified.
292 static char *get_default_separator() {
293 if (output_format == O_DZEN2)
294 return "^p(5;-2)^ro(2)^p()^p(5)";
295 if (output_format == O_I3BAR)
296 // anything besides the empty string indicates that the default separator should be used
301 int main(int argc, char *argv[]) {
304 cfg_opt_t general_opts[] = {
305 CFG_STR("output_format", "auto", CFGF_NONE),
306 CFG_BOOL("colors", 1, CFGF_NONE),
307 CFG_STR("separator", "default", CFGF_NONE),
308 CFG_STR("color_separator", "#333333", CFGF_NONE),
309 CFG_INT("interval", 1, CFGF_NONE),
310 CFG_COLOR_OPTS("#00FF00", "#FFFF00", "#FF0000"),
311 CFG_STR("markup", "none", CFGF_NONE),
314 cfg_opt_t run_watch_opts[] = {
315 CFG_STR("pidfile", NULL, CFGF_NONE),
316 CFG_STR("format", "%title: %status", CFGF_NONE),
317 CFG_STR("format_down", NULL, CFGF_NONE),
318 CFG_CUSTOM_ALIGN_OPT,
319 CFG_CUSTOM_COLOR_OPTS,
320 CFG_CUSTOM_MIN_WIDTH_OPT,
321 CFG_CUSTOM_SEPARATOR_OPT,
322 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
325 cfg_opt_t path_exists_opts[] = {
326 CFG_STR("path", NULL, CFGF_NONE),
327 CFG_STR("format", "%title: %status", CFGF_NONE),
328 CFG_STR("format_down", NULL, CFGF_NONE),
329 CFG_CUSTOM_ALIGN_OPT,
330 CFG_CUSTOM_COLOR_OPTS,
331 CFG_CUSTOM_MIN_WIDTH_OPT,
332 CFG_CUSTOM_SEPARATOR_OPT,
333 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
336 cfg_opt_t wireless_opts[] = {
337 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
338 CFG_STR("format_down", "W: down", CFGF_NONE),
339 CFG_STR("format_quality", "%3d%s", CFGF_NONE),
340 CFG_CUSTOM_ALIGN_OPT,
341 CFG_CUSTOM_COLOR_OPTS,
342 CFG_CUSTOM_MIN_WIDTH_OPT,
343 CFG_CUSTOM_SEPARATOR_OPT,
344 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
347 cfg_opt_t ethernet_opts[] = {
348 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
349 CFG_STR("format_down", "E: down", CFGF_NONE),
350 CFG_CUSTOM_ALIGN_OPT,
351 CFG_CUSTOM_COLOR_OPTS,
352 CFG_CUSTOM_MIN_WIDTH_OPT,
353 CFG_CUSTOM_SEPARATOR_OPT,
354 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
357 cfg_opt_t ipv6_opts[] = {
358 CFG_STR("format_up", "%ip", CFGF_NONE),
359 CFG_STR("format_down", "no IPv6", CFGF_NONE),
360 CFG_CUSTOM_ALIGN_OPT,
361 CFG_CUSTOM_COLOR_OPTS,
362 CFG_CUSTOM_MIN_WIDTH_OPT,
363 CFG_CUSTOM_SEPARATOR_OPT,
364 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
367 cfg_opt_t battery_opts[] = {
368 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
369 CFG_STR("format_down", "No battery", CFGF_NONE),
370 CFG_STR("status_chr", "CHR", CFGF_NONE),
371 CFG_STR("status_bat", "BAT", CFGF_NONE),
372 CFG_STR("status_unk", "UNK", CFGF_NONE),
373 CFG_STR("status_full", "FULL", CFGF_NONE),
374 CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
375 CFG_INT("low_threshold", 30, CFGF_NONE),
376 CFG_STR("threshold_type", "time", CFGF_NONE),
377 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
378 CFG_BOOL("integer_battery_capacity", false, CFGF_NONE),
379 CFG_BOOL("hide_seconds", true, CFGF_NONE),
380 CFG_CUSTOM_ALIGN_OPT,
381 CFG_CUSTOM_COLOR_OPTS,
382 CFG_CUSTOM_MIN_WIDTH_OPT,
383 CFG_CUSTOM_SEPARATOR_OPT,
384 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
387 cfg_opt_t time_opts[] = {
388 CFG_STR("format", "%Y-%m-%d %H:%M:%S", CFGF_NONE),
389 CFG_CUSTOM_ALIGN_OPT,
390 CFG_CUSTOM_MIN_WIDTH_OPT,
391 CFG_CUSTOM_SEPARATOR_OPT,
392 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
395 cfg_opt_t tztime_opts[] = {
396 CFG_STR("format", "%Y-%m-%d %H:%M:%S %Z", CFGF_NONE),
397 CFG_STR("timezone", "", CFGF_NONE),
398 CFG_STR("locale", "", CFGF_NONE),
399 CFG_STR("format_time", NULL, CFGF_NONE),
400 CFG_CUSTOM_ALIGN_OPT,
401 CFG_CUSTOM_MIN_WIDTH_OPT,
402 CFG_CUSTOM_SEPARATOR_OPT,
403 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
406 cfg_opt_t ddate_opts[] = {
407 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
408 CFG_CUSTOM_ALIGN_OPT,
409 CFG_CUSTOM_MIN_WIDTH_OPT,
410 CFG_CUSTOM_SEPARATOR_OPT,
411 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
414 cfg_opt_t load_opts[] = {
415 CFG_STR("format", "%1min %5min %15min", CFGF_NONE),
416 CFG_STR("format_above_threshold", NULL, CFGF_NONE),
417 CFG_FLOAT("max_threshold", 5, 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,
425 cfg_opt_t memory_opts[] = {
426 CFG_STR("format", "%used %free %available", CFGF_NONE),
427 CFG_STR("format_degraded", NULL, CFGF_NONE),
428 CFG_STR("threshold_degraded", NULL, CFGF_NONE),
429 CFG_STR("threshold_critical", NULL, CFGF_NONE),
430 CFG_STR("memory_used_method", "classical", CFGF_NONE),
431 CFG_CUSTOM_ALIGN_OPT,
432 CFG_CUSTOM_COLOR_OPTS,
433 CFG_CUSTOM_MIN_WIDTH_OPT,
434 CFG_CUSTOM_SEPARATOR_OPT,
435 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
438 cfg_opt_t usage_opts[] = {
439 CFG_STR("format", "%usage", CFGF_NONE),
440 CFG_STR("format_above_threshold", NULL, CFGF_NONE),
441 CFG_STR("format_above_degraded_threshold", NULL, CFGF_NONE),
442 CFG_STR("path", "/proc/stat", CFGF_NONE),
443 CFG_FLOAT("max_threshold", 95, CFGF_NONE),
444 CFG_FLOAT("degraded_threshold", 90, CFGF_NONE),
445 CFG_CUSTOM_ALIGN_OPT,
446 CFG_CUSTOM_COLOR_OPTS,
447 CFG_CUSTOM_MIN_WIDTH_OPT,
448 CFG_CUSTOM_SEPARATOR_OPT,
449 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
452 cfg_opt_t temp_opts[] = {
453 CFG_STR("format", "%degrees C", CFGF_NONE),
454 CFG_STR("format_above_threshold", NULL, CFGF_NONE),
455 CFG_STR("path", NULL, CFGF_NONE),
456 CFG_INT("max_threshold", 75, CFGF_NONE),
457 CFG_CUSTOM_ALIGN_OPT,
458 CFG_CUSTOM_COLOR_OPTS,
459 CFG_CUSTOM_MIN_WIDTH_OPT,
460 CFG_CUSTOM_SEPARATOR_OPT,
461 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
464 cfg_opt_t disk_opts[] = {
465 CFG_STR("format", "%free", CFGF_NONE),
466 CFG_STR("format_below_threshold", NULL, CFGF_NONE),
467 CFG_STR("format_not_mounted", NULL, CFGF_NONE),
468 CFG_STR("prefix_type", "binary", CFGF_NONE),
469 CFG_STR("threshold_type", "percentage_avail", CFGF_NONE),
470 CFG_FLOAT("low_threshold", 0, CFGF_NONE),
471 CFG_CUSTOM_ALIGN_OPT,
472 CFG_CUSTOM_COLOR_OPTS,
473 CFG_CUSTOM_MIN_WIDTH_OPT,
474 CFG_CUSTOM_SEPARATOR_OPT,
475 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
478 cfg_opt_t volume_opts[] = {
479 CFG_STR("format", "♪: %volume", CFGF_NONE),
480 CFG_STR("format_muted", "♪: 0%%", CFGF_NONE),
481 CFG_STR("device", "default", CFGF_NONE),
482 CFG_STR("mixer", "Master", CFGF_NONE),
483 CFG_INT("mixer_idx", 0, CFGF_NONE),
484 CFG_CUSTOM_ALIGN_OPT,
485 CFG_CUSTOM_COLOR_OPTS,
486 CFG_CUSTOM_MIN_WIDTH_OPT,
487 CFG_CUSTOM_SEPARATOR_OPT,
488 CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
492 CFG_STR_LIST("order", "{}", CFGF_NONE),
493 CFG_SEC("general", general_opts, CFGF_NONE),
494 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
495 CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI),
496 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
497 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
498 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
499 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
500 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
501 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
502 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
503 CFG_SEC("time", time_opts, CFGF_NONE),
504 CFG_SEC("tztime", tztime_opts, CFGF_TITLE | CFGF_MULTI),
505 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
506 CFG_SEC("load", load_opts, CFGF_NONE),
507 CFG_SEC("memory", memory_opts, CFGF_NONE),
508 CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
511 char *configfile = NULL;
512 int opt, option_index = 0;
513 struct option long_options[] = {
514 {"config", required_argument, 0, 'c'},
515 {"help", no_argument, 0, 'h'},
516 {"version", no_argument, 0, 'v'},
517 {"run-once", no_argument, 0, 0},
520 struct sigaction action;
521 memset(&action, 0, sizeof(struct sigaction));
522 action.sa_handler = fatalsig;
523 main_thread = pthread_self();
525 /* Exit upon SIGPIPE because when we have nowhere to write to, gathering system
526 * information is pointless. Also exit explicitly on SIGTERM and SIGINT because
527 * only this will trigger a reset of the cursor in the terminal output-format.
529 sigaction(SIGPIPE, &action, NULL);
530 sigaction(SIGTERM, &action, NULL);
531 sigaction(SIGINT, &action, NULL);
533 memset(&action, 0, sizeof(struct sigaction));
534 action.sa_handler = sigusr1;
535 sigaction(SIGUSR1, &action, NULL);
537 if (setlocale(LC_ALL, "") == NULL)
538 die("Could not set locale. Please make sure all your LC_* / LANG settings are correct.");
540 while ((opt = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1) {
546 printf("i3status " VERSION " © 2008 Michael Stapelberg and contributors\n"
547 "Syntax: %s [-c <configfile>] [-h] [-v]\n",
552 printf("i3status " VERSION " © 2008 Michael Stapelberg and contributors\n");
556 if (strcmp(long_options[option_index].name, "run-once") == 0) {
563 if (configfile == NULL)
564 configfile = get_config_path();
566 cfg = cfg_init(opts, CFGF_NOCASE);
567 if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
570 if (cfg_size(cfg, "order") == 0)
571 die("Your 'order' array is empty. Please fix your config.\n");
573 cfg_general = cfg_getsec(cfg, "general");
574 if (cfg_general == NULL)
575 die("Could not get section \"general\"\n");
577 char *output_str = cfg_getstr(cfg_general, "output_format");
578 if (strcasecmp(output_str, "auto") == 0) {
579 fprintf(stderr, "i3status: trying to auto-detect output_format setting\n");
580 output_str = auto_detect_format();
583 fprintf(stderr, "i3status: falling back to \"none\"\n");
585 fprintf(stderr, "i3status: auto-detected \"%s\"\n", output_str);
589 if (strcasecmp(output_str, "dzen2") == 0)
590 output_format = O_DZEN2;
591 else if (strcasecmp(output_str, "xmobar") == 0)
592 output_format = O_XMOBAR;
593 else if (strcasecmp(output_str, "i3bar") == 0)
594 output_format = O_I3BAR;
595 else if (strcasecmp(output_str, "lemonbar") == 0)
596 output_format = O_LEMONBAR;
597 else if (strcasecmp(output_str, "term") == 0)
598 output_format = O_TERM;
599 else if (strcasecmp(output_str, "none") == 0)
600 output_format = O_NONE;
602 die("Unknown output format: \"%s\"\n", output_str);
604 const char *separator = cfg_getstr(cfg_general, "separator");
606 /* lemonbar needs % to be escaped with another % */
607 pct_mark = (output_format == O_LEMONBAR) ? "%%" : "%";
609 // if no custom separator has been provided, use the default one
610 if (strcasecmp(separator, "default") == 0)
611 separator = get_default_separator();
613 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")))
614 die("Bad color format");
616 char *markup_str = cfg_getstr(cfg_general, "markup");
617 if (strcasecmp(markup_str, "pango") == 0)
618 markup_format = M_PANGO;
619 else if (strcasecmp(markup_str, "none") == 0)
620 markup_format = M_NONE;
622 die("Unknown markup format: \"%s\"\n", markup_str);
625 yajl_gen json_gen = yajl_gen_alloc(NULL);
627 yajl_gen json_gen = yajl_gen_alloc(NULL, NULL);
630 if (output_format == O_I3BAR) {
631 /* Initialize the i3bar protocol. See i3/docs/i3bar-protocol
633 printf("{\"version\":1}\n[\n");
635 yajl_gen_array_open(json_gen);
636 yajl_gen_clear(json_gen);
638 if (output_format == O_TERM) {
639 /* Save the cursor-position and hide the cursor */
640 printf("\033[s\033[?25l");
642 atexit(&reset_cursor);
645 if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
646 die("Could not create socket\n");
648 int interval = cfg_getint(cfg_general, "interval");
650 die("Invalid interval attribute found in section %s, line %d: %d\n"
651 "Expected positive integer\n",
652 cfg_general->name, cfg_general->line, interval);
655 /* One memory page which each plugin can use to buffer output.
656 * Even though it’s unclean, we just assume that the user will not
657 * specify a format string which expands to something longer than 4096
658 * bytes — given that the output of i3status is used to display
659 * information on screen, more than 1024 characters for the full line
660 * (!), not individual plugins, seem very unlikely. */
663 void **per_instance = calloc(cfg_size(cfg, "order"), sizeof(*per_instance));
666 if (exit_upon_signal) {
667 fprintf(stderr, "Exiting due to signal.\n");
671 gettimeofday(&tv, NULL);
672 if (output_format == O_I3BAR)
673 yajl_gen_array_open(json_gen);
674 else if (output_format == O_TERM)
675 /* Restore the cursor-position, clear line */
676 printf("\033[u\033[K");
677 for (j = 0; j < cfg_size(cfg, "order"); j++) {
678 cur_instance = per_instance + j;
680 print_separator(separator);
682 const char *current = cfg_getnstr(cfg, "order", j);
685 SEC_OPEN_MAP("ipv6");
686 print_ipv6_info(json_gen, buffer, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
690 CASE_SEC_TITLE("wireless") {
691 SEC_OPEN_MAP("wireless");
692 const char *interface = NULL;
693 if (strcasecmp(title, "_first_") == 0)
694 interface = first_eth_interface(NET_TYPE_WIRELESS);
695 if (interface == NULL)
697 print_wireless_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"), cfg_getstr(sec, "format_quality"));
701 CASE_SEC_TITLE("ethernet") {
702 SEC_OPEN_MAP("ethernet");
703 const char *interface = NULL;
704 if (strcasecmp(title, "_first_") == 0)
705 interface = first_eth_interface(NET_TYPE_ETHERNET);
706 if (interface == NULL)
708 print_eth_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
712 CASE_SEC_TITLE("battery") {
713 SEC_OPEN_MAP("battery");
714 print_battery_info(json_gen, buffer, (strcasecmp(title, "all") == 0 ? -1 : 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"));
718 CASE_SEC_TITLE("run_watch") {
719 SEC_OPEN_MAP("run_watch");
720 print_run_watch(json_gen, buffer, title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"));
724 CASE_SEC_TITLE("path_exists") {
725 SEC_OPEN_MAP("path_exists");
726 print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"));
730 CASE_SEC_TITLE("disk") {
731 SEC_OPEN_MAP("disk_info");
732 print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_below_threshold"), cfg_getstr(sec, "format_not_mounted"), cfg_getstr(sec, "prefix_type"), cfg_getstr(sec, "threshold_type"), cfg_getfloat(sec, "low_threshold"));
737 SEC_OPEN_MAP("load");
738 print_load(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_above_threshold"), cfg_getfloat(sec, "max_threshold"));
743 SEC_OPEN_MAP("memory");
744 print_memory(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_degraded"), cfg_getstr(sec, "threshold_degraded"), cfg_getstr(sec, "threshold_critical"), cfg_getstr(sec, "memory_used_method"));
749 SEC_OPEN_MAP("time");
750 print_time(json_gen, buffer, NULL, cfg_getstr(sec, "format"), NULL, NULL, NULL, tv.tv_sec);
754 CASE_SEC_TITLE("tztime") {
755 SEC_OPEN_MAP("tztime");
756 print_time(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "timezone"), cfg_getstr(sec, "locale"), cfg_getstr(sec, "format_time"), tv.tv_sec);
761 SEC_OPEN_MAP("ddate");
762 print_ddate(json_gen, buffer, cfg_getstr(sec, "format"), tv.tv_sec);
766 CASE_SEC_TITLE("volume") {
767 SEC_OPEN_MAP("volume");
768 print_volume(json_gen, buffer, cfg_getstr(sec, "format"),
769 cfg_getstr(sec, "format_muted"),
770 cfg_getstr(sec, "device"),
771 cfg_getstr(sec, "mixer"),
772 cfg_getint(sec, "mixer_idx"));
776 CASE_SEC_TITLE("cpu_temperature") {
777 SEC_OPEN_MAP("cpu_temperature");
778 print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_above_threshold"), cfg_getint(sec, "max_threshold"));
782 CASE_SEC("cpu_usage") {
783 SEC_OPEN_MAP("cpu_usage");
784 print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_above_threshold"), cfg_getstr(sec, "format_above_degraded_threshold"), cfg_getstr(sec, "path"), cfg_getfloat(sec, "max_threshold"), cfg_getfloat(sec, "degraded_threshold"));
788 if (output_format == O_I3BAR) {
789 yajl_gen_array_close(json_gen);
790 const unsigned char *buf;
796 yajl_gen_get_buf(json_gen, &buf, &len);
797 write(STDOUT_FILENO, buf, len);
798 yajl_gen_clear(json_gen);
808 /* To provide updates on every full second (as good as possible)
809 * we don’t use sleep(interval) but we sleep until the next
810 * second (with microsecond precision) plus (interval-1)
811 * seconds. We also align to 60 seconds modulo interval such
812 * that we start with :00 on every new minute. */
813 struct timeval current_timeval;
814 gettimeofday(¤t_timeval, NULL);
815 struct timespec ts = {interval - 1 - (current_timeval.tv_sec % interval), (10e5 - current_timeval.tv_usec) * 1000};
816 nanosleep(&ts, NULL);