]> git.mdlowis.com Git - proto/labwc.git/commitdiff
rcxml: use fixed arrays for rc.title_buttons_*
authorJohn Lindgren <john@jlindgren.net>
Tue, 9 Sep 2025 02:35:23 +0000 (22:35 -0400)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sat, 13 Sep 2025 11:06:52 +0000 (12:06 +0100)
These are just lists of enum lab_node_type, with a bounded size and
no middle-insertions/removals, so linked lists are overkill.

Also, the use of wl_list_for_each[_reverse] just to access the first or
last entry in the list (corner button) was weird.

include/config/rcxml.h
src/config/rcxml.c
src/ssd/ssd-titlebar.c
src/theme.c

index 33c493767b27dcabd84f4d43e1ecbbf03d5e440e..89e215c0ea1cc527a4e6cd6dbf934501e732448d 100644 (file)
@@ -14,6 +14,9 @@
 
 #define BUTTON_MAP_MAX 16
 
+/* max of one button of each type (no repeats) */
+#define TITLE_BUTTONS_MAX ((LAB_NODE_BUTTON_LAST + 1) - LAB_NODE_BUTTON_FIRST)
+
 enum adaptive_sync_mode {
        LAB_ADAPTIVE_SYNC_DISABLED,
        LAB_ADAPTIVE_SYNC_ENABLED,
@@ -48,11 +51,6 @@ struct button_map_entry {
        uint32_t to;
 };
 
-struct title_button {
-       enum lab_node_type type;
-       struct wl_list link;
-};
-
 struct usable_area_override {
        struct border margin;
        char *output;
@@ -88,8 +86,12 @@ struct rcxml {
        char *theme_name;
        char *icon_theme_name;
        char *fallback_app_icon_name;
-       struct wl_list title_buttons_left;
-       struct wl_list title_buttons_right;
+
+       enum lab_node_type title_buttons_left[TITLE_BUTTONS_MAX];
+       int nr_title_buttons_left;
+       enum lab_node_type title_buttons_right[TITLE_BUTTONS_MAX];
+       int nr_title_buttons_right;
+
        int corner_radius;
        bool show_title;
        bool title_layout_loaded;
index 351953459b71d144f422abdbec84cea8008c96f1..7473344122d6b824716bdd7f25b38c27be2ba5c7 100644 (file)
@@ -116,7 +116,8 @@ parse_window_type(const char *type)
  * desk         D           All-desktops toggle (aka omnipresent)
  */
 static void
-fill_section(const char *content, struct wl_list *list, uint32_t *found_buttons)
+fill_section(const char *content, enum lab_node_type *buttons, int *count,
+               uint32_t *found_buttons /* bitmask */)
 {
        gchar **identifiers = g_strsplit(content, ",", -1);
        for (size_t i = 0; identifiers[i]; ++i) {
@@ -162,9 +163,8 @@ fill_section(const char *content, struct wl_list *list, uint32_t *found_buttons)
 
                *found_buttons |= (1 << type);
 
-               struct title_button *item = znew(*item);
-               item->type = type;
-               wl_list_append(list, &item->link);
+               assert(*count < TITLE_BUTTONS_MAX);
+               buttons[(*count)++] = type;
        }
        g_strfreev(identifiers);
 }
@@ -172,15 +172,8 @@ fill_section(const char *content, struct wl_list *list, uint32_t *found_buttons)
 static void
 clear_title_layout(void)
 {
-       struct title_button *button, *button_tmp;
-       wl_list_for_each_safe(button, button_tmp, &rc.title_buttons_left, link) {
-               wl_list_remove(&button->link);
-               zfree(button);
-       }
-       wl_list_for_each_safe(button, button_tmp, &rc.title_buttons_right, link) {
-               wl_list_remove(&button->link);
-               zfree(button);
-       }
+       rc.nr_title_buttons_left = 0;
+       rc.nr_title_buttons_right = 0;
        rc.title_layout_loaded = false;
 }
 
@@ -189,11 +182,6 @@ fill_title_layout(char *content)
 {
        clear_title_layout();
 
-       struct wl_list *sections[] = {
-               &rc.title_buttons_left,
-               &rc.title_buttons_right,
-       };
-
        gchar **parts = g_strsplit(content, ":", -1);
 
        if (g_strv_length(parts) != 2) {
@@ -202,9 +190,10 @@ fill_title_layout(char *content)
        }
 
        uint32_t found_buttons = 0;
-       for (size_t i = 0; parts[i]; ++i) {
-               fill_section(parts[i], sections[i], &found_buttons);
-       }
+       fill_section(parts[0], rc.title_buttons_left,
+               &rc.nr_title_buttons_left, &found_buttons);
+       fill_section(parts[1], rc.title_buttons_right,
+               &rc.nr_title_buttons_right, &found_buttons);
 
        rc.title_layout_loaded = true;
 err:
@@ -1362,8 +1351,6 @@ rcxml_init(void)
        static bool has_run;
 
        if (!has_run) {
-               wl_list_init(&rc.title_buttons_left);
-               wl_list_init(&rc.title_buttons_right);
                wl_list_init(&rc.usable_area_overrides);
                wl_list_init(&rc.keybinds);
                wl_list_init(&rc.mousebinds);
index 595a203d5299a0fe7b42b5c5a50abd63e9301ef4..13f5e7dfa3ee7396cfc92a73751e7e3e2e404b1b 100644 (file)
@@ -81,7 +81,6 @@ ssd_titlebar_create(struct ssd *ssd)
                        LAB_NODE_TITLE, view, /*data*/ NULL);
 
                /* Buttons */
-               struct title_button *b;
                int x = theme->window_titlebar_padding_width;
 
                /* Center vertically within titlebar */
@@ -90,20 +89,22 @@ ssd_titlebar_create(struct ssd *ssd)
                wl_list_init(&subtree->buttons_left);
                wl_list_init(&subtree->buttons_right);
 
-               wl_list_for_each(b, &rc.title_buttons_left, link) {
+               for (int b = 0; b < rc.nr_title_buttons_left; b++) {
+                       enum lab_node_type type = rc.title_buttons_left[b];
                        struct lab_img **imgs =
-                               theme->window[active].button_imgs[b->type];
-                       attach_ssd_button(&subtree->buttons_left, b->type, parent,
+                               theme->window[active].button_imgs[type];
+                       attach_ssd_button(&subtree->buttons_left, type, parent,
                                imgs, x, y, view);
                        x += theme->window_button_width + theme->window_button_spacing;
                }
 
                x = width - theme->window_titlebar_padding_width + theme->window_button_spacing;
-               wl_list_for_each_reverse(b, &rc.title_buttons_right, link) {
+               for (int b = rc.nr_title_buttons_right - 1; b >= 0; b--) {
                        x -= theme->window_button_width + theme->window_button_spacing;
+                       enum lab_node_type type = rc.title_buttons_right[b];
                        struct lab_img **imgs =
-                               theme->window[active].button_imgs[b->type];
-                       attach_ssd_button(&subtree->buttons_right, b->type, parent,
+                               theme->window[active].button_imgs[type];
+                       attach_ssd_button(&subtree->buttons_right, type, parent,
                                imgs, x, y, view);
                }
        }
@@ -223,8 +224,8 @@ update_visible_buttons(struct ssd *ssd)
        int width = MAX(view->current.width - 2 * theme->window_titlebar_padding_width, 0);
        int button_width = theme->window_button_width;
        int button_spacing = theme->window_button_spacing;
-       int button_count_left = wl_list_length(&rc.title_buttons_left);
-       int button_count_right = wl_list_length(&rc.title_buttons_right);
+       int button_count_left = rc.nr_title_buttons_left;
+       int button_count_right = rc.nr_title_buttons_right;
 
        /* Make sure infinite loop never occurs */
        assert(button_width > 0);
index f0b481584c962b756e5afac4a47fb2766436ae4d..d5279367ed74e978731678349644c8b6e8daee46 100644 (file)
@@ -231,25 +231,16 @@ load_button(struct theme *theme, struct button *b, int active)
        struct lab_img **rounded_img =
                &button_imgs[b->type][b->state_set | LAB_BS_ROUNDED];
 
-       struct title_button *leftmost_button;
-       wl_list_for_each(leftmost_button,
-                       &rc.title_buttons_left, link) {
-               if (leftmost_button->type == b->type) {
-                       *rounded_img = lab_img_copy(*img);
-                       lab_img_add_modifier(*rounded_img,
-                               round_left_corner_button);
-               }
-               break;
-       }
-       struct title_button *rightmost_button;
-       wl_list_for_each_reverse(rightmost_button,
-                       &rc.title_buttons_right, link) {
-               if (rightmost_button->type == b->type) {
-                       *rounded_img = lab_img_copy(*img);
-                       lab_img_add_modifier(*rounded_img,
-                               round_right_corner_button);
-               }
-               break;
+       if (rc.nr_title_buttons_left > 0
+                       && b->type == rc.title_buttons_left[0]) {
+               *rounded_img = lab_img_copy(*img);
+               lab_img_add_modifier(*rounded_img, round_left_corner_button);
+       }
+       if (rc.nr_title_buttons_right > 0
+                       && b->type == rc.title_buttons_right
+                               [rc.nr_title_buttons_right - 1]) {
+               *rounded_img = lab_img_copy(*img);
+               lab_img_add_modifier(*rounded_img, round_right_corner_button);
        }
 }