+/*
+ * vim:ts=4:sw=4:expandtab
+ *
+ * i3bar - an xcb-based status- and ws-bar for i3
+ * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
+ *
+ */
#include <stdio.h>
#include <i3/ipc.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
-#include <fcntl.h>
#include <errno.h>
#include <ev.h>
-#include <xcb/xcb.h>
+#include <getopt.h>
+#include <glob.h>
-#include "ipc.h"
-#include "outputs.h"
-#include "workspaces.h"
#include "common.h"
-#include "xcb.h"
-#define STDIN_CHUNK_SIZE 1024
+/*
+ * Having verboselog() and errorlog() is necessary when using libi3.
+ *
+ */
+void verboselog(char *fmt, ...) {
+ va_list args;
-void ev_prepare_cb(struct ev_loop *loop, ev_prepare *w, int revents) {
- xcb_flush(xcb_connection);
+ va_start(args, fmt);
+ vfprintf(stdout, fmt, args);
+ va_end(args);
}
-void ev_check_cb(struct ev_loop *loop, ev_check *w, int revents) {
- xcb_generic_event_t *event;
- if ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
- handle_xcb_event(event);
- }
- free(event);
-}
+void errorlog(char *fmt, ...) {
+ va_list args;
-void xcb_io_cb(struct ev_loop *loop, ev_io *w, int revents) {
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
}
-void start_child(char *command) {
- int fd[2];
- pipe(fd);
- child_pid = fork();
- switch (child_pid) {
- case -1:
- printf("ERROR: Couldn't fork()");
- exit(EXIT_FAILURE);
- case 0:
- close(fd[0]);
-
- dup2(fd[1], STDOUT_FILENO);
-
- static const char *shell = NULL;
+/*
+ * Glob path, i.e. expand ~
+ *
+ */
+char *expand_path(char *path) {
+ static glob_t globbuf;
+ if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
+ ELOG("glob() failed\n");
+ exit(EXIT_FAILURE);
+ }
+ char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
+ globfree(&globbuf);
+ return result;
+}
- if ((shell = getenv("SHELL")) == NULL)
- shell = "/bin/sh";
+void print_usage(char *elf_name) {
+ printf("Usage: %s -b bar_id [-s sock_path] [-h] [-v]\n", elf_name);
+ printf("\n");
+ printf("-b, --bar_id <bar_id>\tBar ID for which to get the configuration\n");
+ printf("-s, --socket <sock_path>\tConnect to i3 via <sock_path>\n");
+ printf("-h, --help Display this help-message and exit\n");
+ printf("-v, --version Display version number and exit\n");
+ printf("\n");
+ printf(" PLEASE NOTE that i3bar will be automatically started by i3\n"
+ " as soon as there is a 'bar' configuration block in your\n"
+ " config file. You should never need to start it manually.\n");
+ printf("\n");
+}
- execl(shell, shell, "-c", command, (char*) NULL);
+/*
+ * We watch various signals, that are there to make our application stop.
+ * If we get one of those, we ev_unloop() and invoke the cleanup-routines
+ * in main() with that
+ *
+ */
+void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
+ switch (watcher->signum) {
+ case SIGTERM:
+ DLOG("Got a SIGTERM, stopping\n");
break;
- default:
- close(fd[1]);
-
- dup2(fd[0], STDIN_FILENO);
-
+ case SIGINT:
+ DLOG("Got a SIGINT, stopping\n");
break;
+ case SIGHUP:
+ DLOG("Got a SIGHUP, stopping\n");
}
+ ev_unloop(main_loop, EVUNLOOP_ALL);
}
-void strip_dzen_formats(char *buffer) {
- char *src = buffer;
- char *dest = buffer;
- while (*src != '\0') {
- if (*src == '^') {
- if (!strncmp(src, "^ro", strlen("^ro"))) {
- *(dest++) = ' ';
- *(dest++) = '|';
- *(dest++) = ' ';
- }
- while (*src != ')') {
- src++;
- }
- src++;
- } else {
- *dest = *src;
- src++;
- dest++;
- }
- }
- *(--dest) = '\0';
-}
-
-void child_io_cb(struct ev_loop *loop, ev_io *w, int revents) {
- int fd = w->fd;
- int n = 0;
- int rec = 0;
- int buffer_len = STDIN_CHUNK_SIZE;
- char *buffer = malloc(buffer_len);
- memset(buffer, '\0', buffer_len);
- while(1) {
- n = read(fd, buffer + rec, buffer_len - rec);
- if (n == -1) {
- if (errno == EAGAIN) {
+int main(int argc, char **argv) {
+ int opt;
+ int option_index = 0;
+ char *socket_path = getenv("I3SOCK");
+ char *i3_default_sock_path = "/tmp/i3-ipc.sock";
+
+ /* Initialize the standard config to use 0 as default */
+ memset(&config, '\0', sizeof(config_t));
+
+ static struct option long_opt[] = {
+ { "socket", required_argument, 0, 's' },
+ { "bar_id", required_argument, 0, 'b' },
+ { "help", no_argument, 0, 'h' },
+ { "version", no_argument, 0, 'v' },
+ { NULL, 0, 0, 0}
+ };
+
+ while ((opt = getopt_long(argc, argv, "b:s:hv", long_opt, &option_index)) != -1) {
+ switch (opt) {
+ case 's':
+ socket_path = expand_path(optarg);
break;
- }
- printf("ERROR: read() failed!");
- exit(EXIT_FAILURE);
- }
- if (n == 0) {
- if (rec == buffer_len) {
- char *tmp = buffer;
- buffer = malloc(buffer_len + STDIN_CHUNK_SIZE);
- memset(buffer, '\0', buffer_len);
- strncpy(buffer, tmp, buffer_len);
- buffer_len += STDIN_CHUNK_SIZE;
- FREE(tmp);
- } else {
+ case 'v':
+ printf("i3bar version " I3_VERSION " © 2010-2011 Axel Wagner and contributors\n");
+ exit(EXIT_SUCCESS);
+ break;
+ case 'b':
+ config.bar_id = sstrdup(optarg);
+ break;
+ default:
+ print_usage(argv[0]);
+ exit(EXIT_SUCCESS);
break;
- }
}
- rec += n;
}
- if (strlen(buffer) == 0) {
- FREE(buffer);
- return;
+
+ if (!config.bar_id) {
+ /* TODO: maybe we want -f which will automatically ask i3 for the first
+ * configured bar (and error out if there are too many)? */
+ ELOG("No bar_id passed. Please let i3 start i3bar or specify --bar_id\n");
+ exit(EXIT_FAILURE);
}
- strip_dzen_formats(buffer);
- FREE(statusline);
- statusline = buffer;
- printf("%s", buffer);
- draw_bars();
-}
-int main(int argc, char **argv) {
main_loop = ev_default_loop(0);
- init_xcb();
- init_connection("/home/mero/.i3/ipc.sock");
-
- subscribe_events();
+ char *atom_sock_path = init_xcb_early();
- ev_io *xcb_io = malloc(sizeof(ev_io));
- ev_prepare *ev_prep = malloc(sizeof(ev_prepare));
- ev_check *ev_chk = malloc(sizeof(ev_check));
+ if (socket_path == NULL) {
+ socket_path = atom_sock_path;
+ }
- ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
- ev_prepare_init(ev_prep, &ev_prepare_cb);
- ev_check_init(ev_chk, &ev_check_cb);
+ if (socket_path == NULL) {
+ ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
+ socket_path = expand_path(i3_default_sock_path);
+ }
- ev_io_start(main_loop, xcb_io);
- ev_prepare_start(main_loop, ev_prep);
- ev_check_start(main_loop, ev_chk);
+ init_outputs();
+ if (init_connection(socket_path)) {
+ /* Request the bar configuration. When it arrives, we fill the config array. */
+ i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id);
+ }
- i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
- i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
+ /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
+ * We only need those watchers on the stack, so putting them on the stack saves us
+ * some calls to free() */
+ ev_signal *sig_term = smalloc(sizeof(ev_signal));
+ ev_signal *sig_int = smalloc(sizeof(ev_signal));
+ ev_signal *sig_hup = smalloc(sizeof(ev_signal));
- start_child("i3status");
+ ev_signal_init(sig_term, &sig_cb, SIGTERM);
+ ev_signal_init(sig_int, &sig_cb, SIGINT);
+ ev_signal_init(sig_hup, &sig_cb, SIGHUP);
- fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
- ev_io *child_io = malloc(sizeof(ev_io));
- ev_io_init(child_io, &child_io_cb, STDIN_FILENO, EV_READ);
- ev_io_start(main_loop, child_io);
+ ev_signal_start(main_loop, sig_term);
+ ev_signal_start(main_loop, sig_int);
+ ev_signal_start(main_loop, sig_hup);
+ /* From here on everything should run smooth for itself, just start listening for
+ * events. We stop simply stop the event-loop, when we are finished */
ev_loop(main_loop, 0);
- ev_prepare_stop(main_loop, ev_prep);
- ev_check_stop(main_loop, ev_chk);
- ev_io_stop(main_loop, xcb_io);
- ev_io_stop(main_loop, child_io);
+ kill_child();
- FREE(xcb_io);
- FREE(ev_prep);
- FREE(ev_chk);
- free(child_io);
+ FREE(statusline_buffer);
- FREE(statusline);
-
- ev_default_destroy();
clean_xcb();
+ ev_default_destroy();
free_workspaces();
- FREE_SLIST(outputs, i3_output);
return 0;
}