4 * i3status – Generates a status line for dzen2 or xmobar
6 * Copyright © 2008-2011 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>
35 #define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }
37 /* socket file descriptor for general purposes */
40 cfg_t *cfg, *cfg_general;
43 * Exit upon SIGPIPE because when we have nowhere to write to, gathering
44 * system information is pointless.
47 void sigpipe(int signum) {
48 fprintf(stderr, "Received SIGPIPE, exiting\n");
53 * Checks if the given path exists by calling stat().
56 static bool path_exists(const char *path) {
58 return (stat(path, &buf) == 0);
61 static void *scalloc(size_t size) {
62 void *result = calloc(size, 1);
63 exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
67 static char *sstrdup(const char *str) {
68 char *result = strdup(str);
69 exit_if_null(result, "Error: out of memory (strdup())\n");
75 * Validates a color in "#RRGGBB" format
78 static int valid_color(const char *value)
80 if (strlen(value) != 7) return 0;
81 if (value[0] != '#') return 0;
82 for (int i = 1; i < 7; ++i) {
83 if (value[i] >= '0' && value[i] <= '9') continue;
84 if (value[i] >= 'a' && value[i] <= 'f') continue;
85 if (value[i] >= 'A' && value[i] <= 'F') continue;
92 * This function resolves ~ in pathnames.
93 * It may resolve wildcards in the first part of the path, but if no match
94 * or multiple matches are found, it just returns a copy of path as given.
97 static char *resolve_tilde(const char *path) {
98 static glob_t globbuf;
99 char *head, *tail, *result = NULL;
101 tail = strchr(path, '/');
102 head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
104 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
106 /* no match, or many wildcard matches are bad */
107 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
108 result = sstrdup(path);
110 die("glob() failed");
112 head = globbuf.gl_pathv[0];
113 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
114 strncpy(result, head, strlen(head));
116 strncat(result, tail, strlen(tail));
123 static char *get_config_path() {
124 char *xdg_config_home, *xdg_config_dirs, *config_path;
126 /* 1: check the traditional path under the home directory */
127 config_path = resolve_tilde("~/.i3status.conf");
128 if (path_exists(config_path))
131 /* 2: check for $XDG_CONFIG_HOME/i3status/config */
132 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
133 xdg_config_home = "~/.config";
135 xdg_config_home = resolve_tilde(xdg_config_home);
136 if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
137 die("asprintf() failed");
138 free(xdg_config_home);
140 if (path_exists(config_path))
144 /* 3: check the traditional path under /etc */
145 config_path = SYSCONFDIR "/i3status.conf";
146 if (path_exists(config_path))
147 return sstrdup(config_path);
149 /* 4: check for $XDG_CONFIG_DIRS/i3status/config */
150 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
151 xdg_config_dirs = "/etc/xdg";
153 char *buf = strdup(xdg_config_dirs);
154 char *tok = strtok(buf, ":");
155 while (tok != NULL) {
156 tok = resolve_tilde(tok);
157 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
158 die("asprintf() failed");
160 if (path_exists(config_path)) {
165 tok = strtok(NULL, ":");
169 die("Unable to find the configuration file (looked at "
170 "~/.i3status.conf, $XDG_CONFIG_HOME/i3status/config, "
171 "/etc/i3status.conf and $XDG_CONFIG_DIRS/i3status/config)");
175 int main(int argc, char *argv[]) {
178 cfg_opt_t general_opts[] = {
179 CFG_STR("output_format", "auto", CFGF_NONE),
180 CFG_BOOL("colors", 1, CFGF_NONE),
181 CFG_STR("color_good", "#00FF00", CFGF_NONE),
182 CFG_STR("color_degraded", "#FFFF00", CFGF_NONE),
183 CFG_STR("color_bad", "#FF0000", CFGF_NONE),
184 CFG_STR("color_separator", "#333333", CFGF_NONE),
185 CFG_INT("interval", 1, CFGF_NONE),
189 cfg_opt_t run_watch_opts[] = {
190 CFG_STR("pidfile", NULL, CFGF_NONE),
191 CFG_STR("format", "%title: %status", CFGF_NONE),
195 cfg_opt_t wireless_opts[] = {
196 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
197 CFG_STR("format_down", "W: down", CFGF_NONE),
201 cfg_opt_t ethernet_opts[] = {
202 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
203 CFG_STR("format_down", "E: down", CFGF_NONE),
207 cfg_opt_t ipv6_opts[] = {
208 CFG_STR("format_up", "%ip", CFGF_NONE),
209 CFG_STR("format_down", "no IPv6", CFGF_NONE),
213 cfg_opt_t battery_opts[] = {
214 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
215 CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
216 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
220 cfg_opt_t time_opts[] = {
221 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
225 cfg_opt_t ddate_opts[] = {
226 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
230 cfg_opt_t load_opts[] = {
231 CFG_STR("format", "%1min %5min %15min", CFGF_NONE),
235 cfg_opt_t usage_opts[] = {
236 CFG_STR("format", "%usage", CFGF_NONE),
240 cfg_opt_t temp_opts[] = {
241 CFG_STR("format", "%degrees C", CFGF_NONE),
242 CFG_STR("path", NULL, CFGF_NONE),
246 cfg_opt_t disk_opts[] = {
247 CFG_STR("format", "%free", CFGF_NONE),
251 cfg_opt_t volume_opts[] = {
252 CFG_STR("format", "♪: %volume", CFGF_NONE),
253 CFG_STR("device", "default", CFGF_NONE),
254 CFG_STR("mixer", "Master", CFGF_NONE),
255 CFG_INT("mixer_idx", 0, CFGF_NONE),
260 CFG_STR_LIST("order", "{}", CFGF_NONE),
261 CFG_SEC("general", general_opts, CFGF_NONE),
262 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
263 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
264 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
265 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
266 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
267 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
268 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
269 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
270 CFG_SEC("time", time_opts, CFGF_NONE),
271 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
272 CFG_SEC("load", load_opts, CFGF_NONE),
273 CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
277 char *configfile = NULL;
278 int o, option_index = 0;
279 struct option long_options[] = {
280 {"config", required_argument, 0, 'c'},
281 {"help", no_argument, 0, 'h'},
282 {"version", no_argument, 0, 'v'},
286 struct sigaction action;
287 memset(&action, 0, sizeof(struct sigaction));
288 action.sa_handler = sigpipe;
289 sigaction(SIGPIPE, &action, NULL);
291 if (setlocale(LC_ALL, "") == NULL)
292 die("Could not set locale. Please make sure all your LC_* / LANG settings are correct.");
294 while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
297 else if ((char)o == 'h') {
298 printf("i3status " VERSION " © 2008-2011 Michael Stapelberg and contributors\n"
299 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
301 } else if ((char)o == 'v') {
302 printf("i3status " VERSION " © 2008-2011 Michael Stapelberg and contributors\n");
307 if (configfile == NULL)
308 configfile = get_config_path();
310 cfg = cfg_init(opts, CFGF_NOCASE);
311 if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
314 if (cfg_size(cfg, "order") == 0)
315 die("Your 'order' array is empty. Please fix your config.\n");
317 cfg_general = cfg_getsec(cfg, "general");
318 if (cfg_general == NULL)
319 die("Could not get section \"general\"\n");
321 char *output_str = cfg_getstr(cfg_general, "output_format");
322 if (strcasecmp(output_str, "auto") == 0) {
323 fprintf(stderr, "i3status: trying to auto-detect output_format setting\n");
324 output_str = auto_detect_format();
327 fprintf(stderr, "i3status: falling back to \"none\"\n");
329 fprintf(stderr, "i3status: auto-detected \"%s\"\n", output_str);
333 if (strcasecmp(output_str, "dzen2") == 0)
334 output_format = O_DZEN2;
335 else if (strcasecmp(output_str, "xmobar") == 0)
336 output_format = O_XMOBAR;
337 else if (strcasecmp(output_str, "i3bar") == 0)
338 output_format = O_I3BAR;
339 else if (strcasecmp(output_str, "none") == 0)
340 output_format = O_NONE;
341 else die("Unknown output format: \"%s\"\n", output_str);
343 if (!valid_color(cfg_getstr(cfg_general, "color_good"))
344 || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
345 || !valid_color(cfg_getstr(cfg_general, "color_bad"))
346 || !valid_color(cfg_getstr(cfg_general, "color_separator")))
347 die("Bad color format");
349 yajl_gen json_gen = yajl_gen_alloc(NULL);
351 if (output_format == O_I3BAR) {
352 /* Initialize the i3bar protocol. See i3/docs/i3bar-protocol
354 printf("{\"version\":1}\n[\n");
356 yajl_gen_array_open(json_gen);
357 yajl_gen_clear(json_gen);
360 if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
361 die("Could not create socket\n");
363 int interval = cfg_getint(cfg_general, "interval");
365 /* One memory page which each plugin can use to buffer output.
366 * Even though it’s unclean, we just assume that the user will not
367 * specify a format string which expands to something longer than 4096
368 * bytes — given that the output of i3status is used to display
369 * information on screen, more than 1024 characters for the full line
370 * (!), not individual plugins, seem very unlikely. */
376 gettimeofday(&tv, NULL);
377 time_t current_time = tv.tv_sec;
378 struct tm *current_tm = NULL;
379 if (current_time != (time_t) -1) {
380 localtime_r(¤t_time, &tm);
383 if (output_format == O_I3BAR)
384 yajl_gen_array_open(json_gen);
385 for (j = 0; j < cfg_size(cfg, "order"); j++) {
389 const char *current = cfg_getnstr(cfg, "order", j);
392 SEC_OPEN_MAP("ipv6");
393 print_ipv6_info(json_gen, buffer, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
397 CASE_SEC_TITLE("wireless") {
398 SEC_OPEN_MAP("wireless");
399 print_wireless_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
403 CASE_SEC_TITLE("ethernet") {
404 SEC_OPEN_MAP("ethernet");
405 print_eth_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
409 CASE_SEC_TITLE("battery") {
410 SEC_OPEN_MAP("battery");
411 print_battery_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
415 CASE_SEC_TITLE("run_watch") {
416 SEC_OPEN_MAP("run_watch");
417 print_run_watch(json_gen, buffer, title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
421 CASE_SEC_TITLE("disk") {
422 SEC_OPEN_MAP("disk_info");
423 print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"));
428 SEC_OPEN_MAP("load");
429 print_load(json_gen, buffer, cfg_getstr(sec, "format"));
434 SEC_OPEN_MAP("time");
435 print_time(json_gen, buffer, cfg_getstr(sec, "format"), current_tm);
440 SEC_OPEN_MAP("ddate");
441 print_ddate(json_gen, buffer, cfg_getstr(sec, "format"), current_tm);
445 CASE_SEC_TITLE("volume") {
446 SEC_OPEN_MAP("volume");
447 print_volume(json_gen, buffer, cfg_getstr(sec, "format"),
448 cfg_getstr(sec, "device"),
449 cfg_getstr(sec, "mixer"),
450 cfg_getint(sec, "mixer_idx"));
454 CASE_SEC_TITLE("cpu_temperature") {
455 SEC_OPEN_MAP("cpu_temperature");
456 print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
460 CASE_SEC("cpu_usage") {
461 SEC_OPEN_MAP("cpu_usage");
462 print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"));
466 if (output_format == O_I3BAR) {
467 yajl_gen_array_close(json_gen);
468 const unsigned char *buf;
470 yajl_gen_get_buf(json_gen, &buf, &len);
471 write(STDOUT_FILENO, buf, len);
472 yajl_gen_clear(json_gen);
478 /* To provide updates on every full second (as good as possible)
479 * we don’t use sleep(interval) but we sleep until the next
480 * second (with microsecond precision) plus (interval-1)
482 struct timeval current_timeval;
483 gettimeofday(¤t_timeval, NULL);
484 struct timespec ts = {interval - 1, (10e5 - current_timeval.tv_usec) * 1000};
485 nanosleep(&ts, NULL);