]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
bfccb8edd93c4ec7a4e9fce50d00ecb05c442ef9
[i3/i3] / i3bar / src / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3bar - an xcb-based status- and ws-bar for i3
5  *
6  * © 2010-2011 Axel Wagner and contributors
7  *
8  * See file LICNSE for license information
9  *
10  */
11 #include <stdio.h>
12 #include <i3/ipc.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <ev.h>
18 #include <getopt.h>
19 #include <glob.h>
20
21 #include "common.h"
22
23 /*
24  * Glob path, i.e. expand ~
25  *
26  */
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");
31         exit(EXIT_FAILURE);
32     }
33     char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
34     if (result == NULL) {
35         ELOG("malloc() failed: %s\n", strerror(errno));
36         exit(EXIT_FAILURE);
37     }
38     globfree(&globbuf);
39     return result;
40 }
41
42 static void read_color(char **color) {
43     int len = strlen(optarg);
44     if (len == 6 || (len == 7 && optarg[0] == '#')) {
45         int offset = len - 6;
46         int good = 1, i;
47         for (i = offset; good && i < 6 + offset; ++i) {
48             char c = optarg[i];
49             if (!(c >= 'a' && c <= 'f')
50                     && !(c >= 'A' && c <= 'F')
51                     && !(c >= '0' && c <= '9')) {
52                 good = 0;
53                 break;
54             }
55         }
56         if (good) {
57             *color = strdup(optarg + offset);
58             return;
59         }
60     }
61
62     fprintf(stderr, "Bad color value \"%s\"\n", optarg);
63     exit(EXIT_FAILURE);
64 }
65
66 static void free_colors(struct xcb_color_strings_t *colors) {
67 #define FREE_COLOR(x) \
68     do { \
69         if (colors->x) \
70             free(colors->x); \
71     } while (0)
72     FREE_COLOR(bar_fg);
73     FREE_COLOR(bar_bg);
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);
82 #undef FREE_COLOR
83 }
84
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");
97 }
98
99 /*
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
103  *
104  */
105 void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
106     switch (watcher->signum) {
107         case SIGTERM:
108             DLOG("Got a SIGTERM, stopping\n");
109             break;
110         case SIGINT:
111             DLOG("Got a SIGINT, stopping\n");
112             break;
113         case SIGHUP:
114             DLOG("Got a SIGHUP, stopping\n");
115     }
116     ev_unloop(main_loop, EVUNLOOP_ALL);
117 }
118
119 int main(int argc, char **argv) {
120     int opt;
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, };
127
128     /* Definition of the standard-config */
129     config.hide_on_modifier = 0;
130     config.dockpos = DOCKPOS_NONE;
131     config.disable_ws = 0;
132
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' },
153         { NULL,                   0,                 0, 0}
154     };
155
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) {
157         switch (opt) {
158             case 's':
159                 socket_path = expand_path(optarg);
160                 break;
161             case 'c':
162                 command = strdup(optarg);
163                 break;
164             case 'm':
165                 config.hide_on_modifier = 1;
166                 break;
167             case 'd':
168                 config.hide_on_modifier = 0;
169                 if (optarg == NULL) {
170                     config.dockpos = DOCKPOS_BOT;
171                     break;
172                 }
173                 if (!strcmp(optarg, "top")) {
174                     config.dockpos = DOCKPOS_TOP;
175                 } else if (!strcmp(optarg, "bottom")) {
176                     config.dockpos = DOCKPOS_BOT;
177                 } else {
178                     print_usage(argv[0]);
179                     exit(EXIT_FAILURE);
180                 }
181                 break;
182             case 'f':
183                 fontname = strdup(optarg);
184                 break;
185             case 'w':
186                 config.disable_ws = 1;
187                 break;
188             case 'v':
189                 printf("i3bar version " I3_VERSION " © 2010-2011 Axel Wagner and contributors\n");
190                 exit(EXIT_SUCCESS);
191                 break;
192             case 'V':
193                 config.verbose = 1;
194                 break;
195             case 'A':
196                 read_color(&colors.bar_fg);
197                 break;
198             case 'B':
199                 read_color(&colors.bar_bg);
200                 break;
201             case 'C':
202                 read_color(&colors.active_ws_fg);
203                 break;
204             case 'D':
205                 read_color(&colors.active_ws_bg);
206                 break;
207             case 'E':
208                 read_color(&colors.inactive_ws_fg);
209                 break;
210             case 'F':
211                 read_color(&colors.inactive_ws_bg);
212                 break;
213             case 'G':
214                 read_color(&colors.urgent_ws_bg);
215                 break;
216             case 'H':
217                 read_color(&colors.urgent_ws_fg);
218                 break;
219             case 'I':
220                 read_color(&colors.focus_ws_bg);
221                 break;
222             case 'J':
223                 read_color(&colors.focus_ws_fg);
224                 break;
225             default:
226                 print_usage(argv[0]);
227                 exit(EXIT_SUCCESS);
228                 break;
229         }
230     }
231
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";
237     }
238
239     if (config.dockpos != DOCKPOS_NONE) {
240         if (config.hide_on_modifier) {
241             ELOG("--dock and --hide are mutually exclusive!\n");
242             exit(EXIT_FAILURE);
243         }
244     } else {
245         config.hide_on_modifier = 1;
246     }
247
248     main_loop = ev_default_loop(0);
249
250     init_colors(&colors);
251     char *atom_sock_path = init_xcb(fontname);
252
253     if (socket_path == NULL) {
254         socket_path = atom_sock_path;
255     }
256
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);
260     }
261
262     free_colors(&colors);
263
264     init_outputs();
265     if (init_connection(socket_path)) {
266         /* We subscribe to the i3-events we need */
267         subscribe_events();
268
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);
275         }
276     }
277
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);
281     FREE(command);
282
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));
289
290     if (sig_term == NULL || sig_int == NULL || sig_hup == NULL) {
291         ELOG("malloc() failed: %s\n", strerror(errno));
292         exit(EXIT_FAILURE);
293     }
294
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);
298
299     ev_signal_start(main_loop, sig_term);
300     ev_signal_start(main_loop, sig_int);
301     ev_signal_start(main_loop, sig_hup);
302
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);
306
307     kill_child();
308
309     FREE(statusline_buffer);
310
311     clean_xcb();
312     ev_default_destroy();
313
314     free_workspaces();
315
316     return 0;
317 }