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 /* socket file descriptor for general purposes */
35 cfg_t *cfg, *cfg_general;
38 * Exit upon SIGPIPE because when we have nowhere to write to, gathering
39 * system information is pointless.
42 void sigpipe(int signum) {
43 fprintf(stderr, "Received SIGPIPE, exiting\n");
48 * Checks if there is a file at the given path (expanding ~) and returns the
49 * full path if so or NULL if there is no file.
52 static char *file_exists(char *path) {
53 static glob_t globbuf;
55 char *full_path = NULL;
57 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
60 full_path = (globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
62 if (stat(full_path, &buf) < 0)
69 * Validates a color in "#RRGGBB" format
72 static int valid_color(const char *value)
74 if (strlen(value) != 7) return 0;
75 if (value[0] != '#') return 0;
76 for (int i = 1; i < 7; ++i) {
77 if (value[i] >= '0' && value[i] <= '9') continue;
78 if (value[i] >= 'a' && value[i] <= 'f') continue;
79 if (value[i] >= 'A' && value[i] <= 'F') continue;
85 int main(int argc, char *argv[]) {
88 cfg_opt_t general_opts[] = {
89 CFG_STR("output_format", "dzen2", CFGF_NONE),
90 CFG_BOOL("colors", 1, CFGF_NONE),
91 CFG_STR("color_good", "#00FF00", CFGF_NONE),
92 CFG_STR("color_degraded", "#FFFF00", CFGF_NONE),
93 CFG_STR("color_bad", "#FF0000", CFGF_NONE),
94 CFG_STR("color_separator", "#333333", CFGF_NONE),
95 CFG_INT("interval", 1, CFGF_NONE),
99 cfg_opt_t run_watch_opts[] = {
100 CFG_STR("pidfile", NULL, CFGF_NONE),
101 CFG_STR("format", "%title: %status", CFGF_NONE),
105 cfg_opt_t wireless_opts[] = {
106 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
107 CFG_STR("format_down", "W: down", CFGF_NONE),
111 cfg_opt_t ethernet_opts[] = {
112 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
113 CFG_STR("format_down", "E: down", CFGF_NONE),
117 cfg_opt_t ipv6_opts[] = {
118 CFG_STR("format_up", "%ip", CFGF_NONE),
119 CFG_STR("format_down", "no IPv6", CFGF_NONE),
123 cfg_opt_t battery_opts[] = {
124 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
125 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
129 cfg_opt_t time_opts[] = {
130 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
134 cfg_opt_t ddate_opts[] = {
135 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
139 cfg_opt_t load_opts[] = {
140 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
144 cfg_opt_t temp_opts[] = {
145 CFG_STR("format", "%degrees C", CFGF_NONE),
149 cfg_opt_t disk_opts[] = {
150 CFG_STR("format", "%free", CFGF_NONE),
154 cfg_opt_t volume_opts[] = {
155 CFG_STR("format", "♪: %volume", CFGF_NONE),
156 CFG_STR("device", "default", CFGF_NONE),
157 CFG_STR("mixer", "Master", CFGF_NONE),
158 CFG_INT("mixer_idx", 0, CFGF_NONE),
163 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
164 CFG_SEC("general", general_opts, CFGF_NONE),
165 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
166 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
167 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
168 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
169 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
170 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
171 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
172 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
173 CFG_SEC("time", time_opts, CFGF_NONE),
174 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
175 CFG_SEC("load", load_opts, CFGF_NONE),
180 int o, option_index = 0;
181 struct option long_options[] = {
182 {"config", required_argument, 0, 'c'},
183 {"help", no_argument, 0, 'h'},
184 {"version", no_argument, 0, 'v'},
188 struct sigaction action;
189 memset(&action, 0, sizeof(struct sigaction));
190 action.sa_handler = sigpipe;
191 sigaction(SIGPIPE, &action, NULL);
193 /* Figure out which configuration file to use before the user may
194 * override this setting using -c */
196 if ((configfile = file_exists("~/.i3status.conf")) == NULL)
197 configfile = file_exists(PREFIX "/etc/i3status.conf");
199 while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
202 else if ((char)o == 'h') {
203 printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n"
204 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
206 } else if ((char)o == 'v') {
207 printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n");
212 if (configfile == NULL)
213 die("No configuration file found\n");
215 cfg = cfg_init(opts, CFGF_NONE);
216 if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
219 cfg_general = cfg_getsec(cfg, "general");
220 if (cfg_general == NULL)
221 die("Could not get section \"general\"\n");
223 char *output_str = cfg_getstr(cfg_general, "output_format");
224 if (strcasecmp(output_str, "dzen2") == 0)
225 output_format = O_DZEN2;
226 else if (strcasecmp(output_str, "xmobar") == 0)
227 output_format = O_XMOBAR;
228 else if (strcasecmp(output_str, "none") == 0)
229 output_format = O_NONE;
230 else die("Unknown output format: \"%s\"\n", output_str);
232 if (!valid_color(cfg_getstr(cfg_general, "color_good"))
233 || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
234 || !valid_color(cfg_getstr(cfg_general, "color_bad"))
235 || !valid_color(cfg_getstr(cfg_general, "color_separator")))
236 die("Bad color format");
238 if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
239 die("Could not create socket\n");
241 int interval = cfg_getint(cfg_general, "interval");
244 for (j = 0; j < cfg_size(cfg, "order"); j++) {
248 const char *current = cfg_getnstr(cfg, "order", j);
251 print_ipv6_info(cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
253 CASE_SEC_TITLE("wireless")
254 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
256 CASE_SEC_TITLE("ethernet")
257 print_eth_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
259 CASE_SEC_TITLE("battery")
260 print_battery_info(atoi(title), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
262 CASE_SEC_TITLE("run_watch")
263 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
265 CASE_SEC_TITLE("disk")
266 print_disk_info(title, cfg_getstr(sec, "format"));
269 print_load(cfg_getstr(sec, "format"));
272 print_time(cfg_getstr(sec, "format"));
275 print_ddate(cfg_getstr(sec, "format"));
277 CASE_SEC_TITLE("volume")
278 print_volume(cfg_getstr(sec, "format"),
279 cfg_getstr(sec, "device"),
280 cfg_getstr(sec, "mixer"),
281 cfg_getint(sec, "mixer_idx"));
283 CASE_SEC_TITLE("cpu_temperature")
284 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "format"));
289 /* To provide updates on every full second (as good as possible)
290 * we don’t use sleep(interval) but we sleep until the next
291 * second (with microsecond precision) plus (interval-1)
293 struct timeval current_time;
294 gettimeofday(¤t_time, NULL);
295 struct timespec ts = {interval - 1, (10e5 - current_time.tv_usec) * 1000};
296 nanosleep(&ts, NULL);