2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
6 * © 2010-2011 Axel Wagner and contributors
8 * See file LICNSE for license information
24 * Glob path, i.e. expand ~
27 char *expand_path(char *path) {
28 static glob_t globbuf;
29 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
30 ELOG("glob() failed\n");
33 char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
35 ELOG("malloc() failed: %s\n", strerror(errno));
42 static void read_color(char **color) {
43 int len = strlen(optarg);
44 if (len == 6 || (len == 7 && optarg[0] == '#')) {
47 for (i = offset; good && i < 6 + offset; ++i) {
49 if (!(c >= 'a' && c <= 'f')
50 && !(c >= 'A' && c <= 'F')
51 && !(c >= '0' && c <= '9')) {
57 *color = strdup(optarg + offset);
62 fprintf(stderr, "Bad color value \"%s\"\n", optarg);
66 static void free_colors(struct xcb_color_strings_t *colors) {
67 #define FREE_COLOR(x) \
74 FREE_COLOR(active_ws_fg);
75 FREE_COLOR(active_ws_bg);
76 FREE_COLOR(inactive_ws_fg);
77 FREE_COLOR(inactive_ws_bg);
78 FREE_COLOR(urgent_ws_fg);
79 FREE_COLOR(urgent_ws_bg);
80 FREE_COLOR(focus_ws_fg);
81 FREE_COLOR(focus_ws_bg);
85 void print_usage(char *elf_name) {
86 printf("Usage: %s [-s sock_path] [-c command] [-m|-d[pos]] [-f font] [-V] [-h]\n", elf_name);
87 printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
88 printf("-c <command>\tExecute <command> to get stdin\n");
89 printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
90 printf("-d[<pos>]\tEnable dockmode. <pos> is \"top\" or \"bottom\". Default is bottom\n");
91 printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
92 printf("\t\tand a SIGCONT on unhiding of the bars\n");
93 printf("-f <font>\tUse X-Core-Font <font> for display\n");
94 printf("-w\t\tDisable workspace-buttons\n");
95 printf("-V\t\tBe (very) verbose with the debug-output\n");
96 printf("-h\t\tDisplay this help-message and exit\n");
100 * We watch various signals, that are there to make our application stop.
101 * If we get one of those, we ev_unloop() and invoke the cleanup-routines
102 * in main() with that
105 void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
106 switch (watcher->signum) {
108 DLOG("Got a SIGTERM, stopping\n");
111 DLOG("Got a SIGINT, stopping\n");
114 DLOG("Got a SIGHUP, stopping\n");
116 ev_unloop(main_loop, EVUNLOOP_ALL);
119 int main(int argc, char **argv) {
121 int option_index = 0;
122 char *socket_path = getenv("I3SOCK");
123 char *command = NULL;
124 char *fontname = NULL;
125 char *i3_default_sock_path = "/tmp/i3-ipc.sock";
126 struct xcb_color_strings_t colors = { NULL, };
128 /* Definition of the standard-config */
129 config.hide_on_modifier = 0;
130 config.dockpos = DOCKPOS_NONE;
131 config.disable_ws = 0;
133 static struct option long_opt[] = {
134 { "socket", required_argument, 0, 's' },
135 { "command", required_argument, 0, 'c' },
136 { "hide", no_argument, 0, 'm' },
137 { "dock", optional_argument, 0, 'd' },
138 { "font", required_argument, 0, 'f' },
139 { "nows", no_argument, 0, 'w' },
140 { "help", no_argument, 0, 'h' },
141 { "version", no_argument, 0, 'v' },
142 { "verbose", no_argument, 0, 'V' },
143 { "color-bar-fg", required_argument, 0, 'A' },
144 { "color-bar-bg", required_argument, 0, 'B' },
145 { "color-active-ws-fg", required_argument, 0, 'C' },
146 { "color-active-ws-bg", required_argument, 0, 'D' },
147 { "color-inactive-ws-fg", required_argument, 0, 'E' },
148 { "color-inactive-ws-bg", required_argument, 0, 'F' },
149 { "color-urgent-ws-bg", required_argument, 0, 'G' },
150 { "color-urgent-ws-fg", required_argument, 0, 'H' },
151 { "color-focus-ws-bg", required_argument, 0, 'I' },
152 { "color-focus-ws-fg", required_argument, 0, 'J' },
156 while ((opt = getopt_long(argc, argv, "s:c:d::mf:whvVA:B:C:D:E:F:G:H:I:J:", long_opt, &option_index)) != -1) {
159 socket_path = expand_path(optarg);
162 command = strdup(optarg);
165 config.hide_on_modifier = 1;
168 config.hide_on_modifier = 0;
169 if (optarg == NULL) {
170 config.dockpos = DOCKPOS_BOT;
173 if (!strcmp(optarg, "top")) {
174 config.dockpos = DOCKPOS_TOP;
175 } else if (!strcmp(optarg, "bottom")) {
176 config.dockpos = DOCKPOS_BOT;
178 print_usage(argv[0]);
183 fontname = strdup(optarg);
186 config.disable_ws = 1;
189 printf("i3bar version " I3_VERSION " © 2010-2011 Axel Wagner and contributors\n");
196 read_color(&colors.bar_fg);
199 read_color(&colors.bar_bg);
202 read_color(&colors.active_ws_fg);
205 read_color(&colors.active_ws_bg);
208 read_color(&colors.inactive_ws_fg);
211 read_color(&colors.inactive_ws_bg);
214 read_color(&colors.urgent_ws_bg);
217 read_color(&colors.urgent_ws_fg);
220 read_color(&colors.focus_ws_bg);
223 read_color(&colors.focus_ws_fg);
226 print_usage(argv[0]);
232 if (fontname == NULL) {
233 /* This is a very restrictive default. More sensefull would be something like
234 * "-misc-*-*-*-*--*-*-*-*-*-*-*-*". But since that produces very ugly results
235 * on my machine, let's stick with this until we have a configfile */
236 fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
239 if (config.dockpos != DOCKPOS_NONE) {
240 if (config.hide_on_modifier) {
241 ELOG("--dock and --hide are mutually exclusive!\n");
245 config.hide_on_modifier = 1;
248 main_loop = ev_default_loop(0);
250 init_colors(&colors);
251 char *atom_sock_path = init_xcb(fontname);
253 if (socket_path == NULL) {
254 socket_path = atom_sock_path;
257 if (socket_path == NULL) {
258 ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
259 socket_path = expand_path(i3_default_sock_path);
262 free_colors(&colors);
265 if (init_connection(socket_path)) {
266 /* We subscribe to the i3-events we need */
269 /* We initiate the main-function by requesting infos about the outputs and
270 * workspaces. Everything else (creating the bars, showing the right workspace-
271 * buttons and more) is taken care of by the event-driveniness of the code */
272 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
273 if (!config.disable_ws) {
274 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
278 /* The name of this function is actually misleading. Even if no -c is specified,
279 * this function initiates the watchers to listen on stdin and react accordingly */
280 start_child(command);
283 /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
284 * We only need those watchers on the stack, so putting them on the stack saves us
285 * some calls to free() */
286 ev_signal *sig_term = malloc(sizeof(ev_signal));
287 ev_signal *sig_int = malloc(sizeof(ev_signal));
288 ev_signal *sig_hup = malloc(sizeof(ev_signal));
290 if (sig_term == NULL || sig_int == NULL || sig_hup == NULL) {
291 ELOG("malloc() failed: %s\n", strerror(errno));
295 ev_signal_init(sig_term, &sig_cb, SIGTERM);
296 ev_signal_init(sig_int, &sig_cb, SIGINT);
297 ev_signal_init(sig_hup, &sig_cb, SIGHUP);
299 ev_signal_start(main_loop, sig_term);
300 ev_signal_start(main_loop, sig_int);
301 ev_signal_start(main_loop, sig_hup);
303 /* From here on everything should run smooth for itself, just start listening for
304 * events. We stop simply stop the event-loop, when we are finished */
305 ev_loop(main_loop, 0);
309 FREE(statusline_buffer);
312 ev_default_destroy();