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