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