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>
33 #include <cairo/cairo-xcb.h>
38 /* We save the atoms in an easy to access array, indexed by an enum */
40 #define ATOM_DO(name) name,
41 #include "xcb_atoms.def"
45 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
46 xcb_atom_t atoms[NUM_ATOMS];
48 /* Variables, that are the same for all functions at all times */
49 xcb_connection_t *xcb_connection;
51 xcb_screen_t *root_screen;
52 xcb_window_t xcb_root;
54 /* selection window for tray support */
55 static xcb_window_t selwin = XCB_NONE;
56 static xcb_intern_atom_reply_t *tray_reply = NULL;
58 /* This is needed for integration with libi3 */
59 xcb_connection_t *conn;
61 /* The font we'll use */
64 /* Icon size (based on font size) */
67 /* Overall height of the bar (based on font size) */
70 /* These are only relevant for XKB, which we only need for grabbing modifiers */
74 /* Because the statusline is the same on all outputs, we have
75 * global buffer to render it on */
76 surface_t statusline_surface;
77 uint32_t statusline_width;
79 /* Event watchers, to interact with the user */
85 /* The name of current binding mode */
88 /* Indicates whether a new binding mode was recently activated */
89 bool activated_mode = false;
91 /* The parsed colors */
98 color_t active_ws_border;
99 color_t inactive_ws_fg;
100 color_t inactive_ws_bg;
101 color_t inactive_ws_border;
102 color_t urgent_ws_bg;
103 color_t urgent_ws_fg;
104 color_t urgent_ws_border;
107 color_t focus_ws_border;
108 color_t binding_mode_bg;
109 color_t binding_mode_fg;
110 color_t binding_mode_border;
112 struct xcb_colors_t colors;
114 /* Horizontal offset between a workspace label and button borders */
115 static const int ws_hoff_px = 4;
117 /* Vertical offset between a workspace label and button borders */
118 static const int ws_voff_px = 3;
120 /* Offset between two workspace buttons */
121 static const int ws_spacing_px = 1;
123 /* Offset between the statusline and 1) workspace buttons on the left
124 * 2) the tray or screen edge on the right */
125 static const int sb_hoff_px = 4;
127 /* Additional offset between the tray and the statusline, if the tray is not empty */
128 static const int tray_loff_px = 2;
130 /* Vertical offset between the bar and a separator */
131 static const int sep_voff_px = 4;
133 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
134 xcb_generic_error_t *err;
135 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
136 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
137 return err->error_code;
142 uint32_t get_sep_offset(struct status_block *block) {
143 if (!block->no_separator && block->sep_block_width > 0)
144 return block->sep_block_width / 2 + block->sep_block_width % 2;
148 int get_tray_width(struct tc_head *trayclients) {
149 trayclient *trayclient;
151 TAILQ_FOREACH_REVERSE(trayclient, trayclients, tc_head, tailq) {
152 if (!trayclient->mapped)
154 tray_width += icon_size + logical_px(config.tray_padding);
157 tray_width += logical_px(tray_loff_px);
162 * Draws a separator for the given block if necessary.
165 static void draw_separator(uint32_t x, struct status_block *block) {
166 uint32_t sep_offset = get_sep_offset(block);
167 if (TAILQ_NEXT(block, blocks) == NULL || sep_offset == 0)
170 uint32_t center_x = x - sep_offset;
171 if (config.separator_symbol == NULL) {
172 /* Draw a classic one pixel, vertical separator. */
173 cairo_set_source_color(&statusline_surface, colors.sep_fg);
174 cairo_rectangle(statusline_surface.cr, center_x, logical_px(sep_voff_px), logical_px(1), bar_height - 2 * logical_px(sep_voff_px));
175 cairo_fill(statusline_surface.cr);
177 /* Draw a custom separator. */
178 uint32_t separator_x = MAX(x - block->sep_block_width, center_x - separator_symbol_width / 2);
179 set_font_colors(statusline_surface.gc, colors.sep_fg.colorpixel, colors.bar_bg.colorpixel);
180 draw_text(config.separator_symbol, statusline_surface.id, statusline_surface.gc,
181 separator_x, logical_px(ws_voff_px), x - separator_x);
186 * Redraws the statusline to the buffer
189 void refresh_statusline(bool use_short_text) {
190 struct status_block *block;
192 uint32_t old_statusline_width = statusline_width;
193 statusline_width = 0;
195 /* Predict the text width of all blocks (in pixels). */
196 TAILQ_FOREACH(block, &statusline_head, blocks) {
197 /* Try to use the shorter text if necessary and possible. */
198 if (use_short_text && block->short_text != NULL) {
199 I3STRING_FREE(block->full_text);
200 block->full_text = i3string_copy(block->short_text);
203 if (i3string_get_num_bytes(block->full_text) == 0)
206 block->width = predict_text_width(block->full_text);
208 /* Compute offset and append for text aligment in min_width. */
209 if (block->min_width <= block->width) {
213 uint32_t padding_width = block->min_width - block->width;
214 switch (block->align) {
216 block->x_append = padding_width;
219 block->x_offset = padding_width;
222 block->x_offset = padding_width / 2;
223 block->x_append = padding_width / 2 + padding_width % 2;
228 /* If this is not the last block, add some pixels for a separator. */
229 if (TAILQ_NEXT(block, blocks) != NULL)
230 statusline_width += block->sep_block_width;
232 statusline_width += block->width + block->x_offset + block->x_append;
235 /* If the statusline is bigger than our screen we need to make sure that
236 * the pixmap provides enough space, so re-allocate if the width grew */
237 if (statusline_width > root_screen->width_in_pixels &&
238 statusline_width > old_statusline_width)
241 /* Clear the statusline pixmap. */
242 cairo_set_source_color(&statusline_surface, colors.bar_bg);
243 cairo_rectangle(statusline_surface.cr, 0, 0, MAX(root_screen->width_in_pixels, statusline_width), bar_height);
244 cairo_fill(statusline_surface.cr);
246 /* Draw the text of each block. */
248 TAILQ_FOREACH(block, &statusline_head, blocks) {
249 if (i3string_get_num_bytes(block->full_text) == 0)
253 /* If this block is urgent, draw it with the defined color and border. */
255 fg_color = colors.urgent_ws_fg;
257 /* Draw the background */
258 cairo_set_source_color(&statusline_surface, colors.urgent_ws_bg);
259 cairo_rectangle(statusline_surface.cr, x - logical_px(2), logical_px(1), block->width + logical_px(4), bar_height - logical_px(2));
260 cairo_fill(statusline_surface.cr);
262 fg_color = (block->color ? cairo_hex_to_color(block->color) : colors.bar_fg);
265 set_font_colors(statusline_surface.gc, fg_color.colorpixel, colors.bar_bg.colorpixel);
266 draw_text(block->full_text, statusline_surface.id, statusline_surface.gc, x + block->x_offset, logical_px(ws_voff_px), block->width);
267 x += block->width + block->sep_block_width + block->x_offset + block->x_append;
269 /* If this is not the last block, draw a separator. */
270 draw_separator(x, block);
275 * Hides all bars (unmaps them)
278 void hide_bars(void) {
279 if ((config.hide_on_modifier == M_DOCK) || (config.hidden_state == S_SHOW && config.hide_on_modifier == M_HIDE)) {
284 SLIST_FOREACH(walk, outputs, slist) {
288 xcb_unmap_window(xcb_connection, walk->bar.id);
294 * Unhides all bars (maps them)
297 void unhide_bars(void) {
298 if (config.hide_on_modifier != M_HIDE) {
303 xcb_void_cookie_t cookie;
309 SLIST_FOREACH(walk, outputs, slist) {
310 if (walk->bar.id == XCB_NONE) {
313 mask = XCB_CONFIG_WINDOW_X |
314 XCB_CONFIG_WINDOW_Y |
315 XCB_CONFIG_WINDOW_WIDTH |
316 XCB_CONFIG_WINDOW_HEIGHT |
317 XCB_CONFIG_WINDOW_STACK_MODE;
318 values[0] = walk->rect.x;
319 if (config.position == POS_TOP)
320 values[1] = walk->rect.y;
322 values[1] = walk->rect.y + walk->rect.h - bar_height;
323 values[2] = walk->rect.w;
324 values[3] = bar_height;
325 values[4] = XCB_STACK_MODE_ABOVE;
326 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
327 cookie = xcb_configure_window_checked(xcb_connection,
332 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
335 xcb_map_window(xcb_connection, walk->bar.id);
340 * Parse the colors into a format that we can use
343 void init_colors(const struct xcb_color_strings_t *new_colors) {
344 #define PARSE_COLOR(name, def) \
346 colors.name = cairo_hex_to_color(new_colors->name ? new_colors->name : def); \
348 PARSE_COLOR(bar_fg, "#FFFFFF");
349 PARSE_COLOR(bar_bg, "#000000");
350 PARSE_COLOR(sep_fg, "#666666");
351 PARSE_COLOR(active_ws_fg, "#FFFFFF");
352 PARSE_COLOR(active_ws_bg, "#333333");
353 PARSE_COLOR(active_ws_border, "#333333");
354 PARSE_COLOR(inactive_ws_fg, "#888888");
355 PARSE_COLOR(inactive_ws_bg, "#222222");
356 PARSE_COLOR(inactive_ws_border, "#333333");
357 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
358 PARSE_COLOR(urgent_ws_bg, "#900000");
359 PARSE_COLOR(urgent_ws_border, "#2f343a");
360 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
361 PARSE_COLOR(focus_ws_bg, "#285577");
362 PARSE_COLOR(focus_ws_border, "#4c7899");
365 #define PARSE_COLOR_FALLBACK(name, fallback) \
367 colors.name = new_colors->name ? cairo_hex_to_color(new_colors->name) : colors.fallback; \
370 /* For the binding mode indicator colors, we don't hardcode a default.
371 * Instead, we fall back to urgent_ws_* colors. */
372 PARSE_COLOR_FALLBACK(binding_mode_fg, urgent_ws_fg);
373 PARSE_COLOR_FALLBACK(binding_mode_bg, urgent_ws_bg);
374 PARSE_COLOR_FALLBACK(binding_mode_border, urgent_ws_border);
375 #undef PARSE_COLOR_FALLBACK
378 xcb_flush(xcb_connection);
382 * Handle a button press event (i.e. a mouse click on one of our bars).
383 * We determine, whether the click occured on a workspace button or if the scroll-
384 * wheel was used and change the workspace appropriately
387 void handle_button(xcb_button_press_event_t *event) {
388 /* Determine, which bar was clicked */
390 xcb_window_t bar = event->event;
391 SLIST_FOREACH(walk, outputs, slist) {
392 if (walk->bar.id == bar) {
398 DLOG("Unknown bar clicked!\n");
402 int32_t x = event->event_x >= 0 ? event->event_x : 0;
403 int32_t original_x = x;
405 DLOG("Got button %d\n", event->detail);
407 int workspace_width = 0;
408 i3_ws *cur_ws = NULL, *clicked_ws = NULL, *ws_walk;
410 TAILQ_FOREACH(ws_walk, walk->workspaces, tailq) {
411 int w = 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
412 if (x >= workspace_width && x <= workspace_width + w)
413 clicked_ws = ws_walk;
414 if (ws_walk->visible)
416 workspace_width += w;
417 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
418 workspace_width += logical_px(ws_spacing_px);
421 if (x > workspace_width && child_want_click_events()) {
422 /* If the child asked for click events,
423 * check if a status block has been clicked. */
424 int tray_width = get_tray_width(walk->trayclients);
425 int block_x = 0, last_block_x;
426 int offset = walk->rect.w - statusline_width - tray_width - logical_px(sb_hoff_px);
428 x = original_x - offset;
429 if (x >= 0 && (size_t)x < statusline_width) {
430 struct status_block *block;
431 int sep_offset_remainder = 0;
433 TAILQ_FOREACH(block, &statusline_head, blocks) {
434 if (i3string_get_num_bytes(block->full_text) == 0)
437 last_block_x = block_x;
438 block_x += block->width + block->x_offset + block->x_append + get_sep_offset(block) + sep_offset_remainder;
440 if (x <= block_x && x >= last_block_x) {
441 send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y);
445 sep_offset_remainder = block->sep_block_width - get_sep_offset(block);
451 /* If a custom command was specified for this mouse button, it overrides
452 * the default behavior. */
454 TAILQ_FOREACH(binding, &(config.bindings), bindings) {
455 if (binding->input_code != event->detail)
458 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, binding->command);
462 if (cur_ws == NULL) {
463 DLOG("No workspace active?\n");
466 switch (event->detail) {
468 /* Mouse wheel up. We select the previous ws, if any.
469 * If there is no more workspace, don’t even send the workspace
470 * command, otherwise (with workspace auto_back_and_forth) we’d end
471 * up on the wrong workspace. */
472 if (cur_ws == TAILQ_FIRST(walk->workspaces))
475 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
478 /* Mouse wheel down. We select the next ws, if any.
479 * If there is no more workspace, don’t even send the workspace
480 * command, otherwise (with workspace auto_back_and_forth) we’d end
481 * up on the wrong workspace. */
482 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
485 cur_ws = TAILQ_NEXT(cur_ws, tailq);
490 /* if no workspace was clicked, focus our currently visible
491 * workspace if it is not already focused */
492 if (cur_ws == NULL) {
493 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
494 if (cur_ws->visible && !cur_ws->focused)
499 /* if there is nothing to focus, we are done */
508 /* To properly handle workspace names with double quotes in them, we need
509 * to escape the double quotes. Unfortunately, that’s rather ugly in C: We
510 * first count the number of double quotes, then we allocate a large enough
511 * buffer, then we copy character by character. */
514 const char *utf8_name = cur_ws->canonical_name;
515 for (const char *walk = utf8_name; *walk != '\0'; walk++) {
516 if (*walk == '"' || *walk == '\\')
518 /* While we’re looping through the name anyway, we can save one
523 const size_t len = namelen + strlen("workspace \"\"") + 1;
524 char *buffer = scalloc(len + num_quotes, 1);
525 strncpy(buffer, "workspace \"", strlen("workspace \""));
526 size_t inpos, outpos;
527 for (inpos = 0, outpos = strlen("workspace \"");
530 if (utf8_name[inpos] == '"' || utf8_name[inpos] == '\\') {
531 buffer[outpos] = '\\';
534 buffer[outpos] = utf8_name[inpos];
536 buffer[outpos] = '"';
537 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
542 * Handle visibility notifications: when none of the bars are visible, e.g.
543 * if windows are in fullscreen on each output, suspend the child process.
546 static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
547 bool visible = (event->state != XCB_VISIBILITY_FULLY_OBSCURED);
551 SLIST_FOREACH(output, outputs, slist) {
552 if (!output->active) {
555 if (output->bar.id == event->window) {
556 if (output->visible == visible) {
559 output->visible = visible;
561 num_visible += output->visible;
564 if (num_visible == 0) {
566 } else if (num_visible == visible) {
567 /* Wake the child only when transitioning from 0 to 1 visible bar.
568 * We cannot transition from 0 to 2 or more visible bars at once since
569 * visibility events are delivered to each window separately */
575 * Adjusts the size of the tray window and alignment of the tray clients by
576 * configuring their respective x coordinates. To be called when mapping or
577 * unmapping a tray client window.
580 static void configure_trayclients(void) {
581 trayclient *trayclient;
583 SLIST_FOREACH(output, outputs, slist) {
588 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
589 if (!trayclient->mapped)
593 DLOG("Configuring tray window %08x to x=%d\n",
594 trayclient->win, output->rect.w - (clients * (icon_size + logical_px(config.tray_padding))));
595 uint32_t x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
596 xcb_configure_window(xcb_connection,
605 * Handles ClientMessages (messages sent from another client directly to us).
607 * At the moment, only the tray window will receive client messages. All
608 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
611 static void handle_client_message(xcb_client_message_event_t *event) {
612 if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
613 event->format == 32) {
614 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
615 /* event->data.data32[0] is the timestamp */
616 uint32_t op = event->data.data32[1];
619 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
620 xcb_window_t client = event->data.data32[2];
622 /* Listen for PropertyNotify events to get the most recent value of
623 * the XEMBED_MAPPED atom, also listen for UnmapNotify events */
624 mask = XCB_CW_EVENT_MASK;
625 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
626 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
627 xcb_change_window_attributes(xcb_connection,
632 /* Request the _XEMBED_INFO property. The XEMBED specification
633 * (which is referred by the tray specification) says this *has* to
634 * be set, but VLC does not set it… */
637 xcb_get_property_cookie_t xembedc;
638 xcb_generic_error_t *error;
639 xembedc = xcb_get_property(xcb_connection,
643 XCB_GET_PROPERTY_TYPE_ANY,
647 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
651 ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
656 if (xembedr != NULL && xembedr->length != 0) {
657 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
658 uint32_t *xembed = xcb_get_property_value(xembedr);
659 DLOG("xembed version = %d\n", xembed[0]);
660 DLOG("xembed flags = %d\n", xembed[1]);
661 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
662 xe_version = xembed[0];
667 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
670 DLOG("X window %08x requested docking\n", client);
671 i3_output *walk, *output = NULL;
672 SLIST_FOREACH(walk, outputs, slist) {
675 if (config.tray_output) {
676 if ((strcasecmp(walk->name, config.tray_output) != 0) &&
677 (!walk->primary || strcasecmp("primary", config.tray_output) != 0))
681 DLOG("using output %s\n", walk->name);
685 /* In case of tray_output == primary and there is no primary output
686 * configured, we fall back to the first available output. */
687 if (output == NULL &&
688 config.tray_output &&
689 strcasecmp("primary", config.tray_output) == 0) {
690 SLIST_FOREACH(walk, outputs, slist) {
693 DLOG("Falling back to output %s because no primary output is configured\n", walk->name);
698 if (output == NULL) {
699 ELOG("No output found\n");
702 xcb_reparent_window(xcb_connection,
705 output->rect.w - icon_size - logical_px(config.tray_padding),
706 logical_px(config.tray_padding));
707 /* We reconfigure the window to use a reasonable size. The systray
708 * specification explicitly says:
709 * Tray icons may be assigned any size by the system tray, and
710 * should do their best to cope with any size effectively
712 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
713 values[0] = icon_size;
714 values[1] = icon_size;
715 xcb_configure_window(xcb_connection,
720 /* send the XEMBED_EMBEDDED_NOTIFY message */
721 void *event = scalloc(32, 1);
722 xcb_client_message_event_t *ev = event;
723 ev->response_type = XCB_CLIENT_MESSAGE;
725 ev->type = atoms[_XEMBED];
727 ev->data.data32[0] = XCB_CURRENT_TIME;
728 ev->data.data32[1] = atoms[XEMBED_EMBEDDED_NOTIFY];
729 ev->data.data32[2] = output->bar.id;
730 ev->data.data32[3] = xe_version;
731 xcb_send_event(xcb_connection,
734 XCB_EVENT_MASK_NO_EVENT,
738 /* Put the client inside the save set. Upon termination (whether
739 * killed or normal exit does not matter) of i3bar, these clients
740 * will be correctly reparented to their most closest living
741 * ancestor. Without this, tray icons might die when i3bar
743 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
745 trayclient *tc = smalloc(sizeof(trayclient));
747 tc->xe_version = xe_version;
749 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
752 DLOG("Mapping dock client\n");
753 xcb_map_window(xcb_connection, client);
755 DLOG("Not mapping dock client yet\n");
757 /* Trigger an update to copy the statusline text to the appropriate
759 configure_trayclients();
766 * Handles DestroyNotify events by removing the tray client from the data
767 * structure. According to the XEmbed protocol, this is one way for a tray
768 * client to finish the protocol. After this event is received, there is no
769 * further interaction with the tray client.
771 * See: http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
774 static void handle_destroy_notify(xcb_destroy_notify_event_t *event) {
775 DLOG("DestroyNotify for window = %08x, event = %08x\n", event->window, event->event);
778 SLIST_FOREACH(walk, outputs, slist) {
781 DLOG("checking output %s\n", walk->name);
782 trayclient *trayclient;
783 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
784 if (trayclient->win != event->window)
787 DLOG("Removing tray client with window ID %08x\n", event->window);
788 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
790 /* Trigger an update, we now have more space for the statusline */
791 configure_trayclients();
799 * Handles MapNotify events. These events happen when a tray client shows its
800 * window. We respond by realigning the tray clients.
803 static void handle_map_notify(xcb_map_notify_event_t *event) {
804 DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event);
807 SLIST_FOREACH(walk, outputs, slist) {
810 DLOG("checking output %s\n", walk->name);
811 trayclient *trayclient;
812 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
813 if (trayclient->win != event->window)
816 DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window);
817 trayclient->mapped = true;
819 /* Trigger an update, we now have more space for the statusline */
820 configure_trayclients();
827 * Handles UnmapNotify events. These events happen when a tray client hides its
828 * window. We respond by realigning the tray clients.
831 static void handle_unmap_notify(xcb_unmap_notify_event_t *event) {
832 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
835 SLIST_FOREACH(walk, outputs, slist) {
838 DLOG("checking output %s\n", walk->name);
839 trayclient *trayclient;
840 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
841 if (trayclient->win != event->window)
844 DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window);
845 trayclient->mapped = false;
847 /* Trigger an update, we now have more space for the statusline */
848 configure_trayclients();
856 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
857 * handled, which tells us whether a dock client should be mapped or unmapped.
860 static void handle_property_notify(xcb_property_notify_event_t *event) {
861 DLOG("PropertyNotify\n");
862 if (event->atom == atoms[_XEMBED_INFO] &&
863 event->state == XCB_PROPERTY_NEW_VALUE) {
864 DLOG("xembed_info updated\n");
865 trayclient *trayclient = NULL, *walk;
867 SLIST_FOREACH(o_walk, outputs, slist) {
871 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
872 if (walk->win != event->window)
882 ELOG("PropertyNotify received for unknown window %08x\n",
886 xcb_get_property_cookie_t xembedc;
887 xembedc = xcb_get_property_unchecked(xcb_connection,
891 XCB_GET_PROPERTY_TYPE_ANY,
895 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
898 if (xembedr == NULL || xembedr->length == 0) {
899 DLOG("xembed_info unset\n");
903 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
904 uint32_t *xembed = xcb_get_property_value(xembedr);
905 DLOG("xembed version = %d\n", xembed[0]);
906 DLOG("xembed flags = %d\n", xembed[1]);
907 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
908 DLOG("map state now %d\n", map_it);
909 if (trayclient->mapped && !map_it) {
910 /* need to unmap the window */
911 xcb_unmap_window(xcb_connection, trayclient->win);
912 } else if (!trayclient->mapped && map_it) {
913 /* need to map the window */
914 xcb_map_window(xcb_connection, trayclient->win);
921 * Handle ConfigureRequests by denying them and sending the client a
922 * ConfigureNotify with its actual size.
925 static void handle_configure_request(xcb_configure_request_event_t *event) {
926 DLOG("ConfigureRequest for window = %08x\n", event->window);
928 trayclient *trayclient;
930 SLIST_FOREACH(output, outputs, slist) {
935 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
936 if (!trayclient->mapped)
940 if (trayclient->win != event->window)
943 xcb_rectangle_t rect;
944 rect.x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
945 rect.y = logical_px(config.tray_padding);
946 rect.width = icon_size;
947 rect.height = icon_size;
949 DLOG("This is a tray window. x = %d\n", rect.x);
950 fake_configure_notify(xcb_connection, rect, event->window, 0);
955 DLOG("WARNING: Could not find corresponding tray window.\n");
959 * This function is called immediately before the main loop locks. We flush xcb
960 * then (and only then)
963 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
964 xcb_flush(xcb_connection);
968 * This function is called immediately after the main loop locks, so when one
969 * of the watchers registered an event.
970 * We check whether an X-Event arrived and handle it.
973 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
974 xcb_generic_event_t *event;
976 if (xcb_connection_has_error(xcb_connection)) {
977 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
981 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
982 int type = (event->response_type & ~0x80);
984 if (type == xkb_base && xkb_base > -1) {
985 DLOG("received an xkb event\n");
987 xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event;
988 if (state->xkbType == XCB_XKB_STATE_NOTIFY) {
989 int modstate = state->mods & config.modifier;
991 #define DLOGMOD(modmask, status) \
995 DLOG("ShiftMask got " #status "!\n"); \
998 DLOG("ControlMask got " #status "!\n"); \
1001 DLOG("Mod1Mask got " #status "!\n"); \
1004 DLOG("Mod2Mask got " #status "!\n"); \
1007 DLOG("Mod3Mask got " #status "!\n"); \
1010 DLOG("Mod4Mask got " #status "!\n"); \
1013 DLOG("Mod5Mask got " #status "!\n"); \
1018 if (modstate != mod_pressed) {
1019 if (modstate == 0) {
1020 DLOGMOD(config.modifier, released);
1021 if (!activated_mode)
1024 DLOGMOD(config.modifier, pressed);
1025 activated_mode = false;
1028 mod_pressed = modstate;
1038 case XCB_VISIBILITY_NOTIFY:
1039 /* Visibility change: a bar is [un]obscured by other window */
1040 handle_visibility_notify((xcb_visibility_notify_event_t *)event);
1043 /* Expose-events happen, when the window needs to be redrawn */
1046 case XCB_BUTTON_PRESS:
1047 /* Button press events are mouse buttons clicked on one of our bars */
1048 handle_button((xcb_button_press_event_t *)event);
1050 case XCB_CLIENT_MESSAGE:
1051 /* Client messages are used for client-to-client communication, for
1052 * example system tray widgets talk to us directly via client messages. */
1053 handle_client_message((xcb_client_message_event_t *)event);
1055 case XCB_DESTROY_NOTIFY:
1056 /* DestroyNotify signifies the end of the XEmbed protocol */
1057 handle_destroy_notify((xcb_destroy_notify_event_t *)event);
1059 case XCB_UNMAP_NOTIFY:
1060 /* UnmapNotify is received when a tray client hides its window. */
1061 handle_unmap_notify((xcb_unmap_notify_event_t *)event);
1063 case XCB_MAP_NOTIFY:
1064 handle_map_notify((xcb_map_notify_event_t *)event);
1066 case XCB_PROPERTY_NOTIFY:
1067 /* PropertyNotify */
1068 handle_property_notify((xcb_property_notify_event_t *)event);
1070 case XCB_CONFIGURE_REQUEST:
1071 /* ConfigureRequest, sent by a tray child */
1072 handle_configure_request((xcb_configure_request_event_t *)event);
1080 * Dummy callback. We only need this, so that the prepare and check watchers
1084 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
1088 * Early initialization of the connection to X11: Everything which does not
1089 * depend on 'config'.
1092 char *init_xcb_early() {
1093 /* FIXME: xcb_connect leaks memory */
1094 xcb_connection = xcb_connect(NULL, &screen);
1095 if (xcb_connection_has_error(xcb_connection)) {
1096 ELOG("Cannot open display\n");
1099 conn = xcb_connection;
1100 DLOG("Connected to xcb\n");
1102 /* We have to request the atoms we need */
1103 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
1104 #include "xcb_atoms.def"
1106 root_screen = xcb_aux_get_screen(xcb_connection, screen);
1107 xcb_root = root_screen->root;
1109 /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
1110 * this way, we can choose to crop it */
1111 xcb_pixmap_t statusline_id = xcb_generate_id(xcb_connection);
1112 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1113 root_screen->root_depth,
1116 root_screen->width_in_pixels,
1117 root_screen->height_in_pixels);
1118 cairo_surface_init(&statusline_surface, statusline_id, root_screen->width_in_pixels, root_screen->height_in_pixels);
1120 /* The various watchers to communicate with xcb */
1121 xcb_io = smalloc(sizeof(ev_io));
1122 xcb_prep = smalloc(sizeof(ev_prepare));
1123 xcb_chk = smalloc(sizeof(ev_check));
1125 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
1126 ev_prepare_init(xcb_prep, &xcb_prep_cb);
1127 ev_check_init(xcb_chk, &xcb_chk_cb);
1129 ev_io_start(main_loop, xcb_io);
1130 ev_prepare_start(main_loop, xcb_prep);
1131 ev_check_start(main_loop, xcb_chk);
1133 /* Now we get the atoms and save them in a nice data structure */
1136 char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen);
1138 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline buffer"))
1145 * Register for xkb keyevents. To grab modifiers without blocking other applications from receiving key events
1146 * involving that modifier, we sadly have to use xkb which is not yet fully supported
1150 void register_xkb_keyevents() {
1151 const xcb_query_extension_reply_t *extreply;
1152 extreply = xcb_get_extension_data(conn, &xcb_xkb_id);
1153 if (!extreply->present) {
1154 ELOG("xkb is not present on this server\n");
1157 DLOG("initializing xcb-xkb\n");
1158 xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
1159 xcb_xkb_select_events(conn,
1160 XCB_XKB_ID_USE_CORE_KBD,
1161 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1163 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
1167 xkb_base = extreply->first_event;
1171 * Deregister from xkb keyevents.
1174 void deregister_xkb_keyevents() {
1175 xcb_xkb_select_events(conn,
1176 XCB_XKB_ID_USE_CORE_KBD,
1186 * Initialization which depends on 'config' being usable. Called after the
1187 * configuration has arrived.
1190 void init_xcb_late(char *fontname) {
1191 if (fontname == NULL)
1192 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
1195 font = load_font(fontname, true);
1197 DLOG("Calculated font height: %d\n", font.height);
1198 bar_height = font.height + 2 * logical_px(ws_voff_px);
1199 icon_size = bar_height - 2 * logical_px(config.tray_padding);
1201 if (config.separator_symbol)
1202 separator_symbol_width = predict_text_width(config.separator_symbol);
1204 xcb_flush(xcb_connection);
1206 if (config.hide_on_modifier == M_HIDE)
1207 register_xkb_keyevents();
1211 * Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the
1215 static void send_tray_clientmessage(void) {
1216 uint8_t buffer[32] = {0};
1217 xcb_client_message_event_t *ev = (xcb_client_message_event_t *)buffer;
1219 ev->response_type = XCB_CLIENT_MESSAGE;
1220 ev->window = xcb_root;
1221 ev->type = atoms[MANAGER];
1223 ev->data.data32[0] = XCB_CURRENT_TIME;
1224 ev->data.data32[1] = tray_reply->atom;
1225 ev->data.data32[2] = selwin;
1227 xcb_send_event(xcb_connection,
1235 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
1236 * for the X11 display we are running on, then acquiring the selection for this
1237 * atom. Afterwards, tray clients will send ClientMessages to our window.
1240 void init_tray(void) {
1241 DLOG("Initializing system tray functionality\n");
1242 /* request the tray manager atom for the X11 display we are running on */
1243 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
1244 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
1245 xcb_intern_atom_cookie_t tray_cookie;
1246 if (tray_reply == NULL)
1247 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
1249 /* tray support: we need a window to own the selection */
1250 selwin = xcb_generate_id(xcb_connection);
1251 uint32_t selmask = XCB_CW_OVERRIDE_REDIRECT;
1252 uint32_t selval[] = {1};
1253 xcb_create_window(xcb_connection,
1254 root_screen->root_depth,
1260 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1261 root_screen->root_visual,
1265 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
1267 xcb_change_property(xcb_connection,
1268 XCB_PROP_MODE_REPLACE,
1270 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
1278 if (tray_reply == NULL) {
1279 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
1280 ELOG("Could not get atom %s\n", atomname);
1285 xcb_set_selection_owner(xcb_connection,
1290 /* Verify that we have the selection */
1291 xcb_get_selection_owner_cookie_t selcookie;
1292 xcb_get_selection_owner_reply_t *selreply;
1294 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
1295 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
1296 ELOG("Could not get selection owner for %s\n", atomname);
1300 if (selreply->owner != selwin) {
1301 ELOG("Could not set the %s selection. "
1302 "Maybe another tray is already running?\n",
1304 /* NOTE that this error is not fatal. We just can’t provide tray
1310 send_tray_clientmessage();
1314 * We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window
1315 * to make GTK+ 3 applets with symbolic icons visible. If the colors are unset,
1316 * they assume a light background.
1317 * See also https://bugzilla.gnome.org/show_bug.cgi?id=679591
1320 void init_tray_colors(void) {
1321 /* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */
1322 const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF");
1324 DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg);
1326 char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'},
1327 {bar_fg[3], bar_fg[4], '\0'},
1328 {bar_fg[5], bar_fg[6], '\0'}};
1329 const uint8_t r = strtol(strgroups[0], NULL, 16);
1330 const uint8_t g = strtol(strgroups[1], NULL, 16);
1331 const uint8_t b = strtol(strgroups[2], NULL, 16);
1333 const uint16_t r16 = ((uint16_t)r << 8) | r;
1334 const uint16_t g16 = ((uint16_t)g << 8) | g;
1335 const uint16_t b16 = ((uint16_t)b << 8) | b;
1337 const uint32_t tray_colors[12] = {
1338 r16, g16, b16, /* foreground color */
1339 r16, g16, b16, /* error color */
1340 r16, g16, b16, /* warning color */
1341 r16, g16, b16, /* success color */
1344 xcb_change_property(xcb_connection,
1345 XCB_PROP_MODE_REPLACE,
1347 atoms[_NET_SYSTEM_TRAY_COLORS],
1355 * Cleanup the xcb stuff.
1356 * Called once, before the program terminates.
1359 void clean_xcb(void) {
1362 SLIST_FOREACH(o_walk, outputs, slist) {
1363 destroy_window(o_walk);
1364 FREE(o_walk->trayclients);
1365 FREE(o_walk->workspaces);
1368 FREE_SLIST(outputs, i3_output);
1371 xcb_flush(xcb_connection);
1372 xcb_aux_sync(xcb_connection);
1373 xcb_disconnect(xcb_connection);
1375 ev_check_stop(main_loop, xcb_chk);
1376 ev_prepare_stop(main_loop, xcb_prep);
1377 ev_io_stop(main_loop, xcb_io);
1385 * Get the earlier requested atoms and save them in the prepared data structure
1388 void get_atoms(void) {
1389 xcb_intern_atom_reply_t *reply;
1390 #define ATOM_DO(name) \
1391 reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1392 if (reply == NULL) { \
1393 ELOG("Could not get atom %s\n", #name); \
1394 exit(EXIT_FAILURE); \
1396 atoms[name] = reply->atom; \
1399 #include "xcb_atoms.def"
1400 DLOG("Got atoms\n");
1404 * Reparents all tray clients of the specified output to the root window. This
1405 * is either used when shutting down, when an output appears (xrandr --output
1406 * VGA1 --off) or when the primary output changes.
1408 * Applications using the tray will start the protocol from the beginning again
1412 void kick_tray_clients(i3_output *output) {
1413 if (TAILQ_EMPTY(output->trayclients))
1416 trayclient *trayclient;
1417 while (!TAILQ_EMPTY(output->trayclients)) {
1418 trayclient = TAILQ_FIRST(output->trayclients);
1419 /* Unmap, then reparent (to root) the tray client windows */
1420 xcb_unmap_window(xcb_connection, trayclient->win);
1421 xcb_reparent_window(xcb_connection,
1427 /* We remove the trayclient right here. We might receive an UnmapNotify
1428 * event afterwards, but better safe than sorry. */
1429 TAILQ_REMOVE(output->trayclients, trayclient, tailq);
1432 /* Fake a DestroyNotify so that Qt re-adds tray icons.
1433 * We cannot actually destroy the window because then Qt will not restore
1434 * its event mask on the new window. */
1435 uint8_t buffer[32] = {0};
1436 xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t *)buffer;
1438 event->response_type = XCB_DESTROY_NOTIFY;
1439 event->event = selwin;
1440 event->window = selwin;
1442 xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)event);
1444 send_tray_clientmessage();
1448 * Destroy the bar of the specified output
1451 void destroy_window(i3_output *output) {
1452 if (output == NULL) {
1455 if (output->bar.id == XCB_NONE) {
1459 kick_tray_clients(output);
1460 xcb_destroy_window(xcb_connection, output->bar.id);
1461 output->bar.id = XCB_NONE;
1465 * Reallocate the statusline buffer
1468 void realloc_sl_buffer(void) {
1469 DLOG("Re-allocating statusline buffer, statusline_width = %d, root_screen->width_in_pixels = %d\n",
1470 statusline_width, root_screen->width_in_pixels);
1471 xcb_free_pixmap(xcb_connection, statusline_surface.id);
1472 cairo_surface_free(&statusline_surface);
1474 xcb_pixmap_t statusline_id = xcb_generate_id(xcb_connection);
1475 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1476 root_screen->root_depth,
1479 MAX(root_screen->width_in_pixels, statusline_width),
1481 cairo_surface_init(&statusline_surface, statusline_id, root_screen->width_in_pixels, root_screen->height_in_pixels);
1483 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline buffer"))
1487 /* Strut partial tells i3 where to reserve space for i3bar. This is determined
1488 * by the `position` bar config directive. */
1489 xcb_void_cookie_t config_strut_partial(i3_output *output) {
1490 /* A local struct to save the strut_partial property */
1496 uint32_t left_start_y;
1497 uint32_t left_end_y;
1498 uint32_t right_start_y;
1499 uint32_t right_end_y;
1500 uint32_t top_start_x;
1502 uint32_t bottom_start_x;
1503 uint32_t bottom_end_x;
1504 } __attribute__((__packed__)) strut_partial;
1505 memset(&strut_partial, 0, sizeof(strut_partial));
1507 switch (config.position) {
1511 strut_partial.top = bar_height;
1512 strut_partial.top_start_x = output->rect.x;
1513 strut_partial.top_end_x = output->rect.x + output->rect.w;
1516 strut_partial.bottom = bar_height;
1517 strut_partial.bottom_start_x = output->rect.x;
1518 strut_partial.bottom_end_x = output->rect.x + output->rect.w;
1521 return xcb_change_property(xcb_connection,
1522 XCB_PROP_MODE_REPLACE,
1524 atoms[_NET_WM_STRUT_PARTIAL],
1532 * Reconfigure all bars and create new bars for recently activated outputs
1535 void reconfig_windows(bool redraw_bars) {
1538 static bool tray_configured = false;
1541 SLIST_FOREACH(walk, outputs, slist) {
1542 if (!walk->active) {
1543 /* If an output is not active, we destroy its bar */
1544 /* FIXME: Maybe we rather want to unmap? */
1545 DLOG("Destroying window for output %s\n", walk->name);
1546 destroy_window(walk);
1549 if (walk->bar.id == XCB_NONE) {
1550 DLOG("Creating window for output %s\n", walk->name);
1552 xcb_window_t bar_id = xcb_generate_id(xcb_connection);
1553 xcb_pixmap_t buffer_id = xcb_generate_id(xcb_connection);
1554 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
1555 /* Black background */
1556 values[0] = colors.bar_bg.colorpixel;
1557 /* If hide_on_modifier is set to hide or invisible mode, i3 is not supposed to manage our bar windows */
1558 values[1] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1559 /* We enable the following EventMask fields:
1560 * EXPOSURE, to get expose events (we have to re-draw then)
1561 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1562 * child windows use ConfigureWindow
1563 * BUTTON_PRESS, to handle clicks on the workspace buttons
1565 values[2] = XCB_EVENT_MASK_EXPOSURE |
1566 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1567 XCB_EVENT_MASK_BUTTON_PRESS;
1568 if (config.hide_on_modifier == M_DOCK) {
1569 /* If the bar is normally visible, catch visibility change events to suspend
1570 * the status process when the bar is obscured by full-screened windows. */
1571 values[2] |= XCB_EVENT_MASK_VISIBILITY_CHANGE;
1572 walk->visible = true;
1574 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1575 root_screen->root_depth,
1578 walk->rect.x, walk->rect.y + walk->rect.h - bar_height,
1579 walk->rect.w, bar_height,
1581 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1582 root_screen->root_visual,
1586 /* The double-buffer we use to render stuff off-screen */
1587 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1588 root_screen->root_depth,
1594 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1595 xcb_void_cookie_t class_cookie;
1596 class_cookie = xcb_change_property(xcb_connection,
1597 XCB_PROP_MODE_REPLACE,
1602 (strlen("i3bar") + 1) * 2,
1606 sasprintf(&name, "i3bar for output %s", walk->name);
1607 xcb_void_cookie_t name_cookie;
1608 name_cookie = xcb_change_property(xcb_connection,
1609 XCB_PROP_MODE_REPLACE,
1618 /* We want dock windows (for now). When override_redirect is set, i3 is ignoring
1620 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1621 XCB_PROP_MODE_REPLACE,
1623 atoms[_NET_WM_WINDOW_TYPE],
1627 (unsigned char *)&atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1629 cairo_surface_init(&walk->bar, bar_id, walk->rect.w, bar_height);
1630 cairo_surface_init(&walk->buffer, buffer_id, walk->rect.w, bar_height);
1632 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1634 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1635 xcb_void_cookie_t map_cookie;
1636 if (config.hide_on_modifier == M_DOCK) {
1637 map_cookie = xcb_map_window_checked(xcb_connection, bar_id);
1640 if (xcb_request_failed(win_cookie, "Could not create window") ||
1641 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1642 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1643 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1644 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1645 xcb_request_failed(strut_cookie, "Could not set strut") ||
1646 ((config.hide_on_modifier == M_DOCK) && xcb_request_failed(map_cookie, "Could not map window"))) {
1650 const char *tray_output = (config.tray_output ? config.tray_output : SLIST_FIRST(outputs)->name);
1651 if (!tray_configured && strcasecmp(tray_output, "none") != 0) {
1652 /* Configuration sanity check: ensure this i3bar instance handles the output on
1653 * which the tray should appear (e.g. don’t initialize a tray if tray_output ==
1654 * VGA-1 but output == [HDMI-1]).
1657 SLIST_FOREACH(output, outputs, slist) {
1658 if (strcasecmp(output->name, tray_output) == 0 ||
1659 (strcasecmp(tray_output, "primary") == 0 && output->primary)) {
1664 tray_configured = true;
1667 /* We already have a bar, so we just reconfigure it */
1668 mask = XCB_CONFIG_WINDOW_X |
1669 XCB_CONFIG_WINDOW_Y |
1670 XCB_CONFIG_WINDOW_WIDTH |
1671 XCB_CONFIG_WINDOW_HEIGHT |
1672 XCB_CONFIG_WINDOW_STACK_MODE;
1673 values[0] = walk->rect.x;
1674 if (config.position == POS_TOP)
1675 values[1] = walk->rect.y;
1677 values[1] = walk->rect.y + walk->rect.h - bar_height;
1678 values[2] = walk->rect.w;
1679 values[3] = bar_height;
1680 values[4] = XCB_STACK_MODE_ABOVE;
1682 DLOG("Reconfiguring strut partial property for output %s\n", walk->name);
1683 xcb_void_cookie_t strut_cookie = config_strut_partial(walk);
1685 DLOG("Destroying buffer for output %s\n", walk->name);
1686 xcb_free_pixmap(xcb_connection, walk->buffer.id);
1688 DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1689 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1694 mask = XCB_CW_OVERRIDE_REDIRECT;
1695 values[0] = (config.hide_on_modifier == M_DOCK ? 0 : 1);
1696 DLOG("Changing window attribute override_redirect for output %s to %d\n", walk->name, values[0]);
1697 xcb_void_cookie_t chg_cookie = xcb_change_window_attributes(xcb_connection,
1702 DLOG("Recreating buffer for output %s\n", walk->name);
1703 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1704 root_screen->root_depth,
1710 cairo_surface_free(&(walk->bar));
1711 cairo_surface_free(&(walk->buffer));
1712 cairo_surface_init(&(walk->bar), walk->bar.id, walk->rect.w, bar_height);
1713 cairo_surface_init(&(walk->buffer), walk->buffer.id, walk->rect.w, bar_height);
1715 xcb_void_cookie_t map_cookie, umap_cookie;
1717 /* Unmap the window, and draw it again when in dock mode */
1718 umap_cookie = xcb_unmap_window_checked(xcb_connection, walk->bar.id);
1719 if (config.hide_on_modifier == M_DOCK) {
1721 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar.id);
1726 if (config.hide_on_modifier == M_HIDE) {
1727 /* Switching to hide mode, register for keyevents */
1728 register_xkb_keyevents();
1730 /* Switching to dock/invisible mode, deregister from keyevents */
1731 deregister_xkb_keyevents();
1735 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") ||
1736 xcb_request_failed(chg_cookie, "Could not change window") ||
1737 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1738 xcb_request_failed(strut_cookie, "Could not set strut") ||
1739 (redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") ||
1740 (config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) {
1748 * Render the bars, with buttons and statusline
1751 void draw_bars(bool unhide) {
1752 DLOG("Drawing bars...\n");
1753 int workspace_width = 0;
1754 /* Is the currently-rendered statusline using short_text items? */
1755 bool rendered_statusline_is_short = false;
1757 refresh_statusline(false);
1759 i3_output *outputs_walk;
1760 SLIST_FOREACH(outputs_walk, outputs, slist) {
1761 if (!outputs_walk->active) {
1762 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1765 if (outputs_walk->bar.id == XCB_NONE) {
1766 /* Oh shit, an active output without an own bar. Create it now! */
1767 reconfig_windows(false);
1769 /* First things first: clear the backbuffer */
1770 cairo_set_source_color(&(outputs_walk->buffer), colors.bar_bg);
1771 cairo_rectangle(outputs_walk->buffer.cr, 0, 0, outputs_walk->rect.w, bar_height);
1772 cairo_fill(outputs_walk->buffer.cr);
1774 if (!config.disable_ws) {
1776 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1777 DLOG("Drawing button for WS %s at x = %d, len = %d\n",
1778 i3string_as_utf8(ws_walk->name), workspace_width, ws_walk->name_width);
1779 color_t fg_color = colors.inactive_ws_fg;
1780 color_t bg_color = colors.inactive_ws_bg;
1781 color_t border_color = colors.inactive_ws_border;
1782 if (ws_walk->visible) {
1783 if (!ws_walk->focused) {
1784 fg_color = colors.active_ws_fg;
1785 bg_color = colors.active_ws_bg;
1786 border_color = colors.active_ws_border;
1788 fg_color = colors.focus_ws_fg;
1789 bg_color = colors.focus_ws_bg;
1790 border_color = colors.focus_ws_border;
1793 if (ws_walk->urgent) {
1794 DLOG("WS %s is urgent!\n", i3string_as_utf8(ws_walk->name));
1795 fg_color = colors.urgent_ws_fg;
1796 bg_color = colors.urgent_ws_bg;
1797 border_color = colors.urgent_ws_border;
1801 cairo_set_source_color(&(outputs_walk->buffer), border_color);
1802 cairo_rectangle(outputs_walk->buffer.cr, workspace_width, logical_px(1),
1803 ws_walk->name_width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
1804 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1));
1805 cairo_fill(outputs_walk->buffer.cr);
1807 cairo_set_source_color(&(outputs_walk->buffer), bg_color);
1808 cairo_rectangle(outputs_walk->buffer.cr, workspace_width + logical_px(1), 2 * logical_px(1),
1809 ws_walk->name_width + 2 * logical_px(ws_hoff_px),
1810 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1));
1811 cairo_fill(outputs_walk->buffer.cr);
1813 set_font_colors(outputs_walk->buffer.gc, fg_color.colorpixel, bg_color.colorpixel);
1814 draw_text(ws_walk->name, outputs_walk->buffer.id, outputs_walk->buffer.gc,
1815 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
1816 logical_px(ws_voff_px),
1817 ws_walk->name_width);
1819 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
1820 if (TAILQ_NEXT(ws_walk, tailq) != NULL)
1821 workspace_width += logical_px(ws_spacing_px);
1825 if (binding.name && !config.disable_binding_mode_indicator) {
1826 workspace_width += logical_px(ws_spacing_px);
1828 color_t fg_color = colors.binding_mode_fg;
1829 color_t bg_color = colors.binding_mode_bg;
1831 cairo_set_source_color(&(outputs_walk->buffer), colors.binding_mode_border);
1832 cairo_rectangle(outputs_walk->buffer.cr, workspace_width, logical_px(1),
1833 binding.width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1),
1834 font.height + 2 * logical_px(ws_voff_px) - 2 * logical_px(1));
1835 cairo_fill(outputs_walk->buffer.cr);
1837 cairo_set_source_color(&(outputs_walk->buffer), bg_color);
1838 cairo_rectangle(outputs_walk->buffer.cr, workspace_width + logical_px(1), 2 * logical_px(1),
1839 binding.width + 2 * logical_px(ws_hoff_px),
1840 font.height + 2 * logical_px(ws_voff_px) - 4 * logical_px(1));
1841 cairo_fill(outputs_walk->buffer.cr);
1843 set_font_colors(outputs_walk->buffer.gc, fg_color.colorpixel, bg_color.colorpixel);
1844 draw_text(binding.name,
1845 outputs_walk->buffer.id,
1846 outputs_walk->buffer.gc,
1847 workspace_width + logical_px(ws_hoff_px) + logical_px(1),
1848 logical_px(ws_voff_px),
1852 workspace_width += 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + binding.width;
1855 if (!TAILQ_EMPTY(&statusline_head)) {
1856 DLOG("Printing statusline!\n");
1858 int tray_width = get_tray_width(outputs_walk->trayclients);
1859 uint32_t max_statusline_width = outputs_walk->rect.w - workspace_width - tray_width - 2 * logical_px(sb_hoff_px);
1861 /* If the statusline is too long, try to use short texts. */
1862 if (statusline_width > max_statusline_width) {
1863 /* If the currently rendered statusline is long, render a short status line */
1864 refresh_statusline(true);
1865 rendered_statusline_is_short = true;
1866 } else if (rendered_statusline_is_short) {
1867 /* If the currently rendered statusline is short, render a long status line */
1868 refresh_statusline(false);
1869 rendered_statusline_is_short = false;
1872 /* Luckily we already prepared a seperate pixmap containing the rendered
1873 * statusline, we just have to copy the relevant parts to the relevant
1875 int visible_statusline_width = MIN(statusline_width, max_statusline_width);
1876 int x_src = (int16_t)(statusline_width - visible_statusline_width);
1877 int x_dest = (int16_t)(outputs_walk->rect.w - tray_width - logical_px(sb_hoff_px) - visible_statusline_width);
1879 cairo_set_source_surface(outputs_walk->buffer.cr, statusline_surface.surface, x_dest - x_src, 0);
1880 cairo_rectangle(outputs_walk->buffer.cr, x_dest, 0, (int16_t)visible_statusline_width, (int16_t)bar_height);
1881 cairo_fill(outputs_walk->buffer.cr);
1884 workspace_width = 0;
1887 /* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */
1889 config.hidden_state == S_SHOW ||
1892 } else if (config.hide_on_modifier == M_HIDE) {
1900 * Redraw the bars, i.e. simply copy the buffer to the barwindow
1903 void redraw_bars(void) {
1904 i3_output *outputs_walk;
1905 SLIST_FOREACH(outputs_walk, outputs, slist) {
1906 if (!outputs_walk->active) {
1909 cairo_set_source_surface(outputs_walk->bar.cr, outputs_walk->buffer.surface, 0, 0);
1910 cairo_rectangle(outputs_walk->bar.cr, 0, 0, outputs_walk->rect.w, outputs_walk->rect.h);
1911 cairo_fill(outputs_walk->bar.cr);
1912 xcb_flush(xcb_connection);
1917 * Set the current binding mode
1920 void set_current_mode(struct mode *current) {
1921 I3STRING_FREE(binding.name);
1923 activated_mode = binding.name != NULL;