]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
Bugfix: Don't SIGSTOP child in dockmode
[i3/i3] / i3bar / src / main.c
1 /*
2  * i3bar - an xcb-based status- and ws-bar for i3
3  *
4  * © 2010 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 #undef FREE_COLOR
79 }
80
81 void print_usage(char *elf_name) {
82     printf("Usage: %s [-s sock_path] [-c command] [-m|-d[pos]] [-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("-d[<pos>]\tEnable dockmode. <pos> is \"top\" or \"bottom\". Default is bottom\n");
87     printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
88     printf("\t\tand a SIGCONT on unhiding of the bars\n");
89     printf("-f <font>\tUse X-Core-Font <font> for display\n");
90     printf("-V\t\tBe (very) verbose with the debug-output\n");
91     printf("-h\t\tDisplay this help-message and exit\n");
92 }
93
94 /*
95  * We watch various signals, that are there to make our application stop.
96  * If we get one of those, we ev_unloop() and invoke the cleanup-routines
97  * in main() with that
98  *
99  */
100 void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
101     switch (watcher->signum) {
102         case SIGTERM:
103             DLOG("Got a SIGTERM, stopping\n");
104             break;
105         case SIGINT:
106             DLOG("Got a SIGINT, stopping\n");
107             break;
108         case SIGHUP:
109             DLOG("Got a SIGHUP, stopping\n");
110     }
111     ev_unloop(main_loop, EVUNLOOP_ALL);
112 }
113
114 int main(int argc, char **argv) {
115     int opt;
116     int option_index = 0;
117     char *socket_path = getenv("I3SOCK");
118     char *command = NULL;
119     char *fontname = NULL;
120     char *i3_default_sock_path = "/tmp/i3-ipc.sock";
121     struct xcb_color_strings_t colors = { NULL, };
122
123     /* Definition of the standard-config */
124     config.hide_on_modifier = 0;
125     config.dockpos = DOCKPOS_NONE;
126
127     static struct option long_opt[] = {
128         { "socket",               required_argument, 0, 's' },
129         { "command",              required_argument, 0, 'c' },
130         { "hide",                 no_argument,       0, 'm' },
131         { "dock",                 optional_argument, 0, 'd' },
132         { "font",                 required_argument, 0, 'f' },
133         { "help",                 no_argument,       0, 'h' },
134         { "version",              no_argument,       0, 'v' },
135         { "verbose",              no_argument,       0, 'V' },
136         { "color-bar-fg",         required_argument, 0, 'A' },
137         { "color-bar-bg",         required_argument, 0, 'B' },
138         { "color-active-ws-fg",   required_argument, 0, 'C' },
139         { "color-active-ws-bg",   required_argument, 0, 'D' },
140         { "color-inactive-ws-fg", required_argument, 0, 'E' },
141         { "color-inactive-ws-bg", required_argument, 0, 'F' },
142         { "color-urgent-ws-bg",   required_argument, 0, 'G' },
143         { "color-urgent-ws-fg",   required_argument, 0, 'H' },
144         { NULL,                   0,                 0, 0}
145     };
146
147     while ((opt = getopt_long(argc, argv, "s:c:d::mf:hvVA:B:C:D:E:F:G:H:", long_opt, &option_index)) != -1) {
148         switch (opt) {
149             case 's':
150                 socket_path = expand_path(optarg);
151                 break;
152             case 'c':
153                 command = strdup(optarg);
154                 break;
155             case 'm':
156                 config.hide_on_modifier = 1;
157                 break;
158             case 'd':
159                 config.hide_on_modifier = 0;
160                 if (optarg == NULL) {
161                     config.dockpos = DOCKPOS_BOT;
162                     break;
163                 }
164                 if (!strcmp(optarg, "top")) {
165                     config.dockpos = DOCKPOS_TOP;
166                 } else if (!strcmp(optarg, "bottom")) {
167                     config.dockpos = DOCKPOS_BOT;
168                 } else {
169                     print_usage(argv[0]);
170                     exit(EXIT_FAILURE);
171                 }
172                 break;
173             case 'f':
174                 fontname = strdup(optarg);
175                 break;
176             case 'v':
177                 printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
178                 exit(EXIT_SUCCESS);
179                 break;
180             case 'V':
181                 config.verbose = 1;
182                 break;
183             case 'A':
184                 read_color(&colors.bar_fg);
185                 break;
186             case 'B':
187                 read_color(&colors.bar_bg);
188                 break;
189             case 'C':
190                 read_color(&colors.active_ws_fg);
191                 break;
192             case 'D':
193                 read_color(&colors.active_ws_bg);
194                 break;
195             case 'E':
196                 read_color(&colors.inactive_ws_fg);
197                 break;
198             case 'F':
199                 read_color(&colors.inactive_ws_bg);
200                 break;
201             case 'G':
202                 read_color(&colors.urgent_ws_bg);
203                 break;
204             case 'H':
205                 read_color(&colors.urgent_ws_fg);
206                 break;
207             default:
208                 print_usage(argv[0]);
209                 exit(EXIT_SUCCESS);
210                 break;
211         }
212     }
213
214     if (fontname == NULL) {
215         /* This is a very restrictive default. More sensefull would be something like
216          * "-misc-*-*-*-*--*-*-*-*-*-*-*-*". But since that produces very ugly results
217          * on my machine, let's stick with this until we have a configfile */
218         fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
219     }
220
221     if (socket_path == NULL) {
222         ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
223         socket_path = expand_path(i3_default_sock_path);
224     }
225
226     if (config.dockpos != DOCKPOS_NONE) {
227         if (config.hide_on_modifier) {
228             ELOG("--dock and --hide are mutually exclusive!\n");
229             exit(EXIT_FAILURE);
230         }
231     } else {
232         config.hide_on_modifier = 1;
233     }
234
235     main_loop = ev_default_loop(0);
236
237     init_colors(&colors);
238     init_xcb(fontname);
239
240     free_colors(&colors);
241
242     init_outputs();
243     init_connection(socket_path);
244
245     /* We subscribe to the i3-events we need */
246     subscribe_events();
247
248     /* We initiate the main-function by requesting infos about the outputs and
249      * workspaces. Everything else (creating the bars, showing the right workspace-
250      * buttons and more) is taken care of by the event-driveniness of the code */
251     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
252     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
253
254     /* The name of this function is actually misleading. Even if no -c is specified,
255      * this function initiates the watchers to listen on stdin and react accordingly */
256     start_child(command);
257     FREE(command);
258
259     /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
260      * We only need those watchers on the stack, so putting them on the stack saves us
261      * some calls to free() */
262     ev_signal sig_term, sig_int, sig_hup;
263
264     ev_signal_init(&sig_term, &sig_cb, SIGTERM);
265     ev_signal_init(&sig_int, &sig_cb, SIGINT);
266     ev_signal_init(&sig_hup, &sig_cb, SIGHUP);
267
268     ev_signal_start(main_loop, &sig_term);
269     ev_signal_start(main_loop, &sig_int);
270     ev_signal_start(main_loop, &sig_hup);
271
272     /* From here on everything should run smooth for itself, just start listening for
273      * events. We stop simply stop the event-loop, when we are finished */
274     ev_loop(main_loop, 0);
275
276     kill_child();
277
278     FREE(statusline_buffer);
279
280     clean_xcb();
281     ev_default_destroy();
282
283     free_workspaces();
284
285     return 0;
286 }