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
12 #include <xcb/xproto.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 *root_screen;
51 xcb_window_t xcb_root;
53 /* selection window for tray support */
54 static xcb_window_t selwin = XCB_NONE;
55 static xcb_intern_atom_reply_t *tray_reply = NULL;
57 /* This is needed for integration with libi3 */
58 xcb_connection_t *conn;
60 /* The font we'll use */
63 /* Overall height of the bar (based on font size) */
66 /* 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 / logical_px(2);
152 block->x_append = padding_width / logical_px(2) + padding_width % logical_px(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 + logical_px(5)};
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 | XCB_GC_LINE_WIDTH;
189 uint32_t values[] = {colors.sep_fg, colors.bar_bg, logical_px(1)};
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;
247 values[1] = walk->rect.y + walk->rect.h - bar_height;
248 values[2] = walk->rect.w;
249 values[3] = bar_height;
250 values[4] = XCB_STACK_MODE_ABOVE;
251 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
252 cookie = xcb_configure_window_checked(xcb_connection,
257 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
260 xcb_map_window(xcb_connection, walk->bar);
265 * Parse the colors into a format that we can use
268 void init_colors(const struct xcb_color_strings_t *new_colors) {
269 #define PARSE_COLOR(name, def) \
271 colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
273 PARSE_COLOR(bar_fg, "#FFFFFF");
274 PARSE_COLOR(bar_bg, "#000000");
275 PARSE_COLOR(sep_fg, "#666666");
276 PARSE_COLOR(active_ws_fg, "#FFFFFF");
277 PARSE_COLOR(active_ws_bg, "#333333");
278 PARSE_COLOR(active_ws_border, "#333333");
279 PARSE_COLOR(inactive_ws_fg, "#888888");
280 PARSE_COLOR(inactive_ws_bg, "#222222");
281 PARSE_COLOR(inactive_ws_border, "#333333");
282 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
283 PARSE_COLOR(urgent_ws_bg, "#900000");
284 PARSE_COLOR(urgent_ws_border, "#2f343a");
285 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
286 PARSE_COLOR(focus_ws_bg, "#285577");
287 PARSE_COLOR(focus_ws_border, "#4c7899");
291 xcb_flush(xcb_connection);
295 * Handle a button-press-event (i.e. a mouse click on one of our bars).
296 * We determine, whether the click occured on a ws-button or if the scroll-
297 * wheel was used and change the workspace appropriately
300 void handle_button(xcb_button_press_event_t *event) {
303 /* Determine, which bar was clicked */
305 xcb_window_t bar = event->event;
306 SLIST_FOREACH(walk, outputs, slist) {
307 if (walk->bar == bar) {
313 DLOG("Unknown Bar klicked!\n");
317 /* TODO: Move this to extern get_ws_for_output() */
318 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
319 if (cur_ws->visible) {
324 if (cur_ws == NULL) {
325 DLOG("No Workspace active?\n");
329 int32_t x = event->event_x >= 0 ? event->event_x : 0;
330 int32_t original_x = x;
332 DLOG("Got Button %d\n", event->detail);
334 if (child_want_click_events()) {
335 /* If the child asked for click events,
336 * check if a status block has been clicked. */
338 /* First calculate width of tray area */
339 trayclient *trayclient;
341 TAILQ_FOREACH_REVERSE(trayclient, walk->trayclients, tc_head, tailq) {
342 if (!trayclient->mapped)
344 tray_width += (font.height + logical_px(2));
347 int block_x = 0, last_block_x;
348 int offset = (walk->rect.w - (statusline_width + tray_width)) - logical_px(10);
350 x = original_x - offset;
352 struct status_block *block;
354 TAILQ_FOREACH(block, &statusline_head, blocks) {
355 last_block_x = block_x;
356 block_x += block->width + block->x_offset + block->x_append;
358 if (x <= block_x && x >= last_block_x) {
359 send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y);
367 switch (event->detail) {
369 /* Mouse wheel up. We select the previous ws, if any.
370 * If there is no more workspace, don’t even send the workspace
371 * command, otherwise (with workspace auto_back_and_forth) we’d end
372 * up on the wrong workspace. */
373 if (cur_ws == TAILQ_FIRST(walk->workspaces))
376 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
379 /* Mouse wheel down. We select the next ws, if any.
380 * If there is no more workspace, don’t even send the workspace
381 * command, otherwise (with workspace auto_back_and_forth) we’d end
382 * up on the wrong workspace. */
383 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
386 cur_ws = TAILQ_NEXT(cur_ws, tailq);
389 /* Check if this event regards a workspace button */
390 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
392 if (x >= 0 && x < cur_ws->name_width + logical_px(10)) {
395 x -= cur_ws->name_width + logical_px(11);
398 /* Otherwise, focus our currently visible workspace if it is not
400 if (cur_ws == NULL) {
401 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
402 if (cur_ws->visible && !cur_ws->focused)
407 /* if there is nothing to focus, we are done */
416 /* To properly handle workspace names with double quotes in them, we need
417 * to escape the double quotes. Unfortunately, that’s rather ugly in C: We
418 * first count the number of double quotes, then we allocate a large enough
419 * buffer, then we copy character by character. */
422 const char *utf8_name = cur_ws->canonical_name;
423 for (const char *walk = utf8_name; *walk != '\0'; walk++) {
426 /* While we’re looping through the name anyway, we can save one
431 const size_t len = namelen + strlen("workspace \"\"") + 1;
432 char *buffer = scalloc(len + num_quotes);
433 strncpy(buffer, "workspace \"", strlen("workspace \""));
434 size_t inpos, outpos;
435 for (inpos = 0, outpos = strlen("workspace \"");
438 if (utf8_name[inpos] == '"') {
439 buffer[outpos] = '\\';
442 buffer[outpos] = utf8_name[inpos];
444 buffer[outpos] = '"';
445 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
450 * Adjusts the size of the tray window and alignment of the tray clients by
451 * configuring their respective x coordinates. To be called when mapping or
452 * unmapping a tray client window.
455 static void configure_trayclients(void) {
456 trayclient *trayclient;
458 SLIST_FOREACH(output, outputs, slist) {
463 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
464 if (!trayclient->mapped)
468 DLOG("Configuring tray window %08x to x=%d\n",
469 trayclient->win, output->rect.w - (clients * (font.height + logical_px(2))));
470 uint32_t x = output->rect.w - (clients * (font.height + logical_px(2)));
471 xcb_configure_window(xcb_connection,
480 * Handles ClientMessages (messages sent from another client directly to us).
482 * At the moment, only the tray window will receive client messages. All
483 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
486 static void handle_client_message(xcb_client_message_event_t *event) {
487 if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
488 event->format == 32) {
489 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
490 /* event->data.data32[0] is the timestamp */
491 uint32_t op = event->data.data32[1];
494 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
495 xcb_window_t client = event->data.data32[2];
497 /* Listen for PropertyNotify events to get the most recent value of
498 * the XEMBED_MAPPED atom, also listen for UnmapNotify events */
499 mask = XCB_CW_EVENT_MASK;
500 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
501 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
502 xcb_change_window_attributes(xcb_connection,
507 /* Request the _XEMBED_INFO property. The XEMBED specification
508 * (which is referred by the tray specification) says this *has* to
509 * be set, but VLC does not set it… */
512 xcb_get_property_cookie_t xembedc;
513 xcb_generic_error_t *error;
514 xembedc = xcb_get_property(xcb_connection,
518 XCB_GET_PROPERTY_TYPE_ANY,
522 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
526 ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
531 if (xembedr != NULL && xembedr->length != 0) {
532 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
533 uint32_t *xembed = xcb_get_property_value(xembedr);
534 DLOG("xembed version = %d\n", xembed[0]);
535 DLOG("xembed flags = %d\n", xembed[1]);
536 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
537 xe_version = xembed[0];
542 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
545 DLOG("X window %08x requested docking\n", client);
546 i3_output *walk, *output = NULL;
547 SLIST_FOREACH(walk, outputs, slist) {
550 if (config.tray_output) {
551 if ((strcasecmp(walk->name, config.tray_output) != 0) &&
552 (!walk->primary || strcasecmp("primary", config.tray_output) != 0))
556 DLOG("using output %s\n", walk->name);
560 /* In case of tray_output == primary and there is no primary output
561 * configured, we fall back to the first available output. */
562 if (output == NULL &&
563 config.tray_output &&
564 strcasecmp("primary", config.tray_output) == 0) {
565 SLIST_FOREACH(walk, outputs, slist) {
568 DLOG("Falling back to output %s because no primary output is configured\n", walk->name);
573 if (output == NULL) {
574 ELOG("No output found\n");
577 xcb_reparent_window(xcb_connection,
580 output->rect.w - font.height - 2,
582 /* We reconfigure the window to use a reasonable size. The systray
583 * specification explicitly says:
584 * Tray icons may be assigned any size by the system tray, and
585 * should do their best to cope with any size effectively
587 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
588 values[0] = font.height;
589 values[1] = font.height;
590 xcb_configure_window(xcb_connection,
595 /* send the XEMBED_EMBEDDED_NOTIFY message */
596 void *event = scalloc(32);
597 xcb_client_message_event_t *ev = event;
598 ev->response_type = XCB_CLIENT_MESSAGE;
600 ev->type = atoms[_XEMBED];
602 ev->data.data32[0] = XCB_CURRENT_TIME;
603 ev->data.data32[1] = atoms[XEMBED_EMBEDDED_NOTIFY];
604 ev->data.data32[2] = output->bar;
605 ev->data.data32[3] = xe_version;
606 xcb_send_event(xcb_connection,
609 XCB_EVENT_MASK_NO_EVENT,
613 /* Put the client inside the save set. Upon termination (whether
614 * killed or normal exit does not matter) of i3bar, these clients
615 * will be correctly reparented to their most closest living
616 * ancestor. Without this, tray icons might die when i3bar
618 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
620 trayclient *tc = smalloc(sizeof(trayclient));
622 tc->xe_version = xe_version;
624 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
627 DLOG("Mapping dock client\n");
628 xcb_map_window(xcb_connection, client);
630 DLOG("Not mapping dock client yet\n");
632 /* Trigger an update to copy the statusline text to the appropriate
634 configure_trayclients();
641 * Handles DestroyNotify events by removing the tray client from the data
642 * structure. According to the XEmbed protocol, this is one way for a tray
643 * client to finish the protocol. After this event is received, there is no
644 * further interaction with the tray client.
646 * See: http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
649 static void handle_destroy_notify(xcb_destroy_notify_event_t *event) {
650 DLOG("DestroyNotify for window = %08x, event = %08x\n", event->window, event->event);
653 SLIST_FOREACH(walk, outputs, slist) {
656 DLOG("checking output %s\n", walk->name);
657 trayclient *trayclient;
658 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
659 if (trayclient->win != event->window)
662 DLOG("Removing tray client with window ID %08x\n", event->window);
663 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
665 /* Trigger an update, we now have more space for the statusline */
666 configure_trayclients();
674 * Handles MapNotify events. These events happen when a tray client shows its
675 * window. We respond by realigning the tray clients.
678 static void handle_map_notify(xcb_map_notify_event_t *event) {
679 DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event);
682 SLIST_FOREACH(walk, outputs, slist) {
685 DLOG("checking output %s\n", walk->name);
686 trayclient *trayclient;
687 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
688 if (trayclient->win != event->window)
691 DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window);
692 trayclient->mapped = true;
694 /* Trigger an update, we now have more space for the statusline */
695 configure_trayclients();
702 * Handles UnmapNotify events. These events happen when a tray client hides its
703 * window. We respond by realigning the tray clients.
706 static void handle_unmap_notify(xcb_unmap_notify_event_t *event) {
707 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
710 SLIST_FOREACH(walk, outputs, slist) {
713 DLOG("checking output %s\n", walk->name);
714 trayclient *trayclient;
715 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
716 if (trayclient->win != event->window)
719 DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window);
720 trayclient->mapped = false;
722 /* Trigger an update, we now have more space for the statusline */
723 configure_trayclients();
731 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
732 * handled, which tells us whether a dock client should be mapped or unmapped.
735 static void handle_property_notify(xcb_property_notify_event_t *event) {
736 DLOG("PropertyNotify\n");
737 if (event->atom == atoms[_XEMBED_INFO] &&
738 event->state == XCB_PROPERTY_NEW_VALUE) {
739 DLOG("xembed_info updated\n");
740 trayclient *trayclient = NULL, *walk;
742 SLIST_FOREACH(o_walk, outputs, slist) {
746 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
747 if (walk->win != event->window)
757 ELOG("PropertyNotify received for unknown window %08x\n",
761 xcb_get_property_cookie_t xembedc;
762 xembedc = xcb_get_property_unchecked(xcb_connection,
766 XCB_GET_PROPERTY_TYPE_ANY,
770 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
773 if (xembedr == NULL || xembedr->length == 0) {
774 DLOG("xembed_info unset\n");
778 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
779 uint32_t *xembed = xcb_get_property_value(xembedr);
780 DLOG("xembed version = %d\n", xembed[0]);
781 DLOG("xembed flags = %d\n", xembed[1]);
782 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
783 DLOG("map-state now %d\n", map_it);
784 if (trayclient->mapped && !map_it) {
785 /* need to unmap the window */
786 xcb_unmap_window(xcb_connection, trayclient->win);
787 } else if (!trayclient->mapped && map_it) {
788 /* need to map the window */
789 xcb_map_window(xcb_connection, trayclient->win);
796 * Handle ConfigureRequests by denying them and sending the client a
797 * ConfigureNotify with its actual size.
800 static void handle_configure_request(xcb_configure_request_event_t *event) {
801 DLOG("ConfigureRequest for window = %08x\n", event->window);
803 trayclient *trayclient;
805 SLIST_FOREACH(output, outputs, slist) {
810 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
811 if (!trayclient->mapped)
815 if (trayclient->win != event->window)
818 xcb_rectangle_t rect;
819 rect.x = output->rect.w - (clients * (font.height + 2));
821 rect.width = font.height;
822 rect.height = font.height;
824 DLOG("This is a tray window. x = %d\n", rect.x);
825 fake_configure_notify(xcb_connection, rect, event->window, 0);
830 DLOG("WARNING: Could not find corresponding tray window.\n");
834 * This function is called immediately before the main loop locks. We flush xcb
835 * then (and only then)
838 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
839 xcb_flush(xcb_connection);
843 * This function is called immediately after the main loop locks, so when one
844 * of the watchers registered an event.
845 * We check whether an X-Event arrived and handle it.
848 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
849 xcb_generic_event_t *event;
851 if (xcb_connection_has_error(xcb_connection)) {
852 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
856 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
857 int type = (event->response_type & ~0x80);
859 if (type == xkb_base && xkb_base > -1) {
860 DLOG("received an xkb event\n");
862 xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event;
863 if (state->xkbType == XCB_XKB_STATE_NOTIFY) {
864 int modstate = state->mods & config.modifier;
866 #define DLOGMOD(modmask, status) \
870 DLOG("ShiftMask got " #status "!\n"); \
873 DLOG("ControlMask got " #status "!\n"); \
876 DLOG("Mod1Mask got " #status "!\n"); \
879 DLOG("Mod2Mask got " #status "!\n"); \
882 DLOG("Mod3Mask got " #status "!\n"); \
885 DLOG("Mod4Mask got " #status "!\n"); \
888 DLOG("Mod5Mask got " #status "!\n"); \
893 if (modstate != mod_pressed) {
895 DLOGMOD(config.modifier, released);
899 DLOGMOD(config.modifier, pressed);
900 activated_mode = false;
903 mod_pressed = modstate;
914 /* Expose-events happen, when the window needs to be redrawn */
917 case XCB_BUTTON_PRESS:
918 /* Button-press-events are mouse-buttons clicked on one of our bars */
919 handle_button((xcb_button_press_event_t *)event);
921 case XCB_CLIENT_MESSAGE:
922 /* Client messages are used for client-to-client communication, for
923 * example system tray widgets talk to us directly via client messages. */
924 handle_client_message((xcb_client_message_event_t *)event);
926 case XCB_DESTROY_NOTIFY:
927 /* DestroyNotify signifies the end of the XEmbed protocol */
928 handle_destroy_notify((xcb_destroy_notify_event_t *)event);
930 case XCB_UNMAP_NOTIFY:
931 /* UnmapNotify is received when a tray client hides its window. */
932 handle_unmap_notify((xcb_unmap_notify_event_t *)event);
935 handle_map_notify((xcb_map_notify_event_t *)event);
937 case XCB_PROPERTY_NOTIFY:
939 handle_property_notify((xcb_property_notify_event_t *)event);
941 case XCB_CONFIGURE_REQUEST:
942 /* ConfigureRequest, sent by a tray child */
943 handle_configure_request((xcb_configure_request_event_t *)event);
951 * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
955 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
959 * Early initialization of the connection to X11: Everything which does not
960 * depend on 'config'.
963 char *init_xcb_early() {
964 /* FIXME: xcb_connect leaks Memory */
965 xcb_connection = xcb_connect(NULL, &screen);
966 if (xcb_connection_has_error(xcb_connection)) {
967 ELOG("Cannot open display\n");
970 conn = xcb_connection;
971 DLOG("Connected to xcb\n");
973 /* We have to request the atoms we need */
974 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
975 #include "xcb_atoms.def"
977 root_screen = xcb_aux_get_screen(xcb_connection, screen);
978 xcb_root = root_screen->root;
980 /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
981 * this way, we can choose to crop it */
982 uint32_t mask = XCB_GC_FOREGROUND;
983 uint32_t vals[] = {colors.bar_bg, colors.bar_bg};
985 statusline_clear = xcb_generate_id(xcb_connection);
986 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
992 statusline_ctx = xcb_generate_id(xcb_connection);
993 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
999 statusline_pm = xcb_generate_id(xcb_connection);
1000 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1001 root_screen->root_depth,
1004 root_screen->width_in_pixels,
1005 root_screen->height_in_pixels);
1007 /* The various Watchers to communicate with xcb */
1008 xcb_io = smalloc(sizeof(ev_io));
1009 xcb_prep = smalloc(sizeof(ev_prepare));
1010 xcb_chk = smalloc(sizeof(ev_check));
1012 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
1013 ev_prepare_init(xcb_prep, &xcb_prep_cb);
1014 ev_check_init(xcb_chk, &xcb_chk_cb);
1016 ev_io_start(main_loop, xcb_io);
1017 ev_prepare_start(main_loop, xcb_prep);
1018 ev_check_start(main_loop, xcb_chk);
1020 /* Now we get the atoms and save them in a nice data structure */
1023 char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen);
1025 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
1026 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
1027 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
1035 * Register for xkb keyevents. To grab modifiers without blocking other applications from receiving key-events
1036 * involving that modifier, we sadly have to use xkb which is not yet fully supported
1040 void register_xkb_keyevents() {
1041 const xcb_query_extension_reply_t *extreply;
1042 extreply = xcb_get_extension_data(conn, &xcb_xkb_id);
1043 if (!extreply->present) {
1044 ELOG("xkb is not present on this server\n");
1047 DLOG("initializing xcb-xkb\n");
1048 xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
1049 xcb_xkb_select_events(conn,
1050 XCB_XKB_ID_USE_CORE_KBD,
1051 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1053 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1057 xkb_base = extreply->first_event;
1061 * Deregister from xkb keyevents.
1064 void deregister_xkb_keyevents() {
1065 xcb_xkb_select_events(conn,
1066 XCB_XKB_ID_USE_CORE_KBD,
1076 * Initialization which depends on 'config' being usable. Called after the
1077 * configuration has arrived.
1080 void init_xcb_late(char *fontname) {
1081 if (fontname == NULL)
1082 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
1085 font = load_font(fontname, true);
1087 DLOG("Calculated Font-height: %d\n", font.height);
1088 bar_height = font.height + logical_px(6);
1090 xcb_flush(xcb_connection);
1092 if (config.hide_on_modifier == M_HIDE)
1093 register_xkb_keyevents();
1097 * Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the
1101 static void send_tray_clientmessage(void) {
1102 uint8_t buffer[32] = {0};
1103 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)buffer;
1105 ev->response_type = XCB_CLIENT_MESSAGE;
1106 ev->window = xcb_root;
1107 ev->type = atoms[MANAGER];
1109 ev->data.data32[0] = XCB_CURRENT_TIME;
1110 ev->data.data32[1] = tray_reply->atom;
1111 ev->data.data32[2] = selwin;
1113 xcb_send_event(xcb_connection,
1121 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
1122 * for the X11 display we are running on, then acquiring the selection for this
1123 * atom. Afterwards, tray clients will send ClientMessages to our window.
1126 void init_tray(void) {
1127 DLOG("Initializing system tray functionality\n");
1128 /* request the tray manager atom for the X11 display we are running on */
1129 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
1130 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
1131 xcb_intern_atom_cookie_t tray_cookie;
1132 if (tray_reply == NULL)
1133 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
1135 /* tray support: we need a window to own the selection */
1136 selwin = xcb_generate_id(xcb_connection);
1137 uint32_t selmask = XCB_CW_OVERRIDE_REDIRECT;
1138 uint32_t selval[] = {1};
1139 xcb_create_window(xcb_connection,
1140 root_screen->root_depth,
1146 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1147 root_screen->root_visual,
1151 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
1153 xcb_change_property(xcb_connection,
1154 XCB_PROP_MODE_REPLACE,
1156 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1164 if (tray_reply == NULL) {
1165 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1166 ELOG("Could not get atom %s\n", atomname);
1171 xcb_set_selection_owner(xcb_connection,
1176 /* Verify that we have the selection */
1177 xcb_get_selection_owner_cookie_t selcookie;
1178 xcb_get_selection_owner_reply_t *selreply;
1180 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1181 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1182 ELOG("Could not get selection owner for %s\n", atomname);
1186 if (selreply->owner != selwin) {
1187 ELOG("Could not set the %s selection. "
1188 "Maybe another tray is already running?\n",
1190 /* NOTE that this error is not fatal. We just can’t provide tray
1196 send_tray_clientmessage();
1200 * We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window
1201 * to make GTK+ 3 applets with Symbolic Icons visible. If the colors are unset,
1202 * they assume a light background.
1203 * See also https://bugzilla.gnome.org/show_bug.cgi?id=679591
1206 void init_tray_colors(void) {
1207 /* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */
1208 const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF");
1210 DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg);
1212 char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'},
1213 {bar_fg[3], bar_fg[4], '\0'},
1214 {bar_fg[5], bar_fg[6], '\0'}};
1215 const uint8_t r = strtol(strgroups[0], NULL, 16);
1216 const uint8_t g = strtol(strgroups[1], NULL, 16);
1217 const uint8_t b = strtol(strgroups[2], NULL, 16);
1219 const uint16_t r16 = ((uint16_t)r << 8) | r;
1220 const uint16_t g16 = ((uint16_t)g << 8) | g;
1221 const uint16_t b16 = ((uint16_t)b << 8) | b;
1223 const uint32_t tray_colors[12] = {
1224 r16, g16, b16, /* foreground color */
1225 r16, g16, b16, /* error color */
1226 r16, g16, b16, /* warning color */
1227 r16, g16, b16, /* success color */
1230 xcb_change_property(xcb_connection,
1231 XCB_PROP_MODE_REPLACE,
1233 atoms[_NET_SYSTEM_TRAY_COLORS],
1241 * Cleanup the xcb-stuff.
1242 * Called once, before the program terminates.
1245 void clean_xcb(void) {
1248 SLIST_FOREACH(o_walk, outputs, slist) {
1249 destroy_window(o_walk);
1250 FREE(o_walk->trayclients);
1251 FREE(o_walk->workspaces);
1254 FREE_SLIST(outputs, i3_output);
1257 xcb_flush(xcb_connection);
1258 xcb_disconnect(xcb_connection);
1260 ev_check_stop(main_loop, xcb_chk);
1261 ev_prepare_stop(main_loop, xcb_prep);
1262 ev_io_stop(main_loop, xcb_io);
1270 * Get the earlier requested atoms and save them in the prepared data structure
1273 void get_atoms(void) {
1274 xcb_intern_atom_reply_t *reply;
1275 #define ATOM_DO(name) \
1276 reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1277 if (reply == NULL) { \
1278 ELOG("Could not get atom %s\n", #name); \
1279 exit(EXIT_FAILURE); \
1281 atoms[name] = reply->atom; \
1284 #include "xcb_atoms.def"
1285 DLOG("Got Atoms\n");
1289 * Reparents all tray clients of the specified output to the root window. This
1290 * is either used when shutting down, when an output appears (xrandr --output
1291 * VGA1 --off) or when the primary output changes.
1293 * Applications using the tray will start the protocol from the beginning again
1297 void kick_tray_clients(i3_output *output) {
1298 if (TAILQ_EMPTY(output->trayclients))
1301 trayclient *trayclient;
1302 while (!TAILQ_EMPTY(output->trayclients)) {
1303 trayclient = TAILQ_FIRST(output->trayclients);
1304 /* Unmap, then reparent (to root) the tray client windows */
1305 xcb_unmap_window(xcb_connection, trayclient->win);
1306 xcb_reparent_window(xcb_connection,
1312 /* We remove the trayclient right here. We might receive an UnmapNotify
1313 * event afterwards, but better safe than sorry. */
1314 TAILQ_REMOVE(output->trayclients, trayclient, tailq);
1317 /* Fake a DestroyNotify so that Qt re-adds tray icons.
1318 * We cannot actually destroy the window because then Qt will not restore
1319 * its event mask on the new window. */
1320 uint8_t buffer[32] = {0};
1321 xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t *)buffer;
1323 event->response_type = XCB_DESTROY_NOTIFY;
1324 event->event = selwin;
1325 event->window = selwin;
1327 xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)event);
1329 send_tray_clientmessage();
1333 * Destroy the bar of the specified output
1336 void destroy_window(i3_output *output) {
1337 if (output == NULL) {
1340 if (output->bar == XCB_NONE) {
1344 kick_tray_clients(output);
1345 xcb_destroy_window(xcb_connection, output->bar);
1346 output->bar = XCB_NONE;
1350 * Reallocate the statusline-buffer
1353 void realloc_sl_buffer(void) {
1354 DLOG("Re-allocating statusline-buffer, statusline_width = %d, root_screen->width_in_pixels = %d\n",
1355 statusline_width, root_screen->width_in_pixels);
1356 xcb_free_pixmap(xcb_connection, statusline_pm);
1357 statusline_pm = xcb_generate_id(xcb_connection);
1358 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1359 root_screen->root_depth,
1362 MAX(root_screen->width_in_pixels, statusline_width),
1365 uint32_t mask = XCB_GC_FOREGROUND;
1366 uint32_t vals[2] = {colors.bar_bg, colors.bar_bg};
1367 xcb_free_gc(xcb_connection, statusline_clear);
1368 statusline_clear = xcb_generate_id(xcb_connection);
1369 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1375 mask |= XCB_GC_BACKGROUND;
1376 vals[0] = colors.bar_fg;
1377 xcb_free_gc(xcb_connection, statusline_ctx);
1378 statusline_ctx = xcb_generate_id(xcb_connection);
1379 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1385 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
1386 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
1387 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
1393 * Reconfigure all bars and create new bars for recently activated outputs
1396 void reconfig_windows(bool redraw_bars) {
1399 static bool tray_configured = false;
1402 SLIST_FOREACH(walk, outputs, slist) {
1403 if (!walk->active) {
1404 /* If an output is not active, we destroy its bar */
1405 /* FIXME: Maybe we rather want to unmap? */
1406 DLOG("Destroying window for output %s\n", walk->name);
1407 destroy_window(walk);
1410 if (walk->bar == XCB_NONE) {
1411 DLOG("Creating Window for output %s\n", walk->name);
1413 walk->bar = xcb_generate_id(xcb_connection);
1414 walk->buffer = xcb_generate_id(xcb_connection);
1415 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
1416 /* Black background */
1417 values[0] = colors.bar_bg;
1418 /* If hide_on_modifier is set to hide or invisible mode, i3 is not supposed to manage our bar-windows */
1419 values[1] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1420 /* We enable the following EventMask fields:
1421 * EXPOSURE, to get expose events (we have to re-draw then)
1422 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1423 * child windows use ConfigureWindow
1424 * BUTTON_PRESS, to handle clicks on the workspace buttons
1426 values[2] = XCB_EVENT_MASK_EXPOSURE |
1427 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
1428 if (!config.disable_ws) {
1429 values[2] |= XCB_EVENT_MASK_BUTTON_PRESS;
1431 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1432 root_screen->root_depth,
1435 walk->rect.x, walk->rect.y + walk->rect.h - bar_height,
1436 walk->rect.w, bar_height,
1438 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1439 root_screen->root_visual,
1443 /* The double-buffer we use to render stuff off-screen */
1444 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1445 root_screen->root_depth,
1451 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1452 xcb_void_cookie_t class_cookie;
1453 class_cookie = xcb_change_property(xcb_connection,
1454 XCB_PROP_MODE_REPLACE,
1459 (strlen("i3bar") + 1) * 2,
1463 if (asprintf(&name, "i3bar for output %s", walk->name) == -1)
1464 err(EXIT_FAILURE, "asprintf()");
1465 xcb_void_cookie_t name_cookie;
1466 name_cookie = xcb_change_property(xcb_connection,
1467 XCB_PROP_MODE_REPLACE,
1476 /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
1478 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1479 XCB_PROP_MODE_REPLACE,
1481 atoms[_NET_WM_WINDOW_TYPE],
1485 (unsigned char *)&atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1487 /* We need to tell i3, where to reserve space for i3bar */
1488 /* left, right, top, bottom, left_start_y, left_end_y,
1489 * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
1491 /* A local struct to save the strut_partial property */
1497 uint32_t left_start_y;
1498 uint32_t left_end_y;
1499 uint32_t right_start_y;
1500 uint32_t right_end_y;
1501 uint32_t top_start_x;
1503 uint32_t bottom_start_x;
1504 uint32_t bottom_end_x;
1505 } __attribute__((__packed__)) strut_partial;
1506 memset(&strut_partial, 0, sizeof(strut_partial));
1508 switch (config.position) {
1512 strut_partial.top = bar_height;
1513 strut_partial.top_start_x = walk->rect.x;
1514 strut_partial.top_end_x = walk->rect.x + walk->rect.w;
1517 strut_partial.bottom = bar_height;
1518 strut_partial.bottom_start_x = walk->rect.x;
1519 strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
1522 xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
1523 XCB_PROP_MODE_REPLACE,
1525 atoms[_NET_WM_STRUT_PARTIAL],
1531 /* We also want a graphics-context for the bars (it defines the properties
1532 * with which we draw to them) */
1533 walk->bargc = xcb_generate_id(xcb_connection);
1534 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
1540 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1541 xcb_void_cookie_t map_cookie;
1542 if (config.hide_on_modifier == M_DOCK) {
1543 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1546 if (xcb_request_failed(win_cookie, "Could not create window") ||
1547 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1548 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1549 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1550 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1551 xcb_request_failed(strut_cookie, "Could not set strut") ||
1552 xcb_request_failed(gc_cookie, "Could not create graphical context") ||
1553 ((config.hide_on_modifier == M_DOCK) && xcb_request_failed(map_cookie, "Could not map window"))) {
1557 const char *tray_output = (config.tray_output ? config.tray_output : SLIST_FIRST(outputs)->name);
1558 if (!tray_configured && strcasecmp(tray_output, "none") != 0) {
1559 /* Configuration sanity check: ensure this i3bar instance handles the output on
1560 * which the tray should appear (e.g. don’t initialize a tray if tray_output ==
1561 * VGA-1 but output == [HDMI-1]).
1564 SLIST_FOREACH(output, outputs, slist) {
1565 if (strcasecmp(output->name, tray_output) == 0 ||
1566 (strcasecmp(tray_output, "primary") == 0 && output->primary)) {
1571 tray_configured = true;
1574 /* We already have a bar, so we just reconfigure it */
1575 mask = XCB_CONFIG_WINDOW_X |
1576 XCB_CONFIG_WINDOW_Y |
1577 XCB_CONFIG_WINDOW_WIDTH |
1578 XCB_CONFIG_WINDOW_HEIGHT |
1579 XCB_CONFIG_WINDOW_STACK_MODE;
1580 values[0] = walk->rect.x;
1581 values[1] = walk->rect.y + walk->rect.h - bar_height;
1582 values[2] = walk->rect.w;
1583 values[3] = bar_height;
1584 values[4] = XCB_STACK_MODE_ABOVE;
1586 DLOG("Destroying buffer for output %s\n", walk->name);
1587 xcb_free_pixmap(xcb_connection, walk->buffer);
1589 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1590 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1595 mask = XCB_CW_OVERRIDE_REDIRECT;
1596 values[0] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1597 DLOG("Changing Window attribute override_redirect for output %s to %d\n", walk->name, values[0]);
1598 xcb_void_cookie_t chg_cookie = xcb_change_window_attributes(xcb_connection,
1603 DLOG("Recreating buffer for output %s\n", walk->name);
1604 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1605 root_screen->root_depth,
1611 xcb_void_cookie_t map_cookie, umap_cookie;
1613 /* Unmap the window, and draw it again when in dock mode */
1614 umap_cookie = xcb_unmap_window_checked(xcb_connection, walk->bar);
1615 if (config.hide_on_modifier == M_DOCK) {
1617 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1622 if (config.hide_on_modifier == M_HIDE) {
1623 /* Switching to hide mode, register for keyevents */
1624 register_xkb_keyevents();
1626 /* Switching to dock/invisible mode, deregister from keyevents */
1627 deregister_xkb_keyevents();
1631 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") ||
1632 xcb_request_failed(chg_cookie, "Could not change window") ||
1633 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1634 (redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") ||
1635 (config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) {
1643 * Render the bars, with buttons and statusline
1646 void draw_bars(bool unhide) {
1647 DLOG("Drawing Bars...\n");
1650 refresh_statusline();
1652 i3_output *outputs_walk;
1653 SLIST_FOREACH(outputs_walk, outputs, slist) {
1654 if (!outputs_walk->active) {
1655 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1658 if (outputs_walk->bar == XCB_NONE) {
1659 /* Oh shit, an active output without an own bar. Create it now! */
1660 reconfig_windows(false);
1662 /* First things first: clear the backbuffer */
1663 uint32_t color = colors.bar_bg;
1664 xcb_change_gc(xcb_connection,
1665 outputs_walk->bargc,
1668 xcb_rectangle_t rect = {0, 0, outputs_walk->rect.w, bar_height};
1669 xcb_poly_fill_rectangle(xcb_connection,
1670 outputs_walk->buffer,
1671 outputs_walk->bargc,
1675 if (!TAILQ_EMPTY(&statusline_head)) {
1676 DLOG("Printing statusline!\n");
1678 /* Luckily we already prepared a seperate pixmap containing the rendered
1679 * statusline, we just have to copy the relevant parts to the relevant
1681 trayclient *trayclient;
1683 TAILQ_FOREACH(trayclient, outputs_walk->trayclients, tailq) {
1684 if (!trayclient->mapped)
1686 /* We assume the tray icons are quadratic (we use the font
1687 * *height* as *width* of the icons) because we configured them
1689 traypx += font.height + 2;
1691 /* Add 2px of padding if there are any tray icons */
1694 xcb_copy_area(xcb_connection,
1696 outputs_walk->buffer,
1697 outputs_walk->bargc,
1698 MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
1699 MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - traypx - 4)), 3,
1700 MIN(outputs_walk->rect.w - traypx - 4, (int)statusline_width), font.height + 2);
1703 if (!config.disable_ws) {
1705 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1706 DLOG("Drawing Button for WS %s at x = %d, len = %d\n",
1707 i3string_as_utf8(ws_walk->name), i, ws_walk->name_width);
1708 uint32_t fg_color = colors.inactive_ws_fg;
1709 uint32_t bg_color = colors.inactive_ws_bg;
1710 uint32_t border_color = colors.inactive_ws_border;
1711 if (ws_walk->visible) {
1712 if (!ws_walk->focused) {
1713 fg_color = colors.active_ws_fg;
1714 bg_color = colors.active_ws_bg;
1715 border_color = colors.active_ws_border;
1717 fg_color = colors.focus_ws_fg;
1718 bg_color = colors.focus_ws_bg;
1719 border_color = colors.focus_ws_border;
1722 if (ws_walk->urgent) {
1723 DLOG("WS %s is urgent!\n", i3string_as_utf8(ws_walk->name));
1724 fg_color = colors.urgent_ws_fg;
1725 bg_color = colors.urgent_ws_bg;
1726 border_color = colors.urgent_ws_border;
1729 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1730 uint32_t vals_border[] = {border_color, border_color};
1731 xcb_change_gc(xcb_connection,
1732 outputs_walk->bargc,
1735 xcb_rectangle_t rect_border = {i,
1737 ws_walk->name_width + logical_px(10),
1738 font.height + logical_px(4)};
1739 xcb_poly_fill_rectangle(xcb_connection,
1740 outputs_walk->buffer,
1741 outputs_walk->bargc,
1744 uint32_t vals[] = {bg_color, bg_color};
1745 xcb_change_gc(xcb_connection,
1746 outputs_walk->bargc,
1749 xcb_rectangle_t rect = {i + logical_px(1),
1751 ws_walk->name_width + logical_px(8),
1752 font.height + logical_px(2)};
1753 xcb_poly_fill_rectangle(xcb_connection,
1754 outputs_walk->buffer,
1755 outputs_walk->bargc,
1758 set_font_colors(outputs_walk->bargc, fg_color, bg_color);
1759 draw_text(ws_walk->name, outputs_walk->buffer, outputs_walk->bargc,
1760 i + logical_px(5), 3 * logical_px(1), ws_walk->name_width);
1761 i += logical_px(10) + ws_walk->name_width + logical_px(1);
1765 if (binding.name && !config.disable_binding_mode_indicator) {
1766 uint32_t fg_color = colors.urgent_ws_fg;
1767 uint32_t bg_color = colors.urgent_ws_bg;
1768 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1770 uint32_t vals_border[] = {colors.urgent_ws_border, colors.urgent_ws_border};
1771 xcb_change_gc(xcb_connection,
1772 outputs_walk->bargc,
1775 xcb_rectangle_t rect_border = {i, 1, binding.width + 10, font.height + 4};
1776 xcb_poly_fill_rectangle(xcb_connection,
1777 outputs_walk->buffer,
1778 outputs_walk->bargc,
1782 uint32_t vals[] = {bg_color, bg_color};
1783 xcb_change_gc(xcb_connection,
1784 outputs_walk->bargc,
1787 xcb_rectangle_t rect = {i + 1, 2, binding.width + 8, font.height + 2};
1788 xcb_poly_fill_rectangle(xcb_connection,
1789 outputs_walk->buffer,
1790 outputs_walk->bargc,
1794 set_font_colors(outputs_walk->bargc, fg_color, bg_color);
1795 draw_text(binding.name, outputs_walk->buffer, outputs_walk->bargc, i + 5, 3, binding.width);
1803 /* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */
1805 config.hidden_state == S_SHOW ||
1808 } else if (config.hide_on_modifier == M_HIDE) {
1816 * Redraw the bars, i.e. simply copy the buffer to the barwindow
1819 void redraw_bars(void) {
1820 i3_output *outputs_walk;
1821 SLIST_FOREACH(outputs_walk, outputs, slist) {
1822 if (!outputs_walk->active) {
1825 xcb_copy_area(xcb_connection,
1826 outputs_walk->buffer,
1828 outputs_walk->bargc,
1831 outputs_walk->rect.w,
1832 outputs_walk->rect.h);
1833 xcb_flush(xcb_connection);
1838 * Set the current binding mode
1841 void set_current_mode(struct mode *current) {
1842 I3STRING_FREE(binding.name);
1844 activated_mode = binding.name != NULL;