]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
Call init_colors() earlier
[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\n");
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] [-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");
91 }
92
93 int main(int argc, char **argv) {
94     int opt;
95     int option_index = 0;
96     char *socket_path = NULL;
97     char *command = NULL;
98     char *fontname = NULL;
99     char *i3_default_sock_path = "~/.i3/ipc.sock";
100     struct xcb_color_strings_t colors = { NULL, };
101
102     /* Definition of the standard-config */
103     config.hide_on_modifier = 0;
104
105     static struct option long_opt[] = {
106         { "socket",               required_argument, 0, 's' },
107         { "command",              required_argument, 0, 'c' },
108         { "hide",                 no_argument,       0, 'm' },
109         { "font",                 required_argument, 0, 'f' },
110         { "help",                 no_argument,       0, 'h' },
111         { "version",              no_argument,       0, 'v' },
112         { "verbose",              no_argument,       0, 'V' },
113         { "color-bar-fg",         required_argument, 0, 'A' },
114         { "color-bar-bg",         required_argument, 0, 'B' },
115         { "color-active-ws-fg",   required_argument, 0, 'C' },
116         { "color-active-ws-bg",   required_argument, 0, 'D' },
117         { "color-inactive-ws-fg", required_argument, 0, 'E' },
118         { "color-inactive-ws-bg", required_argument, 0, 'F' },
119         { "color-urgent-ws-bg",   required_argument, 0, 'G' },
120         { "color-urgent-ws-fg",   required_argument, 0, 'H' },
121         { NULL,                   0,                 0, 0}
122     };
123
124     while ((opt = getopt_long(argc, argv, "s:c:mf:hvV:A:B:C:D:E:F:G:H:", long_opt, &option_index)) != -1) {
125         switch (opt) {
126             case 's':
127                 socket_path = expand_path(optarg);
128                 break;
129             case 'c':
130                 command = strdup(optarg);
131                 break;
132             case 'm':
133                 config.hide_on_modifier = 1;
134                 break;
135             case 'f':
136                 fontname = strdup(optarg);
137                 break;
138             case 'v':
139                 printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
140                 exit(EXIT_SUCCESS);
141                 break;
142             case 'V':
143                 config.verbose = 1;
144                 break;
145             case 'A':
146                 read_color(&colors.bar_fg);
147                 break;
148             case 'B':
149                 read_color(&colors.bar_bg);
150                 break;
151             case 'C':
152                 read_color(&colors.active_ws_fg);
153                 break;
154             case 'D':
155                 read_color(&colors.active_ws_bg);
156                 break;
157             case 'E':
158                 read_color(&colors.inactive_ws_fg);
159                 break;
160             case 'F':
161                 read_color(&colors.inactive_ws_bg);
162                 break;
163             case 'G':
164                 read_color(&colors.urgent_ws_fg);
165                 break;
166             case 'H':
167                 read_color(&colors.urgent_ws_bg);
168                 break;
169             default:
170                 print_usage(argv[0]);
171                 exit(EXIT_SUCCESS);
172                 break;
173         }
174     }
175
176     if (fontname == NULL) {
177         /* This is a very restrictive default. More sensefull would be something like
178          * "-misc-*-*-*-*--*-*-*-*-*-*-*-*". But since that produces very ugly results
179          * on my machine, let's stick with this until we have a configfile */
180         fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
181     }
182
183     if (socket_path == NULL) {
184         ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
185         socket_path = expand_path(i3_default_sock_path);
186     }
187
188     main_loop = ev_default_loop(0);
189
190     init_colors(&colors);
191     init_xcb(fontname);
192
193     free_colors(&colors);
194
195     init_outputs();
196     init_connection(socket_path);
197
198     FREE(socket_path);
199
200     /* We subscribe to the i3-events we need */
201     subscribe_events();
202
203     /* We initiate the main-function by requesting infos about the outputs and
204      * workspaces. Everything else (creating the bars, showing the right workspace-
205      * buttons and more) is taken care of by the event-driveniness of the code */
206     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
207     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
208
209     /* The name of this function is actually misleading. Even if no -c is specified,
210      * this function initiates the watchers to listen on stdin and react accordingly */
211     start_child(command);
212
213     /* From here on everything should run smooth for itself, just start listening for
214      * events. We stop simply stop the event-loop, when we are finished */
215     ev_loop(main_loop, 0);
216
217     kill_child();
218
219     FREE(statusline);
220
221     clean_xcb();
222     ev_default_destroy();
223
224     free_workspaces();
225     FREE_SLIST(outputs, i3_output);
226
227     return 0;
228 }