2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010 Axel Wagner and contributors (see also: LICENSE)
21 * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
24 void verboselog(char *fmt, ...) {
28 vfprintf(stdout, fmt, args);
32 void errorlog(char *fmt, ...) {
36 vfprintf(stderr, fmt, args);
40 void debuglog(char *fmt, ...) {
44 * Glob path, i.e. expand ~
47 static char *expand_path(char *path) {
48 static glob_t globbuf;
49 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
50 ELOG("glob() failed\n");
53 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
58 static void print_usage(char *elf_name) {
59 printf("Usage: %s -b bar_id [-s sock_path] [-h] [-v]\n", elf_name);
61 printf("-b, --bar_id <bar_id>\tBar ID for which to get the configuration\n");
62 printf("-s, --socket <sock_path>\tConnect to i3 via <sock_path>\n");
63 printf("-h, --help Display this help message and exit\n");
64 printf("-v, --version Display version number and exit\n");
65 printf("-V, --verbose Enable verbose mode\n");
67 printf(" PLEASE NOTE that i3bar will be automatically started by i3\n"
68 " as soon as there is a 'bar' configuration block in your\n"
69 " config file. You should never need to start it manually.\n");
74 * We watch various signals, that are there to make our application stop.
75 * If we get one of those, we ev_unloop() and invoke the cleanup routines
79 static void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
80 switch (watcher->signum) {
82 DLOG("Got a SIGTERM, stopping\n");
85 DLOG("Got a SIGINT, stopping\n");
88 DLOG("Got a SIGHUP, stopping\n");
90 ev_unloop(main_loop, EVUNLOOP_ALL);
93 int main(int argc, char **argv) {
96 char *socket_path = getenv("I3SOCK");
97 if (socket_path != NULL) {
98 socket_path = sstrdup(socket_path);
100 char *i3_default_sock_path = "/tmp/i3-ipc.sock";
102 /* Initialize the standard config to use 0 as default */
103 memset(&config, '\0', sizeof(config_t));
105 static struct option long_opt[] = {
106 {"socket", required_argument, 0, 's'},
107 {"bar_id", required_argument, 0, 'b'},
108 {"help", no_argument, 0, 'h'},
109 {"version", no_argument, 0, 'v'},
110 {"verbose", no_argument, 0, 'V'},
113 while ((opt = getopt_long(argc, argv, "b:s:hvV", long_opt, &option_index)) != -1) {
116 socket_path = expand_path(optarg);
119 printf("i3bar version " I3_VERSION " © 2010 Axel Wagner and contributors\n");
123 config.bar_id = sstrdup(optarg);
126 config.verbose = true;
129 print_usage(argv[0]);
135 if (!config.bar_id) {
136 /* TODO: maybe we want -f which will automatically ask i3 for the first
137 * configured bar (and error out if there are too many)? */
138 ELOG("No bar_id passed. Please let i3 start i3bar or specify --bar_id\n");
142 main_loop = ev_default_loop(0);
144 char *atom_sock_path = init_xcb_early();
146 if (socket_path == NULL) {
147 socket_path = atom_sock_path;
149 free(atom_sock_path);
152 if (socket_path == NULL) {
153 ELOG("No socket path specified, default to %s\n", i3_default_sock_path);
154 socket_path = expand_path(i3_default_sock_path);
160 if (init_connection(socket_path)) {
161 /* Request the bar configuration. When it arrives, we fill the config array. */
162 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id);
166 /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main loop.
167 * We only need those watchers on the stack, so putting them on the stack saves us
168 * some calls to free() */
169 ev_signal *sig_term = smalloc(sizeof(ev_signal));
170 ev_signal *sig_int = smalloc(sizeof(ev_signal));
171 ev_signal *sig_hup = smalloc(sizeof(ev_signal));
173 ev_signal_init(sig_term, &sig_cb, SIGTERM);
174 ev_signal_init(sig_int, &sig_cb, SIGINT);
175 ev_signal_init(sig_hup, &sig_cb, SIGHUP);
177 ev_signal_start(main_loop, sig_term);
178 ev_signal_start(main_loop, sig_int);
179 ev_signal_start(main_loop, sig_hup);
181 /* From here on everything should run smooth for itself, just start listening for
182 * events. We stop simply stop the event loop, when we are finished */
183 ev_loop(main_loop, 0);
188 ev_default_destroy();