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
12 #include <xcb/xproto.h>
13 #include <xcb/xcb_aux.h>
14 #include <xcb/xcb_cursor.h>
28 #include <X11/XKBlib.h>
29 #include <X11/extensions/XKB.h>
31 #ifdef I3_ASAN_ENABLED
32 #include <sanitizer/lsan_interface.h>
38 /** This is the equivalent of XC_left_ptr. I’m not sure why xcb doesn’t have a
39 * constant for that. */
40 #define XCB_CURSOR_LEFT_PTR 68
42 /* We save the atoms in an easy to access array, indexed by an enum */
44 #define ATOM_DO(name) name,
45 #include "xcb_atoms.def"
49 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
50 xcb_atom_t atoms[NUM_ATOMS];
52 /* Variables, that are the same for all functions at all times */
53 xcb_connection_t *xcb_connection;
55 xcb_screen_t *root_screen;
56 xcb_window_t xcb_root;
57 static xcb_cursor_t cursor;
59 /* selection window for tray support */
60 static xcb_window_t selwin = XCB_NONE;
61 static xcb_intern_atom_reply_t *tray_reply = NULL;
63 /* This is needed for integration with libi3 */
64 xcb_connection_t *conn;
66 /* The font we'll use */
69 /* Icon size (based on font size) */
72 xcb_visualtype_t *visual_type;
74 xcb_colormap_t colormap;
76 /* Overall height of the bar (based on font size) */
79 /* These are only relevant for XKB, which we only need for grabbing modifiers */
83 /* 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 parsed colors */
100 color_t focus_bar_fg;
101 color_t focus_bar_bg;
102 color_t focus_sep_fg;
103 color_t active_ws_fg;
104 color_t active_ws_bg;
105 color_t active_ws_border;
106 color_t inactive_ws_fg;
107 color_t inactive_ws_bg;
108 color_t inactive_ws_border;
109 color_t urgent_ws_bg;
110 color_t urgent_ws_fg;
111 color_t urgent_ws_border;
114 color_t focus_ws_border;
115 color_t binding_mode_bg;
116 color_t binding_mode_fg;
117 color_t binding_mode_border;
119 struct xcb_colors_t colors;
121 /* Horizontal offset between a workspace label and button borders */
122 static const int ws_hoff_px = 4;
124 /* Vertical offset between a workspace label and button borders */
125 static const int ws_voff_px = 3;
127 /* Offset between two workspace buttons */
128 static const int ws_spacing_px = 1;
130 /* Offset between the statusline and 1) workspace buttons on the left
131 * 2) the tray or screen edge on the right */
132 static const int sb_hoff_px = 4;
134 /* Additional offset between the tray and the statusline, if the tray is not empty */
135 static const int tray_loff_px = 2;
137 /* Vertical offset between the bar and a separator */
138 static const int sep_voff_px = 4;
140 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
141 xcb_generic_error_t *err;
142 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
143 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
144 return err->error_code;
149 uint32_t get_sep_offset(struct status_block *block) {
150 if (!block->no_separator && block->sep_block_width > 0)
151 return block->sep_block_width / 2 + block->sep_block_width % 2;
155 int get_tray_width(struct tc_head *trayclients) {
156 trayclient *trayclient;
158 TAILQ_FOREACH_REVERSE(trayclient, trayclients, tc_head, tailq) {
159 if (!trayclient->mapped)
161 tray_width += icon_size + logical_px(config.tray_padding);
164 tray_width += logical_px(tray_loff_px);
169 * Draws a separator for the given block if necessary.
172 static void draw_separator(i3_output *output, uint32_t x, struct status_block *block, bool use_focus_colors) {
173 color_t sep_fg = (use_focus_colors ? colors.focus_sep_fg : colors.sep_fg);
174 color_t bar_bg = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg);
176 uint32_t sep_offset = get_sep_offset(block);
177 if (TAILQ_NEXT(block, blocks) == NULL || sep_offset == 0)
180 uint32_t center_x = x - sep_offset;
181 if (config.separator_symbol == NULL) {
182 /* Draw a classic one pixel, vertical separator. */
183 draw_util_rectangle(xcb_connection, &output->statusline_buffer, sep_fg,
185 logical_px(sep_voff_px),
187 bar_height - 2 * logical_px(sep_voff_px));
189 /* Draw a custom separator. */
190 uint32_t separator_x = MAX(x - block->sep_block_width, center_x - separator_symbol_width / 2);
191 draw_util_text(config.separator_symbol, &output->statusline_buffer, sep_fg, bar_bg,
192 separator_x, logical_px(ws_voff_px), x - separator_x);
196 uint32_t predict_statusline_length(bool use_short_text) {
198 struct status_block *block;
200 TAILQ_FOREACH(block, &statusline_head, blocks) {
201 i3String *text = block->full_text;
202 struct status_block_render_desc *render = &block->full_render;
203 if (use_short_text && block->short_text != NULL) {
204 text = block->short_text;
205 render = &block->short_render;
208 if (i3string_get_num_bytes(text) == 0)
211 render->width = predict_text_width(text);
213 render->width += logical_px(2);
215 /* Compute offset and append for text aligment in min_width. */
216 if (block->min_width <= render->width) {
217 render->x_offset = 0;
218 render->x_append = 0;
220 uint32_t padding_width = block->min_width - render->width;
221 switch (block->align) {
223 render->x_append = padding_width;
226 render->x_offset = padding_width;
229 render->x_offset = padding_width / 2;
230 render->x_append = padding_width / 2 + padding_width % 2;
235 width += render->width + render->x_offset + render->x_append;
237 /* If this is not the last block, add some pixels for a separator. */
238 if (TAILQ_NEXT(block, blocks) != NULL)
239 width += block->sep_block_width;
246 * Redraws the statusline to the output's statusline_buffer
248 void draw_statusline(i3_output *output, uint32_t clip_left, bool use_focus_colors, bool use_short_text) {
249 struct status_block *block;
251 color_t bar_color = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg);
252 draw_util_clear_surface(xcb_connection, &output->statusline_buffer, bar_color);
254 /* Use unsigned integer wraparound to clip off the left side.
255 * For example, if clip_left is 75, then x will start at the very large
256 * number INT_MAX-75, which is way outside the surface dimensions. Drawing
257 * to that x position is a no-op which XCB and Cairo safely ignore. Once x moves
258 * up by 75 and goes past INT_MAX, it will wrap around again to 0, and we start
259 * actually rendering content to the surface. */
260 uint32_t x = 0 - clip_left;
262 /* Draw the text of each block */
263 TAILQ_FOREACH(block, &statusline_head, blocks) {
264 i3String *text = block->full_text;
265 struct status_block_render_desc *render = &block->full_render;
266 if (use_short_text && block->short_text != NULL) {
267 text = block->short_text;
268 render = &block->short_render;
271 if (i3string_get_num_bytes(text) == 0)
276 fg_color = colors.urgent_ws_fg;
277 } else if (block->color) {
278 fg_color = draw_util_hex_to_color(block->color);
279 } else if (use_focus_colors) {
280 fg_color = colors.focus_bar_fg;
282 fg_color = colors.bar_fg;
285 color_t bg_color = bar_color;
287 int border_width = (block->border) ? logical_px(1) : 0;
288 int full_render_width = render->width + render->x_offset + render->x_append;
289 if (block->border || block->background || block->urgent) {
290 /* Let's determine the colors first. */
291 color_t border_color = bar_color;
293 border_color = colors.urgent_ws_border;
294 bg_color = colors.urgent_ws_bg;
297 border_color = draw_util_hex_to_color(block->border);
298 if (block->background)
299 bg_color = draw_util_hex_to_color(block->background);
302 /* Draw the border. */
303 draw_util_rectangle(xcb_connection, &output->statusline_buffer, border_color,
306 bar_height - logical_px(2));
308 /* Draw the background. */
309 draw_util_rectangle(xcb_connection, &output->statusline_buffer, bg_color,
311 logical_px(1) + border_width,
312 full_render_width - 2 * border_width,
313 bar_height - 2 * border_width - logical_px(2));
316 draw_util_text(text, &output->statusline_buffer, fg_color, bg_color,
317 x + render->x_offset + border_width, logical_px(ws_voff_px),
318 render->width - 2 * border_width);
319 x += full_render_width;
321 /* If this is not the last block, draw a separator. */
322 if (TAILQ_NEXT(block, blocks) != NULL) {
323 x += block->sep_block_width;
324 draw_separator(output, x, block, use_focus_colors);
330 * Hides all bars (unmaps them)
333 void hide_bars(void) {
334 if ((config.hide_on_modifier == M_DOCK) || (config.hidden_state == S_SHOW && config.hide_on_modifier == M_HIDE)) {
339 SLIST_FOREACH(walk, outputs, slist) {
343 xcb_unmap_window(xcb_connection, walk->bar.id);
349 * Unhides all bars (maps them)
352 void unhide_bars(void) {
353 if (config.hide_on_modifier != M_HIDE) {
358 xcb_void_cookie_t cookie;
364 SLIST_FOREACH(walk, outputs, slist) {
365 if (walk->bar.id == XCB_NONE) {
368 mask = XCB_CONFIG_WINDOW_X |
369 XCB_CONFIG_WINDOW_Y |
370 XCB_CONFIG_WINDOW_WIDTH |
371 XCB_CONFIG_WINDOW_HEIGHT |
372 XCB_CONFIG_WINDOW_STACK_MODE;
373 values[0] = walk->rect.x;
374 if (config.position == POS_TOP)
375 values[1] = walk->rect.y;
377 values[1] = walk->rect.y + walk->rect.h - bar_height;
378 values[2] = walk->rect.w;
379 values[3] = bar_height;
380 values[4] = XCB_STACK_MODE_ABOVE;
381 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
382 cookie = xcb_configure_window_checked(xcb_connection,
387 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
390 xcb_map_window(xcb_connection, walk->bar.id);
395 * Parse the colors into a format that we can use
398 void init_colors(const struct xcb_color_strings_t *new_colors) {
399 #define PARSE_COLOR(name, def) \
401 colors.name = draw_util_hex_to_color(new_colors->name ? new_colors->name : def); \
403 PARSE_COLOR(bar_fg, "#FFFFFF");
404 PARSE_COLOR(bar_bg, "#000000");
405 PARSE_COLOR(sep_fg, "#666666");
406 PARSE_COLOR(active_ws_fg, "#FFFFFF");
407 PARSE_COLOR(active_ws_bg, "#333333");
408 PARSE_COLOR(active_ws_border, "#333333");
409 PARSE_COLOR(inactive_ws_fg, "#888888");
410 PARSE_COLOR(inactive_ws_bg, "#222222");
411 PARSE_COLOR(inactive_ws_border, "#333333");
412 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
413 PARSE_COLOR(urgent_ws_bg, "#900000");
414 PARSE_COLOR(urgent_ws_border, "#2f343a");
415 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
416 PARSE_COLOR(focus_ws_bg, "#285577");
417 PARSE_COLOR(focus_ws_border, "#4c7899");
420 #define PARSE_COLOR_FALLBACK(name, fallback) \
422 colors.name = new_colors->name ? draw_util_hex_to_color(new_colors->name) : colors.fallback; \
425 /* For the binding mode indicator colors, we don't hardcode a default.
426 * Instead, we fall back to urgent_ws_* colors. */
427 PARSE_COLOR_FALLBACK(binding_mode_fg, urgent_ws_fg);
428 PARSE_COLOR_FALLBACK(binding_mode_bg, urgent_ws_bg);
429 PARSE_COLOR_FALLBACK(binding_mode_border, urgent_ws_border);
431 /* Similarly, for unspecified focused bar colors, we fall back to the
432 * regular bar colors. */
433 PARSE_COLOR_FALLBACK(focus_bar_fg, bar_fg);
434 PARSE_COLOR_FALLBACK(focus_bar_bg, bar_bg);
435 PARSE_COLOR_FALLBACK(focus_sep_fg, sep_fg);
436 #undef PARSE_COLOR_FALLBACK
439 xcb_flush(xcb_connection);
443 * Handle a button press event (i.e. a mouse click on one of our bars).
444 * We determine, whether the click occurred on a workspace button or if the scroll-
445 * wheel was used and change the workspace appropriately
448 void handle_button(xcb_button_press_event_t *event) {
449 /* Determine, which bar was clicked */
451 xcb_window_t bar = event->event;
452 SLIST_FOREACH(walk, outputs, slist) {
453 if (walk->bar.id == bar) {
459 DLOG("Unknown bar clicked!\n");
463 int32_t x = event->event_x >= 0 ? event->event_x : 0;
465 DLOG("Got button %d\n", event->detail);
467 int workspace_width = 0;
468 i3_ws *cur_ws = NULL, *clicked_ws = NULL, *ws_walk;
470 TAILQ_FOREACH(ws_walk, walk->workspaces, tailq) {
471 int w = 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
472 if (x >= workspace_width && x <= workspace_width + w)
473 clicked_ws = ws_walk;
474 if (ws_walk->visible)
476 workspace_width += w;
477 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
478 workspace_width += logical_px(ws_spacing_px);
481 if (x > workspace_width && child_want_click_events()) {
482 /* If the child asked for click events,
483 * check if a status block has been clicked. */
484 int tray_width = get_tray_width(walk->trayclients);
485 int block_x = 0, last_block_x;
486 int offset = walk->rect.w - walk->statusline_width - tray_width - logical_px(sb_hoff_px);
487 int32_t statusline_x = x - offset;
489 if (statusline_x >= 0 && statusline_x < walk->statusline_width) {
490 struct status_block *block;
491 int sep_offset_remainder = 0;
493 TAILQ_FOREACH(block, &statusline_head, blocks) {
494 i3String *text = block->full_text;
495 struct status_block_render_desc *render = &block->full_render;
496 if (walk->statusline_short_text && block->short_text != NULL) {
497 text = block->short_text;
498 render = &block->short_render;
501 if (i3string_get_num_bytes(text) == 0)
504 last_block_x = block_x;
505 block_x += render->width + render->x_offset + render->x_append + get_sep_offset(block) + sep_offset_remainder;
507 if (statusline_x <= block_x && statusline_x >= last_block_x) {
508 send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y);
512 sep_offset_remainder = block->sep_block_width - get_sep_offset(block);
517 /* If a custom command was specified for this mouse button, it overrides
518 * the default behavior. */
520 TAILQ_FOREACH(binding, &(config.bindings), bindings) {
521 if (binding->input_code != event->detail)
524 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, binding->command);
528 if (cur_ws == NULL) {
529 DLOG("No workspace active?\n");
532 switch (event->detail) {
534 /* Mouse wheel up. We select the previous ws, if any.
535 * If there is no more workspace, don’t even send the workspace
536 * command, otherwise (with workspace auto_back_and_forth) we’d end
537 * up on the wrong workspace. */
538 if (cur_ws == TAILQ_FIRST(walk->workspaces))
541 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
544 /* Mouse wheel down. We select the next ws, if any.
545 * If there is no more workspace, don’t even send the workspace
546 * command, otherwise (with workspace auto_back_and_forth) we’d end
547 * up on the wrong workspace. */
548 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
551 cur_ws = TAILQ_NEXT(cur_ws, tailq);
556 /* if no workspace was clicked, focus our currently visible
557 * workspace if it is not already focused */
558 if (cur_ws == NULL) {
559 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
560 if (cur_ws->visible && !cur_ws->focused)
565 /* if there is nothing to focus, we are done */
574 /* To properly handle workspace names with double quotes in them, we need
575 * to escape the double quotes. Unfortunately, that’s rather ugly in C: We
576 * first count the number of double quotes, then we allocate a large enough
577 * buffer, then we copy character by character. */
580 const char *utf8_name = cur_ws->canonical_name;
581 for (const char *walk = utf8_name; *walk != '\0'; walk++) {
582 if (*walk == '"' || *walk == '\\')
584 /* While we’re looping through the name anyway, we can save one
589 const size_t len = namelen + strlen("workspace \"\"") + 1;
590 char *buffer = scalloc(len + num_quotes, 1);
591 strncpy(buffer, "workspace \"", strlen("workspace \""));
592 size_t inpos, outpos;
593 for (inpos = 0, outpos = strlen("workspace \"");
596 if (utf8_name[inpos] == '"' || utf8_name[inpos] == '\\') {
597 buffer[outpos] = '\\';
600 buffer[outpos] = utf8_name[inpos];
602 buffer[outpos] = '"';
603 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
608 * Handle visibility notifications: when none of the bars are visible, e.g.
609 * if windows are in fullscreen on each output, suspend the child process.
612 static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
613 bool visible = (event->state != XCB_VISIBILITY_FULLY_OBSCURED);
617 SLIST_FOREACH(output, outputs, slist) {
618 if (!output->active) {
621 if (output->bar.id == event->window) {
622 if (output->visible == visible) {
625 output->visible = visible;
627 num_visible += output->visible;
630 if (num_visible == 0) {
632 } else if (num_visible == visible) {
633 /* Wake the child only when transitioning from 0 to 1 visible bar.
634 * We cannot transition from 0 to 2 or more visible bars at once since
635 * visibility events are delivered to each window separately */
641 * Adjusts the size of the tray window and alignment of the tray clients by
642 * configuring their respective x coordinates. To be called when mapping or
643 * unmapping a tray client window.
646 static void configure_trayclients(void) {
647 trayclient *trayclient;
649 SLIST_FOREACH(output, outputs, slist) {
654 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
655 if (!trayclient->mapped)
659 DLOG("Configuring tray window %08x to x=%d\n",
660 trayclient->win, output->rect.w - (clients * (icon_size + logical_px(config.tray_padding))));
661 uint32_t x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
662 xcb_configure_window(xcb_connection,
671 * Handles ClientMessages (messages sent from another client directly to us).
673 * At the moment, only the tray window will receive client messages. All
674 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
677 static void handle_client_message(xcb_client_message_event_t *event) {
678 if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
679 event->format == 32) {
680 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
681 /* event->data.data32[0] is the timestamp */
682 uint32_t op = event->data.data32[1];
685 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
686 xcb_window_t client = event->data.data32[2];
688 mask = XCB_CW_EVENT_MASK;
690 /* Needed to get the most recent value of XEMBED_MAPPED. */
691 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
692 /* Needed for UnmapNotify events. */
693 values[0] |= XCB_EVENT_MASK_STRUCTURE_NOTIFY;
694 /* Needed because some tray applications (e.g., VLC) use
695 * override_redirect which causes no ConfigureRequest to be sent. */
696 values[0] |= XCB_EVENT_MASK_RESIZE_REDIRECT;
698 xcb_change_window_attributes(xcb_connection, client, mask, values);
700 /* Request the _XEMBED_INFO property. The XEMBED specification
701 * (which is referred by the tray specification) says this *has* to
702 * be set, but VLC does not set it… */
705 xcb_get_property_cookie_t xembedc;
706 xcb_generic_error_t *error;
707 xembedc = xcb_get_property(xcb_connection,
711 XCB_GET_PROPERTY_TYPE_ANY,
715 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
719 ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
724 if (xembedr != NULL && xembedr->length != 0) {
725 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
726 uint32_t *xembed = xcb_get_property_value(xembedr);
727 DLOG("xembed version = %d\n", xembed[0]);
728 DLOG("xembed flags = %d\n", xembed[1]);
729 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
730 xe_version = xembed[0];
735 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
738 DLOG("X window %08x requested docking\n", client);
739 i3_output *output = NULL;
740 i3_output *walk = NULL;
741 tray_output_t *tray_output = NULL;
742 /* We need to iterate through the tray_output assignments first in
743 * order to prioritize them. Otherwise, if this bar manages two
744 * outputs and both are assigned as tray_output as well, the first
745 * output in our list would receive the tray rather than the first
746 * one defined via tray_output. */
747 TAILQ_FOREACH(tray_output, &(config.tray_outputs), tray_outputs) {
748 SLIST_FOREACH(walk, outputs, slist) {
752 if (strcasecmp(walk->name, tray_output->output) == 0) {
753 DLOG("Found tray_output assignment for output %s.\n", walk->name);
758 if (walk->primary && strcasecmp("primary", tray_output->output) == 0) {
759 DLOG("Found tray_output assignment on primary output %s.\n", walk->name);
765 /* If we found an output, we're done. */
770 /* If no tray_output has been specified, we fall back to the first
771 * available output. */
772 if (output == NULL && TAILQ_EMPTY(&(config.tray_outputs))) {
773 SLIST_FOREACH(walk, outputs, slist) {
776 DLOG("Falling back to output %s because no primary output is configured\n", walk->name);
782 if (output == NULL) {
783 ELOG("No output found\n");
787 xcb_void_cookie_t rcookie = xcb_reparent_window(xcb_connection,
790 output->rect.w - icon_size - logical_px(config.tray_padding),
791 logical_px(config.tray_padding));
792 if (xcb_request_failed(rcookie, "Could not reparent window. Maybe it is using an incorrect depth/visual?"))
795 /* We reconfigure the window to use a reasonable size. The systray
796 * specification explicitly says:
797 * Tray icons may be assigned any size by the system tray, and
798 * should do their best to cope with any size effectively
800 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
801 values[0] = icon_size;
802 values[1] = icon_size;
803 xcb_configure_window(xcb_connection,
808 /* send the XEMBED_EMBEDDED_NOTIFY message */
809 void *event = scalloc(32, 1);
810 xcb_client_message_event_t *ev = event;
811 ev->response_type = XCB_CLIENT_MESSAGE;
813 ev->type = atoms[_XEMBED];
815 ev->data.data32[0] = XCB_CURRENT_TIME;
816 ev->data.data32[1] = XEMBED_EMBEDDED_NOTIFY;
817 ev->data.data32[2] = output->bar.id;
818 ev->data.data32[3] = xe_version;
819 xcb_send_event(xcb_connection,
822 XCB_EVENT_MASK_NO_EVENT,
826 /* Put the client inside the save set. Upon termination (whether
827 * killed or normal exit does not matter) of i3bar, these clients
828 * will be correctly reparented to their most closest living
829 * ancestor. Without this, tray icons might die when i3bar
831 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
833 trayclient *tc = smalloc(sizeof(trayclient));
835 tc->xe_version = xe_version;
837 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
840 DLOG("Mapping dock client\n");
841 xcb_map_window(xcb_connection, client);
843 DLOG("Not mapping dock client yet\n");
845 /* Trigger an update to copy the statusline text to the appropriate
847 configure_trayclients();
854 * Handles DestroyNotify events by removing the tray client from the data
855 * structure. According to the XEmbed protocol, this is one way for a tray
856 * client to finish the protocol. After this event is received, there is no
857 * further interaction with the tray client.
859 * See: http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
862 static void handle_destroy_notify(xcb_destroy_notify_event_t *event) {
863 DLOG("DestroyNotify for window = %08x, event = %08x\n", event->window, event->event);
866 SLIST_FOREACH(walk, outputs, slist) {
869 DLOG("checking output %s\n", walk->name);
870 trayclient *trayclient;
871 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
872 if (trayclient->win != event->window)
875 DLOG("Removing tray client with window ID %08x\n", event->window);
876 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
878 /* Trigger an update, we now have more space for the statusline */
879 configure_trayclients();
887 * Handles MapNotify events. These events happen when a tray client shows its
888 * window. We respond by realigning the tray clients.
891 static void handle_map_notify(xcb_map_notify_event_t *event) {
892 DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event);
895 SLIST_FOREACH(walk, outputs, slist) {
898 DLOG("checking output %s\n", walk->name);
899 trayclient *trayclient;
900 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
901 if (trayclient->win != event->window)
904 DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window);
905 trayclient->mapped = true;
907 /* Trigger an update, we now have more space for the statusline */
908 configure_trayclients();
915 * Handles UnmapNotify events. These events happen when a tray client hides its
916 * window. We respond by realigning the tray clients.
919 static void handle_unmap_notify(xcb_unmap_notify_event_t *event) {
920 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
923 SLIST_FOREACH(walk, outputs, slist) {
926 DLOG("checking output %s\n", walk->name);
927 trayclient *trayclient;
928 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
929 if (trayclient->win != event->window)
932 DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window);
933 trayclient->mapped = false;
935 /* Trigger an update, we now have more space for the statusline */
936 configure_trayclients();
944 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
945 * handled, which tells us whether a dock client should be mapped or unmapped.
948 static void handle_property_notify(xcb_property_notify_event_t *event) {
949 DLOG("PropertyNotify\n");
950 if (event->atom == atoms[_XEMBED_INFO] &&
951 event->state == XCB_PROPERTY_NEW_VALUE) {
952 DLOG("xembed_info updated\n");
953 trayclient *trayclient = NULL, *walk;
955 SLIST_FOREACH(o_walk, outputs, slist) {
959 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
960 if (walk->win != event->window)
970 ELOG("PropertyNotify received for unknown window %08x\n",
974 xcb_get_property_cookie_t xembedc;
975 xembedc = xcb_get_property_unchecked(xcb_connection,
979 XCB_GET_PROPERTY_TYPE_ANY,
983 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
986 if (xembedr == NULL || xembedr->length == 0) {
987 DLOG("xembed_info unset\n");
991 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
992 uint32_t *xembed = xcb_get_property_value(xembedr);
993 DLOG("xembed version = %d\n", xembed[0]);
994 DLOG("xembed flags = %d\n", xembed[1]);
995 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
996 DLOG("map state now %d\n", map_it);
997 if (trayclient->mapped && !map_it) {
998 /* need to unmap the window */
999 xcb_unmap_window(xcb_connection, trayclient->win);
1000 } else if (!trayclient->mapped && map_it) {
1001 /* need to map the window */
1002 xcb_map_window(xcb_connection, trayclient->win);
1009 * If a tray client attempts to change its size we deny the request and respond
1010 * by telling it its actual size.
1013 static void handle_configuration_change(xcb_window_t window) {
1014 trayclient *trayclient;
1016 SLIST_FOREACH(output, outputs, slist) {
1017 if (!output->active)
1021 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
1022 if (!trayclient->mapped)
1026 if (trayclient->win != window)
1029 xcb_rectangle_t rect;
1030 rect.x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
1031 rect.y = logical_px(config.tray_padding);
1032 rect.width = icon_size;
1033 rect.height = icon_size;
1035 DLOG("This is a tray window. x = %d\n", rect.x);
1036 fake_configure_notify(xcb_connection, rect, window, 0);
1041 DLOG("WARNING: Could not find corresponding tray window.\n");
1044 static void handle_configure_request(xcb_configure_request_event_t *event) {
1045 DLOG("ConfigureRequest for window = %08x\n", event->window);
1046 handle_configuration_change(event->window);
1049 static void handle_resize_request(xcb_resize_request_event_t *event) {
1050 DLOG("ResizeRequest for window = %08x\n", event->window);
1051 handle_configuration_change(event->window);
1055 * This function is called immediately before the main loop locks. We flush xcb
1056 * then (and only then)
1059 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
1060 xcb_flush(xcb_connection);
1064 * This function is called immediately after the main loop locks, so when one
1065 * of the watchers registered an event.
1066 * We check whether an X-Event arrived and handle it.
1069 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
1070 xcb_generic_event_t *event;
1072 if (xcb_connection_has_error(xcb_connection)) {
1073 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
1074 #ifdef I3_ASAN_ENABLED
1075 __lsan_do_leak_check();
1080 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
1081 if (event->response_type == 0) {
1082 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
1083 DLOG("Received X11 error, sequence 0x%x, error_code = %d\n", error->sequence, error->error_code);
1088 int type = (event->response_type & ~0x80);
1090 if (type == xkb_base && xkb_base > -1) {
1091 DLOG("received an xkb event\n");
1093 xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event;
1094 if (state->xkbType == XCB_XKB_STATE_NOTIFY && config.modifier != XCB_NONE) {
1095 int modstate = state->mods & config.modifier;
1097 #define DLOGMOD(modmask, status) \
1099 switch (modmask) { \
1101 DLOG("ShiftMask got " #status "!\n"); \
1104 DLOG("ControlMask got " #status "!\n"); \
1107 DLOG("Mod1Mask got " #status "!\n"); \
1110 DLOG("Mod2Mask got " #status "!\n"); \
1113 DLOG("Mod3Mask got " #status "!\n"); \
1116 DLOG("Mod4Mask got " #status "!\n"); \
1119 DLOG("Mod5Mask got " #status "!\n"); \
1124 if (modstate != mod_pressed) {
1125 if (modstate == 0) {
1126 DLOGMOD(config.modifier, released);
1127 if (!activated_mode)
1130 DLOGMOD(config.modifier, pressed);
1131 activated_mode = false;
1134 mod_pressed = modstate;
1144 case XCB_VISIBILITY_NOTIFY:
1145 /* Visibility change: a bar is [un]obscured by other window */
1146 handle_visibility_notify((xcb_visibility_notify_event_t *)event);
1149 /* Expose-events happen, when the window needs to be redrawn */
1152 case XCB_BUTTON_PRESS:
1153 /* Button press events are mouse buttons clicked on one of our bars */
1154 handle_button((xcb_button_press_event_t *)event);
1156 case XCB_CLIENT_MESSAGE:
1157 /* Client messages are used for client-to-client communication, for
1158 * example system tray widgets talk to us directly via client messages. */
1159 handle_client_message((xcb_client_message_event_t *)event);
1161 case XCB_DESTROY_NOTIFY:
1162 /* DestroyNotify signifies the end of the XEmbed protocol */
1163 handle_destroy_notify((xcb_destroy_notify_event_t *)event);
1165 case XCB_UNMAP_NOTIFY:
1166 /* UnmapNotify is received when a tray client hides its window. */
1167 handle_unmap_notify((xcb_unmap_notify_event_t *)event);
1169 case XCB_MAP_NOTIFY:
1170 handle_map_notify((xcb_map_notify_event_t *)event);
1172 case XCB_PROPERTY_NOTIFY:
1173 /* PropertyNotify */
1174 handle_property_notify((xcb_property_notify_event_t *)event);
1176 case XCB_CONFIGURE_REQUEST:
1177 /* ConfigureRequest, sent by a tray child */
1178 handle_configure_request((xcb_configure_request_event_t *)event);
1179 case XCB_RESIZE_REQUEST:
1180 /* ResizeRequest sent by a tray child using override_redirect. */
1181 handle_resize_request((xcb_resize_request_event_t *)event);
1189 * Dummy callback. We only need this, so that the prepare and check watchers
1193 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
1197 * Early initialization of the connection to X11: Everything which does not
1198 * depend on 'config'.
1201 char *init_xcb_early() {
1202 /* FIXME: xcb_connect leaks memory */
1203 xcb_connection = xcb_connect(NULL, &screen);
1204 if (xcb_connection_has_error(xcb_connection)) {
1205 ELOG("Cannot open display\n");
1208 conn = xcb_connection;
1209 DLOG("Connected to xcb\n");
1211 /* We have to request the atoms we need */
1212 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
1213 #include "xcb_atoms.def"
1215 root_screen = xcb_aux_get_screen(xcb_connection, screen);
1216 xcb_root = root_screen->root;
1218 depth = root_screen->root_depth;
1219 colormap = root_screen->default_colormap;
1220 visual_type = get_visualtype(root_screen);
1222 xcb_cursor_context_t *cursor_ctx;
1223 if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) == 0) {
1224 cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr");
1225 xcb_cursor_context_free(cursor_ctx);
1227 cursor = xcb_generate_id(xcb_connection);
1228 i3Font cursor_font = load_font("cursor", false);
1229 xcb_create_glyph_cursor(
1232 cursor_font.specific.xcb.id,
1233 cursor_font.specific.xcb.id,
1234 XCB_CURSOR_LEFT_PTR,
1235 XCB_CURSOR_LEFT_PTR + 1,
1237 65535, 65535, 65535);
1240 /* The various watchers to communicate with xcb */
1241 xcb_io = smalloc(sizeof(ev_io));
1242 xcb_prep = smalloc(sizeof(ev_prepare));
1243 xcb_chk = smalloc(sizeof(ev_check));
1245 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
1246 ev_prepare_init(xcb_prep, &xcb_prep_cb);
1247 ev_check_init(xcb_chk, &xcb_chk_cb);
1249 ev_io_start(main_loop, xcb_io);
1250 ev_prepare_start(main_loop, xcb_prep);
1251 ev_check_start(main_loop, xcb_chk);
1253 /* Now we get the atoms and save them in a nice data structure */
1256 char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen);
1262 * Register for xkb keyevents. To grab modifiers without blocking other applications from receiving key events
1263 * involving that modifier, we sadly have to use xkb which is not yet fully supported
1267 void register_xkb_keyevents() {
1268 const xcb_query_extension_reply_t *extreply;
1269 extreply = xcb_get_extension_data(conn, &xcb_xkb_id);
1270 if (!extreply->present) {
1271 ELOG("xkb is not present on this server\n");
1274 DLOG("initializing xcb-xkb\n");
1275 xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
1276 xcb_xkb_select_events(conn,
1277 XCB_XKB_ID_USE_CORE_KBD,
1278 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1280 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1284 xkb_base = extreply->first_event;
1288 * Deregister from xkb keyevents.
1291 void deregister_xkb_keyevents() {
1292 xcb_xkb_select_events(conn,
1293 XCB_XKB_ID_USE_CORE_KBD,
1303 * Initialization which depends on 'config' being usable. Called after the
1304 * configuration has arrived.
1307 void init_xcb_late(char *fontname) {
1308 if (fontname == NULL)
1309 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
1312 font = load_font(fontname, true);
1314 DLOG("Calculated font height: %d\n", font.height);
1315 bar_height = font.height + 2 * logical_px(ws_voff_px);
1316 icon_size = bar_height - 2 * logical_px(config.tray_padding);
1318 if (config.separator_symbol)
1319 separator_symbol_width = predict_text_width(config.separator_symbol);
1321 xcb_flush(xcb_connection);
1323 if (config.hide_on_modifier == M_HIDE)
1324 register_xkb_keyevents();
1328 * Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the
1332 static void send_tray_clientmessage(void) {
1333 uint8_t buffer[32] = {0};
1334 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)buffer;
1336 ev->response_type = XCB_CLIENT_MESSAGE;
1337 ev->window = xcb_root;
1338 ev->type = atoms[MANAGER];
1340 ev->data.data32[0] = XCB_CURRENT_TIME;
1341 ev->data.data32[1] = tray_reply->atom;
1342 ev->data.data32[2] = selwin;
1344 xcb_send_event(xcb_connection,
1352 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
1353 * for the X11 display we are running on, then acquiring the selection for this
1354 * atom. Afterwards, tray clients will send ClientMessages to our window.
1357 void init_tray(void) {
1358 DLOG("Initializing system tray functionality\n");
1359 /* request the tray manager atom for the X11 display we are running on */
1360 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
1361 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
1362 xcb_intern_atom_cookie_t tray_cookie;
1363 if (tray_reply == NULL)
1364 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
1366 /* tray support: we need a window to own the selection */
1367 selwin = xcb_generate_id(xcb_connection);
1368 uint32_t selmask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_COLORMAP;
1369 uint32_t selval[] = {root_screen->black_pixel, root_screen->black_pixel, 1, colormap};
1370 xcb_create_window(xcb_connection,
1377 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1378 visual_type->visual_id,
1382 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
1384 xcb_change_property(xcb_connection,
1385 XCB_PROP_MODE_REPLACE,
1387 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1392 xcb_change_property(xcb_connection,
1393 XCB_PROP_MODE_REPLACE,
1395 atoms[_NET_SYSTEM_TRAY_VISUAL],
1399 &visual_type->visual_id);
1403 if (tray_reply == NULL) {
1404 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1405 ELOG("Could not get atom %s\n", atomname);
1410 xcb_set_selection_owner(xcb_connection,
1415 /* Verify that we have the selection */
1416 xcb_get_selection_owner_cookie_t selcookie;
1417 xcb_get_selection_owner_reply_t *selreply;
1419 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1420 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1421 ELOG("Could not get selection owner for %s\n", atomname);
1425 if (selreply->owner != selwin) {
1426 ELOG("Could not set the %s selection. "
1427 "Maybe another tray is already running?\n",
1429 /* NOTE that this error is not fatal. We just can’t provide tray
1437 send_tray_clientmessage();
1441 * We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window
1442 * to make GTK+ 3 applets with symbolic icons visible. If the colors are unset,
1443 * they assume a light background.
1444 * See also https://bugzilla.gnome.org/show_bug.cgi?id=679591
1447 void init_tray_colors(void) {
1448 /* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */
1449 const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF");
1451 DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg);
1453 char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'},
1454 {bar_fg[3], bar_fg[4], '\0'},
1455 {bar_fg[5], bar_fg[6], '\0'}};
1456 const uint8_t r = strtol(strgroups[0], NULL, 16);
1457 const uint8_t g = strtol(strgroups[1], NULL, 16);
1458 const uint8_t b = strtol(strgroups[2], NULL, 16);
1460 const uint16_t r16 = ((uint16_t)r << 8) | r;
1461 const uint16_t g16 = ((uint16_t)g << 8) | g;
1462 const uint16_t b16 = ((uint16_t)b << 8) | b;
1464 const uint32_t tray_colors[12] = {
1465 r16, g16, b16, /* foreground color */
1466 r16, g16, b16, /* error color */
1467 r16, g16, b16, /* warning color */
1468 r16, g16, b16, /* success color */
1471 xcb_change_property(xcb_connection,
1472 XCB_PROP_MODE_REPLACE,
1474 atoms[_NET_SYSTEM_TRAY_COLORS],
1482 * Cleanup the xcb stuff.
1483 * Called once, before the program terminates.
1486 void clean_xcb(void) {
1489 SLIST_FOREACH(o_walk, outputs, slist) {
1490 destroy_window(o_walk);
1491 FREE(o_walk->trayclients);
1492 FREE(o_walk->workspaces);
1495 FREE_SLIST(outputs, i3_output);
1500 xcb_free_cursor(xcb_connection, cursor);
1501 xcb_flush(xcb_connection);
1502 xcb_aux_sync(xcb_connection);
1503 xcb_disconnect(xcb_connection);
1505 ev_check_stop(main_loop, xcb_chk);
1506 ev_prepare_stop(main_loop, xcb_prep);
1507 ev_io_stop(main_loop, xcb_io);
1515 * Get the earlier requested atoms and save them in the prepared data structure
1518 void get_atoms(void) {
1519 xcb_intern_atom_reply_t *reply;
1520 #define ATOM_DO(name) \
1521 reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1522 if (reply == NULL) { \
1523 ELOG("Could not get atom %s\n", #name); \
1524 exit(EXIT_FAILURE); \
1526 atoms[name] = reply->atom; \
1529 #include "xcb_atoms.def"
1530 DLOG("Got atoms\n");
1534 * Reparents all tray clients of the specified output to the root window. This
1535 * is either used when shutting down, when an output appears (xrandr --output
1536 * VGA1 --off) or when the primary output changes.
1538 * Applications using the tray will start the protocol from the beginning again
1542 void kick_tray_clients(i3_output *output) {
1543 if (TAILQ_EMPTY(output->trayclients))
1546 trayclient *trayclient;
1547 while (!TAILQ_EMPTY(output->trayclients)) {
1548 trayclient = TAILQ_FIRST(output->trayclients);
1549 /* Unmap, then reparent (to root) the tray client windows */
1550 xcb_unmap_window(xcb_connection, trayclient->win);
1551 xcb_reparent_window(xcb_connection,
1557 /* We remove the trayclient right here. We might receive an UnmapNotify
1558 * event afterwards, but better safe than sorry. */
1559 TAILQ_REMOVE(output->trayclients, trayclient, tailq);
1562 /* Fake a DestroyNotify so that Qt re-adds tray icons.
1563 * We cannot actually destroy the window because then Qt will not restore
1564 * its event mask on the new window. */
1565 uint8_t buffer[32] = {0};
1566 xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t *)buffer;
1568 event->response_type = XCB_DESTROY_NOTIFY;
1569 event->event = selwin;
1570 event->window = selwin;
1572 xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)event);
1574 send_tray_clientmessage();
1578 * Destroy the bar of the specified output
1581 void destroy_window(i3_output *output) {
1582 if (output == NULL) {
1585 if (output->bar.id == XCB_NONE) {
1589 kick_tray_clients(output);
1590 xcb_destroy_window(xcb_connection, output->bar.id);
1591 output->bar.id = XCB_NONE;
1594 /* Strut partial tells i3 where to reserve space for i3bar. This is determined
1595 * by the `position` bar config directive. */
1596 xcb_void_cookie_t config_strut_partial(i3_output *output) {
1597 /* A local struct to save the strut_partial property */
1603 uint32_t left_start_y;
1604 uint32_t left_end_y;
1605 uint32_t right_start_y;
1606 uint32_t right_end_y;
1607 uint32_t top_start_x;
1609 uint32_t bottom_start_x;
1610 uint32_t bottom_end_x;
1611 } __attribute__((__packed__)) strut_partial;
1612 memset(&strut_partial, 0, sizeof(strut_partial));
1614 switch (config.position) {
1618 strut_partial.top = bar_height;
1619 strut_partial.top_start_x = output->rect.x;
1620 strut_partial.top_end_x = output->rect.x + output->rect.w;
1623 strut_partial.bottom = bar_height;
1624 strut_partial.bottom_start_x = output->rect.x;
1625 strut_partial.bottom_end_x = output->rect.x + output->rect.w;
1628 return xcb_change_property(xcb_connection,
1629 XCB_PROP_MODE_REPLACE,
1631 atoms[_NET_WM_STRUT_PARTIAL],
1639 * Reconfigure all bars and create new bars for recently activated outputs
1642 void reconfig_windows(bool redraw_bars) {
1645 static bool tray_configured = false;
1648 SLIST_FOREACH(walk, outputs, slist) {
1649 if (!walk->active) {
1650 /* If an output is not active, we destroy its bar */
1651 /* FIXME: Maybe we rather want to unmap? */
1652 DLOG("Destroying window for output %s\n", walk->name);
1653 destroy_window(walk);
1656 if (walk->bar.id == XCB_NONE) {
1657 DLOG("Creating window for output %s\n", walk->name);
1659 xcb_window_t bar_id = xcb_generate_id(xcb_connection);
1660 xcb_pixmap_t buffer_id = xcb_generate_id(xcb_connection);
1661 xcb_pixmap_t statusline_buffer_id = xcb_generate_id(xcb_connection);
1662 mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP | XCB_CW_CURSOR;
1664 values[0] = colors.bar_bg.colorpixel;
1665 values[1] = root_screen->black_pixel;
1666 /* If hide_on_modifier is set to hide or invisible mode, i3 is not supposed to manage our bar windows */
1667 values[2] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1668 /* We enable the following EventMask fields:
1669 * EXPOSURE, to get expose events (we have to re-draw then)
1670 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1671 * child windows use ConfigureWindow
1672 * BUTTON_PRESS, to handle clicks on the workspace buttons
1674 values[3] = XCB_EVENT_MASK_EXPOSURE |
1675 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1676 XCB_EVENT_MASK_BUTTON_PRESS;
1677 if (config.hide_on_modifier == M_DOCK) {
1678 /* If the bar is normally visible, catch visibility change events to suspend
1679 * the status process when the bar is obscured by full-screened windows. */
1680 values[3] |= XCB_EVENT_MASK_VISIBILITY_CHANGE;
1681 walk->visible = true;
1683 values[4] = colormap;
1686 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1690 walk->rect.x, walk->rect.y + walk->rect.h - bar_height,
1691 walk->rect.w, bar_height,
1693 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1694 visual_type->visual_id,
1698 /* The double-buffer we use to render stuff off-screen */
1699 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1706 /* The double-buffer we use to render the statusline before copying to buffer */
1707 xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection,
1709 statusline_buffer_id,
1714 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1715 xcb_void_cookie_t class_cookie;
1716 class_cookie = xcb_change_property(xcb_connection,
1717 XCB_PROP_MODE_REPLACE,
1722 (strlen("i3bar") + 1) * 2,
1726 sasprintf(&name, "i3bar for output %s", walk->name);
1727 xcb_void_cookie_t name_cookie;
1728 name_cookie = xcb_change_property(xcb_connection,
1729 XCB_PROP_MODE_REPLACE,
1738 /* We want dock windows (for now). When override_redirect is set, i3 is ignoring
1740 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1741 XCB_PROP_MODE_REPLACE,
1743 atoms[_NET_WM_WINDOW_TYPE],
1747 (unsigned char *)&atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1749 draw_util_surface_init(xcb_connection, &walk->bar, bar_id, NULL, walk->rect.w, bar_height);
1750 draw_util_surface_init(xcb_connection, &walk->buffer, buffer_id, NULL, walk->rect.w, bar_height);
1751 draw_util_surface_init(xcb_connection, &walk->statusline_buffer, statusline_buffer_id, NULL, walk->rect.w, bar_height);
1753 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1755 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1756 xcb_void_cookie_t map_cookie;
1757 if (config.hide_on_modifier == M_DOCK) {
1758 map_cookie = xcb_map_window_checked(xcb_connection, bar_id);
1761 if (xcb_request_failed(win_cookie, "Could not create window") ||
1762 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1763 xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") ||
1764 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1765 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1766 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1767 xcb_request_failed(strut_cookie, "Could not set strut") ||
1768 ((config.hide_on_modifier == M_DOCK) && xcb_request_failed(map_cookie, "Could not map window"))) {
1772 /* Unless "tray_output none" was specified, we need to initialize the tray. */
1773 bool no_tray = false;
1774 if (!(TAILQ_EMPTY(&(config.tray_outputs)))) {
1775 no_tray = strcasecmp(TAILQ_FIRST(&(config.tray_outputs))->output, "none") == 0;
1779 * There are three scenarios in which we need to initialize the tray:
1780 * 1. A specific output was listed in tray_outputs which is also
1781 * in the list of outputs managed by this bar.
1782 * 2. No tray_output directive was specified. In this case, we
1783 * use the first available output.
1784 * 3. 'tray_output primary' was specified. In this case we use the
1787 * Three scenarios in which we specifically don't want to
1788 * initialize the tray are:
1789 * 1. 'tray_output none' was specified.
1790 * 2. A specific output was listed as a tray_output, but is not
1791 * one of the outputs managed by this bar. For example, consider
1792 * tray_outputs == [VGA-1], but outputs == [HDMI-1].
1793 * 3. 'tray_output primary' was specified and no output in the list
1796 if (!tray_configured && !no_tray) {
1797 /* If no tray_output was specified, we go ahead and initialize the tray as
1798 * we will be using the first available output. */
1799 if (TAILQ_EMPTY(&(config.tray_outputs))) {
1803 /* If one or more tray_output assignments were specified, we ensure that at least one of
1804 * them is actually an output managed by this instance. */
1805 tray_output_t *tray_output;
1806 TAILQ_FOREACH(tray_output, &(config.tray_outputs), tray_outputs) {
1809 SLIST_FOREACH(output, outputs, slist) {
1810 if (strcasecmp(output->name, tray_output->output) == 0 ||
1811 (strcasecmp(tray_output->output, "primary") == 0 && output->primary)) {
1822 tray_configured = true;
1825 /* We already have a bar, so we just reconfigure it */
1826 mask = XCB_CONFIG_WINDOW_X |
1827 XCB_CONFIG_WINDOW_Y |
1828 XCB_CONFIG_WINDOW_WIDTH |
1829 XCB_CONFIG_WINDOW_HEIGHT |
1830 XCB_CONFIG_WINDOW_STACK_MODE;
1831 values[0] = walk->rect.x;
1832 if (config.position == POS_TOP)
1833 values[1] = walk->rect.y;
1835 values[1] = walk->rect.y + walk->rect.h - bar_height;
1836 values[2] = walk->rect.w;
1837 values[3] = bar_height;
1838 values[4] = XCB_STACK_MODE_ABOVE;
1840 DLOG("Reconfiguring strut partial property for output %s\n", walk->name);
1841 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1843 DLOG("Destroying buffer for output %s\n", walk->name);
1844 xcb_free_pixmap(xcb_connection, walk->buffer.id);
1846 DLOG("Destroying statusline buffer for output %s\n", walk->name);
1847 xcb_free_pixmap(xcb_connection, walk->statusline_buffer.id);
1849 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1850 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1855 mask = XCB_CW_OVERRIDE_REDIRECT;
1856 values[0] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1857 DLOG("Changing window attribute override_redirect for output %s to %d\n", walk->name, values[0]);
1858 xcb_void_cookie_t chg_cookie = xcb_change_window_attributes(xcb_connection,
1863 DLOG("Recreating buffer for output %s\n", walk->name);
1864 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1871 DLOG("Recreating statusline buffer for output %s\n", walk->name);
1872 xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection,
1874 walk->statusline_buffer.id,
1879 draw_util_surface_free(xcb_connection, &(walk->bar));
1880 draw_util_surface_free(xcb_connection, &(walk->buffer));
1881 draw_util_surface_free(xcb_connection, &(walk->statusline_buffer));
1882 draw_util_surface_init(xcb_connection, &(walk->bar), walk->bar.id, NULL, walk->rect.w, bar_height);
1883 draw_util_surface_init(xcb_connection, &(walk->buffer), walk->buffer.id, NULL, walk->rect.w, bar_height);
1884 draw_util_surface_init(xcb_connection, &(walk->statusline_buffer), walk->statusline_buffer.id, NULL, walk->rect.w, bar_height);
1886 xcb_void_cookie_t map_cookie, umap_cookie;
1888 /* Unmap the window, and draw it again when in dock mode */
1889 umap_cookie = xcb_unmap_window_checked(xcb_connection, walk->bar.id);
1890 if (config.hide_on_modifier == M_DOCK) {
1892 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar.id);
1897 if (config.hide_on_modifier == M_HIDE) {
1898 /* Switching to hide mode, register for keyevents */
1899 register_xkb_keyevents();
1901 /* Switching to dock/invisible mode, deregister from keyevents */
1902 deregister_xkb_keyevents();
1906 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") ||
1907 xcb_request_failed(chg_cookie, "Could not change window") ||
1908 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1909 xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") ||
1910 xcb_request_failed(strut_cookie, "Could not set strut") ||
1911 (redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") ||
1912 (config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) {
1920 * Render the bars, with buttons and statusline
1923 void draw_bars(bool unhide) {
1924 DLOG("Drawing bars...\n");
1926 uint32_t full_statusline_width = predict_statusline_length(false);
1927 uint32_t short_statusline_width = predict_statusline_length(true);
1929 i3_output *outputs_walk;
1930 SLIST_FOREACH(outputs_walk, outputs, slist) {
1931 int workspace_width = 0;
1933 if (!outputs_walk->active) {
1934 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1937 if (outputs_walk->bar.id == XCB_NONE) {
1938 /* Oh shit, an active output without an own bar. Create it now! */
1939 reconfig_windows(false);
1942 bool use_focus_colors = output_has_focus(outputs_walk);
1944 /* First things first: clear the backbuffer */
1945 draw_util_clear_surface(xcb_connection, &(outputs_walk->buffer),
1946 (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg));
1948 if (!config.disable_ws) {
1950 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1951 DLOG("Drawing button for WS %s at x = %d, len = %d\n",
1952 i3string_as_utf8(ws_walk->name), workspace_width, ws_walk->name_width);
1953 color_t fg_color = colors.inactive_ws_fg;
1954 color_t bg_color = colors.inactive_ws_bg;
1955 color_t border_color = colors.inactive_ws_border;
1956 if (ws_walk->visible) {
1957 if (!ws_walk->focused) {
1958 fg_color = colors.active_ws_fg;
1959 bg_color = colors.active_ws_bg;
1960 border_color = colors.active_ws_border;
1962 fg_color = colors.focus_ws_fg;
1963 bg_color = colors.focus_ws_bg;
1964 border_color = colors.focus_ws_border;
1967 if (ws_walk->urgent) {
1968 DLOG("WS %s is urgent!\n", i3string_as_utf8(ws_walk->name));
1969 fg_color = colors.urgent_ws_fg;
1970 bg_color = colors.urgent_ws_bg;
1971 border_color = colors.urgent_ws_border;
1975 /* Draw the border of the button. */
1976 draw_util_rectangle(xcb_connection, &(outputs_walk->buffer), border_color,
1979 ws_walk->name_width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
1980 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1));
1982 /* Draw the inside of the button. */
1983 draw_util_rectangle(xcb_connection, &(outputs_walk->buffer), bg_color,
1984 workspace_width + logical_px(1),
1986 ws_walk->name_width + 2 * logical_px(ws_hoff_px),
1987 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1));
1989 draw_util_text(ws_walk->name, &(outputs_walk->buffer), fg_color, bg_color,
1990 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
1991 logical_px(ws_voff_px),
1992 ws_walk->name_width);
1994 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
1995 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
1996 workspace_width += logical_px(ws_spacing_px);
2000 if (binding.name && !config.disable_binding_mode_indicator) {
2001 workspace_width += logical_px(ws_spacing_px);
2003 color_t fg_color = colors.binding_mode_fg;
2004 color_t bg_color = colors.binding_mode_bg;
2006 draw_util_rectangle(xcb_connection, &(outputs_walk->buffer), colors.binding_mode_border,
2009 binding.width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
2010 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1));
2012 draw_util_rectangle(xcb_connection, &(outputs_walk->buffer), bg_color,
2013 workspace_width + logical_px(1),
2015 binding.width + 2 * logical_px(ws_hoff_px),
2016 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1));
2018 draw_util_text(binding.name, &(outputs_walk->buffer), fg_color, bg_color,
2019 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
2020 logical_px(ws_voff_px),
2024 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + binding.width;
2027 if (!TAILQ_EMPTY(&statusline_head)) {
2028 DLOG("Printing statusline!\n");
2030 int tray_width = get_tray_width(outputs_walk->trayclients);
2031 uint32_t max_statusline_width = outputs_walk->rect.w - workspace_width - tray_width - 2 * logical_px(sb_hoff_px);
2032 uint32_t clip_left = 0;
2033 uint32_t statusline_width = full_statusline_width;
2034 bool use_short_text = false;
2036 if (statusline_width > max_statusline_width) {
2037 statusline_width = short_statusline_width;
2038 use_short_text = true;
2039 if (statusline_width > max_statusline_width) {
2040 clip_left = statusline_width - max_statusline_width;
2044 int16_t visible_statusline_width = MIN(statusline_width, max_statusline_width);
2045 int x_dest = outputs_walk->rect.w - tray_width - logical_px(sb_hoff_px) - visible_statusline_width;
2047 draw_statusline(outputs_walk, clip_left, use_focus_colors, use_short_text);
2048 draw_util_copy_surface(xcb_connection, &outputs_walk->statusline_buffer, &outputs_walk->buffer, 0, 0,
2049 x_dest, 0, visible_statusline_width, (int16_t)bar_height);
2051 outputs_walk->statusline_width = statusline_width;
2052 outputs_walk->statusline_short_text = use_short_text;
2056 /* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */
2058 config.hidden_state == S_SHOW ||
2061 } else if (config.hide_on_modifier == M_HIDE) {
2069 * Redraw the bars, i.e. simply copy the buffer to the barwindow
2072 void redraw_bars(void) {
2073 i3_output *outputs_walk;
2074 SLIST_FOREACH(outputs_walk, outputs, slist) {
2075 if (!outputs_walk->active) {
2079 draw_util_copy_surface(xcb_connection, &(outputs_walk->buffer), &(outputs_walk->bar), 0, 0,
2080 0, 0, outputs_walk->rect.w, outputs_walk->rect.h);
2081 xcb_flush(xcb_connection);
2086 * Set the current binding mode
2089 void set_current_mode(struct mode *current) {
2090 I3STRING_FREE(binding.name);
2092 activated_mode = binding.name != NULL;