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