]> git.sur5r.net Git - i3/i3status/blob - i3status.c
Fix FTBFS on kFreeBSD and Hurd: libiw is Linux-only
[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  *
9  * See file LICENSE for license information.
10  *
11  */
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <getopt.h>
20 #include <signal.h>
21 #include <confuse.h>
22 #include <glob.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #include "i3status.h"
27
28 /* socket file descriptor for general purposes */
29 int general_socket;
30
31 cfg_t *cfg, *cfg_general;
32
33 /*
34  * Exit upon SIGPIPE because when we have nowhere to write to, gathering
35  * system information is pointless.
36  *
37  */
38 void sigpipe(int signum) {
39         fprintf(stderr, "Received SIGPIPE, exiting\n");
40         exit(1);
41 }
42
43 /*
44  * Checks if there is a file at the given path (expanding ~) and returns the
45  * full path if so or NULL if there is no file.
46  *
47  */
48 static char *file_exists(char *path) {
49         static glob_t globbuf;
50         struct stat buf;
51         char *full_path = NULL;
52
53         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
54                 return NULL;
55
56         full_path = (globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
57
58         if (stat(full_path, &buf) < 0)
59                 return NULL;
60
61         return full_path;
62 }
63
64 int main(int argc, char *argv[]) {
65         unsigned int j;
66
67         cfg_opt_t general_opts[] = {
68                 CFG_STR("output_format", "dzen2", CFGF_NONE),
69                 CFG_BOOL("colors", 1, CFGF_NONE),
70                 CFG_INT("interval", 1, CFGF_NONE),
71                 CFG_END()
72         };
73
74         cfg_opt_t run_watch_opts[] = {
75                 CFG_STR("pidfile", NULL, CFGF_NONE),
76                 CFG_STR("format", "%title: %status", CFGF_NONE),
77                 CFG_END()
78         };
79
80         cfg_opt_t wireless_opts[] = {
81                 CFG_STR("format_up", "W: (%quality at %essid) %ip", CFGF_NONE),
82                 CFG_STR("format_down", "W: down", CFGF_NONE),
83                 CFG_END()
84         };
85
86         cfg_opt_t ethernet_opts[] = {
87                 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
88                 CFG_STR("format_down", "E: down", CFGF_NONE),
89                 CFG_END()
90         };
91
92         cfg_opt_t ipv6_opts[] = {
93                 CFG_STR("format", "%ip", CFGF_NONE),
94                 CFG_END()
95         };
96
97         cfg_opt_t battery_opts[] = {
98                 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
99                 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
100                 CFG_END()
101         };
102
103         cfg_opt_t time_opts[] = {
104                 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
105                 CFG_END()
106         };
107
108         cfg_opt_t load_opts[] = {
109                 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
110                 CFG_END()
111         };
112
113         cfg_opt_t temp_opts[] = {
114                 CFG_STR("format", "%degrees C", CFGF_NONE),
115                 CFG_END()
116         };
117
118         cfg_opt_t disk_opts[] = {
119                 CFG_STR("format", "%free", CFGF_NONE),
120                 CFG_END()
121         };
122
123         cfg_opt_t opts[] = {
124                 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
125                 CFG_SEC("general", general_opts, CFGF_NONE),
126                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
127                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
128                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
129                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
130                 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
131                 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
132                 CFG_SEC("ipv6", ipv6_opts, CFGF_TITLE),
133                 CFG_SEC("time", time_opts, CFGF_NONE),
134                 CFG_SEC("load", load_opts, CFGF_NONE),
135                 CFG_END()
136         };
137
138         char *configfile;
139         int o, option_index = 0;
140         struct option long_options[] = {
141                 {"config", required_argument, 0, 'c'},
142                 {"help", no_argument, 0, 'h'},
143                 {0, 0, 0, 0}
144         };
145
146         struct sigaction action;
147         memset(&action, 0, sizeof(struct sigaction));
148         action.sa_handler = sigpipe;
149         sigaction(SIGPIPE, &action, NULL);
150
151         /* Figure out which configuration file to use before the user may
152          * override this setting using -c */
153
154         if ((configfile = file_exists("~/.i3status.conf")) == NULL)
155                 configfile = file_exists(PREFIX "/etc/i3status.conf");
156
157         while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
158                 if ((char)o == 'c')
159                         configfile = optarg;
160                 else if ((char)o == 'h') {
161                         printf("i3status © 2008-2009 Michael Stapelberg\n"
162                                 "Syntax: %s [-c <configfile>]\n", argv[0]);
163                         return 0;
164                 }
165
166         if (configfile == NULL)
167                 die("No configuration file found\n");
168
169         cfg = cfg_init(opts, CFGF_NONE);
170         if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
171                 return EXIT_FAILURE;
172
173         cfg_general = cfg_getsec(cfg, "general");
174         if (cfg_general == NULL)
175                 die("Could not get section \"general\"\n");
176
177         char *output_str = cfg_getstr(cfg_general, "output_format");
178         if (strcasecmp(output_str, "dzen2") == 0)
179                 output_format = O_DZEN2;
180         else if (strcasecmp(output_str, "xmobar") == 0)
181                 output_format = O_XMOBAR;
182         else if (strcasecmp(output_str, "none") == 0)
183                 output_format = O_NONE;
184         else die("Unknown output format: \"%s\"\n", output_str);
185
186         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
187                 die("Could not create socket\n");
188
189         while (1) {
190                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
191                         if (j > 0)
192                                 print_seperator();
193
194                         const char *current = cfg_getnstr(cfg, "order", j);
195
196                         CASE_SEC("ipv6")
197                                 print_ipv6_info(cfg_getstr(sec, "format"));
198
199                         CASE_SEC_TITLE("wireless")
200                                 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
201
202                         CASE_SEC_TITLE("ethernet")
203                                 print_eth_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
204
205                         CASE_SEC_TITLE("battery")
206                                 print_battery_info(atoi(title), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
207
208                         CASE_SEC_TITLE("run_watch")
209                                 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
210
211                         CASE_SEC_TITLE("disk")
212                                 print_disk_info(title, cfg_getstr(sec, "format"));
213
214                         CASE_SEC("load")
215                                 print_load(cfg_getstr(sec, "format"));
216
217                         CASE_SEC("time")
218                                 print_time(cfg_getstr(sec, "format"));
219
220                         CASE_SEC_TITLE("cpu_temperature")
221                                 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "format"));
222                 }
223                 printf("\n");
224                 fflush(stdout);
225
226                 sleep(cfg_getint(cfg_general, "interval"));
227         }
228 }