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 /* socket file descriptor for general purposes */
59 static bool exit_upon_signal = false;
61 cfg_t *cfg, *cfg_general, *cfg_section;
65 pthread_cond_t i3status_sleep_cond = PTHREAD_COND_INITIALIZER;
66 pthread_mutex_t i3status_sleep_mutex = PTHREAD_MUTEX_INITIALIZER;
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
75 void fatalsig(int signum) {
76 exit_upon_signal = true;
80 * Do nothing upon SIGUSR1. Running this signal handler will nevertheless
81 * interrupt nanosleep() so that i3status immediately generates new output.
84 void sigusr1(int signum) {
88 * Checks if the given path exists by calling stat().
91 static bool path_exists(const char *path) {
93 return (stat(path, &buf) == 0);
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);
102 static char *sstrdup(const char *str) {
103 char *result = strdup(str);
104 exit_if_null(result, "Error: out of memory (strdup())\n");
109 * Parses the "align" module option (to validate input).
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);
117 const char **cresult = result;
124 * Parses the "min_width" module option whose value can either be a string or an integer.
126 static int parse_min_width(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
128 long num = strtol(value, &end, 10);
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'))
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);
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);
147 struct min_width *parsed = scalloc(sizeof(struct min_width));
150 /* num is preferred, but if it’s 0 (i.e. not valid), store and use
151 * the raw string value */
153 parsed->str = sstrdup(value);
155 struct min_width **cresult = result;
162 * Validates a color in "#RRGGBB" format
165 static int valid_color(const char *value) {
166 const int len = strlen(value);
168 if (output_format == O_LEMONBAR) {
169 /* lemonbar supports an optional alpha channel */
170 if (len != strlen("#rrggbb") && len != strlen("#aarrggbb")) {
174 if (len != strlen("#rrggbb")) {
180 for (int i = 1; i < len; ++i) {
181 if (value[i] >= '0' && value[i] <= '9')
183 if (value[i] >= 'a' && value[i] <= 'f')
185 if (value[i] >= 'A' && value[i] <= 'F')
193 * This function resolves ~ in pathnames.
194 * It may resolve wildcards in the first part of the path, but if no match
195 * or multiple matches are found, it just returns a copy of path as given.
198 static char *resolve_tilde(const char *path) {
199 static glob_t globbuf;
200 char *head, *tail, *result = NULL;
202 tail = strchr(path, '/');
203 head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
205 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
207 /* no match, or many wildcard matches are bad */
208 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
209 result = sstrdup(path);
211 die("glob() failed");
213 head = globbuf.gl_pathv[0];
214 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
215 strncpy(result, head, strlen(head));
217 strncat(result, tail, strlen(tail));
224 static char *get_config_path(void) {
225 char *xdg_config_home, *xdg_config_dirs, *config_path;
227 /* 1: check the traditional path under the home directory */
228 config_path = resolve_tilde("~/.i3status.conf");
229 if (path_exists(config_path))
232 /* 2: check for $XDG_CONFIG_HOME/i3status/config */
233 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
234 xdg_config_home = "~/.config";
236 xdg_config_home = resolve_tilde(xdg_config_home);
237 if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
238 die("asprintf() failed");
239 free(xdg_config_home);
241 if (path_exists(config_path))
245 /* 3: check the traditional path under /etc */
246 config_path = SYSCONFDIR "/i3status.conf";
247 if (path_exists(config_path))
248 return sstrdup(config_path);
250 /* 4: check for $XDG_CONFIG_DIRS/i3status/config */
251 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
252 xdg_config_dirs = "/etc/xdg";
254 char *buf = strdup(xdg_config_dirs);
255 char *tok = strtok(buf, ":");
256 while (tok != NULL) {
257 tok = resolve_tilde(tok);
258 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
259 die("asprintf() failed");
261 if (path_exists(config_path)) {
266 tok = strtok(NULL, ":");
270 die("Unable to find the configuration file (looked at "
271 "~/.i3status.conf, $XDG_CONFIG_HOME/i3status/config, "
272 "/etc/i3status.conf and $XDG_CONFIG_DIRS/i3status/config)");
277 * Returns the default separator to use if no custom separator has been specified.
279 static char *get_default_separator() {
280 if (output_format == O_DZEN2)
281 return "^p(5;-2)^ro(2)^p()^p(5)";
282 if (output_format == O_I3BAR)
283 // anything besides the empty string indicates that the default separator should be used
288 int main(int argc, char *argv[]) {
291 cfg_opt_t general_opts[] = {
292 CFG_STR("output_format", "auto", CFGF_NONE),
293 CFG_BOOL("colors", 1, CFGF_NONE),
294 CFG_STR("separator", "default", CFGF_NONE),
295 CFG_STR("color_separator", "#333333", CFGF_NONE),
296 CFG_INT("interval", 1, CFGF_NONE),
297 CFG_COLOR_OPTS("#00FF00", "#FFFF00", "#FF0000"),
300 cfg_opt_t run_watch_opts[] = {
301 CFG_STR("pidfile", NULL, CFGF_NONE),
302 CFG_STR("format", "%title: %status", CFGF_NONE),
303 CFG_STR("format_down", NULL, CFGF_NONE),
304 CFG_CUSTOM_ALIGN_OPT,
305 CFG_CUSTOM_COLOR_OPTS,
306 CFG_CUSTOM_MIN_WIDTH_OPT,
309 cfg_opt_t path_exists_opts[] = {
310 CFG_STR("path", NULL, CFGF_NONE),
311 CFG_STR("format", "%title: %status", CFGF_NONE),
312 CFG_STR("format_down", NULL, CFGF_NONE),
313 CFG_CUSTOM_ALIGN_OPT,
314 CFG_CUSTOM_COLOR_OPTS,
315 CFG_CUSTOM_MIN_WIDTH_OPT,
318 cfg_opt_t wireless_opts[] = {
319 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
320 CFG_STR("format_down", "W: down", CFGF_NONE),
321 CFG_CUSTOM_ALIGN_OPT,
322 CFG_CUSTOM_COLOR_OPTS,
323 CFG_CUSTOM_MIN_WIDTH_OPT,
326 cfg_opt_t ethernet_opts[] = {
327 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
328 CFG_STR("format_down", "E: down", CFGF_NONE),
329 CFG_CUSTOM_ALIGN_OPT,
330 CFG_CUSTOM_COLOR_OPTS,
331 CFG_CUSTOM_MIN_WIDTH_OPT,
334 cfg_opt_t ipv6_opts[] = {
335 CFG_STR("format_up", "%ip", CFGF_NONE),
336 CFG_STR("format_down", "no IPv6", CFGF_NONE),
337 CFG_CUSTOM_ALIGN_OPT,
338 CFG_CUSTOM_COLOR_OPTS,
339 CFG_CUSTOM_MIN_WIDTH_OPT,
342 cfg_opt_t battery_opts[] = {
343 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
344 CFG_STR("format_down", "No battery", CFGF_NONE),
345 CFG_STR("status_chr", "CHR", CFGF_NONE),
346 CFG_STR("status_bat", "BAT", CFGF_NONE),
347 CFG_STR("status_full", "FULL", CFGF_NONE),
348 CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
349 CFG_INT("low_threshold", 30, CFGF_NONE),
350 CFG_STR("threshold_type", "time", CFGF_NONE),
351 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
352 CFG_BOOL("integer_battery_capacity", false, CFGF_NONE),
353 CFG_BOOL("hide_seconds", false, CFGF_NONE),
354 CFG_CUSTOM_ALIGN_OPT,
355 CFG_CUSTOM_COLOR_OPTS,
356 CFG_CUSTOM_MIN_WIDTH_OPT,
359 cfg_opt_t time_opts[] = {
360 CFG_STR("format", "%Y-%m-%d %H:%M:%S", CFGF_NONE),
361 CFG_CUSTOM_ALIGN_OPT,
362 CFG_CUSTOM_MIN_WIDTH_OPT,
365 cfg_opt_t tztime_opts[] = {
366 CFG_STR("format", "%Y-%m-%d %H:%M:%S %Z", CFGF_NONE),
367 CFG_STR("timezone", "", CFGF_NONE),
368 CFG_CUSTOM_ALIGN_OPT,
369 CFG_CUSTOM_MIN_WIDTH_OPT,
372 cfg_opt_t ddate_opts[] = {
373 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
374 CFG_CUSTOM_ALIGN_OPT,
375 CFG_CUSTOM_MIN_WIDTH_OPT,
378 cfg_opt_t load_opts[] = {
379 CFG_STR("format", "%1min %5min %15min", CFGF_NONE),
380 CFG_FLOAT("max_threshold", 5, CFGF_NONE),
381 CFG_CUSTOM_ALIGN_OPT,
382 CFG_CUSTOM_COLOR_OPTS,
383 CFG_CUSTOM_MIN_WIDTH_OPT,
386 cfg_opt_t usage_opts[] = {
387 CFG_STR("format", "%usage", CFGF_NONE),
388 CFG_CUSTOM_ALIGN_OPT,
389 CFG_CUSTOM_MIN_WIDTH_OPT,
392 cfg_opt_t temp_opts[] = {
393 CFG_STR("format", "%degrees C", CFGF_NONE),
394 CFG_STR("path", NULL, CFGF_NONE),
395 CFG_INT("max_threshold", 75, CFGF_NONE),
396 CFG_CUSTOM_ALIGN_OPT,
397 CFG_CUSTOM_COLOR_OPTS,
398 CFG_CUSTOM_MIN_WIDTH_OPT,
401 cfg_opt_t disk_opts[] = {
402 CFG_STR("format", "%free", CFGF_NONE),
403 CFG_STR("format_not_mounted", NULL, CFGF_NONE),
404 CFG_STR("prefix_type", "binary", CFGF_NONE),
405 CFG_STR("threshold_type", "percentage_avail", CFGF_NONE),
406 CFG_FLOAT("low_threshold", 0, CFGF_NONE),
407 CFG_CUSTOM_ALIGN_OPT,
408 CFG_CUSTOM_COLOR_OPTS,
409 CFG_CUSTOM_MIN_WIDTH_OPT,
412 cfg_opt_t volume_opts[] = {
413 CFG_STR("format", "♪: %volume", CFGF_NONE),
414 CFG_STR("format_muted", "♪: 0%%", CFGF_NONE),
415 CFG_STR("device", "default", CFGF_NONE),
416 CFG_STR("mixer", "Master", CFGF_NONE),
417 CFG_INT("mixer_idx", 0, CFGF_NONE),
418 CFG_CUSTOM_ALIGN_OPT,
419 CFG_CUSTOM_COLOR_OPTS,
420 CFG_CUSTOM_MIN_WIDTH_OPT,
424 CFG_STR_LIST("order", "{}", CFGF_NONE),
425 CFG_SEC("general", general_opts, CFGF_NONE),
426 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
427 CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI),
428 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
429 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
430 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
431 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
432 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
433 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
434 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
435 CFG_SEC("time", time_opts, CFGF_NONE),
436 CFG_SEC("tztime", tztime_opts, CFGF_TITLE | CFGF_MULTI),
437 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
438 CFG_SEC("load", load_opts, CFGF_NONE),
439 CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
442 char *configfile = NULL;
443 int o, option_index = 0;
444 struct option long_options[] = {
445 {"config", required_argument, 0, 'c'},
446 {"help", no_argument, 0, 'h'},
447 {"version", no_argument, 0, 'v'},
450 struct sigaction action;
451 memset(&action, 0, sizeof(struct sigaction));
452 action.sa_handler = fatalsig;
454 /* Exit upon SIGPIPE because when we have nowhere to write to, gathering system
455 * information is pointless. Also exit explicitly on SIGTERM and SIGINT because
456 * only this will trigger a reset of the cursor in the terminal output-format.
458 sigaction(SIGPIPE, &action, NULL);
459 sigaction(SIGTERM, &action, NULL);
460 sigaction(SIGINT, &action, NULL);
462 memset(&action, 0, sizeof(struct sigaction));
463 action.sa_handler = sigusr1;
464 sigaction(SIGUSR1, &action, NULL);
466 if (setlocale(LC_ALL, "") == NULL)
467 die("Could not set locale. Please make sure all your LC_* / LANG settings are correct.");
469 while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
472 else if ((char)o == 'h') {
473 printf("i3status " VERSION " © 2008 Michael Stapelberg and contributors\n"
474 "Syntax: %s [-c <configfile>] [-h] [-v]\n",
477 } else if ((char)o == 'v') {
478 printf("i3status " VERSION " © 2008 Michael Stapelberg and contributors\n");
482 if (configfile == NULL)
483 configfile = get_config_path();
485 cfg = cfg_init(opts, CFGF_NOCASE);
486 if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
489 if (cfg_size(cfg, "order") == 0)
490 die("Your 'order' array is empty. Please fix your config.\n");
492 cfg_general = cfg_getsec(cfg, "general");
493 if (cfg_general == NULL)
494 die("Could not get section \"general\"\n");
496 char *output_str = cfg_getstr(cfg_general, "output_format");
497 if (strcasecmp(output_str, "auto") == 0) {
498 fprintf(stderr, "i3status: trying to auto-detect output_format setting\n");
499 output_str = auto_detect_format();
502 fprintf(stderr, "i3status: falling back to \"none\"\n");
504 fprintf(stderr, "i3status: auto-detected \"%s\"\n", output_str);
508 if (strcasecmp(output_str, "dzen2") == 0)
509 output_format = O_DZEN2;
510 else if (strcasecmp(output_str, "xmobar") == 0)
511 output_format = O_XMOBAR;
512 else if (strcasecmp(output_str, "i3bar") == 0)
513 output_format = O_I3BAR;
514 else if (strcasecmp(output_str, "lemonbar") == 0)
515 output_format = O_LEMONBAR;
516 else if (strcasecmp(output_str, "term") == 0)
517 output_format = O_TERM;
518 else if (strcasecmp(output_str, "none") == 0)
519 output_format = O_NONE;
521 die("Unknown output format: \"%s\"\n", output_str);
523 const char *separator = cfg_getstr(cfg_general, "separator");
525 /* lemonbar needs % to be escaped with another % */
526 pct_mark = (output_format == O_LEMONBAR) ? "%%" : "%";
528 // if no custom separator has been provided, use the default one
529 if (strcasecmp(separator, "default") == 0)
530 separator = get_default_separator();
532 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")))
533 die("Bad color format");
536 yajl_gen json_gen = yajl_gen_alloc(NULL);
538 yajl_gen json_gen = yajl_gen_alloc(NULL, NULL);
541 if (output_format == O_I3BAR) {
542 /* Initialize the i3bar protocol. See i3/docs/i3bar-protocol
544 printf("{\"version\":1}\n[\n");
546 yajl_gen_array_open(json_gen);
547 yajl_gen_clear(json_gen);
549 if (output_format == O_TERM) {
550 /* Save the cursor-position and hide the cursor */
551 printf("\033[s\033[?25l");
553 atexit(&reset_cursor);
556 if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
557 die("Could not create socket\n");
559 int interval = cfg_getint(cfg_general, "interval");
561 /* One memory page which each plugin can use to buffer output.
562 * Even though it’s unclean, we just assume that the user will not
563 * specify a format string which expands to something longer than 4096
564 * bytes — given that the output of i3status is used to display
565 * information on screen, more than 1024 characters for the full line
566 * (!), not individual plugins, seem very unlikely. */
569 void **per_instance = calloc(cfg_size(cfg, "order"), sizeof(*per_instance));
570 pthread_mutex_lock(&i3status_sleep_mutex);
573 if (exit_upon_signal) {
574 fprintf(stderr, "Exiting due to signal.\n");
578 gettimeofday(&tv, NULL);
579 if (output_format == O_I3BAR)
580 yajl_gen_array_open(json_gen);
581 else if (output_format == O_TERM)
582 /* Restore the cursor-position, clear line */
583 printf("\033[u\033[K");
584 for (j = 0; j < cfg_size(cfg, "order"); j++) {
585 cur_instance = per_instance + j;
587 print_separator(separator);
589 const char *current = cfg_getnstr(cfg, "order", j);
592 SEC_OPEN_MAP("ipv6");
593 print_ipv6_info(json_gen, buffer, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
597 CASE_SEC_TITLE("wireless") {
598 SEC_OPEN_MAP("wireless");
599 const char *interface = NULL;
600 if (strcasecmp(title, "_first_") == 0)
601 interface = first_eth_interface(NET_TYPE_WIRELESS);
602 if (interface == NULL)
604 print_wireless_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
608 CASE_SEC_TITLE("ethernet") {
609 SEC_OPEN_MAP("ethernet");
610 const char *interface = NULL;
611 if (strcasecmp(title, "_first_") == 0)
612 interface = first_eth_interface(NET_TYPE_ETHERNET);
613 if (interface == NULL)
615 print_eth_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
619 CASE_SEC_TITLE("battery") {
620 SEC_OPEN_MAP("battery");
621 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"));
625 CASE_SEC_TITLE("run_watch") {
626 SEC_OPEN_MAP("run_watch");
627 print_run_watch(json_gen, buffer, title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"));
631 CASE_SEC_TITLE("path_exists") {
632 SEC_OPEN_MAP("path_exists");
633 print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"));
637 CASE_SEC_TITLE("disk") {
638 SEC_OPEN_MAP("disk_info");
639 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"));
644 SEC_OPEN_MAP("load");
645 print_load(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getfloat(sec, "max_threshold"));
650 SEC_OPEN_MAP("time");
651 print_time(json_gen, buffer, NULL, cfg_getstr(sec, "format"), NULL, tv.tv_sec);
655 CASE_SEC_TITLE("tztime") {
656 SEC_OPEN_MAP("tztime");
657 print_time(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "timezone"), tv.tv_sec);
662 SEC_OPEN_MAP("ddate");
663 print_ddate(json_gen, buffer, cfg_getstr(sec, "format"), tv.tv_sec);
667 CASE_SEC_TITLE("volume") {
668 SEC_OPEN_MAP("volume");
669 print_volume(json_gen, buffer, cfg_getstr(sec, "format"),
670 cfg_getstr(sec, "format_muted"),
671 cfg_getstr(sec, "device"),
672 cfg_getstr(sec, "mixer"),
673 cfg_getint(sec, "mixer_idx"));
677 CASE_SEC_TITLE("cpu_temperature") {
678 SEC_OPEN_MAP("cpu_temperature");
679 print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "max_threshold"));
683 CASE_SEC("cpu_usage") {
684 SEC_OPEN_MAP("cpu_usage");
685 print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"));
689 if (output_format == O_I3BAR) {
690 yajl_gen_array_close(json_gen);
691 const unsigned char *buf;
697 yajl_gen_get_buf(json_gen, &buf, &len);
698 write(STDOUT_FILENO, buf, len);
699 yajl_gen_clear(json_gen);
705 /* To provide updates on every full second (as good as possible)
706 * we don’t use sleep(interval) but we sleep until the next second.
707 * We also align to 60 seconds modulo interval such
708 * that we start with :00 on every new minute. */
710 #if defined(__APPLE__)
711 gettimeofday(&tv, NULL);
712 ts.tv_sec = tv.tv_sec;
714 clock_gettime(CLOCK_REALTIME, &ts);
716 ts.tv_sec += interval - (ts.tv_sec % interval);
719 /* Sleep to absolute time 'ts', unless the condition
720 * 'i3status_sleep_cond' is signaled from another thread */
721 pthread_cond_timedwait(&i3status_sleep_cond, &i3status_sleep_mutex, &ts);