2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2011 Axel Wagner and contributors (see also: LICENSE)
7 * xcb.c: Communicating with X
11 #include <xcb/xproto.h>
12 #include <xcb/xcb_atom.h>
13 #include <xcb/xcb_aux.h>
16 #include "xcb_compat.h"
31 #include <X11/XKBlib.h>
32 #include <X11/extensions/XKB.h>
37 /* We save the Atoms in an easy to access array, indexed by an enum */
39 #define ATOM_DO(name) name,
40 #include "xcb_atoms.def"
44 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
45 xcb_atom_t atoms[NUM_ATOMS];
47 /* Variables, that are the same for all functions at all times */
48 xcb_connection_t *xcb_connection;
50 xcb_screen_t *xcb_screen;
51 xcb_window_t xcb_root;
53 /* This is needed for integration with libi3 */
54 xcb_connection_t *conn;
56 /* The font we'll use */
59 /* These are only relevant for XKB, which we only need for grabbing modifiers */
64 /* Because the statusline is the same on all outputs, we have
65 * global buffer to render it on */
66 xcb_gcontext_t statusline_ctx;
67 xcb_gcontext_t statusline_clear;
68 xcb_pixmap_t statusline_pm;
69 uint32_t statusline_width;
71 /* Event-Watchers, to interact with the user */
77 /* The parsed colors */
81 uint32_t active_ws_fg;
82 uint32_t active_ws_bg;
83 uint32_t active_ws_border;
84 uint32_t inactive_ws_fg;
85 uint32_t inactive_ws_bg;
86 uint32_t inactive_ws_border;
87 uint32_t urgent_ws_bg;
88 uint32_t urgent_ws_fg;
89 uint32_t urgent_ws_border;
92 uint32_t focus_ws_border;
94 struct xcb_colors_t colors;
96 /* We define xcb_request_failed as a macro to include the relevant line-number */
97 #define xcb_request_failed(cookie, err_msg) _xcb_request_failed(cookie, err_msg, __LINE__)
98 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
99 xcb_generic_error_t *err;
100 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
101 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
102 return err->error_code;
108 * Redraws the statusline to the buffer
111 void refresh_statusline() {
112 struct status_block *block;
114 uint32_t old_statusline_width = statusline_width;
115 statusline_width = 0;
117 /* Convert all blocks from UTF-8 to UCS-2 and predict the text width (in
119 TAILQ_FOREACH(block, &statusline_head, blocks) {
120 block->ucs2_full_text = (xcb_char2b_t*)convert_utf8_to_ucs2(block->full_text, &(block->glyph_count_full_text));
121 block->width = predict_text_width((char*)block->ucs2_full_text, block->glyph_count_full_text, true);
122 /* If this is not the last block, add some pixels for a separator. */
123 if (TAILQ_NEXT(block, blocks) != NULL)
125 statusline_width += block->width;
128 /* If the statusline is bigger than our screen we need to make sure that
129 * the pixmap provides enough space, so re-allocate if the width grew */
130 if (statusline_width > xcb_screen->width_in_pixels &&
131 statusline_width > old_statusline_width)
134 /* Clear the statusline pixmap. */
135 xcb_rectangle_t rect = { 0, 0, xcb_screen->width_in_pixels, font.height };
136 xcb_poly_fill_rectangle(xcb_connection, statusline_pm, statusline_clear, 1, &rect);
138 /* Draw the text of each block. */
140 TAILQ_FOREACH(block, &statusline_head, blocks) {
141 uint32_t colorpixel = (block->color ? get_colorpixel(block->color) : colors.bar_fg);
142 set_font_colors(statusline_ctx, colorpixel, colors.bar_bg);
143 draw_text((char*)block->ucs2_full_text, block->glyph_count_full_text,
144 true, statusline_pm, statusline_ctx, x, 0, block->width);
147 if (TAILQ_NEXT(block, blocks) != NULL) {
148 /* This is not the last block, draw a separator. */
149 set_font_colors(statusline_ctx, get_colorpixel("#666666"), colors.bar_bg);
150 xcb_poly_line(xcb_connection, XCB_COORD_MODE_ORIGIN, statusline_pm,
152 (xcb_point_t[]){ { x - 5, 2 }, { x - 5, font.height - 2 } });
155 FREE(block->ucs2_full_text);
160 * Hides all bars (unmaps them)
164 if (!config.hide_on_modifier) {
169 SLIST_FOREACH(walk, outputs, slist) {
173 xcb_unmap_window(xcb_connection, walk->bar);
179 * Unhides all bars (maps them)
183 if (!config.hide_on_modifier) {
188 xcb_void_cookie_t cookie;
194 SLIST_FOREACH(walk, outputs, slist) {
195 if (walk->bar == XCB_NONE) {
198 mask = XCB_CONFIG_WINDOW_X |
199 XCB_CONFIG_WINDOW_Y |
200 XCB_CONFIG_WINDOW_WIDTH |
201 XCB_CONFIG_WINDOW_HEIGHT |
202 XCB_CONFIG_WINDOW_STACK_MODE;
203 values[0] = walk->rect.x;
204 if (config.position == POS_TOP)
205 values[1] = walk->rect.y;
206 else values[1] = walk->rect.y + walk->rect.h - font.height - 6;
207 values[2] = walk->rect.w;
208 values[3] = font.height + 6;
209 values[4] = XCB_STACK_MODE_ABOVE;
210 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
211 cookie = xcb_configure_window_checked(xcb_connection,
216 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
219 xcb_map_window(xcb_connection, walk->bar);
224 * Parse the colors into a format that we can use
227 void init_colors(const struct xcb_color_strings_t *new_colors) {
228 #define PARSE_COLOR(name, def) \
230 colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
232 PARSE_COLOR(bar_fg, "#FFFFFF");
233 PARSE_COLOR(bar_bg, "#000000");
234 PARSE_COLOR(active_ws_fg, "#FFFFFF");
235 PARSE_COLOR(active_ws_bg, "#333333");
236 PARSE_COLOR(active_ws_border, "#333333");
237 PARSE_COLOR(inactive_ws_fg, "#888888");
238 PARSE_COLOR(inactive_ws_bg, "#222222");
239 PARSE_COLOR(inactive_ws_border, "#333333");
240 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
241 PARSE_COLOR(urgent_ws_bg, "#900000");
242 PARSE_COLOR(urgent_ws_border, "#2f343a");
243 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
244 PARSE_COLOR(focus_ws_bg, "#285577");
245 PARSE_COLOR(focus_ws_border, "#4c7899");
250 * Handle a button-press-event (i.e. a mouse click on one of our bars).
251 * We determine, whether the click occured on a ws-button or if the scroll-
252 * wheel was used and change the workspace appropriately
255 void handle_button(xcb_button_press_event_t *event) {
258 /* Determine, which bar was clicked */
260 xcb_window_t bar = event->event;
261 SLIST_FOREACH(walk, outputs, slist) {
262 if (walk->bar == bar) {
268 DLOG("Unknown Bar klicked!\n");
272 /* TODO: Move this to extern get_ws_for_output() */
273 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
274 if (cur_ws->visible) {
279 if (cur_ws == NULL) {
280 DLOG("No Workspace active?\n");
284 int32_t x = event->event_x;
286 DLOG("Got Button %d\n", event->detail);
288 switch (event->detail) {
290 /* Left Mousbutton. We determine, which button was clicked
291 * and set cur_ws accordingly */
292 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
294 if (x >= 0 && x < cur_ws->name_width + 10) {
297 x -= cur_ws->name_width + 11;
299 if (cur_ws == NULL) {
304 /* Mouse wheel down. We select the next ws */
305 if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
306 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
308 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
312 /* Mouse wheel up. We select the previos ws */
313 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
314 cur_ws = TAILQ_FIRST(walk->workspaces);
316 cur_ws = TAILQ_NEXT(cur_ws, tailq);
321 /* To properly handle workspace names with double quotes in them, we need
322 * to escape the double quotes. Unfortunately, that’s rather ugly in C: We
323 * first count the number of double quotes, then we allocate a large enough
324 * buffer, then we copy character by character. */
327 for (char *walk = cur_ws->name; *walk != '\0'; walk++) {
330 /* While we’re looping through the name anyway, we can save one
335 const size_t len = namelen + strlen("workspace \"\"") + 1;
336 char *buffer = scalloc(len+num_quotes);
337 strncpy(buffer, "workspace \"", strlen("workspace \""));
339 for (inpos = 0, outpos = strlen("workspace \"");
342 if (cur_ws->name[inpos] == '"') {
343 buffer[outpos] = '\\';
346 buffer[outpos] = cur_ws->name[inpos];
348 buffer[outpos] = '"';
349 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
354 * Configures the x coordinate of all trayclients. To be called after adding a
355 * new tray client or removing an old one.
358 static void configure_trayclients() {
359 trayclient *trayclient;
361 SLIST_FOREACH(output, outputs, slist) {
366 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
367 if (!trayclient->mapped)
371 DLOG("Configuring tray window %08x to x=%d\n",
372 trayclient->win, output->rect.w - (clients * (font.height + 2)));
373 uint32_t x = output->rect.w - (clients * (font.height + 2));
374 xcb_configure_window(xcb_connection,
383 * Handles ClientMessages (messages sent from another client directly to us).
385 * At the moment, only the tray window will receive client messages. All
386 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
389 static void handle_client_message(xcb_client_message_event_t* event) {
390 if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
391 event->format == 32) {
392 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
393 /* event->data.data32[0] is the timestamp */
394 uint32_t op = event->data.data32[1];
397 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
398 xcb_window_t client = event->data.data32[2];
400 /* Listen for PropertyNotify events to get the most recent value of
401 * the XEMBED_MAPPED atom, also listen for UnmapNotify events */
402 mask = XCB_CW_EVENT_MASK;
403 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
404 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
405 xcb_change_window_attributes(xcb_connection,
410 /* Request the _XEMBED_INFO property. The XEMBED specification
411 * (which is referred by the tray specification) says this *has* to
412 * be set, but VLC does not set it… */
415 xcb_get_property_cookie_t xembedc;
416 xcb_generic_error_t *error;
417 xembedc = xcb_get_property(xcb_connection,
421 XCB_GET_PROPERTY_TYPE_ANY,
425 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
429 ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
434 if (xembedr != NULL && xembedr->length != 0) {
435 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
436 uint32_t *xembed = xcb_get_property_value(xembedr);
437 DLOG("xembed version = %d\n", xembed[0]);
438 DLOG("xembed flags = %d\n", xembed[1]);
439 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
440 xe_version = xembed[0];
445 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
448 DLOG("X window %08x requested docking\n", client);
449 i3_output *walk, *output = NULL;
450 SLIST_FOREACH(walk, outputs, slist) {
453 if (config.tray_output) {
454 if ((strcasecmp(walk->name, config.tray_output) != 0) &&
455 (!walk->primary || strcasecmp("primary", config.tray_output) != 0))
459 DLOG("using output %s\n", walk->name);
463 /* In case of tray_output == primary and there is no primary output
464 * configured, we fall back to the first available output. */
465 if (output == NULL && strcasecmp("primary", config.tray_output) == 0) {
466 SLIST_FOREACH(walk, outputs, slist) {
469 DLOG("Falling back to output %s because no primary output is configured\n", walk->name);
474 if (output == NULL) {
475 ELOG("No output found\n");
478 xcb_reparent_window(xcb_connection,
481 output->rect.w - font.height - 2,
483 /* We reconfigure the window to use a reasonable size. The systray
484 * specification explicitly says:
485 * Tray icons may be assigned any size by the system tray, and
486 * should do their best to cope with any size effectively
488 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
489 values[0] = font.height;
490 values[1] = font.height;
491 xcb_configure_window(xcb_connection,
496 /* send the XEMBED_EMBEDDED_NOTIFY message */
497 void *event = scalloc(32);
498 xcb_client_message_event_t *ev = event;
499 ev->response_type = XCB_CLIENT_MESSAGE;
501 ev->type = atoms[_XEMBED];
503 ev->data.data32[0] = XCB_CURRENT_TIME;
504 ev->data.data32[1] = atoms[XEMBED_EMBEDDED_NOTIFY];
505 ev->data.data32[2] = output->bar;
506 ev->data.data32[3] = xe_version;
507 xcb_send_event(xcb_connection,
510 XCB_EVENT_MASK_NO_EVENT,
514 /* Put the client inside the save set. Upon termination (whether
515 * killed or normal exit does not matter) of i3bar, these clients
516 * will be correctly reparented to their most closest living
517 * ancestor. Without this, tray icons might die when i3bar
519 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
522 DLOG("Mapping dock client\n");
523 xcb_map_window(xcb_connection, client);
525 DLOG("Not mapping dock client yet\n");
527 trayclient *tc = smalloc(sizeof(trayclient));
530 tc->xe_version = xe_version;
531 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
533 /* Trigger an update to copy the statusline text to the appropriate
535 configure_trayclients();
542 * Handles UnmapNotify events. These events happen when a tray window unmaps
543 * itself. We then update our data structure
546 static void handle_unmap_notify(xcb_unmap_notify_event_t* event) {
547 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
550 SLIST_FOREACH(walk, outputs, slist) {
553 DLOG("checking output %s\n", walk->name);
554 trayclient *trayclient;
555 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
556 if (trayclient->win != event->window)
559 DLOG("Removing tray client with window ID %08x\n", event->window);
560 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
562 /* Trigger an update, we now have more space for the statusline */
563 configure_trayclients();
571 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
572 * handled, which tells us whether a dock client should be mapped or unmapped.
575 static void handle_property_notify(xcb_property_notify_event_t *event) {
576 DLOG("PropertyNotify\n");
577 if (event->atom == atoms[_XEMBED_INFO] &&
578 event->state == XCB_PROPERTY_NEW_VALUE) {
579 DLOG("xembed_info updated\n");
580 trayclient *trayclient = NULL, *walk;
582 SLIST_FOREACH(o_walk, outputs, slist) {
586 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
587 if (walk->win != event->window)
597 ELOG("PropertyNotify received for unknown window %08x\n",
601 xcb_get_property_cookie_t xembedc;
602 xembedc = xcb_get_property_unchecked(xcb_connection,
606 XCB_GET_PROPERTY_TYPE_ANY,
610 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
613 if (xembedr == NULL || xembedr->length == 0) {
614 DLOG("xembed_info unset\n");
618 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
619 uint32_t *xembed = xcb_get_property_value(xembedr);
620 DLOG("xembed version = %d\n", xembed[0]);
621 DLOG("xembed flags = %d\n", xembed[1]);
622 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
623 DLOG("map-state now %d\n", map_it);
624 if (trayclient->mapped && !map_it) {
625 /* need to unmap the window */
626 xcb_unmap_window(xcb_connection, trayclient->win);
627 trayclient->mapped = map_it;
628 configure_trayclients();
630 } else if (!trayclient->mapped && map_it) {
631 /* need to map the window */
632 xcb_map_window(xcb_connection, trayclient->win);
633 trayclient->mapped = map_it;
634 configure_trayclients();
642 * Handle ConfigureRequests by denying them and sending the client a
643 * ConfigureNotify with its actual size.
646 static void handle_configure_request(xcb_configure_request_event_t *event) {
647 DLOG("ConfigureRequest for window = %08x\n", event->window);
649 trayclient *trayclient;
651 SLIST_FOREACH(output, outputs, slist) {
656 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
657 if (!trayclient->mapped)
661 if (trayclient->win != event->window)
664 xcb_rectangle_t rect;
665 rect.x = output->rect.w - (clients * (font.height + 2));
667 rect.width = font.height;
668 rect.height = font.height;
670 DLOG("This is a tray window. x = %d\n", rect.x);
671 fake_configure_notify(xcb_connection, rect, event->window, 0);
676 DLOG("WARNING: Could not find corresponding tray window.\n");
680 * This function is called immediately before the main loop locks. We flush xcb
681 * then (and only then)
684 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
685 xcb_flush(xcb_connection);
689 * This function is called immediately after the main loop locks, so when one
690 * of the watchers registered an event.
691 * We check whether an X-Event arrived and handle it.
694 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
695 xcb_generic_event_t *event;
697 if (xcb_connection_has_error(xcb_connection)) {
698 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
702 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
703 switch (event->response_type & ~0x80) {
705 /* Expose-events happen, when the window needs to be redrawn */
708 case XCB_BUTTON_PRESS:
709 /* Button-press-events are mouse-buttons clicked on one of our bars */
710 handle_button((xcb_button_press_event_t*) event);
712 case XCB_CLIENT_MESSAGE:
713 /* Client messages are used for client-to-client communication, for
714 * example system tray widgets talk to us directly via client messages. */
715 handle_client_message((xcb_client_message_event_t*) event);
717 case XCB_UNMAP_NOTIFY:
718 case XCB_DESTROY_NOTIFY:
719 /* UnmapNotifies are received when a tray window unmaps itself */
720 handle_unmap_notify((xcb_unmap_notify_event_t*) event);
722 case XCB_PROPERTY_NOTIFY:
724 handle_property_notify((xcb_property_notify_event_t*) event);
726 case XCB_CONFIGURE_REQUEST:
727 /* ConfigureRequest, sent by a tray child */
728 handle_configure_request((xcb_configure_request_event_t*) event);
736 * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
740 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
744 * We need to bind to the modifier per XKB. Sadly, XCB does not implement this
747 void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
751 DLOG("Got XKB-Event!\n");
753 while (XPending(xkb_dpy)) {
754 XNextEvent(xkb_dpy, (XEvent*)&ev);
756 if (ev.type != xkb_event_base) {
757 ELOG("No Xkb-Event!\n");
761 if (ev.any.xkb_type != XkbStateNotify) {
762 ELOG("No State Notify!\n");
766 unsigned int mods = ev.state.mods;
767 modstate = mods & config.modifier;
770 #define DLOGMOD(modmask, status, barfunc) \
774 DLOG("ShiftMask got " #status "!\n"); \
777 DLOG("ControlMask got " #status "!\n"); \
780 DLOG("Mod1Mask got " #status "!\n"); \
783 DLOG("Mod2Mask got " #status "!\n"); \
786 DLOG("Mod3Mask got " #status "!\n"); \
789 DLOG("Mod4Mask got " #status "!\n"); \
792 DLOG("Mod5Mask got " #status "!\n"); \
798 if (modstate != mod_pressed) {
800 DLOGMOD(config.modifier, released, hide_bars);
802 DLOGMOD(config.modifier, pressed, unhide_bars);
804 mod_pressed = modstate;
811 * Early initialization of the connection to X11: Everything which does not
812 * depend on 'config'.
815 char *init_xcb_early() {
816 /* FIXME: xcb_connect leaks Memory */
817 xcb_connection = xcb_connect(NULL, &screen);
818 if (xcb_connection_has_error(xcb_connection)) {
819 ELOG("Cannot open display\n");
822 conn = xcb_connection;
823 DLOG("Connected to xcb\n");
825 /* We have to request the atoms we need */
826 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
827 #include "xcb_atoms.def"
829 xcb_screen = xcb_aux_get_screen(xcb_connection, screen);
830 xcb_root = xcb_screen->root;
832 /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
833 * this way, we can choose to crop it */
834 uint32_t mask = XCB_GC_FOREGROUND;
835 uint32_t vals[] = { colors.bar_bg, colors.bar_bg };
837 statusline_clear = xcb_generate_id(xcb_connection);
838 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
844 statusline_ctx = xcb_generate_id(xcb_connection);
845 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
851 statusline_pm = xcb_generate_id(xcb_connection);
852 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
853 xcb_screen->root_depth,
856 xcb_screen->width_in_pixels,
857 xcb_screen->height_in_pixels);
860 /* The various Watchers to communicate with xcb */
861 xcb_io = smalloc(sizeof(ev_io));
862 xcb_prep = smalloc(sizeof(ev_prepare));
863 xcb_chk = smalloc(sizeof(ev_check));
865 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
866 ev_prepare_init(xcb_prep, &xcb_prep_cb);
867 ev_check_init(xcb_chk, &xcb_chk_cb);
869 ev_io_start(main_loop, xcb_io);
870 ev_prepare_start(main_loop, xcb_prep);
871 ev_check_start(main_loop, xcb_chk);
873 /* Now we get the atoms and save them in a nice data structure */
876 xcb_get_property_cookie_t path_cookie;
877 path_cookie = xcb_get_property_unchecked(xcb_connection,
880 atoms[I3_SOCKET_PATH],
881 XCB_GET_PROPERTY_TYPE_ANY,
884 /* We check, if i3 set its socket-path */
885 xcb_get_property_reply_t *path_reply = xcb_get_property_reply(xcb_connection,
890 int len = xcb_get_property_value_length(path_reply);
892 path = strndup(xcb_get_property_value(path_reply), len);
897 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
898 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
899 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
907 * Initialization which depends on 'config' being usable. Called after the
908 * configuration has arrived.
911 void init_xcb_late(char *fontname) {
912 if (fontname == NULL)
913 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
916 font = load_font(fontname, true);
918 DLOG("Calculated Font-height: %d\n", font.height);
920 xcb_flush(xcb_connection);
922 /* To grab modifiers without blocking other applications from receiving key-events
923 * involving that modifier, we sadly have to use xkb which is not yet fully supported
925 if (config.hide_on_modifier) {
926 int xkb_major, xkb_minor, xkb_errbase, xkb_err;
927 xkb_major = XkbMajorVersion;
928 xkb_minor = XkbMinorVersion;
930 xkb_dpy = XkbOpenDisplay(NULL,
937 if (xkb_dpy == NULL) {
942 if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
943 ELOG("Could not set FD_CLOEXEC on xkbdpy: %s\n", strerror(errno));
948 if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
949 ELOG("XKB not supported by X-server!\n");
953 if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
954 ELOG("Could not grab Key!\n");
958 xkb_io = smalloc(sizeof(ev_io));
959 ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
960 ev_io_start(main_loop, xkb_io);
966 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
967 * for the X11 display we are running on, then acquiring the selection for this
968 * atom. Afterwards, tray clients will send ClientMessages to our window.
972 DLOG("Initializing system tray functionality\n");
973 /* request the tray manager atom for the X11 display we are running on */
974 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
975 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
976 xcb_intern_atom_cookie_t tray_cookie;
977 xcb_intern_atom_reply_t *tray_reply;
978 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
980 /* tray support: we need a window to own the selection */
981 xcb_window_t selwin = xcb_generate_id(xcb_connection);
982 uint32_t selmask = XCB_CW_OVERRIDE_REDIRECT;
983 uint32_t selval[] = { 1 };
984 xcb_create_window(xcb_connection,
985 xcb_screen->root_depth,
991 XCB_WINDOW_CLASS_INPUT_OUTPUT,
992 xcb_screen->root_visual,
996 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
998 xcb_change_property(xcb_connection,
999 XCB_PROP_MODE_REPLACE,
1001 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1007 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1008 ELOG("Could not get atom %s\n", atomname);
1012 xcb_set_selection_owner(xcb_connection,
1017 /* Verify that we have the selection */
1018 xcb_get_selection_owner_cookie_t selcookie;
1019 xcb_get_selection_owner_reply_t *selreply;
1021 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1022 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1023 ELOG("Could not get selection owner for %s\n", atomname);
1027 if (selreply->owner != selwin) {
1028 ELOG("Could not set the %s selection. " \
1029 "Maybe another tray is already running?\n", atomname);
1030 /* NOTE that this error is not fatal. We just can’t provide tray
1036 /* Inform clients waiting for a new _NET_SYSTEM_TRAY that we are here */
1037 void *event = scalloc(32);
1038 xcb_client_message_event_t *ev = event;
1039 ev->response_type = XCB_CLIENT_MESSAGE;
1040 ev->window = xcb_root;
1041 ev->type = atoms[MANAGER];
1043 ev->data.data32[0] = XCB_CURRENT_TIME;
1044 ev->data.data32[1] = tray_reply->atom;
1045 ev->data.data32[2] = selwin;
1046 xcb_send_event(xcb_connection,
1049 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
1056 * Cleanup the xcb-stuff.
1057 * Called once, before the program terminates.
1063 SLIST_FOREACH(o_walk, outputs, slist) {
1064 destroy_window(o_walk);
1065 FREE(o_walk->trayclients);
1066 FREE(o_walk->workspaces);
1069 FREE_SLIST(outputs, i3_output);
1072 xcb_flush(xcb_connection);
1073 xcb_disconnect(xcb_connection);
1075 ev_check_stop(main_loop, xcb_chk);
1076 ev_prepare_stop(main_loop, xcb_prep);
1077 ev_io_stop(main_loop, xcb_io);
1085 * Get the earlier requested atoms and save them in the prepared data structure
1089 xcb_intern_atom_reply_t *reply;
1090 #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1091 if (reply == NULL) { \
1092 ELOG("Could not get atom %s\n", #name); \
1093 exit(EXIT_FAILURE); \
1095 atoms[name] = reply->atom; \
1098 #include "xcb_atoms.def"
1099 DLOG("Got Atoms\n");
1103 * Reparents all tray clients of the specified output to the root window. This
1104 * is either used when shutting down, when an output appears (xrandr --output
1105 * VGA1 --off) or when the primary output changes.
1107 * Applications using the tray will start the protocol from the beginning again
1111 void kick_tray_clients(i3_output *output) {
1112 trayclient *trayclient;
1113 while (!TAILQ_EMPTY(output->trayclients)) {
1114 trayclient = TAILQ_FIRST(output->trayclients);
1115 /* Unmap, then reparent (to root) the tray client windows */
1116 xcb_unmap_window(xcb_connection, trayclient->win);
1117 xcb_reparent_window(xcb_connection,
1123 /* We remove the trayclient right here. We might receive an UnmapNotify
1124 * event afterwards, but better safe than sorry. */
1125 TAILQ_REMOVE(output->trayclients, trayclient, tailq);
1130 * Destroy the bar of the specified output
1133 void destroy_window(i3_output *output) {
1134 if (output == NULL) {
1137 if (output->bar == XCB_NONE) {
1141 kick_tray_clients(output);
1142 xcb_destroy_window(xcb_connection, output->bar);
1143 output->bar = XCB_NONE;
1147 * Reallocate the statusline-buffer
1150 void realloc_sl_buffer() {
1151 DLOG("Re-allocating statusline-buffer, statusline_width = %d, xcb_screen->width_in_pixels = %d\n",
1152 statusline_width, xcb_screen->width_in_pixels);
1153 xcb_free_pixmap(xcb_connection, statusline_pm);
1154 statusline_pm = xcb_generate_id(xcb_connection);
1155 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1156 xcb_screen->root_depth,
1159 MAX(xcb_screen->width_in_pixels, statusline_width),
1160 xcb_screen->height_in_pixels);
1162 uint32_t mask = XCB_GC_FOREGROUND;
1163 uint32_t vals[2] = { colors.bar_bg, colors.bar_bg };
1164 xcb_free_gc(xcb_connection, statusline_clear);
1165 statusline_clear = xcb_generate_id(xcb_connection);
1166 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1172 mask |= XCB_GC_BACKGROUND;
1173 vals[0] = colors.bar_fg;
1174 statusline_ctx = xcb_generate_id(xcb_connection);
1175 xcb_free_gc(xcb_connection, statusline_ctx);
1176 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1182 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
1183 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
1184 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
1191 * Reconfigure all bars and create new bars for recently activated outputs
1194 void reconfig_windows() {
1197 static bool tray_configured = false;
1200 SLIST_FOREACH(walk, outputs, slist) {
1201 if (!walk->active) {
1202 /* If an output is not active, we destroy its bar */
1203 /* FIXME: Maybe we rather want to unmap? */
1204 DLOG("Destroying window for output %s\n", walk->name);
1205 destroy_window(walk);
1208 if (walk->bar == XCB_NONE) {
1209 DLOG("Creating Window for output %s\n", walk->name);
1211 walk->bar = xcb_generate_id(xcb_connection);
1212 walk->buffer = xcb_generate_id(xcb_connection);
1213 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
1214 /* Black background */
1215 values[0] = colors.bar_bg;
1216 /* If hide_on_modifier is set, i3 is not supposed to manage our bar-windows */
1217 values[1] = config.hide_on_modifier;
1218 /* We enable the following EventMask fields:
1219 * EXPOSURE, to get expose events (we have to re-draw then)
1220 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1221 * child windows use ConfigureWindow
1222 * BUTTON_PRESS, to handle clicks on the workspace buttons
1224 values[2] = XCB_EVENT_MASK_EXPOSURE |
1225 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
1226 if (!config.disable_ws) {
1227 values[2] |= XCB_EVENT_MASK_BUTTON_PRESS;
1229 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1230 xcb_screen->root_depth,
1233 walk->rect.x, walk->rect.y + walk->rect.h - font.height - 6,
1234 walk->rect.w, font.height + 6,
1236 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1237 xcb_screen->root_visual,
1241 /* The double-buffer we use to render stuff off-screen */
1242 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1243 xcb_screen->root_depth,
1249 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1250 xcb_void_cookie_t class_cookie;
1251 class_cookie = xcb_change_property(xcb_connection,
1252 XCB_PROP_MODE_REPLACE,
1257 (strlen("i3bar") + 1) * 2,
1261 if (asprintf(&name, "i3bar for output %s", walk->name) == -1)
1262 err(EXIT_FAILURE, "asprintf()");
1263 xcb_void_cookie_t name_cookie;
1264 name_cookie = xcb_change_property(xcb_connection,
1265 XCB_PROP_MODE_REPLACE,
1274 /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
1276 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1277 XCB_PROP_MODE_REPLACE,
1279 atoms[_NET_WM_WINDOW_TYPE],
1283 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1285 /* We need to tell i3, where to reserve space for i3bar */
1286 /* left, right, top, bottom, left_start_y, left_end_y,
1287 * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
1289 /* A local struct to save the strut_partial property */
1295 uint32_t left_start_y;
1296 uint32_t left_end_y;
1297 uint32_t right_start_y;
1298 uint32_t right_end_y;
1299 uint32_t top_start_x;
1301 uint32_t bottom_start_x;
1302 uint32_t bottom_end_x;
1303 } __attribute__((__packed__)) strut_partial = {0,};
1304 switch (config.position) {
1308 strut_partial.top = font.height + 6;
1309 strut_partial.top_start_x = walk->rect.x;
1310 strut_partial.top_end_x = walk->rect.x + walk->rect.w;
1313 strut_partial.bottom = font.height + 6;
1314 strut_partial.bottom_start_x = walk->rect.x;
1315 strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
1318 xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
1319 XCB_PROP_MODE_REPLACE,
1321 atoms[_NET_WM_STRUT_PARTIAL],
1327 /* We also want a graphics-context for the bars (it defines the properties
1328 * with which we draw to them) */
1329 walk->bargc = xcb_generate_id(xcb_connection);
1330 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
1336 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1337 xcb_void_cookie_t map_cookie;
1338 if (!config.hide_on_modifier) {
1339 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1342 if (xcb_request_failed(win_cookie, "Could not create window") ||
1343 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1344 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1345 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1346 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1347 xcb_request_failed(strut_cookie, "Could not set strut") ||
1348 xcb_request_failed(gc_cookie, "Could not create graphical context") ||
1349 (!config.hide_on_modifier && xcb_request_failed(map_cookie, "Could not map window"))) {
1353 if (!tray_configured &&
1354 (!config.tray_output ||
1355 strcasecmp("none", config.tray_output) != 0)) {
1357 tray_configured = true;
1360 /* We already have a bar, so we just reconfigure it */
1361 mask = XCB_CONFIG_WINDOW_X |
1362 XCB_CONFIG_WINDOW_Y |
1363 XCB_CONFIG_WINDOW_WIDTH |
1364 XCB_CONFIG_WINDOW_HEIGHT |
1365 XCB_CONFIG_WINDOW_STACK_MODE;
1366 values[0] = walk->rect.x;
1367 values[1] = walk->rect.y + walk->rect.h - font.height - 6;
1368 values[2] = walk->rect.w;
1369 values[3] = font.height + 6;
1370 values[4] = XCB_STACK_MODE_ABOVE;
1372 DLOG("Destroying buffer for output %s", walk->name);
1373 xcb_free_pixmap(xcb_connection, walk->buffer);
1375 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1376 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1381 DLOG("Recreating buffer for output %s", walk->name);
1382 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1383 xcb_screen->root_depth,
1389 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window")) {
1392 if (xcb_request_failed(pm_cookie, "Could not create pixmap")) {
1400 * Render the bars, with buttons and statusline
1404 DLOG("Drawing Bars...\n");
1407 refresh_statusline();
1409 i3_output *outputs_walk;
1410 SLIST_FOREACH(outputs_walk, outputs, slist) {
1411 if (!outputs_walk->active) {
1412 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1415 if (outputs_walk->bar == XCB_NONE) {
1416 /* Oh shit, an active output without an own bar. Create it now! */
1419 /* First things first: clear the backbuffer */
1420 uint32_t color = colors.bar_bg;
1421 xcb_change_gc(xcb_connection,
1422 outputs_walk->bargc,
1425 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font.height + 6 };
1426 xcb_poly_fill_rectangle(xcb_connection,
1427 outputs_walk->buffer,
1428 outputs_walk->bargc,
1432 if (!TAILQ_EMPTY(&statusline_head)) {
1433 DLOG("Printing statusline!\n");
1435 /* Luckily we already prepared a seperate pixmap containing the rendered
1436 * statusline, we just have to copy the relevant parts to the relevant
1438 trayclient *trayclient;
1440 TAILQ_FOREACH(trayclient, outputs_walk->trayclients, tailq) {
1441 if (!trayclient->mapped)
1443 /* We assume the tray icons are quadratic (we use the font
1444 * *height* as *width* of the icons) because we configured them
1446 traypx += font.height + 2;
1448 /* Add 2px of padding if there are any tray icons */
1451 xcb_copy_area(xcb_connection,
1453 outputs_walk->buffer,
1454 outputs_walk->bargc,
1455 MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
1456 MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - traypx - 4)), 3,
1457 MIN(outputs_walk->rect.w - traypx - 4, statusline_width), font.height);
1460 if (config.disable_ws) {
1465 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1466 DLOG("Drawing Button for WS %s at x = %d, len = %d\n", ws_walk->name, i, ws_walk->name_width);
1467 uint32_t fg_color = colors.inactive_ws_fg;
1468 uint32_t bg_color = colors.inactive_ws_bg;
1469 uint32_t border_color = colors.inactive_ws_border;
1470 if (ws_walk->visible) {
1471 if (!ws_walk->focused) {
1472 fg_color = colors.active_ws_fg;
1473 bg_color = colors.active_ws_bg;
1474 border_color = colors.active_ws_border;
1476 fg_color = colors.focus_ws_fg;
1477 bg_color = colors.focus_ws_bg;
1478 border_color = colors.focus_ws_border;
1481 if (ws_walk->urgent) {
1482 DLOG("WS %s is urgent!\n", ws_walk->name);
1483 fg_color = colors.urgent_ws_fg;
1484 bg_color = colors.urgent_ws_bg;
1485 border_color = colors.urgent_ws_border;
1486 /* The urgent-hint should get noticed, so we unhide the bars shortly */
1489 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1490 uint32_t vals_border[] = { border_color, border_color };
1491 xcb_change_gc(xcb_connection,
1492 outputs_walk->bargc,
1495 xcb_rectangle_t rect_border = { i, 0, ws_walk->name_width + 10, font.height + 4 };
1496 xcb_poly_fill_rectangle(xcb_connection,
1497 outputs_walk->buffer,
1498 outputs_walk->bargc,
1501 uint32_t vals[] = { bg_color, bg_color };
1502 xcb_change_gc(xcb_connection,
1503 outputs_walk->bargc,
1506 xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font.height + 2 };
1507 xcb_poly_fill_rectangle(xcb_connection,
1508 outputs_walk->buffer,
1509 outputs_walk->bargc,
1512 set_font_colors(outputs_walk->bargc, fg_color, bg_color);
1513 draw_text((char*)ws_walk->ucs2_name, ws_walk->name_glyphs, true,
1514 outputs_walk->buffer, outputs_walk->bargc, i + 5, 2, ws_walk->name_width);
1515 i += 10 + ws_walk->name_width + 1;
1525 * Redraw the bars, i.e. simply copy the buffer to the barwindow
1528 void redraw_bars() {
1529 i3_output *outputs_walk;
1530 SLIST_FOREACH(outputs_walk, outputs, slist) {
1531 if (!outputs_walk->active) {
1534 xcb_copy_area(xcb_connection,
1535 outputs_walk->buffer,
1537 outputs_walk->bargc,
1540 outputs_walk->rect.w,
1541 outputs_walk->rect.h);
1542 xcb_flush(xcb_connection);