2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
6 * © 2010-2011 Axel Wagner and contributors
8 * See file LICNSE for license information
25 * Glob path, i.e. expand ~
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");
34 char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
36 ELOG("malloc() failed: %s\n", strerror(errno));
43 void print_usage(char *elf_name) {
44 printf("Usage: %s [-s sock_path] [-h] [-v]\n", elf_name);
45 printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
46 printf("-h\t\tDisplay this help-message and exit\n");
47 printf("-v\t\tDisplay version number and exit\n");
51 * We watch various signals, that are there to make our application stop.
52 * If we get one of those, we ev_unloop() and invoke the cleanup-routines
56 void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
57 switch (watcher->signum) {
59 DLOG("Got a SIGTERM, stopping\n");
62 DLOG("Got a SIGINT, stopping\n");
65 DLOG("Got a SIGHUP, stopping\n");
67 ev_unloop(main_loop, EVUNLOOP_ALL);
70 int main(int argc, char **argv) {
73 char *socket_path = getenv("I3SOCK");
74 char *i3_default_sock_path = "/tmp/i3-ipc.sock";
76 /* Initialize the standard config to use 0 as default */
77 memset(&config, '\0', sizeof(config_t));
79 static struct option long_opt[] = {
80 { "socket", required_argument, 0, 's' },
81 { "bar_id", required_argument, 0, 0 },
82 { "help", no_argument, 0, 'h' },
83 { "version", no_argument, 0, 'v' },
87 while ((opt = getopt_long(argc, argv, "s:hv", long_opt, &option_index)) != -1) {
90 socket_path = expand_path(optarg);
93 printf("i3bar version " I3_VERSION " © 2010-2011 Axel Wagner and contributors\n");
97 if (!strcmp(long_opt[option_index].name, "bar_id")) {
99 config.bar_id = sstrdup(optarg);
103 print_usage(argv[0]);
109 if (!config.bar_id) {
110 /* TODO: maybe we want -f which will automatically ask i3 for the first
111 * configured bar (and error out if there are too many)? */
112 ELOG("No bar_id passed. Please let i3 start i3bar or specify --bar_id\n");
116 main_loop = ev_default_loop(0);
118 char *atom_sock_path = init_xcb_early();
120 if (socket_path == NULL) {
121 socket_path = atom_sock_path;
124 if (socket_path == NULL) {
125 ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
126 socket_path = expand_path(i3_default_sock_path);
130 if (init_connection(socket_path)) {
131 /* Request the bar configuration. When it arrives, we fill the config array. */
132 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id);
135 /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
136 * We only need those watchers on the stack, so putting them on the stack saves us
137 * some calls to free() */
138 ev_signal *sig_term = malloc(sizeof(ev_signal));
139 ev_signal *sig_int = malloc(sizeof(ev_signal));
140 ev_signal *sig_hup = malloc(sizeof(ev_signal));
142 if (sig_term == NULL || sig_int == NULL || sig_hup == NULL) {
143 ELOG("malloc() failed: %s\n", strerror(errno));
147 ev_signal_init(sig_term, &sig_cb, SIGTERM);
148 ev_signal_init(sig_int, &sig_cb, SIGINT);
149 ev_signal_init(sig_hup, &sig_cb, SIGHUP);
151 ev_signal_start(main_loop, sig_term);
152 ev_signal_start(main_loop, sig_int);
153 ev_signal_start(main_loop, sig_hup);
155 /* From here on everything should run smooth for itself, just start listening for
156 * events. We stop simply stop the event-loop, when we are finished */
157 ev_loop(main_loop, 0);
161 FREE(statusline_buffer);
164 ev_default_destroy();