From 033f21a1f222d14d60134dd7306cdd673988d565 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Mon, 16 Jan 2023 22:28:45 -0500 Subject: [PATCH] Pre-expose modules ~ Attempt to pre expose modules to calculate proper width/height ~ Doesn't really work, but also doesn't segfault, so a win in my book --- bar/bar.c | 148 +++++++++++++++++++++++++++++++++++--------------- bar/wayland.c | 18 +++--- particle.h | 2 +- 3 files changed, 114 insertions(+), 54 deletions(-) diff --git a/bar/bar.c b/bar/bar.c index c87ffc7..d53c8fc 100644 --- a/bar/bar.c +++ b/bar/bar.c @@ -35,7 +35,8 @@ * also be preformed on the pre-given value. */ static void -accum_widths(const struct section *s, const struct private *b, +accum_widths( + const struct section *s, const struct private *b, int *out, int (*act)(int a, int b)) { for (size_t i = 0; i < s->count; i++) { @@ -104,6 +105,7 @@ calculate_widths(const struct private *b, int *left, int *center, int *right) assert(*right >= 0); } +#if 0 /* * Calculate total height of left/center/right groups. * Note: begin_expose() must have been called @@ -131,7 +133,7 @@ calculate_heights (const struct private *b, int *top, int *middle, int *bottom) assert(*middle >= 0); assert(*bottom >= 0); } - +#endif /* * 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. @@ -140,13 +142,13 @@ calculate_heights (const struct private *b, int *top, int *middle, int *bottom) static int min_bar_width (const struct private *b) { - int max = 0; + int min = 0; - accum_widths(&(b->left), b, &max, &larger); - accum_widths(&(b->center), b, &max, &larger); - accum_widths(&(b->right), b, &max, &larger); + accum_widths(&(b->left), b, &min, &larger); + accum_widths(&(b->center), b, &min, &larger); + accum_widths(&(b->right), b, &min, &larger); - return max; + return min; } /* @@ -156,16 +158,106 @@ min_bar_width (const struct private *b) */ static int min_bar_height (const struct private *b) +{ + LOG_INFO("Mining"); + int min = 0; + + accum_heights(&(b->left), b, &min, &larger); + accum_heights(&(b->center), b, &min, &larger); + accum_heights(&(b->right), b, &min, &larger); + + 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_heights(&(b->left), b, &max, &larger); - accum_heights(&(b->center), b, &max, &larger); - accum_heights(&(b->right), b, &max, &larger); + 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) +{ + for (size_t i = 0; i < s->count; i++) + { + struct module *m = s->mods[i]; + struct exposable *e = s->exps[i]; + if (e != NULL) + e->destroy(e); + s->exps[i] = module_begin_expose(m); + assert(s->exps[i]->width >= 0); + } +} + +static void +bar_recalc_size(struct private *bar) +{ + LOG_INFO("Bar width auto? %s", + bar->width < 0 ? "yes" : "no"); + LOG_INFO("Bar height auto? %s", + bar->height < 0 ? "yes" : "no"); + + begin_expose_mods(&(bar->left)); + begin_expose_mods(&(bar->center)); + begin_expose_mods(&(bar->right)); + + if (bar->height < 0){ + if (bar->location == BAR_TOP || bar->location == BAR_BOTTOM) + bar->height_with_border = + 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; + } 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) + bar->width_with_border = + 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; + }else + bar->width_with_border = + bar->width + bar->border.top_width + bar->border.bottom_width; + + LOG_INFO("Bar width calculated size %d", + bar->width); + LOG_INFO("Bar height calculated size %d", + bar->height); +} + static void expose(const struct bar *_bar) { @@ -199,33 +291,6 @@ expose(const struct bar *_bar) bar->border.bottom_width}, }); - for (size_t i = 0; i < bar->left.count; i++) { - struct module *m = bar->left.mods[i]; - struct exposable *e = bar->left.exps[i]; - if (e != NULL) - e->destroy(e); - bar->left.exps[i] = module_begin_expose(m); - assert(bar->left.exps[i]->width >= 0); - } - - for (size_t i = 0; i < bar->center.count; i++) { - struct module *m = bar->center.mods[i]; - struct exposable *e = bar->center.exps[i]; - if (e != NULL) - e->destroy(e); - bar->center.exps[i] = module_begin_expose(m); - assert(bar->center.exps[i]->width >= 0); - } - - for (size_t i = 0; i < bar->right.count; i++) { - struct module *m = bar->right.mods[i]; - struct exposable *e = bar->right.exps[i]; - if (e != NULL) - e->destroy(e); - bar->right.exps[i] = module_begin_expose(m); - assert(bar->right.exps[i]->width >= 0); - } - int left_width, center_width, right_width; calculate_widths(bar, &left_width, ¢er_width, &right_width); @@ -278,7 +343,8 @@ expose(const struct bar *_bar) static void refresh(const struct bar *bar) { - const struct private *b = bar->private; + struct private *b = bar->private; + bar_recalc_size(b); b->backend.iface->refresh(bar); } @@ -391,11 +457,7 @@ run(struct bar *_bar) { struct private *bar = _bar->private; - bar->height_with_border = - bar->height + bar->border.top_width + bar->border.bottom_width; - - bar->width_with_border = - bar->width + bar->border.left_width + bar->border.right_width; + bar_recalc_size(bar); if (!bar->backend.iface->setup(_bar)) { bar->backend.iface->cleanup(_bar); diff --git a/bar/wayland.c b/bar/wayland.c index 2bc89cf..fb61254 100644 --- a/bar/wayland.c +++ b/bar/wayland.c @@ -1044,13 +1044,16 @@ update_size(struct wayland_backend *backend) backend->scale = scale; - int height = bar->height; - bar->height = height - bar->border.top_width - bar->border.bottom_width; - bar->height_with_border = height; + 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); if (bar->location == BAR_TOP || bar->location == BAR_BOTTOM) { - zwlr_layer_surface_v1_set_size( - backend->layer_surface, 0, bar->height_with_border / scale); zwlr_layer_surface_v1_set_exclusive_zone( backend->layer_surface, (bar->height_with_border + (bar->location == BAR_TOP @@ -1058,8 +1061,6 @@ update_size(struct wayland_backend *backend) : bar->border.top_margin)) / scale); } else { - zwlr_layer_surface_v1_set_size( - backend->layer_surface, bar->width_with_border / scale, 0); zwlr_layer_surface_v1_set_exclusive_zone( backend->layer_surface, (bar->width_with_border + (bar->location == BAR_LEFT @@ -1085,9 +1086,6 @@ update_size(struct wayland_backend *backend) return false; } - bar->width = backend->width; - bar->height = backend->height; - /* Reload buffers */ if (backend->next_buffer != NULL) backend->next_buffer->busy = false; diff --git a/particle.h b/particle.h index 92ae9ad..2cfecb6 100644 --- a/particle.h +++ b/particle.h @@ -52,7 +52,7 @@ struct exposable { void *private; int width; /* Should be set by begin_expose(), at latest */ - int height; + int height; /* Should be set by begin_expose(), at latest */ char *on_click[MOUSE_BTN_COUNT];