2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010 Axel Wagner and contributors (see also: LICENSE)
7 * xcb.c: Communicating with X
14 #include <xcb/xproto.h>
15 #include <xcb/xcb_aux.h>
16 #include <xcb/xcb_cursor.h>
30 #include <X11/XKBlib.h>
31 #include <X11/extensions/XKB.h>
33 #ifdef I3_ASAN_ENABLED
34 #include <sanitizer/lsan_interface.h>
39 /** This is the equivalent of XC_left_ptr. I’m not sure why xcb doesn’t have a
40 * constant for that. */
41 #define XCB_CURSOR_LEFT_PTR 68
43 /* We save the atoms in an easy to access array, indexed by an enum */
45 #define ATOM_DO(name) name,
46 #include "xcb_atoms.def"
50 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
51 xcb_atom_t atoms[NUM_ATOMS];
53 /* Variables, that are the same for all functions at all times */
54 xcb_connection_t *xcb_connection;
56 xcb_screen_t *root_screen;
57 xcb_window_t xcb_root;
58 static xcb_cursor_t cursor;
60 /* selection window for tray support */
61 static xcb_window_t selwin = XCB_NONE;
62 static xcb_intern_atom_reply_t *tray_reply = NULL;
64 /* This is needed for integration with libi3 */
65 xcb_connection_t *conn;
67 /* The font we'll use */
70 /* Icon size (based on font size) */
73 xcb_visualtype_t *visual_type;
75 xcb_colormap_t colormap;
77 /* Overall height of the bar (based on font size) */
80 /* These are only relevant for XKB, which we only need for grabbing modifiers */
84 /* Event watchers, to interact with the user */
90 /* The name of current binding mode */
93 /* Indicates whether a new binding mode was recently activated */
94 bool activated_mode = false;
96 /* The parsed colors */
101 color_t focus_bar_fg;
102 color_t focus_bar_bg;
103 color_t focus_sep_fg;
104 color_t active_ws_fg;
105 color_t active_ws_bg;
106 color_t active_ws_border;
107 color_t inactive_ws_fg;
108 color_t inactive_ws_bg;
109 color_t inactive_ws_border;
110 color_t urgent_ws_bg;
111 color_t urgent_ws_fg;
112 color_t urgent_ws_border;
115 color_t focus_ws_border;
116 color_t binding_mode_bg;
117 color_t binding_mode_fg;
118 color_t binding_mode_border;
120 struct xcb_colors_t colors;
122 /* Horizontal offset between a workspace label and button borders */
123 static const int ws_hoff_px = 4;
125 /* Vertical offset between a workspace label and button borders */
126 static const int ws_voff_px = 3;
128 /* Offset between two workspace buttons */
129 static const int ws_spacing_px = 1;
131 /* Offset between the statusline and 1) workspace buttons on the left
132 * 2) the tray or screen edge on the right */
133 static const int sb_hoff_px = 4;
135 /* Additional offset between the tray and the statusline, if the tray is not empty */
136 static const int tray_loff_px = 2;
138 /* Vertical offset between the bar and a separator */
139 static const int sep_voff_px = 4;
141 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
142 xcb_generic_error_t *err;
143 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
144 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
145 return err->error_code;
150 uint32_t get_sep_offset(struct status_block *block) {
151 if (!block->no_separator && block->sep_block_width > 0)
152 return block->sep_block_width / 2 + block->sep_block_width % 2;
156 int get_tray_width(struct tc_head *trayclients) {
157 trayclient *trayclient;
159 TAILQ_FOREACH_REVERSE(trayclient, trayclients, tc_head, tailq) {
160 if (!trayclient->mapped)
162 tray_width += icon_size + logical_px(config.tray_padding);
165 tray_width += logical_px(tray_loff_px);
170 * Draws a separator for the given block if necessary.
173 static void draw_separator(i3_output *output, uint32_t x, struct status_block *block, bool use_focus_colors) {
174 color_t sep_fg = (use_focus_colors ? colors.focus_sep_fg : colors.sep_fg);
175 color_t bar_bg = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg);
177 uint32_t sep_offset = get_sep_offset(block);
178 if (TAILQ_NEXT(block, blocks) == NULL || sep_offset == 0)
181 uint32_t center_x = x - sep_offset;
182 if (config.separator_symbol == NULL) {
183 /* Draw a classic one pixel, vertical separator. */
184 draw_util_rectangle(&output->statusline_buffer, sep_fg,
186 logical_px(sep_voff_px),
188 bar_height - 2 * logical_px(sep_voff_px));
190 /* Draw a custom separator. */
191 uint32_t separator_x = MAX(x - block->sep_block_width, center_x - separator_symbol_width / 2);
192 draw_util_text(config.separator_symbol, &output->statusline_buffer, sep_fg, bar_bg,
193 separator_x, logical_px(ws_voff_px), x - separator_x);
197 uint32_t predict_statusline_length(bool use_short_text) {
199 struct status_block *block;
201 TAILQ_FOREACH(block, &statusline_head, blocks) {
202 i3String *text = block->full_text;
203 struct status_block_render_desc *render = &block->full_render;
204 if (use_short_text && block->short_text != NULL) {
205 text = block->short_text;
206 render = &block->short_render;
209 if (i3string_get_num_bytes(text) == 0)
212 render->width = predict_text_width(text);
214 render->width += logical_px(2);
216 /* Compute offset and append for text aligment in min_width. */
217 if (block->min_width <= render->width) {
218 render->x_offset = 0;
219 render->x_append = 0;
221 uint32_t padding_width = block->min_width - render->width;
222 switch (block->align) {
224 render->x_append = padding_width;
227 render->x_offset = padding_width;
230 render->x_offset = padding_width / 2;
231 render->x_append = padding_width / 2 + padding_width % 2;
236 width += render->width + render->x_offset + render->x_append;
238 /* If this is not the last block, add some pixels for a separator. */
239 if (TAILQ_NEXT(block, blocks) != NULL)
240 width += block->sep_block_width;
247 * Redraws the statusline to the output's statusline_buffer
249 void draw_statusline(i3_output *output, uint32_t clip_left, bool use_focus_colors, bool use_short_text) {
250 struct status_block *block;
252 color_t bar_color = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg);
253 draw_util_clear_surface(&output->statusline_buffer, bar_color);
255 /* Use unsigned integer wraparound to clip off the left side.
256 * For example, if clip_left is 75, then x will start at the very large
257 * number INT_MAX-75, which is way outside the surface dimensions. Drawing
258 * to that x position is a no-op which XCB and Cairo safely ignore. Once x moves
259 * up by 75 and goes past INT_MAX, it will wrap around again to 0, and we start
260 * actually rendering content to the surface. */
261 uint32_t x = 0 - clip_left;
263 /* Draw the text of each block */
264 TAILQ_FOREACH(block, &statusline_head, blocks) {
265 i3String *text = block->full_text;
266 struct status_block_render_desc *render = &block->full_render;
267 if (use_short_text && block->short_text != NULL) {
268 text = block->short_text;
269 render = &block->short_render;
272 if (i3string_get_num_bytes(text) == 0)
277 fg_color = colors.urgent_ws_fg;
278 } else if (block->color) {
279 fg_color = draw_util_hex_to_color(block->color);
280 } else if (use_focus_colors) {
281 fg_color = colors.focus_bar_fg;
283 fg_color = colors.bar_fg;
286 color_t bg_color = bar_color;
288 int border_width = (block->border) ? logical_px(1) : 0;
289 int full_render_width = render->width + render->x_offset + render->x_append;
290 if (block->border || block->background || block->urgent) {
291 /* Let's determine the colors first. */
292 color_t border_color = bar_color;
294 border_color = colors.urgent_ws_border;
295 bg_color = colors.urgent_ws_bg;
298 border_color = draw_util_hex_to_color(block->border);
299 if (block->background)
300 bg_color = draw_util_hex_to_color(block->background);
303 /* Draw the border. */
304 draw_util_rectangle(&output->statusline_buffer, border_color,
307 bar_height - logical_px(2));
309 /* Draw the background. */
310 draw_util_rectangle(&output->statusline_buffer, bg_color,
312 logical_px(1) + border_width,
313 full_render_width - 2 * border_width,
314 bar_height - 2 * border_width - logical_px(2));
317 draw_util_text(text, &output->statusline_buffer, fg_color, bg_color,
318 x + render->x_offset + border_width, logical_px(ws_voff_px),
319 render->width - 2 * border_width);
320 x += full_render_width;
322 /* If this is not the last block, draw a separator. */
323 if (TAILQ_NEXT(block, blocks) != NULL) {
324 x += block->sep_block_width;
325 draw_separator(output, x, block, use_focus_colors);
331 * Hides all bars (unmaps them)
334 void hide_bars(void) {
335 if ((config.hide_on_modifier == M_DOCK) || (config.hidden_state == S_SHOW && config.hide_on_modifier == M_HIDE)) {
340 SLIST_FOREACH(walk, outputs, slist) {
344 xcb_unmap_window(xcb_connection, walk->bar.id);
350 * Unhides all bars (maps them)
353 void unhide_bars(void) {
354 if (config.hide_on_modifier != M_HIDE) {
359 xcb_void_cookie_t cookie;
365 SLIST_FOREACH(walk, outputs, slist) {
366 if (walk->bar.id == XCB_NONE) {
369 mask = XCB_CONFIG_WINDOW_X |
370 XCB_CONFIG_WINDOW_Y |
371 XCB_CONFIG_WINDOW_WIDTH |
372 XCB_CONFIG_WINDOW_HEIGHT |
373 XCB_CONFIG_WINDOW_STACK_MODE;
374 values[0] = walk->rect.x;
375 if (config.position == POS_TOP)
376 values[1] = walk->rect.y;
378 values[1] = walk->rect.y + walk->rect.h - bar_height;
379 values[2] = walk->rect.w;
380 values[3] = bar_height;
381 values[4] = XCB_STACK_MODE_ABOVE;
382 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
383 cookie = xcb_configure_window_checked(xcb_connection,
388 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
391 xcb_map_window(xcb_connection, walk->bar.id);
396 * Parse the colors into a format that we can use
399 void init_colors(const struct xcb_color_strings_t *new_colors) {
400 #define PARSE_COLOR(name, def) \
402 colors.name = draw_util_hex_to_color(new_colors->name ? new_colors->name : def); \
404 PARSE_COLOR(bar_fg, "#FFFFFF");
405 PARSE_COLOR(bar_bg, "#000000");
406 PARSE_COLOR(sep_fg, "#666666");
407 PARSE_COLOR(active_ws_fg, "#FFFFFF");
408 PARSE_COLOR(active_ws_bg, "#333333");
409 PARSE_COLOR(active_ws_border, "#333333");
410 PARSE_COLOR(inactive_ws_fg, "#888888");
411 PARSE_COLOR(inactive_ws_bg, "#222222");
412 PARSE_COLOR(inactive_ws_border, "#333333");
413 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
414 PARSE_COLOR(urgent_ws_bg, "#900000");
415 PARSE_COLOR(urgent_ws_border, "#2f343a");
416 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
417 PARSE_COLOR(focus_ws_bg, "#285577");
418 PARSE_COLOR(focus_ws_border, "#4c7899");
421 #define PARSE_COLOR_FALLBACK(name, fallback) \
423 colors.name = new_colors->name ? draw_util_hex_to_color(new_colors->name) : colors.fallback; \
426 /* For the binding mode indicator colors, we don't hardcode a default.
427 * Instead, we fall back to urgent_ws_* colors. */
428 PARSE_COLOR_FALLBACK(binding_mode_fg, urgent_ws_fg);
429 PARSE_COLOR_FALLBACK(binding_mode_bg, urgent_ws_bg);
430 PARSE_COLOR_FALLBACK(binding_mode_border, urgent_ws_border);
432 /* Similarly, for unspecified focused bar colors, we fall back to the
433 * regular bar colors. */
434 PARSE_COLOR_FALLBACK(focus_bar_fg, bar_fg);
435 PARSE_COLOR_FALLBACK(focus_bar_bg, bar_bg);
436 PARSE_COLOR_FALLBACK(focus_sep_fg, sep_fg);
437 #undef PARSE_COLOR_FALLBACK
440 xcb_flush(xcb_connection);
444 * Handle a button press event (i.e. a mouse click on one of our bars).
445 * We determine, whether the click occurred on a workspace button or if the scroll-
446 * wheel was used and change the workspace appropriately
449 void handle_button(xcb_button_press_event_t *event) {
450 /* Determine, which bar was clicked */
452 xcb_window_t bar = event->event;
453 SLIST_FOREACH(walk, outputs, slist) {
454 if (walk->bar.id == bar) {
460 DLOG("Unknown bar clicked!\n");
464 int32_t x = event->event_x >= 0 ? event->event_x : 0;
466 DLOG("Got button %d\n", event->detail);
468 int workspace_width = 0;
469 i3_ws *cur_ws = NULL, *clicked_ws = NULL, *ws_walk;
471 TAILQ_FOREACH(ws_walk, walk->workspaces, tailq) {
472 int w = 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
473 if (x >= workspace_width && x <= workspace_width + w)
474 clicked_ws = ws_walk;
475 if (ws_walk->visible)
477 workspace_width += w;
478 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
479 workspace_width += logical_px(ws_spacing_px);
482 if (x > workspace_width && child_want_click_events()) {
483 /* If the child asked for click events,
484 * check if a status block has been clicked. */
485 int tray_width = get_tray_width(walk->trayclients);
486 int block_x = 0, last_block_x;
487 int offset = walk->rect.w - walk->statusline_width - tray_width - logical_px(sb_hoff_px);
488 int32_t statusline_x = x - offset;
490 if (statusline_x >= 0 && statusline_x < walk->statusline_width) {
491 struct status_block *block;
492 int sep_offset_remainder = 0;
494 TAILQ_FOREACH(block, &statusline_head, blocks) {
495 i3String *text = block->full_text;
496 struct status_block_render_desc *render = &block->full_render;
497 if (walk->statusline_short_text && block->short_text != NULL) {
498 text = block->short_text;
499 render = &block->short_render;
502 if (i3string_get_num_bytes(text) == 0)
505 last_block_x = block_x;
506 block_x += render->width + render->x_offset + render->x_append + get_sep_offset(block) + sep_offset_remainder;
508 if (statusline_x <= block_x && statusline_x >= last_block_x) {
509 send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y);
513 sep_offset_remainder = block->sep_block_width - get_sep_offset(block);
518 /* If a custom command was specified for this mouse button, it overrides
519 * the default behavior. */
521 TAILQ_FOREACH(binding, &(config.bindings), bindings) {
522 if (binding->input_code != event->detail)
525 i3_send_msg(I3_IPC_MESSAGE_TYPE_RUN_COMMAND, binding->command);
529 if (cur_ws == NULL) {
530 DLOG("No workspace active?\n");
533 switch (event->detail) {
534 case XCB_BUTTON_SCROLL_UP:
535 case XCB_BUTTON_SCROLL_LEFT:
536 /* Mouse wheel up. We select the previous ws, if any.
537 * If there is no more workspace, don’t even send the workspace
538 * command, otherwise (with workspace auto_back_and_forth) we’d end
539 * up on the wrong workspace. */
540 if (cur_ws == TAILQ_FIRST(walk->workspaces))
543 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
545 case XCB_BUTTON_SCROLL_DOWN:
546 case XCB_BUTTON_SCROLL_RIGHT:
547 /* Mouse wheel down. We select the next ws, if any.
548 * If there is no more workspace, don’t even send the workspace
549 * command, otherwise (with workspace auto_back_and_forth) we’d end
550 * up on the wrong workspace. */
551 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
554 cur_ws = TAILQ_NEXT(cur_ws, tailq);
559 /* if no workspace was clicked, focus our currently visible
560 * workspace if it is not already focused */
561 if (cur_ws == NULL) {
562 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
563 if (cur_ws->visible && !cur_ws->focused)
568 /* if there is nothing to focus, we are done */
577 /* To properly handle workspace names with double quotes in them, we need
578 * to escape the double quotes. Unfortunately, that’s rather ugly in C: We
579 * first count the number of double quotes, then we allocate a large enough
580 * buffer, then we copy character by character. */
583 const char *utf8_name = cur_ws->canonical_name;
584 for (const char *walk = utf8_name; *walk != '\0'; walk++) {
585 if (*walk == '"' || *walk == '\\')
587 /* While we’re looping through the name anyway, we can save one
592 const size_t len = namelen + strlen("workspace \"\"") + 1;
593 char *buffer = scalloc(len + num_quotes, 1);
594 strncpy(buffer, "workspace \"", strlen("workspace \""));
595 size_t inpos, outpos;
596 for (inpos = 0, outpos = strlen("workspace \"");
599 if (utf8_name[inpos] == '"' || utf8_name[inpos] == '\\') {
600 buffer[outpos] = '\\';
603 buffer[outpos] = utf8_name[inpos];
605 buffer[outpos] = '"';
606 i3_send_msg(I3_IPC_MESSAGE_TYPE_RUN_COMMAND, buffer);
611 * Handle visibility notifications: when none of the bars are visible, e.g.
612 * if windows are in fullscreen on each output, suspend the child process.
615 static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
616 bool visible = (event->state != XCB_VISIBILITY_FULLY_OBSCURED);
620 SLIST_FOREACH(output, outputs, slist) {
621 if (!output->active) {
624 if (output->bar.id == event->window) {
625 if (output->visible == visible) {
628 output->visible = visible;
630 num_visible += output->visible;
633 if (num_visible == 0) {
635 } else if (num_visible == visible) {
636 /* Wake the child only when transitioning from 0 to 1 visible bar.
637 * We cannot transition from 0 to 2 or more visible bars at once since
638 * visibility events are delivered to each window separately */
644 * Adjusts the size of the tray window and alignment of the tray clients by
645 * configuring their respective x coordinates. To be called when mapping or
646 * unmapping a tray client window.
649 static void configure_trayclients(void) {
650 trayclient *trayclient;
652 SLIST_FOREACH(output, outputs, slist) {
657 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
658 if (!trayclient->mapped)
662 DLOG("Configuring tray window %08x to x=%d\n",
663 trayclient->win, output->rect.w - (clients * (icon_size + logical_px(config.tray_padding))));
664 uint32_t x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
665 xcb_configure_window(xcb_connection,
674 * Handles ClientMessages (messages sent from another client directly to us).
676 * At the moment, only the tray window will receive client messages. All
677 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
680 static void handle_client_message(xcb_client_message_event_t *event) {
681 if (event->type == atoms[I3_SYNC]) {
682 xcb_window_t window = event->data.data32[0];
683 uint32_t rnd = event->data.data32[1];
684 DLOG("[i3 sync protocol] Forwarding random value %d, X11 window 0x%08x to i3\n", rnd, window);
686 void *reply = scalloc(32, 1);
687 xcb_client_message_event_t *ev = reply;
689 ev->response_type = XCB_CLIENT_MESSAGE;
691 ev->type = atoms[I3_SYNC];
693 ev->data.data32[0] = window;
694 ev->data.data32[1] = rnd;
696 xcb_send_event(conn, false, xcb_root, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char *)ev);
699 } else if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
700 event->format == 32) {
701 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
702 /* event->data.data32[0] is the timestamp */
703 uint32_t op = event->data.data32[1];
706 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
707 xcb_window_t client = event->data.data32[2];
709 mask = XCB_CW_EVENT_MASK;
711 /* Needed to get the most recent value of XEMBED_MAPPED. */
712 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
713 /* Needed for UnmapNotify events. */
714 values[0] |= XCB_EVENT_MASK_STRUCTURE_NOTIFY;
715 /* Needed because some tray applications (e.g., VLC) use
716 * override_redirect which causes no ConfigureRequest to be sent. */
717 values[0] |= XCB_EVENT_MASK_RESIZE_REDIRECT;
719 xcb_change_window_attributes(xcb_connection, client, mask, values);
721 /* Request the _XEMBED_INFO property. The XEMBED specification
722 * (which is referred by the tray specification) says this *has* to
723 * be set, but VLC does not set it… */
726 xcb_get_property_cookie_t xembedc;
727 xcb_generic_error_t *error;
728 xembedc = xcb_get_property(xcb_connection,
732 XCB_GET_PROPERTY_TYPE_ANY,
736 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
740 ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
745 if (xembedr != NULL && xembedr->length != 0) {
746 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
747 uint32_t *xembed = xcb_get_property_value(xembedr);
748 DLOG("xembed version = %d\n", xembed[0]);
749 DLOG("xembed flags = %d\n", xembed[1]);
750 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
751 xe_version = xembed[0];
756 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
759 DLOG("X window %08x requested docking\n", client);
760 i3_output *output = NULL;
761 i3_output *walk = NULL;
762 tray_output_t *tray_output = NULL;
763 /* We need to iterate through the tray_output assignments first in
764 * order to prioritize them. Otherwise, if this bar manages two
765 * outputs and both are assigned as tray_output as well, the first
766 * output in our list would receive the tray rather than the first
767 * one defined via tray_output. */
768 TAILQ_FOREACH(tray_output, &(config.tray_outputs), tray_outputs) {
769 SLIST_FOREACH(walk, outputs, slist) {
773 if (strcasecmp(walk->name, tray_output->output) == 0) {
774 DLOG("Found tray_output assignment for output %s.\n", walk->name);
779 if (walk->primary && strcasecmp("primary", tray_output->output) == 0) {
780 DLOG("Found tray_output assignment on primary output %s.\n", walk->name);
786 /* If we found an output, we're done. */
791 /* If no tray_output has been specified, we fall back to the first
792 * available output. */
793 if (output == NULL && TAILQ_EMPTY(&(config.tray_outputs))) {
794 SLIST_FOREACH(walk, outputs, slist) {
797 DLOG("Falling back to output %s because no primary output is configured\n", walk->name);
803 if (output == NULL) {
804 ELOG("No output found\n");
808 xcb_void_cookie_t rcookie = xcb_reparent_window(xcb_connection,
811 output->rect.w - icon_size - logical_px(config.tray_padding),
812 logical_px(config.tray_padding));
813 if (xcb_request_failed(rcookie, "Could not reparent window. Maybe it is using an incorrect depth/visual?"))
816 /* We reconfigure the window to use a reasonable size. The systray
817 * specification explicitly says:
818 * Tray icons may be assigned any size by the system tray, and
819 * should do their best to cope with any size effectively
821 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
822 values[0] = icon_size;
823 values[1] = icon_size;
824 xcb_configure_window(xcb_connection,
829 /* send the XEMBED_EMBEDDED_NOTIFY message */
830 void *event = scalloc(32, 1);
831 xcb_client_message_event_t *ev = event;
832 ev->response_type = XCB_CLIENT_MESSAGE;
834 ev->type = atoms[_XEMBED];
836 ev->data.data32[0] = XCB_CURRENT_TIME;
837 ev->data.data32[1] = XEMBED_EMBEDDED_NOTIFY;
838 ev->data.data32[2] = output->bar.id;
839 ev->data.data32[3] = xe_version;
840 xcb_send_event(xcb_connection,
843 XCB_EVENT_MASK_NO_EVENT,
847 /* Put the client inside the save set. Upon termination (whether
848 * killed or normal exit does not matter) of i3bar, these clients
849 * will be correctly reparented to their most closest living
850 * ancestor. Without this, tray icons might die when i3bar
852 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
854 trayclient *tc = smalloc(sizeof(trayclient));
856 tc->xe_version = xe_version;
858 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
861 DLOG("Mapping dock client\n");
862 xcb_map_window(xcb_connection, client);
864 DLOG("Not mapping dock client yet\n");
866 /* Trigger an update to copy the statusline text to the appropriate
868 configure_trayclients();
875 * Handles DestroyNotify events by removing the tray client from the data
876 * structure. According to the XEmbed protocol, this is one way for a tray
877 * client to finish the protocol. After this event is received, there is no
878 * further interaction with the tray client.
880 * See: https://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
883 static void handle_destroy_notify(xcb_destroy_notify_event_t *event) {
884 DLOG("DestroyNotify for window = %08x, event = %08x\n", event->window, event->event);
887 SLIST_FOREACH(walk, outputs, slist) {
890 DLOG("checking output %s\n", walk->name);
891 trayclient *trayclient;
892 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
893 if (trayclient->win != event->window) {
897 DLOG("Removing tray client with window ID %08x\n", event->window);
898 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
901 /* Trigger an update, we now have more space for the statusline */
902 configure_trayclients();
910 * Handles MapNotify events. These events happen when a tray client shows its
911 * window. We respond by realigning the tray clients.
914 static void handle_map_notify(xcb_map_notify_event_t *event) {
915 DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event);
918 SLIST_FOREACH(walk, outputs, slist) {
921 DLOG("checking output %s\n", walk->name);
922 trayclient *trayclient;
923 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
924 if (trayclient->win != event->window)
927 DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window);
928 trayclient->mapped = true;
930 /* Trigger an update, we now have more space for the statusline */
931 configure_trayclients();
938 * Handles UnmapNotify events. These events happen when a tray client hides its
939 * window. We respond by realigning the tray clients.
942 static void handle_unmap_notify(xcb_unmap_notify_event_t *event) {
943 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
946 SLIST_FOREACH(walk, outputs, slist) {
949 DLOG("checking output %s\n", walk->name);
950 trayclient *trayclient;
951 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
952 if (trayclient->win != event->window)
955 DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window);
956 trayclient->mapped = false;
958 /* Trigger an update, we now have more space for the statusline */
959 configure_trayclients();
967 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
968 * handled, which tells us whether a dock client should be mapped or unmapped.
971 static void handle_property_notify(xcb_property_notify_event_t *event) {
972 DLOG("PropertyNotify\n");
973 if (event->atom == atoms[_XEMBED_INFO] &&
974 event->state == XCB_PROPERTY_NEW_VALUE) {
975 DLOG("xembed_info updated\n");
976 trayclient *trayclient = NULL, *walk;
978 SLIST_FOREACH(o_walk, outputs, slist) {
982 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
983 if (walk->win != event->window)
993 ELOG("PropertyNotify received for unknown window %08x\n",
997 xcb_get_property_cookie_t xembedc;
998 xembedc = xcb_get_property_unchecked(xcb_connection,
1001 atoms[_XEMBED_INFO],
1002 XCB_GET_PROPERTY_TYPE_ANY,
1006 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
1009 if (xembedr == NULL || xembedr->length == 0) {
1010 DLOG("xembed_info unset\n");
1014 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
1015 uint32_t *xembed = xcb_get_property_value(xembedr);
1016 DLOG("xembed version = %d\n", xembed[0]);
1017 DLOG("xembed flags = %d\n", xembed[1]);
1018 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
1019 DLOG("map state now %d\n", map_it);
1020 if (trayclient->mapped && !map_it) {
1021 /* need to unmap the window */
1022 xcb_unmap_window(xcb_connection, trayclient->win);
1023 } else if (!trayclient->mapped && map_it) {
1024 /* need to map the window */
1025 xcb_map_window(xcb_connection, trayclient->win);
1032 * If a tray client attempts to change its size we deny the request and respond
1033 * by telling it its actual size.
1036 static void handle_configuration_change(xcb_window_t window) {
1037 trayclient *trayclient;
1039 SLIST_FOREACH(output, outputs, slist) {
1040 if (!output->active)
1044 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
1045 if (!trayclient->mapped)
1049 if (trayclient->win != window)
1052 xcb_rectangle_t rect;
1053 rect.x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
1054 rect.y = logical_px(config.tray_padding);
1055 rect.width = icon_size;
1056 rect.height = icon_size;
1058 DLOG("This is a tray window. x = %d\n", rect.x);
1059 fake_configure_notify(xcb_connection, rect, window, 0);
1064 DLOG("WARNING: Could not find corresponding tray window.\n");
1067 static void handle_configure_request(xcb_configure_request_event_t *event) {
1068 DLOG("ConfigureRequest for window = %08x\n", event->window);
1069 handle_configuration_change(event->window);
1072 static void handle_resize_request(xcb_resize_request_event_t *event) {
1073 DLOG("ResizeRequest for window = %08x\n", event->window);
1074 handle_configuration_change(event->window);
1078 * This function is called immediately before the main loop locks. We flush xcb
1079 * then (and only then)
1082 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
1083 xcb_flush(xcb_connection);
1087 * This function is called immediately after the main loop locks, so when one
1088 * of the watchers registered an event.
1089 * We check whether an X-Event arrived and handle it.
1092 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
1093 xcb_generic_event_t *event;
1095 if (xcb_connection_has_error(xcb_connection)) {
1096 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
1097 #ifdef I3_ASAN_ENABLED
1098 __lsan_do_leak_check();
1103 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
1104 if (event->response_type == 0) {
1105 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
1106 DLOG("Received X11 error, sequence 0x%x, error_code = %d\n", error->sequence, error->error_code);
1111 int type = (event->response_type & ~0x80);
1113 if (type == xkb_base && xkb_base > -1) {
1114 DLOG("received an xkb event\n");
1116 xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event;
1117 if (state->xkbType == XCB_XKB_STATE_NOTIFY && config.modifier != XCB_NONE) {
1118 int modstate = state->mods & config.modifier;
1120 #define DLOGMOD(modmask, status) \
1122 switch (modmask) { \
1124 DLOG("ShiftMask got " #status "!\n"); \
1127 DLOG("ControlMask got " #status "!\n"); \
1130 DLOG("Mod1Mask got " #status "!\n"); \
1133 DLOG("Mod2Mask got " #status "!\n"); \
1136 DLOG("Mod3Mask got " #status "!\n"); \
1139 DLOG("Mod4Mask got " #status "!\n"); \
1142 DLOG("Mod5Mask got " #status "!\n"); \
1147 if (modstate != mod_pressed) {
1148 if (modstate == 0) {
1149 DLOGMOD(config.modifier, released);
1150 if (!activated_mode)
1153 DLOGMOD(config.modifier, pressed);
1154 activated_mode = false;
1157 mod_pressed = modstate;
1167 case XCB_VISIBILITY_NOTIFY:
1168 /* Visibility change: a bar is [un]obscured by other window */
1169 handle_visibility_notify((xcb_visibility_notify_event_t *)event);
1172 if (((xcb_expose_event_t *)event)->count == 0) {
1173 /* Expose-events happen, when the window needs to be redrawn */
1178 case XCB_BUTTON_PRESS:
1179 /* Button press events are mouse buttons clicked on one of our bars */
1180 handle_button((xcb_button_press_event_t *)event);
1182 case XCB_CLIENT_MESSAGE:
1183 /* Client messages are used for client-to-client communication, for
1184 * example system tray widgets talk to us directly via client messages. */
1185 handle_client_message((xcb_client_message_event_t *)event);
1187 case XCB_DESTROY_NOTIFY:
1188 /* DestroyNotify signifies the end of the XEmbed protocol */
1189 handle_destroy_notify((xcb_destroy_notify_event_t *)event);
1191 case XCB_UNMAP_NOTIFY:
1192 /* UnmapNotify is received when a tray client hides its window. */
1193 handle_unmap_notify((xcb_unmap_notify_event_t *)event);
1195 case XCB_MAP_NOTIFY:
1196 handle_map_notify((xcb_map_notify_event_t *)event);
1198 case XCB_PROPERTY_NOTIFY:
1199 /* PropertyNotify */
1200 handle_property_notify((xcb_property_notify_event_t *)event);
1202 case XCB_CONFIGURE_REQUEST:
1203 /* ConfigureRequest, sent by a tray child */
1204 handle_configure_request((xcb_configure_request_event_t *)event);
1206 case XCB_RESIZE_REQUEST:
1207 /* ResizeRequest sent by a tray child using override_redirect. */
1208 handle_resize_request((xcb_resize_request_event_t *)event);
1216 * Dummy callback. We only need this, so that the prepare and check watchers
1220 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
1224 * Early initialization of the connection to X11: Everything which does not
1225 * depend on 'config'.
1228 char *init_xcb_early() {
1229 /* FIXME: xcb_connect leaks memory */
1230 xcb_connection = xcb_connect(NULL, &screen);
1231 if (xcb_connection_has_error(xcb_connection)) {
1232 ELOG("Cannot open display\n");
1235 conn = xcb_connection;
1236 DLOG("Connected to xcb\n");
1238 /* We have to request the atoms we need */
1239 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
1240 #include "xcb_atoms.def"
1242 root_screen = xcb_aux_get_screen(xcb_connection, screen);
1243 xcb_root = root_screen->root;
1245 depth = root_screen->root_depth;
1246 colormap = root_screen->default_colormap;
1247 visual_type = get_visualtype(root_screen);
1249 xcb_cursor_context_t *cursor_ctx;
1250 if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) == 0) {
1251 cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr");
1252 xcb_cursor_context_free(cursor_ctx);
1254 cursor = xcb_generate_id(xcb_connection);
1255 i3Font cursor_font = load_font("cursor", false);
1256 xcb_create_glyph_cursor(
1259 cursor_font.specific.xcb.id,
1260 cursor_font.specific.xcb.id,
1261 XCB_CURSOR_LEFT_PTR,
1262 XCB_CURSOR_LEFT_PTR + 1,
1264 65535, 65535, 65535);
1267 /* The various watchers to communicate with xcb */
1268 xcb_io = smalloc(sizeof(ev_io));
1269 xcb_prep = smalloc(sizeof(ev_prepare));
1270 xcb_chk = smalloc(sizeof(ev_check));
1272 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
1273 ev_prepare_init(xcb_prep, &xcb_prep_cb);
1274 ev_check_init(xcb_chk, &xcb_chk_cb);
1276 /* Within an event loop iteration, run the xcb_chk watcher last: other
1277 * watchers might call xcb_flush(), which, unexpectedly, can also read
1278 * events into the queue (see _xcb_conn_wait). Hence, we need to drain xcb’s
1279 * queue last, otherwise we risk dead-locking. */
1280 ev_set_priority(xcb_chk, EV_MINPRI);
1282 ev_io_start(main_loop, xcb_io);
1283 ev_prepare_start(main_loop, xcb_prep);
1284 ev_check_start(main_loop, xcb_chk);
1286 /* Now we get the atoms and save them in a nice data structure */
1289 char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen);
1295 * Register for xkb keyevents. To grab modifiers without blocking other applications from receiving key events
1296 * involving that modifier, we sadly have to use xkb which is not yet fully supported
1300 void register_xkb_keyevents() {
1301 const xcb_query_extension_reply_t *extreply;
1302 extreply = xcb_get_extension_data(conn, &xcb_xkb_id);
1303 if (!extreply->present) {
1304 ELOG("xkb is not present on this server\n");
1307 DLOG("initializing xcb-xkb\n");
1308 xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
1309 xcb_xkb_select_events(conn,
1310 XCB_XKB_ID_USE_CORE_KBD,
1311 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1313 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1317 xkb_base = extreply->first_event;
1321 * Deregister from xkb keyevents.
1324 void deregister_xkb_keyevents() {
1325 xcb_xkb_select_events(conn,
1326 XCB_XKB_ID_USE_CORE_KBD,
1336 * Initialization which depends on 'config' being usable. Called after the
1337 * configuration has arrived.
1340 void init_xcb_late(char *fontname) {
1341 if (fontname == NULL)
1342 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
1345 font = load_font(fontname, true);
1347 DLOG("Calculated font height: %d\n", font.height);
1348 bar_height = font.height + 2 * logical_px(ws_voff_px);
1349 icon_size = bar_height - 2 * logical_px(config.tray_padding);
1351 if (config.separator_symbol)
1352 separator_symbol_width = predict_text_width(config.separator_symbol);
1354 xcb_flush(xcb_connection);
1356 if (config.hide_on_modifier == M_HIDE)
1357 register_xkb_keyevents();
1361 * Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the
1365 static void send_tray_clientmessage(void) {
1366 uint8_t buffer[32] = {0};
1367 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)buffer;
1369 ev->response_type = XCB_CLIENT_MESSAGE;
1370 ev->window = xcb_root;
1371 ev->type = atoms[MANAGER];
1373 ev->data.data32[0] = XCB_CURRENT_TIME;
1374 ev->data.data32[1] = tray_reply->atom;
1375 ev->data.data32[2] = selwin;
1377 xcb_send_event(xcb_connection,
1385 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
1386 * for the X11 display we are running on, then acquiring the selection for this
1387 * atom. Afterwards, tray clients will send ClientMessages to our window.
1390 void init_tray(void) {
1391 DLOG("Initializing system tray functionality\n");
1392 /* request the tray manager atom for the X11 display we are running on */
1393 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
1394 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
1395 xcb_intern_atom_cookie_t tray_cookie;
1396 if (tray_reply == NULL)
1397 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
1399 /* tray support: we need a window to own the selection */
1400 selwin = xcb_generate_id(xcb_connection);
1401 uint32_t selmask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_COLORMAP;
1402 uint32_t selval[] = {root_screen->black_pixel, root_screen->black_pixel, 1, colormap};
1403 xcb_create_window(xcb_connection,
1410 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1411 visual_type->visual_id,
1415 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
1417 xcb_change_property(xcb_connection,
1418 XCB_PROP_MODE_REPLACE,
1420 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1425 xcb_change_property(xcb_connection,
1426 XCB_PROP_MODE_REPLACE,
1428 atoms[_NET_SYSTEM_TRAY_VISUAL],
1432 &visual_type->visual_id);
1436 if (tray_reply == NULL) {
1437 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1438 ELOG("Could not get atom %s\n", atomname);
1443 xcb_set_selection_owner(xcb_connection,
1448 /* Verify that we have the selection */
1449 xcb_get_selection_owner_cookie_t selcookie;
1450 xcb_get_selection_owner_reply_t *selreply;
1452 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1453 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1454 ELOG("Could not get selection owner for %s\n", atomname);
1458 if (selreply->owner != selwin) {
1459 ELOG("Could not set the %s selection. "
1460 "Maybe another tray is already running?\n",
1462 /* NOTE that this error is not fatal. We just can’t provide tray
1470 send_tray_clientmessage();
1474 * We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window
1475 * to make GTK+ 3 applets with symbolic icons visible. If the colors are unset,
1476 * they assume a light background.
1477 * See also https://bugzilla.gnome.org/show_bug.cgi?id=679591
1480 void init_tray_colors(void) {
1481 /* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */
1482 const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF");
1484 DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg);
1486 char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'},
1487 {bar_fg[3], bar_fg[4], '\0'},
1488 {bar_fg[5], bar_fg[6], '\0'}};
1489 const uint8_t r = strtol(strgroups[0], NULL, 16);
1490 const uint8_t g = strtol(strgroups[1], NULL, 16);
1491 const uint8_t b = strtol(strgroups[2], NULL, 16);
1493 const uint16_t r16 = ((uint16_t)r << 8) | r;
1494 const uint16_t g16 = ((uint16_t)g << 8) | g;
1495 const uint16_t b16 = ((uint16_t)b << 8) | b;
1497 const uint32_t tray_colors[12] = {
1498 r16, g16, b16, /* foreground color */
1499 r16, g16, b16, /* error color */
1500 r16, g16, b16, /* warning color */
1501 r16, g16, b16, /* success color */
1504 xcb_change_property(xcb_connection,
1505 XCB_PROP_MODE_REPLACE,
1507 atoms[_NET_SYSTEM_TRAY_COLORS],
1515 * Cleanup the xcb stuff.
1516 * Called once, before the program terminates.
1519 void clean_xcb(void) {
1524 xcb_free_cursor(xcb_connection, cursor);
1525 xcb_flush(xcb_connection);
1526 xcb_aux_sync(xcb_connection);
1527 xcb_disconnect(xcb_connection);
1529 ev_check_stop(main_loop, xcb_chk);
1530 ev_prepare_stop(main_loop, xcb_prep);
1531 ev_io_stop(main_loop, xcb_io);
1539 * Get the earlier requested atoms and save them in the prepared data structure
1542 void get_atoms(void) {
1543 xcb_intern_atom_reply_t *reply;
1544 #define ATOM_DO(name) \
1545 reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1546 if (reply == NULL) { \
1547 ELOG("Could not get atom %s\n", #name); \
1548 exit(EXIT_FAILURE); \
1550 atoms[name] = reply->atom; \
1553 #include "xcb_atoms.def"
1554 DLOG("Got atoms\n");
1558 * Reparents all tray clients of the specified output to the root window. This
1559 * is either used when shutting down, when an output appears (xrandr --output
1560 * VGA1 --off) or when the primary output changes.
1562 * Applications using the tray will start the protocol from the beginning again
1566 void kick_tray_clients(i3_output *output) {
1567 if (TAILQ_EMPTY(output->trayclients))
1570 trayclient *trayclient;
1571 while (!TAILQ_EMPTY(output->trayclients)) {
1572 trayclient = TAILQ_FIRST(output->trayclients);
1573 /* Unmap, then reparent (to root) the tray client windows */
1574 xcb_unmap_window(xcb_connection, trayclient->win);
1575 xcb_reparent_window(xcb_connection,
1581 /* We remove the trayclient right here. We might receive an UnmapNotify
1582 * event afterwards, but better safe than sorry. */
1583 TAILQ_REMOVE(output->trayclients, trayclient, tailq);
1587 /* Fake a DestroyNotify so that Qt re-adds tray icons.
1588 * We cannot actually destroy the window because then Qt will not restore
1589 * its event mask on the new window. */
1590 uint8_t buffer[32] = {0};
1591 xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t *)buffer;
1593 event->response_type = XCB_DESTROY_NOTIFY;
1594 event->event = selwin;
1595 event->window = selwin;
1597 xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)event);
1599 send_tray_clientmessage();
1603 * Destroy the bar of the specified output
1606 void destroy_window(i3_output *output) {
1607 if (output == NULL) {
1610 if (output->bar.id == XCB_NONE) {
1614 kick_tray_clients(output);
1615 xcb_destroy_window(xcb_connection, output->bar.id);
1616 output->bar.id = XCB_NONE;
1619 /* Strut partial tells i3 where to reserve space for i3bar. This is determined
1620 * by the `position` bar config directive. */
1621 xcb_void_cookie_t config_strut_partial(i3_output *output) {
1622 /* A local struct to save the strut_partial property */
1628 uint32_t left_start_y;
1629 uint32_t left_end_y;
1630 uint32_t right_start_y;
1631 uint32_t right_end_y;
1632 uint32_t top_start_x;
1634 uint32_t bottom_start_x;
1635 uint32_t bottom_end_x;
1636 } __attribute__((__packed__)) strut_partial;
1637 memset(&strut_partial, 0, sizeof(strut_partial));
1639 switch (config.position) {
1643 strut_partial.top = bar_height;
1644 strut_partial.top_start_x = output->rect.x;
1645 strut_partial.top_end_x = output->rect.x + output->rect.w;
1648 strut_partial.bottom = bar_height;
1649 strut_partial.bottom_start_x = output->rect.x;
1650 strut_partial.bottom_end_x = output->rect.x + output->rect.w;
1653 return xcb_change_property(xcb_connection,
1654 XCB_PROP_MODE_REPLACE,
1656 atoms[_NET_WM_STRUT_PARTIAL],
1664 * Reconfigure all bars and create new bars for recently activated outputs
1667 void reconfig_windows(bool redraw_bars) {
1670 static bool tray_configured = false;
1673 SLIST_FOREACH(walk, outputs, slist) {
1674 if (!walk->active) {
1675 /* If an output is not active, we destroy its bar */
1676 /* FIXME: Maybe we rather want to unmap? */
1677 DLOG("Destroying window for output %s\n", walk->name);
1678 destroy_window(walk);
1681 if (walk->bar.id == XCB_NONE) {
1682 DLOG("Creating window for output %s\n", walk->name);
1684 xcb_window_t bar_id = xcb_generate_id(xcb_connection);
1685 xcb_pixmap_t buffer_id = xcb_generate_id(xcb_connection);
1686 xcb_pixmap_t statusline_buffer_id = xcb_generate_id(xcb_connection);
1687 mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP | XCB_CW_CURSOR;
1689 values[0] = colors.bar_bg.colorpixel;
1690 values[1] = root_screen->black_pixel;
1691 /* If hide_on_modifier is set to hide or invisible mode, i3 is not supposed to manage our bar windows */
1692 values[2] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1693 /* We enable the following EventMask fields:
1694 * EXPOSURE, to get expose events (we have to re-draw then)
1695 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1696 * child windows use ConfigureWindow
1697 * BUTTON_PRESS, to handle clicks on the workspace buttons
1699 values[3] = XCB_EVENT_MASK_EXPOSURE |
1700 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1701 XCB_EVENT_MASK_BUTTON_PRESS;
1702 if (config.hide_on_modifier == M_DOCK) {
1703 /* If the bar is normally visible, catch visibility change events to suspend
1704 * the status process when the bar is obscured by full-screened windows. */
1705 values[3] |= XCB_EVENT_MASK_VISIBILITY_CHANGE;
1706 walk->visible = true;
1708 values[4] = colormap;
1711 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1715 walk->rect.x, walk->rect.y + walk->rect.h - bar_height,
1716 walk->rect.w, bar_height,
1718 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1719 visual_type->visual_id,
1723 /* The double-buffer we use to render stuff off-screen */
1724 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1731 /* The double-buffer we use to render the statusline before copying to buffer */
1732 xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection,
1734 statusline_buffer_id,
1739 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1740 xcb_void_cookie_t class_cookie;
1741 class_cookie = xcb_change_property(xcb_connection,
1742 XCB_PROP_MODE_REPLACE,
1747 (strlen("i3bar") + 1) * 2,
1751 sasprintf(&name, "i3bar for output %s", walk->name);
1752 xcb_void_cookie_t name_cookie;
1753 name_cookie = xcb_change_property(xcb_connection,
1754 XCB_PROP_MODE_REPLACE,
1763 /* We want dock windows (for now). When override_redirect is set, i3 is ignoring
1765 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1766 XCB_PROP_MODE_REPLACE,
1768 atoms[_NET_WM_WINDOW_TYPE],
1772 (unsigned char *)&atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1774 draw_util_surface_init(xcb_connection, &walk->bar, bar_id, NULL, walk->rect.w, bar_height);
1775 draw_util_surface_init(xcb_connection, &walk->buffer, buffer_id, NULL, walk->rect.w, bar_height);
1776 draw_util_surface_init(xcb_connection, &walk->statusline_buffer, statusline_buffer_id, NULL, walk->rect.w, bar_height);
1778 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1780 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1781 xcb_void_cookie_t map_cookie;
1782 if (config.hide_on_modifier == M_DOCK) {
1783 map_cookie = xcb_map_window_checked(xcb_connection, bar_id);
1786 if (xcb_request_failed(win_cookie, "Could not create window") ||
1787 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1788 xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") ||
1789 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1790 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1791 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1792 xcb_request_failed(strut_cookie, "Could not set strut") ||
1793 ((config.hide_on_modifier == M_DOCK) && xcb_request_failed(map_cookie, "Could not map window"))) {
1797 /* Unless "tray_output none" was specified, we need to initialize the tray. */
1798 bool no_tray = false;
1799 if (!(TAILQ_EMPTY(&(config.tray_outputs)))) {
1800 no_tray = strcasecmp(TAILQ_FIRST(&(config.tray_outputs))->output, "none") == 0;
1804 * There are three scenarios in which we need to initialize the tray:
1805 * 1. A specific output was listed in tray_outputs which is also
1806 * in the list of outputs managed by this bar.
1807 * 2. No tray_output directive was specified. In this case, we
1808 * use the first available output.
1809 * 3. 'tray_output primary' was specified. In this case we use the
1812 * Three scenarios in which we specifically don't want to
1813 * initialize the tray are:
1814 * 1. 'tray_output none' was specified.
1815 * 2. A specific output was listed as a tray_output, but is not
1816 * one of the outputs managed by this bar. For example, consider
1817 * tray_outputs == [VGA-1], but outputs == [HDMI-1].
1818 * 3. 'tray_output primary' was specified and no output in the list
1821 if (!tray_configured && !no_tray) {
1822 /* If no tray_output was specified, we go ahead and initialize the tray as
1823 * we will be using the first available output. */
1824 if (TAILQ_EMPTY(&(config.tray_outputs))) {
1828 /* If one or more tray_output assignments were specified, we ensure that at least one of
1829 * them is actually an output managed by this instance. */
1830 tray_output_t *tray_output;
1831 TAILQ_FOREACH(tray_output, &(config.tray_outputs), tray_outputs) {
1834 SLIST_FOREACH(output, outputs, slist) {
1835 if (strcasecmp(output->name, tray_output->output) == 0 ||
1836 (strcasecmp(tray_output->output, "primary") == 0 && output->primary)) {
1847 tray_configured = true;
1850 /* We already have a bar, so we just reconfigure it */
1851 mask = XCB_CONFIG_WINDOW_X |
1852 XCB_CONFIG_WINDOW_Y |
1853 XCB_CONFIG_WINDOW_WIDTH |
1854 XCB_CONFIG_WINDOW_HEIGHT |
1855 XCB_CONFIG_WINDOW_STACK_MODE;
1856 values[0] = walk->rect.x;
1857 if (config.position == POS_TOP)
1858 values[1] = walk->rect.y;
1860 values[1] = walk->rect.y + walk->rect.h - bar_height;
1861 values[2] = walk->rect.w;
1862 values[3] = bar_height;
1863 values[4] = XCB_STACK_MODE_ABOVE;
1865 DLOG("Reconfiguring strut partial property for output %s\n", walk->name);
1866 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1868 DLOG("Destroying buffer for output %s\n", walk->name);
1869 xcb_free_pixmap(xcb_connection, walk->buffer.id);
1871 DLOG("Destroying statusline buffer for output %s\n", walk->name);
1872 xcb_free_pixmap(xcb_connection, walk->statusline_buffer.id);
1874 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1875 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1880 mask = XCB_CW_OVERRIDE_REDIRECT;
1881 values[0] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1882 DLOG("Changing window attribute override_redirect for output %s to %d\n", walk->name, values[0]);
1883 xcb_void_cookie_t chg_cookie = xcb_change_window_attributes(xcb_connection,
1888 DLOG("Recreating buffer for output %s\n", walk->name);
1889 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1896 DLOG("Recreating statusline buffer for output %s\n", walk->name);
1897 xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection,
1899 walk->statusline_buffer.id,
1904 draw_util_surface_free(xcb_connection, &(walk->bar));
1905 draw_util_surface_free(xcb_connection, &(walk->buffer));
1906 draw_util_surface_free(xcb_connection, &(walk->statusline_buffer));
1907 draw_util_surface_init(xcb_connection, &(walk->bar), walk->bar.id, NULL, walk->rect.w, bar_height);
1908 draw_util_surface_init(xcb_connection, &(walk->buffer), walk->buffer.id, NULL, walk->rect.w, bar_height);
1909 draw_util_surface_init(xcb_connection, &(walk->statusline_buffer), walk->statusline_buffer.id, NULL, walk->rect.w, bar_height);
1911 xcb_void_cookie_t map_cookie, umap_cookie;
1913 /* Unmap the window, and draw it again when in dock mode */
1914 umap_cookie = xcb_unmap_window_checked(xcb_connection, walk->bar.id);
1915 if (config.hide_on_modifier == M_DOCK) {
1917 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar.id);
1922 if (config.hide_on_modifier == M_HIDE) {
1923 /* Switching to hide mode, register for keyevents */
1924 register_xkb_keyevents();
1926 /* Switching to dock/invisible mode, deregister from keyevents */
1927 deregister_xkb_keyevents();
1931 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") ||
1932 xcb_request_failed(chg_cookie, "Could not change window") ||
1933 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1934 xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") ||
1935 xcb_request_failed(strut_cookie, "Could not set strut") ||
1936 (redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") ||
1937 (config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) {
1945 * Render the bars, with buttons and statusline
1948 void draw_bars(bool unhide) {
1949 DLOG("Drawing bars...\n");
1951 uint32_t full_statusline_width = predict_statusline_length(false);
1952 uint32_t short_statusline_width = predict_statusline_length(true);
1954 i3_output *outputs_walk;
1955 SLIST_FOREACH(outputs_walk, outputs, slist) {
1956 int workspace_width = 0;
1958 if (!outputs_walk->active) {
1959 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1962 if (outputs_walk->bar.id == XCB_NONE) {
1963 /* Oh shit, an active output without an own bar. Create it now! */
1964 reconfig_windows(false);
1967 bool use_focus_colors = output_has_focus(outputs_walk);
1969 /* First things first: clear the backbuffer */
1970 draw_util_clear_surface(&(outputs_walk->buffer), (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg));
1972 if (!config.disable_ws) {
1974 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1975 DLOG("Drawing button for WS %s at x = %d, len = %d\n",
1976 i3string_as_utf8(ws_walk->name), workspace_width, ws_walk->name_width);
1977 color_t fg_color = colors.inactive_ws_fg;
1978 color_t bg_color = colors.inactive_ws_bg;
1979 color_t border_color = colors.inactive_ws_border;
1980 if (ws_walk->visible) {
1981 if (!ws_walk->focused) {
1982 fg_color = colors.active_ws_fg;
1983 bg_color = colors.active_ws_bg;
1984 border_color = colors.active_ws_border;
1986 fg_color = colors.focus_ws_fg;
1987 bg_color = colors.focus_ws_bg;
1988 border_color = colors.focus_ws_border;
1991 if (ws_walk->urgent) {
1992 DLOG("WS %s is urgent!\n", i3string_as_utf8(ws_walk->name));
1993 fg_color = colors.urgent_ws_fg;
1994 bg_color = colors.urgent_ws_bg;
1995 border_color = colors.urgent_ws_border;
1999 /* Draw the border of the button. */
2000 draw_util_rectangle(&(outputs_walk->buffer), border_color,
2003 ws_walk->name_width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
2004 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1));
2006 /* Draw the inside of the button. */
2007 draw_util_rectangle(&(outputs_walk->buffer), bg_color,
2008 workspace_width + logical_px(1),
2010 ws_walk->name_width + 2 * logical_px(ws_hoff_px),
2011 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1));
2013 draw_util_text(ws_walk->name, &(outputs_walk->buffer), fg_color, bg_color,
2014 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
2015 logical_px(ws_voff_px),
2016 ws_walk->name_width);
2018 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
2019 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
2020 workspace_width += logical_px(ws_spacing_px);
2024 if (binding.name && !config.disable_binding_mode_indicator) {
2025 workspace_width += logical_px(ws_spacing_px);
2027 color_t fg_color = colors.binding_mode_fg;
2028 color_t bg_color = colors.binding_mode_bg;
2030 draw_util_rectangle(&(outputs_walk->buffer), colors.binding_mode_border,
2033 binding.width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
2034 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1));
2036 draw_util_rectangle(&(outputs_walk->buffer), bg_color,
2037 workspace_width + logical_px(1),
2039 binding.width + 2 * logical_px(ws_hoff_px),
2040 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1));
2042 draw_util_text(binding.name, &(outputs_walk->buffer), fg_color, bg_color,
2043 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
2044 logical_px(ws_voff_px),
2048 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + binding.width;
2051 if (!TAILQ_EMPTY(&statusline_head)) {
2052 DLOG("Printing statusline!\n");
2054 int tray_width = get_tray_width(outputs_walk->trayclients);
2055 uint32_t max_statusline_width = outputs_walk->rect.w - workspace_width - tray_width - 2 * logical_px(sb_hoff_px);
2056 uint32_t clip_left = 0;
2057 uint32_t statusline_width = full_statusline_width;
2058 bool use_short_text = false;
2060 if (statusline_width > max_statusline_width) {
2061 statusline_width = short_statusline_width;
2062 use_short_text = true;
2063 if (statusline_width > max_statusline_width) {
2064 clip_left = statusline_width - max_statusline_width;
2068 int16_t visible_statusline_width = MIN(statusline_width, max_statusline_width);
2069 int x_dest = outputs_walk->rect.w - tray_width - logical_px(sb_hoff_px) - visible_statusline_width;
2071 draw_statusline(outputs_walk, clip_left, use_focus_colors, use_short_text);
2072 draw_util_copy_surface(&outputs_walk->statusline_buffer, &outputs_walk->buffer, 0, 0,
2073 x_dest, 0, visible_statusline_width, (int16_t)bar_height);
2075 outputs_walk->statusline_width = statusline_width;
2076 outputs_walk->statusline_short_text = use_short_text;
2080 /* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */
2082 config.hidden_state == S_SHOW ||
2085 } else if (config.hide_on_modifier == M_HIDE) {
2093 * Redraw the bars, i.e. simply copy the buffer to the barwindow
2096 void redraw_bars(void) {
2097 i3_output *outputs_walk;
2098 SLIST_FOREACH(outputs_walk, outputs, slist) {
2099 if (!outputs_walk->active) {
2103 draw_util_copy_surface(&(outputs_walk->buffer), &(outputs_walk->bar), 0, 0,
2104 0, 0, outputs_walk->rect.w, outputs_walk->rect.h);
2105 xcb_flush(xcb_connection);
2110 * Set the current binding mode
2113 void set_current_mode(struct mode *current) {
2114 I3STRING_FREE(binding.name);
2116 activated_mode = binding.name != NULL;