2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
7 * xcb.c: Communicating with X
11 #include <xcb/xproto.h>
12 #include <xcb/xcb_aux.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 *root_screen;
50 xcb_window_t xcb_root;
52 /* selection window for tray support */
53 static xcb_window_t selwin = XCB_NONE;
54 static xcb_intern_atom_reply_t *tray_reply = NULL;
56 /* This is needed for integration with libi3 */
57 xcb_connection_t *conn;
59 /* The font we'll use */
62 /* Overall height of the bar (based on font size) */
65 /* These are only relevant for XKB, which we only need for grabbing modifiers */
70 /* Because the statusline is the same on all outputs, we have
71 * global buffer to render it on */
72 xcb_gcontext_t statusline_ctx;
73 xcb_gcontext_t statusline_clear;
74 xcb_pixmap_t statusline_pm;
75 uint32_t statusline_width;
77 /* Event-Watchers, to interact with the user */
83 /* The name of current binding mode */
86 /* Indicates whether a new binding mode was recently activated */
87 bool activated_mode = false;
89 /* The parsed colors */
94 uint32_t active_ws_fg;
95 uint32_t active_ws_bg;
96 uint32_t active_ws_border;
97 uint32_t inactive_ws_fg;
98 uint32_t inactive_ws_bg;
99 uint32_t inactive_ws_border;
100 uint32_t urgent_ws_bg;
101 uint32_t urgent_ws_fg;
102 uint32_t urgent_ws_border;
103 uint32_t focus_ws_bg;
104 uint32_t focus_ws_fg;
105 uint32_t focus_ws_border;
107 struct xcb_colors_t colors;
109 /* We define xcb_request_failed as a macro to include the relevant line-number */
110 #define xcb_request_failed(cookie, err_msg) _xcb_request_failed(cookie, err_msg, __LINE__)
111 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
112 xcb_generic_error_t *err;
113 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
114 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
115 return err->error_code;
121 * Redraws the statusline to the buffer
124 void refresh_statusline(void) {
125 struct status_block *block;
127 uint32_t old_statusline_width = statusline_width;
128 statusline_width = 0;
130 /* Predict the text width of all blocks (in pixels). */
131 TAILQ_FOREACH(block, &statusline_head, blocks) {
132 if (i3string_get_num_bytes(block->full_text) == 0)
135 block->width = predict_text_width(block->full_text);
137 /* Compute offset and append for text aligment in min_width. */
138 if (block->min_width <= block->width) {
142 uint32_t padding_width = block->min_width - block->width;
143 switch (block->align) {
145 block->x_append = padding_width;
148 block->x_offset = padding_width;
151 block->x_offset = padding_width / 2;
152 block->x_append = padding_width / 2 + padding_width % 2;
157 /* If this is not the last block, add some pixels for a separator. */
158 if (TAILQ_NEXT(block, blocks) != NULL)
159 block->width += block->sep_block_width;
161 statusline_width += block->width + block->x_offset + block->x_append;
164 /* If the statusline is bigger than our screen we need to make sure that
165 * the pixmap provides enough space, so re-allocate if the width grew */
166 if (statusline_width > root_screen->width_in_pixels &&
167 statusline_width > old_statusline_width)
170 /* Clear the statusline pixmap. */
171 xcb_rectangle_t rect = { 0, 0, root_screen->width_in_pixels, font.height + 2 };
172 xcb_poly_fill_rectangle(xcb_connection, statusline_pm, statusline_clear, 1, &rect);
174 /* Draw the text of each block. */
176 TAILQ_FOREACH(block, &statusline_head, blocks) {
177 if (i3string_get_num_bytes(block->full_text) == 0)
180 uint32_t colorpixel = (block->color ? get_colorpixel(block->color) : colors.bar_fg);
181 set_font_colors(statusline_ctx, colorpixel, colors.bar_bg);
182 draw_text(block->full_text, statusline_pm, statusline_ctx, x + block->x_offset, 1, block->width);
183 x += block->width + block->x_offset + block->x_append;
185 if (TAILQ_NEXT(block, blocks) != NULL && !block->no_separator && block->sep_block_width > 0) {
186 /* This is not the last block, draw a separator. */
187 uint32_t sep_offset = block->sep_block_width/2 + block->sep_block_width % 2;
188 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
189 uint32_t values[] = { colors.sep_fg, colors.bar_bg };
190 xcb_change_gc(xcb_connection, statusline_ctx, mask, values);
191 xcb_poly_line(xcb_connection, XCB_COORD_MODE_ORIGIN, statusline_pm,
193 (xcb_point_t[]){ { x - sep_offset, 2 },
194 { x - sep_offset, font.height - 2 } });
200 * Hides all bars (unmaps them)
203 void hide_bars(void) {
204 if ((config.hide_on_modifier == M_DOCK) || (config.hidden_state == S_SHOW && config.hide_on_modifier == M_HIDE)) {
209 SLIST_FOREACH(walk, outputs, slist) {
213 xcb_unmap_window(xcb_connection, walk->bar);
219 * Unhides all bars (maps them)
222 void unhide_bars(void) {
223 if (config.hide_on_modifier != M_HIDE) {
228 xcb_void_cookie_t cookie;
234 SLIST_FOREACH(walk, outputs, slist) {
235 if (walk->bar == XCB_NONE) {
238 mask = XCB_CONFIG_WINDOW_X |
239 XCB_CONFIG_WINDOW_Y |
240 XCB_CONFIG_WINDOW_WIDTH |
241 XCB_CONFIG_WINDOW_HEIGHT |
242 XCB_CONFIG_WINDOW_STACK_MODE;
243 values[0] = walk->rect.x;
244 if (config.position == POS_TOP)
245 values[1] = walk->rect.y;
246 else values[1] = walk->rect.y + walk->rect.h - bar_height;
247 values[2] = walk->rect.w;
248 values[3] = bar_height;
249 values[4] = XCB_STACK_MODE_ABOVE;
250 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
251 cookie = xcb_configure_window_checked(xcb_connection,
256 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
259 xcb_map_window(xcb_connection, walk->bar);
264 * Parse the colors into a format that we can use
267 void init_colors(const struct xcb_color_strings_t *new_colors) {
268 #define PARSE_COLOR(name, def) \
270 colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
272 PARSE_COLOR(bar_fg, "#FFFFFF");
273 PARSE_COLOR(bar_bg, "#000000");
274 PARSE_COLOR(sep_fg, "#666666");
275 PARSE_COLOR(active_ws_fg, "#FFFFFF");
276 PARSE_COLOR(active_ws_bg, "#333333");
277 PARSE_COLOR(active_ws_border, "#333333");
278 PARSE_COLOR(inactive_ws_fg, "#888888");
279 PARSE_COLOR(inactive_ws_bg, "#222222");
280 PARSE_COLOR(inactive_ws_border, "#333333");
281 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
282 PARSE_COLOR(urgent_ws_bg, "#900000");
283 PARSE_COLOR(urgent_ws_border, "#2f343a");
284 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
285 PARSE_COLOR(focus_ws_bg, "#285577");
286 PARSE_COLOR(focus_ws_border, "#4c7899");
290 xcb_flush(xcb_connection);
294 * Handle a button-press-event (i.e. a mouse click on one of our bars).
295 * We determine, whether the click occured on a ws-button or if the scroll-
296 * wheel was used and change the workspace appropriately
299 void handle_button(xcb_button_press_event_t *event) {
302 /* Determine, which bar was clicked */
304 xcb_window_t bar = event->event;
305 SLIST_FOREACH(walk, outputs, slist) {
306 if (walk->bar == bar) {
312 DLOG("Unknown Bar klicked!\n");
316 /* TODO: Move this to extern get_ws_for_output() */
317 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
318 if (cur_ws->visible) {
323 if (cur_ws == NULL) {
324 DLOG("No Workspace active?\n");
328 int32_t x = event->event_x >= 0 ? event->event_x : 0;
329 int32_t original_x = x;
331 DLOG("Got Button %d\n", event->detail);
333 switch (event->detail) {
335 /* Mouse wheel up. We select the previous ws, if any.
336 * If there is no more workspace, don’t even send the workspace
337 * command, otherwise (with workspace auto_back_and_forth) we’d end
338 * up on the wrong workspace. */
339 if (cur_ws == TAILQ_FIRST(walk->workspaces))
342 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
345 /* Mouse wheel down. We select the next ws, if any.
346 * If there is no more workspace, don’t even send the workspace
347 * command, otherwise (with workspace auto_back_and_forth) we’d end
348 * up on the wrong workspace. */
349 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
352 cur_ws = TAILQ_NEXT(cur_ws, tailq);
355 /* Check if this event regards a workspace button */
356 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
358 if (x >= 0 && x < cur_ws->name_width + 10) {
361 x -= cur_ws->name_width + 11;
363 if (cur_ws == NULL) {
364 /* No workspace button was pressed.
365 * Check if a status block has been clicked.
366 * This of course only has an effect,
367 * if the child reported bidirectional protocol usage. */
369 /* First calculate width of tray area */
370 trayclient *trayclient;
372 TAILQ_FOREACH_REVERSE(trayclient, walk->trayclients, tc_head, tailq) {
373 if (!trayclient->mapped)
375 tray_width += (font.height + 2);
378 int block_x = 0, last_block_x;
379 int offset = (walk->rect.w - (statusline_width + tray_width)) - 10;
381 x = original_x - offset;
385 struct status_block *block;
387 TAILQ_FOREACH(block, &statusline_head, blocks) {
388 last_block_x = block_x;
389 block_x += block->width + block->x_offset + block->x_append;
391 if (x <= block_x && x >= last_block_x) {
392 send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y);
398 if (event->detail != 1)
402 /* To properly handle workspace names with double quotes in them, we need
403 * to escape the double quotes. Unfortunately, that’s rather ugly in C: We
404 * first count the number of double quotes, then we allocate a large enough
405 * buffer, then we copy character by character. */
408 const char *utf8_name = i3string_as_utf8(cur_ws->name);
409 for (const char *walk = utf8_name; *walk != '\0'; walk++) {
412 /* While we’re looping through the name anyway, we can save one
417 const size_t len = namelen + strlen("workspace \"\"") + 1;
418 char *buffer = scalloc(len+num_quotes);
419 strncpy(buffer, "workspace \"", strlen("workspace \""));
421 for (inpos = 0, outpos = strlen("workspace \"");
424 if (utf8_name[inpos] == '"') {
425 buffer[outpos] = '\\';
428 buffer[outpos] = utf8_name[inpos];
430 buffer[outpos] = '"';
431 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
436 * Configures the x coordinate of all trayclients. To be called after adding a
437 * new tray client or removing an old one.
440 static void configure_trayclients(void) {
441 trayclient *trayclient;
443 SLIST_FOREACH(output, outputs, slist) {
448 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
449 if (!trayclient->mapped)
453 DLOG("Configuring tray window %08x to x=%d\n",
454 trayclient->win, output->rect.w - (clients * (font.height + 2)));
455 uint32_t x = output->rect.w - (clients * (font.height + 2));
456 xcb_configure_window(xcb_connection,
465 * Handles ClientMessages (messages sent from another client directly to us).
467 * At the moment, only the tray window will receive client messages. All
468 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
471 static void handle_client_message(xcb_client_message_event_t* event) {
472 if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
473 event->format == 32) {
474 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
475 /* event->data.data32[0] is the timestamp */
476 uint32_t op = event->data.data32[1];
479 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
480 xcb_window_t client = event->data.data32[2];
482 /* Listen for PropertyNotify events to get the most recent value of
483 * the XEMBED_MAPPED atom, also listen for UnmapNotify events */
484 mask = XCB_CW_EVENT_MASK;
485 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
486 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
487 xcb_change_window_attributes(xcb_connection,
492 /* Request the _XEMBED_INFO property. The XEMBED specification
493 * (which is referred by the tray specification) says this *has* to
494 * be set, but VLC does not set it… */
497 xcb_get_property_cookie_t xembedc;
498 xcb_generic_error_t *error;
499 xembedc = xcb_get_property(xcb_connection,
503 XCB_GET_PROPERTY_TYPE_ANY,
507 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
511 ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
516 if (xembedr != NULL && xembedr->length != 0) {
517 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
518 uint32_t *xembed = xcb_get_property_value(xembedr);
519 DLOG("xembed version = %d\n", xembed[0]);
520 DLOG("xembed flags = %d\n", xembed[1]);
521 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
522 xe_version = xembed[0];
527 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
530 DLOG("X window %08x requested docking\n", client);
531 i3_output *walk, *output = NULL;
532 SLIST_FOREACH(walk, outputs, slist) {
535 if (config.tray_output) {
536 if ((strcasecmp(walk->name, config.tray_output) != 0) &&
537 (!walk->primary || strcasecmp("primary", config.tray_output) != 0))
541 DLOG("using output %s\n", walk->name);
545 /* In case of tray_output == primary and there is no primary output
546 * configured, we fall back to the first available output. */
547 if (output == NULL &&
548 config.tray_output &&
549 strcasecmp("primary", config.tray_output) == 0) {
550 SLIST_FOREACH(walk, outputs, slist) {
553 DLOG("Falling back to output %s because no primary output is configured\n", walk->name);
558 if (output == NULL) {
559 ELOG("No output found\n");
562 xcb_reparent_window(xcb_connection,
565 output->rect.w - font.height - 2,
567 /* We reconfigure the window to use a reasonable size. The systray
568 * specification explicitly says:
569 * Tray icons may be assigned any size by the system tray, and
570 * should do their best to cope with any size effectively
572 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
573 values[0] = font.height;
574 values[1] = font.height;
575 xcb_configure_window(xcb_connection,
580 /* send the XEMBED_EMBEDDED_NOTIFY message */
581 void *event = scalloc(32);
582 xcb_client_message_event_t *ev = event;
583 ev->response_type = XCB_CLIENT_MESSAGE;
585 ev->type = atoms[_XEMBED];
587 ev->data.data32[0] = XCB_CURRENT_TIME;
588 ev->data.data32[1] = atoms[XEMBED_EMBEDDED_NOTIFY];
589 ev->data.data32[2] = output->bar;
590 ev->data.data32[3] = xe_version;
591 xcb_send_event(xcb_connection,
594 XCB_EVENT_MASK_NO_EVENT,
598 /* Put the client inside the save set. Upon termination (whether
599 * killed or normal exit does not matter) of i3bar, these clients
600 * will be correctly reparented to their most closest living
601 * ancestor. Without this, tray icons might die when i3bar
603 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
606 DLOG("Mapping dock client\n");
607 xcb_map_window(xcb_connection, client);
609 DLOG("Not mapping dock client yet\n");
611 trayclient *tc = smalloc(sizeof(trayclient));
614 tc->xe_version = xe_version;
615 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
617 /* Trigger an update to copy the statusline text to the appropriate
619 configure_trayclients();
626 * Handles UnmapNotify events. These events happen when a tray window unmaps
627 * itself. We then update our data structure
630 static void handle_unmap_notify(xcb_unmap_notify_event_t* event) {
631 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
634 SLIST_FOREACH(walk, outputs, slist) {
637 DLOG("checking output %s\n", walk->name);
638 trayclient *trayclient;
639 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
640 if (trayclient->win != event->window)
643 DLOG("Removing tray client with window ID %08x\n", event->window);
644 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
646 /* Trigger an update, we now have more space for the statusline */
647 configure_trayclients();
655 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
656 * handled, which tells us whether a dock client should be mapped or unmapped.
659 static void handle_property_notify(xcb_property_notify_event_t *event) {
660 DLOG("PropertyNotify\n");
661 if (event->atom == atoms[_XEMBED_INFO] &&
662 event->state == XCB_PROPERTY_NEW_VALUE) {
663 DLOG("xembed_info updated\n");
664 trayclient *trayclient = NULL, *walk;
666 SLIST_FOREACH(o_walk, outputs, slist) {
670 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
671 if (walk->win != event->window)
681 ELOG("PropertyNotify received for unknown window %08x\n",
685 xcb_get_property_cookie_t xembedc;
686 xembedc = xcb_get_property_unchecked(xcb_connection,
690 XCB_GET_PROPERTY_TYPE_ANY,
694 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
697 if (xembedr == NULL || xembedr->length == 0) {
698 DLOG("xembed_info unset\n");
702 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
703 uint32_t *xembed = xcb_get_property_value(xembedr);
704 DLOG("xembed version = %d\n", xembed[0]);
705 DLOG("xembed flags = %d\n", xembed[1]);
706 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
707 DLOG("map-state now %d\n", map_it);
708 if (trayclient->mapped && !map_it) {
709 /* need to unmap the window */
710 xcb_unmap_window(xcb_connection, trayclient->win);
711 trayclient->mapped = map_it;
712 configure_trayclients();
714 } else if (!trayclient->mapped && map_it) {
715 /* need to map the window */
716 xcb_map_window(xcb_connection, trayclient->win);
717 trayclient->mapped = map_it;
718 configure_trayclients();
726 * Handle ConfigureRequests by denying them and sending the client a
727 * ConfigureNotify with its actual size.
730 static void handle_configure_request(xcb_configure_request_event_t *event) {
731 DLOG("ConfigureRequest for window = %08x\n", event->window);
733 trayclient *trayclient;
735 SLIST_FOREACH(output, outputs, slist) {
740 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
741 if (!trayclient->mapped)
745 if (trayclient->win != event->window)
748 xcb_rectangle_t rect;
749 rect.x = output->rect.w - (clients * (font.height + 2));
751 rect.width = font.height;
752 rect.height = font.height;
754 DLOG("This is a tray window. x = %d\n", rect.x);
755 fake_configure_notify(xcb_connection, rect, event->window, 0);
760 DLOG("WARNING: Could not find corresponding tray window.\n");
764 * This function is called immediately before the main loop locks. We flush xcb
765 * then (and only then)
768 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
769 xcb_flush(xcb_connection);
773 * This function is called immediately after the main loop locks, so when one
774 * of the watchers registered an event.
775 * We check whether an X-Event arrived and handle it.
778 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
779 xcb_generic_event_t *event;
781 if (xcb_connection_has_error(xcb_connection)) {
782 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
786 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
787 switch (event->response_type & ~0x80) {
789 /* Expose-events happen, when the window needs to be redrawn */
792 case XCB_BUTTON_PRESS:
793 /* Button-press-events are mouse-buttons clicked on one of our bars */
794 handle_button((xcb_button_press_event_t*) event);
796 case XCB_CLIENT_MESSAGE:
797 /* Client messages are used for client-to-client communication, for
798 * example system tray widgets talk to us directly via client messages. */
799 handle_client_message((xcb_client_message_event_t*) event);
801 case XCB_UNMAP_NOTIFY:
802 case XCB_DESTROY_NOTIFY:
803 /* UnmapNotifies are received when a tray window unmaps itself */
804 handle_unmap_notify((xcb_unmap_notify_event_t*) event);
806 case XCB_PROPERTY_NOTIFY:
808 handle_property_notify((xcb_property_notify_event_t*) event);
810 case XCB_CONFIGURE_REQUEST:
811 /* ConfigureRequest, sent by a tray child */
812 handle_configure_request((xcb_configure_request_event_t*) event);
820 * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
824 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
828 * We need to bind to the modifier per XKB. Sadly, XCB does not implement this
831 void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
835 DLOG("Got XKB-Event!\n");
837 while (XPending(xkb_dpy)) {
838 XNextEvent(xkb_dpy, (XEvent*)&ev);
840 if (ev.type != xkb_event_base) {
841 ELOG("No Xkb-Event!\n");
845 if (ev.any.xkb_type != XkbStateNotify) {
846 ELOG("No State Notify!\n");
850 unsigned int mods = ev.state.mods;
851 modstate = mods & config.modifier;
854 #define DLOGMOD(modmask, status) \
858 DLOG("ShiftMask got " #status "!\n"); \
861 DLOG("ControlMask got " #status "!\n"); \
864 DLOG("Mod1Mask got " #status "!\n"); \
867 DLOG("Mod2Mask got " #status "!\n"); \
870 DLOG("Mod3Mask got " #status "!\n"); \
873 DLOG("Mod4Mask got " #status "!\n"); \
876 DLOG("Mod5Mask got " #status "!\n"); \
881 if (modstate != mod_pressed) {
883 DLOGMOD(config.modifier, released);
887 DLOGMOD(config.modifier, pressed);
888 activated_mode = false;
891 mod_pressed = modstate;
898 * Early initialization of the connection to X11: Everything which does not
899 * depend on 'config'.
902 char *init_xcb_early() {
903 /* FIXME: xcb_connect leaks Memory */
904 xcb_connection = xcb_connect(NULL, &screen);
905 if (xcb_connection_has_error(xcb_connection)) {
906 ELOG("Cannot open display\n");
909 conn = xcb_connection;
910 DLOG("Connected to xcb\n");
912 /* We have to request the atoms we need */
913 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
914 #include "xcb_atoms.def"
916 root_screen = xcb_aux_get_screen(xcb_connection, screen);
917 xcb_root = root_screen->root;
919 /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
920 * this way, we can choose to crop it */
921 uint32_t mask = XCB_GC_FOREGROUND;
922 uint32_t vals[] = { colors.bar_bg, colors.bar_bg };
924 statusline_clear = xcb_generate_id(xcb_connection);
925 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
931 statusline_ctx = xcb_generate_id(xcb_connection);
932 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
938 statusline_pm = xcb_generate_id(xcb_connection);
939 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
940 root_screen->root_depth,
943 root_screen->width_in_pixels,
944 root_screen->height_in_pixels);
947 /* The various Watchers to communicate with xcb */
948 xcb_io = smalloc(sizeof(ev_io));
949 xcb_prep = smalloc(sizeof(ev_prepare));
950 xcb_chk = smalloc(sizeof(ev_check));
952 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
953 ev_prepare_init(xcb_prep, &xcb_prep_cb);
954 ev_check_init(xcb_chk, &xcb_chk_cb);
956 ev_io_start(main_loop, xcb_io);
957 ev_prepare_start(main_loop, xcb_prep);
958 ev_check_start(main_loop, xcb_chk);
960 /* Now we get the atoms and save them in a nice data structure */
963 char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen);
965 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
966 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
967 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
975 * Register for xkb keyevents. To grab modifiers without blocking other applications from receiving key-events
976 * involving that modifier, we sadly have to use xkb which is not yet fully supported
980 void register_xkb_keyevents() {
981 if (xkb_dpy == NULL) {
982 int xkb_major, xkb_minor, xkb_errbase, xkb_err;
983 xkb_major = XkbMajorVersion;
984 xkb_minor = XkbMinorVersion;
986 xkb_dpy = XkbOpenDisplay(NULL,
993 if (xkb_dpy == NULL) {
998 if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
999 ELOG("Could not set FD_CLOEXEC on xkbdpy: %s\n", strerror(errno));
1004 if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
1005 ELOG("XKB not supported by X-server!\n");
1009 if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
1010 ELOG("Could not grab Key!\n");
1014 xkb_io = smalloc(sizeof(ev_io));
1015 ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
1016 ev_io_start(main_loop, xkb_io);
1022 * Deregister from xkb keyevents.
1025 void deregister_xkb_keyevents() {
1026 if (xkb_dpy != NULL) {
1027 ev_io_stop (main_loop, xkb_io);
1028 XCloseDisplay(xkb_dpy);
1036 * Initialization which depends on 'config' being usable. Called after the
1037 * configuration has arrived.
1040 void init_xcb_late(char *fontname) {
1041 if (fontname == NULL)
1042 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
1045 font = load_font(fontname, true);
1047 DLOG("Calculated Font-height: %d\n", font.height);
1048 bar_height = font.height + 6;
1050 xcb_flush(xcb_connection);
1052 if (config.hide_on_modifier == M_HIDE)
1053 register_xkb_keyevents();
1057 * Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the
1061 static void send_tray_clientmessage(void) {
1062 uint8_t buffer[32] = { 0 };
1063 xcb_client_message_event_t *ev = (xcb_client_message_event_t*)buffer;
1065 ev->response_type = XCB_CLIENT_MESSAGE;
1066 ev->window = xcb_root;
1067 ev->type = atoms[MANAGER];
1069 ev->data.data32[0] = XCB_CURRENT_TIME;
1070 ev->data.data32[1] = tray_reply->atom;
1071 ev->data.data32[2] = selwin;
1073 xcb_send_event(xcb_connection,
1082 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
1083 * for the X11 display we are running on, then acquiring the selection for this
1084 * atom. Afterwards, tray clients will send ClientMessages to our window.
1087 void init_tray(void) {
1088 DLOG("Initializing system tray functionality\n");
1089 /* request the tray manager atom for the X11 display we are running on */
1090 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
1091 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
1092 xcb_intern_atom_cookie_t tray_cookie;
1093 if (tray_reply == NULL)
1094 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
1096 /* tray support: we need a window to own the selection */
1097 selwin = xcb_generate_id(xcb_connection);
1098 uint32_t selmask = XCB_CW_OVERRIDE_REDIRECT;
1099 uint32_t selval[] = { 1 };
1100 xcb_create_window(xcb_connection,
1101 root_screen->root_depth,
1107 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1108 root_screen->root_visual,
1112 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
1114 xcb_change_property(xcb_connection,
1115 XCB_PROP_MODE_REPLACE,
1117 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1125 if (tray_reply == NULL) {
1126 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1127 ELOG("Could not get atom %s\n", atomname);
1132 xcb_set_selection_owner(xcb_connection,
1137 /* Verify that we have the selection */
1138 xcb_get_selection_owner_cookie_t selcookie;
1139 xcb_get_selection_owner_reply_t *selreply;
1141 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1142 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1143 ELOG("Could not get selection owner for %s\n", atomname);
1147 if (selreply->owner != selwin) {
1148 ELOG("Could not set the %s selection. " \
1149 "Maybe another tray is already running?\n", atomname);
1150 /* NOTE that this error is not fatal. We just can’t provide tray
1156 send_tray_clientmessage();
1160 * We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window
1161 * to make GTK+ 3 applets with Symbolic Icons visible. If the colors are unset,
1162 * they assume a light background.
1163 * See also https://bugzilla.gnome.org/show_bug.cgi?id=679591
1166 void init_tray_colors(void) {
1167 /* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */
1168 const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF");
1170 DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg);
1172 char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'},
1173 {bar_fg[3], bar_fg[4], '\0'},
1174 {bar_fg[5], bar_fg[6], '\0'}};
1175 const uint8_t r = strtol(strgroups[0], NULL, 16);
1176 const uint8_t g = strtol(strgroups[1], NULL, 16);
1177 const uint8_t b = strtol(strgroups[2], NULL, 16);
1179 const uint16_t r16 = ((uint16_t)r << 8) | r;
1180 const uint16_t g16 = ((uint16_t)g << 8) | g;
1181 const uint16_t b16 = ((uint16_t)b << 8) | b;
1183 const uint32_t tray_colors[12] = {
1184 r16, g16, b16, /* foreground color */
1185 r16, g16, b16, /* error color */
1186 r16, g16, b16, /* warning color */
1187 r16, g16, b16, /* success color */
1190 xcb_change_property(xcb_connection,
1191 XCB_PROP_MODE_REPLACE,
1193 atoms[_NET_SYSTEM_TRAY_COLORS],
1201 * Cleanup the xcb-stuff.
1202 * Called once, before the program terminates.
1205 void clean_xcb(void) {
1208 SLIST_FOREACH(o_walk, outputs, slist) {
1209 destroy_window(o_walk);
1210 FREE(o_walk->trayclients);
1211 FREE(o_walk->workspaces);
1214 FREE_SLIST(outputs, i3_output);
1217 xcb_flush(xcb_connection);
1218 xcb_disconnect(xcb_connection);
1220 ev_check_stop(main_loop, xcb_chk);
1221 ev_prepare_stop(main_loop, xcb_prep);
1222 ev_io_stop(main_loop, xcb_io);
1230 * Get the earlier requested atoms and save them in the prepared data structure
1233 void get_atoms(void) {
1234 xcb_intern_atom_reply_t *reply;
1235 #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1236 if (reply == NULL) { \
1237 ELOG("Could not get atom %s\n", #name); \
1238 exit(EXIT_FAILURE); \
1240 atoms[name] = reply->atom; \
1243 #include "xcb_atoms.def"
1244 DLOG("Got Atoms\n");
1248 * Reparents all tray clients of the specified output to the root window. This
1249 * is either used when shutting down, when an output appears (xrandr --output
1250 * VGA1 --off) or when the primary output changes.
1252 * Applications using the tray will start the protocol from the beginning again
1256 void kick_tray_clients(i3_output *output) {
1257 if (TAILQ_EMPTY(output->trayclients))
1260 trayclient *trayclient;
1261 while (!TAILQ_EMPTY(output->trayclients)) {
1262 trayclient = TAILQ_FIRST(output->trayclients);
1263 /* Unmap, then reparent (to root) the tray client windows */
1264 xcb_unmap_window(xcb_connection, trayclient->win);
1265 xcb_reparent_window(xcb_connection,
1271 /* We remove the trayclient right here. We might receive an UnmapNotify
1272 * event afterwards, but better safe than sorry. */
1273 TAILQ_REMOVE(output->trayclients, trayclient, tailq);
1276 /* Fake a DestroyNotify so that Qt re-adds tray icons.
1277 * We cannot actually destroy the window because then Qt will not restore
1278 * its event mask on the new window. */
1279 uint8_t buffer[32] = { 0 };
1280 xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t*)buffer;
1282 event->response_type = XCB_DESTROY_NOTIFY;
1283 event->event = selwin;
1284 event->window = selwin;
1286 xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)event);
1288 send_tray_clientmessage();
1292 * Destroy the bar of the specified output
1295 void destroy_window(i3_output *output) {
1296 if (output == NULL) {
1299 if (output->bar == XCB_NONE) {
1303 kick_tray_clients(output);
1304 xcb_destroy_window(xcb_connection, output->bar);
1305 output->bar = XCB_NONE;
1309 * Reallocate the statusline-buffer
1312 void realloc_sl_buffer(void) {
1313 DLOG("Re-allocating statusline-buffer, statusline_width = %d, root_screen->width_in_pixels = %d\n",
1314 statusline_width, root_screen->width_in_pixels);
1315 xcb_free_pixmap(xcb_connection, statusline_pm);
1316 statusline_pm = xcb_generate_id(xcb_connection);
1317 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1318 root_screen->root_depth,
1321 MAX(root_screen->width_in_pixels, statusline_width),
1324 uint32_t mask = XCB_GC_FOREGROUND;
1325 uint32_t vals[2] = { colors.bar_bg, colors.bar_bg };
1326 xcb_free_gc(xcb_connection, statusline_clear);
1327 statusline_clear = xcb_generate_id(xcb_connection);
1328 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1334 mask |= XCB_GC_BACKGROUND;
1335 vals[0] = colors.bar_fg;
1336 statusline_ctx = xcb_generate_id(xcb_connection);
1337 xcb_free_gc(xcb_connection, statusline_ctx);
1338 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1344 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
1345 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
1346 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
1353 * Reconfigure all bars and create new bars for recently activated outputs
1356 void reconfig_windows(bool redraw_bars) {
1359 static bool tray_configured = false;
1362 SLIST_FOREACH(walk, outputs, slist) {
1363 if (!walk->active) {
1364 /* If an output is not active, we destroy its bar */
1365 /* FIXME: Maybe we rather want to unmap? */
1366 DLOG("Destroying window for output %s\n", walk->name);
1367 destroy_window(walk);
1370 if (walk->bar == XCB_NONE) {
1371 DLOG("Creating Window for output %s\n", walk->name);
1373 walk->bar = xcb_generate_id(xcb_connection);
1374 walk->buffer = xcb_generate_id(xcb_connection);
1375 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
1376 /* Black background */
1377 values[0] = colors.bar_bg;
1378 /* If hide_on_modifier is set to hide or invisible mode, i3 is not supposed to manage our bar-windows */
1379 values[1] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1380 /* We enable the following EventMask fields:
1381 * EXPOSURE, to get expose events (we have to re-draw then)
1382 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1383 * child windows use ConfigureWindow
1384 * BUTTON_PRESS, to handle clicks on the workspace buttons
1386 values[2] = XCB_EVENT_MASK_EXPOSURE |
1387 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
1388 if (!config.disable_ws) {
1389 values[2] |= XCB_EVENT_MASK_BUTTON_PRESS;
1391 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1392 root_screen->root_depth,
1395 walk->rect.x, walk->rect.y + walk->rect.h - bar_height,
1396 walk->rect.w, bar_height,
1398 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1399 root_screen->root_visual,
1403 /* The double-buffer we use to render stuff off-screen */
1404 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1405 root_screen->root_depth,
1411 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1412 xcb_void_cookie_t class_cookie;
1413 class_cookie = xcb_change_property(xcb_connection,
1414 XCB_PROP_MODE_REPLACE,
1419 (strlen("i3bar") + 1) * 2,
1423 if (asprintf(&name, "i3bar for output %s", walk->name) == -1)
1424 err(EXIT_FAILURE, "asprintf()");
1425 xcb_void_cookie_t name_cookie;
1426 name_cookie = xcb_change_property(xcb_connection,
1427 XCB_PROP_MODE_REPLACE,
1436 /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
1438 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1439 XCB_PROP_MODE_REPLACE,
1441 atoms[_NET_WM_WINDOW_TYPE],
1445 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1447 /* We need to tell i3, where to reserve space for i3bar */
1448 /* left, right, top, bottom, left_start_y, left_end_y,
1449 * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
1451 /* A local struct to save the strut_partial property */
1457 uint32_t left_start_y;
1458 uint32_t left_end_y;
1459 uint32_t right_start_y;
1460 uint32_t right_end_y;
1461 uint32_t top_start_x;
1463 uint32_t bottom_start_x;
1464 uint32_t bottom_end_x;
1465 } __attribute__((__packed__)) strut_partial = {0,};
1466 switch (config.position) {
1470 strut_partial.top = bar_height;
1471 strut_partial.top_start_x = walk->rect.x;
1472 strut_partial.top_end_x = walk->rect.x + walk->rect.w;
1475 strut_partial.bottom = bar_height;
1476 strut_partial.bottom_start_x = walk->rect.x;
1477 strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
1480 xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
1481 XCB_PROP_MODE_REPLACE,
1483 atoms[_NET_WM_STRUT_PARTIAL],
1489 /* We also want a graphics-context for the bars (it defines the properties
1490 * with which we draw to them) */
1491 walk->bargc = xcb_generate_id(xcb_connection);
1492 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
1498 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1499 xcb_void_cookie_t map_cookie;
1500 if (config.hide_on_modifier == M_DOCK) {
1501 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1504 if (xcb_request_failed(win_cookie, "Could not create window") ||
1505 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1506 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1507 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1508 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1509 xcb_request_failed(strut_cookie, "Could not set strut") ||
1510 xcb_request_failed(gc_cookie, "Could not create graphical context") ||
1511 ((config.hide_on_modifier == M_DOCK) && xcb_request_failed(map_cookie, "Could not map window"))) {
1515 const char *tray_output = (config.tray_output ? config.tray_output : SLIST_FIRST(outputs)->name);
1516 if (!tray_configured && strcasecmp(tray_output, "none") != 0) {
1517 /* Configuration sanity check: ensure this i3bar instance handles the output on
1518 * which the tray should appear (e.g. don’t initialize a tray if tray_output ==
1519 * VGA-1 but output == [HDMI-1]).
1522 SLIST_FOREACH(output, outputs, slist) {
1523 if (strcasecmp(output->name, tray_output) == 0 ||
1524 (strcasecmp(tray_output, "primary") == 0 && output->primary)) {
1529 tray_configured = true;
1532 /* We already have a bar, so we just reconfigure it */
1533 mask = XCB_CONFIG_WINDOW_X |
1534 XCB_CONFIG_WINDOW_Y |
1535 XCB_CONFIG_WINDOW_WIDTH |
1536 XCB_CONFIG_WINDOW_HEIGHT |
1537 XCB_CONFIG_WINDOW_STACK_MODE;
1538 values[0] = walk->rect.x;
1539 values[1] = walk->rect.y + walk->rect.h - bar_height;
1540 values[2] = walk->rect.w;
1541 values[3] = bar_height;
1542 values[4] = XCB_STACK_MODE_ABOVE;
1544 DLOG("Destroying buffer for output %s\n", walk->name);
1545 xcb_free_pixmap(xcb_connection, walk->buffer);
1547 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1548 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1553 mask = XCB_CW_OVERRIDE_REDIRECT;
1554 values[0] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1555 DLOG("Changing Window attribute override_redirect for output %s to %d\n", walk->name, values[0]);
1556 xcb_void_cookie_t chg_cookie = xcb_change_window_attributes(xcb_connection,
1561 DLOG("Recreating buffer for output %s\n", walk->name);
1562 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1563 root_screen->root_depth,
1569 xcb_void_cookie_t map_cookie, umap_cookie;
1571 /* Unmap the window, and draw it again when in dock mode */
1572 umap_cookie = xcb_unmap_window_checked(xcb_connection, walk->bar);
1573 if (config.hide_on_modifier == M_DOCK) {
1575 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1580 if (config.hide_on_modifier == M_HIDE) {
1581 /* Switching to hide mode, register for keyevents */
1582 register_xkb_keyevents();
1584 /* Switching to dock/invisible mode, deregister from keyevents */
1585 deregister_xkb_keyevents();
1589 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") ||
1590 xcb_request_failed(chg_cookie, "Could not change window") ||
1591 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1592 (redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") ||
1593 (config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) {
1601 * Render the bars, with buttons and statusline
1604 void draw_bars(bool unhide) {
1605 DLOG("Drawing Bars...\n");
1608 refresh_statusline();
1610 i3_output *outputs_walk;
1611 SLIST_FOREACH(outputs_walk, outputs, slist) {
1612 if (!outputs_walk->active) {
1613 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1616 if (outputs_walk->bar == XCB_NONE) {
1617 /* Oh shit, an active output without an own bar. Create it now! */
1618 reconfig_windows(false);
1620 /* First things first: clear the backbuffer */
1621 uint32_t color = colors.bar_bg;
1622 xcb_change_gc(xcb_connection,
1623 outputs_walk->bargc,
1626 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, bar_height };
1627 xcb_poly_fill_rectangle(xcb_connection,
1628 outputs_walk->buffer,
1629 outputs_walk->bargc,
1633 if (!TAILQ_EMPTY(&statusline_head)) {
1634 DLOG("Printing statusline!\n");
1636 /* Luckily we already prepared a seperate pixmap containing the rendered
1637 * statusline, we just have to copy the relevant parts to the relevant
1639 trayclient *trayclient;
1641 TAILQ_FOREACH(trayclient, outputs_walk->trayclients, tailq) {
1642 if (!trayclient->mapped)
1644 /* We assume the tray icons are quadratic (we use the font
1645 * *height* as *width* of the icons) because we configured them
1647 traypx += font.height + 2;
1649 /* Add 2px of padding if there are any tray icons */
1652 xcb_copy_area(xcb_connection,
1654 outputs_walk->buffer,
1655 outputs_walk->bargc,
1656 MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
1657 MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - traypx - 4)), 3,
1658 MIN(outputs_walk->rect.w - traypx - 4, statusline_width), font.height + 2);
1661 if (!config.disable_ws) {
1663 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1664 DLOG("Drawing Button for WS %s at x = %d, len = %d\n",
1665 i3string_as_utf8(ws_walk->name), i, ws_walk->name_width);
1666 uint32_t fg_color = colors.inactive_ws_fg;
1667 uint32_t bg_color = colors.inactive_ws_bg;
1668 uint32_t border_color = colors.inactive_ws_border;
1669 if (ws_walk->visible) {
1670 if (!ws_walk->focused) {
1671 fg_color = colors.active_ws_fg;
1672 bg_color = colors.active_ws_bg;
1673 border_color = colors.active_ws_border;
1675 fg_color = colors.focus_ws_fg;
1676 bg_color = colors.focus_ws_bg;
1677 border_color = colors.focus_ws_border;
1680 if (ws_walk->urgent) {
1681 DLOG("WS %s is urgent!\n", i3string_as_utf8(ws_walk->name));
1682 fg_color = colors.urgent_ws_fg;
1683 bg_color = colors.urgent_ws_bg;
1684 border_color = colors.urgent_ws_border;
1687 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1688 uint32_t vals_border[] = { border_color, border_color };
1689 xcb_change_gc(xcb_connection,
1690 outputs_walk->bargc,
1693 xcb_rectangle_t rect_border = { i, 1, ws_walk->name_width + 10, font.height + 4 };
1694 xcb_poly_fill_rectangle(xcb_connection,
1695 outputs_walk->buffer,
1696 outputs_walk->bargc,
1699 uint32_t vals[] = { bg_color, bg_color };
1700 xcb_change_gc(xcb_connection,
1701 outputs_walk->bargc,
1704 xcb_rectangle_t rect = { i + 1, 2, ws_walk->name_width + 8, font.height + 2 };
1705 xcb_poly_fill_rectangle(xcb_connection,
1706 outputs_walk->buffer,
1707 outputs_walk->bargc,
1710 set_font_colors(outputs_walk->bargc, fg_color, bg_color);
1711 draw_text(ws_walk->name, outputs_walk->buffer, outputs_walk->bargc,
1712 i + 5, 3, ws_walk->name_width);
1713 i += 10 + ws_walk->name_width + 1;
1718 if (binding.name && !config.disable_binding_mode_indicator) {
1719 uint32_t fg_color = colors.urgent_ws_fg;
1720 uint32_t bg_color = colors.urgent_ws_bg;
1721 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1723 uint32_t vals_border[] = { colors.urgent_ws_border, colors.urgent_ws_border };
1724 xcb_change_gc(xcb_connection,
1725 outputs_walk->bargc,
1728 xcb_rectangle_t rect_border = { i, 1, binding.width + 10, font.height + 4 };
1729 xcb_poly_fill_rectangle(xcb_connection,
1730 outputs_walk->buffer,
1731 outputs_walk->bargc,
1735 uint32_t vals[] = { bg_color, bg_color };
1736 xcb_change_gc(xcb_connection,
1737 outputs_walk->bargc,
1740 xcb_rectangle_t rect = { i + 1, 2, binding.width + 8, font.height + 2 };
1741 xcb_poly_fill_rectangle(xcb_connection,
1742 outputs_walk->buffer,
1743 outputs_walk->bargc,
1747 set_font_colors(outputs_walk->bargc, fg_color, bg_color);
1748 draw_text(binding.name, outputs_walk->buffer, outputs_walk->bargc, i + 5, 3, binding.width);
1756 /* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */
1758 config.hidden_state == S_SHOW ||
1761 } else if (config.hide_on_modifier == M_HIDE) {
1769 * Redraw the bars, i.e. simply copy the buffer to the barwindow
1772 void redraw_bars(void) {
1773 i3_output *outputs_walk;
1774 SLIST_FOREACH(outputs_walk, outputs, slist) {
1775 if (!outputs_walk->active) {
1778 xcb_copy_area(xcb_connection,
1779 outputs_walk->buffer,
1781 outputs_walk->bargc,
1784 outputs_walk->rect.w,
1785 outputs_walk->rect.h);
1786 xcb_flush(xcb_connection);
1791 * Set the current binding mode
1794 void set_current_mode(struct mode *current) {
1795 I3STRING_FREE(binding.name);
1797 activated_mode = binding.name != NULL;