2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010-2011 Axel Wagner and contributors
6 * See file LICNSE for license information
8 * src/xcb.c: Communicating with X
12 #include <xcb/xproto.h>
13 #include <xcb/xcb_atom.h>
25 #include <X11/XKBlib.h>
26 #include <X11/extensions/XKB.h>
30 #if defined(__APPLE__)
34 * Returns a pointer to a new string which is a duplicate of the
35 * string, but only copies at most n characters.
38 char *strndup(const char *str, size_t n) {
42 for (len = 0; len < n && str[len]; len++)
45 if ((copy = malloc(len + 1)) == NULL)
47 memcpy(copy, str, len);
54 /* We save the Atoms in an easy to access array, indexed by an enum */
56 #define ATOM_DO(name) name,
57 #include "xcb_atoms.def"
61 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
62 xcb_atom_t atoms[NUM_ATOMS];
64 /* Variables, that are the same for all functions at all times */
65 xcb_connection_t *xcb_connection;
66 xcb_screen_t *xcb_screen;
67 xcb_window_t xcb_root;
70 /* We need to cache some data to speed up text-width-prediction */
71 xcb_query_font_reply_t *font_info;
73 xcb_charinfo_t *font_table;
75 /* These are only relevant for XKB, which we only need for grabbing modifiers */
80 /* Because the statusline is the same on all outputs, we have
81 * global buffer to render it on */
82 xcb_gcontext_t statusline_ctx;
83 xcb_gcontext_t statusline_clear;
84 xcb_pixmap_t statusline_pm;
85 uint32_t statusline_width;
87 /* Event-Watchers, to interact with the user */
93 /* The parsed colors */
97 uint32_t active_ws_fg;
98 uint32_t active_ws_bg;
99 uint32_t inactive_ws_fg;
100 uint32_t inactive_ws_bg;
101 uint32_t urgent_ws_bg;
102 uint32_t urgent_ws_fg;
103 uint32_t focus_ws_bg;
104 uint32_t focus_ws_fg;
106 struct xcb_colors_t colors;
108 /* We define xcb_request_failed as a macro to include the relevant line-number */
109 #define xcb_request_failed(cookie, err_msg) _xcb_request_failed(cookie, err_msg, __LINE__)
110 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
111 xcb_generic_error_t *err;
112 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
113 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
114 return err->error_code;
120 * Predicts the length of text based on cached data.
121 * The string has to be encoded in ucs2 and glyph_len has to be the length
122 * of the string (in glyphs).
125 uint32_t predict_text_extents(xcb_char2b_t *text, uint32_t length) {
126 /* If we don't have per-character data, return the maximum width */
127 if (font_table == NULL) {
128 return (font_info->max_bounds.character_width * length);
134 for (i = 0; i < length; i++) {
135 xcb_charinfo_t *info;
136 int row = text[i].byte1;
137 int col = text[i].byte2;
139 if (row < font_info->min_byte1 || row > font_info->max_byte1 ||
140 col < font_info->min_char_or_byte2 || col > font_info->max_char_or_byte2) {
144 /* Don't you ask me, how this one works… */
145 info = &font_table[((row - font_info->min_byte1) *
146 (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) +
147 (col - font_info->min_char_or_byte2)];
149 if (info->character_width != 0 ||
150 (info->right_side_bearing |
151 info->left_side_bearing |
153 info->descent) != 0) {
154 width += info->character_width;
162 * Draws text given in UCS-2-encoding to a given drawable and position
165 void draw_text(xcb_drawable_t drawable, xcb_gcontext_t ctx, int16_t x, int16_t y,
166 xcb_char2b_t *text, uint32_t glyph_count) {
169 int16_t font_ascent = font_info->font_ascent;
171 while (glyph_count > 0) {
172 uint8_t chunk_size = MIN(255, glyph_count);
173 uint32_t chunk_width = predict_text_extents(text + offset, chunk_size);
175 xcb_image_text_16(xcb_connection,
179 pos_x, y + font_ascent,
182 offset += chunk_size;
183 pos_x += chunk_width;
184 glyph_count -= chunk_size;
189 * Converts a colorstring to a colorpixel as expected from xcb_change_gc.
190 * s is assumed to be in the format "rrggbb"
193 uint32_t get_colorpixel(const char *s) {
194 char strings[3][3] = { { s[0], s[1], '\0'} ,
195 { s[2], s[3], '\0'} ,
196 { s[4], s[5], '\0'} };
197 uint8_t r = strtol(strings[0], NULL, 16);
198 uint8_t g = strtol(strings[1], NULL, 16);
199 uint8_t b = strtol(strings[2], NULL, 16);
200 return (r << 16 | g << 8 | b);
204 * Redraws the statusline to the buffer
207 void refresh_statusline() {
210 if (statusline == NULL) {
214 xcb_char2b_t *text = (xcb_char2b_t*) convert_utf8_to_ucs2(statusline, &glyph_count);
215 statusline_width = predict_text_extents(text, glyph_count);
217 xcb_rectangle_t rect = { 0, 0, xcb_screen->width_in_pixels, font_height };
218 xcb_poly_fill_rectangle(xcb_connection, statusline_pm, statusline_clear, 1, &rect);
219 draw_text(statusline_pm, statusline_ctx, 0, 0, text, glyph_count);
225 * Hides all bars (unmaps them)
229 if (!config.hide_on_modifier) {
234 SLIST_FOREACH(walk, outputs, slist) {
238 xcb_unmap_window(xcb_connection, walk->bar);
244 * Unhides all bars (maps them)
248 if (!config.hide_on_modifier) {
253 xcb_void_cookie_t cookie;
259 SLIST_FOREACH(walk, outputs, slist) {
260 if (walk->bar == XCB_NONE) {
263 mask = XCB_CONFIG_WINDOW_X |
264 XCB_CONFIG_WINDOW_Y |
265 XCB_CONFIG_WINDOW_WIDTH |
266 XCB_CONFIG_WINDOW_HEIGHT |
267 XCB_CONFIG_WINDOW_STACK_MODE;
268 values[0] = walk->rect.x;
269 values[1] = walk->rect.y + walk->rect.h - font_height - 6;
270 values[2] = walk->rect.w;
271 values[3] = font_height + 6;
272 values[4] = XCB_STACK_MODE_ABOVE;
273 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
274 cookie = xcb_configure_window_checked(xcb_connection,
279 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
282 xcb_map_window(xcb_connection, walk->bar);
287 * Parse the colors into a format that we can use
290 void init_colors(const struct xcb_color_strings_t *new_colors) {
291 #define PARSE_COLOR(name, def) \
293 colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
295 PARSE_COLOR(bar_fg, "FFFFFF");
296 PARSE_COLOR(bar_bg, "000000");
297 PARSE_COLOR(active_ws_fg, "FFFFFF");
298 PARSE_COLOR(active_ws_bg, "480000");
299 PARSE_COLOR(inactive_ws_fg, "FFFFFF");
300 PARSE_COLOR(inactive_ws_bg, "240000");
301 PARSE_COLOR(urgent_ws_fg, "FFFFFF");
302 PARSE_COLOR(urgent_ws_bg, "002400");
303 PARSE_COLOR(focus_ws_fg, "FFFFFF");
304 PARSE_COLOR(focus_ws_bg, "480000");
309 * Handle a button-press-event (i.c. a mouse click on one of our bars).
310 * We determine, wether the click occured on a ws-button or if the scroll-
311 * wheel was used and change the workspace appropriately
314 void handle_button(xcb_button_press_event_t *event) {
317 /* Determine, which bar was clicked */
319 xcb_window_t bar = event->event;
320 SLIST_FOREACH(walk, outputs, slist) {
321 if (walk->bar == bar) {
327 DLOG("Unknown Bar klicked!\n");
331 /* TODO: Move this to extern get_ws_for_output() */
332 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
333 if (cur_ws->visible) {
338 if (cur_ws == NULL) {
339 DLOG("No Workspace active?\n");
343 int32_t x = event->event_x;
345 DLOG("Got Button %d\n", event->detail);
347 switch (event->detail) {
349 /* Left Mousbutton. We determine, which button was clicked
350 * and set cur_ws accordingly */
351 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
353 if (x < cur_ws->name_width + 10) {
356 x -= cur_ws->name_width + 10;
358 if (cur_ws == NULL) {
363 /* Mouse wheel down. We select the next ws */
364 if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
365 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
367 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
371 /* Mouse wheel up. We select the previos ws */
372 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
373 cur_ws = TAILQ_FIRST(walk->workspaces);
375 cur_ws = TAILQ_NEXT(cur_ws, tailq);
380 char buffer[strlen(cur_ws->name) + 11];
381 snprintf(buffer, 50, "workspace %s", cur_ws->name);
382 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
386 * This function is called immediately bevor the main loop locks. We flush xcb
387 * then (and only then)
390 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
391 xcb_flush(xcb_connection);
395 * This function is called immediately after the main loop locks, so when one
396 * of the watchers registered an event.
397 * We check wether an X-Event arrived and handle it.
400 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
401 xcb_generic_event_t *event;
402 while ((event = xcb_poll_for_event(xcb_connection)) == NULL) {
406 switch (event->response_type & ~0x80) {
408 /* Expose-events happen, when the window needs to be redrawn */
411 case XCB_BUTTON_PRESS:
412 /* Button-press-events are mouse-buttons clicked on one of our bars */
413 handle_button((xcb_button_press_event_t*) event);
420 * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
424 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
428 * We need to bind to the modifier per XKB. Sadly, XCB does not implement this
431 void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
435 DLOG("Got XKB-Event!\n");
437 while (XPending(xkb_dpy)) {
438 XNextEvent(xkb_dpy, (XEvent*)&ev);
440 if (ev.type != xkb_event_base) {
441 ELOG("No Xkb-Event!\n");
445 if (ev.any.xkb_type != XkbStateNotify) {
446 ELOG("No State Notify!\n");
450 unsigned int mods = ev.state.mods;
451 modstate = mods & Mod4Mask;
454 if (modstate != mod_pressed) {
456 DLOG("Mod4 got released!\n");
459 DLOG("Mod4 got pressed!\n");
462 mod_pressed = modstate;
467 * Initialize xcb and use the specified fontname for text-rendering
470 char *init_xcb(char *fontname) {
471 /* FIXME: xcb_connect leaks Memory */
472 xcb_connection = xcb_connect(NULL, NULL);
473 if (xcb_connection_has_error(xcb_connection)) {
474 ELOG("Cannot open display\n");
477 DLOG("Connected to xcb\n");
479 /* We have to request the atoms we need */
480 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
481 #include "xcb_atoms.def"
483 xcb_screen = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
484 xcb_root = xcb_screen->root;
486 /* We load and allocate the font */
487 xcb_font = xcb_generate_id(xcb_connection);
488 xcb_void_cookie_t open_font_cookie;
489 open_font_cookie = xcb_open_font_checked(xcb_connection,
494 /* We need to save info about the font, because we need the fonts height and
495 * information about the width of characters */
496 xcb_query_font_cookie_t query_font_cookie;
497 query_font_cookie = xcb_query_font(xcb_connection,
500 /* To grab modifiers without blocking other applications from receiving key-events
501 * involving that modifier, we sadly have to use xkb which is not yet fully supported
503 if (config.hide_on_modifier) {
504 int xkb_major, xkb_minor, xkb_errbase, xkb_err;
505 xkb_major = XkbMajorVersion;
506 xkb_minor = XkbMinorVersion;
508 char *dispname = getenv("DISPLAY");
509 if (dispname == NULL) {
512 xkb_dpy = XkbOpenDisplay(dispname,
519 if (xkb_dpy == NULL) {
524 if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
525 ELOG("Could not set FD_CLOEXEC on xkbdpy: %s\n", strerror(errno));
530 if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
531 ELOG("XKB not supported by X-server!\n");
535 if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
536 ELOG("Could not grab Key!\n");
540 xkb_io = malloc(sizeof(ev_io));
541 ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
542 ev_io_start(main_loop, xkb_io);
546 /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
547 * this way, we can choose to crop it */
548 uint32_t mask = XCB_GC_FOREGROUND;
549 uint32_t vals[3] = { colors.bar_bg, colors.bar_bg, xcb_font };
551 statusline_clear = xcb_generate_id(xcb_connection);
552 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
558 mask |= XCB_GC_BACKGROUND | XCB_GC_FONT;
559 vals[0] = colors.bar_fg;
560 statusline_ctx = xcb_generate_id(xcb_connection);
561 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
567 statusline_pm = xcb_generate_id(xcb_connection);
568 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
569 xcb_screen->root_depth,
572 xcb_screen->width_in_pixels,
573 xcb_screen->height_in_pixels);
576 /* The varios Watchers to communicate with xcb */
577 xcb_io = malloc(sizeof(ev_io));
578 xcb_prep = malloc(sizeof(ev_prepare));
579 xcb_chk = malloc(sizeof(ev_check));
581 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
582 ev_prepare_init(xcb_prep, &xcb_prep_cb);
583 ev_check_init(xcb_chk, &xcb_chk_cb);
585 ev_io_start(main_loop, xcb_io);
586 ev_prepare_start(main_loop, xcb_prep);
587 ev_check_start(main_loop, xcb_chk);
589 /* Now we get the atoms and save them in a nice data-structure */
592 xcb_get_property_cookie_t path_cookie;
593 path_cookie = xcb_get_property_unchecked(xcb_connection,
596 atoms[I3_SOCKET_PATH],
597 XCB_GET_PROPERTY_TYPE_ANY,
600 /* We check, if i3 set it's socket-path */
601 xcb_get_property_reply_t *path_reply = xcb_get_property_reply(xcb_connection,
606 int len = xcb_get_property_value_length(path_reply);
608 path = strndup(xcb_get_property_value(path_reply), len);
612 /* Now we save the font-infos */
613 font_info = xcb_query_font_reply(xcb_connection,
617 if (xcb_request_failed(open_font_cookie, "Could not open font")) {
621 font_height = font_info->font_ascent + font_info->font_descent;
623 if (xcb_query_font_char_infos_length(font_info) == 0) {
626 font_table = xcb_query_font_char_infos(font_info);
629 DLOG("Calculated Font-height: %d\n", font_height);
631 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
632 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
633 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
641 * Cleanup the xcb-stuff.
642 * Called once, before the program terminates.
648 SLIST_FOREACH(o_walk, outputs, slist) {
649 destroy_window(o_walk);
650 FREE(o_walk->workspaces);
653 FREE_SLIST(outputs, i3_output);
656 xcb_disconnect(xcb_connection);
658 ev_check_stop(main_loop, xcb_chk);
659 ev_prepare_stop(main_loop, xcb_prep);
660 ev_io_stop(main_loop, xcb_io);
669 * Get the earlier requested atoms and save them in the prepared data-structure
673 xcb_intern_atom_reply_t *reply;
674 #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
675 if (reply == NULL) { \
676 ELOG("Could not get atom %s\n", #name); \
677 exit(EXIT_FAILURE); \
679 atoms[name] = reply->atom; \
682 #include "xcb_atoms.def"
687 * Destroy the bar of the specified output
690 void destroy_window(i3_output *output) {
691 if (output == NULL) {
694 if (output->bar == XCB_NONE) {
697 xcb_destroy_window(xcb_connection, output->bar);
698 output->bar = XCB_NONE;
702 * Reallocate the statusline-buffer
705 void realloc_sl_buffer() {
706 xcb_free_pixmap(xcb_connection, statusline_pm);
707 statusline_pm = xcb_generate_id(xcb_connection);
708 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
709 xcb_screen->root_depth,
712 xcb_screen->width_in_pixels,
713 xcb_screen->height_in_pixels);
715 uint32_t mask = XCB_GC_FOREGROUND;
716 uint32_t vals[3] = { colors.bar_bg, colors.bar_bg, xcb_font };
717 xcb_free_gc(xcb_connection, statusline_clear);
718 statusline_clear = xcb_generate_id(xcb_connection);
719 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
725 mask |= XCB_GC_BACKGROUND | XCB_GC_FONT;
726 vals[0] = colors.bar_fg;
727 statusline_ctx = xcb_generate_id(xcb_connection);
728 xcb_free_gc(xcb_connection, statusline_ctx);
729 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
735 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
736 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
737 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
744 * Reconfigure all bars and create new for newly activated outputs
747 void reconfig_windows() {
752 SLIST_FOREACH(walk, outputs, slist) {
754 /* If an output is not active, we destroy it's bar */
755 /* FIXME: Maybe we rather want to unmap? */
756 DLOG("Destroying window for output %s\n", walk->name);
757 destroy_window(walk);
760 if (walk->bar == XCB_NONE) {
761 DLOG("Creating Window for output %s\n", walk->name);
763 walk->bar = xcb_generate_id(xcb_connection);
764 walk->buffer = xcb_generate_id(xcb_connection);
765 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
766 /* Black background */
767 values[0] = colors.bar_bg;
768 /* If hide_on_modifier is set, i3 is not supposed to manage our bar-windows */
769 values[1] = config.hide_on_modifier;
770 /* The events we want to receive */
771 values[2] = XCB_EVENT_MASK_EXPOSURE;
772 if (!config.disable_ws) {
773 values[2] |= XCB_EVENT_MASK_BUTTON_PRESS;
775 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
776 xcb_screen->root_depth,
779 walk->rect.x, walk->rect.y + walk->rect.h - font_height - 6,
780 walk->rect.w, font_height + 6,
782 XCB_WINDOW_CLASS_INPUT_OUTPUT,
783 xcb_screen->root_visual,
787 /* The double-buffer we use to render stuff off-screen */
788 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
789 xcb_screen->root_depth,
795 /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
797 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
798 XCB_PROP_MODE_REPLACE,
800 atoms[_NET_WM_WINDOW_TYPE],
804 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
806 /* We need to tell i3, where to reserve space for i3bar */
807 /* left, right, top, bottom, left_start_y, left_end_y,
808 * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
810 /* A local struct to save the strut_partial property */
816 uint32_t left_start_y;
818 uint32_t right_start_y;
819 uint32_t right_end_y;
820 uint32_t top_start_x;
822 uint32_t bottom_start_x;
823 uint32_t bottom_end_x;
824 } __attribute__((__packed__)) strut_partial = {0,};
825 switch (config.dockpos) {
829 strut_partial.top = font_height + 6;
830 strut_partial.top_start_x = walk->rect.x;
831 strut_partial.top_end_x = walk->rect.x + walk->rect.w;
834 strut_partial.bottom = font_height + 6;
835 strut_partial.bottom_start_x = walk->rect.x;
836 strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
839 xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
840 XCB_PROP_MODE_REPLACE,
842 atoms[_NET_WM_STRUT_PARTIAL],
848 /* We also want a graphics-context for the bars (it defines the properties
849 * with which we draw to them) */
850 walk->bargc = xcb_generate_id(xcb_connection);
852 values[0] = xcb_font;
853 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
859 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
860 xcb_void_cookie_t map_cookie;
861 if (!config.hide_on_modifier) {
862 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
865 if (xcb_request_failed(win_cookie, "Could not create window") ||
866 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
867 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
868 xcb_request_failed(strut_cookie, "Could not set strut") ||
869 xcb_request_failed(gc_cookie, "Could not create graphical context") ||
870 (!config.hide_on_modifier && xcb_request_failed(map_cookie, "Could not map window"))) {
874 /* We already have a bar, so we just reconfigure it */
875 mask = XCB_CONFIG_WINDOW_X |
876 XCB_CONFIG_WINDOW_Y |
877 XCB_CONFIG_WINDOW_WIDTH |
878 XCB_CONFIG_WINDOW_HEIGHT |
879 XCB_CONFIG_WINDOW_STACK_MODE;
880 values[0] = walk->rect.x;
881 values[1] = walk->rect.y + walk->rect.h - font_height - 6;
882 values[2] = walk->rect.w;
883 values[3] = font_height + 6;
884 values[4] = XCB_STACK_MODE_ABOVE;
886 DLOG("Destroying buffer for output %s", walk->name);
887 xcb_free_pixmap(xcb_connection, walk->buffer);
889 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
890 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
895 DLOG("Recreating buffer for output %s", walk->name);
896 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
897 xcb_screen->root_depth,
903 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window")) {
906 if (xcb_request_failed(pm_cookie, "Could not create pixmap")) {
914 * Render the bars, with buttons and statusline
918 DLOG("Drawing Bars...\n");
921 refresh_statusline();
923 i3_output *outputs_walk;
924 SLIST_FOREACH(outputs_walk, outputs, slist) {
925 if (!outputs_walk->active) {
926 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
929 if (outputs_walk->bar == XCB_NONE) {
930 /* Oh shit, an active output without an own bar. Create it now! */
933 /* First things first: clear the backbuffer */
934 uint32_t color = colors.bar_bg;
935 xcb_change_gc(xcb_connection,
939 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
940 xcb_poly_fill_rectangle(xcb_connection,
941 outputs_walk->buffer,
946 if (statusline != NULL) {
947 DLOG("Printing statusline!\n");
949 /* Luckily we already prepared a seperate pixmap containing the rendered
950 * statusline, we just have to copy the relevant parts to the relevant
952 xcb_copy_area(xcb_connection,
954 outputs_walk->buffer,
956 MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
957 MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - 4)), 3,
958 MIN(outputs_walk->rect.w - 4, statusline_width), font_height);
961 if (config.disable_ws) {
966 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
967 DLOG("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
968 uint32_t fg_color = colors.inactive_ws_fg;
969 uint32_t bg_color = colors.inactive_ws_bg;
970 if (ws_walk->visible) {
971 if (!ws_walk->focused) {
972 fg_color = colors.active_ws_fg;
973 bg_color = colors.active_ws_bg;
975 fg_color = colors.focus_ws_fg;
976 bg_color = colors.focus_ws_bg;
979 if (ws_walk->urgent) {
980 DLOG("WS %s is urgent!\n", ws_walk->name);
981 fg_color = colors.urgent_ws_fg;
982 bg_color = colors.urgent_ws_bg;
983 /* The urgent-hint should get noticed, so we unhide the bars shortly */
986 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
987 uint32_t vals[] = { bg_color, bg_color };
988 xcb_change_gc(xcb_connection,
992 xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
993 xcb_poly_fill_rectangle(xcb_connection,
994 outputs_walk->buffer,
998 xcb_change_gc(xcb_connection,
1002 xcb_image_text_16(xcb_connection,
1003 ws_walk->name_glyphs,
1004 outputs_walk->buffer,
1005 outputs_walk->bargc,
1006 i + 5, font_info->font_ascent + 2,
1007 ws_walk->ucs2_name);
1008 i += 10 + ws_walk->name_width;
1018 * Redraw the bars, i.e. simply copy the buffer to the barwindow
1021 void redraw_bars() {
1022 i3_output *outputs_walk;
1023 SLIST_FOREACH(outputs_walk, outputs, slist) {
1024 if (!outputs_walk->active) {
1027 xcb_copy_area(xcb_connection,
1028 outputs_walk->buffer,
1030 outputs_walk->bargc,
1033 outputs_walk->rect.w,
1034 outputs_walk->rect.h);
1035 xcb_flush(xcb_connection);