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>
16 #include "xcb_compat.h"
31 #include <X11/XKBlib.h>
32 #include <X11/extensions/XKB.h>
37 /* We save the atoms in an easy to access array, indexed by an enum */
39 #define ATOM_DO(name) name,
40 #include "xcb_atoms.def"
44 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
45 xcb_atom_t atoms[NUM_ATOMS];
47 /* Variables, that are the same for all functions at all times */
48 xcb_connection_t *xcb_connection;
50 xcb_screen_t *root_screen;
51 xcb_window_t xcb_root;
53 /* selection window for tray support */
54 static xcb_window_t selwin = XCB_NONE;
55 static xcb_intern_atom_reply_t *tray_reply = NULL;
57 /* This is needed for integration with libi3 */
58 xcb_connection_t *conn;
60 /* The font we'll use */
63 /* Icon size (based on font size) */
66 /* Overall height of the bar (based on font size) */
69 /* These are only relevant for XKB, which we only need for grabbing modifiers */
73 /* Because the statusline is the same on all outputs, we have
74 * global buffer to render it on */
75 xcb_gcontext_t statusline_ctx;
76 xcb_gcontext_t statusline_clear;
77 xcb_pixmap_t statusline_pm;
78 uint32_t statusline_width;
80 /* Event watchers, to interact with the user */
86 /* The name of current binding mode */
89 /* Indicates whether a new binding mode was recently activated */
90 bool activated_mode = false;
92 /* The parsed colors */
97 uint32_t active_ws_fg;
98 uint32_t active_ws_bg;
99 uint32_t active_ws_border;
100 uint32_t inactive_ws_fg;
101 uint32_t inactive_ws_bg;
102 uint32_t inactive_ws_border;
103 uint32_t urgent_ws_bg;
104 uint32_t urgent_ws_fg;
105 uint32_t urgent_ws_border;
106 uint32_t focus_ws_bg;
107 uint32_t focus_ws_fg;
108 uint32_t focus_ws_border;
109 uint32_t binding_mode_bg;
110 uint32_t binding_mode_fg;
111 uint32_t binding_mode_border;
113 struct xcb_colors_t colors;
115 /* Horizontal offset between a workspace label and button borders */
116 static const int ws_hoff_px = 4;
118 /* Vertical offset between a workspace label and button borders */
119 static const int ws_voff_px = 3;
121 /* Offset between two workspace buttons */
122 static const int ws_spacing_px = 1;
124 /* Offset between the statusline and 1) workspace buttons on the left
125 * 2) the tray or screen edge on the right */
126 static const int sb_hoff_px = 4;
128 /* Additional offset between the tray and the statusline, if the tray is not empty */
129 static const int tray_loff_px = 2;
131 /* Padding around the tray icons */
132 static const int tray_spacing_px = 2;
134 /* Vertical offset between the bar and a separator */
135 static const int sep_voff_px = 4;
137 /* We define xcb_request_failed as a macro to include the relevant line number */
138 #define xcb_request_failed(cookie, err_msg) _xcb_request_failed(cookie, err_msg, __LINE__)
139 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
140 xcb_generic_error_t *err;
141 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
142 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
143 return err->error_code;
148 uint32_t get_sep_offset(struct status_block *block) {
149 if (!block->no_separator && block->sep_block_width > 0)
150 return block->sep_block_width / 2 + block->sep_block_width % 2;
154 int get_tray_width(struct tc_head *trayclients) {
155 trayclient *trayclient;
157 TAILQ_FOREACH_REVERSE(trayclient, trayclients, tc_head, tailq) {
158 if (!trayclient->mapped)
160 tray_width += icon_size + logical_px(tray_spacing_px);
163 tray_width += logical_px(tray_loff_px);
168 * Draws a separator for the given block if necessary.
171 static void draw_separator(uint32_t x, struct status_block *block) {
172 uint32_t sep_offset = get_sep_offset(block);
173 if (TAILQ_NEXT(block, blocks) == NULL || sep_offset == 0)
176 uint32_t center_x = x - sep_offset;
177 if (config.separator_symbol == NULL) {
178 /* Draw a classic one pixel, vertical separator. */
179 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_LINE_WIDTH;
180 uint32_t values[] = {colors.sep_fg, colors.bar_bg, logical_px(1)};
181 xcb_change_gc(xcb_connection, statusline_ctx, mask, values);
182 xcb_poly_line(xcb_connection, XCB_COORD_MODE_ORIGIN, statusline_pm, statusline_ctx, 2,
183 (xcb_point_t[]){{center_x, logical_px(sep_voff_px)},
184 {center_x, bar_height - logical_px(sep_voff_px)}});
186 /* Draw a custom separator. */
187 uint32_t separator_x = MAX(x - block->sep_block_width, center_x - separator_symbol_width / 2);
188 set_font_colors(statusline_ctx, colors.sep_fg, colors.bar_bg);
189 draw_text(config.separator_symbol, statusline_pm, statusline_ctx,
190 separator_x, logical_px(ws_voff_px), x - separator_x);
195 * Redraws the statusline to the buffer
198 void refresh_statusline(bool use_short_text) {
199 struct status_block *block;
201 uint32_t old_statusline_width = statusline_width;
202 statusline_width = 0;
204 /* Predict the text width of all blocks (in pixels). */
205 TAILQ_FOREACH(block, &statusline_head, blocks) {
206 /* Try to use the shorter text if necessary and possible. */
207 if (use_short_text && block->short_text != NULL) {
208 I3STRING_FREE(block->full_text);
209 block->full_text = i3string_copy(block->short_text);
212 if (i3string_get_num_bytes(block->full_text) == 0)
215 block->width = predict_text_width(block->full_text);
217 /* Compute offset and append for text aligment in min_width. */
218 if (block->min_width <= block->width) {
222 uint32_t padding_width = block->min_width - block->width;
223 switch (block->align) {
225 block->x_append = padding_width;
228 block->x_offset = padding_width;
231 block->x_offset = padding_width / 2;
232 block->x_append = padding_width / 2 + padding_width % 2;
237 /* If this is not the last block, add some pixels for a separator. */
238 if (TAILQ_NEXT(block, blocks) != NULL)
239 statusline_width += block->sep_block_width;
241 statusline_width += block->width + block->x_offset + block->x_append;
244 /* If the statusline is bigger than our screen we need to make sure that
245 * the pixmap provides enough space, so re-allocate if the width grew */
246 if (statusline_width > root_screen->width_in_pixels &&
247 statusline_width > old_statusline_width)
250 /* Clear the statusline pixmap. */
251 xcb_rectangle_t rect = {0, 0, MAX(root_screen->width_in_pixels, statusline_width), bar_height};
252 xcb_poly_fill_rectangle(xcb_connection, statusline_pm, statusline_clear, 1, &rect);
254 /* Draw the text of each block. */
256 TAILQ_FOREACH(block, &statusline_head, blocks) {
257 if (i3string_get_num_bytes(block->full_text) == 0)
261 /* If this block is urgent, draw it with the defined color and border. */
263 fg_color = colors.urgent_ws_fg;
265 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
267 /* Draw the background */
268 uint32_t bg_color = colors.urgent_ws_bg;
269 uint32_t bg_values[] = {bg_color, bg_color};
270 xcb_change_gc(xcb_connection, statusline_ctx, mask, bg_values);
272 /* The urgent background “overshoots” by 2 px so that the text that
273 * is printed onto it will not be look so cut off. */
274 xcb_rectangle_t bg_rect = {x - logical_px(2), logical_px(1), block->width + logical_px(4), bar_height - logical_px(2)};
275 xcb_poly_fill_rectangle(xcb_connection, statusline_pm, statusline_ctx, 1, &bg_rect);
277 fg_color = (block->color ? get_colorpixel(block->color) : colors.bar_fg);
280 set_font_colors(statusline_ctx, fg_color, colors.bar_bg);
281 draw_text(block->full_text, statusline_pm, statusline_ctx, x + block->x_offset, logical_px(ws_voff_px), block->width);
282 x += block->width + block->sep_block_width + block->x_offset + block->x_append;
284 /* If this is not the last block, draw a separator. */
285 draw_separator(x, block);
290 * Hides all bars (unmaps them)
293 void hide_bars(void) {
294 if ((config.hide_on_modifier == M_DOCK) || (config.hidden_state == S_SHOW && config.hide_on_modifier == M_HIDE)) {
299 SLIST_FOREACH(walk, outputs, slist) {
303 xcb_unmap_window(xcb_connection, walk->bar);
309 * Unhides all bars (maps them)
312 void unhide_bars(void) {
313 if (config.hide_on_modifier != M_HIDE) {
318 xcb_void_cookie_t cookie;
324 SLIST_FOREACH(walk, outputs, slist) {
325 if (walk->bar == XCB_NONE) {
328 mask = XCB_CONFIG_WINDOW_X |
329 XCB_CONFIG_WINDOW_Y |
330 XCB_CONFIG_WINDOW_WIDTH |
331 XCB_CONFIG_WINDOW_HEIGHT |
332 XCB_CONFIG_WINDOW_STACK_MODE;
333 values[0] = walk->rect.x;
334 if (config.position == POS_TOP)
335 values[1] = walk->rect.y;
337 values[1] = walk->rect.y + walk->rect.h - bar_height;
338 values[2] = walk->rect.w;
339 values[3] = bar_height;
340 values[4] = XCB_STACK_MODE_ABOVE;
341 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
342 cookie = xcb_configure_window_checked(xcb_connection,
347 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
350 xcb_map_window(xcb_connection, walk->bar);
355 * Parse the colors into a format that we can use
358 void init_colors(const struct xcb_color_strings_t *new_colors) {
359 #define PARSE_COLOR(name, def) \
361 colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
363 PARSE_COLOR(bar_fg, "#FFFFFF");
364 PARSE_COLOR(bar_bg, "#000000");
365 PARSE_COLOR(sep_fg, "#666666");
366 PARSE_COLOR(active_ws_fg, "#FFFFFF");
367 PARSE_COLOR(active_ws_bg, "#333333");
368 PARSE_COLOR(active_ws_border, "#333333");
369 PARSE_COLOR(inactive_ws_fg, "#888888");
370 PARSE_COLOR(inactive_ws_bg, "#222222");
371 PARSE_COLOR(inactive_ws_border, "#333333");
372 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
373 PARSE_COLOR(urgent_ws_bg, "#900000");
374 PARSE_COLOR(urgent_ws_border, "#2f343a");
375 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
376 PARSE_COLOR(focus_ws_bg, "#285577");
377 PARSE_COLOR(focus_ws_border, "#4c7899");
380 #define PARSE_COLOR_FALLBACK(name, fallback) \
382 colors.name = new_colors->name ? get_colorpixel(new_colors->name) : colors.fallback; \
385 /* For the binding mode indicator colors, we don't hardcode a default.
386 * Instead, we fall back to urgent_ws_* colors. */
387 PARSE_COLOR_FALLBACK(binding_mode_fg, urgent_ws_fg);
388 PARSE_COLOR_FALLBACK(binding_mode_bg, urgent_ws_bg);
389 PARSE_COLOR_FALLBACK(binding_mode_border, urgent_ws_border);
390 #undef PARSE_COLOR_FALLBACK
393 xcb_flush(xcb_connection);
397 * Handle a button press event (i.e. a mouse click on one of our bars).
398 * We determine, whether the click occured on a workspace button or if the scroll-
399 * wheel was used and change the workspace appropriately
402 void handle_button(xcb_button_press_event_t *event) {
403 /* Determine, which bar was clicked */
405 xcb_window_t bar = event->event;
406 SLIST_FOREACH(walk, outputs, slist) {
407 if (walk->bar == bar) {
413 DLOG("Unknown bar clicked!\n");
417 int32_t x = event->event_x >= 0 ? event->event_x : 0;
418 int32_t original_x = x;
420 DLOG("Got button %d\n", event->detail);
422 int workspace_width = 0;
423 i3_ws *cur_ws = NULL, *clicked_ws = NULL, *ws_walk;
425 TAILQ_FOREACH(ws_walk, walk->workspaces, tailq) {
426 int w = 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
427 if (x >= workspace_width && x <= workspace_width + w)
428 clicked_ws = ws_walk;
429 if (ws_walk->visible)
431 workspace_width += w;
432 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
433 workspace_width += logical_px(ws_spacing_px);
436 if (x > workspace_width && child_want_click_events()) {
437 /* If the child asked for click events,
438 * check if a status block has been clicked. */
439 int tray_width = get_tray_width(walk->trayclients);
440 int block_x = 0, last_block_x;
441 int offset = walk->rect.w - statusline_width - tray_width - logical_px(sb_hoff_px);
443 x = original_x - offset;
444 if (x >= 0 && (size_t)x < statusline_width) {
445 struct status_block *block;
446 int sep_offset_remainder = 0;
448 TAILQ_FOREACH(block, &statusline_head, blocks) {
449 if (i3string_get_num_bytes(block->full_text) == 0)
452 last_block_x = block_x;
453 block_x += block->width + block->x_offset + block->x_append + get_sep_offset(block) + sep_offset_remainder;
455 if (x <= block_x && x >= last_block_x) {
456 send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y);
460 sep_offset_remainder = block->sep_block_width - get_sep_offset(block);
466 if (cur_ws == NULL) {
467 DLOG("No workspace active?\n");
471 /* If a custom command was specified for this mouse button, it overrides
472 * the default behavior. */
473 mouse_command_t *command;
474 TAILQ_FOREACH(command, &(config.mouse_commands), commands) {
475 if (command->button != event->detail)
478 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, command->command);
482 switch (event->detail) {
484 /* Mouse wheel up. We select the previous ws, if any.
485 * If there is no more workspace, don’t even send the workspace
486 * command, otherwise (with workspace auto_back_and_forth) we’d end
487 * up on the wrong workspace. */
488 if (cur_ws == TAILQ_FIRST(walk->workspaces))
491 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
494 /* Mouse wheel down. We select the next ws, if any.
495 * If there is no more workspace, don’t even send the workspace
496 * command, otherwise (with workspace auto_back_and_forth) we’d end
497 * up on the wrong workspace. */
498 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
501 cur_ws = TAILQ_NEXT(cur_ws, tailq);
506 /* if no workspace was clicked, focus our currently visible
507 * workspace if it is not already focused */
508 if (cur_ws == NULL) {
509 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
510 if (cur_ws->visible && !cur_ws->focused)
515 /* if there is nothing to focus, we are done */
524 /* To properly handle workspace names with double quotes in them, we need
525 * to escape the double quotes. Unfortunately, that’s rather ugly in C: We
526 * first count the number of double quotes, then we allocate a large enough
527 * buffer, then we copy character by character. */
530 const char *utf8_name = cur_ws->canonical_name;
531 for (const char *walk = utf8_name; *walk != '\0'; walk++) {
532 if (*walk == '"' || *walk == '\\')
534 /* While we’re looping through the name anyway, we can save one
539 const size_t len = namelen + strlen("workspace \"\"") + 1;
540 char *buffer = scalloc(len + num_quotes);
541 strncpy(buffer, "workspace \"", strlen("workspace \""));
542 size_t inpos, outpos;
543 for (inpos = 0, outpos = strlen("workspace \"");
546 if (utf8_name[inpos] == '"' || utf8_name[inpos] == '\\') {
547 buffer[outpos] = '\\';
550 buffer[outpos] = utf8_name[inpos];
552 buffer[outpos] = '"';
553 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
558 * Handle visibility notifications: when none of the bars are visible, e.g.
559 * if windows are in fullscreen on each output, suspend the child process.
562 static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
563 bool visible = (event->state != XCB_VISIBILITY_FULLY_OBSCURED);
567 SLIST_FOREACH(output, outputs, slist) {
568 if (!output->active) {
571 if (output->bar == event->window) {
572 if (output->visible == visible) {
575 output->visible = visible;
577 num_visible += output->visible;
580 if (num_visible == 0) {
582 } else if (num_visible == visible) {
583 /* Wake the child only when transitioning from 0 to 1 visible bar.
584 * We cannot transition from 0 to 2 or more visible bars at once since
585 * visibility events are delivered to each window separately */
591 * Adjusts the size of the tray window and alignment of the tray clients by
592 * configuring their respective x coordinates. To be called when mapping or
593 * unmapping a tray client window.
596 static void configure_trayclients(void) {
597 trayclient *trayclient;
599 SLIST_FOREACH(output, outputs, slist) {
604 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
605 if (!trayclient->mapped)
609 DLOG("Configuring tray window %08x to x=%d\n",
610 trayclient->win, output->rect.w - (clients * (icon_size + logical_px(tray_spacing_px))));
611 uint32_t x = output->rect.w - (clients * (icon_size + logical_px(tray_spacing_px)));
612 xcb_configure_window(xcb_connection,
621 * Handles ClientMessages (messages sent from another client directly to us).
623 * At the moment, only the tray window will receive client messages. All
624 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
627 static void handle_client_message(xcb_client_message_event_t *event) {
628 if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
629 event->format == 32) {
630 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
631 /* event->data.data32[0] is the timestamp */
632 uint32_t op = event->data.data32[1];
635 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
636 xcb_window_t client = event->data.data32[2];
638 /* Listen for PropertyNotify events to get the most recent value of
639 * the XEMBED_MAPPED atom, also listen for UnmapNotify events */
640 mask = XCB_CW_EVENT_MASK;
641 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
642 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
643 xcb_change_window_attributes(xcb_connection,
648 /* Request the _XEMBED_INFO property. The XEMBED specification
649 * (which is referred by the tray specification) says this *has* to
650 * be set, but VLC does not set it… */
653 xcb_get_property_cookie_t xembedc;
654 xcb_generic_error_t *error;
655 xembedc = xcb_get_property(xcb_connection,
659 XCB_GET_PROPERTY_TYPE_ANY,
663 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
667 ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
672 if (xembedr != NULL && xembedr->length != 0) {
673 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
674 uint32_t *xembed = xcb_get_property_value(xembedr);
675 DLOG("xembed version = %d\n", xembed[0]);
676 DLOG("xembed flags = %d\n", xembed[1]);
677 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
678 xe_version = xembed[0];
683 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
686 DLOG("X window %08x requested docking\n", client);
687 i3_output *walk, *output = NULL;
688 SLIST_FOREACH(walk, outputs, slist) {
691 if (config.tray_output) {
692 if ((strcasecmp(walk->name, config.tray_output) != 0) &&
693 (!walk->primary || strcasecmp("primary", config.tray_output) != 0))
697 DLOG("using output %s\n", walk->name);
701 /* In case of tray_output == primary and there is no primary output
702 * configured, we fall back to the first available output. */
703 if (output == NULL &&
704 config.tray_output &&
705 strcasecmp("primary", config.tray_output) == 0) {
706 SLIST_FOREACH(walk, outputs, slist) {
709 DLOG("Falling back to output %s because no primary output is configured\n", walk->name);
714 if (output == NULL) {
715 ELOG("No output found\n");
718 xcb_reparent_window(xcb_connection,
721 output->rect.w - icon_size - logical_px(tray_spacing_px),
722 logical_px(tray_spacing_px));
723 /* We reconfigure the window to use a reasonable size. The systray
724 * specification explicitly says:
725 * Tray icons may be assigned any size by the system tray, and
726 * should do their best to cope with any size effectively
728 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
729 values[0] = icon_size;
730 values[1] = icon_size;
731 xcb_configure_window(xcb_connection,
736 /* send the XEMBED_EMBEDDED_NOTIFY message */
737 void *event = scalloc(32);
738 xcb_client_message_event_t *ev = event;
739 ev->response_type = XCB_CLIENT_MESSAGE;
741 ev->type = atoms[_XEMBED];
743 ev->data.data32[0] = XCB_CURRENT_TIME;
744 ev->data.data32[1] = atoms[XEMBED_EMBEDDED_NOTIFY];
745 ev->data.data32[2] = output->bar;
746 ev->data.data32[3] = xe_version;
747 xcb_send_event(xcb_connection,
750 XCB_EVENT_MASK_NO_EVENT,
754 /* Put the client inside the save set. Upon termination (whether
755 * killed or normal exit does not matter) of i3bar, these clients
756 * will be correctly reparented to their most closest living
757 * ancestor. Without this, tray icons might die when i3bar
759 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
761 trayclient *tc = smalloc(sizeof(trayclient));
763 tc->xe_version = xe_version;
765 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
768 DLOG("Mapping dock client\n");
769 xcb_map_window(xcb_connection, client);
771 DLOG("Not mapping dock client yet\n");
773 /* Trigger an update to copy the statusline text to the appropriate
775 configure_trayclients();
782 * Handles DestroyNotify events by removing the tray client from the data
783 * structure. According to the XEmbed protocol, this is one way for a tray
784 * client to finish the protocol. After this event is received, there is no
785 * further interaction with the tray client.
787 * See: http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
790 static void handle_destroy_notify(xcb_destroy_notify_event_t *event) {
791 DLOG("DestroyNotify for window = %08x, event = %08x\n", event->window, event->event);
794 SLIST_FOREACH(walk, outputs, slist) {
797 DLOG("checking output %s\n", walk->name);
798 trayclient *trayclient;
799 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
800 if (trayclient->win != event->window)
803 DLOG("Removing tray client with window ID %08x\n", event->window);
804 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
806 /* Trigger an update, we now have more space for the statusline */
807 configure_trayclients();
815 * Handles MapNotify events. These events happen when a tray client shows its
816 * window. We respond by realigning the tray clients.
819 static void handle_map_notify(xcb_map_notify_event_t *event) {
820 DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event);
823 SLIST_FOREACH(walk, outputs, slist) {
826 DLOG("checking output %s\n", walk->name);
827 trayclient *trayclient;
828 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
829 if (trayclient->win != event->window)
832 DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window);
833 trayclient->mapped = true;
835 /* Trigger an update, we now have more space for the statusline */
836 configure_trayclients();
843 * Handles UnmapNotify events. These events happen when a tray client hides its
844 * window. We respond by realigning the tray clients.
847 static void handle_unmap_notify(xcb_unmap_notify_event_t *event) {
848 DLOG("UnmapNotify 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)
860 DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window);
861 trayclient->mapped = false;
863 /* Trigger an update, we now have more space for the statusline */
864 configure_trayclients();
872 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
873 * handled, which tells us whether a dock client should be mapped or unmapped.
876 static void handle_property_notify(xcb_property_notify_event_t *event) {
877 DLOG("PropertyNotify\n");
878 if (event->atom == atoms[_XEMBED_INFO] &&
879 event->state == XCB_PROPERTY_NEW_VALUE) {
880 DLOG("xembed_info updated\n");
881 trayclient *trayclient = NULL, *walk;
883 SLIST_FOREACH(o_walk, outputs, slist) {
887 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
888 if (walk->win != event->window)
898 ELOG("PropertyNotify received for unknown window %08x\n",
902 xcb_get_property_cookie_t xembedc;
903 xembedc = xcb_get_property_unchecked(xcb_connection,
907 XCB_GET_PROPERTY_TYPE_ANY,
911 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
914 if (xembedr == NULL || xembedr->length == 0) {
915 DLOG("xembed_info unset\n");
919 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
920 uint32_t *xembed = xcb_get_property_value(xembedr);
921 DLOG("xembed version = %d\n", xembed[0]);
922 DLOG("xembed flags = %d\n", xembed[1]);
923 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
924 DLOG("map state now %d\n", map_it);
925 if (trayclient->mapped && !map_it) {
926 /* need to unmap the window */
927 xcb_unmap_window(xcb_connection, trayclient->win);
928 } else if (!trayclient->mapped && map_it) {
929 /* need to map the window */
930 xcb_map_window(xcb_connection, trayclient->win);
937 * Handle ConfigureRequests by denying them and sending the client a
938 * ConfigureNotify with its actual size.
941 static void handle_configure_request(xcb_configure_request_event_t *event) {
942 DLOG("ConfigureRequest for window = %08x\n", event->window);
944 trayclient *trayclient;
946 SLIST_FOREACH(output, outputs, slist) {
951 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
952 if (!trayclient->mapped)
956 if (trayclient->win != event->window)
959 xcb_rectangle_t rect;
960 rect.x = output->rect.w - (clients * (icon_size + logical_px(tray_spacing_px)));
961 rect.y = logical_px(tray_spacing_px);
962 rect.width = icon_size;
963 rect.height = icon_size;
965 DLOG("This is a tray window. x = %d\n", rect.x);
966 fake_configure_notify(xcb_connection, rect, event->window, 0);
971 DLOG("WARNING: Could not find corresponding tray window.\n");
975 * This function is called immediately before the main loop locks. We flush xcb
976 * then (and only then)
979 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
980 xcb_flush(xcb_connection);
984 * This function is called immediately after the main loop locks, so when one
985 * of the watchers registered an event.
986 * We check whether an X-Event arrived and handle it.
989 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
990 xcb_generic_event_t *event;
992 if (xcb_connection_has_error(xcb_connection)) {
993 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
997 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
998 int type = (event->response_type & ~0x80);
1000 if (type == xkb_base && xkb_base > -1) {
1001 DLOG("received an xkb event\n");
1003 xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event;
1004 if (state->xkbType == XCB_XKB_STATE_NOTIFY) {
1005 int modstate = state->mods & config.modifier;
1007 #define DLOGMOD(modmask, status) \
1009 switch (modmask) { \
1011 DLOG("ShiftMask got " #status "!\n"); \
1014 DLOG("ControlMask got " #status "!\n"); \
1017 DLOG("Mod1Mask got " #status "!\n"); \
1020 DLOG("Mod2Mask got " #status "!\n"); \
1023 DLOG("Mod3Mask got " #status "!\n"); \
1026 DLOG("Mod4Mask got " #status "!\n"); \
1029 DLOG("Mod5Mask got " #status "!\n"); \
1034 if (modstate != mod_pressed) {
1035 if (modstate == 0) {
1036 DLOGMOD(config.modifier, released);
1037 if (!activated_mode)
1040 DLOGMOD(config.modifier, pressed);
1041 activated_mode = false;
1044 mod_pressed = modstate;
1054 case XCB_VISIBILITY_NOTIFY:
1055 /* Visibility change: a bar is [un]obscured by other window */
1056 handle_visibility_notify((xcb_visibility_notify_event_t *)event);
1059 /* Expose-events happen, when the window needs to be redrawn */
1062 case XCB_BUTTON_PRESS:
1063 /* Button press events are mouse buttons clicked on one of our bars */
1064 handle_button((xcb_button_press_event_t *)event);
1066 case XCB_CLIENT_MESSAGE:
1067 /* Client messages are used for client-to-client communication, for
1068 * example system tray widgets talk to us directly via client messages. */
1069 handle_client_message((xcb_client_message_event_t *)event);
1071 case XCB_DESTROY_NOTIFY:
1072 /* DestroyNotify signifies the end of the XEmbed protocol */
1073 handle_destroy_notify((xcb_destroy_notify_event_t *)event);
1075 case XCB_UNMAP_NOTIFY:
1076 /* UnmapNotify is received when a tray client hides its window. */
1077 handle_unmap_notify((xcb_unmap_notify_event_t *)event);
1079 case XCB_MAP_NOTIFY:
1080 handle_map_notify((xcb_map_notify_event_t *)event);
1082 case XCB_PROPERTY_NOTIFY:
1083 /* PropertyNotify */
1084 handle_property_notify((xcb_property_notify_event_t *)event);
1086 case XCB_CONFIGURE_REQUEST:
1087 /* ConfigureRequest, sent by a tray child */
1088 handle_configure_request((xcb_configure_request_event_t *)event);
1096 * Dummy callback. We only need this, so that the prepare and check watchers
1100 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
1104 * Early initialization of the connection to X11: Everything which does not
1105 * depend on 'config'.
1108 char *init_xcb_early() {
1109 /* FIXME: xcb_connect leaks memory */
1110 xcb_connection = xcb_connect(NULL, &screen);
1111 if (xcb_connection_has_error(xcb_connection)) {
1112 ELOG("Cannot open display\n");
1115 conn = xcb_connection;
1116 DLOG("Connected to xcb\n");
1118 /* We have to request the atoms we need */
1119 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
1120 #include "xcb_atoms.def"
1122 root_screen = xcb_aux_get_screen(xcb_connection, screen);
1123 xcb_root = root_screen->root;
1125 /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
1126 * this way, we can choose to crop it */
1127 uint32_t mask = XCB_GC_FOREGROUND;
1128 uint32_t vals[] = {colors.bar_bg, colors.bar_bg};
1130 statusline_clear = xcb_generate_id(xcb_connection);
1131 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1137 statusline_ctx = xcb_generate_id(xcb_connection);
1138 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1144 statusline_pm = xcb_generate_id(xcb_connection);
1145 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1146 root_screen->root_depth,
1149 root_screen->width_in_pixels,
1150 root_screen->height_in_pixels);
1152 /* The various watchers to communicate with xcb */
1153 xcb_io = smalloc(sizeof(ev_io));
1154 xcb_prep = smalloc(sizeof(ev_prepare));
1155 xcb_chk = smalloc(sizeof(ev_check));
1157 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
1158 ev_prepare_init(xcb_prep, &xcb_prep_cb);
1159 ev_check_init(xcb_chk, &xcb_chk_cb);
1161 ev_io_start(main_loop, xcb_io);
1162 ev_prepare_start(main_loop, xcb_prep);
1163 ev_check_start(main_loop, xcb_chk);
1165 /* Now we get the atoms and save them in a nice data structure */
1168 char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen);
1170 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline buffer") ||
1171 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline buffer clearcontext") ||
1172 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline buffer context")) {
1180 * Register for xkb keyevents. To grab modifiers without blocking other applications from receiving key events
1181 * involving that modifier, we sadly have to use xkb which is not yet fully supported
1185 void register_xkb_keyevents() {
1186 const xcb_query_extension_reply_t *extreply;
1187 extreply = xcb_get_extension_data(conn, &xcb_xkb_id);
1188 if (!extreply->present) {
1189 ELOG("xkb is not present on this server\n");
1192 DLOG("initializing xcb-xkb\n");
1193 xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
1194 xcb_xkb_select_events(conn,
1195 XCB_XKB_ID_USE_CORE_KBD,
1196 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1198 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1202 xkb_base = extreply->first_event;
1206 * Deregister from xkb keyevents.
1209 void deregister_xkb_keyevents() {
1210 xcb_xkb_select_events(conn,
1211 XCB_XKB_ID_USE_CORE_KBD,
1221 * Initialization which depends on 'config' being usable. Called after the
1222 * configuration has arrived.
1225 void init_xcb_late(char *fontname) {
1226 if (fontname == NULL)
1227 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
1230 font = load_font(fontname, true);
1232 DLOG("Calculated font height: %d\n", font.height);
1233 bar_height = font.height + 2 * logical_px(ws_voff_px);
1234 icon_size = bar_height - 2 * logical_px(tray_spacing_px);
1236 if (config.separator_symbol)
1237 separator_symbol_width = predict_text_width(config.separator_symbol);
1239 xcb_flush(xcb_connection);
1241 if (config.hide_on_modifier == M_HIDE)
1242 register_xkb_keyevents();
1246 * Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the
1250 static void send_tray_clientmessage(void) {
1251 uint8_t buffer[32] = {0};
1252 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)buffer;
1254 ev->response_type = XCB_CLIENT_MESSAGE;
1255 ev->window = xcb_root;
1256 ev->type = atoms[MANAGER];
1258 ev->data.data32[0] = XCB_CURRENT_TIME;
1259 ev->data.data32[1] = tray_reply->atom;
1260 ev->data.data32[2] = selwin;
1262 xcb_send_event(xcb_connection,
1270 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
1271 * for the X11 display we are running on, then acquiring the selection for this
1272 * atom. Afterwards, tray clients will send ClientMessages to our window.
1275 void init_tray(void) {
1276 DLOG("Initializing system tray functionality\n");
1277 /* request the tray manager atom for the X11 display we are running on */
1278 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
1279 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
1280 xcb_intern_atom_cookie_t tray_cookie;
1281 if (tray_reply == NULL)
1282 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
1284 /* tray support: we need a window to own the selection */
1285 selwin = xcb_generate_id(xcb_connection);
1286 uint32_t selmask = XCB_CW_OVERRIDE_REDIRECT;
1287 uint32_t selval[] = {1};
1288 xcb_create_window(xcb_connection,
1289 root_screen->root_depth,
1295 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1296 root_screen->root_visual,
1300 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
1302 xcb_change_property(xcb_connection,
1303 XCB_PROP_MODE_REPLACE,
1305 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1313 if (tray_reply == NULL) {
1314 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1315 ELOG("Could not get atom %s\n", atomname);
1320 xcb_set_selection_owner(xcb_connection,
1325 /* Verify that we have the selection */
1326 xcb_get_selection_owner_cookie_t selcookie;
1327 xcb_get_selection_owner_reply_t *selreply;
1329 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1330 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1331 ELOG("Could not get selection owner for %s\n", atomname);
1335 if (selreply->owner != selwin) {
1336 ELOG("Could not set the %s selection. "
1337 "Maybe another tray is already running?\n",
1339 /* NOTE that this error is not fatal. We just can’t provide tray
1345 send_tray_clientmessage();
1349 * We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window
1350 * to make GTK+ 3 applets with symbolic icons visible. If the colors are unset,
1351 * they assume a light background.
1352 * See also https://bugzilla.gnome.org/show_bug.cgi?id=679591
1355 void init_tray_colors(void) {
1356 /* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */
1357 const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF");
1359 DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg);
1361 char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'},
1362 {bar_fg[3], bar_fg[4], '\0'},
1363 {bar_fg[5], bar_fg[6], '\0'}};
1364 const uint8_t r = strtol(strgroups[0], NULL, 16);
1365 const uint8_t g = strtol(strgroups[1], NULL, 16);
1366 const uint8_t b = strtol(strgroups[2], NULL, 16);
1368 const uint16_t r16 = ((uint16_t)r << 8) | r;
1369 const uint16_t g16 = ((uint16_t)g << 8) | g;
1370 const uint16_t b16 = ((uint16_t)b << 8) | b;
1372 const uint32_t tray_colors[12] = {
1373 r16, g16, b16, /* foreground color */
1374 r16, g16, b16, /* error color */
1375 r16, g16, b16, /* warning color */
1376 r16, g16, b16, /* success color */
1379 xcb_change_property(xcb_connection,
1380 XCB_PROP_MODE_REPLACE,
1382 atoms[_NET_SYSTEM_TRAY_COLORS],
1390 * Cleanup the xcb stuff.
1391 * Called once, before the program terminates.
1394 void clean_xcb(void) {
1397 SLIST_FOREACH(o_walk, outputs, slist) {
1398 destroy_window(o_walk);
1399 FREE(o_walk->trayclients);
1400 FREE(o_walk->workspaces);
1403 FREE_SLIST(outputs, i3_output);
1406 xcb_flush(xcb_connection);
1407 xcb_aux_sync(xcb_connection);
1408 xcb_disconnect(xcb_connection);
1410 ev_check_stop(main_loop, xcb_chk);
1411 ev_prepare_stop(main_loop, xcb_prep);
1412 ev_io_stop(main_loop, xcb_io);
1420 * Get the earlier requested atoms and save them in the prepared data structure
1423 void get_atoms(void) {
1424 xcb_intern_atom_reply_t *reply;
1425 #define ATOM_DO(name) \
1426 reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1427 if (reply == NULL) { \
1428 ELOG("Could not get atom %s\n", #name); \
1429 exit(EXIT_FAILURE); \
1431 atoms[name] = reply->atom; \
1434 #include "xcb_atoms.def"
1435 DLOG("Got atoms\n");
1439 * Reparents all tray clients of the specified output to the root window. This
1440 * is either used when shutting down, when an output appears (xrandr --output
1441 * VGA1 --off) or when the primary output changes.
1443 * Applications using the tray will start the protocol from the beginning again
1447 void kick_tray_clients(i3_output *output) {
1448 if (TAILQ_EMPTY(output->trayclients))
1451 trayclient *trayclient;
1452 while (!TAILQ_EMPTY(output->trayclients)) {
1453 trayclient = TAILQ_FIRST(output->trayclients);
1454 /* Unmap, then reparent (to root) the tray client windows */
1455 xcb_unmap_window(xcb_connection, trayclient->win);
1456 xcb_reparent_window(xcb_connection,
1462 /* We remove the trayclient right here. We might receive an UnmapNotify
1463 * event afterwards, but better safe than sorry. */
1464 TAILQ_REMOVE(output->trayclients, trayclient, tailq);
1467 /* Fake a DestroyNotify so that Qt re-adds tray icons.
1468 * We cannot actually destroy the window because then Qt will not restore
1469 * its event mask on the new window. */
1470 uint8_t buffer[32] = {0};
1471 xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t *)buffer;
1473 event->response_type = XCB_DESTROY_NOTIFY;
1474 event->event = selwin;
1475 event->window = selwin;
1477 xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)event);
1479 send_tray_clientmessage();
1483 * Destroy the bar of the specified output
1486 void destroy_window(i3_output *output) {
1487 if (output == NULL) {
1490 if (output->bar == XCB_NONE) {
1494 kick_tray_clients(output);
1495 xcb_destroy_window(xcb_connection, output->bar);
1496 output->bar = XCB_NONE;
1500 * Reallocate the statusline buffer
1503 void realloc_sl_buffer(void) {
1504 DLOG("Re-allocating statusline buffer, statusline_width = %d, root_screen->width_in_pixels = %d\n",
1505 statusline_width, root_screen->width_in_pixels);
1506 xcb_free_pixmap(xcb_connection, statusline_pm);
1507 statusline_pm = xcb_generate_id(xcb_connection);
1508 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1509 root_screen->root_depth,
1512 MAX(root_screen->width_in_pixels, statusline_width),
1515 uint32_t mask = XCB_GC_FOREGROUND;
1516 uint32_t vals[2] = {colors.bar_bg, colors.bar_bg};
1517 xcb_free_gc(xcb_connection, statusline_clear);
1518 statusline_clear = xcb_generate_id(xcb_connection);
1519 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1525 mask |= XCB_GC_BACKGROUND;
1526 vals[0] = colors.bar_fg;
1527 xcb_free_gc(xcb_connection, statusline_ctx);
1528 statusline_ctx = xcb_generate_id(xcb_connection);
1529 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1535 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline buffer") ||
1536 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline buffer clearcontext") ||
1537 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline buffer context")) {
1542 /* Strut partial tells i3 where to reserve space for i3bar. This is determined
1543 * by the `position` bar config directive. */
1544 xcb_void_cookie_t config_strut_partial(i3_output *output) {
1545 /* A local struct to save the strut_partial property */
1551 uint32_t left_start_y;
1552 uint32_t left_end_y;
1553 uint32_t right_start_y;
1554 uint32_t right_end_y;
1555 uint32_t top_start_x;
1557 uint32_t bottom_start_x;
1558 uint32_t bottom_end_x;
1559 } __attribute__((__packed__)) strut_partial;
1560 memset(&strut_partial, 0, sizeof(strut_partial));
1562 switch (config.position) {
1566 strut_partial.top = bar_height;
1567 strut_partial.top_start_x = output->rect.x;
1568 strut_partial.top_end_x = output->rect.x + output->rect.w;
1571 strut_partial.bottom = bar_height;
1572 strut_partial.bottom_start_x = output->rect.x;
1573 strut_partial.bottom_end_x = output->rect.x + output->rect.w;
1576 return xcb_change_property(xcb_connection,
1577 XCB_PROP_MODE_REPLACE,
1579 atoms[_NET_WM_STRUT_PARTIAL],
1587 * Reconfigure all bars and create new bars for recently activated outputs
1590 void reconfig_windows(bool redraw_bars) {
1593 static bool tray_configured = false;
1596 SLIST_FOREACH(walk, outputs, slist) {
1597 if (!walk->active) {
1598 /* If an output is not active, we destroy its bar */
1599 /* FIXME: Maybe we rather want to unmap? */
1600 DLOG("Destroying window for output %s\n", walk->name);
1601 destroy_window(walk);
1604 if (walk->bar == XCB_NONE) {
1605 DLOG("Creating window for output %s\n", walk->name);
1607 walk->bar = xcb_generate_id(xcb_connection);
1608 walk->buffer = xcb_generate_id(xcb_connection);
1609 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
1610 /* Black background */
1611 values[0] = colors.bar_bg;
1612 /* If hide_on_modifier is set to hide or invisible mode, i3 is not supposed to manage our bar windows */
1613 values[1] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1614 /* We enable the following EventMask fields:
1615 * EXPOSURE, to get expose events (we have to re-draw then)
1616 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1617 * child windows use ConfigureWindow
1618 * BUTTON_PRESS, to handle clicks on the workspace buttons
1620 values[2] = XCB_EVENT_MASK_EXPOSURE |
1621 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1622 XCB_EVENT_MASK_BUTTON_PRESS;
1623 if (config.hide_on_modifier == M_DOCK) {
1624 /* If the bar is normally visible, catch visibility change events to suspend
1625 * the status process when the bar is obscured by full-screened windows. */
1626 values[2] |= XCB_EVENT_MASK_VISIBILITY_CHANGE;
1627 walk->visible = true;
1629 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1630 root_screen->root_depth,
1633 walk->rect.x, walk->rect.y + walk->rect.h - bar_height,
1634 walk->rect.w, bar_height,
1636 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1637 root_screen->root_visual,
1641 /* The double-buffer we use to render stuff off-screen */
1642 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1643 root_screen->root_depth,
1649 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1650 xcb_void_cookie_t class_cookie;
1651 class_cookie = xcb_change_property(xcb_connection,
1652 XCB_PROP_MODE_REPLACE,
1657 (strlen("i3bar") + 1) * 2,
1661 if (asprintf(&name, "i3bar for output %s", walk->name) == -1)
1662 err(EXIT_FAILURE, "asprintf()");
1663 xcb_void_cookie_t name_cookie;
1664 name_cookie = xcb_change_property(xcb_connection,
1665 XCB_PROP_MODE_REPLACE,
1674 /* We want dock windows (for now). When override_redirect is set, i3 is ignoring
1676 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1677 XCB_PROP_MODE_REPLACE,
1679 atoms[_NET_WM_WINDOW_TYPE],
1683 (unsigned char *)&atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1685 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1687 /* We also want a graphics context for the bars (it defines the properties
1688 * with which we draw to them) */
1689 walk->bargc = xcb_generate_id(xcb_connection);
1690 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
1696 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1697 xcb_void_cookie_t map_cookie;
1698 if (config.hide_on_modifier == M_DOCK) {
1699 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1702 if (xcb_request_failed(win_cookie, "Could not create window") ||
1703 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1704 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1705 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1706 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1707 xcb_request_failed(strut_cookie, "Could not set strut") ||
1708 xcb_request_failed(gc_cookie, "Could not create graphical context") ||
1709 ((config.hide_on_modifier == M_DOCK) && xcb_request_failed(map_cookie, "Could not map window"))) {
1713 const char *tray_output = (config.tray_output ? config.tray_output : SLIST_FIRST(outputs)->name);
1714 if (!tray_configured && strcasecmp(tray_output, "none") != 0) {
1715 /* Configuration sanity check: ensure this i3bar instance handles the output on
1716 * which the tray should appear (e.g. don’t initialize a tray if tray_output ==
1717 * VGA-1 but output == [HDMI-1]).
1720 SLIST_FOREACH(output, outputs, slist) {
1721 if (strcasecmp(output->name, tray_output) == 0 ||
1722 (strcasecmp(tray_output, "primary") == 0 && output->primary)) {
1727 tray_configured = true;
1730 /* We already have a bar, so we just reconfigure it */
1731 mask = XCB_CONFIG_WINDOW_X |
1732 XCB_CONFIG_WINDOW_Y |
1733 XCB_CONFIG_WINDOW_WIDTH |
1734 XCB_CONFIG_WINDOW_HEIGHT |
1735 XCB_CONFIG_WINDOW_STACK_MODE;
1736 values[0] = walk->rect.x;
1737 if (config.position == POS_TOP)
1738 values[1] = walk->rect.y;
1740 values[1] = walk->rect.y + walk->rect.h - bar_height;
1741 values[2] = walk->rect.w;
1742 values[3] = bar_height;
1743 values[4] = XCB_STACK_MODE_ABOVE;
1745 DLOG("Reconfiguring strut partial property for output %s\n", walk->name);
1746 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1748 DLOG("Destroying buffer for output %s\n", walk->name);
1749 xcb_free_pixmap(xcb_connection, walk->buffer);
1751 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1752 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1757 mask = XCB_CW_OVERRIDE_REDIRECT;
1758 values[0] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1759 DLOG("Changing window attribute override_redirect for output %s to %d\n", walk->name, values[0]);
1760 xcb_void_cookie_t chg_cookie = xcb_change_window_attributes(xcb_connection,
1765 DLOG("Recreating buffer for output %s\n", walk->name);
1766 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1767 root_screen->root_depth,
1773 xcb_void_cookie_t map_cookie, umap_cookie;
1775 /* Unmap the window, and draw it again when in dock mode */
1776 umap_cookie = xcb_unmap_window_checked(xcb_connection, walk->bar);
1777 if (config.hide_on_modifier == M_DOCK) {
1779 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1784 if (config.hide_on_modifier == M_HIDE) {
1785 /* Switching to hide mode, register for keyevents */
1786 register_xkb_keyevents();
1788 /* Switching to dock/invisible mode, deregister from keyevents */
1789 deregister_xkb_keyevents();
1793 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") ||
1794 xcb_request_failed(chg_cookie, "Could not change window") ||
1795 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1796 xcb_request_failed(strut_cookie, "Could not set strut") ||
1797 (redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") ||
1798 (config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) {
1806 * Render the bars, with buttons and statusline
1809 void draw_bars(bool unhide) {
1810 DLOG("Drawing bars...\n");
1811 int workspace_width = 0;
1812 /* Is the currently-rendered statusline using short_text items? */
1813 bool rendered_statusline_is_short = false;
1815 refresh_statusline(false);
1817 i3_output *outputs_walk;
1818 SLIST_FOREACH(outputs_walk, outputs, slist) {
1819 if (!outputs_walk->active) {
1820 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1823 if (outputs_walk->bar == XCB_NONE) {
1824 /* Oh shit, an active output without an own bar. Create it now! */
1825 reconfig_windows(false);
1827 /* First things first: clear the backbuffer */
1828 uint32_t color = colors.bar_bg;
1829 xcb_change_gc(xcb_connection,
1830 outputs_walk->bargc,
1833 xcb_rectangle_t rect = {0, 0, outputs_walk->rect.w, bar_height};
1834 xcb_poly_fill_rectangle(xcb_connection,
1835 outputs_walk->buffer,
1836 outputs_walk->bargc,
1840 if (!config.disable_ws) {
1842 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1843 DLOG("Drawing button for WS %s at x = %d, len = %d\n",
1844 i3string_as_utf8(ws_walk->name), workspace_width, ws_walk->name_width);
1845 uint32_t fg_color = colors.inactive_ws_fg;
1846 uint32_t bg_color = colors.inactive_ws_bg;
1847 uint32_t border_color = colors.inactive_ws_border;
1848 if (ws_walk->visible) {
1849 if (!ws_walk->focused) {
1850 fg_color = colors.active_ws_fg;
1851 bg_color = colors.active_ws_bg;
1852 border_color = colors.active_ws_border;
1854 fg_color = colors.focus_ws_fg;
1855 bg_color = colors.focus_ws_bg;
1856 border_color = colors.focus_ws_border;
1859 if (ws_walk->urgent) {
1860 DLOG("WS %s is urgent!\n", i3string_as_utf8(ws_walk->name));
1861 fg_color = colors.urgent_ws_fg;
1862 bg_color = colors.urgent_ws_bg;
1863 border_color = colors.urgent_ws_border;
1866 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1867 uint32_t vals_border[] = {border_color, border_color};
1868 xcb_change_gc(xcb_connection,
1869 outputs_walk->bargc,
1872 xcb_rectangle_t rect_border = {workspace_width,
1874 ws_walk->name_width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
1875 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1)};
1876 xcb_poly_fill_rectangle(xcb_connection,
1877 outputs_walk->buffer,
1878 outputs_walk->bargc,
1881 uint32_t vals[] = {bg_color, bg_color};
1882 xcb_change_gc(xcb_connection,
1883 outputs_walk->bargc,
1886 xcb_rectangle_t rect = {workspace_width + logical_px(1),
1888 ws_walk->name_width + 2 * logical_px(ws_hoff_px),
1889 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1)};
1890 xcb_poly_fill_rectangle(xcb_connection,
1891 outputs_walk->buffer,
1892 outputs_walk->bargc,
1895 set_font_colors(outputs_walk->bargc, fg_color, bg_color);
1896 draw_text(ws_walk->name, outputs_walk->buffer, outputs_walk->bargc,
1897 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
1898 logical_px(ws_voff_px),
1899 ws_walk->name_width);
1901 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
1902 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
1903 workspace_width += logical_px(ws_spacing_px);
1907 if (binding.name && !config.disable_binding_mode_indicator) {
1908 workspace_width += logical_px(ws_spacing_px);
1910 uint32_t fg_color = colors.binding_mode_fg;
1911 uint32_t bg_color = colors.binding_mode_bg;
1912 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1914 uint32_t vals_border[] = {colors.binding_mode_border, colors.binding_mode_border};
1915 xcb_change_gc(xcb_connection,
1916 outputs_walk->bargc,
1919 xcb_rectangle_t rect_border = {workspace_width,
1921 binding.width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
1922 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1)};
1923 xcb_poly_fill_rectangle(xcb_connection,
1924 outputs_walk->buffer,
1925 outputs_walk->bargc,
1929 uint32_t vals[] = {bg_color, bg_color};
1930 xcb_change_gc(xcb_connection,
1931 outputs_walk->bargc,
1934 xcb_rectangle_t rect = {workspace_width + logical_px(1),
1936 binding.width + 2 * logical_px(ws_hoff_px),
1937 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1)};
1938 xcb_poly_fill_rectangle(xcb_connection,
1939 outputs_walk->buffer,
1940 outputs_walk->bargc,
1944 set_font_colors(outputs_walk->bargc, fg_color, bg_color);
1945 draw_text(binding.name,
1946 outputs_walk->buffer,
1947 outputs_walk->bargc,
1948 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
1949 logical_px(ws_voff_px),
1953 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + binding.width;
1956 if (!TAILQ_EMPTY(&statusline_head)) {
1957 DLOG("Printing statusline!\n");
1959 int tray_width = get_tray_width(outputs_walk->trayclients);
1960 uint32_t max_statusline_width = outputs_walk->rect.w - workspace_width - tray_width - 2 * logical_px(sb_hoff_px);
1962 /* If the statusline is too long, try to use short texts. */
1963 if (statusline_width > max_statusline_width) {
1964 /* If the currently rendered statusline is long, render a short status line */
1965 refresh_statusline(true);
1966 rendered_statusline_is_short = true;
1967 } else if (rendered_statusline_is_short) {
1968 /* If the currently rendered statusline is short, render a long status line */
1969 refresh_statusline(false);
1970 rendered_statusline_is_short = false;
1973 /* Luckily we already prepared a seperate pixmap containing the rendered
1974 * statusline, we just have to copy the relevant parts to the relevant
1976 int visible_statusline_width = MIN(statusline_width, max_statusline_width);
1977 xcb_copy_area(xcb_connection,
1979 outputs_walk->buffer,
1980 outputs_walk->bargc,
1981 (int16_t)(statusline_width - visible_statusline_width), 0,
1982 (int16_t)(outputs_walk->rect.w - tray_width - logical_px(sb_hoff_px) - visible_statusline_width), 0,
1983 (int16_t)visible_statusline_width, (int16_t)bar_height);
1986 workspace_width = 0;
1989 /* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */
1991 config.hidden_state == S_SHOW ||
1994 } else if (config.hide_on_modifier == M_HIDE) {
2002 * Redraw the bars, i.e. simply copy the buffer to the barwindow
2005 void redraw_bars(void) {
2006 i3_output *outputs_walk;
2007 SLIST_FOREACH(outputs_walk, outputs, slist) {
2008 if (!outputs_walk->active) {
2011 xcb_copy_area(xcb_connection,
2012 outputs_walk->buffer,
2014 outputs_walk->bargc,
2017 outputs_walk->rect.w,
2018 outputs_walk->rect.h);
2019 xcb_flush(xcb_connection);
2024 * Set the current binding mode
2027 void set_current_mode(struct mode *current) {
2028 I3STRING_FREE(binding.name);
2030 activated_mode = binding.name != NULL;