4 * i3status – Generates a status line for dzen2 or xmobar
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>
11 * See file LICENSE for license information.
19 #include <sys/socket.h>
20 #include <netinet/in.h>
26 #include <sys/types.h>
31 #include <yajl/yajl_gen.h>
32 #include <yajl/yajl_version.h>
36 #define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }
38 #define CFG_COLOR_OPTS(good, degraded, bad) \
39 CFG_STR("color_good", good, CFGF_NONE), \
40 CFG_STR("color_degraded", degraded, CFGF_NONE), \
41 CFG_STR("color_bad", bad, CFGF_NONE)
43 #define CFG_CUSTOM_COLOR_OPTS CFG_COLOR_OPTS(NULL, NULL, NULL)
45 /* socket file descriptor for general purposes */
48 cfg_t *cfg, *cfg_general, *cfg_section;
51 * Exit upon SIGPIPE because when we have nowhere to write to, gathering
52 * system information is pointless.
55 void sigpipe(int signum) {
56 fprintf(stderr, "Received SIGPIPE, exiting\n");
61 * Checks if the given path exists by calling stat().
64 static bool path_exists(const char *path) {
66 return (stat(path, &buf) == 0);
69 static void *scalloc(size_t size) {
70 void *result = calloc(size, 1);
71 exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
75 static char *sstrdup(const char *str) {
76 char *result = strdup(str);
77 exit_if_null(result, "Error: out of memory (strdup())\n");
83 * Validates a color in "#RRGGBB" format
86 static int valid_color(const char *value)
88 if (strlen(value) != 7) return 0;
89 if (value[0] != '#') return 0;
90 for (int i = 1; i < 7; ++i) {
91 if (value[i] >= '0' && value[i] <= '9') continue;
92 if (value[i] >= 'a' && value[i] <= 'f') continue;
93 if (value[i] >= 'A' && value[i] <= 'F') continue;
100 * This function resolves ~ in pathnames.
101 * It may resolve wildcards in the first part of the path, but if no match
102 * or multiple matches are found, it just returns a copy of path as given.
105 static char *resolve_tilde(const char *path) {
106 static glob_t globbuf;
107 char *head, *tail, *result = NULL;
109 tail = strchr(path, '/');
110 head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
112 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
114 /* no match, or many wildcard matches are bad */
115 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
116 result = sstrdup(path);
118 die("glob() failed");
120 head = globbuf.gl_pathv[0];
121 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
122 strncpy(result, head, strlen(head));
124 strncat(result, tail, strlen(tail));
131 static char *get_config_path(void) {
132 char *xdg_config_home, *xdg_config_dirs, *config_path;
134 /* 1: check the traditional path under the home directory */
135 config_path = resolve_tilde("~/.i3status.conf");
136 if (path_exists(config_path))
139 /* 2: check for $XDG_CONFIG_HOME/i3status/config */
140 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
141 xdg_config_home = "~/.config";
143 xdg_config_home = resolve_tilde(xdg_config_home);
144 if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
145 die("asprintf() failed");
146 free(xdg_config_home);
148 if (path_exists(config_path))
152 /* 3: check the traditional path under /etc */
153 config_path = SYSCONFDIR "/i3status.conf";
154 if (path_exists(config_path))
155 return sstrdup(config_path);
157 /* 4: check for $XDG_CONFIG_DIRS/i3status/config */
158 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
159 xdg_config_dirs = "/etc/xdg";
161 char *buf = strdup(xdg_config_dirs);
162 char *tok = strtok(buf, ":");
163 while (tok != NULL) {
164 tok = resolve_tilde(tok);
165 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
166 die("asprintf() failed");
168 if (path_exists(config_path)) {
173 tok = strtok(NULL, ":");
177 die("Unable to find the configuration file (looked at "
178 "~/.i3status.conf, $XDG_CONFIG_HOME/i3status/config, "
179 "/etc/i3status.conf and $XDG_CONFIG_DIRS/i3status/config)");
183 int main(int argc, char *argv[]) {
186 cfg_opt_t general_opts[] = {
187 CFG_STR("output_format", "auto", CFGF_NONE),
188 CFG_BOOL("colors", 1, CFGF_NONE),
189 CFG_STR("color_separator", "#333333", CFGF_NONE),
190 CFG_INT("interval", 1, CFGF_NONE),
191 CFG_COLOR_OPTS("#00FF00", "#FFFF00", "#FF0000"),
195 cfg_opt_t run_watch_opts[] = {
196 CFG_STR("pidfile", NULL, CFGF_NONE),
197 CFG_STR("format", "%title: %status", CFGF_NONE),
198 CFG_CUSTOM_COLOR_OPTS,
202 cfg_opt_t wireless_opts[] = {
203 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
204 CFG_STR("format_down", "W: down", CFGF_NONE),
205 CFG_CUSTOM_COLOR_OPTS,
209 cfg_opt_t ethernet_opts[] = {
210 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
211 CFG_STR("format_down", "E: down", CFGF_NONE),
212 CFG_CUSTOM_COLOR_OPTS,
216 cfg_opt_t ipv6_opts[] = {
217 CFG_STR("format_up", "%ip", CFGF_NONE),
218 CFG_STR("format_down", "no IPv6", CFGF_NONE),
219 CFG_CUSTOM_COLOR_OPTS,
223 cfg_opt_t battery_opts[] = {
224 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
225 CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
226 CFG_INT("low_threshold", 30, CFGF_NONE),
227 CFG_STR("threshold_type", "time", CFGF_NONE),
228 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
229 CFG_CUSTOM_COLOR_OPTS,
233 cfg_opt_t time_opts[] = {
234 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
238 cfg_opt_t ddate_opts[] = {
239 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
243 cfg_opt_t load_opts[] = {
244 CFG_STR("format", "%1min %5min %15min", CFGF_NONE),
248 cfg_opt_t usage_opts[] = {
249 CFG_STR("format", "%usage", CFGF_NONE),
253 cfg_opt_t temp_opts[] = {
254 CFG_STR("format", "%degrees C", CFGF_NONE),
255 CFG_STR("path", NULL, CFGF_NONE),
256 CFG_INT("max_threshold", 75, CFGF_NONE),
257 CFG_CUSTOM_COLOR_OPTS,
261 cfg_opt_t disk_opts[] = {
262 CFG_STR("format", "%free", CFGF_NONE),
266 cfg_opt_t volume_opts[] = {
267 CFG_STR("format", "♪: %volume", CFGF_NONE),
268 CFG_STR("device", "default", CFGF_NONE),
269 CFG_STR("mixer", "Master", CFGF_NONE),
270 CFG_INT("mixer_idx", 0, CFGF_NONE),
271 CFG_CUSTOM_COLOR_OPTS,
276 CFG_STR_LIST("order", "{}", CFGF_NONE),
277 CFG_SEC("general", general_opts, CFGF_NONE),
278 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
279 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
280 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
281 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
282 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
283 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
284 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
285 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
286 CFG_SEC("time", time_opts, CFGF_NONE),
287 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
288 CFG_SEC("load", load_opts, CFGF_NONE),
289 CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
290 CFG_CUSTOM_COLOR_OPTS,
294 char *configfile = NULL;
295 int o, option_index = 0;
296 struct option long_options[] = {
297 {"config", required_argument, 0, 'c'},
298 {"help", no_argument, 0, 'h'},
299 {"version", no_argument, 0, 'v'},
303 struct sigaction action;
304 memset(&action, 0, sizeof(struct sigaction));
305 action.sa_handler = sigpipe;
306 sigaction(SIGPIPE, &action, NULL);
308 if (setlocale(LC_ALL, "") == NULL)
309 die("Could not set locale. Please make sure all your LC_* / LANG settings are correct.");
311 while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
314 else if ((char)o == 'h') {
315 printf("i3status " VERSION " © 2008-2012 Michael Stapelberg and contributors\n"
316 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
318 } else if ((char)o == 'v') {
319 printf("i3status " VERSION " © 2008-2012 Michael Stapelberg and contributors\n");
324 if (configfile == NULL)
325 configfile = get_config_path();
327 cfg = cfg_init(opts, CFGF_NOCASE);
328 if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
331 if (cfg_size(cfg, "order") == 0)
332 die("Your 'order' array is empty. Please fix your config.\n");
334 cfg_general = cfg_getsec(cfg, "general");
335 if (cfg_general == NULL)
336 die("Could not get section \"general\"\n");
338 char *output_str = cfg_getstr(cfg_general, "output_format");
339 if (strcasecmp(output_str, "auto") == 0) {
340 fprintf(stderr, "i3status: trying to auto-detect output_format setting\n");
341 output_str = auto_detect_format();
344 fprintf(stderr, "i3status: falling back to \"none\"\n");
346 fprintf(stderr, "i3status: auto-detected \"%s\"\n", output_str);
350 if (strcasecmp(output_str, "dzen2") == 0)
351 output_format = O_DZEN2;
352 else if (strcasecmp(output_str, "xmobar") == 0)
353 output_format = O_XMOBAR;
354 else if (strcasecmp(output_str, "i3bar") == 0)
355 output_format = O_I3BAR;
356 else if (strcasecmp(output_str, "none") == 0)
357 output_format = O_NONE;
358 else die("Unknown output format: \"%s\"\n", output_str);
360 if (!valid_color(cfg_getstr(cfg_general, "color_good"))
361 || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
362 || !valid_color(cfg_getstr(cfg_general, "color_bad"))
363 || !valid_color(cfg_getstr(cfg_general, "color_separator")))
364 die("Bad color format");
367 yajl_gen json_gen = yajl_gen_alloc(NULL);
369 yajl_gen json_gen = yajl_gen_alloc(NULL, NULL);
372 if (output_format == O_I3BAR) {
373 /* Initialize the i3bar protocol. See i3/docs/i3bar-protocol
375 printf("{\"version\":1}\n[\n");
377 yajl_gen_array_open(json_gen);
378 yajl_gen_clear(json_gen);
381 if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
382 die("Could not create socket\n");
384 int interval = cfg_getint(cfg_general, "interval");
386 /* One memory page which each plugin can use to buffer output.
387 * Even though it’s unclean, we just assume that the user will not
388 * specify a format string which expands to something longer than 4096
389 * bytes — given that the output of i3status is used to display
390 * information on screen, more than 1024 characters for the full line
391 * (!), not individual plugins, seem very unlikely. */
397 gettimeofday(&tv, NULL);
398 time_t current_time = tv.tv_sec;
399 struct tm *current_tm = NULL;
400 if (current_time != (time_t) -1) {
401 localtime_r(¤t_time, &tm);
404 if (output_format == O_I3BAR)
405 yajl_gen_array_open(json_gen);
406 for (j = 0; j < cfg_size(cfg, "order"); j++) {
410 const char *current = cfg_getnstr(cfg, "order", j);
413 SEC_OPEN_MAP("ipv6");
414 print_ipv6_info(json_gen, buffer, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
418 CASE_SEC_TITLE("wireless") {
419 SEC_OPEN_MAP("wireless");
420 print_wireless_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
424 CASE_SEC_TITLE("ethernet") {
425 SEC_OPEN_MAP("ethernet");
426 print_eth_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
430 CASE_SEC_TITLE("battery") {
431 SEC_OPEN_MAP("battery");
432 print_battery_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "low_threshold"), cfg_getstr(sec, "threshold_type"), cfg_getbool(sec, "last_full_capacity"));
436 CASE_SEC_TITLE("run_watch") {
437 SEC_OPEN_MAP("run_watch");
438 print_run_watch(json_gen, buffer, title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
442 CASE_SEC_TITLE("disk") {
443 SEC_OPEN_MAP("disk_info");
444 print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"));
449 SEC_OPEN_MAP("load");
450 print_load(json_gen, buffer, cfg_getstr(sec, "format"));
455 SEC_OPEN_MAP("time");
456 print_time(json_gen, buffer, cfg_getstr(sec, "format"), current_tm);
461 SEC_OPEN_MAP("ddate");
462 print_ddate(json_gen, buffer, cfg_getstr(sec, "format"), current_tm);
466 CASE_SEC_TITLE("volume") {
467 SEC_OPEN_MAP("volume");
468 print_volume(json_gen, buffer, cfg_getstr(sec, "format"),
469 cfg_getstr(sec, "device"),
470 cfg_getstr(sec, "mixer"),
471 cfg_getint(sec, "mixer_idx"));
475 CASE_SEC_TITLE("cpu_temperature") {
476 SEC_OPEN_MAP("cpu_temperature");
477 print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "max_threshold"));
481 CASE_SEC("cpu_usage") {
482 SEC_OPEN_MAP("cpu_usage");
483 print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"));
487 if (output_format == O_I3BAR) {
488 yajl_gen_array_close(json_gen);
489 const unsigned char *buf;
495 yajl_gen_get_buf(json_gen, &buf, &len);
496 write(STDOUT_FILENO, buf, len);
497 yajl_gen_clear(json_gen);
503 /* To provide updates on every full second (as good as possible)
504 * we don’t use sleep(interval) but we sleep until the next
505 * second (with microsecond precision) plus (interval-1)
507 struct timeval current_timeval;
508 gettimeofday(¤t_timeval, NULL);
509 struct timespec ts = {interval - 1, (10e5 - current_timeval.tv_usec) * 1000};
510 nanosleep(&ts, NULL);