]> git.sur5r.net Git - i3/i3status/blob - i3status.c
bump copyright
[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-2011 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 #include <locale.h>
30
31 #include "i3status.h"
32
33 #define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }
34
35 /* socket file descriptor for general purposes */
36 int general_socket;
37
38 cfg_t *cfg, *cfg_general;
39
40 /*
41  * Exit upon SIGPIPE because when we have nowhere to write to, gathering
42  * system information is pointless.
43  *
44  */
45 void sigpipe(int signum) {
46         fprintf(stderr, "Received SIGPIPE, exiting\n");
47         exit(1);
48 }
49
50 /*
51  * Checks if the given path exists by calling stat().
52  *
53  */
54 static bool path_exists(const char *path) {
55         struct stat buf;
56         return (stat(path, &buf) == 0);
57 }
58
59 static void *scalloc(size_t size) {
60         void *result = calloc(size, 1);
61         exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
62         return result;
63 }
64
65 static char *sstrdup(const char *str) {
66         char *result = strdup(str);
67         exit_if_null(result, "Error: out of memory (strdup())\n");
68         return result;
69 }
70
71
72 /*
73  * Validates a color in "#RRGGBB" format
74  *
75  */
76 static int valid_color(const char *value)
77 {
78         if (strlen(value) != 7) return 0;
79         if (value[0] != '#') return 0;
80         for (int i = 1; i < 7; ++i) {
81                 if (value[i] >= '0' && value[i] <= '9') continue;
82                 if (value[i] >= 'a' && value[i] <= 'f') continue;
83                 if (value[i] >= 'A' && value[i] <= 'F') continue;
84                 return 0;
85         }
86         return 1;
87 }
88
89 /*
90  * This function resolves ~ in pathnames.
91  * It may resolve wildcards in the first part of the path, but if no match
92  * or multiple matches are found, it just returns a copy of path as given.
93  *
94  */
95 static char *resolve_tilde(const char *path) {
96         static glob_t globbuf;
97         char *head, *tail, *result = NULL;
98
99         tail = strchr(path, '/');
100         head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
101
102         int res = glob(head, GLOB_TILDE, NULL, &globbuf);
103         free(head);
104         /* no match, or many wildcard matches are bad */
105         if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
106                 result = sstrdup(path);
107         else if (res != 0) {
108                 die("glob() failed");
109         } else {
110                 head = globbuf.gl_pathv[0];
111                 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
112                 strncpy(result, head, strlen(head));
113                 strncat(result, tail, strlen(tail));
114         }
115         globfree(&globbuf);
116
117         return result;
118 }
119
120 static char *get_config_path() {
121         char *xdg_config_home, *xdg_config_dirs, *config_path;
122
123         /* 1: check the traditional path under the home directory */
124         config_path = resolve_tilde("~/.i3status.conf");
125         if (path_exists(config_path))
126                 return config_path;
127
128         /* 2: check for $XDG_CONFIG_HOME/i3status/config */
129         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
130                 xdg_config_home = "~/.config";
131
132         xdg_config_home = resolve_tilde(xdg_config_home);
133         if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
134                 die("asprintf() failed");
135         free(xdg_config_home);
136
137         if (path_exists(config_path))
138                 return config_path;
139         free(config_path);
140
141         /* 3: check the traditional path under /etc */
142         config_path = SYSCONFDIR "/i3status.conf";
143         if (path_exists(config_path))
144             return sstrdup(config_path);
145
146         /* 4: check for $XDG_CONFIG_DIRS/i3status/config */
147         if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
148                 xdg_config_dirs = "/etc/xdg";
149
150         char *buf = strdup(xdg_config_dirs);
151         char *tok = strtok(buf, ":");
152         while (tok != NULL) {
153                 tok = resolve_tilde(tok);
154                 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
155                         die("asprintf() failed");
156                 free(tok);
157                 if (path_exists(config_path)) {
158                         free(buf);
159                         return config_path;
160                 }
161                 free(config_path);
162                 tok = strtok(NULL, ":");
163         }
164         free(buf);
165
166         die("Unable to find the configuration file (looked at "
167                 "~/.i3status/config, $XDG_CONFIG_HOME/i3status/config, "
168                 "/etc/i3status/config and $XDG_CONFIG_DIRS/i3status/config)");
169         return NULL;
170 }
171
172 int main(int argc, char *argv[]) {
173         unsigned int j;
174
175         cfg_opt_t general_opts[] = {
176                 CFG_STR("output_format", "auto", CFGF_NONE),
177                 CFG_BOOL("colors", 1, CFGF_NONE),
178                 CFG_STR("color_good", "#00FF00", CFGF_NONE),
179                 CFG_STR("color_degraded", "#FFFF00", CFGF_NONE),
180                 CFG_STR("color_bad", "#FF0000", CFGF_NONE),
181                 CFG_STR("color_separator", "#333333", CFGF_NONE),
182                 CFG_INT("interval", 1, CFGF_NONE),
183                 CFG_END()
184         };
185
186         cfg_opt_t run_watch_opts[] = {
187                 CFG_STR("pidfile", NULL, CFGF_NONE),
188                 CFG_STR("format", "%title: %status", CFGF_NONE),
189                 CFG_END()
190         };
191
192         cfg_opt_t wireless_opts[] = {
193                 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
194                 CFG_STR("format_down", "W: down", CFGF_NONE),
195                 CFG_END()
196         };
197
198         cfg_opt_t ethernet_opts[] = {
199                 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
200                 CFG_STR("format_down", "E: down", CFGF_NONE),
201                 CFG_END()
202         };
203
204         cfg_opt_t ipv6_opts[] = {
205                 CFG_STR("format_up", "%ip", CFGF_NONE),
206                 CFG_STR("format_down", "no IPv6", CFGF_NONE),
207                 CFG_END()
208         };
209
210         cfg_opt_t battery_opts[] = {
211                 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
212                 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
213                 CFG_END()
214         };
215
216         cfg_opt_t time_opts[] = {
217                 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
218                 CFG_END()
219         };
220
221         cfg_opt_t ddate_opts[] = {
222                 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
223                 CFG_END()
224         };
225
226         cfg_opt_t load_opts[] = {
227                 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
228                 CFG_END()
229         };
230
231         cfg_opt_t usage_opts[] = {
232                 CFG_STR("format", "%usage", CFGF_NONE),
233                 CFG_END()
234         };
235
236         cfg_opt_t temp_opts[] = {
237                 CFG_STR("format", "%degrees C", CFGF_NONE),
238                 CFG_STR("path", NULL, CFGF_NONE),
239                 CFG_END()
240         };
241
242         cfg_opt_t disk_opts[] = {
243                 CFG_STR("format", "%free", CFGF_NONE),
244                 CFG_END()
245         };
246
247         cfg_opt_t volume_opts[] = {
248                 CFG_STR("format", "♪: %volume", CFGF_NONE),
249                 CFG_STR("device", "default", CFGF_NONE),
250                 CFG_STR("mixer", "Master", CFGF_NONE),
251                 CFG_INT("mixer_idx", 0, CFGF_NONE),
252                 CFG_END()
253         };
254
255         cfg_opt_t opts[] = {
256                 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
257                 CFG_SEC("general", general_opts, CFGF_NONE),
258                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
259                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
260                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
261                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
262                 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
263                 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
264                 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
265                 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
266                 CFG_SEC("time", time_opts, CFGF_NONE),
267                 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
268                 CFG_SEC("load", load_opts, CFGF_NONE),
269                 CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
270                 CFG_END()
271         };
272
273         char *configfile = NULL;
274         int o, option_index = 0;
275         struct option long_options[] = {
276                 {"config", required_argument, 0, 'c'},
277                 {"help", no_argument, 0, 'h'},
278                 {"version", no_argument, 0, 'v'},
279                 {0, 0, 0, 0}
280         };
281
282         struct sigaction action;
283         memset(&action, 0, sizeof(struct sigaction));
284         action.sa_handler = sigpipe;
285         sigaction(SIGPIPE, &action, NULL);
286
287         if (setlocale(LC_ALL, "") == NULL)
288                 die("Could not set locale. Please make sure all your LC_* / LANG settings are correct.");
289
290         while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
291                 if ((char)o == 'c')
292                         configfile = optarg;
293                 else if ((char)o == 'h') {
294                         printf("i3status " VERSION " © 2008-2011 Michael Stapelberg and contributors\n"
295                                 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
296                         return 0;
297                 } else if ((char)o == 'v') {
298                         printf("i3status " VERSION " © 2008-2011 Michael Stapelberg and contributors\n");
299                         return 0;
300                 }
301
302
303         if (configfile == NULL)
304                 configfile = get_config_path();
305
306         cfg = cfg_init(opts, CFGF_NONE);
307         if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
308                 return EXIT_FAILURE;
309
310         cfg_general = cfg_getsec(cfg, "general");
311         if (cfg_general == NULL)
312                 die("Could not get section \"general\"\n");
313
314         char *output_str = cfg_getstr(cfg_general, "output_format");
315         if (strcasecmp(output_str, "auto") == 0) {
316                 fprintf(stderr, "i3status: trying to auto-detect output_format setting\n");
317                 output_str = auto_detect_format();
318                 if (!output_str) {
319                         output_str = "none";
320                         fprintf(stderr, "i3status: falling back to \"none\"\n");
321                 } else {
322                         fprintf(stderr, "i3status: auto-detected \"%s\"\n", output_str);
323                 }
324         }
325
326         if (strcasecmp(output_str, "dzen2") == 0)
327                 output_format = O_DZEN2;
328         else if (strcasecmp(output_str, "xmobar") == 0)
329                 output_format = O_XMOBAR;
330         else if (strcasecmp(output_str, "none") == 0)
331                 output_format = O_NONE;
332         else die("Unknown output format: \"%s\"\n", output_str);
333
334         if (!valid_color(cfg_getstr(cfg_general, "color_good"))
335                         || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
336                         || !valid_color(cfg_getstr(cfg_general, "color_bad"))
337                         || !valid_color(cfg_getstr(cfg_general, "color_separator")))
338                die("Bad color format");
339
340         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
341                 die("Could not create socket\n");
342
343         int interval = cfg_getint(cfg_general, "interval");
344
345         struct tm tm;
346         while (1) {
347                 struct timeval tv;
348                 gettimeofday(&tv, NULL);
349                 time_t current_time = tv.tv_sec;
350                 struct tm *current_tm = NULL;
351                 if (current_time != (time_t) -1) {
352                         localtime_r(&current_time, &tm);
353                         current_tm = &tm;
354                 }
355                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
356                         if (j > 0)
357                                 print_seperator();
358
359                         const char *current = cfg_getnstr(cfg, "order", j);
360
361                         CASE_SEC("ipv6")
362                                 print_ipv6_info(cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
363
364                         CASE_SEC_TITLE("wireless")
365                                 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
366
367                         CASE_SEC_TITLE("ethernet")
368                                 print_eth_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
369
370                         CASE_SEC_TITLE("battery")
371                                 print_battery_info(atoi(title), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
372
373                         CASE_SEC_TITLE("run_watch")
374                                 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
375
376                         CASE_SEC_TITLE("disk")
377                                 print_disk_info(title, cfg_getstr(sec, "format"));
378
379                         CASE_SEC("load")
380                                 print_load(cfg_getstr(sec, "format"));
381
382                         CASE_SEC("time")
383                                 print_time(cfg_getstr(sec, "format"), current_tm);
384
385                         CASE_SEC("ddate")
386                                 print_ddate(cfg_getstr(sec, "format"), current_tm);
387
388                         CASE_SEC_TITLE("volume")
389                                 print_volume(cfg_getstr(sec, "format"),
390                                              cfg_getstr(sec, "device"),
391                                              cfg_getstr(sec, "mixer"),
392                                              cfg_getint(sec, "mixer_idx"));
393
394                         CASE_SEC_TITLE("cpu_temperature")
395                                 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
396
397                         CASE_SEC("cpu_usage")
398                                 print_cpu_usage(cfg_getstr(sec, "format"));
399                 }
400                 printf("\n");
401                 fflush(stdout);
402
403                 /* To provide updates on every full second (as good as possible)
404                  * we don’t use sleep(interval) but we sleep until the next
405                  * second (with microsecond precision) plus (interval-1)
406                  * seconds. */
407                 struct timeval current_timeval;
408                 gettimeofday(&current_timeval, NULL);
409                 struct timespec ts = {interval - 1, (10e5 - current_timeval.tv_usec) * 1000};
410                 nanosleep(&ts, NULL);
411         }
412 }