From 37b5b02fc4a0839142318d4ece2f05a994058e8f Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Tue, 17 Jan 2023 21:23:47 -0500 Subject: [PATCH] Fixes for size setting and config + height is optional + width is optional + make location enum into bitmask --- bar/bar.c | 63 ++++++++++------------------------------------- bar/bar.h | 2 +- bar/wayland.c | 39 ++++++++++++++++++----------- config-verify.c | 5 ++-- config.c | 13 ++++++---- modules/battery.c | 3 ++- 6 files changed, 51 insertions(+), 74 deletions(-) diff --git a/bar/bar.c b/bar/bar.c index d53c8fc..f0dc1d8 100644 --- a/bar/bar.c +++ b/bar/bar.c @@ -62,7 +62,7 @@ accum_heights(const struct section *s, const struct private *b, if (e->height > 0) *out = act(*out, b->top_spacing + e->height + b->bottom_spacing); } -} + } /* Add action */ @@ -169,41 +169,6 @@ min_bar_height (const struct private *b) return min; } -/* - * Calculate the minimum width the bar needs to be to show all particles. - * This assumes the bar is at the left or right of screen. - * NOTE: begin_expose() must have been called - */ -static int -max_bar_width (const struct private *b) -{ - int max = 0; - - accum_widths(&(b->left), b, &max, &add); - accum_widths(&(b->center), b, &max, &add); - accum_widths(&(b->right), b, &max, &add); - LOG_INFO("Max: %d", max); - - return max; -} - -/* - * Calculate the minimum height the bar needs to be to show all particles. - * This assumes the bar is at the top or bottom of the screen. - * NOTE: begin_expose() must have been called - */ -static int -max_bar_height (const struct private *b) -{ - int max = 0; - - accum_heights(&(b->left), b, &max, &add); - accum_heights(&(b->center), b, &max, &add); - accum_heights(&(b->right), b, &max, &add); - - return max; -} - static void begin_expose_mods(const struct section *s) { @@ -221,9 +186,9 @@ begin_expose_mods(const struct section *s) static void bar_recalc_size(struct private *bar) { - LOG_INFO("Bar width auto? %s", + LOG_DBG("Bar width auto? %s", bar->width < 0 ? "yes" : "no"); - LOG_INFO("Bar height auto? %s", + LOG_DBG("Bar height auto? %s", bar->height < 0 ? "yes" : "no"); begin_expose_mods(&(bar->left)); @@ -231,30 +196,28 @@ bar_recalc_size(struct private *bar) begin_expose_mods(&(bar->right)); if (bar->height < 0){ - if (bar->location == BAR_TOP || bar->location == BAR_BOTTOM) + if ((bar->location & (BAR_TOP | BAR_BOTTOM)) == bar->location) bar->height_with_border = - min_bar_height(bar) + bar->border.top_width + bar->border.bottom_width; + min_bar_height(bar) + bar->border.top_width + bar->border.bottom_width; else - bar->height_with_border = - max_bar_height(bar) + bar->border.top_width + bar->border.bottom_width; + bar->height_with_border = 0; } else bar->height_with_border = bar->height + bar->border.top_width + bar->border.bottom_width; if (bar->width < 0){ - if (bar->location == BAR_LEFT || bar->location == BAR_RIGHT) + if ((bar->location & (BAR_LEFT | BAR_RIGHT)) == bar->location) bar->width_with_border = - min_bar_width(bar) + bar->border.left_width + bar->border.right_width; + min_bar_width(bar) + bar->border.left_width + bar->border.right_width; else - bar->width_with_border = - max_bar_width(bar) + bar->border.left_width + bar->border.right_width; + bar->width_with_border = 0; }else bar->width_with_border = bar->width + bar->border.top_width + bar->border.bottom_width; - LOG_INFO("Bar width calculated size %d", + LOG_DBG("Bar width calculated size %d", bar->width); - LOG_INFO("Bar height calculated size %d", + LOG_DBG("Bar height calculated size %d", bar->height); } @@ -293,7 +256,7 @@ expose(const struct bar *_bar) int left_width, center_width, right_width; calculate_widths(bar, &left_width, ¢er_width, &right_width); - +# if 0 int y = bar->border.top_width; int x = bar->border.left_width + bar->left_margin - bar->left_spacing; pixman_region32_t clip; @@ -335,7 +298,7 @@ expose(const struct bar *_bar) if (e->width > 0) x += bar->left_spacing + e->width + bar->right_spacing; } - +#endif bar->backend.iface->commit(_bar); } diff --git a/bar/bar.h b/bar/bar.h index 7ff4e46..54529a1 100644 --- a/bar/bar.h +++ b/bar/bar.h @@ -17,7 +17,7 @@ struct bar { const char *(*output_name)(const struct bar *bar); }; -enum bar_location { BAR_TOP, BAR_BOTTOM, BAR_LEFT, BAR_RIGHT }; +enum bar_location { BAR_TOP = 0b1, BAR_BOTTOM = 0b10, BAR_LEFT = 0b100, BAR_RIGHT = 0b1000 }; enum bar_layer { BAR_LAYER_TOP, BAR_LAYER_BOTTOM }; enum bar_backend { BAR_BACKEND_AUTO, BAR_BACKEND_XCB, BAR_BACKEND_WAYLAND }; diff --git a/bar/wayland.c b/bar/wayland.c index fb61254..835035a 100644 --- a/bar/wayland.c +++ b/bar/wayland.c @@ -1044,16 +1044,28 @@ update_size(struct wayland_backend *backend) backend->scale = scale; - int height = bar->height < 0 ? 0 : bar->height_with_border; - int width = bar->width < 0 ? 0 : bar->width_with_border; - - LOG_INFO("Attempting to set %dx%d", width, height); - // TODO: Somehow set up width and height properly // I need to read more to understand how bar->width and bar->height are used - zwlr_layer_surface_v1_set_size(backend->layer_surface, width / scale, height / scale); + zwlr_layer_surface_v1_set_size( + backend->layer_surface, + bar->width_with_border / scale, + bar->height_with_border / scale + ); - if (bar->location == BAR_TOP || bar->location == BAR_BOTTOM) { + /* Trigger a 'configure' event, after which we'll have the width */ + wl_surface_commit(backend->surface); + wl_display_roundtrip(backend->display); + + if (backend->width == -1 || backend->height == -1) { + LOG_ERR("failed to get panel size"); + return false; + } + + LOG_INFO("backend size: %dx%d", backend->width, backend->height); + bar->width_with_border = backend->width; + bar->height_with_border = backend->height; + + if (bar->location & (BAR_TOP | BAR_BOTTOM)) { zwlr_layer_surface_v1_set_exclusive_zone( backend->layer_surface, (bar->height_with_border + (bar->location == BAR_TOP @@ -1076,15 +1088,12 @@ update_size(struct wayland_backend *backend) bar->border.bottom_margin / scale, bar->border.left_margin / scale ); - - /* Trigger a 'configure' event, after which we'll have the width */ + wl_surface_commit(backend->surface); - wl_display_roundtrip(backend->display); - - if (backend->width == -1 || backend->height == -1) { - LOG_ERR("failed to get panel size"); - return false; - } + // TODO: Figure out why not setting width & height + // make the bar fail to appear. Don't want to have to do this + bar->width = backend->width - (bar->border.left_width + bar->border.right_width); + bar->height = backend->height - (bar->border.top_width + bar->border.bottom_width); /* Reload buffers */ if (backend->next_buffer != NULL) diff --git a/config-verify.c b/config-verify.c index a099ef7..aa1fce9 100644 --- a/config-verify.c +++ b/config-verify.c @@ -421,7 +421,7 @@ verify_bar_border(keychain_t *chain, const struct yml_node *node) static bool verify_bar_location(keychain_t *chain, const struct yml_node *node) { - return conf_verify_enum(chain, node, (const char *[]){"top", "bottom"}, 2); + return conf_verify_enum(chain, node, (const char *[]){"top", "left", "right", "bottom"}, 4); } static bool @@ -442,9 +442,10 @@ conf_verify_bar(const struct yml_node *bar) chain_push(&chain, "bar"); static const struct attr_info attrs[] = { - {"height", true, &conf_verify_unsigned}, {"location", true, &verify_bar_location}, {"background", true, &conf_verify_color}, + {"height", false, &conf_verify_unsigned}, + {"width", false, &conf_verify_unsigned}, {"monitor", false, &conf_verify_string}, {"layer", false, &verify_bar_layer}, diff --git a/config.c b/config.c index 35a27c5..2df0b19 100644 --- a/config.c +++ b/config.c @@ -289,11 +289,14 @@ conf_to_bar(const struct yml_node *bar, enum bar_backend backend) /* * Required attributes */ - const struct yml_node *location = yml_get_value(bar, "location"); - conf.location = strcmp(yml_value_as_string(location), "top") == 0 - ? BAR_TOP : BAR_BOTTOM; - + const char *location_str = yml_value_as_string(location); + conf.location = strcmp(location_str, "top") == 0 + ? BAR_TOP + : strcmp(location_str, "left") == 0 + ? BAR_LEFT + : strcmp(location_str, "right") == 0 + ? BAR_RIGHT : BAR_BOTTOM; const struct yml_node *background = yml_get_value(bar, "background"); conf.background = conf_to_color(background); @@ -309,7 +312,7 @@ conf_to_bar(const struct yml_node *bar, enum bar_backend backend) const struct yml_node *width = yml_get_value(bar, "width"); if (width != NULL) - conf.width = yml_value_as_int(height); + conf.width = yml_value_as_int(width); else conf.width = -1; // Represents 'auto' width diff --git a/modules/battery.c b/modules/battery.c index db00add..31be6bc 100644 --- a/modules/battery.c +++ b/modules/battery.c @@ -136,9 +136,10 @@ content(struct module *mod) m->state == STATE_DISCHARGING ? "discharging" : "unknown"), tag_new_int_range(mod, "capacity", m->capacity, 0, 100), + tag_new_int_range(mod, "charge", 50, 0, 100), tag_new_string(mod, "estimate", estimate), }, - .count = 6, + .count = 7, }; mtx_unlock(&mod->lock);