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