2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010 Axel Wagner and contributors
6 * See file LICNSE for license information
22 * Glob path, i.e. expand ~
25 char *expand_path(char *path) {
26 static glob_t globbuf;
27 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
28 ELOG("glob() failed\n");
31 char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
33 ELOG("malloc() failed\n");
40 static void read_color(char **color) {
41 int len = strlen(optarg);
42 if (len == 6 || (len == 7 && optarg[0] == '#')) {
45 for (i = offset; good && i < 6 + offset; ++i) {
47 if (!(c >= 'a' && c <= 'f')
48 && !(c >= 'A' && c <= 'F')
49 && !(c >= '0' && c <= '9')) {
55 *color = strdup(optarg + offset);
60 fprintf(stderr, "Bad color value \"%s\"\n", optarg);
64 static void free_colors(struct xcb_color_strings_t *colors) {
65 #define FREE_COLOR(x) \
72 FREE_COLOR(active_ws_fg);
73 FREE_COLOR(active_ws_bg);
74 FREE_COLOR(inactive_ws_fg);
75 FREE_COLOR(inactive_ws_bg);
76 FREE_COLOR(urgent_ws_fg);
77 FREE_COLOR(urgent_ws_bg);
81 void print_usage(char *elf_name) {
82 printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-V] [-h]\n", elf_name);
83 printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
84 printf("-c <command>\tExecute <command> to get stdin\n");
85 printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
86 printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
87 printf("\t\tand a SIGCONT on unhiding of the bars\n");
88 printf("-f <font>\tUse X-Core-Font <font> for display\n");
89 printf("-V\t\tBe (very) verbose with the debug-output\n");
90 printf("-h\t\tDisplay this help-message and exit\n");
94 * We watch various signals, that are there to make our application stop.
95 * If we get one of those, we ev_unloop() and invoke the cleanup-routines
99 void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
100 switch (watcher->signum) {
102 DLOG("Got a SIGTERM, stopping\n");
105 DLOG("Got a SIGINT, stopping\n");
108 DLOG("Got a SIGHUP, stopping\n");
110 ev_unloop(main_loop, EVUNLOOP_ALL);
113 int main(int argc, char **argv) {
115 int option_index = 0;
116 char *socket_path = NULL;
117 char *command = NULL;
118 char *fontname = NULL;
119 char *i3_default_sock_path = "~/.i3/ipc.sock";
120 struct xcb_color_strings_t colors = { NULL, };
122 /* Definition of the standard-config */
123 config.hide_on_modifier = 0;
125 static struct option long_opt[] = {
126 { "socket", required_argument, 0, 's' },
127 { "command", required_argument, 0, 'c' },
128 { "hide", no_argument, 0, 'm' },
129 { "font", required_argument, 0, 'f' },
130 { "help", no_argument, 0, 'h' },
131 { "version", no_argument, 0, 'v' },
132 { "verbose", no_argument, 0, 'V' },
133 { "color-bar-fg", required_argument, 0, 'A' },
134 { "color-bar-bg", required_argument, 0, 'B' },
135 { "color-active-ws-fg", required_argument, 0, 'C' },
136 { "color-active-ws-bg", required_argument, 0, 'D' },
137 { "color-inactive-ws-fg", required_argument, 0, 'E' },
138 { "color-inactive-ws-bg", required_argument, 0, 'F' },
139 { "color-urgent-ws-bg", required_argument, 0, 'G' },
140 { "color-urgent-ws-fg", required_argument, 0, 'H' },
144 while ((opt = getopt_long(argc, argv, "s:c:mf:hvVA:B:C:D:E:F:G:H:", long_opt, &option_index)) != -1) {
147 socket_path = expand_path(optarg);
150 command = strdup(optarg);
153 config.hide_on_modifier = 1;
156 fontname = strdup(optarg);
159 printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
166 read_color(&colors.bar_fg);
169 read_color(&colors.bar_bg);
172 read_color(&colors.active_ws_fg);
175 read_color(&colors.active_ws_bg);
178 read_color(&colors.inactive_ws_fg);
181 read_color(&colors.inactive_ws_bg);
184 read_color(&colors.urgent_ws_bg);
187 read_color(&colors.urgent_ws_fg);
190 print_usage(argv[0]);
196 if (fontname == NULL) {
197 /* This is a very restrictive default. More sensefull would be something like
198 * "-misc-*-*-*-*--*-*-*-*-*-*-*-*". But since that produces very ugly results
199 * on my machine, let's stick with this until we have a configfile */
200 fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
203 if (socket_path == NULL) {
204 ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
205 socket_path = expand_path(i3_default_sock_path);
208 main_loop = ev_default_loop(0);
210 init_colors(&colors);
213 free_colors(&colors);
216 init_connection(socket_path);
220 /* We subscribe to the i3-events we need */
223 /* We initiate the main-function by requesting infos about the outputs and
224 * workspaces. Everything else (creating the bars, showing the right workspace-
225 * buttons and more) is taken care of by the event-driveniness of the code */
226 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
227 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
229 /* The name of this function is actually misleading. Even if no -c is specified,
230 * this function initiates the watchers to listen on stdin and react accordingly */
231 start_child(command);
233 /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
234 * We only need those watchers on the stack, so putting them on the stack saves us
235 * some calls to free() */
236 ev_signal sig_term, sig_int, sig_hup;
238 ev_signal_init(&sig_term, &sig_cb, SIGTERM);
239 ev_signal_init(&sig_int, &sig_cb, SIGINT);
240 ev_signal_init(&sig_hup, &sig_cb, SIGHUP);
242 ev_signal_start(main_loop, &sig_term);
243 ev_signal_start(main_loop, &sig_int);
244 ev_signal_start(main_loop, &sig_hup);
246 /* From here on everything should run smooth for itself, just start listening for
247 * events. We stop simply stop the event-loop, when we are finished */
248 ev_loop(main_loop, 0);
255 ev_default_destroy();
258 FREE_SLIST(outputs, i3_output);