+ }
+
+ if (strcasecmp(output_str, "dzen2") == 0)
+ output_format = O_DZEN2;
+ else if (strcasecmp(output_str, "xmobar") == 0)
+ output_format = O_XMOBAR;
+ else if (strcasecmp(output_str, "i3bar") == 0)
+ output_format = O_I3BAR;
+ else if (strcasecmp(output_str, "lemonbar") == 0)
+ output_format = O_LEMONBAR;
+ else if (strcasecmp(output_str, "term") == 0)
+ output_format = O_TERM;
+ else if (strcasecmp(output_str, "none") == 0)
+ output_format = O_NONE;
+ else
+ die("Unknown output format: \"%s\"\n", output_str);
+
+ const char *separator = cfg_getstr(cfg_general, "separator");
+
+ /* lemonbar needs % to be escaped with another % */
+ pct_mark = (output_format == O_LEMONBAR) ? "%%" : "%";
+
+ // if no custom separator has been provided, use the default one
+ if (strcasecmp(separator, "default") == 0)
+ separator = get_default_separator();
+
+ 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")))
+ die("Bad color format");
+
+ char *markup_str = cfg_getstr(cfg_general, "markup");
+ if (strcasecmp(markup_str, "pango") == 0)
+ markup_format = M_PANGO;
+ else if (strcasecmp(markup_str, "none") == 0)
+ markup_format = M_NONE;
+ else
+ die("Unknown markup format: \"%s\"\n", markup_str);
+
+#if YAJL_MAJOR >= 2
+ yajl_gen json_gen = yajl_gen_alloc(NULL);
+#else
+ yajl_gen json_gen = yajl_gen_alloc(NULL, NULL);
+#endif
+
+ if (output_format == O_I3BAR) {
+ /* Initialize the i3bar protocol. See i3/docs/i3bar-protocol
+ * for details. */
+ printf("{\"version\":1}\n[\n");
+ fflush(stdout);
+ yajl_gen_array_open(json_gen);
+ yajl_gen_clear(json_gen);
+ }
+ if (output_format == O_TERM) {
+ /* Save the cursor-position and hide the cursor */
+ printf("\033[s\033[?25l");
+ /* Undo at exit */
+ atexit(&reset_cursor);
+ }
+
+ if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+ die("Could not create socket\n");
+
+ int interval = cfg_getint(cfg_general, "interval");
+ if (interval <= 0) {
+ die("Invalid interval attribute found in section %s, line %d: %d\n"
+ "Expected positive integer\n",
+ cfg_general->name, cfg_general->line, interval);
+ }
+
+ /* One memory page which each plugin can use to buffer output.
+ * Even though it’s unclean, we just assume that the user will not
+ * specify a format string which expands to something longer than 4096
+ * bytes — given that the output of i3status is used to display
+ * information on screen, more than 1024 characters for the full line
+ * (!), not individual plugins, seem very unlikely. */
+ char buffer[4096];
+
+ void **per_instance = calloc(cfg_size(cfg, "order"), sizeof(*per_instance));
+
+ while (1) {
+ if (exit_upon_signal) {
+ fprintf(stderr, "i3status: exiting due to signal.\n");
+ exit(1);
+ }
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ if (output_format == O_I3BAR)
+ yajl_gen_array_open(json_gen);
+ else if (output_format == O_TERM)
+ /* Restore the cursor-position, clear line */
+ printf("\033[u\033[K");
+ for (j = 0; j < cfg_size(cfg, "order"); j++) {
+ cur_instance = per_instance + j;
+ if (j > 0)
+ print_separator(separator);
+
+ const char *current = cfg_getnstr(cfg, "order", j);
+
+ CASE_SEC("ipv6") {
+ SEC_OPEN_MAP("ipv6");
+ print_ipv6_info(json_gen, buffer, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("wireless") {
+ SEC_OPEN_MAP("wireless");
+ const char *interface = NULL;
+ if (strcasecmp(title, "_first_") == 0)
+ interface = first_eth_interface(NET_TYPE_WIRELESS);
+ if (interface == NULL)
+ interface = title;
+ print_wireless_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"), cfg_getstr(sec, "format_quality"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("ethernet") {
+ SEC_OPEN_MAP("ethernet");
+ const char *interface = NULL;
+ if (strcasecmp(title, "_first_") == 0)
+ interface = first_eth_interface(NET_TYPE_ETHERNET);
+ if (interface == NULL)
+ interface = title;
+ print_eth_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("battery") {
+ SEC_OPEN_MAP("battery");
+ 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"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("run_watch") {
+ SEC_OPEN_MAP("run_watch");
+ print_run_watch(json_gen, buffer, title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("path_exists") {
+ SEC_OPEN_MAP("path_exists");
+ print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("disk") {
+ SEC_OPEN_MAP("disk_info");
+ 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"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC("load") {
+ SEC_OPEN_MAP("load");
+ print_load(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_above_threshold"), cfg_getfloat(sec, "max_threshold"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC("memory") {
+ SEC_OPEN_MAP("memory");
+ 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"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC("time") {
+ SEC_OPEN_MAP("time");
+ print_time(json_gen, buffer, NULL, cfg_getstr(sec, "format"), NULL, NULL, NULL, false, tv.tv_sec);
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("tztime") {
+ SEC_OPEN_MAP("tztime");
+ print_time(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "timezone"), cfg_getstr(sec, "locale"), cfg_getstr(sec, "format_time"), cfg_getbool(sec, "hide_if_equals_localtime"), tv.tv_sec);
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC("ddate") {
+ SEC_OPEN_MAP("ddate");
+ print_ddate(json_gen, buffer, cfg_getstr(sec, "format"), tv.tv_sec);
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("volume") {
+ SEC_OPEN_MAP("volume");
+ print_volume(json_gen, buffer, cfg_getstr(sec, "format"),
+ cfg_getstr(sec, "format_muted"),
+ cfg_getstr(sec, "device"),
+ cfg_getstr(sec, "mixer"),
+ cfg_getint(sec, "mixer_idx"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC_TITLE("cpu_temperature") {
+ SEC_OPEN_MAP("cpu_temperature");
+ 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"));
+ SEC_CLOSE_MAP;
+ }
+
+ CASE_SEC("cpu_usage") {
+ SEC_OPEN_MAP("cpu_usage");
+ 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"));
+ SEC_CLOSE_MAP;
+ }
+ }
+ if (output_format == O_I3BAR) {
+ yajl_gen_array_close(json_gen);
+ const unsigned char *buf;
+#if YAJL_MAJOR >= 2
+ size_t len;
+#else
+ unsigned int len;
+#endif
+ yajl_gen_get_buf(json_gen, &buf, &len);
+ write(STDOUT_FILENO, buf, len);
+ yajl_gen_clear(json_gen);
+ }
+
+ printf("\n");
+ fflush(stdout);
+
+ if (run_once) {
+ break;
+ }
+
+ /* To provide updates on every full second (as good as possible)
+ * we don’t use sleep(interval) but we sleep until the next
+ * second (with microsecond precision) plus (interval-1)
+ * seconds. We also align to 60 seconds modulo interval such
+ * that we start with :00 on every new minute. */
+ struct timeval current_timeval;
+ gettimeofday(¤t_timeval, NULL);
+ struct timespec ts = {interval - 1 - (current_timeval.tv_sec % interval), (10e5 - current_timeval.tv_usec) * 1000};
+ nanosleep(&ts, NULL);
+ }