]> git.sur5r.net Git - i3/i3status/blob - i3status.c
6d6fbc7f1f2e9064d87dde790a9bcbab16ed2ab6
[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         /* 1: check for $XDG_CONFIG_HOME/i3/config */
121         char *xdg_config_home, *xdg_config_dirs, *config_path;
122
123         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
124                 xdg_config_home = "~/.config";
125
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);
130
131         if (path_exists(config_path))
132                 return config_path;
133         free(config_path);
134
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";
138
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");
145                 free(tok);
146                 if (path_exists(config_path)) {
147                         free(buf);
148                         return config_path;
149                 }
150                 free(config_path);
151                 tok = strtok(NULL, ":");
152         }
153         free(buf);
154
155         /* 3: check traditional paths */
156         config_path = resolve_tilde("~/.i3status.conf");
157         if (path_exists(config_path))
158                 return config_path;
159
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.");
165
166         return config_path;
167 }
168
169 int main(int argc, char *argv[]) {
170         unsigned int j;
171
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),
180                 CFG_END()
181         };
182
183         cfg_opt_t run_watch_opts[] = {
184                 CFG_STR("pidfile", NULL, CFGF_NONE),
185                 CFG_STR("format", "%title: %status", CFGF_NONE),
186                 CFG_END()
187         };
188
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),
192                 CFG_END()
193         };
194
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),
198                 CFG_END()
199         };
200
201         cfg_opt_t ipv6_opts[] = {
202                 CFG_STR("format_up", "%ip", CFGF_NONE),
203                 CFG_STR("format_down", "no IPv6", CFGF_NONE),
204                 CFG_END()
205         };
206
207         cfg_opt_t battery_opts[] = {
208                 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
209                 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
210                 CFG_END()
211         };
212
213         cfg_opt_t time_opts[] = {
214                 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
215                 CFG_END()
216         };
217
218         cfg_opt_t ddate_opts[] = {
219                 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
220                 CFG_END()
221         };
222
223         cfg_opt_t load_opts[] = {
224                 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
225                 CFG_END()
226         };
227
228         cfg_opt_t temp_opts[] = {
229                 CFG_STR("format", "%degrees C", CFGF_NONE),
230                 CFG_END()
231         };
232
233         cfg_opt_t disk_opts[] = {
234                 CFG_STR("format", "%free", CFGF_NONE),
235                 CFG_END()
236         };
237
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),
243                 CFG_END()
244         };
245
246         cfg_opt_t opts[] = {
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),
260                 CFG_END()
261         };
262
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'},
269                 {0, 0, 0, 0}
270         };
271
272         struct sigaction action;
273         memset(&action, 0, sizeof(struct sigaction));
274         action.sa_handler = sigpipe;
275         sigaction(SIGPIPE, &action, NULL);
276
277         while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
278                 if ((char)o == 'c')
279                         configfile = optarg;
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]);
283                         return 0;
284                 } else if ((char)o == 'v') {
285                         printf("i3status " VERSION " © 2008-2010 Michael Stapelberg and contributors\n");
286                         return 0;
287                 }
288
289
290         if (configfile == NULL)
291                 configfile = get_config_path();
292
293         cfg = cfg_init(opts, CFGF_NONE);
294         if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
295                 return EXIT_FAILURE;
296
297         cfg_general = cfg_getsec(cfg, "general");
298         if (cfg_general == NULL)
299                 die("Could not get section \"general\"\n");
300
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);
309
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");
315
316         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
317                 die("Could not create socket\n");
318
319         int interval = cfg_getint(cfg_general, "interval");
320
321         while (1) {
322                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
323                         if (j > 0)
324                                 print_seperator();
325
326                         const char *current = cfg_getnstr(cfg, "order", j);
327
328                         CASE_SEC("ipv6")
329                                 print_ipv6_info(cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
330
331                         CASE_SEC_TITLE("wireless")
332                                 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
333
334                         CASE_SEC_TITLE("ethernet")
335                                 print_eth_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
336
337                         CASE_SEC_TITLE("battery")
338                                 print_battery_info(atoi(title), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
339
340                         CASE_SEC_TITLE("run_watch")
341                                 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
342
343                         CASE_SEC_TITLE("disk")
344                                 print_disk_info(title, cfg_getstr(sec, "format"));
345
346                         CASE_SEC("load")
347                                 print_load(cfg_getstr(sec, "format"));
348
349                         CASE_SEC("time")
350                                 print_time(cfg_getstr(sec, "format"));
351
352                         CASE_SEC("ddate")
353                                 print_ddate(cfg_getstr(sec, "format"));
354
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"));
360
361                         CASE_SEC_TITLE("cpu_temperature")
362                                 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "format"));
363                 }
364                 printf("\n");
365                 fflush(stdout);
366
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)
370                  * seconds. */
371                 struct timeval current_time;
372                 gettimeofday(&current_time, NULL);
373                 struct timespec ts = {interval - 1, (10e5 - current_time.tv_usec) * 1000};
374                 nanosleep(&ts, NULL);
375         }
376 }