4 * i3status – Generates a status line for dzen2 or xmobar
6 * Copyright © 2008-2009 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>
32 #define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }
34 /* socket file descriptor for general purposes */
37 cfg_t *cfg, *cfg_general;
40 * Exit upon SIGPIPE because when we have nowhere to write to, gathering
41 * system information is pointless.
44 void sigpipe(int signum) {
45 fprintf(stderr, "Received SIGPIPE, exiting\n");
50 * Checks if the given path exists by calling stat().
53 static bool path_exists(const char *path) {
55 return (stat(path, &buf) == 0);
58 static void *scalloc(size_t size) {
59 void *result = calloc(size, 1);
60 exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
64 static char *sstrdup(const char *str) {
65 char *result = strdup(str);
66 exit_if_null(result, "Error: out of memory (strdup())\n");
72 * Validates a color in "#RRGGBB" format
75 static int valid_color(const char *value)
77 if (strlen(value) != 7) return 0;
78 if (value[0] != '#') return 0;
79 for (int i = 1; i < 7; ++i) {
80 if (value[i] >= '0' && value[i] <= '9') continue;
81 if (value[i] >= 'a' && value[i] <= 'f') continue;
82 if (value[i] >= 'A' && value[i] <= 'F') continue;
89 * This function resolves ~ in pathnames.
90 * It may resolve wildcards in the first part of the path, but if no match
91 * or multiple matches are found, it just returns a copy of path as given.
94 static char *resolve_tilde(const char *path) {
95 static glob_t globbuf;
96 char *head, *tail, *result = NULL;
98 tail = strchr(path, '/');
99 head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
101 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
103 /* no match, or many wildcard matches are bad */
104 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
105 result = sstrdup(path);
107 die("glob() failed");
109 head = globbuf.gl_pathv[0];
110 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
111 strncpy(result, head, strlen(head));
112 strncat(result, tail, strlen(tail));
119 static char *get_config_path() {
120 char *xdg_config_home, *xdg_config_dirs, *config_path;
122 /* 1: check the traditional path under the home directory */
123 config_path = resolve_tilde("~/.i3status.conf");
124 if (path_exists(config_path))
127 /* 2: check for $XDG_CONFIG_HOME/i3status/config */
128 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
129 xdg_config_home = "~/.config";
131 xdg_config_home = resolve_tilde(xdg_config_home);
132 if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
133 die("asprintf() failed");
134 free(xdg_config_home);
136 if (path_exists(config_path))
140 /* 3: check the traditional path under /etc */
141 config_path = SYSCONFDIR "/i3status.conf";
142 if (path_exists(config_path))
143 return sstrdup(config_path);
145 /* 4: check for $XDG_CONFIG_DIRS/i3status/config */
146 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
147 xdg_config_dirs = "/etc/xdg";
149 char *buf = strdup(xdg_config_dirs);
150 char *tok = strtok(buf, ":");
151 while (tok != NULL) {
152 tok = resolve_tilde(tok);
153 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
154 die("asprintf() failed");
156 if (path_exists(config_path)) {
161 tok = strtok(NULL, ":");
165 die("Unable to find the configuration file (looked at "
166 "~/.i3status/config, $XDG_CONFIG_HOME/i3status/config, "
167 "/etc/i3status/config and $XDG_CONFIG_DIRS/i3status/config)");
171 int main(int argc, char *argv[]) {
174 cfg_opt_t general_opts[] = {
175 CFG_STR("output_format", "dzen2", CFGF_NONE),
176 CFG_BOOL("colors", 1, CFGF_NONE),
177 CFG_STR("color_good", "#00FF00", CFGF_NONE),
178 CFG_STR("color_degraded", "#FFFF00", CFGF_NONE),
179 CFG_STR("color_bad", "#FF0000", CFGF_NONE),
180 CFG_STR("color_separator", "#333333", CFGF_NONE),
181 CFG_INT("interval", 1, CFGF_NONE),
185 cfg_opt_t run_watch_opts[] = {
186 CFG_STR("pidfile", NULL, CFGF_NONE),
187 CFG_STR("format", "%title: %status", CFGF_NONE),
191 cfg_opt_t wireless_opts[] = {
192 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
193 CFG_STR("format_down", "W: down", CFGF_NONE),
197 cfg_opt_t ethernet_opts[] = {
198 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
199 CFG_STR("format_down", "E: down", CFGF_NONE),
203 cfg_opt_t ipv6_opts[] = {
204 CFG_STR("format_up", "%ip", CFGF_NONE),
205 CFG_STR("format_down", "no IPv6", CFGF_NONE),
209 cfg_opt_t battery_opts[] = {
210 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
211 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
215 cfg_opt_t time_opts[] = {
216 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
220 cfg_opt_t ddate_opts[] = {
221 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
225 cfg_opt_t load_opts[] = {
226 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
230 cfg_opt_t usage_opts[] = {
231 CFG_STR("format", "%usage", CFGF_NONE),
235 cfg_opt_t temp_opts[] = {
236 CFG_STR("format", "%degrees C", CFGF_NONE),
237 CFG_STR("path", NULL, CFGF_NONE),
241 cfg_opt_t disk_opts[] = {
242 CFG_STR("format", "%free", CFGF_NONE),
246 cfg_opt_t volume_opts[] = {
247 CFG_STR("format", "♪: %volume", CFGF_NONE),
248 CFG_STR("device", "default", CFGF_NONE),
249 CFG_STR("mixer", "Master", CFGF_NONE),
250 CFG_INT("mixer_idx", 0, CFGF_NONE),
255 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
256 CFG_SEC("general", general_opts, CFGF_NONE),
257 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
258 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
259 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
260 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
261 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
262 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
263 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
264 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
265 CFG_SEC("time", time_opts, CFGF_NONE),
266 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
267 CFG_SEC("load", load_opts, CFGF_NONE),
268 CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
272 char *configfile = NULL;
273 int o, option_index = 0;
274 struct option long_options[] = {
275 {"config", required_argument, 0, 'c'},
276 {"help", no_argument, 0, 'h'},
277 {"version", no_argument, 0, 'v'},
281 struct sigaction action;
282 memset(&action, 0, sizeof(struct sigaction));
283 action.sa_handler = sigpipe;
284 sigaction(SIGPIPE, &action, NULL);
286 while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
289 else if ((char)o == 'h') {
290 printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n"
291 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
293 } else if ((char)o == 'v') {
294 printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n");
299 if (configfile == NULL)
300 configfile = get_config_path();
302 cfg = cfg_init(opts, CFGF_NONE);
303 if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
306 cfg_general = cfg_getsec(cfg, "general");
307 if (cfg_general == NULL)
308 die("Could not get section \"general\"\n");
310 char *output_str = cfg_getstr(cfg_general, "output_format");
311 if (strcasecmp(output_str, "dzen2") == 0)
312 output_format = O_DZEN2;
313 else if (strcasecmp(output_str, "xmobar") == 0)
314 output_format = O_XMOBAR;
315 else if (strcasecmp(output_str, "none") == 0)
316 output_format = O_NONE;
317 else die("Unknown output format: \"%s\"\n", output_str);
319 if (!valid_color(cfg_getstr(cfg_general, "color_good"))
320 || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
321 || !valid_color(cfg_getstr(cfg_general, "color_bad"))
322 || !valid_color(cfg_getstr(cfg_general, "color_separator")))
323 die("Bad color format");
325 if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
326 die("Could not create socket\n");
328 int interval = cfg_getint(cfg_general, "interval");
332 time_t current_time = time(NULL);
333 struct tm *current_tm = NULL;
334 if (current_time != (time_t) -1) {
335 localtime_r(¤t_time, &tm);
338 for (j = 0; j < cfg_size(cfg, "order"); j++) {
342 const char *current = cfg_getnstr(cfg, "order", j);
345 print_ipv6_info(cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
347 CASE_SEC_TITLE("wireless")
348 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
350 CASE_SEC_TITLE("ethernet")
351 print_eth_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
353 CASE_SEC_TITLE("battery")
354 print_battery_info(atoi(title), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
356 CASE_SEC_TITLE("run_watch")
357 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
359 CASE_SEC_TITLE("disk")
360 print_disk_info(title, cfg_getstr(sec, "format"));
363 print_load(cfg_getstr(sec, "format"));
366 print_time(cfg_getstr(sec, "format"), current_tm);
369 print_ddate(cfg_getstr(sec, "format"), current_tm);
371 CASE_SEC_TITLE("volume")
372 print_volume(cfg_getstr(sec, "format"),
373 cfg_getstr(sec, "device"),
374 cfg_getstr(sec, "mixer"),
375 cfg_getint(sec, "mixer_idx"));
377 CASE_SEC_TITLE("cpu_temperature")
378 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
380 CASE_SEC("cpu_usage")
381 print_cpu_usage(cfg_getstr(sec, "format"));
386 /* To provide updates on every full second (as good as possible)
387 * we don’t use sleep(interval) but we sleep until the next
388 * second (with microsecond precision) plus (interval-1)
390 struct timeval current_timeval;
391 gettimeofday(¤t_timeval, NULL);
392 struct timespec ts = {interval - 1, (10e5 - current_timeval.tv_usec) * 1000};
393 nanosleep(&ts, NULL);