]> git.sur5r.net Git - i3/i3status/blob - i3status.c
document the path option in sample config
[i3/i3status] / i3status.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3status – Generates a status line for dzen2 or xmobar
5  *
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>
10  *
11  * See file LICENSE for license information.
12  *
13  */
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdbool.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <getopt.h>
22 #include <signal.h>
23 #include <confuse.h>
24 #include <glob.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <time.h>
28 #include <sys/time.h>
29
30 #include "i3status.h"
31
32 #define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }
33
34 /* socket file descriptor for general purposes */
35 int general_socket;
36
37 cfg_t *cfg, *cfg_general;
38
39 /*
40  * Exit upon SIGPIPE because when we have nowhere to write to, gathering
41  * system information is pointless.
42  *
43  */
44 void sigpipe(int signum) {
45         fprintf(stderr, "Received SIGPIPE, exiting\n");
46         exit(1);
47 }
48
49 /*
50  * Checks if the given path exists by calling stat().
51  *
52  */
53 static bool path_exists(const char *path) {
54         struct stat buf;
55         return (stat(path, &buf) == 0);
56 }
57
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);
61         return result;
62 }
63
64 static char *sstrdup(const char *str) {
65         char *result = strdup(str);
66         exit_if_null(result, "Error: out of memory (strdup())\n");
67         return result;
68 }
69
70
71 /*
72  * Validates a color in "#RRGGBB" format
73  *
74  */
75 static int valid_color(const char *value)
76 {
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;
83                 return 0;
84         }
85         return 1;
86 }
87
88 /*
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.
92  *
93  */
94 static char *resolve_tilde(const char *path) {
95         static glob_t globbuf;
96         char *head, *tail, *result = NULL;
97
98         tail = strchr(path, '/');
99         head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
100
101         int res = glob(head, GLOB_TILDE, NULL, &globbuf);
102         free(head);
103         /* no match, or many wildcard matches are bad */
104         if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
105                 result = sstrdup(path);
106         else if (res != 0) {
107                 die("glob() failed");
108         } else {
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));
113         }
114         globfree(&globbuf);
115
116         return result;
117 }
118
119 static char *get_config_path() {
120         char *xdg_config_home, *xdg_config_dirs, *config_path;
121
122         /* 1: check the traditional path under the home directory */
123         config_path = resolve_tilde("~/.i3status.conf");
124         if (path_exists(config_path))
125                 return config_path;
126
127         /* 2: check for $XDG_CONFIG_HOME/i3status/config */
128         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
129                 xdg_config_home = "~/.config";
130
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);
135
136         if (path_exists(config_path))
137                 return config_path;
138         free(config_path);
139
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);
144
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";
148
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");
155                 free(tok);
156                 if (path_exists(config_path)) {
157                         free(buf);
158                         return config_path;
159                 }
160                 free(config_path);
161                 tok = strtok(NULL, ":");
162         }
163         free(buf);
164
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)");
168         return NULL;
169 }
170
171 int main(int argc, char *argv[]) {
172         unsigned int j;
173
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),
182                 CFG_END()
183         };
184
185         cfg_opt_t run_watch_opts[] = {
186                 CFG_STR("pidfile", NULL, CFGF_NONE),
187                 CFG_STR("format", "%title: %status", CFGF_NONE),
188                 CFG_END()
189         };
190
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),
194                 CFG_END()
195         };
196
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),
200                 CFG_END()
201         };
202
203         cfg_opt_t ipv6_opts[] = {
204                 CFG_STR("format_up", "%ip", CFGF_NONE),
205                 CFG_STR("format_down", "no IPv6", CFGF_NONE),
206                 CFG_END()
207         };
208
209         cfg_opt_t battery_opts[] = {
210                 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
211                 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
212                 CFG_END()
213         };
214
215         cfg_opt_t time_opts[] = {
216                 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
217                 CFG_END()
218         };
219
220         cfg_opt_t ddate_opts[] = {
221                 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
222                 CFG_END()
223         };
224
225         cfg_opt_t load_opts[] = {
226                 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
227                 CFG_END()
228         };
229
230         cfg_opt_t temp_opts[] = {
231                 CFG_STR("format", "%degrees C", CFGF_NONE),
232                 CFG_STR("path", NULL, CFGF_NONE),
233                 CFG_END()
234         };
235
236         cfg_opt_t disk_opts[] = {
237                 CFG_STR("format", "%free", CFGF_NONE),
238                 CFG_END()
239         };
240
241         cfg_opt_t volume_opts[] = {
242                 CFG_STR("format", "♪: %volume", CFGF_NONE),
243                 CFG_STR("device", "default", CFGF_NONE),
244                 CFG_STR("mixer", "Master", CFGF_NONE),
245                 CFG_INT("mixer_idx", 0, CFGF_NONE),
246                 CFG_END()
247         };
248
249         cfg_opt_t opts[] = {
250                 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
251                 CFG_SEC("general", general_opts, CFGF_NONE),
252                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
253                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
254                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
255                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
256                 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
257                 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
258                 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
259                 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
260                 CFG_SEC("time", time_opts, CFGF_NONE),
261                 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
262                 CFG_SEC("load", load_opts, CFGF_NONE),
263                 CFG_END()
264         };
265
266         char *configfile = NULL;
267         int o, option_index = 0;
268         struct option long_options[] = {
269                 {"config", required_argument, 0, 'c'},
270                 {"help", no_argument, 0, 'h'},
271                 {"version", no_argument, 0, 'v'},
272                 {0, 0, 0, 0}
273         };
274
275         struct sigaction action;
276         memset(&action, 0, sizeof(struct sigaction));
277         action.sa_handler = sigpipe;
278         sigaction(SIGPIPE, &action, NULL);
279
280         while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
281                 if ((char)o == 'c')
282                         configfile = optarg;
283                 else if ((char)o == 'h') {
284                         printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n"
285                                 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
286                         return 0;
287                 } else if ((char)o == 'v') {
288                         printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n");
289                         return 0;
290                 }
291
292
293         if (configfile == NULL)
294                 configfile = get_config_path();
295
296         cfg = cfg_init(opts, CFGF_NONE);
297         if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
298                 return EXIT_FAILURE;
299
300         cfg_general = cfg_getsec(cfg, "general");
301         if (cfg_general == NULL)
302                 die("Could not get section \"general\"\n");
303
304         char *output_str = cfg_getstr(cfg_general, "output_format");
305         if (strcasecmp(output_str, "dzen2") == 0)
306                 output_format = O_DZEN2;
307         else if (strcasecmp(output_str, "xmobar") == 0)
308                 output_format = O_XMOBAR;
309         else if (strcasecmp(output_str, "none") == 0)
310                 output_format = O_NONE;
311         else die("Unknown output format: \"%s\"\n", output_str);
312
313         if (!valid_color(cfg_getstr(cfg_general, "color_good"))
314                         || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
315                         || !valid_color(cfg_getstr(cfg_general, "color_bad"))
316                         || !valid_color(cfg_getstr(cfg_general, "color_separator")))
317                die("Bad color format");
318
319         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
320                 die("Could not create socket\n");
321
322         int interval = cfg_getint(cfg_general, "interval");
323
324         while (1) {
325                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
326                         if (j > 0)
327                                 print_seperator();
328
329                         const char *current = cfg_getnstr(cfg, "order", j);
330
331                         CASE_SEC("ipv6")
332                                 print_ipv6_info(cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
333
334                         CASE_SEC_TITLE("wireless")
335                                 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
336
337                         CASE_SEC_TITLE("ethernet")
338                                 print_eth_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
339
340                         CASE_SEC_TITLE("battery")
341                                 print_battery_info(atoi(title), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
342
343                         CASE_SEC_TITLE("run_watch")
344                                 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
345
346                         CASE_SEC_TITLE("disk")
347                                 print_disk_info(title, cfg_getstr(sec, "format"));
348
349                         CASE_SEC("load")
350                                 print_load(cfg_getstr(sec, "format"));
351
352                         CASE_SEC("time")
353                                 print_time(cfg_getstr(sec, "format"));
354
355                         CASE_SEC("ddate")
356                                 print_ddate(cfg_getstr(sec, "format"));
357
358                         CASE_SEC_TITLE("volume")
359                                 print_volume(cfg_getstr(sec, "format"),
360                                              cfg_getstr(sec, "device"),
361                                              cfg_getstr(sec, "mixer"),
362                                              cfg_getint(sec, "mixer_idx"));
363
364                         CASE_SEC_TITLE("cpu_temperature")
365                                 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
366                 }
367                 printf("\n");
368                 fflush(stdout);
369
370                 /* To provide updates on every full second (as good as possible)
371                  * we don’t use sleep(interval) but we sleep until the next
372                  * second (with microsecond precision) plus (interval-1)
373                  * seconds. */
374                 struct timeval current_time;
375                 gettimeofday(&current_time, NULL);
376                 struct timespec ts = {interval - 1, (10e5 - current_time.tv_usec) * 1000};
377                 nanosleep(&ts, NULL);
378         }
379 }