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>
15 #include "xcb_compat.h"
30 #include <X11/XKBlib.h>
31 #include <X11/extensions/XKB.h>
36 /* We save the Atoms in an easy to access array, indexed by an enum */
38 #define ATOM_DO(name) name,
39 #include "xcb_atoms.def"
43 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
44 xcb_atom_t atoms[NUM_ATOMS];
46 /* Variables, that are the same for all functions at all times */
47 xcb_connection_t *xcb_connection;
49 xcb_screen_t *xcb_screen;
50 xcb_window_t xcb_root;
53 /* We need to cache some data to speed up text-width-prediction */
54 xcb_query_font_reply_t *font_info;
56 xcb_charinfo_t *font_table;
58 /* These are only relevant for XKB, which we only need for grabbing modifiers */
63 /* Because the statusline is the same on all outputs, we have
64 * global buffer to render it on */
65 xcb_gcontext_t statusline_ctx;
66 xcb_gcontext_t statusline_clear;
67 xcb_pixmap_t statusline_pm;
68 uint32_t statusline_width;
70 /* Event-Watchers, to interact with the user */
76 /* The parsed colors */
80 uint32_t active_ws_fg;
81 uint32_t active_ws_bg;
82 uint32_t inactive_ws_fg;
83 uint32_t inactive_ws_bg;
84 uint32_t urgent_ws_bg;
85 uint32_t urgent_ws_fg;
89 struct xcb_colors_t colors;
91 /* We define xcb_request_failed as a macro to include the relevant line-number */
92 #define xcb_request_failed(cookie, err_msg) _xcb_request_failed(cookie, err_msg, __LINE__)
93 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
94 xcb_generic_error_t *err;
95 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
96 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
97 return err->error_code;
103 * Predicts the length of text based on cached data.
104 * The string has to be encoded in ucs2 and glyph_len has to be the length
105 * of the string (in glyphs).
108 uint32_t predict_text_extents(xcb_char2b_t *text, uint32_t length) {
109 /* If we don't have per-character data, return the maximum width */
110 if (font_table == NULL) {
111 return (font_info->max_bounds.character_width * length);
117 for (i = 0; i < length; i++) {
118 xcb_charinfo_t *info;
119 int row = text[i].byte1;
120 int col = text[i].byte2;
122 if (row < font_info->min_byte1 || row > font_info->max_byte1 ||
123 col < font_info->min_char_or_byte2 || col > font_info->max_char_or_byte2) {
127 /* Don't you ask me, how this one works… */
128 info = &font_table[((row - font_info->min_byte1) *
129 (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) +
130 (col - font_info->min_char_or_byte2)];
132 if (info->character_width != 0 ||
133 (info->right_side_bearing |
134 info->left_side_bearing |
136 info->descent) != 0) {
137 width += info->character_width;
145 * Draws text given in UCS-2-encoding to a given drawable and position
148 void draw_text(xcb_drawable_t drawable, xcb_gcontext_t ctx, int16_t x, int16_t y,
149 xcb_char2b_t *text, uint32_t glyph_count) {
152 int16_t font_ascent = font_info->font_ascent;
154 while (glyph_count > 0) {
155 uint8_t chunk_size = MIN(255, glyph_count);
156 uint32_t chunk_width = predict_text_extents(text + offset, chunk_size);
158 xcb_image_text_16(xcb_connection,
162 pos_x, y + font_ascent,
165 offset += chunk_size;
166 pos_x += chunk_width;
167 glyph_count -= chunk_size;
172 * Redraws the statusline to the buffer
175 void refresh_statusline() {
178 if (statusline == NULL) {
182 xcb_char2b_t *text = (xcb_char2b_t*) convert_utf8_to_ucs2(statusline, &glyph_count);
183 uint32_t old_statusline_width = statusline_width;
184 statusline_width = predict_text_extents(text, glyph_count);
185 /* If the statusline is bigger than our screen we need to make sure that
186 * the pixmap provides enough space, so re-allocate if the width grew */
187 if (statusline_width > xcb_screen->width_in_pixels &&
188 statusline_width > old_statusline_width)
191 xcb_rectangle_t rect = { 0, 0, xcb_screen->width_in_pixels, font_height };
192 xcb_poly_fill_rectangle(xcb_connection, statusline_pm, statusline_clear, 1, &rect);
193 draw_text(statusline_pm, statusline_ctx, 0, 0, text, glyph_count);
199 * Hides all bars (unmaps them)
203 if (!config.hide_on_modifier) {
208 SLIST_FOREACH(walk, outputs, slist) {
212 xcb_unmap_window(xcb_connection, walk->bar);
218 * Unhides all bars (maps them)
222 if (!config.hide_on_modifier) {
227 xcb_void_cookie_t cookie;
233 SLIST_FOREACH(walk, outputs, slist) {
234 if (walk->bar == XCB_NONE) {
237 mask = XCB_CONFIG_WINDOW_X |
238 XCB_CONFIG_WINDOW_Y |
239 XCB_CONFIG_WINDOW_WIDTH |
240 XCB_CONFIG_WINDOW_HEIGHT |
241 XCB_CONFIG_WINDOW_STACK_MODE;
242 values[0] = walk->rect.x;
243 if (config.position == POS_TOP)
244 values[1] = walk->rect.y;
245 else values[1] = walk->rect.y + walk->rect.h - font_height - 6;
246 values[2] = walk->rect.w;
247 values[3] = font_height + 6;
248 values[4] = XCB_STACK_MODE_ABOVE;
249 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
250 cookie = xcb_configure_window_checked(xcb_connection,
255 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
258 xcb_map_window(xcb_connection, walk->bar);
263 * Parse the colors into a format that we can use
266 void init_colors(const struct xcb_color_strings_t *new_colors) {
267 #define PARSE_COLOR(name, def) \
269 colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
271 PARSE_COLOR(bar_fg, "#FFFFFF");
272 PARSE_COLOR(bar_bg, "#000000");
273 PARSE_COLOR(active_ws_fg, "#FFFFFF");
274 PARSE_COLOR(active_ws_bg, "#333333");
275 PARSE_COLOR(inactive_ws_fg, "#888888");
276 PARSE_COLOR(inactive_ws_bg, "#222222");
277 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
278 PARSE_COLOR(urgent_ws_bg, "#900000");
279 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
280 PARSE_COLOR(focus_ws_bg, "#285577");
285 * Handle a button-press-event (i.e. a mouse click on one of our bars).
286 * We determine, whether the click occured on a ws-button or if the scroll-
287 * wheel was used and change the workspace appropriately
290 void handle_button(xcb_button_press_event_t *event) {
293 /* Determine, which bar was clicked */
295 xcb_window_t bar = event->event;
296 SLIST_FOREACH(walk, outputs, slist) {
297 if (walk->bar == bar) {
303 DLOG("Unknown Bar klicked!\n");
307 /* TODO: Move this to extern get_ws_for_output() */
308 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
309 if (cur_ws->visible) {
314 if (cur_ws == NULL) {
315 DLOG("No Workspace active?\n");
319 int32_t x = event->event_x;
321 DLOG("Got Button %d\n", event->detail);
323 switch (event->detail) {
325 /* Left Mousbutton. We determine, which button was clicked
326 * and set cur_ws accordingly */
327 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
329 if (x < cur_ws->name_width + 10) {
332 x -= cur_ws->name_width + 10;
334 if (cur_ws == NULL) {
339 /* Mouse wheel down. We select the next ws */
340 if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
341 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
343 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
347 /* Mouse wheel up. We select the previos ws */
348 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
349 cur_ws = TAILQ_FIRST(walk->workspaces);
351 cur_ws = TAILQ_NEXT(cur_ws, tailq);
356 const size_t len = strlen(cur_ws->name) + strlen("workspace \"\"") + 1;
358 snprintf(buffer, len, "workspace \"%s\"", cur_ws->name);
359 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
363 * Configures the x coordinate of all trayclients. To be called after adding a
364 * new tray client or removing an old one.
367 static void configure_trayclients() {
368 trayclient *trayclient;
370 SLIST_FOREACH(output, outputs, slist) {
375 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
376 if (!trayclient->mapped)
380 DLOG("Configuring tray window %08x to x=%d\n",
381 trayclient->win, output->rect.w - (clients * (font_height + 2)));
382 uint32_t x = output->rect.w - (clients * (font_height + 2));
383 xcb_configure_window(xcb_connection,
392 * Handles ClientMessages (messages sent from another client directly to us).
394 * At the moment, only the tray window will receive client messages. All
395 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
398 static void handle_client_message(xcb_client_message_event_t* event) {
399 if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
400 event->format == 32) {
401 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
402 /* event->data.data32[0] is the timestamp */
403 uint32_t op = event->data.data32[1];
406 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
407 xcb_window_t client = event->data.data32[2];
409 /* Listen for PropertyNotify events to get the most recent value of
410 * the XEMBED_MAPPED atom, also listen for UnmapNotify events */
411 mask = XCB_CW_EVENT_MASK;
412 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
413 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
414 xcb_change_window_attributes(xcb_connection,
419 /* Request the _XEMBED_INFO property. The XEMBED specification
420 * (which is referred by the tray specification) says this *has* to
421 * be set, but VLC does not set it… */
424 xcb_get_property_cookie_t xembedc;
425 xembedc = xcb_get_property_unchecked(xcb_connection,
429 XCB_GET_PROPERTY_TYPE_ANY,
433 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
436 if (xembedr != NULL && xembedr->length != 0) {
437 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
438 uint32_t *xembed = xcb_get_property_value(xembedr);
439 DLOG("xembed version = %d\n", xembed[0]);
440 DLOG("xembed flags = %d\n", xembed[1]);
441 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
442 xe_version = xembed[0];
447 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
450 DLOG("X window %08x requested docking\n", client);
451 i3_output *walk, *output = NULL;
452 SLIST_FOREACH(walk, outputs, slist) {
455 if (config.tray_output &&
456 strcasecmp(walk->name, config.tray_output) != 0)
458 DLOG("using output %s\n", walk->name);
461 if (output == NULL) {
462 ELOG("No output found\n");
465 xcb_reparent_window(xcb_connection,
468 output->rect.w - font_height - 2,
470 /* We reconfigure the window to use a reasonable size. The systray
471 * specification explicitly says:
472 * Tray icons may be assigned any size by the system tray, and
473 * should do their best to cope with any size effectively
475 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
476 values[0] = font_height;
477 values[1] = font_height;
478 xcb_configure_window(xcb_connection,
483 /* send the XEMBED_EMBEDDED_NOTIFY message */
484 void *event = scalloc(32);
485 xcb_client_message_event_t *ev = event;
486 ev->response_type = XCB_CLIENT_MESSAGE;
488 ev->type = atoms[_XEMBED];
490 ev->data.data32[0] = XCB_CURRENT_TIME;
491 ev->data.data32[1] = atoms[XEMBED_EMBEDDED_NOTIFY];
492 ev->data.data32[2] = output->bar;
493 ev->data.data32[3] = xe_version;
494 xcb_send_event(xcb_connection,
497 XCB_EVENT_MASK_NO_EVENT,
501 /* Put the client inside the save set. Upon termination (whether
502 * killed or normal exit does not matter) of i3bar, these clients
503 * will be correctly reparented to their most closest living
504 * ancestor. Without this, tray icons might die when i3bar
506 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
509 DLOG("Mapping dock client\n");
510 xcb_map_window(xcb_connection, client);
512 DLOG("Not mapping dock client yet\n");
514 trayclient *tc = smalloc(sizeof(trayclient));
517 tc->xe_version = xe_version;
518 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
520 /* Trigger an update to copy the statusline text to the appropriate
522 configure_trayclients();
529 * Handles UnmapNotify events. These events happen when a tray window unmaps
530 * itself. We then update our data structure
533 static void handle_unmap_notify(xcb_unmap_notify_event_t* event) {
534 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
537 SLIST_FOREACH(walk, outputs, slist) {
540 DLOG("checking output %s\n", walk->name);
541 trayclient *trayclient;
542 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
543 if (trayclient->win != event->window)
546 DLOG("Removing tray client with window ID %08x\n", event->window);
547 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
549 /* Trigger an update, we now have more space for the statusline */
550 configure_trayclients();
558 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
559 * handled, which tells us whether a dock client should be mapped or unmapped.
562 static void handle_property_notify(xcb_property_notify_event_t *event) {
563 DLOG("PropertyNotify\n");
564 if (event->atom == atoms[_XEMBED_INFO] &&
565 event->state == XCB_PROPERTY_NEW_VALUE) {
566 DLOG("xembed_info updated\n");
567 trayclient *trayclient = NULL, *walk;
569 SLIST_FOREACH(o_walk, outputs, slist) {
573 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
574 if (walk->win != event->window)
584 ELOG("PropertyNotify received for unknown window %08x\n",
588 xcb_get_property_cookie_t xembedc;
589 xembedc = xcb_get_property_unchecked(xcb_connection,
593 XCB_GET_PROPERTY_TYPE_ANY,
597 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
600 if (xembedr == NULL || xembedr->length == 0) {
601 DLOG("xembed_info unset\n");
605 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
606 uint32_t *xembed = xcb_get_property_value(xembedr);
607 DLOG("xembed version = %d\n", xembed[0]);
608 DLOG("xembed flags = %d\n", xembed[1]);
609 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
610 DLOG("map-state now %d\n", map_it);
611 if (trayclient->mapped && !map_it) {
612 /* need to unmap the window */
613 xcb_unmap_window(xcb_connection, trayclient->win);
614 trayclient->mapped = map_it;
615 configure_trayclients();
617 } else if (!trayclient->mapped && map_it) {
618 /* need to map the window */
619 xcb_map_window(xcb_connection, trayclient->win);
620 trayclient->mapped = map_it;
621 configure_trayclients();
629 * Handle ConfigureRequests by denying them and sending the client a
630 * ConfigureNotify with its actual size.
633 static void handle_configure_request(xcb_configure_request_event_t *event) {
634 DLOG("ConfigureRequest for window = %08x\n", event->window);
636 trayclient *trayclient;
638 SLIST_FOREACH(output, outputs, slist) {
643 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
644 if (!trayclient->mapped)
648 if (trayclient->win != event->window)
651 xcb_rectangle_t rect;
652 rect.x = output->rect.w - (clients * (font_height + 2));
654 rect.width = font_height;
655 rect.height = font_height;
657 DLOG("This is a tray window. x = %d\n", rect.x);
658 fake_configure_notify(xcb_connection, rect, event->window, 0);
663 DLOG("WARNING: Could not find corresponding tray window.\n");
667 * This function is called immediately before the main loop locks. We flush xcb
668 * then (and only then)
671 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
672 xcb_flush(xcb_connection);
676 * This function is called immediately after the main loop locks, so when one
677 * of the watchers registered an event.
678 * We check whether an X-Event arrived and handle it.
681 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
682 xcb_generic_event_t *event;
684 if (xcb_connection_has_error(xcb_connection)) {
685 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
689 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
690 switch (event->response_type & ~0x80) {
692 /* Expose-events happen, when the window needs to be redrawn */
695 case XCB_BUTTON_PRESS:
696 /* Button-press-events are mouse-buttons clicked on one of our bars */
697 handle_button((xcb_button_press_event_t*) event);
699 case XCB_CLIENT_MESSAGE:
700 /* Client messages are used for client-to-client communication, for
701 * example system tray widgets talk to us directly via client messages. */
702 handle_client_message((xcb_client_message_event_t*) event);
704 case XCB_UNMAP_NOTIFY:
705 case XCB_DESTROY_NOTIFY:
706 /* UnmapNotifies are received when a tray window unmaps itself */
707 handle_unmap_notify((xcb_unmap_notify_event_t*) event);
709 case XCB_PROPERTY_NOTIFY:
711 handle_property_notify((xcb_property_notify_event_t*) event);
713 case XCB_CONFIGURE_REQUEST:
714 /* ConfigureRequest, sent by a tray child */
715 handle_configure_request((xcb_configure_request_event_t*) event);
723 * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
727 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
731 * We need to bind to the modifier per XKB. Sadly, XCB does not implement this
734 void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
738 DLOG("Got XKB-Event!\n");
740 while (XPending(xkb_dpy)) {
741 XNextEvent(xkb_dpy, (XEvent*)&ev);
743 if (ev.type != xkb_event_base) {
744 ELOG("No Xkb-Event!\n");
748 if (ev.any.xkb_type != XkbStateNotify) {
749 ELOG("No State Notify!\n");
753 unsigned int mods = ev.state.mods;
754 modstate = mods & Mod4Mask;
757 if (modstate != mod_pressed) {
759 DLOG("Mod4 got released!\n");
762 DLOG("Mod4 got pressed!\n");
765 mod_pressed = modstate;
770 * Early initialization of the connection to X11: Everything which does not
771 * depend on 'config'.
774 char *init_xcb_early() {
775 /* FIXME: xcb_connect leaks Memory */
776 xcb_connection = xcb_connect(NULL, &screen);
777 if (xcb_connection_has_error(xcb_connection)) {
778 ELOG("Cannot open display\n");
781 DLOG("Connected to xcb\n");
783 /* We have to request the atoms we need */
784 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
785 #include "xcb_atoms.def"
787 xcb_screen = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
788 xcb_root = xcb_screen->root;
790 /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
791 * this way, we can choose to crop it */
792 uint32_t mask = XCB_GC_FOREGROUND;
793 uint32_t vals[] = { colors.bar_bg, colors.bar_bg };
795 statusline_clear = xcb_generate_id(xcb_connection);
796 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
802 mask |= XCB_GC_BACKGROUND;
803 vals[0] = colors.bar_fg;
804 statusline_ctx = xcb_generate_id(xcb_connection);
805 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
811 statusline_pm = xcb_generate_id(xcb_connection);
812 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
813 xcb_screen->root_depth,
816 xcb_screen->width_in_pixels,
817 xcb_screen->height_in_pixels);
820 /* The various Watchers to communicate with xcb */
821 xcb_io = smalloc(sizeof(ev_io));
822 xcb_prep = smalloc(sizeof(ev_prepare));
823 xcb_chk = smalloc(sizeof(ev_check));
825 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
826 ev_prepare_init(xcb_prep, &xcb_prep_cb);
827 ev_check_init(xcb_chk, &xcb_chk_cb);
829 ev_io_start(main_loop, xcb_io);
830 ev_prepare_start(main_loop, xcb_prep);
831 ev_check_start(main_loop, xcb_chk);
833 /* Now we get the atoms and save them in a nice data structure */
836 xcb_get_property_cookie_t path_cookie;
837 path_cookie = xcb_get_property_unchecked(xcb_connection,
840 atoms[I3_SOCKET_PATH],
841 XCB_GET_PROPERTY_TYPE_ANY,
844 /* We check, if i3 set its socket-path */
845 xcb_get_property_reply_t *path_reply = xcb_get_property_reply(xcb_connection,
850 int len = xcb_get_property_value_length(path_reply);
852 path = strndup(xcb_get_property_value(path_reply), len);
857 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
858 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
859 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
867 * Initialization which depends on 'config' being usable. Called after the
868 * configuration has arrived.
871 void init_xcb_late(char *fontname) {
872 if (fontname == NULL) {
873 /* XXX: font fallback to 'misc' like i3 does it would be good. */
874 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
877 /* We load and allocate the font */
878 xcb_font = xcb_generate_id(xcb_connection);
879 xcb_void_cookie_t open_font_cookie;
880 open_font_cookie = xcb_open_font_checked(xcb_connection,
885 /* We need to save info about the font, because we need the font's height and
886 * information about the width of characters */
887 xcb_query_font_cookie_t query_font_cookie;
888 query_font_cookie = xcb_query_font(xcb_connection,
891 xcb_change_gc(xcb_connection,
894 (uint32_t[]){ xcb_font });
896 xcb_flush(xcb_connection);
898 /* To grab modifiers without blocking other applications from receiving key-events
899 * involving that modifier, we sadly have to use xkb which is not yet fully supported
901 if (config.hide_on_modifier) {
902 int xkb_major, xkb_minor, xkb_errbase, xkb_err;
903 xkb_major = XkbMajorVersion;
904 xkb_minor = XkbMinorVersion;
906 xkb_dpy = XkbOpenDisplay(NULL,
913 if (xkb_dpy == NULL) {
918 if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
919 ELOG("Could not set FD_CLOEXEC on xkbdpy: %s\n", strerror(errno));
924 if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
925 ELOG("XKB not supported by X-server!\n");
929 if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
930 ELOG("Could not grab Key!\n");
934 xkb_io = smalloc(sizeof(ev_io));
935 ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
936 ev_io_start(main_loop, xkb_io);
940 /* Now we save the font-infos */
941 font_info = xcb_query_font_reply(xcb_connection,
945 if (xcb_request_failed(open_font_cookie, "Could not open font")) {
949 font_height = font_info->font_ascent + font_info->font_descent;
951 if (xcb_query_font_char_infos_length(font_info) == 0) {
954 font_table = xcb_query_font_char_infos(font_info);
957 DLOG("Calculated Font-height: %d\n", font_height);
961 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
962 * for the X11 display we are running on, then acquiring the selection for this
963 * atom. Afterwards, tray clients will send ClientMessages to our window.
967 DLOG("Initializing system tray functionality\n");
968 /* request the tray manager atom for the X11 display we are running on */
969 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
970 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
971 xcb_intern_atom_cookie_t tray_cookie;
972 xcb_intern_atom_reply_t *tray_reply;
973 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
975 /* tray support: we need a window to own the selection */
976 xcb_window_t selwin = xcb_generate_id(xcb_connection);
977 uint32_t selmask = XCB_CW_OVERRIDE_REDIRECT;
978 uint32_t selval[] = { 1 };
979 xcb_create_window(xcb_connection,
980 xcb_screen->root_depth,
986 XCB_WINDOW_CLASS_INPUT_OUTPUT,
987 xcb_screen->root_visual,
991 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
993 xcb_change_property(xcb_connection,
994 XCB_PROP_MODE_REPLACE,
996 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1002 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1003 ELOG("Could not get atom %s\n", atomname);
1007 xcb_set_selection_owner(xcb_connection,
1012 /* Verify that we have the selection */
1013 xcb_get_selection_owner_cookie_t selcookie;
1014 xcb_get_selection_owner_reply_t *selreply;
1016 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1017 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1018 ELOG("Could not get selection owner for %s\n", atomname);
1022 if (selreply->owner != selwin) {
1023 ELOG("Could not set the %s selection. " \
1024 "Maybe another tray is already running?\n", atomname);
1025 /* NOTE that this error is not fatal. We just can’t provide tray
1031 /* Inform clients waiting for a new _NET_SYSTEM_TRAY that we are here */
1032 void *event = scalloc(32);
1033 xcb_client_message_event_t *ev = event;
1034 ev->response_type = XCB_CLIENT_MESSAGE;
1035 ev->window = xcb_root;
1036 ev->type = atoms[MANAGER];
1038 ev->data.data32[0] = XCB_CURRENT_TIME;
1039 ev->data.data32[1] = tray_reply->atom;
1040 ev->data.data32[2] = selwin;
1041 xcb_send_event(xcb_connection,
1044 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
1051 * Cleanup the xcb-stuff.
1052 * Called once, before the program terminates.
1057 trayclient *trayclient;
1059 SLIST_FOREACH(o_walk, outputs, slist) {
1060 TAILQ_FOREACH(trayclient, o_walk->trayclients, tailq) {
1061 /* Unmap, then reparent (to root) the tray client windows */
1062 xcb_unmap_window(xcb_connection, trayclient->win);
1063 xcb_reparent_window(xcb_connection,
1069 destroy_window(o_walk);
1070 FREE(o_walk->trayclients);
1071 FREE(o_walk->workspaces);
1074 FREE_SLIST(outputs, i3_output);
1077 xcb_flush(xcb_connection);
1078 xcb_disconnect(xcb_connection);
1080 ev_check_stop(main_loop, xcb_chk);
1081 ev_prepare_stop(main_loop, xcb_prep);
1082 ev_io_stop(main_loop, xcb_io);
1091 * Get the earlier requested atoms and save them in the prepared data structure
1095 xcb_intern_atom_reply_t *reply;
1096 #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1097 if (reply == NULL) { \
1098 ELOG("Could not get atom %s\n", #name); \
1099 exit(EXIT_FAILURE); \
1101 atoms[name] = reply->atom; \
1104 #include "xcb_atoms.def"
1105 DLOG("Got Atoms\n");
1109 * Destroy the bar of the specified output
1112 void destroy_window(i3_output *output) {
1113 if (output == NULL) {
1116 if (output->bar == XCB_NONE) {
1119 xcb_destroy_window(xcb_connection, output->bar);
1120 output->bar = XCB_NONE;
1124 * Reallocate the statusline-buffer
1127 void realloc_sl_buffer() {
1128 DLOG("Re-allocating statusline-buffer, statusline_width = %d, xcb_screen->width_in_pixels = %d\n",
1129 statusline_width, xcb_screen->width_in_pixels);
1130 xcb_free_pixmap(xcb_connection, statusline_pm);
1131 statusline_pm = xcb_generate_id(xcb_connection);
1132 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1133 xcb_screen->root_depth,
1136 MAX(xcb_screen->width_in_pixels, statusline_width),
1137 xcb_screen->height_in_pixels);
1139 uint32_t mask = XCB_GC_FOREGROUND;
1140 uint32_t vals[3] = { colors.bar_bg, colors.bar_bg, xcb_font };
1141 xcb_free_gc(xcb_connection, statusline_clear);
1142 statusline_clear = xcb_generate_id(xcb_connection);
1143 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1149 mask |= XCB_GC_BACKGROUND | XCB_GC_FONT;
1150 vals[0] = colors.bar_fg;
1151 statusline_ctx = xcb_generate_id(xcb_connection);
1152 xcb_free_gc(xcb_connection, statusline_ctx);
1153 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1159 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
1160 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
1161 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
1168 * Reconfigure all bars and create new bars for recently activated outputs
1171 void reconfig_windows() {
1174 static bool tray_configured = false;
1177 SLIST_FOREACH(walk, outputs, slist) {
1178 if (!walk->active) {
1179 /* If an output is not active, we destroy its bar */
1180 /* FIXME: Maybe we rather want to unmap? */
1181 DLOG("Destroying window for output %s\n", walk->name);
1182 destroy_window(walk);
1185 if (walk->bar == XCB_NONE) {
1186 DLOG("Creating Window for output %s\n", walk->name);
1188 walk->bar = xcb_generate_id(xcb_connection);
1189 walk->buffer = xcb_generate_id(xcb_connection);
1190 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
1191 /* Black background */
1192 values[0] = colors.bar_bg;
1193 /* If hide_on_modifier is set, i3 is not supposed to manage our bar-windows */
1194 values[1] = config.hide_on_modifier;
1195 /* We enable the following EventMask fields:
1196 * EXPOSURE, to get expose events (we have to re-draw then)
1197 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1198 * child windows use ConfigureWindow
1199 * BUTTON_PRESS, to handle clicks on the workspace buttons
1201 values[2] = XCB_EVENT_MASK_EXPOSURE |
1202 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
1203 if (!config.disable_ws) {
1204 values[2] |= XCB_EVENT_MASK_BUTTON_PRESS;
1206 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1207 xcb_screen->root_depth,
1210 walk->rect.x, walk->rect.y + walk->rect.h - font_height - 6,
1211 walk->rect.w, font_height + 6,
1213 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1214 xcb_screen->root_visual,
1218 /* The double-buffer we use to render stuff off-screen */
1219 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1220 xcb_screen->root_depth,
1226 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1227 xcb_void_cookie_t class_cookie;
1228 class_cookie = xcb_change_property(xcb_connection,
1229 XCB_PROP_MODE_REPLACE,
1234 (strlen("i3bar") + 1) * 2,
1238 if (asprintf(&name, "i3bar for output %s", walk->name) == -1)
1239 err(EXIT_FAILURE, "asprintf()");
1240 xcb_void_cookie_t name_cookie;
1241 name_cookie = xcb_change_property(xcb_connection,
1242 XCB_PROP_MODE_REPLACE,
1251 /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
1253 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1254 XCB_PROP_MODE_REPLACE,
1256 atoms[_NET_WM_WINDOW_TYPE],
1260 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1262 /* We need to tell i3, where to reserve space for i3bar */
1263 /* left, right, top, bottom, left_start_y, left_end_y,
1264 * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
1266 /* A local struct to save the strut_partial property */
1272 uint32_t left_start_y;
1273 uint32_t left_end_y;
1274 uint32_t right_start_y;
1275 uint32_t right_end_y;
1276 uint32_t top_start_x;
1278 uint32_t bottom_start_x;
1279 uint32_t bottom_end_x;
1280 } __attribute__((__packed__)) strut_partial = {0,};
1281 switch (config.position) {
1285 strut_partial.top = font_height + 6;
1286 strut_partial.top_start_x = walk->rect.x;
1287 strut_partial.top_end_x = walk->rect.x + walk->rect.w;
1290 strut_partial.bottom = font_height + 6;
1291 strut_partial.bottom_start_x = walk->rect.x;
1292 strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
1295 xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
1296 XCB_PROP_MODE_REPLACE,
1298 atoms[_NET_WM_STRUT_PARTIAL],
1304 /* We also want a graphics-context for the bars (it defines the properties
1305 * with which we draw to them) */
1306 walk->bargc = xcb_generate_id(xcb_connection);
1308 values[0] = xcb_font;
1309 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
1315 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1316 xcb_void_cookie_t map_cookie;
1317 if (!config.hide_on_modifier) {
1318 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1321 if (xcb_request_failed(win_cookie, "Could not create window") ||
1322 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1323 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1324 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1325 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1326 xcb_request_failed(strut_cookie, "Could not set strut") ||
1327 xcb_request_failed(gc_cookie, "Could not create graphical context") ||
1328 (!config.hide_on_modifier && xcb_request_failed(map_cookie, "Could not map window"))) {
1332 if (!tray_configured &&
1333 (!config.tray_output ||
1334 strcasecmp("none", config.tray_output) != 0)) {
1336 tray_configured = true;
1339 /* We already have a bar, so we just reconfigure it */
1340 mask = XCB_CONFIG_WINDOW_X |
1341 XCB_CONFIG_WINDOW_Y |
1342 XCB_CONFIG_WINDOW_WIDTH |
1343 XCB_CONFIG_WINDOW_HEIGHT |
1344 XCB_CONFIG_WINDOW_STACK_MODE;
1345 values[0] = walk->rect.x;
1346 values[1] = walk->rect.y + walk->rect.h - font_height - 6;
1347 values[2] = walk->rect.w;
1348 values[3] = font_height + 6;
1349 values[4] = XCB_STACK_MODE_ABOVE;
1351 DLOG("Destroying buffer for output %s", walk->name);
1352 xcb_free_pixmap(xcb_connection, walk->buffer);
1354 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1355 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1360 DLOG("Recreating buffer for output %s", walk->name);
1361 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1362 xcb_screen->root_depth,
1368 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window")) {
1371 if (xcb_request_failed(pm_cookie, "Could not create pixmap")) {
1379 * Render the bars, with buttons and statusline
1383 DLOG("Drawing Bars...\n");
1386 refresh_statusline();
1388 i3_output *outputs_walk;
1389 SLIST_FOREACH(outputs_walk, outputs, slist) {
1390 if (!outputs_walk->active) {
1391 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1394 if (outputs_walk->bar == XCB_NONE) {
1395 /* Oh shit, an active output without an own bar. Create it now! */
1398 /* First things first: clear the backbuffer */
1399 uint32_t color = colors.bar_bg;
1400 xcb_change_gc(xcb_connection,
1401 outputs_walk->bargc,
1404 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
1405 xcb_poly_fill_rectangle(xcb_connection,
1406 outputs_walk->buffer,
1407 outputs_walk->bargc,
1411 if (statusline != NULL) {
1412 DLOG("Printing statusline!\n");
1414 /* Luckily we already prepared a seperate pixmap containing the rendered
1415 * statusline, we just have to copy the relevant parts to the relevant
1417 trayclient *trayclient;
1419 TAILQ_FOREACH(trayclient, outputs_walk->trayclients, tailq) {
1420 if (!trayclient->mapped)
1422 /* We assume the tray icons are quadratic (we use the font
1423 * *height* as *width* of the icons) because we configured them
1425 traypx += font_height + 2;
1427 /* Add 2px of padding if there are any tray icons */
1430 xcb_copy_area(xcb_connection,
1432 outputs_walk->buffer,
1433 outputs_walk->bargc,
1434 MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
1435 MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - traypx - 4)), 3,
1436 MIN(outputs_walk->rect.w - traypx - 4, statusline_width), font_height);
1439 if (config.disable_ws) {
1444 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1445 DLOG("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
1446 uint32_t fg_color = colors.inactive_ws_fg;
1447 uint32_t bg_color = colors.inactive_ws_bg;
1448 if (ws_walk->visible) {
1449 if (!ws_walk->focused) {
1450 fg_color = colors.active_ws_fg;
1451 bg_color = colors.active_ws_bg;
1453 fg_color = colors.focus_ws_fg;
1454 bg_color = colors.focus_ws_bg;
1457 if (ws_walk->urgent) {
1458 DLOG("WS %s is urgent!\n", ws_walk->name);
1459 fg_color = colors.urgent_ws_fg;
1460 bg_color = colors.urgent_ws_bg;
1461 /* The urgent-hint should get noticed, so we unhide the bars shortly */
1464 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1465 uint32_t vals[] = { bg_color, bg_color };
1466 xcb_change_gc(xcb_connection,
1467 outputs_walk->bargc,
1470 xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
1471 xcb_poly_fill_rectangle(xcb_connection,
1472 outputs_walk->buffer,
1473 outputs_walk->bargc,
1476 xcb_change_gc(xcb_connection,
1477 outputs_walk->bargc,
1480 xcb_image_text_16(xcb_connection,
1481 ws_walk->name_glyphs,
1482 outputs_walk->buffer,
1483 outputs_walk->bargc,
1484 i + 5, font_info->font_ascent + 2,
1485 ws_walk->ucs2_name);
1486 i += 10 + ws_walk->name_width;
1496 * Redraw the bars, i.e. simply copy the buffer to the barwindow
1499 void redraw_bars() {
1500 i3_output *outputs_walk;
1501 SLIST_FOREACH(outputs_walk, outputs, slist) {
1502 if (!outputs_walk->active) {
1505 xcb_copy_area(xcb_connection,
1506 outputs_walk->buffer,
1508 outputs_walk->bargc,
1511 outputs_walk->rect.w,
1512 outputs_walk->rect.h);
1513 xcb_flush(xcb_connection);