]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
Merge branch 'bar-config' into next
[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 = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
34     globfree(&globbuf);
35     return result;
36 }
37
38 void print_usage(char *elf_name) {
39     printf("Usage: %s [-b bar_id] [-s sock_path] [-h] [-v]\n", elf_name);
40     printf("\n");
41     printf("-b <bar_id>\tBar ID for which to get the configuration\n");
42     printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
43     printf("-h\t\tDisplay this help-message and exit\n");
44     printf("-v\t\tDisplay version number and exit\n");
45     printf("\n");
46     printf(" PLEASE NOTE that i3bar will be automatically started by i3\n"
47            " as soon as there is a 'bar' configuration block in your\n"
48            " config file. You should never need to start it manually.\n");
49     printf("\n");
50 }
51
52 /*
53  * We watch various signals, that are there to make our application stop.
54  * If we get one of those, we ev_unloop() and invoke the cleanup-routines
55  * in main() with that
56  *
57  */
58 void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
59     switch (watcher->signum) {
60         case SIGTERM:
61             DLOG("Got a SIGTERM, stopping\n");
62             break;
63         case SIGINT:
64             DLOG("Got a SIGINT, stopping\n");
65             break;
66         case SIGHUP:
67             DLOG("Got a SIGHUP, stopping\n");
68     }
69     ev_unloop(main_loop, EVUNLOOP_ALL);
70 }
71
72 int main(int argc, char **argv) {
73     int opt;
74     int option_index = 0;
75     char *socket_path = getenv("I3SOCK");
76     char *i3_default_sock_path = "/tmp/i3-ipc.sock";
77
78     /* Initialize the standard config to use 0 as default */
79     memset(&config, '\0', sizeof(config_t));
80
81     static struct option long_opt[] = {
82         { "socket",               required_argument, 0, 's' },
83         { "bar_id",               required_argument, 0, 0 },
84         { "help",                 no_argument,       0, 'h' },
85         { "version",              no_argument,       0, 'v' },
86         { NULL,                   0,                 0, 0}
87     };
88
89     while ((opt = getopt_long(argc, argv, "s:hv", long_opt, &option_index)) != -1) {
90         switch (opt) {
91             case 's':
92                 socket_path = expand_path(optarg);
93                 break;
94             case 'v':
95                 printf("i3bar version " I3_VERSION " © 2010-2011 Axel Wagner and contributors\n");
96                 exit(EXIT_SUCCESS);
97                 break;
98             case 0:
99                 if (!strcmp(long_opt[option_index].name, "bar_id")) {
100                     FREE(config.bar_id);
101                     config.bar_id = sstrdup(optarg);
102                 }
103                 break;
104             default:
105                 print_usage(argv[0]);
106                 exit(EXIT_SUCCESS);
107                 break;
108         }
109     }
110
111     if (!config.bar_id) {
112         /* TODO: maybe we want -f which will automatically ask i3 for the first
113          * configured bar (and error out if there are too many)? */
114         ELOG("No bar_id passed. Please let i3 start i3bar or specify --bar_id\n");
115         exit(EXIT_FAILURE);
116     }
117
118     main_loop = ev_default_loop(0);
119
120     char *atom_sock_path = init_xcb_early();
121
122     if (socket_path == NULL) {
123         socket_path = atom_sock_path;
124     }
125
126     if (socket_path == NULL) {
127         ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
128         socket_path = expand_path(i3_default_sock_path);
129     }
130
131     init_outputs();
132     if (init_connection(socket_path)) {
133         /* Request the bar configuration. When it arrives, we fill the config array. */
134         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id);
135     }
136
137     /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
138      * We only need those watchers on the stack, so putting them on the stack saves us
139      * some calls to free() */
140     ev_signal *sig_term = smalloc(sizeof(ev_signal));
141     ev_signal *sig_int = smalloc(sizeof(ev_signal));
142     ev_signal *sig_hup = smalloc(sizeof(ev_signal));
143
144     ev_signal_init(sig_term, &sig_cb, SIGTERM);
145     ev_signal_init(sig_int, &sig_cb, SIGINT);
146     ev_signal_init(sig_hup, &sig_cb, SIGHUP);
147
148     ev_signal_start(main_loop, sig_term);
149     ev_signal_start(main_loop, sig_int);
150     ev_signal_start(main_loop, sig_hup);
151
152     /* From here on everything should run smooth for itself, just start listening for
153      * events. We stop simply stop the event-loop, when we are finished */
154     ev_loop(main_loop, 0);
155
156     kill_child();
157
158     FREE(statusline_buffer);
159
160     clean_xcb();
161     ev_default_destroy();
162
163     free_workspaces();
164
165     return 0;
166 }