typedef struct config_t {
int hide_on_modifier;
+ int verbose;
} config_t;
config_t config;
walk = TAILQ_FIRST(l); \
} \
} while (0)
+
+/* Use cool logging-macros */
+#define DLOG(fmt, ...) do { \
+ if (config.verbose) { \
+ printf("[%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
+ } \
+} while(0)
+
+#define ELOG(fmt, ...) do { \
+ fprintf(stderr, "[%s:%d] ERROR: " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
+} while(0)
buffer[rec-1] = '\0';
break;
}
- printf("ERROR: read() failed!");
+ ELOG("read() failed!\n");
exit(EXIT_FAILURE);
}
if (n == 0) {
}
FREE(statusline);
statusline = buffer;
- printf("%s\n", buffer);
+ DLOG("%s\n", buffer);
draw_bars();
}
*
*/
void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
- printf("Child (pid: %d) unexpectedly exited with status %d\n",
+ DLOG("Child (pid: %d) unexpectedly exited with status %d\n",
child_pid,
watcher->rstatus);
cleanup();
child_pid = fork();
switch (child_pid) {
case -1:
- printf("ERROR: Couldn't fork()");
+ ELOG("Couldn't fork()\n");
exit(EXIT_FAILURE);
case 0:
/* Child-process. Reroute stdout and start shell */
int get_ipc_fd(const char *socket_path) {
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (sockfd == -1) {
- printf("ERROR: Could not create Socket!\n");
+ ELOG("Could not create Socket!\n");
exit(EXIT_FAILURE);
}
addr.sun_family = AF_LOCAL;
strcpy(addr.sun_path, socket_path);
if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
- printf("ERROR: Could not connct to i3\n");
+ ELOG("Could not connct to i3!\n");
exit(EXIT_FAILURE);
}
return sockfd;
*
*/
void got_workspace_reply(char *reply) {
- printf("Got Workspace-Data!\n");
+ DLOG("Got Workspace-Data!\n");
parse_workspaces_json(reply);
draw_bars();
}
*
*/
void got_subscribe_reply(char *reply) {
- printf("Got Subscribe Reply: %s\n", reply);
+ DLOG("Got Subscribe Reply: %s\n", reply);
/* TODO: Error handling for subscribe-commands */
}
*
*/
void got_output_reply(char *reply) {
- printf("Parsing Outputs-JSON...\n");
+ DLOG("Parsing Outputs-JSON...\n");
parse_outputs_json(reply);
- printf("Reconfiguring Windows...\n");
+ DLOG("Reconfiguring Windows...\n");
reconfig_windows();
}
*
*/
void got_workspace_event(char *event) {
- printf("Got Workspace Event!\n");
+ DLOG("Got Workspace Event!\n");
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
}
*
*/
void got_output_event(char *event) {
- printf("Got Output Event!\n");
+ DLOG("Got Output Event!\n");
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
}
*
*/
void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
- printf("Got data!\n");
+ DLOG("Got data!\n");
int fd = watcher->fd;
/* First we only read the header, because we know it's length */
uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
char *header = malloc(header_len);
if (header == NULL) {
- printf("ERROR: Could not allocate memory!\n");
+ ELOG("Could not allocate memory!\n");
exit(EXIT_FAILURE);
}
while (rec < header_len) {
int n = read(fd, header + rec, header_len - rec);
if (n == -1) {
- printf("ERROR: read() failed!\n");
+ ELOG("read() failed!\n");
exit(EXIT_FAILURE);
}
if (n == 0) {
- printf("ERROR: Nothing to read!\n");
+ ELOG("Nothing to read!\n");
exit(EXIT_FAILURE);
}
rec += n;
}
if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
- printf("ERROR: Wrong magic code: %.*s\n Expected: %s\n",
- (int) strlen(I3_IPC_MAGIC),
- header,
- I3_IPC_MAGIC);
+ ELOG("Wrong magic code: %.*s\n Expected: %s\n",
+ (int) strlen(I3_IPC_MAGIC),
+ header,
+ I3_IPC_MAGIC);
exit(EXIT_FAILURE);
}
* of the message */
char *buffer = malloc(size + 1);
if (buffer == NULL) {
- printf("ERROR: Could not allocate memory!\n");
+ ELOG("Could not allocate memory!\n");
exit(EXIT_FAILURE);
}
rec = 0;
while (rec < size) {
int n = read(fd, buffer + rec, size - rec);
if (n == -1) {
- printf("ERROR: read() failed!\n");
+ ELOG("read() failed!\n");
exit(EXIT_FAILURE);
}
if (n == 0) {
- printf("ERROR: Nothing to read!\n");
+ ELOG("Nothing to read!\n");
exit(EXIT_FAILURE);
}
rec += n;
* but we leave it for now */
char *buffer = malloc(to_write);
if (buffer == NULL) {
- printf("ERROR: Could not allocate memory\n");
+ ELOG("Could not allocate memory\n");
exit(EXIT_FAILURE);
}
while (to_write > 0) {
int n = write(i3_connection->fd, buffer + written, to_write);
if (n == -1) {
- printf("ERROR: write() failed!\n");
+ ELOG("write() failed!\n");
exit(EXIT_FAILURE);
}
char *expand_path(char *path) {
static glob_t globbuf;
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
- printf("glob() failed");
+ ELOG("glob() failed\n");
exit(EXIT_FAILURE);
}
char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
if (result == NULL) {
- printf("malloc() failed");
+ ELOG("malloc() failed\n");
exit(EXIT_FAILURE);
}
globfree(&globbuf);
}
void print_usage(char *elf_name) {
- printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-h]\n", elf_name);
+ printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-V] [-h]\n", elf_name);
printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
printf("-c <command>\tExecute <command> to get stdin\n");
printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
printf("\t\tand a SIGCONT on unhiding of the bars\n");
printf("-f <font>\tUse X-Core-Font <font> for display\n");
+ printf("-V\t\tBe (very) verbose with the debug-output\n");
printf("-h\t\tDisplay this help-message and exit\n");
}
{ "font", required_argument, 0, 'f' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
+ { "verbose", no_argument, 0, 'V' },
{ NULL, 0, 0, 0}
};
- while ((opt = getopt_long(argc, argv, "s:c:mf:hv", long_opt, &option_index)) != -1) {
+ while ((opt = getopt_long(argc, argv, "s:c:mf:hvV", long_opt, &option_index)) != -1) {
switch (opt) {
case 's':
socket_path = expand_path(optarg);
printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
exit(EXIT_SUCCESS);
break;
+ case 'V':
+ config.verbose = 1;
+ break;
default:
print_usage(argv[0]);
exit(EXIT_SUCCESS);
}
if (socket_path == NULL) {
- printf("No Socket Path Specified, default to %s\n", i3_default_sock_path);
+ ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
socket_path = expand_path(i3_default_sock_path);
}
case yajl_status_client_canceled:
case yajl_status_insufficient_data:
case yajl_status_error:
- printf("ERROR: Could not parse outputs-reply!\n");
+ ELOG("Could not parse outputs-reply!\n");
exit(EXIT_FAILURE);
break;
}
predict_text_extents(params->workspaces_walk->ucs2_name,
params->workspaces_walk->name_glyphs);
- printf("Got Workspace %s, name_width: %d, glyphs: %d\n",
- params->workspaces_walk->name,
- params->workspaces_walk->name_width,
- params->workspaces_walk->name_glyphs);
+ DLOG("Got Workspace %s, name_width: %d, glyphs: %d\n",
+ params->workspaces_walk->name,
+ params->workspaces_walk->name_width,
+ params->workspaces_walk->name_glyphs);
FREE(params->cur_key);
return 1;
params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
if (params->cur_key == NULL) {
- printf("ERROR: Could not allocate memory!\n");
+ ELOG("Could not allocate memory!\n");
exit(EXIT_FAILURE);
}
strncpy(params->cur_key, (const char*) keyVal, keyLen);
case yajl_status_client_canceled:
case yajl_status_insufficient_data:
case yajl_status_error:
- printf("ERROR: Could not parse workspaces-reply!\n");
+ ELOG("Could not parse workspaces-reply!\n");
exit(EXIT_FAILURE);
break;
}
int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
xcb_generic_error_t *err;
if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
- printf("%s:%d - %s. X Error Code: %d", __FILE__, line, err_msg, err->error_code);
+ ELOG("%s. X Error Code: %d\n", err_msg, err->error_code);
return err->error_code;
}
return 0;
values[2] = walk->rect.w;
values[3] = font_height + 6;
values[4] = XCB_STACK_MODE_ABOVE;
- printf("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
+ DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
cookie = xcb_configure_window_checked(xcb_connection,
walk->bar,
mask,
}
if (walk == NULL) {
- printf("Unknown Bar klicked!\n");
+ DLOG("Unknown Bar klicked!\n");
return;
}
- /* TODO: Move this to exern get_ws_for_output() */
+ /* TODO: Move this to extern get_ws_for_output() */
TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
if (cur_ws->visible) {
break;
}
if (cur_ws == NULL) {
- printf("No Workspace active?\n");
+ DLOG("No Workspace active?\n");
return;
}
int32_t x = event->event_x;
- printf("Got Button %d\n", event->detail);
+ DLOG("Got Button %d\n", event->detail);
switch (event->detail) {
case 1:
/* Left Mousbutton. We determine, which button was clicked
* and set cur_ws accordingly */
TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
- printf("x = %d\n", x);
+ DLOG("x = %d\n", x);
if (x < cur_ws->name_width + 10) {
break;
}
XkbEvent ev;
int modstate;
- printf("Got XKB-Event!\n");
+ DLOG("Got XKB-Event!\n");
while (XPending(xkb_dpy)) {
XNextEvent(xkb_dpy, (XEvent*)&ev);
if (ev.type != xkb_event_base) {
- printf("ERROR: No Xkb-Event!\n");
+ ELOG("No Xkb-Event!\n");
continue;
}
if (ev.any.xkb_type != XkbStateNotify) {
- printf("ERROR: No State Notify!\n");
+ ELOG("No State Notify!\n");
continue;
}
if (modstate != mod_pressed) {
if (modstate == 0) {
- printf("Mod4 got released!\n");
+ DLOG("Mod4 got released!\n");
hide_bars();
} else {
- printf("Mod4 got pressed!\n");
+ DLOG("Mod4 got pressed!\n");
unhide_bars();
}
mod_pressed = modstate;
/* FIXME: xcb_connect leaks Memory */
xcb_connection = xcb_connect(NULL, NULL);
if (xcb_connection_has_error(xcb_connection)) {
- printf("Cannot open display\n");
+ ELOG("Cannot open display\n");
exit(EXIT_FAILURE);
}
- printf("Connected to xcb\n");
+ DLOG("Connected to xcb\n");
/* We have to request the atoms we need */
#define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
&xkb_err);
if (xkb_dpy == NULL) {
- printf("ERROR: No XKB!\n");
+ ELOG("No XKB!\n");
exit(EXIT_FAILURE);
}
if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
- fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
+ ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
exit(EXIT_FAILURE);
}
int i1;
if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
- printf("ERROR: XKB not supported by X-server!\n");
+ ELOG("XKB not supported by X-server!\n");
exit(EXIT_FAILURE);
}
if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
- printf("Could not grab Key!\n");
+ ELOG("Could not grab Key!\n");
exit(EXIT_FAILURE);
}
font_table = xcb_query_font_char_infos(font_info);
}
- printf("Calculated Font-height: %d\n", font_height);
+ DLOG("Calculated Font-height: %d\n", font_height);
if (xcb_request_failed(sl_ctx_cookie, "Could not create context for statusline")) {
exit(EXIT_FAILURE);
xcb_intern_atom_reply_t *reply;
#define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
if (reply == NULL) { \
- printf("ERROR: Could not get atom %s\n", #name); \
+ ELOG("Could not get atom %s\n", #name); \
exit(EXIT_FAILURE); \
} \
atoms[name] = reply->atom; \
free(reply);
#include "xcb_atoms.def"
- printf("Got Atoms\n");
+ DLOG("Got Atoms\n");
}
/*
if (!walk->active) {
/* If an output is not active, we destroy it's bar */
/* FIXME: Maybe we rather want to unmap? */
- printf("Destroying window for output %s\n", walk->name);
+ DLOG("Destroying window for output %s\n", walk->name);
destroy_window(walk);
continue;
}
if (walk->bar == XCB_NONE) {
- printf("Creating Window for output %s\n", walk->name);
+ DLOG("Creating Window for output %s\n", walk->name);
walk->bar = xcb_generate_id(xcb_connection);
walk->buffer = xcb_generate_id(xcb_connection);
values[2] = walk->rect.w;
values[3] = font_height + 6;
values[4] = XCB_STACK_MODE_ABOVE;
- printf("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
+ DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
walk->bar,
mask,
*
*/
void draw_bars() {
- printf("Drawing Bars...\n");
+ DLOG("Drawing Bars...\n");
int i = 0;
refresh_statusline();
i3_output *outputs_walk;
SLIST_FOREACH(outputs_walk, outputs, slist) {
if (!outputs_walk->active) {
- printf("Output %s inactive, skipping...\n", outputs_walk->name);
+ DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
continue;
}
if (outputs_walk->bar == XCB_NONE) {
&rect);
if (statusline != NULL) {
- printf("Printing statusline!\n");
+ DLOG("Printing statusline!\n");
/* Luckily we already prepared a seperate pixmap containing the rendered
* statusline, we just have to copy the relevant parts to the relevant
i3_ws *ws_walk;
TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
- printf("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
+ DLOG("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
uint32_t color = get_colorpixel("240000");
if (ws_walk->visible) {
color = get_colorpixel("480000");
}
if (ws_walk->urgent) {
- printf("WS %s is urgent!\n", ws_walk->name);
+ DLOG("WS %s is urgent!\n", ws_walk->name);
color = get_colorpixel("002400");
/* The urgent-hint should get noticed, so we unhide the bars shortly */
unhide_bars();