4 * i3status – Generates a status line for dzen2 or xmobar
7 * Copyright © 2008-2009 Michael Stapelberg and contributors
8 * Copyright © 2009 Thorsten Toepper <atsutane at freethoughts dot de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice, this
15 * list of conditions and the following disclaimer.
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.
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.
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.
43 #include <sys/socket.h>
44 #include <netinet/in.h>
51 /* socket file descriptor for general purposes */
54 cfg_t *cfg, *cfg_general;
57 * Exit upon SIGPIPE because when we have nowhere to write to, gathering
58 * system information is pointless.
61 void sigpipe(int signum) {
62 fprintf(stderr, "Received SIGPIPE, exiting\n");
66 int main(int argc, char *argv[]) {
69 cfg_opt_t general_opts[] = {
70 CFG_BOOL("colors", 1, CFGF_NONE),
71 CFG_INT("interval", 1, CFGF_NONE),
75 cfg_opt_t run_watch_opts[] = {
76 CFG_STR("pidfile", NULL, CFGF_NONE),
77 CFG_STR("format", "%title: %status", CFGF_NONE),
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),
87 cfg_opt_t ethernet_opts[] = {
88 CFG_STR("format", "E: %ip (%speed)", CFGF_NONE),
92 cfg_opt_t ipv6_opts[] = {
93 CFG_STR("format", "%ip", CFGF_NONE),
97 cfg_opt_t battery_opts[] = {
98 CFG_STR("format", "%status %remaining", CFGF_NONE),
102 cfg_opt_t time_opts[] = {
103 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
107 cfg_opt_t load_opts[] = {
108 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
112 cfg_opt_t temp_opts[] = {
113 CFG_STR("format", "%degrees C", CFGF_NONE),
117 cfg_opt_t disk_opts[] = {
118 CFG_STR("format", "%free", CFGF_NONE),
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),
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'},
145 struct sigaction action;
146 memset(&action, 0, sizeof(struct sigaction));
147 action.sa_handler = sigpipe;
148 sigaction(SIGPIPE, &action, NULL);
150 while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
153 else if ((char)o == 'h') {
154 printf("i3status © 2008-2009 Michael Stapelberg\n"
155 "Syntax: %s [-c <configfile>]\n", argv[0]);
159 cfg = cfg_init(opts, CFGF_NONE);
160 if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
163 cfg_general = cfg_getsec(cfg, "general");
165 if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
166 die("Could not create socket\n");
169 for (j = 0; j < cfg_size(cfg, "order"); j++) {
173 const char *current = cfg_getnstr(cfg, "order", j);
176 print_ipv6_info(cfg_getstr(sec, "format"));
178 CASE_SEC_TITLE("wireless")
179 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
181 CASE_SEC_TITLE("ethernet")
182 print_eth_info(title, cfg_getstr(sec, "format"));
184 CASE_SEC_TITLE("battery")
185 print_battery_info(atoi(title), cfg_getstr(sec, "format"));
187 CASE_SEC_TITLE("run_watch")
188 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
190 CASE_SEC_TITLE("disk")
191 print_disk_info(title, cfg_getstr(sec, "format"));
194 print_load(cfg_getstr(sec, "format"));
197 print_time(cfg_getstr(sec, "format"));
199 CASE_SEC_TITLE("cpu_temperature")
200 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "format"));
205 sleep(cfg_getint(cfg_general, "interval"));