]> git.sur5r.net Git - i3/i3status/blob - i3status.c
Change output format to be a config option instead of a compile time define
[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
23 #include "i3status.h"
24
25 /* socket file descriptor for general purposes */
26 int general_socket;
27
28 cfg_t *cfg, *cfg_general;
29
30 /*
31  * Exit upon SIGPIPE because when we have nowhere to write to, gathering
32  * system information is pointless.
33  *
34  */
35 void sigpipe(int signum) {
36         fprintf(stderr, "Received SIGPIPE, exiting\n");
37         exit(1);
38 }
39
40 int main(int argc, char *argv[]) {
41         unsigned int j;
42
43         cfg_opt_t general_opts[] = {
44                 CFG_STR("output_format", "dzen2", CFGF_NONE),
45                 CFG_BOOL("colors", 1, CFGF_NONE),
46                 CFG_INT("interval", 1, CFGF_NONE),
47                 CFG_END()
48         };
49
50         cfg_opt_t run_watch_opts[] = {
51                 CFG_STR("pidfile", NULL, CFGF_NONE),
52                 CFG_STR("format", "%title: %status", CFGF_NONE),
53                 CFG_END()
54         };
55
56         cfg_opt_t wireless_opts[] = {
57                 CFG_STR("format_up", "W: (%quality at %essid) %ip", CFGF_NONE),
58                 CFG_STR("format_down", "W: down", CFGF_NONE),
59                 CFG_END()
60         };
61
62         cfg_opt_t ethernet_opts[] = {
63                 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
64                 CFG_STR("format_down", "E: down", CFGF_NONE),
65                 CFG_END()
66         };
67
68         cfg_opt_t ipv6_opts[] = {
69                 CFG_STR("format", "%ip", CFGF_NONE),
70                 CFG_END()
71         };
72
73         cfg_opt_t battery_opts[] = {
74                 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
75                 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
76                 CFG_END()
77         };
78
79         cfg_opt_t time_opts[] = {
80                 CFG_STR("format", "%d.%m.%Y %H:%M:%S", CFGF_NONE),
81                 CFG_END()
82         };
83
84         cfg_opt_t load_opts[] = {
85                 CFG_STR("format", "%5min %10min %15min", CFGF_NONE),
86                 CFG_END()
87         };
88
89         cfg_opt_t temp_opts[] = {
90                 CFG_STR("format", "%degrees C", CFGF_NONE),
91                 CFG_END()
92         };
93
94         cfg_opt_t disk_opts[] = {
95                 CFG_STR("format", "%free", CFGF_NONE),
96                 CFG_END()
97         };
98
99         cfg_opt_t opts[] = {
100                 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
101                 CFG_SEC("general", general_opts, CFGF_NONE),
102                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
103                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
104                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
105                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
106                 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
107                 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
108                 CFG_SEC("ipv6", ipv6_opts, CFGF_TITLE),
109                 CFG_SEC("time", time_opts, CFGF_NONE),
110                 CFG_SEC("load", load_opts, CFGF_NONE),
111                 CFG_END()
112         };
113
114         char *configfile = PREFIX "/etc/i3status.conf";
115         int o, option_index = 0;
116         struct option long_options[] = {
117                 {"config", required_argument, 0, 'c'},
118                 {"help", no_argument, 0, 'h'},
119                 {0, 0, 0, 0}
120         };
121
122         struct sigaction action;
123         memset(&action, 0, sizeof(struct sigaction));
124         action.sa_handler = sigpipe;
125         sigaction(SIGPIPE, &action, NULL);
126
127         while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
128                 if ((char)o == 'c')
129                         configfile = optarg;
130                 else if ((char)o == 'h') {
131                         printf("i3status © 2008-2009 Michael Stapelberg\n"
132                                 "Syntax: %s [-c <configfile>]\n", argv[0]);
133                         return 0;
134                 }
135
136         cfg = cfg_init(opts, CFGF_NONE);
137         if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
138                 return EXIT_FAILURE;
139
140         cfg_general = cfg_getsec(cfg, "general");
141         if (cfg_general == NULL)
142                 die("Could not get section \"general\"\n");
143
144         char *output_str = cfg_getstr(cfg_general, "output_format");
145         if (strcasecmp(output_str, "dzen2") == 0)
146                 output_format = O_DZEN2;
147         else if (strcasecmp(output_str, "xmobar") == 0)
148                 output_format = O_XMOBAR;
149         else if (strcasecmp(output_str, "none") == 0)
150                 output_format = O_NONE;
151         else die("Unknown output format: \"%s\"\n", output_str);
152
153         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
154                 die("Could not create socket\n");
155
156         while (1) {
157                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
158                         if (j > 0)
159                                 print_seperator();
160
161                         const char *current = cfg_getnstr(cfg, "order", j);
162
163                         CASE_SEC("ipv6")
164                                 print_ipv6_info(cfg_getstr(sec, "format"));
165
166                         CASE_SEC_TITLE("wireless")
167                                 print_wireless_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
168
169                         CASE_SEC_TITLE("ethernet")
170                                 print_eth_info(title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
171
172                         CASE_SEC_TITLE("battery")
173                                 print_battery_info(atoi(title), cfg_getstr(sec, "format"), cfg_getbool(sec, "last_full_capacity"));
174
175                         CASE_SEC_TITLE("run_watch")
176                                 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
177
178                         CASE_SEC_TITLE("disk")
179                                 print_disk_info(title, cfg_getstr(sec, "format"));
180
181                         CASE_SEC("load")
182                                 print_load(cfg_getstr(sec, "format"));
183
184                         CASE_SEC("time")
185                                 print_time(cfg_getstr(sec, "format"));
186
187                         CASE_SEC_TITLE("cpu_temperature")
188                                 print_cpu_temperature_info(atoi(title), cfg_getstr(sec, "format"));
189                 }
190                 printf("\n");
191                 fflush(stdout);
192
193                 sleep(cfg_getint(cfg_general, "interval"));
194         }
195 }