]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
Merge i3bar into next
[i3/i3] / i3bar / src / main.c
1 /*
2  * i3bar - an xcb-based status- and ws-bar for i3
3  *
4  * © 2010-2011 Axel Wagner and contributors
5  *
6  * See file LICNSE for license information
7  *
8  */
9 #include <stdio.h>
10 #include <i3/ipc.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <ev.h>
16 #include <getopt.h>
17 #include <glob.h>
18
19 #include "common.h"
20
21 /*
22  * Glob path, i.e. expand ~
23  *
24  */
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");
29         exit(EXIT_FAILURE);
30     }
31     char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
32     if (result == NULL) {
33         ELOG("malloc() failed: %s\n", strerror(errno));
34         exit(EXIT_FAILURE);
35     }
36     globfree(&globbuf);
37     return result;
38 }
39
40 static void read_color(char **color) {
41     int len = strlen(optarg);
42     if (len == 6 || (len == 7 && optarg[0] == '#')) {
43         int offset = len - 6;
44         int good = 1, i;
45         for (i = offset; good && i < 6 + offset; ++i) {
46             char c = optarg[i];
47             if (!(c >= 'a' && c <= 'f')
48                     && !(c >= 'A' && c <= 'F')
49                     && !(c >= '0' && c <= '9')) {
50                 good = 0;
51                 break;
52             }
53         }
54         if (good) {
55             *color = strdup(optarg + offset);
56             return;
57         }
58     }
59
60     fprintf(stderr, "Bad color value \"%s\"\n", optarg);
61     exit(EXIT_FAILURE);
62 }
63
64 static void free_colors(struct xcb_color_strings_t *colors) {
65 #define FREE_COLOR(x) \
66     do { \
67         if (colors->x) \
68             free(colors->x); \
69     } while (0)
70     FREE_COLOR(bar_fg);
71     FREE_COLOR(bar_bg);
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);
78     FREE_COLOR(focus_ws_fg);
79     FREE_COLOR(focus_ws_bg);
80 #undef FREE_COLOR
81 }
82
83 void print_usage(char *elf_name) {
84     printf("Usage: %s [-s sock_path] [-c command] [-m|-d[pos]] [-f font] [-V] [-h]\n", elf_name);
85     printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
86     printf("-c <command>\tExecute <command> to get stdin\n");
87     printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
88     printf("-d[<pos>]\tEnable dockmode. <pos> is \"top\" or \"bottom\". Default is bottom\n");
89     printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
90     printf("\t\tand a SIGCONT on unhiding of the bars\n");
91     printf("-f <font>\tUse X-Core-Font <font> for display\n");
92     printf("-w\t\tDisable workspace-buttons\n");
93     printf("-V\t\tBe (very) verbose with the debug-output\n");
94     printf("-h\t\tDisplay this help-message and exit\n");
95 }
96
97 /*
98  * We watch various signals, that are there to make our application stop.
99  * If we get one of those, we ev_unloop() and invoke the cleanup-routines
100  * in main() with that
101  *
102  */
103 void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
104     switch (watcher->signum) {
105         case SIGTERM:
106             DLOG("Got a SIGTERM, stopping\n");
107             break;
108         case SIGINT:
109             DLOG("Got a SIGINT, stopping\n");
110             break;
111         case SIGHUP:
112             DLOG("Got a SIGHUP, stopping\n");
113     }
114     ev_unloop(main_loop, EVUNLOOP_ALL);
115 }
116
117 int main(int argc, char **argv) {
118     int opt;
119     int option_index = 0;
120     char *socket_path = getenv("I3SOCK");
121     char *command = NULL;
122     char *fontname = NULL;
123     char *i3_default_sock_path = "/tmp/i3-ipc.sock";
124     struct xcb_color_strings_t colors = { NULL, };
125
126     /* Definition of the standard-config */
127     config.hide_on_modifier = 0;
128     config.dockpos = DOCKPOS_NONE;
129     config.disable_ws = 0;
130
131     static struct option long_opt[] = {
132         { "socket",               required_argument, 0, 's' },
133         { "command",              required_argument, 0, 'c' },
134         { "hide",                 no_argument,       0, 'm' },
135         { "dock",                 optional_argument, 0, 'd' },
136         { "font",                 required_argument, 0, 'f' },
137         { "nows",                 no_argument,       0, 'w' },
138         { "help",                 no_argument,       0, 'h' },
139         { "version",              no_argument,       0, 'v' },
140         { "verbose",              no_argument,       0, 'V' },
141         { "color-bar-fg",         required_argument, 0, 'A' },
142         { "color-bar-bg",         required_argument, 0, 'B' },
143         { "color-active-ws-fg",   required_argument, 0, 'C' },
144         { "color-active-ws-bg",   required_argument, 0, 'D' },
145         { "color-inactive-ws-fg", required_argument, 0, 'E' },
146         { "color-inactive-ws-bg", required_argument, 0, 'F' },
147         { "color-urgent-ws-bg",   required_argument, 0, 'G' },
148         { "color-urgent-ws-fg",   required_argument, 0, 'H' },
149         { "color-focus-ws-bg",    required_argument, 0, 'I' },
150         { "color-focus-ws-fg",    required_argument, 0, 'J' },
151         { NULL,                   0,                 0, 0}
152     };
153
154     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) {
155         switch (opt) {
156             case 's':
157                 socket_path = expand_path(optarg);
158                 break;
159             case 'c':
160                 command = strdup(optarg);
161                 break;
162             case 'm':
163                 config.hide_on_modifier = 1;
164                 break;
165             case 'd':
166                 config.hide_on_modifier = 0;
167                 if (optarg == NULL) {
168                     config.dockpos = DOCKPOS_BOT;
169                     break;
170                 }
171                 if (!strcmp(optarg, "top")) {
172                     config.dockpos = DOCKPOS_TOP;
173                 } else if (!strcmp(optarg, "bottom")) {
174                     config.dockpos = DOCKPOS_BOT;
175                 } else {
176                     print_usage(argv[0]);
177                     exit(EXIT_FAILURE);
178                 }
179                 break;
180             case 'f':
181                 fontname = strdup(optarg);
182                 break;
183             case 'w':
184                 config.disable_ws = 1;
185                 break;
186             case 'v':
187                 printf("i3bar version " I3BAR_VERSION " © 2010-2011 Axel Wagner and contributors\n");
188                 exit(EXIT_SUCCESS);
189                 break;
190             case 'V':
191                 config.verbose = 1;
192                 break;
193             case 'A':
194                 read_color(&colors.bar_fg);
195                 break;
196             case 'B':
197                 read_color(&colors.bar_bg);
198                 break;
199             case 'C':
200                 read_color(&colors.active_ws_fg);
201                 break;
202             case 'D':
203                 read_color(&colors.active_ws_bg);
204                 break;
205             case 'E':
206                 read_color(&colors.inactive_ws_fg);
207                 break;
208             case 'F':
209                 read_color(&colors.inactive_ws_bg);
210                 break;
211             case 'G':
212                 read_color(&colors.urgent_ws_bg);
213                 break;
214             case 'H':
215                 read_color(&colors.urgent_ws_fg);
216                 break;
217             case 'I':
218                 read_color(&colors.focus_ws_bg);
219                 break;
220             case 'J':
221                 read_color(&colors.focus_ws_fg);
222                 break;
223             default:
224                 print_usage(argv[0]);
225                 exit(EXIT_SUCCESS);
226                 break;
227         }
228     }
229
230     if (fontname == NULL) {
231         /* This is a very restrictive default. More sensefull would be something like
232          * "-misc-*-*-*-*--*-*-*-*-*-*-*-*". But since that produces very ugly results
233          * on my machine, let's stick with this until we have a configfile */
234         fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
235     }
236
237     if (config.dockpos != DOCKPOS_NONE) {
238         if (config.hide_on_modifier) {
239             ELOG("--dock and --hide are mutually exclusive!\n");
240             exit(EXIT_FAILURE);
241         }
242     } else {
243         config.hide_on_modifier = 1;
244     }
245
246     main_loop = ev_default_loop(0);
247
248     init_colors(&colors);
249     char *atom_sock_path = init_xcb(fontname);
250
251     if (socket_path == NULL) {
252         socket_path = atom_sock_path;
253     }
254
255     if (socket_path == NULL) {
256         ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
257         socket_path = expand_path(i3_default_sock_path);
258     }
259
260     free_colors(&colors);
261
262     init_outputs();
263     if (init_connection(socket_path)) {
264         /* We subscribe to the i3-events we need */
265         subscribe_events();
266
267         /* We initiate the main-function by requesting infos about the outputs and
268          * workspaces. Everything else (creating the bars, showing the right workspace-
269          * buttons and more) is taken care of by the event-driveniness of the code */
270         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
271         if (!config.disable_ws) {
272             i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
273         }
274     }
275
276     /* The name of this function is actually misleading. Even if no -c is specified,
277      * this function initiates the watchers to listen on stdin and react accordingly */
278     start_child(command);
279     FREE(command);
280
281     /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
282      * We only need those watchers on the stack, so putting them on the stack saves us
283      * some calls to free() */
284     ev_signal *sig_term = malloc(sizeof(ev_signal));
285     ev_signal *sig_int = malloc(sizeof(ev_signal));
286     ev_signal *sig_hup = malloc(sizeof(ev_signal));
287
288     if (sig_term == NULL || sig_int == NULL || sig_hup == NULL) {
289         ELOG("malloc() failed: %s\n", strerror(errno));
290         exit(EXIT_FAILURE);
291     }
292
293     ev_signal_init(sig_term, &sig_cb, SIGTERM);
294     ev_signal_init(sig_int, &sig_cb, SIGINT);
295     ev_signal_init(sig_hup, &sig_cb, SIGHUP);
296
297     ev_signal_start(main_loop, sig_term);
298     ev_signal_start(main_loop, sig_int);
299     ev_signal_start(main_loop, sig_hup);
300
301     /* From here on everything should run smooth for itself, just start listening for
302      * events. We stop simply stop the event-loop, when we are finished */
303     ev_loop(main_loop, 0);
304
305     kill_child();
306
307     FREE(statusline_buffer);
308
309     clean_xcb();
310     ev_default_destroy();
311
312     free_workspaces();
313
314     return 0;
315 }