diff --git a/src/menu/menu.c b/src/menu/menu.c
index 461c3153..dd9f7f59 100644
--- a/src/menu/menu.c
+++ b/src/menu/menu.c
@@ -17,7 +17,6 @@
#include "common/font.h"
#include "common/lab-scene-rect.h"
#include "common/list.h"
-#include "common/macros.h"
#include "common/mem.h"
#include "common/nodename.h"
#include "common/scaled-font-buffer.h"
@@ -38,11 +37,13 @@
#define ICON_SIZE (rc.theme->menu_item_height - 2 * rc.theme->menu_items_padding_y)
/* state-machine variables for processing */
-static bool in_item;
-static struct menuitem *current_item;
-static struct action *current_item_action;
-
-static struct menu *current_menu;
+struct menu_parse_context {
+ struct server *server;
+ struct menu *menu;
+ struct menuitem *item;
+ struct action *action;
+ bool in_item;
+};
static bool waiting_for_pipe_menu;
static struct menuitem *selected_item;
@@ -72,7 +73,8 @@ is_unique_id(struct server *server, const char *id)
}
static struct menu *
-menu_create(struct server *server, const char *id, const char *label)
+menu_create(struct server *server, struct menu *parent, const char *id,
+ const char *label)
{
if (!is_unique_id(server, id)) {
wlr_log(WLR_ERROR, "menu id %s already exists", id);
@@ -84,7 +86,7 @@ menu_create(struct server *server, const char *id, const char *label)
wl_list_init(&menu->menuitems);
menu->id = xstrdup(id);
menu->label = xstrdup(label ? label : id);
- menu->parent = current_menu;
+ menu->parent = parent;
menu->server = server;
menu->is_pipemenu_child = waiting_for_pipe_menu;
return menu;
@@ -467,33 +469,33 @@ menu_create_scene(struct menu *menu)
*
*/
static void
-fill_item(const char *nodename, const char *content)
+fill_item(struct menu_parse_context *ctx, const char *nodename,
+ const char *content)
{
/* - defines the start of a new item */
if (!strcmp(nodename, "label")) {
- current_item = item_create(current_menu, content, false);
- current_item_action = NULL;
- } else if (!current_item) {
+ ctx->item = item_create(ctx->menu, content, false);
+ ctx->action = NULL;
+ } else if (!ctx->item) {
wlr_log(WLR_ERROR, "expect
- element first. "
"nodename: '%s' content: '%s'", nodename, content);
} else if (!strcmp(nodename, "icon")) {
#if HAVE_LIBSFDO
if (rc.menu_show_icons && !string_null_or_empty(content)) {
- xstrdup_replace(current_item->icon_name, content);
- current_menu->has_icons = true;
+ xstrdup_replace(ctx->item->icon_name, content);
+ ctx->menu->has_icons = true;
}
#endif
} else if (!strcmp(nodename, "name.action")) {
- current_item_action = action_create(content);
- if (current_item_action) {
- wl_list_append(¤t_item->actions,
- ¤t_item_action->link);
+ ctx->action = action_create(content);
+ if (ctx->action) {
+ wl_list_append(&ctx->item->actions, &ctx->action->link);
}
- } else if (!current_item_action) {
+ } else if (!ctx->action) {
wlr_log(WLR_ERROR, "expect element first. "
"nodename: '%s' content: '%s'", nodename, content);
} else {
- action_arg_from_xml_node(current_item_action, nodename, content);
+ action_arg_from_xml_node(ctx->action, nodename, content);
}
}
@@ -546,7 +548,8 @@ nodename_supports_cdata(char *nodename)
}
static void
-entry(xmlNode *node, char *nodename, char *content)
+entry(struct menu_parse_context *ctx, xmlNode *node, char *nodename,
+ char *content)
{
if (!nodename) {
return;
@@ -563,7 +566,7 @@ entry(xmlNode *node, char *nodename, char *content)
if (getenv("LABWC_DEBUG_MENU_NODENAMES")) {
printf("%s: %s\n", nodename, content ? content : (char *)cdata);
}
- if (in_item) {
+ if (ctx->in_item) {
/*
* Nodenames for most menu-items end with '.item.menu'
* but top-level pipemenu items do not have the associated
@@ -571,13 +574,13 @@ entry(xmlNode *node, char *nodename, char *content)
*/
string_truncate_at_pattern(nodename, ".item.menu");
string_truncate_at_pattern(nodename, ".item");
- fill_item(nodename, content ? content : (char *)cdata);
+ fill_item(ctx, nodename, content ? content : (char *)cdata);
}
xmlFree(cdata);
}
static void
-process_node(xmlNode *node)
+process_node(struct menu_parse_context *ctx, xmlNode *node)
{
static char buffer[256];
@@ -586,24 +589,24 @@ process_node(xmlNode *node)
return;
}
char *name = nodename(node, buffer, sizeof(buffer));
- entry(node, name, content);
+ entry(ctx, node, name, content);
}
-static void xml_tree_walk(xmlNode *node, struct server *server);
+static void xml_tree_walk(struct menu_parse_context *ctx, xmlNode *node);
static void
-traverse(xmlNode *n, struct server *server)
+traverse(struct menu_parse_context *ctx, xmlNode *n)
{
xmlAttr *attr;
- process_node(n);
+ process_node(ctx, n);
for (attr = n->properties; attr; attr = attr->next) {
- xml_tree_walk(attr->children, server);
+ xml_tree_walk(ctx, attr->children);
}
- xml_tree_walk(n->children, server);
+ xml_tree_walk(ctx, n->children);
}
-static bool parse_buf(struct server *server, struct buf *buf);
+static bool parse_buf(struct menu_parse_context *ctx, struct buf *buf);
static int handle_pipemenu_readable(int fd, uint32_t mask, void *_ctx);
static int handle_pipemenu_timeout(void *_ctx);
@@ -614,7 +617,7 @@ static int handle_pipemenu_timeout(void *_ctx);
* * Menuitem of submenu type - has ID only
*/
static void
-handle_menu_element(xmlNode *n, struct server *server)
+handle_menu_element(struct menu_parse_context *ctx, xmlNode *n)
{
char *label = (char *)xmlGetProp(n, (const xmlChar *)"label");
char *icon_name = (char *)xmlGetProp(n, (const xmlChar *)"icon");
@@ -629,9 +632,10 @@ handle_menu_element(xmlNode *n, struct server *server)
if (execute && label) {
wlr_log(WLR_DEBUG, "pipemenu '%s:%s:%s'", id, label, execute);
- struct menu *pipemenu = menu_create(server, id, label);
+ struct menu *pipemenu =
+ menu_create(ctx->server, ctx->menu, id, label);
pipemenu->execute = xstrdup(execute);
- if (!current_menu) {
+ if (!ctx->menu) {
/*
* A pipemenu may not have its parent like:
*
@@ -641,18 +645,18 @@ handle_menu_element(xmlNode *n, struct server *server)
*
*/
} else {
- current_item = item_create(current_menu, label,
+ ctx->item = item_create(ctx->menu, label,
/* arrow */ true);
- fill_item("icon", icon_name);
- current_item_action = NULL;
- current_item->submenu = pipemenu;
+ fill_item(ctx, "icon", icon_name);
+ ctx->action = NULL;
+ ctx->item->submenu = pipemenu;
}
- } else if ((label && current_menu) || !current_menu) {
+ } else if ((label && ctx->menu) || !ctx->menu) {
/*
- * (label && current_menu) refers to