]> git.sur5r.net Git - i3/i3status/blob - i3status.c
Implement disk info (%free/%used/%total)
[i3/i3status] / i3status.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3status – Generates a status line for dzen2 or xmobar
5  *
6  *
7  * Copyright © 2008-2009 Michael Stapelberg and contributors
8  * Copyright © 2009 Thorsten Toepper <atsutane at freethoughts dot de>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * * Redistributions of source code must retain the above copyright notice, this
15  *   list of conditions and the following disclaimer.
16  *
17  * * Redistributions in binary form must reproduce the above copyright notice,
18  *   this list of conditions and the following disclaimer in the documentation
19  *   and/or other materials provided with the distribution.
20  *
21  * * Neither the name of Michael Stapelberg nor the names of contributors
22  *   may be used to endorse or promote products derived from this software
23  *   without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 #include <string.h>
39 #include <stdio.h>
40 #include <stdbool.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <getopt.h>
46 #include <signal.h>
47 #include <confuse.h>
48
49 #include "i3status.h"
50
51 /* socket file descriptor for general purposes */
52 int general_socket;
53
54 cfg_t *cfg, *cfg_general;
55
56 /*
57  * Exit upon SIGPIPE because when we have nowhere to write to, gathering
58  * system information is pointless.
59  *
60  */
61 void sigpipe(int signum) {
62         fprintf(stderr, "Received SIGPIPE, exiting\n");
63         exit(1);
64 }
65
66 int main(int argc, char *argv[]) {
67         unsigned int j;
68
69         cfg_opt_t general_opts[] = {
70                 CFG_BOOL("colors", 1, CFGF_NONE),
71                 CFG_INT("interval", 1, CFGF_NONE),
72                 CFG_END()
73         };
74
75         cfg_opt_t run_watch_opts[] = {
76                 CFG_STR("pidfile", NULL, CFGF_NONE),
77                 CFG_STR("format", "%title: %status", CFGF_NONE),
78                 CFG_END()
79         };
80
81         cfg_opt_t wireless_opts[] = {
82                 CFG_STR("format_up", "W: (%quality at %essid) %ip", CFGF_NONE),
83                 CFG_STR("format_down", "W: down", CFGF_NONE),
84                 CFG_END()
85         };
86
87         cfg_opt_t ethernet_opts[] = {
88                 CFG_STR("format", "E: %ip (%speed)", 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 %remaining", CFGF_NONE),
99                 CFG_END()
100         };
101
102         cfg_opt_t time_opts[] = {
103                 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
104                 CFG_END()
105         };
106
107         cfg_opt_t load_opts[] = {
108                 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
109                 CFG_END()
110         };
111
112         cfg_opt_t temp_opts[] = {
113                 CFG_STR("format", "%degrees C", CFGF_NONE),
114                 CFG_END()
115         };
116
117         cfg_opt_t disk_opts[] = {
118                 CFG_STR("format", "%free", CFGF_NONE),
119                 CFG_END()
120         };
121
122         cfg_opt_t opts[] = {
123                 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
124                 CFG_SEC("general", general_opts, CFGF_NONE),
125                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
126                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
127                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
128                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
129                 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
130                 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
131                 CFG_SEC("ipv6", ipv6_opts, CFGF_TITLE),
132                 CFG_SEC("time", time_opts, CFGF_NONE),
133                 CFG_SEC("load", load_opts, CFGF_NONE),
134                 CFG_END()
135         };
136
137         char *configfile = PREFIX "/etc/i3status.conf";
138         int o, option_index = 0;
139         struct option long_options[] = {
140                 {"config", required_argument, 0, 'c'},
141                 {"help", no_argument, 0, 'h'},
142                 {0, 0, 0, 0}
143         };
144
145         struct sigaction action;
146         memset(&action, 0, sizeof(struct sigaction));
147         action.sa_handler = sigpipe;
148         sigaction(SIGPIPE, &action, NULL);
149
150         while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
151                 if ((char)o == 'c')
152                         configfile = optarg;
153                 else if ((char)o == 'h') {
154                         printf("i3status © 2008-2009 Michael Stapelberg\n"
155                                 "Syntax: %s [-c <configfile>]\n", argv[0]);
156                         return 0;
157                 }
158
159         cfg = cfg_init(opts, CFGF_NONE);
160         if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
161                 return EXIT_FAILURE;
162
163         cfg_general = cfg_getsec(cfg, "general");
164
165         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
166                 die("Could not create socket\n");
167
168         while (1) {
169                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
170                         if (j > 0)
171                                 print_seperator();
172
173                         const char *current = cfg_getnstr(cfg, "order", j);
174
175                         CASE_SEC("ipv6")
176                                 print_ipv6_info(cfg_getstr(sec, "format"));
177
178                         CASE_SEC_TITLE("wireless")
179                                 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
180
181                         CASE_SEC_TITLE("ethernet")
182                                 print_eth_info(title, cfg_getstr(sec, "format"));
183
184                         CASE_SEC_TITLE("battery")
185                                 print_battery_info(atoi(title), cfg_getstr(sec, "format"));
186
187                         CASE_SEC_TITLE("run_watch")
188                                 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
189
190                         CASE_SEC_TITLE("disk")
191                                 print_disk_info(title, cfg_getstr(sec, "format"));
192
193                         CASE_SEC("load")
194                                 print_load(cfg_getstr(sec, "format"));
195
196                         CASE_SEC("time")
197                                 print_time(cfg_getstr(sec, "format"));
198
199                         CASE_SEC_TITLE("cpu_temperature")
200                                 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "format"));
201                 }
202                 printf("\n");
203                 fflush(stdout);
204
205                 sleep(cfg_getint(cfg_general, "interval"));
206         }
207 }