menu: use wl_list instead of array

...to make it easier to split menu.c into smaller chunks without
exposing nr_menus variable.
This commit is contained in:
Johan Malm 2023-10-09 20:59:04 +01:00 committed by Johan Malm
parent b22722dafa
commit a105c8781a
5 changed files with 29 additions and 38 deletions

View file

@ -320,6 +320,7 @@ struct server {
struct theme *theme; struct theme *theme;
struct menu *menu_current; struct menu *menu_current;
struct wl_list menus;
}; };
#define LAB_NR_LAYERS (4) #define LAB_NR_LAYERS (4)

View file

@ -60,6 +60,7 @@ struct menu {
/* Used to match a window-menu to the view that triggered it. */ /* Used to match a window-menu to the view that triggered it. */
struct view *triggered_by_view; /* may be NULL */ struct view *triggered_by_view; /* may be NULL */
struct wl_list link; /* server.menus */
}; };
/* For keyboard support */ /* For keyboard support */
@ -70,14 +71,14 @@ void menu_submenu_leave(struct server *server);
bool menu_call_selected_actions(struct server *server); bool menu_call_selected_actions(struct server *server);
void menu_init(struct server *server); void menu_init(struct server *server);
void menu_finish(void); void menu_finish(struct server *server);
/** /**
* menu_get_by_id - get menu by id * menu_get_by_id - get menu by id
* *
* @id id string defined in menu.xml like "root-menu" * @id id string defined in menu.xml like "root-menu"
*/ */
struct menu *menu_get_by_id(const char *id); struct menu *menu_get_by_id(struct server *server, const char *id);
/** /**
* menu_open - open menu on position (x, y) * menu_open - open menu on position (x, y)

View file

@ -493,7 +493,7 @@ show_menu(struct server *server, struct view *view, const char *menu_name)
} }
bool force_menu_top_left = false; bool force_menu_top_left = false;
struct menu *menu = menu_get_by_id(menu_name); struct menu *menu = menu_get_by_id(server, menu_name);
if (!menu) { if (!menu) {
return; return;
} }

View file

@ -180,7 +180,7 @@ main(int argc, char *argv[])
server_finish(&server); server_finish(&server);
menu_finish(); menu_finish(&server);
theme_finish(&theme); theme_finish(&theme);
rcxml_finish(); rcxml_finish();
font_finish(); font_finish();

View file

@ -33,22 +33,14 @@ static struct action *current_item_action;
static int menu_level; static int menu_level;
static struct menu *current_menu; static struct menu *current_menu;
/* vector for <menu id="" label=""> elements */
static struct menu *menus;
static int nr_menus, alloc_menus;
/* TODO: split this whole file into parser.c and actions.c*/ /* TODO: split this whole file into parser.c and actions.c*/
static struct menu * static struct menu *
menu_create(struct server *server, const char *id, const char *label) menu_create(struct server *server, const char *id, const char *label)
{ {
if (nr_menus == alloc_menus) { struct menu *menu = znew(*menu);
alloc_menus = (alloc_menus + 16) * 2; wl_list_append(&server->menus, &menu->link);
menus = xrealloc(menus, alloc_menus * sizeof(struct menu));
}
struct menu *menu = menus + nr_menus;
memset(menu, 0, sizeof(*menu));
nr_menus++;
wl_list_init(&menu->menuitems); wl_list_init(&menu->menuitems);
menu->id = xstrdup(id); menu->id = xstrdup(id);
menu->label = xstrdup(label ? label : id); menu->label = xstrdup(label ? label : id);
@ -62,14 +54,13 @@ menu_create(struct server *server, const char *id, const char *label)
} }
struct menu * struct menu *
menu_get_by_id(const char *id) menu_get_by_id(struct server *server, const char *id)
{ {
if (!id) { if (!id) {
return NULL; return NULL;
} }
struct menu *menu; struct menu *menu;
for (int i = 0; i < nr_menus; ++i) { wl_list_for_each(menu, &server->menus, link) {
menu = menus + i;
if (!strcmp(menu->id, id)) { if (!strcmp(menu->id, id)) {
return menu; return menu;
} }
@ -124,8 +115,7 @@ static void
post_processing(struct server *server) post_processing(struct server *server)
{ {
struct menu *menu; struct menu *menu;
for (int i = 0; i < nr_menus; ++i) { wl_list_for_each(menu, &server->menus, link) {
menu = menus + i;
menu_update_width(menu); menu_update_width(menu);
} }
} }
@ -149,8 +139,9 @@ validate_menu(struct menu *menu)
static void static void
validate(struct server *server) validate(struct server *server)
{ {
for (int i = 0; i < nr_menus; ++i) { struct menu *menu;
validate_menu(menus + i); wl_list_for_each(menu, &server->menus, link) {
validate_menu(menu);
} }
} }
@ -473,7 +464,7 @@ handle_menu_element(xmlNode *n, struct server *server)
* <menu id=""> creates an entry which points to a menu * <menu id=""> creates an entry which points to a menu
* defined elsewhere * defined elsewhere
*/ */
struct menu *menu = menu_get_by_id(id); struct menu *menu = menu_get_by_id(server, id);
if (menu) { if (menu) {
current_item = item_create(current_menu, menu->label, true); current_item = item_create(current_menu, menu->label, true);
if (current_item) { if (current_item) {
@ -663,15 +654,14 @@ menu_configure(struct menu *menu, int lx, int ly, enum menu_align align)
} }
static void static void
menu_hide_submenu(const char *id) menu_hide_submenu(struct server *server, const char *id)
{ {
struct menu *menu, *hide_menu; struct menu *menu, *hide_menu;
hide_menu = menu_get_by_id(id); hide_menu = menu_get_by_id(server, id);
if (!hide_menu) { if (!hide_menu) {
return; return;
} }
for (int i = 0; i < nr_menus; ++i) { wl_list_for_each(menu, &server->menus, link) {
menu = menus + i;
bool should_reposition = false; bool should_reposition = false;
struct menuitem *item, *next; struct menuitem *item, *next;
wl_list_for_each_safe(item, next, &menu->menuitems, link) { wl_list_for_each_safe(item, next, &menu->menuitems, link) {
@ -697,7 +687,7 @@ menu_hide_submenu(const char *id)
static void static void
init_rootmenu(struct server *server) init_rootmenu(struct server *server)
{ {
struct menu *menu = menu_get_by_id("root-menu"); struct menu *menu = menu_get_by_id(server, "root-menu");
/* Default menu if no menu.xml found */ /* Default menu if no menu.xml found */
if (!menu) { if (!menu) {
@ -715,7 +705,7 @@ init_rootmenu(struct server *server)
static void static void
init_windowmenu(struct server *server) init_windowmenu(struct server *server)
{ {
struct menu *menu = menu_get_by_id("client-menu"); struct menu *menu = menu_get_by_id(server, "client-menu");
/* Default menu if no menu.xml found */ /* Default menu if no menu.xml found */
if (!menu) { if (!menu) {
@ -754,13 +744,14 @@ init_windowmenu(struct server *server)
} }
if (wl_list_length(&rc.workspace_config.workspaces) == 1) { if (wl_list_length(&rc.workspace_config.workspaces) == 1) {
menu_hide_submenu("workspaces"); menu_hide_submenu(server, "workspaces");
} }
} }
void void
menu_init(struct server *server) menu_init(struct server *server)
{ {
wl_list_init(&server->menus);
parse_xml("menu.xml", server); parse_xml("menu.xml", server);
init_rootmenu(server); init_rootmenu(server);
init_windowmenu(server); init_windowmenu(server);
@ -769,11 +760,10 @@ menu_init(struct server *server)
} }
void void
menu_finish(void) menu_finish(struct server *server)
{ {
struct menu *menu; struct menu *menu, *tmp_menu;
for (int i = 0; i < nr_menus; ++i) { wl_list_for_each_safe(menu, tmp_menu, &server->menus, link) {
menu = menus + i;
struct menuitem *item, *next; struct menuitem *item, *next;
wl_list_for_each_safe(item, next, &menu->menuitems, link) { wl_list_for_each_safe(item, next, &menu->menuitems, link) {
item_destroy(item); item_destroy(item);
@ -783,10 +773,9 @@ menu_finish(void)
* including node descriptors and scaled_font_buffers. * including node descriptors and scaled_font_buffers.
*/ */
wlr_scene_node_destroy(&menu->scene_tree->node); wlr_scene_node_destroy(&menu->scene_tree->node);
wl_list_remove(&menu->link);
zfree(menu);
} }
zfree(menus);
alloc_menus = 0;
nr_menus = 0;
} }
/* Sets selection (or clears selection if passing NULL) */ /* Sets selection (or clears selection if passing NULL) */
@ -1049,7 +1038,7 @@ menu_close_root(struct server *server)
void void
menu_reconfigure(struct server *server) menu_reconfigure(struct server *server)
{ {
menu_finish(); menu_finish(server);
server->menu_current = NULL; server->menu_current = NULL;
menu_init(server); menu_init(server);
} }