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