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 /* 1: check for $XDG_CONFIG_HOME/i3/config */
121 char *xdg_config_home, *xdg_config_dirs, *config_path;
123 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
124 xdg_config_home = "~/.config";
126 xdg_config_home = resolve_tilde(xdg_config_home);
127 if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
128 die("asprintf() failed");
129 free(xdg_config_home);
131 if (path_exists(config_path))
135 /* 2: check for $XDG_CONFIG_DIRS/i3/config */
136 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
137 xdg_config_dirs = "/etc/xdg";
139 char *buf = strdup(xdg_config_dirs);
140 char *tok = strtok(buf, ":");
141 while (tok != NULL) {
142 tok = resolve_tilde(tok);
143 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
144 die("asprintf() failed");
146 if (path_exists(config_path)) {
151 tok = strtok(NULL, ":");
155 /* 3: check traditional paths */
156 config_path = resolve_tilde("~/.i3status.conf");
157 if (path_exists(config_path))
160 config_path = strdup(SYSCONFDIR "/i3status.conf");
161 if (!path_exists(config_path))
162 die("Neither $XDG_CONFIG_HOME/i3status/config, nor "
163 "$XDG_CONFIG_DIRS/i3status/config, nor ~/.i3status.conf nor "
164 SYSCONFDIR "/i3status.conf exist.");
169 int main(int argc, char *argv[]) {
172 cfg_opt_t general_opts[] = {
173 CFG_STR("output_format", "dzen2", CFGF_NONE),
174 CFG_BOOL("colors", 1, CFGF_NONE),
175 CFG_STR("color_good", "#00FF00", CFGF_NONE),
176 CFG_STR("color_degraded", "#FFFF00", CFGF_NONE),
177 CFG_STR("color_bad", "#FF0000", CFGF_NONE),
178 CFG_STR("color_separator", "#333333", CFGF_NONE),
179 CFG_INT("interval", 1, CFGF_NONE),
183 cfg_opt_t run_watch_opts[] = {
184 CFG_STR("pidfile", NULL, CFGF_NONE),
185 CFG_STR("format", "%title: %status", CFGF_NONE),
189 cfg_opt_t wireless_opts[] = {
190 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
191 CFG_STR("format_down", "W: down", CFGF_NONE),
195 cfg_opt_t ethernet_opts[] = {
196 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
197 CFG_STR("format_down", "E: down", CFGF_NONE),
201 cfg_opt_t ipv6_opts[] = {
202 CFG_STR("format_up", "%ip", CFGF_NONE),
203 CFG_STR("format_down", "no IPv6", CFGF_NONE),
207 cfg_opt_t battery_opts[] = {
208 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
209 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
213 cfg_opt_t time_opts[] = {
214 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
218 cfg_opt_t ddate_opts[] = {
219 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
223 cfg_opt_t load_opts[] = {
224 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
228 cfg_opt_t temp_opts[] = {
229 CFG_STR("format", "%degrees C", CFGF_NONE),
233 cfg_opt_t disk_opts[] = {
234 CFG_STR("format", "%free", CFGF_NONE),
238 cfg_opt_t volume_opts[] = {
239 CFG_STR("format", "♪: %volume", CFGF_NONE),
240 CFG_STR("device", "default", CFGF_NONE),
241 CFG_STR("mixer", "Master", CFGF_NONE),
242 CFG_INT("mixer_idx", 0, CFGF_NONE),
247 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
248 CFG_SEC("general", general_opts, CFGF_NONE),
249 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
250 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
251 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
252 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
253 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
254 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
255 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
256 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
257 CFG_SEC("time", time_opts, CFGF_NONE),
258 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
259 CFG_SEC("load", load_opts, CFGF_NONE),
263 char *configfile = NULL;
264 int o, option_index = 0;
265 struct option long_options[] = {
266 {"config", required_argument, 0, 'c'},
267 {"help", no_argument, 0, 'h'},
268 {"version", no_argument, 0, 'v'},
272 struct sigaction action;
273 memset(&action, 0, sizeof(struct sigaction));
274 action.sa_handler = sigpipe;
275 sigaction(SIGPIPE, &action, NULL);
277 while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
280 else if ((char)o == 'h') {
281 printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n"
282 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
284 } else if ((char)o == 'v') {
285 printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n");
290 if (configfile == NULL)
291 configfile = get_config_path();
293 cfg = cfg_init(opts, CFGF_NONE);
294 if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
297 cfg_general = cfg_getsec(cfg, "general");
298 if (cfg_general == NULL)
299 die("Could not get section \"general\"\n");
301 char *output_str = cfg_getstr(cfg_general, "output_format");
302 if (strcasecmp(output_str, "dzen2") == 0)
303 output_format = O_DZEN2;
304 else if (strcasecmp(output_str, "xmobar") == 0)
305 output_format = O_XMOBAR;
306 else if (strcasecmp(output_str, "none") == 0)
307 output_format = O_NONE;
308 else die("Unknown output format: \"%s\"\n", output_str);
310 if (!valid_color(cfg_getstr(cfg_general, "color_good"))
311 || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
312 || !valid_color(cfg_getstr(cfg_general, "color_bad"))
313 || !valid_color(cfg_getstr(cfg_general, "color_separator")))
314 die("Bad color format");
316 if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
317 die("Could not create socket\n");
319 int interval = cfg_getint(cfg_general, "interval");
322 for (j = 0; j < cfg_size(cfg, "order"); j++) {
326 const char *current = cfg_getnstr(cfg, "order", j);
329 print_ipv6_info(cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
331 CASE_SEC_TITLE("wireless")
332 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
334 CASE_SEC_TITLE("ethernet")
335 print_eth_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
337 CASE_SEC_TITLE("battery")
338 print_battery_info(atoi(title), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
340 CASE_SEC_TITLE("run_watch")
341 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
343 CASE_SEC_TITLE("disk")
344 print_disk_info(title, cfg_getstr(sec, "format"));
347 print_load(cfg_getstr(sec, "format"));
350 print_time(cfg_getstr(sec, "format"));
353 print_ddate(cfg_getstr(sec, "format"));
355 CASE_SEC_TITLE("volume")
356 print_volume(cfg_getstr(sec, "format"),
357 cfg_getstr(sec, "device"),
358 cfg_getstr(sec, "mixer"),
359 cfg_getint(sec, "mixer_idx"));
361 CASE_SEC_TITLE("cpu_temperature")
362 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "format"));
367 /* To provide updates on every full second (as good as possible)
368 * we don’t use sleep(interval) but we sleep until the next
369 * second (with microsecond precision) plus (interval-1)
371 struct timeval current_time;
372 gettimeofday(¤t_time, NULL);
373 struct timespec ts = {interval - 1, (10e5 - current_time.tv_usec) * 1000};
374 nanosleep(&ts, NULL);