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 */
89 /* The name of current binding mode */
92 /* Indicates whether a new binding mode was recently activated */
93 bool activated_mode = false;
95 /* The output in which the tray should be displayed. */
96 static i3_output *output_for_tray;
98 /* The parsed colors */
103 color_t focus_bar_fg;
104 color_t focus_bar_bg;
105 color_t focus_sep_fg;
106 color_t active_ws_fg;
107 color_t active_ws_bg;
108 color_t active_ws_border;
109 color_t inactive_ws_fg;
110 color_t inactive_ws_bg;
111 color_t inactive_ws_border;
112 color_t urgent_ws_bg;
113 color_t urgent_ws_fg;
114 color_t urgent_ws_border;
117 color_t focus_ws_border;
118 color_t binding_mode_bg;
119 color_t binding_mode_fg;
120 color_t binding_mode_border;
122 struct xcb_colors_t colors;
124 /* Horizontal offset between a workspace label and button borders */
125 static const int ws_hoff_px = 4;
127 /* Vertical offset between a workspace label and button borders */
128 static const int ws_voff_px = 3;
130 /* Offset between two workspace buttons */
131 static const int ws_spacing_px = 1;
133 /* Offset between the statusline and 1) workspace buttons on the left
134 * 2) the tray or screen edge on the right */
135 static const int sb_hoff_px = 4;
137 /* Additional offset between the tray and the statusline, if the tray is not empty */
138 static const int tray_loff_px = 2;
140 /* Vertical offset between the bar and a separator */
141 static const int sep_voff_px = 4;
143 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
144 xcb_generic_error_t *err;
145 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
146 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
147 return err->error_code;
152 static uint32_t get_sep_offset(struct status_block *block) {
153 if (!block->no_separator && block->sep_block_width > 0)
154 return block->sep_block_width / 2 + block->sep_block_width % 2;
158 static int get_tray_width(struct tc_head *trayclients) {
159 trayclient *trayclient;
161 TAILQ_FOREACH_REVERSE(trayclient, trayclients, tc_head, tailq) {
162 if (!trayclient->mapped)
164 tray_width += icon_size + logical_px(config.tray_padding);
167 tray_width += logical_px(tray_loff_px);
172 * Draws a separator for the given block if necessary.
175 static void draw_separator(i3_output *output, uint32_t x, struct status_block *block, bool use_focus_colors) {
176 color_t sep_fg = (use_focus_colors ? colors.focus_sep_fg : colors.sep_fg);
177 color_t bar_bg = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg);
179 uint32_t sep_offset = get_sep_offset(block);
180 if (TAILQ_NEXT(block, blocks) == NULL || sep_offset == 0)
183 uint32_t center_x = x - sep_offset;
184 if (config.separator_symbol == NULL) {
185 /* Draw a classic one pixel, vertical separator. */
186 draw_util_rectangle(&output->statusline_buffer, sep_fg,
188 logical_px(sep_voff_px),
190 bar_height - 2 * logical_px(sep_voff_px));
192 /* Draw a custom separator. */
193 uint32_t separator_x = MAX(x - block->sep_block_width, center_x - separator_symbol_width / 2);
194 draw_util_text(config.separator_symbol, &output->statusline_buffer, sep_fg, bar_bg,
195 separator_x, logical_px(ws_voff_px), x - separator_x);
199 static uint32_t predict_statusline_length(bool use_short_text) {
201 struct status_block *block;
203 TAILQ_FOREACH(block, &statusline_head, blocks) {
204 i3String *text = block->full_text;
205 struct status_block_render_desc *render = &block->full_render;
206 if (use_short_text && block->short_text != NULL) {
207 text = block->short_text;
208 render = &block->short_render;
211 if (i3string_get_num_bytes(text) == 0)
214 render->width = predict_text_width(text);
216 render->width += logical_px(2);
218 /* Compute offset and append for text aligment in min_width. */
219 if (block->min_width <= render->width) {
220 render->x_offset = 0;
221 render->x_append = 0;
223 uint32_t padding_width = block->min_width - render->width;
224 switch (block->align) {
226 render->x_append = padding_width;
229 render->x_offset = padding_width;
232 render->x_offset = padding_width / 2;
233 render->x_append = padding_width / 2 + padding_width % 2;
238 width += render->width + render->x_offset + render->x_append;
240 /* If this is not the last block, add some pixels for a separator. */
241 if (TAILQ_NEXT(block, blocks) != NULL)
242 width += block->sep_block_width;
249 * Redraws the statusline to the output's statusline_buffer
251 static void draw_statusline(i3_output *output, uint32_t clip_left, bool use_focus_colors, bool use_short_text) {
252 struct status_block *block;
254 color_t bar_color = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg);
255 draw_util_clear_surface(&output->statusline_buffer, bar_color);
257 /* Use unsigned integer wraparound to clip off the left side.
258 * For example, if clip_left is 75, then x will start at the very large
259 * number INT_MAX-75, which is way outside the surface dimensions. Drawing
260 * to that x position is a no-op which XCB and Cairo safely ignore. Once x moves
261 * up by 75 and goes past INT_MAX, it will wrap around again to 0, and we start
262 * actually rendering content to the surface. */
263 uint32_t x = 0 - clip_left;
265 /* Draw the text of each block */
266 TAILQ_FOREACH(block, &statusline_head, blocks) {
267 i3String *text = block->full_text;
268 struct status_block_render_desc *render = &block->full_render;
269 if (use_short_text && block->short_text != NULL) {
270 text = block->short_text;
271 render = &block->short_render;
274 if (i3string_get_num_bytes(text) == 0)
279 fg_color = colors.urgent_ws_fg;
280 } else if (block->color) {
281 fg_color = draw_util_hex_to_color(block->color);
282 } else if (use_focus_colors) {
283 fg_color = colors.focus_bar_fg;
285 fg_color = colors.bar_fg;
288 color_t bg_color = bar_color;
290 int border_width = (block->border) ? logical_px(1) : 0;
291 int full_render_width = render->width + render->x_offset + render->x_append;
292 if (block->border || block->background || block->urgent) {
293 /* Let's determine the colors first. */
294 color_t border_color = bar_color;
296 border_color = colors.urgent_ws_border;
297 bg_color = colors.urgent_ws_bg;
300 border_color = draw_util_hex_to_color(block->border);
301 if (block->background)
302 bg_color = draw_util_hex_to_color(block->background);
305 /* Draw the border. */
306 draw_util_rectangle(&output->statusline_buffer, border_color,
309 bar_height - logical_px(2));
311 /* Draw the background. */
312 draw_util_rectangle(&output->statusline_buffer, bg_color,
314 logical_px(1) + border_width,
315 full_render_width - 2 * border_width,
316 bar_height - 2 * border_width - logical_px(2));
319 draw_util_text(text, &output->statusline_buffer, fg_color, bg_color,
320 x + render->x_offset + border_width, logical_px(ws_voff_px),
321 render->width - 2 * border_width);
322 x += full_render_width;
324 /* If this is not the last block, draw a separator. */
325 if (TAILQ_NEXT(block, blocks) != NULL) {
326 x += block->sep_block_width;
327 draw_separator(output, x, block, use_focus_colors);
333 * Hides all bars (unmaps them)
336 static void hide_bars(void) {
337 if ((config.hide_on_modifier == M_DOCK) || (config.hidden_state == S_SHOW && config.hide_on_modifier == M_HIDE)) {
342 SLIST_FOREACH(walk, outputs, slist) {
346 xcb_unmap_window(xcb_connection, walk->bar.id);
352 * Unhides all bars (maps them)
355 static void unhide_bars(void) {
356 if (config.hide_on_modifier != M_HIDE) {
361 xcb_void_cookie_t cookie;
367 SLIST_FOREACH(walk, outputs, slist) {
368 if (walk->bar.id == XCB_NONE) {
371 mask = XCB_CONFIG_WINDOW_X |
372 XCB_CONFIG_WINDOW_Y |
373 XCB_CONFIG_WINDOW_WIDTH |
374 XCB_CONFIG_WINDOW_HEIGHT |
375 XCB_CONFIG_WINDOW_STACK_MODE;
376 values[0] = walk->rect.x;
377 if (config.position == POS_TOP)
378 values[1] = walk->rect.y;
380 values[1] = walk->rect.y + walk->rect.h - bar_height;
381 values[2] = walk->rect.w;
382 values[3] = bar_height;
383 values[4] = XCB_STACK_MODE_ABOVE;
384 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
385 cookie = xcb_configure_window_checked(xcb_connection,
390 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
393 xcb_map_window(xcb_connection, walk->bar.id);
398 * Parse the colors into a format that we can use
401 void init_colors(const struct xcb_color_strings_t *new_colors) {
402 #define PARSE_COLOR(name, def) \
404 colors.name = draw_util_hex_to_color(new_colors->name ? new_colors->name : def); \
406 PARSE_COLOR(bar_fg, "#FFFFFF");
407 PARSE_COLOR(bar_bg, "#000000");
408 PARSE_COLOR(sep_fg, "#666666");
409 PARSE_COLOR(active_ws_fg, "#FFFFFF");
410 PARSE_COLOR(active_ws_bg, "#333333");
411 PARSE_COLOR(active_ws_border, "#333333");
412 PARSE_COLOR(inactive_ws_fg, "#888888");
413 PARSE_COLOR(inactive_ws_bg, "#222222");
414 PARSE_COLOR(inactive_ws_border, "#333333");
415 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
416 PARSE_COLOR(urgent_ws_bg, "#900000");
417 PARSE_COLOR(urgent_ws_border, "#2f343a");
418 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
419 PARSE_COLOR(focus_ws_bg, "#285577");
420 PARSE_COLOR(focus_ws_border, "#4c7899");
423 #define PARSE_COLOR_FALLBACK(name, fallback) \
425 colors.name = new_colors->name ? draw_util_hex_to_color(new_colors->name) : colors.fallback; \
428 /* For the binding mode indicator colors, we don't hardcode a default.
429 * Instead, we fall back to urgent_ws_* colors. */
430 PARSE_COLOR_FALLBACK(binding_mode_fg, urgent_ws_fg);
431 PARSE_COLOR_FALLBACK(binding_mode_bg, urgent_ws_bg);
432 PARSE_COLOR_FALLBACK(binding_mode_border, urgent_ws_border);
434 /* Similarly, for unspecified focused bar colors, we fall back to the
435 * regular bar colors. */
436 PARSE_COLOR_FALLBACK(focus_bar_fg, bar_fg);
437 PARSE_COLOR_FALLBACK(focus_bar_bg, bar_bg);
438 PARSE_COLOR_FALLBACK(focus_sep_fg, sep_fg);
439 #undef PARSE_COLOR_FALLBACK
442 xcb_flush(xcb_connection);
445 static bool execute_custom_command(xcb_keycode_t input_code, bool event_is_release) {
447 TAILQ_FOREACH(binding, &(config.bindings), bindings) {
448 if ((binding->input_code != input_code) || (binding->release != event_is_release))
451 i3_send_msg(I3_IPC_MESSAGE_TYPE_RUN_COMMAND, binding->command);
458 * Handle a button press event (i.e. a mouse click on one of our bars).
459 * We determine, whether the click occurred on a workspace button or if the scroll-
460 * wheel was used and change the workspace appropriately
463 static void handle_button(xcb_button_press_event_t *event) {
464 /* Determine, which bar was clicked */
466 xcb_window_t bar = event->event;
467 SLIST_FOREACH(walk, outputs, slist) {
468 if (walk->bar.id == bar) {
474 DLOG("Unknown bar clicked!\n");
478 DLOG("Got button %d\n", event->detail);
480 /* During button release events, only check for custom commands. */
481 const bool event_is_release = (event->response_type & ~0x80) == XCB_BUTTON_RELEASE;
482 if (event_is_release) {
483 execute_custom_command(event->detail, event_is_release);
487 int32_t x = event->event_x >= 0 ? event->event_x : 0;
488 int workspace_width = 0;
489 i3_ws *cur_ws = NULL, *clicked_ws = NULL, *ws_walk;
491 TAILQ_FOREACH(ws_walk, walk->workspaces, tailq) {
492 int w = 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
493 if (x >= workspace_width && x <= workspace_width + w)
494 clicked_ws = ws_walk;
495 if (ws_walk->visible)
497 workspace_width += w;
498 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
499 workspace_width += logical_px(ws_spacing_px);
502 if (x > workspace_width && child_want_click_events()) {
503 /* If the child asked for click events,
504 * check if a status block has been clicked. */
505 int tray_width = get_tray_width(walk->trayclients);
506 int last_block_x = 0;
507 int offset = walk->rect.w - walk->statusline_width - tray_width - logical_px((tray_width > 0) * sb_hoff_px);
508 int32_t statusline_x = x - offset;
510 if (statusline_x >= 0 && statusline_x < walk->statusline_width) {
511 struct status_block *block;
513 TAILQ_FOREACH(block, &statusline_head, blocks) {
514 i3String *text = block->full_text;
515 struct status_block_render_desc *render = &block->full_render;
516 if (walk->statusline_short_text && block->short_text != NULL) {
517 text = block->short_text;
518 render = &block->short_render;
521 if (i3string_get_num_bytes(text) == 0)
524 const uint32_t full_render_width = render->width + render->x_offset + render->x_append;
525 const int relative_x = statusline_x - last_block_x;
526 if (relative_x >= 0 && (uint32_t)relative_x <= full_render_width) {
527 send_block_clicked(event->detail, block->name, block->instance,
528 event->root_x, event->root_y, relative_x, event->event_y, full_render_width, bar_height,
533 last_block_x += full_render_width + block->sep_block_width;
538 /* If a custom command was specified for this mouse button, it overrides
539 * the default behavior. */
540 if (execute_custom_command(event->detail, event_is_release)) {
544 if (cur_ws == NULL) {
545 DLOG("No workspace active?\n");
548 switch (event->detail) {
549 case XCB_BUTTON_SCROLL_UP:
550 case XCB_BUTTON_SCROLL_LEFT:
551 /* Mouse wheel up. We select the previous ws, if any.
552 * If there is no more workspace, don’t even send the workspace
553 * command, otherwise (with workspace auto_back_and_forth) we’d end
554 * up on the wrong workspace. */
555 if (cur_ws == TAILQ_FIRST(walk->workspaces))
558 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
560 case XCB_BUTTON_SCROLL_DOWN:
561 case XCB_BUTTON_SCROLL_RIGHT:
562 /* Mouse wheel down. We select the next ws, if any.
563 * If there is no more workspace, don’t even send the workspace
564 * command, otherwise (with workspace auto_back_and_forth) we’d end
565 * up on the wrong workspace. */
566 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
569 cur_ws = TAILQ_NEXT(cur_ws, tailq);
574 /* if no workspace was clicked, focus our currently visible
575 * workspace if it is not already focused */
576 if (cur_ws == NULL) {
577 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
578 if (cur_ws->visible && !cur_ws->focused)
583 /* if there is nothing to focus, we are done */
592 /* To properly handle workspace names with double quotes in them, we need
593 * to escape the double quotes. Unfortunately, that’s rather ugly in C: We
594 * first count the number of double quotes, then we allocate a large enough
595 * buffer, then we copy character by character. */
598 const char *utf8_name = cur_ws->canonical_name;
599 for (const char *walk = utf8_name; *walk != '\0'; walk++) {
600 if (*walk == '"' || *walk == '\\')
602 /* While we’re looping through the name anyway, we can save one
607 const size_t len = namelen + strlen("workspace \"\"") + 1;
608 char *buffer = scalloc(len + num_quotes, 1);
609 memcpy(buffer, "workspace \"", strlen("workspace \""));
610 size_t inpos, outpos;
611 for (inpos = 0, outpos = strlen("workspace \"");
614 if (utf8_name[inpos] == '"' || utf8_name[inpos] == '\\') {
615 buffer[outpos] = '\\';
618 buffer[outpos] = utf8_name[inpos];
620 buffer[outpos] = '"';
621 i3_send_msg(I3_IPC_MESSAGE_TYPE_RUN_COMMAND, buffer);
626 * Handle visibility notifications: when none of the bars are visible, e.g.
627 * if windows are in fullscreen on each output, suspend the child process.
630 static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
631 bool visible = (event->state != XCB_VISIBILITY_FULLY_OBSCURED);
635 SLIST_FOREACH(output, outputs, slist) {
636 if (!output->active) {
639 if (output->bar.id == event->window) {
640 if (output->visible == visible) {
643 output->visible = visible;
645 num_visible += output->visible;
648 if (num_visible == 0) {
650 } else if (num_visible == visible) {
651 /* Wake the child only when transitioning from 0 to 1 visible bar.
652 * We cannot transition from 0 to 2 or more visible bars at once since
653 * visibility events are delivered to each window separately */
659 * Adjusts the size of the tray window and alignment of the tray clients by
660 * configuring their respective x coordinates. To be called when mapping or
661 * unmapping a tray client window.
664 static void configure_trayclients(void) {
665 trayclient *trayclient;
667 SLIST_FOREACH(output, outputs, slist) {
672 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
673 if (!trayclient->mapped)
677 DLOG("Configuring tray window %08x to x=%d\n",
678 trayclient->win, output->rect.w - (clients * (icon_size + logical_px(config.tray_padding))));
679 uint32_t x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
680 xcb_configure_window(xcb_connection,
689 * Handles ClientMessages (messages sent from another client directly to us).
691 * At the moment, only the tray window will receive client messages. All
692 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
695 static void handle_client_message(xcb_client_message_event_t *event) {
696 if (event->type == atoms[I3_SYNC]) {
697 xcb_window_t window = event->data.data32[0];
698 uint32_t rnd = event->data.data32[1];
699 /* Forward the request to i3 via the IPC interface so that all pending
700 * IPC messages are guaranteed to be handled. */
701 char *payload = NULL;
702 sasprintf(&payload, "{\"rnd\":%d, \"window\":%d}", rnd, window);
703 i3_send_msg(I3_IPC_MESSAGE_TYPE_SYNC, payload);
705 } else if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
706 event->format == 32) {
707 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
708 /* event->data.data32[0] is the timestamp */
709 uint32_t op = event->data.data32[1];
712 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
713 xcb_window_t client = event->data.data32[2];
715 mask = XCB_CW_EVENT_MASK;
717 /* Needed to get the most recent value of XEMBED_MAPPED. */
718 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
719 /* Needed for UnmapNotify events. */
720 values[0] |= XCB_EVENT_MASK_STRUCTURE_NOTIFY;
721 /* Needed because some tray applications (e.g., VLC) use
722 * override_redirect which causes no ConfigureRequest to be sent. */
723 values[0] |= XCB_EVENT_MASK_RESIZE_REDIRECT;
725 xcb_change_window_attributes(xcb_connection, client, mask, values);
727 /* Request the _XEMBED_INFO property. The XEMBED specification
728 * (which is referred by the tray specification) says this *has* to
729 * be set, but VLC does not set it… */
732 xcb_get_property_cookie_t xembedc;
733 xcb_generic_error_t *error;
734 xembedc = xcb_get_property(xcb_connection,
738 XCB_GET_PROPERTY_TYPE_ANY,
742 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
746 ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
751 if (xembedr != NULL && xembedr->length != 0) {
752 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
753 uint32_t *xembed = xcb_get_property_value(xembedr);
754 DLOG("xembed version = %d\n", xembed[0]);
755 DLOG("xembed flags = %d\n", xembed[1]);
756 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
757 xe_version = xembed[0];
762 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
765 DLOG("X window %08x requested docking\n", client);
767 if (output_for_tray == NULL) {
768 ELOG("No output found for tray\n");
772 xcb_void_cookie_t rcookie = xcb_reparent_window(xcb_connection,
774 output_for_tray->bar.id,
775 output_for_tray->rect.w - icon_size - logical_px(config.tray_padding),
776 logical_px(config.tray_padding));
777 if (xcb_request_failed(rcookie, "Could not reparent window. Maybe it is using an incorrect depth/visual?"))
780 /* We reconfigure the window to use a reasonable size. The systray
781 * specification explicitly says:
782 * Tray icons may be assigned any size by the system tray, and
783 * should do their best to cope with any size effectively
785 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
786 values[0] = icon_size;
787 values[1] = icon_size;
788 xcb_configure_window(xcb_connection,
793 /* send the XEMBED_EMBEDDED_NOTIFY message */
794 void *event = scalloc(32, 1);
795 xcb_client_message_event_t *ev = event;
796 ev->response_type = XCB_CLIENT_MESSAGE;
798 ev->type = atoms[_XEMBED];
800 ev->data.data32[0] = XCB_CURRENT_TIME;
801 ev->data.data32[1] = XEMBED_EMBEDDED_NOTIFY;
802 ev->data.data32[2] = output_for_tray->bar.id;
803 ev->data.data32[3] = xe_version;
804 xcb_send_event(xcb_connection,
807 XCB_EVENT_MASK_NO_EVENT,
811 /* Put the client inside the save set. Upon termination (whether
812 * killed or normal exit does not matter) of i3bar, these clients
813 * will be correctly reparented to their most closest living
814 * ancestor. Without this, tray icons might die when i3bar
816 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
818 trayclient *tc = smalloc(sizeof(trayclient));
820 tc->xe_version = xe_version;
822 TAILQ_INSERT_TAIL(output_for_tray->trayclients, tc, tailq);
825 DLOG("Mapping dock client\n");
826 xcb_map_window(xcb_connection, client);
828 DLOG("Not mapping dock client yet\n");
830 /* Trigger an update to copy the statusline text to the appropriate
832 configure_trayclients();
839 * Handles DestroyNotify events by removing the tray client from the data
840 * structure. According to the XEmbed protocol, this is one way for a tray
841 * client to finish the protocol. After this event is received, there is no
842 * further interaction with the tray client.
844 * See: https://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
847 static void handle_destroy_notify(xcb_destroy_notify_event_t *event) {
848 DLOG("DestroyNotify for window = %08x, event = %08x\n", event->window, event->event);
851 SLIST_FOREACH(walk, outputs, slist) {
854 DLOG("checking output %s\n", walk->name);
855 trayclient *trayclient;
856 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
857 if (trayclient->win != event->window) {
861 DLOG("Removing tray client with window ID %08x\n", event->window);
862 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
865 /* Trigger an update, we now have more space for the statusline */
866 configure_trayclients();
874 * Handles MapNotify events. These events happen when a tray client shows its
875 * window. We respond by realigning the tray clients.
878 static void handle_map_notify(xcb_map_notify_event_t *event) {
879 DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event);
882 SLIST_FOREACH(walk, outputs, slist) {
885 DLOG("checking output %s\n", walk->name);
886 trayclient *trayclient;
887 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
888 if (trayclient->win != event->window)
891 DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window);
892 trayclient->mapped = true;
894 /* Trigger an update, we now have more space for the statusline */
895 configure_trayclients();
902 * Handles UnmapNotify events. These events happen when a tray client hides its
903 * window. We respond by realigning the tray clients.
906 static void handle_unmap_notify(xcb_unmap_notify_event_t *event) {
907 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
910 SLIST_FOREACH(walk, outputs, slist) {
913 DLOG("checking output %s\n", walk->name);
914 trayclient *trayclient;
915 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
916 if (trayclient->win != event->window)
919 DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window);
920 trayclient->mapped = false;
922 /* Trigger an update, we now have more space for the statusline */
923 configure_trayclients();
931 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
932 * handled, which tells us whether a dock client should be mapped or unmapped.
935 static void handle_property_notify(xcb_property_notify_event_t *event) {
936 DLOG("PropertyNotify\n");
937 if (event->atom == atoms[_XEMBED_INFO] &&
938 event->state == XCB_PROPERTY_NEW_VALUE) {
939 DLOG("xembed_info updated\n");
940 trayclient *trayclient = NULL, *walk;
942 SLIST_FOREACH(o_walk, outputs, slist) {
946 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
947 if (walk->win != event->window)
957 ELOG("PropertyNotify received for unknown window %08x\n",
961 xcb_get_property_cookie_t xembedc;
962 xembedc = xcb_get_property_unchecked(xcb_connection,
966 XCB_GET_PROPERTY_TYPE_ANY,
970 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
973 if (xembedr == NULL || xembedr->length == 0) {
974 DLOG("xembed_info unset\n");
978 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
979 uint32_t *xembed = xcb_get_property_value(xembedr);
980 DLOG("xembed version = %d\n", xembed[0]);
981 DLOG("xembed flags = %d\n", xembed[1]);
982 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
983 DLOG("map state now %d\n", map_it);
984 if (trayclient->mapped && !map_it) {
985 /* need to unmap the window */
986 xcb_unmap_window(xcb_connection, trayclient->win);
987 } else if (!trayclient->mapped && map_it) {
988 /* need to map the window */
989 xcb_map_window(xcb_connection, trayclient->win);
996 * If a tray client attempts to change its size we deny the request and respond
997 * by telling it its actual size.
1000 static void handle_configuration_change(xcb_window_t window) {
1001 trayclient *trayclient;
1003 SLIST_FOREACH(output, outputs, slist) {
1004 if (!output->active)
1008 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
1009 if (!trayclient->mapped)
1013 if (trayclient->win != window)
1016 xcb_rectangle_t rect;
1017 rect.x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
1018 rect.y = logical_px(config.tray_padding);
1019 rect.width = icon_size;
1020 rect.height = icon_size;
1022 DLOG("This is a tray window. x = %d\n", rect.x);
1023 fake_configure_notify(xcb_connection, rect, window, 0);
1028 DLOG("WARNING: Could not find corresponding tray window.\n");
1031 static void handle_configure_request(xcb_configure_request_event_t *event) {
1032 DLOG("ConfigureRequest for window = %08x\n", event->window);
1033 handle_configuration_change(event->window);
1036 static void handle_resize_request(xcb_resize_request_event_t *event) {
1037 DLOG("ResizeRequest for window = %08x\n", event->window);
1038 handle_configuration_change(event->window);
1042 * This function is called immediately before the main loop locks. We check for
1043 * events from X11, handle them, then flush our outgoing queue.
1046 static void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
1047 xcb_generic_event_t *event;
1049 if (xcb_connection_has_error(xcb_connection)) {
1050 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
1051 #ifdef I3_ASAN_ENABLED
1052 __lsan_do_leak_check();
1057 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
1058 if (event->response_type == 0) {
1059 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
1060 DLOG("Received X11 error, sequence 0x%x, error_code = %d\n", error->sequence, error->error_code);
1065 int type = (event->response_type & ~0x80);
1067 if (type == xkb_base && xkb_base > -1) {
1068 DLOG("received an xkb event\n");
1070 xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event;
1071 const uint32_t mod = (config.modifier & 0xFFFF);
1072 const bool new_mod_pressed = (mod != 0 && (state->mods & mod) == mod);
1073 if (new_mod_pressed != mod_pressed) {
1074 mod_pressed = new_mod_pressed;
1075 if (state->xkbType == XCB_XKB_STATE_NOTIFY && config.modifier != XCB_NONE) {
1077 activated_mode = false;
1079 } else if (!activated_mode) {
1090 case XCB_VISIBILITY_NOTIFY:
1091 /* Visibility change: a bar is [un]obscured by other window */
1092 handle_visibility_notify((xcb_visibility_notify_event_t *)event);
1095 if (((xcb_expose_event_t *)event)->count == 0) {
1096 /* Expose-events happen, when the window needs to be redrawn */
1101 case XCB_BUTTON_RELEASE:
1102 case XCB_BUTTON_PRESS:
1103 /* Button press events are mouse buttons clicked on one of our bars */
1104 handle_button((xcb_button_press_event_t *)event);
1106 case XCB_CLIENT_MESSAGE:
1107 /* Client messages are used for client-to-client communication, for
1108 * example system tray widgets talk to us directly via client messages. */
1109 handle_client_message((xcb_client_message_event_t *)event);
1111 case XCB_DESTROY_NOTIFY:
1112 /* DestroyNotify signifies the end of the XEmbed protocol */
1113 handle_destroy_notify((xcb_destroy_notify_event_t *)event);
1115 case XCB_UNMAP_NOTIFY:
1116 /* UnmapNotify is received when a tray client hides its window. */
1117 handle_unmap_notify((xcb_unmap_notify_event_t *)event);
1119 case XCB_MAP_NOTIFY:
1120 handle_map_notify((xcb_map_notify_event_t *)event);
1122 case XCB_PROPERTY_NOTIFY:
1123 /* PropertyNotify */
1124 handle_property_notify((xcb_property_notify_event_t *)event);
1126 case XCB_CONFIGURE_REQUEST:
1127 /* ConfigureRequest, sent by a tray child */
1128 handle_configure_request((xcb_configure_request_event_t *)event);
1130 case XCB_RESIZE_REQUEST:
1131 /* ResizeRequest sent by a tray child using override_redirect. */
1132 handle_resize_request((xcb_resize_request_event_t *)event);
1138 xcb_flush(xcb_connection);
1142 * Dummy callback. We only need this, so that the prepare and check watchers
1146 static void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
1150 * Early initialization of the connection to X11: Everything which does not
1151 * depend on 'config'.
1154 char *init_xcb_early(void) {
1155 /* FIXME: xcb_connect leaks memory */
1156 xcb_connection = xcb_connect(NULL, &screen);
1157 if (xcb_connection_has_error(xcb_connection)) {
1158 ELOG("Cannot open display\n");
1161 conn = xcb_connection;
1162 DLOG("Connected to xcb\n");
1164 /* We have to request the atoms we need */
1165 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
1166 #include "xcb_atoms.def"
1168 root_screen = xcb_aux_get_screen(xcb_connection, screen);
1169 xcb_root = root_screen->root;
1171 depth = root_screen->root_depth;
1172 colormap = root_screen->default_colormap;
1173 visual_type = get_visualtype(root_screen);
1175 xcb_cursor_context_t *cursor_ctx;
1176 if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) == 0) {
1177 cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr");
1178 xcb_cursor_context_free(cursor_ctx);
1180 cursor = xcb_generate_id(xcb_connection);
1181 i3Font cursor_font = load_font("cursor", false);
1182 xcb_create_glyph_cursor(
1185 cursor_font.specific.xcb.id,
1186 cursor_font.specific.xcb.id,
1187 XCB_CURSOR_LEFT_PTR,
1188 XCB_CURSOR_LEFT_PTR + 1,
1190 65535, 65535, 65535);
1193 /* The various watchers to communicate with xcb */
1194 xcb_io = smalloc(sizeof(ev_io));
1195 xcb_prep = smalloc(sizeof(ev_prepare));
1197 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
1198 ev_prepare_init(xcb_prep, &xcb_prep_cb);
1200 ev_io_start(main_loop, xcb_io);
1201 ev_prepare_start(main_loop, xcb_prep);
1203 /* Now we get the atoms and save them in a nice data structure */
1206 char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen);
1212 * Register for xkb keyevents. To grab modifiers without blocking other applications from receiving key events
1213 * involving that modifier, we sadly have to use xkb which is not yet fully supported
1217 static void register_xkb_keyevents(void) {
1218 const xcb_query_extension_reply_t *extreply;
1219 extreply = xcb_get_extension_data(conn, &xcb_xkb_id);
1220 if (!extreply->present) {
1221 ELOG("xkb is not present on this server\n");
1224 DLOG("initializing xcb-xkb\n");
1225 xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
1226 xcb_xkb_select_events(conn,
1227 XCB_XKB_ID_USE_CORE_KBD,
1228 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1230 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1234 xkb_base = extreply->first_event;
1238 * Deregister from xkb keyevents.
1241 static void deregister_xkb_keyevents(void) {
1242 xcb_xkb_select_events(conn,
1243 XCB_XKB_ID_USE_CORE_KBD,
1253 * Initialization which depends on 'config' being usable. Called after the
1254 * configuration has arrived.
1257 void init_xcb_late(char *fontname) {
1258 if (fontname == NULL)
1259 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
1262 font = load_font(fontname, true);
1264 DLOG("Calculated font height: %d\n", font.height);
1265 bar_height = font.height + 2 * logical_px(ws_voff_px);
1266 icon_size = bar_height - 2 * logical_px(config.tray_padding);
1268 if (config.separator_symbol)
1269 separator_symbol_width = predict_text_width(config.separator_symbol);
1271 xcb_flush(xcb_connection);
1273 if (config.hide_on_modifier == M_HIDE)
1274 register_xkb_keyevents();
1278 * Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the
1282 static void send_tray_clientmessage(void) {
1283 uint8_t buffer[32] = {0};
1284 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)buffer;
1286 ev->response_type = XCB_CLIENT_MESSAGE;
1287 ev->window = xcb_root;
1288 ev->type = atoms[MANAGER];
1290 ev->data.data32[0] = XCB_CURRENT_TIME;
1291 ev->data.data32[1] = tray_reply->atom;
1292 ev->data.data32[2] = selwin;
1294 xcb_send_event(xcb_connection,
1302 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
1303 * for the X11 display we are running on, then acquiring the selection for this
1304 * atom. Afterwards, tray clients will send ClientMessages to our window.
1307 static void init_tray(void) {
1308 DLOG("Initializing system tray functionality\n");
1309 /* request the tray manager atom for the X11 display we are running on */
1310 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
1311 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
1312 xcb_intern_atom_cookie_t tray_cookie;
1313 if (tray_reply == NULL)
1314 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
1316 /* tray support: we need a window to own the selection */
1317 selwin = xcb_generate_id(xcb_connection);
1318 uint32_t selmask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_COLORMAP;
1319 uint32_t selval[] = {root_screen->black_pixel, root_screen->black_pixel, 1, colormap};
1320 xcb_create_window(xcb_connection,
1327 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1328 visual_type->visual_id,
1332 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
1334 xcb_change_property(xcb_connection,
1335 XCB_PROP_MODE_REPLACE,
1337 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1342 xcb_change_property(xcb_connection,
1343 XCB_PROP_MODE_REPLACE,
1345 atoms[_NET_SYSTEM_TRAY_VISUAL],
1349 &visual_type->visual_id);
1353 if (tray_reply == NULL) {
1354 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1355 ELOG("Could not get atom %s\n", atomname);
1360 xcb_set_selection_owner(xcb_connection,
1365 /* Verify that we have the selection */
1366 xcb_get_selection_owner_cookie_t selcookie;
1367 xcb_get_selection_owner_reply_t *selreply;
1369 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1370 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1371 ELOG("Could not get selection owner for %s\n", atomname);
1375 if (selreply->owner != selwin) {
1376 ELOG("Could not set the %s selection. "
1377 "Maybe another tray is already running?\n",
1379 /* NOTE that this error is not fatal. We just can’t provide tray
1387 send_tray_clientmessage();
1391 * We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window
1392 * to make GTK+ 3 applets with symbolic icons visible. If the colors are unset,
1393 * they assume a light background.
1394 * See also https://bugzilla.gnome.org/show_bug.cgi?id=679591
1397 void init_tray_colors(void) {
1398 /* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */
1399 const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF");
1401 DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg);
1403 char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'},
1404 {bar_fg[3], bar_fg[4], '\0'},
1405 {bar_fg[5], bar_fg[6], '\0'}};
1406 const uint8_t r = strtol(strgroups[0], NULL, 16);
1407 const uint8_t g = strtol(strgroups[1], NULL, 16);
1408 const uint8_t b = strtol(strgroups[2], NULL, 16);
1410 const uint16_t r16 = ((uint16_t)r << 8) | r;
1411 const uint16_t g16 = ((uint16_t)g << 8) | g;
1412 const uint16_t b16 = ((uint16_t)b << 8) | b;
1414 const uint32_t tray_colors[12] = {
1415 r16, g16, b16, /* foreground color */
1416 r16, g16, b16, /* error color */
1417 r16, g16, b16, /* warning color */
1418 r16, g16, b16, /* success color */
1421 xcb_change_property(xcb_connection,
1422 XCB_PROP_MODE_REPLACE,
1424 atoms[_NET_SYSTEM_TRAY_COLORS],
1432 * Cleanup the xcb stuff.
1433 * Called once, before the program terminates.
1436 void clean_xcb(void) {
1441 xcb_free_cursor(xcb_connection, cursor);
1442 xcb_flush(xcb_connection);
1443 xcb_aux_sync(xcb_connection);
1444 xcb_disconnect(xcb_connection);
1446 ev_prepare_stop(main_loop, xcb_prep);
1447 ev_io_stop(main_loop, xcb_io);
1454 * Get the earlier requested atoms and save them in the prepared data structure
1457 void get_atoms(void) {
1458 xcb_intern_atom_reply_t *reply;
1459 #define ATOM_DO(name) \
1460 reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1461 if (reply == NULL) { \
1462 ELOG("Could not get atom %s\n", #name); \
1463 exit(EXIT_FAILURE); \
1465 atoms[name] = reply->atom; \
1468 #include "xcb_atoms.def"
1469 DLOG("Got atoms\n");
1473 * Reparents all tray clients of the specified output to the root window. This
1474 * is either used when shutting down, when an output appears (xrandr --output
1475 * VGA1 --off) or when the primary output changes.
1477 * Applications using the tray will start the protocol from the beginning again
1481 void kick_tray_clients(i3_output *output) {
1482 if (TAILQ_EMPTY(output->trayclients))
1485 trayclient *trayclient;
1486 while (!TAILQ_EMPTY(output->trayclients)) {
1487 trayclient = TAILQ_FIRST(output->trayclients);
1488 /* Unmap, then reparent (to root) the tray client windows */
1489 xcb_unmap_window(xcb_connection, trayclient->win);
1490 xcb_reparent_window(xcb_connection,
1496 /* We remove the trayclient right here. We might receive an UnmapNotify
1497 * event afterwards, but better safe than sorry. */
1498 TAILQ_REMOVE(output->trayclients, trayclient, tailq);
1502 /* Fake a DestroyNotify so that Qt re-adds tray icons.
1503 * We cannot actually destroy the window because then Qt will not restore
1504 * its event mask on the new window. */
1505 uint8_t buffer[32] = {0};
1506 xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t *)buffer;
1508 event->response_type = XCB_DESTROY_NOTIFY;
1509 event->event = selwin;
1510 event->window = selwin;
1512 xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)event);
1514 send_tray_clientmessage();
1518 * Destroy the bar of the specified output
1521 void destroy_window(i3_output *output) {
1522 if (output == NULL) {
1525 if (output->bar.id == XCB_NONE) {
1529 kick_tray_clients(output);
1530 xcb_destroy_window(xcb_connection, output->bar.id);
1531 output->bar.id = XCB_NONE;
1534 /* Strut partial tells i3 where to reserve space for i3bar. This is determined
1535 * by the `position` bar config directive. */
1536 static xcb_void_cookie_t config_strut_partial(i3_output *output) {
1537 /* A local struct to save the strut_partial property */
1543 uint32_t left_start_y;
1544 uint32_t left_end_y;
1545 uint32_t right_start_y;
1546 uint32_t right_end_y;
1547 uint32_t top_start_x;
1549 uint32_t bottom_start_x;
1550 uint32_t bottom_end_x;
1551 } __attribute__((__packed__)) strut_partial;
1552 memset(&strut_partial, 0, sizeof(strut_partial));
1554 switch (config.position) {
1558 strut_partial.top = bar_height;
1559 strut_partial.top_start_x = output->rect.x;
1560 strut_partial.top_end_x = output->rect.x + output->rect.w;
1563 strut_partial.bottom = bar_height;
1564 strut_partial.bottom_start_x = output->rect.x;
1565 strut_partial.bottom_end_x = output->rect.x + output->rect.w;
1568 return xcb_change_property(xcb_connection,
1569 XCB_PROP_MODE_REPLACE,
1571 atoms[_NET_WM_STRUT_PARTIAL],
1579 * Returns the output which should hold the tray, if one exists.
1581 * An output is returned in these scenarios:
1582 * 1. A specific output was listed in tray_outputs which is also in the list
1583 * of outputs managed by this bar.
1584 * 2. No tray_output directive was specified. In this case, we use the first
1586 * 3. 'tray_output primary' was specified. In this case we use the primary
1589 * Three scenarios in which we specifically don't want to use a tray:
1590 * 1. 'tray_output none' was specified.
1591 * 2. A specific output was listed as a tray_output, but is not one of the
1592 * outputs managed by this bar. For example, consider tray_outputs == [VGA-1],
1593 * but outputs == [HDMI-1].
1594 * 3. 'tray_output primary' was specified and no output in the list is
1597 static i3_output *get_tray_output(void) {
1598 i3_output *output = NULL;
1599 if (TAILQ_EMPTY(&(config.tray_outputs))) {
1600 /* No tray_output specified, use first active output. */
1601 SLIST_FOREACH(output, outputs, slist) {
1602 if (output->active) {
1607 } else if (strcasecmp(TAILQ_FIRST(&(config.tray_outputs))->output, "none") == 0) {
1608 /* Check for "tray_output none" */
1612 /* If one or more tray_output assignments were specified, we ensure that at
1613 * least one of them is actually an output managed by this instance. */
1614 tray_output_t *tray_output;
1615 TAILQ_FOREACH(tray_output, &(config.tray_outputs), tray_outputs) {
1616 SLIST_FOREACH(output, outputs, slist) {
1617 if (output->active &&
1618 (strcasecmp(output->name, tray_output->output) == 0 ||
1619 (strcasecmp(tray_output->output, "primary") == 0 && output->primary))) {
1629 * Reconfigure all bars and create new bars for recently activated outputs
1632 void reconfig_windows(bool redraw_bars) {
1637 SLIST_FOREACH(walk, outputs, slist) {
1638 if (!walk->active) {
1639 /* If an output is not active, we destroy its bar */
1640 /* FIXME: Maybe we rather want to unmap? */
1641 DLOG("Destroying window for output %s\n", walk->name);
1642 destroy_window(walk);
1645 if (walk->bar.id == XCB_NONE) {
1646 DLOG("Creating window for output %s\n", walk->name);
1648 xcb_window_t bar_id = xcb_generate_id(xcb_connection);
1649 xcb_pixmap_t buffer_id = xcb_generate_id(xcb_connection);
1650 xcb_pixmap_t statusline_buffer_id = xcb_generate_id(xcb_connection);
1651 mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP | XCB_CW_CURSOR;
1653 values[0] = colors.bar_bg.colorpixel;
1654 values[1] = root_screen->black_pixel;
1655 /* If hide_on_modifier is set to hide or invisible mode, i3 is not supposed to manage our bar windows */
1656 values[2] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1657 /* We enable the following EventMask fields:
1658 * EXPOSURE, to get expose events (we have to re-draw then)
1659 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1660 * child windows use ConfigureWindow
1661 * BUTTON_PRESS, to handle clicks on the workspace buttons
1663 values[3] = XCB_EVENT_MASK_EXPOSURE |
1664 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1665 XCB_EVENT_MASK_BUTTON_PRESS |
1666 XCB_EVENT_MASK_BUTTON_RELEASE;
1667 if (config.hide_on_modifier == M_DOCK) {
1668 /* If the bar is normally visible, catch visibility change events to suspend
1669 * the status process when the bar is obscured by full-screened windows. */
1670 values[3] |= XCB_EVENT_MASK_VISIBILITY_CHANGE;
1671 walk->visible = true;
1673 values[4] = colormap;
1676 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1680 walk->rect.x, walk->rect.y + walk->rect.h - bar_height,
1681 walk->rect.w, bar_height,
1683 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1684 visual_type->visual_id,
1688 /* The double-buffer we use to render stuff off-screen */
1689 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1696 /* The double-buffer we use to render the statusline before copying to buffer */
1697 xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection,
1699 statusline_buffer_id,
1704 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1705 xcb_void_cookie_t class_cookie;
1706 class_cookie = xcb_change_property(xcb_connection,
1707 XCB_PROP_MODE_REPLACE,
1712 (strlen("i3bar") + 1) * 2,
1716 sasprintf(&name, "i3bar for output %s", walk->name);
1717 xcb_void_cookie_t name_cookie;
1718 name_cookie = xcb_change_property(xcb_connection,
1719 XCB_PROP_MODE_REPLACE,
1728 /* We want dock windows (for now). When override_redirect is set, i3 is ignoring
1730 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1731 XCB_PROP_MODE_REPLACE,
1733 atoms[_NET_WM_WINDOW_TYPE],
1737 (unsigned char *)&atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1739 draw_util_surface_init(xcb_connection, &walk->bar, bar_id, NULL, walk->rect.w, bar_height);
1740 draw_util_surface_init(xcb_connection, &walk->buffer, buffer_id, NULL, walk->rect.w, bar_height);
1741 draw_util_surface_init(xcb_connection, &walk->statusline_buffer, statusline_buffer_id, NULL, walk->rect.w, bar_height);
1743 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1745 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1746 xcb_void_cookie_t map_cookie;
1747 if (config.hide_on_modifier == M_DOCK) {
1748 map_cookie = xcb_map_window_checked(xcb_connection, bar_id);
1751 if (xcb_request_failed(win_cookie, "Could not create window") ||
1752 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1753 xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") ||
1754 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1755 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1756 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1757 xcb_request_failed(strut_cookie, "Could not set strut") ||
1758 ((config.hide_on_modifier == M_DOCK) && xcb_request_failed(map_cookie, "Could not map window"))) {
1763 /* We already have a bar, so we just reconfigure it */
1764 mask = XCB_CONFIG_WINDOW_X |
1765 XCB_CONFIG_WINDOW_Y |
1766 XCB_CONFIG_WINDOW_WIDTH |
1767 XCB_CONFIG_WINDOW_HEIGHT |
1768 XCB_CONFIG_WINDOW_STACK_MODE;
1769 values[0] = walk->rect.x;
1770 if (config.position == POS_TOP)
1771 values[1] = walk->rect.y;
1773 values[1] = walk->rect.y + walk->rect.h - bar_height;
1774 values[2] = walk->rect.w;
1775 values[3] = bar_height;
1776 values[4] = XCB_STACK_MODE_ABOVE;
1778 DLOG("Reconfiguring strut partial property for output %s\n", walk->name);
1779 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1781 DLOG("Destroying buffer for output %s\n", walk->name);
1782 xcb_free_pixmap(xcb_connection, walk->buffer.id);
1784 DLOG("Destroying statusline buffer for output %s\n", walk->name);
1785 xcb_free_pixmap(xcb_connection, walk->statusline_buffer.id);
1787 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1788 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1793 mask = XCB_CW_OVERRIDE_REDIRECT;
1794 values[0] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1795 DLOG("Changing window attribute override_redirect for output %s to %d\n", walk->name, values[0]);
1796 xcb_void_cookie_t chg_cookie = xcb_change_window_attributes(xcb_connection,
1801 DLOG("Recreating buffer for output %s\n", walk->name);
1802 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1809 DLOG("Recreating statusline buffer for output %s\n", walk->name);
1810 xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection,
1812 walk->statusline_buffer.id,
1817 draw_util_surface_free(xcb_connection, &(walk->bar));
1818 draw_util_surface_free(xcb_connection, &(walk->buffer));
1819 draw_util_surface_free(xcb_connection, &(walk->statusline_buffer));
1820 draw_util_surface_init(xcb_connection, &(walk->bar), walk->bar.id, NULL, walk->rect.w, bar_height);
1821 draw_util_surface_init(xcb_connection, &(walk->buffer), walk->buffer.id, NULL, walk->rect.w, bar_height);
1822 draw_util_surface_init(xcb_connection, &(walk->statusline_buffer), walk->statusline_buffer.id, NULL, walk->rect.w, bar_height);
1824 xcb_void_cookie_t map_cookie, umap_cookie;
1826 /* Unmap the window, and draw it again when in dock mode */
1827 umap_cookie = xcb_unmap_window_checked(xcb_connection, walk->bar.id);
1828 if (config.hide_on_modifier == M_DOCK) {
1830 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar.id);
1835 if (config.hide_on_modifier == M_HIDE) {
1836 /* Switching to hide mode, register for keyevents */
1837 register_xkb_keyevents();
1839 /* Switching to dock/invisible mode, deregister from keyevents */
1840 deregister_xkb_keyevents();
1844 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") ||
1845 xcb_request_failed(chg_cookie, "Could not change window") ||
1846 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1847 xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") ||
1848 xcb_request_failed(strut_cookie, "Could not set strut") ||
1849 (redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") ||
1850 (config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) {
1856 /* Finally, check if we want to initialize the tray or destroy the selection
1857 * window. The result of get_tray_output() is cached. */
1858 output_for_tray = get_tray_output();
1859 if (output_for_tray) {
1860 if (selwin == XCB_NONE) {
1863 } else if (selwin != XCB_NONE) {
1864 DLOG("Destroying tray selection window\n");
1865 xcb_destroy_window(xcb_connection, selwin);
1871 * Render the bars, with buttons and statusline
1874 void draw_bars(bool unhide) {
1875 DLOG("Drawing bars...\n");
1877 uint32_t full_statusline_width = predict_statusline_length(false);
1878 uint32_t short_statusline_width = predict_statusline_length(true);
1880 i3_output *outputs_walk;
1881 SLIST_FOREACH(outputs_walk, outputs, slist) {
1882 int workspace_width = 0;
1884 if (!outputs_walk->active) {
1885 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1888 if (outputs_walk->bar.id == XCB_NONE) {
1889 /* Oh shit, an active output without an own bar. Create it now! */
1890 reconfig_windows(false);
1893 bool use_focus_colors = output_has_focus(outputs_walk);
1895 /* First things first: clear the backbuffer */
1896 draw_util_clear_surface(&(outputs_walk->buffer), (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg));
1898 if (!config.disable_ws) {
1900 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1901 DLOG("Drawing button for WS %s at x = %d, len = %d\n",
1902 i3string_as_utf8(ws_walk->name), workspace_width, ws_walk->name_width);
1903 color_t fg_color = colors.inactive_ws_fg;
1904 color_t bg_color = colors.inactive_ws_bg;
1905 color_t border_color = colors.inactive_ws_border;
1906 if (ws_walk->visible) {
1907 if (!ws_walk->focused) {
1908 fg_color = colors.active_ws_fg;
1909 bg_color = colors.active_ws_bg;
1910 border_color = colors.active_ws_border;
1912 fg_color = colors.focus_ws_fg;
1913 bg_color = colors.focus_ws_bg;
1914 border_color = colors.focus_ws_border;
1917 if (ws_walk->urgent) {
1918 DLOG("WS %s is urgent!\n", i3string_as_utf8(ws_walk->name));
1919 fg_color = colors.urgent_ws_fg;
1920 bg_color = colors.urgent_ws_bg;
1921 border_color = colors.urgent_ws_border;
1925 /* Draw the border of the button. */
1926 draw_util_rectangle(&(outputs_walk->buffer), border_color,
1929 ws_walk->name_width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
1930 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1));
1932 /* Draw the inside of the button. */
1933 draw_util_rectangle(&(outputs_walk->buffer), bg_color,
1934 workspace_width + logical_px(1),
1936 ws_walk->name_width + 2 * logical_px(ws_hoff_px),
1937 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1));
1939 draw_util_text(ws_walk->name, &(outputs_walk->buffer), fg_color, bg_color,
1940 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
1941 logical_px(ws_voff_px),
1942 ws_walk->name_width);
1944 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
1945 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
1946 workspace_width += logical_px(ws_spacing_px);
1950 if (binding.name && !config.disable_binding_mode_indicator) {
1951 workspace_width += logical_px(ws_spacing_px);
1953 color_t fg_color = colors.binding_mode_fg;
1954 color_t bg_color = colors.binding_mode_bg;
1956 draw_util_rectangle(&(outputs_walk->buffer), colors.binding_mode_border,
1959 binding.width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
1960 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1));
1962 draw_util_rectangle(&(outputs_walk->buffer), bg_color,
1963 workspace_width + logical_px(1),
1965 binding.width + 2 * logical_px(ws_hoff_px),
1966 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1));
1968 draw_util_text(binding.name, &(outputs_walk->buffer), fg_color, bg_color,
1969 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
1970 logical_px(ws_voff_px),
1974 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + binding.width;
1977 if (!TAILQ_EMPTY(&statusline_head)) {
1978 DLOG("Printing statusline!\n");
1980 int tray_width = get_tray_width(outputs_walk->trayclients);
1981 uint32_t hoff = logical_px(((workspace_width > 0) + (tray_width > 0)) * sb_hoff_px);
1982 uint32_t max_statusline_width = outputs_walk->rect.w - workspace_width - tray_width - hoff;
1983 uint32_t clip_left = 0;
1984 uint32_t statusline_width = full_statusline_width;
1985 bool use_short_text = false;
1987 if (statusline_width > max_statusline_width) {
1988 statusline_width = short_statusline_width;
1989 use_short_text = true;
1990 if (statusline_width > max_statusline_width) {
1991 clip_left = statusline_width - max_statusline_width;
1995 int16_t visible_statusline_width = MIN(statusline_width, max_statusline_width);
1996 int x_dest = outputs_walk->rect.w - tray_width - logical_px((tray_width > 0) * sb_hoff_px) - visible_statusline_width;
1998 draw_statusline(outputs_walk, clip_left, use_focus_colors, use_short_text);
1999 draw_util_copy_surface(&outputs_walk->statusline_buffer, &outputs_walk->buffer, 0, 0,
2000 x_dest, 0, visible_statusline_width, (int16_t)bar_height);
2002 outputs_walk->statusline_width = statusline_width;
2003 outputs_walk->statusline_short_text = use_short_text;
2007 /* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */
2009 config.hidden_state == S_SHOW ||
2012 } else if (config.hide_on_modifier == M_HIDE) {
2020 * Redraw the bars, i.e. simply copy the buffer to the barwindow
2023 void redraw_bars(void) {
2024 i3_output *outputs_walk;
2025 SLIST_FOREACH(outputs_walk, outputs, slist) {
2026 if (!outputs_walk->active) {
2030 draw_util_copy_surface(&(outputs_walk->buffer), &(outputs_walk->bar), 0, 0,
2031 0, 0, outputs_walk->rect.w, outputs_walk->rect.h);
2032 xcb_flush(xcb_connection);
2037 * Set the current binding mode
2040 void set_current_mode(struct mode *current) {
2041 I3STRING_FREE(binding.name);
2043 activated_mode = binding.name != NULL;