diff --git a/i3bar/include/common.h b/i3bar/include/common.h index 7ab3441f..aa706bbe 100644 --- a/i3bar/include/common.h +++ b/i3bar/include/common.h @@ -31,6 +31,14 @@ typedef enum { ALIGN_RIGHT } blockalign_t; +/* This data structure describes the way a status block should be rendered. These + * variables are updated each time the statusline is re-rendered. */ +struct status_block_render_desc { + uint32_t width; + uint32_t x_offset; + uint32_t x_append; +}; + /* This data structure represents one JSON dictionary, multiple of these make * up one status line. */ struct status_block { @@ -56,11 +64,9 @@ struct status_block { /* The amount of pixels necessary to render a separater after the block. */ uint32_t sep_block_width; - /* The amount of pixels necessary to render this block. These variables are - * only temporarily used in refresh_statusline(). */ - uint32_t width; - uint32_t x_offset; - uint32_t x_append; + /* Continuously-updated information on how to render this status block. */ + struct status_block_render_desc full_render; + struct status_block_render_desc short_render; /* Optional */ char *name; diff --git a/i3bar/include/outputs.h b/i3bar/include/outputs.h index 87cc023e..ec09e764 100644 --- a/i3bar/include/outputs.h +++ b/i3bar/include/outputs.h @@ -46,8 +46,14 @@ struct i3_output { int ws; /* The number of the currently visible ws */ rect rect; /* The rect (relative to the root window) */ - /* Off-screen buffer for preliminary rendering. */ + /* Off-screen buffer for preliminary rendering of the bar. */ surface_t buffer; + /* Off-screen buffer for pre-rendering the statusline, separated to make clipping easier. */ + surface_t statusline_buffer; + /* How much of statusline_buffer's horizontal space was used on last statusline render. */ + int statusline_width; + /* Whether statusline block short texts where used on last statusline render. */ + bool statusline_short_text; /* The actual window on which we draw. */ surface_t bar; diff --git a/i3bar/src/ipc.c b/i3bar/src/ipc.c index eb48afea..1214954d 100644 --- a/i3bar/src/ipc.c +++ b/i3bar/src/ipc.c @@ -63,7 +63,6 @@ void got_output_reply(char *reply) { DLOG("Parsing outputs JSON...\n"); parse_outputs_json(reply); DLOG("Reconfiguring windows...\n"); - realloc_sl_buffer(); reconfig_windows(false); i3_output *o_walk; @@ -175,7 +174,6 @@ void got_bar_config_update(char *event) { /* update fonts and colors */ init_xcb_late(config.fontname); init_colors(&(config.colors)); - realloc_sl_buffer(); draw_bars(false); } diff --git a/i3bar/src/outputs.c b/i3bar/src/outputs.c index d0d175ca..df3bc69b 100644 --- a/i3bar/src/outputs.c +++ b/i3bar/src/outputs.c @@ -144,10 +144,16 @@ static int outputs_start_map_cb(void *params_) { if (params->cur_key == NULL) { new_output = smalloc(sizeof(i3_output)); new_output->name = NULL; + new_output->active = false; + new_output->primary = false; + new_output->visible = false; new_output->ws = 0, + new_output->statusline_width = 0; + new_output->statusline_short_text = false; memset(&new_output->rect, 0, sizeof(rect)); memset(&new_output->bar, 0, sizeof(surface_t)); memset(&new_output->buffer, 0, sizeof(surface_t)); + memset(&new_output->statusline_buffer, 0, sizeof(surface_t)); new_output->workspaces = smalloc(sizeof(struct ws_head)); TAILQ_INIT(new_output->workspaces); diff --git a/i3bar/src/xcb.c b/i3bar/src/xcb.c index 989b06c7..841570e2 100644 --- a/i3bar/src/xcb.c +++ b/i3bar/src/xcb.c @@ -74,11 +74,6 @@ int bar_height; int xkb_base; int mod_pressed = 0; -/* Because the statusline is the same on all outputs, we have - * global buffer to render it on */ -surface_t statusline_surface; -uint32_t statusline_width; - /* Event watchers, to interact with the user */ ev_prepare *xcb_prep; ev_check *xcb_chk; @@ -165,7 +160,7 @@ int get_tray_width(struct tc_head *trayclients) { * Draws a separator for the given block if necessary. * */ -static void draw_separator(uint32_t x, struct status_block *block) { +static void draw_separator(i3_output *output, uint32_t x, struct status_block *block) { uint32_t sep_offset = get_sep_offset(block); if (TAILQ_NEXT(block, blocks) == NULL || sep_offset == 0) return; @@ -173,7 +168,7 @@ static void draw_separator(uint32_t x, struct status_block *block) { uint32_t center_x = x - sep_offset; if (config.separator_symbol == NULL) { /* Draw a classic one pixel, vertical separator. */ - draw_util_rectangle(&statusline_surface, colors.sep_fg, + draw_util_rectangle(&output->statusline_buffer, colors.sep_fg, center_x, logical_px(sep_voff_px), logical_px(1), @@ -181,119 +176,138 @@ static void draw_separator(uint32_t x, struct status_block *block) { } else { /* Draw a custom separator. */ uint32_t separator_x = MAX(x - block->sep_block_width, center_x - separator_symbol_width / 2); - draw_util_text(config.separator_symbol, &statusline_surface, colors.sep_fg, colors.bar_bg, + draw_util_text(config.separator_symbol, &output->statusline_buffer, colors.sep_fg, colors.bar_bg, separator_x, logical_px(ws_voff_px), x - separator_x); } } -/* - * Redraws the statusline to the buffer - * - */ -void refresh_statusline(bool use_short_text) { +uint32_t predict_statusline_length(bool use_short_text) { + uint32_t width = 0; struct status_block *block; - uint32_t old_statusline_width = statusline_width; - statusline_width = 0; - - /* Predict the text width of all blocks (in pixels). */ TAILQ_FOREACH(block, &statusline_head, blocks) { - /* Try to use the shorter text if necessary and possible. */ + i3String *text = block->full_text; + struct status_block_render_desc *render = &block->full_render; if (use_short_text && block->short_text != NULL) { - I3STRING_FREE(block->full_text); - block->full_text = i3string_copy(block->short_text); + text = block->short_text; + render = &block->short_render; } - if (i3string_get_num_bytes(block->full_text) == 0) + if (i3string_get_num_bytes(text) == 0) continue; - block->width = predict_text_width(block->full_text); - /* Add padding for the border if we have to draw it. */ + render->width = predict_text_width(text); if (block->border) - block->width += logical_px(2); + render->width += logical_px(2); /* Compute offset and append for text aligment in min_width. */ - if (block->min_width <= block->width) { - block->x_offset = 0; - block->x_append = 0; + if (block->min_width <= render->width) { + render->x_offset = 0; + render->x_append = 0; } else { - uint32_t padding_width = block->min_width - block->width; + uint32_t padding_width = block->min_width - render->width; switch (block->align) { case ALIGN_LEFT: - block->x_append = padding_width; + render->x_append = padding_width; break; case ALIGN_RIGHT: - block->x_offset = padding_width; + render->x_offset = padding_width; break; case ALIGN_CENTER: - block->x_offset = padding_width / 2; - block->x_append = padding_width / 2 + padding_width % 2; + render->x_offset = padding_width / 2; + render->x_append = padding_width / 2 + padding_width % 2; break; } } + width += render->width + render->x_offset + render->x_append; + /* If this is not the last block, add some pixels for a separator. */ if (TAILQ_NEXT(block, blocks) != NULL) - statusline_width += block->sep_block_width; - - statusline_width += block->width + block->x_offset + block->x_append; + width += block->sep_block_width; } - /* If the statusline is bigger than our screen we need to make sure that - * the pixmap provides enough space, so re-allocate if the width grew */ - if (statusline_width > root_screen->width_in_pixels && - statusline_width > old_statusline_width) - realloc_sl_buffer(); + return width; +} - /* Clear the statusline pixmap. */ - draw_util_clear_surface(&statusline_surface, colors.bar_bg); +/* + * Redraws the statusline to the output's statusline_buffer + */ +void draw_statusline(i3_output *output, uint32_t clip_left, bool use_short_text) { + struct status_block *block; - /* Draw the text of each block. */ - uint32_t x = 0; + draw_util_clear_surface(&output->statusline_buffer, colors.bar_bg); + + /* Use unsigned integer wraparound to clip off the left side. + * For example, if clip_left is 75, then x will start at the very large + * number INT_MAX-75, which is way outside the surface dimensions. Drawing + * to that x position is a no-op which XCB and Cairo safely ignore. Once x moves + * up by 75 and goes past INT_MAX, it will wrap around again to 0, and we start + * actually rendering content to the surface. */ + uint32_t x = 0 - clip_left; + + /* Draw the text of each block */ TAILQ_FOREACH(block, &statusline_head, blocks) { - if (i3string_get_num_bytes(block->full_text) == 0) + i3String *text = block->full_text; + struct status_block_render_desc *render = &block->full_render; + if (use_short_text && block->short_text != NULL) { + text = block->short_text; + render = &block->short_render; + } + + if (i3string_get_num_bytes(text) == 0) continue; - color_t fg_color = (block->color ? draw_util_hex_to_color(block->color) : colors.bar_fg); - int border_width = (block->border) ? logical_px(1) : 0; - if (block->border || block->background || block->urgent) { - if (block->urgent) - fg_color = colors.urgent_ws_fg; + color_t fg_color; + if (block->urgent) { + fg_color = colors.urgent_ws_fg; + } else if (block->color) { + fg_color = draw_util_hex_to_color(block->color); + } else { + fg_color = colors.bar_fg; + } + color_t bg_color = colors.bar_bg; + + int border_width = (block->border) ? logical_px(1) : 0; + int full_render_width = render->width + render->x_offset + render->x_append; + if (block->border || block->background || block->urgent) { /* Let's determine the colors first. */ color_t border_color = colors.bar_bg; - color_t bg_color = colors.bar_bg; if (block->urgent) { border_color = colors.urgent_ws_border; bg_color = colors.urgent_ws_bg; } else { if (block->border) border_color = draw_util_hex_to_color(block->border); - if (block->background) bg_color = draw_util_hex_to_color(block->background); } /* Draw the border. */ - draw_util_rectangle(&statusline_surface, border_color, + draw_util_rectangle(&output->statusline_buffer, border_color, x, logical_px(1), - block->width + block->x_offset + block->x_append, + full_render_width, bar_height - logical_px(2)); /* Draw the background. */ - draw_util_rectangle(&statusline_surface, bg_color, + draw_util_rectangle(&output->statusline_buffer, bg_color, x + border_width, logical_px(1) + border_width, - block->width + block->x_offset + block->x_append - 2 * border_width, + full_render_width - 2 * border_width, bar_height - 2 * border_width - logical_px(2)); } - draw_util_text(block->full_text, &statusline_surface, fg_color, colors.bar_bg, - x + block->x_offset + border_width, logical_px(ws_voff_px), block->width - 2 * border_width); - x += block->width + block->sep_block_width + block->x_offset + block->x_append; + draw_util_text(text, &output->statusline_buffer, fg_color, bg_color, + x + render->x_offset + border_width, logical_px(ws_voff_px), + render->width - 2 * border_width); + x += full_render_width; /* If this is not the last block, draw a separator. */ - draw_separator(x, block); + if (TAILQ_NEXT(block, blocks) != NULL) { + x += block->sep_block_width; + draw_separator(output, x, block); + } } } @@ -426,7 +440,6 @@ void handle_button(xcb_button_press_event_t *event) { } int32_t x = event->event_x >= 0 ? event->event_x : 0; - int32_t original_x = x; DLOG("Got button %d\n", event->detail); @@ -449,21 +462,28 @@ void handle_button(xcb_button_press_event_t *event) { * check if a status block has been clicked. */ int tray_width = get_tray_width(walk->trayclients); int block_x = 0, last_block_x; - int offset = walk->rect.w - statusline_width - tray_width - logical_px(sb_hoff_px); + int offset = walk->rect.w - walk->statusline_width - tray_width - logical_px(sb_hoff_px); + int32_t statusline_x = x - offset; - x = original_x - offset; - if (x >= 0 && (size_t)x < statusline_width) { + if (statusline_x >= 0 && statusline_x < walk->statusline_width) { struct status_block *block; int sep_offset_remainder = 0; TAILQ_FOREACH(block, &statusline_head, blocks) { - if (i3string_get_num_bytes(block->full_text) == 0) + i3String *text = block->full_text; + struct status_block_render_desc *render = &block->full_render; + if (walk->statusline_short_text && block->short_text != NULL) { + text = block->short_text; + render = &block->short_render; + } + + if (i3string_get_num_bytes(text) == 0) continue; last_block_x = block_x; - block_x += block->width + block->x_offset + block->x_append + get_sep_offset(block) + sep_offset_remainder; + block_x += render->width + render->x_offset + render->x_append + get_sep_offset(block) + sep_offset_remainder; - if (x <= block_x && x >= last_block_x) { + if (statusline_x <= block_x && statusline_x >= last_block_x) { send_block_clicked(event->detail, block->name, block->instance, event->root_x, event->root_y); return; } @@ -471,7 +491,6 @@ void handle_button(xcb_button_press_event_t *event) { sep_offset_remainder = block->sep_block_width - get_sep_offset(block); } } - x = original_x; } /* If a custom command was specified for this mouse button, it overrides @@ -1147,17 +1166,6 @@ char *init_xcb_early() { colormap = root_screen->default_colormap; visual_type = get_visualtype(root_screen); - /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and - * this way, we can choose to crop it */ - xcb_pixmap_t statusline_id = xcb_generate_id(xcb_connection); - xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection, - depth, - statusline_id, - xcb_root, - root_screen->width_in_pixels, - root_screen->height_in_pixels); - draw_util_surface_init(&statusline_surface, statusline_id, root_screen->width_in_pixels, root_screen->height_in_pixels); - /* The various watchers to communicate with xcb */ xcb_io = smalloc(sizeof(ev_io)); xcb_prep = smalloc(sizeof(ev_prepare)); @@ -1176,9 +1184,6 @@ char *init_xcb_early() { char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen); - if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline buffer")) - exit(EXIT_FAILURE); - return path; } @@ -1510,29 +1515,6 @@ void destroy_window(i3_output *output) { output->bar.id = XCB_NONE; } -/* - * Reallocate the statusline buffer - * - */ -void realloc_sl_buffer(void) { - DLOG("Re-allocating statusline buffer, statusline_width = %d, root_screen->width_in_pixels = %d\n", - statusline_width, root_screen->width_in_pixels); - xcb_free_pixmap(xcb_connection, statusline_surface.id); - draw_util_surface_free(&statusline_surface); - - xcb_pixmap_t statusline_id = xcb_generate_id(xcb_connection); - xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection, - depth, - statusline_id, - xcb_root, - MAX(root_screen->width_in_pixels, statusline_width), - bar_height); - draw_util_surface_init(&statusline_surface, statusline_id, root_screen->width_in_pixels, root_screen->height_in_pixels); - - if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline buffer")) - exit(EXIT_FAILURE); -} - /* Strut partial tells i3 where to reserve space for i3bar. This is determined * by the `position` bar config directive. */ xcb_void_cookie_t config_strut_partial(i3_output *output) { @@ -1600,6 +1582,7 @@ void reconfig_windows(bool redraw_bars) { xcb_window_t bar_id = xcb_generate_id(xcb_connection); xcb_pixmap_t buffer_id = xcb_generate_id(xcb_connection); + xcb_pixmap_t statusline_buffer_id = xcb_generate_id(xcb_connection); mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP; values[0] = colors.bar_bg.colorpixel; @@ -1643,6 +1626,14 @@ void reconfig_windows(bool redraw_bars) { walk->rect.w, bar_height); + /* The double-buffer we use to render the statusline before copying to buffer */ + xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection, + depth, + statusline_buffer_id, + bar_id, + walk->rect.w, + bar_height); + /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */ xcb_void_cookie_t class_cookie; class_cookie = xcb_change_property(xcb_connection, @@ -1680,6 +1671,7 @@ void reconfig_windows(bool redraw_bars) { draw_util_surface_init(&walk->bar, bar_id, walk->rect.w, bar_height); draw_util_surface_init(&walk->buffer, buffer_id, walk->rect.w, bar_height); + draw_util_surface_init(&walk->statusline_buffer, statusline_buffer_id, walk->rect.w, bar_height); xcb_void_cookie_t strut_cookie = config_strut_partial(walk); @@ -1691,6 +1683,7 @@ void reconfig_windows(bool redraw_bars) { if (xcb_request_failed(win_cookie, "Could not create window") || xcb_request_failed(pm_cookie, "Could not create pixmap") || + xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") || xcb_request_failed(dock_cookie, "Could not set dock mode") || xcb_request_failed(class_cookie, "Could not set WM_CLASS") || xcb_request_failed(name_cookie, "Could not set WM_NAME") || @@ -1737,6 +1730,9 @@ void reconfig_windows(bool redraw_bars) { DLOG("Destroying buffer for output %s\n", walk->name); xcb_free_pixmap(xcb_connection, walk->buffer.id); + DLOG("Destroying statusline buffer for output %s\n", walk->name); + xcb_free_pixmap(xcb_connection, walk->statusline_buffer.id); + DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]); xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection, walk->bar.id, @@ -1759,10 +1755,20 @@ void reconfig_windows(bool redraw_bars) { walk->rect.w, bar_height); + DLOG("Recreating statusline buffer for output %s\n", walk->name); + xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection, + depth, + walk->statusline_buffer.id, + walk->bar.id, + walk->rect.w, + bar_height); + draw_util_surface_free(&(walk->bar)); draw_util_surface_free(&(walk->buffer)); + draw_util_surface_free(&(walk->statusline_buffer)); draw_util_surface_init(&(walk->bar), walk->bar.id, walk->rect.w, bar_height); draw_util_surface_init(&(walk->buffer), walk->buffer.id, walk->rect.w, bar_height); + draw_util_surface_init(&(walk->statusline_buffer), walk->statusline_buffer.id, walk->rect.w, bar_height); xcb_void_cookie_t map_cookie, umap_cookie; if (redraw_bars) { @@ -1787,6 +1793,7 @@ void reconfig_windows(bool redraw_bars) { if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") || xcb_request_failed(chg_cookie, "Could not change window") || xcb_request_failed(pm_cookie, "Could not create pixmap") || + xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") || xcb_request_failed(strut_cookie, "Could not set strut") || (redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") || (config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) { @@ -1802,14 +1809,14 @@ void reconfig_windows(bool redraw_bars) { */ void draw_bars(bool unhide) { DLOG("Drawing bars...\n"); - int workspace_width = 0; - /* Is the currently-rendered statusline using short_text items? */ - bool rendered_statusline_is_short = false; - refresh_statusline(false); + uint32_t full_statusline_width = predict_statusline_length(false); + uint32_t short_statusline_width = predict_statusline_length(true); i3_output *outputs_walk; SLIST_FOREACH(outputs_walk, outputs, slist) { + int workspace_width = 0; + if (!outputs_walk->active) { DLOG("Output %s inactive, skipping...\n", outputs_walk->name); continue; @@ -1906,30 +1913,28 @@ void draw_bars(bool unhide) { int tray_width = get_tray_width(outputs_walk->trayclients); uint32_t max_statusline_width = outputs_walk->rect.w - workspace_width - tray_width - 2 * logical_px(sb_hoff_px); + uint32_t clip_left = 0; + uint32_t statusline_width = full_statusline_width; + bool use_short_text = false; - /* If the statusline is too long, try to use short texts. */ if (statusline_width > max_statusline_width) { - /* If the currently rendered statusline is long, render a short status line */ - refresh_statusline(true); - rendered_statusline_is_short = true; - } else if (rendered_statusline_is_short) { - /* If the currently rendered statusline is short, render a long status line */ - refresh_statusline(false); - rendered_statusline_is_short = false; + statusline_width = short_statusline_width; + use_short_text = true; + if (statusline_width > max_statusline_width) { + clip_left = statusline_width - max_statusline_width; + } } - /* Luckily we already prepared a seperate pixmap containing the rendered - * statusline, we just have to copy the relevant parts to the relevant - * position */ - int visible_statusline_width = MIN(statusline_width, max_statusline_width); - int x_src = (int16_t)(statusline_width - visible_statusline_width); - int x_dest = (int16_t)(outputs_walk->rect.w - tray_width - logical_px(sb_hoff_px) - visible_statusline_width); + int16_t visible_statusline_width = MIN(statusline_width, max_statusline_width); + int x_dest = outputs_walk->rect.w - tray_width - logical_px(sb_hoff_px) - visible_statusline_width; - draw_util_copy_surface(&statusline_surface, &(outputs_walk->buffer), x_src, 0, - x_dest, 0, (int16_t)visible_statusline_width, (int16_t)bar_height); + draw_statusline(outputs_walk, clip_left, use_short_text); + draw_util_copy_surface(&outputs_walk->statusline_buffer, &outputs_walk->buffer, 0, 0, + x_dest, 0, visible_statusline_width, (int16_t)bar_height); + + outputs_walk->statusline_width = statusline_width; + outputs_walk->statusline_short_text = use_short_text; } - - workspace_width = 0; } /* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */