rcxml: rewrite <region> parser

This commit is contained in:
tokyo4j 2025-04-12 01:48:11 +09:00 committed by Johan Malm
parent a0d2e6a64b
commit 73dd3b8de1

View file

@ -391,40 +391,51 @@ fill_window_switcher_field(char *nodename, char *content, struct parser_state *s
} }
static void static void
fill_region(char *nodename, char *content, struct parser_state *state) fill_region(xmlNode *node)
{ {
string_truncate_at_pattern(nodename, ".region.regions"); struct region *region = znew(*region);
wl_list_append(&rc.regions, &region->link);
if (!strcasecmp(nodename, "region.regions")) { xmlNode *child;
state->current_region = znew(*state->current_region); char *key, *content;
wl_list_append(&rc.regions, &state->current_region->link); LAB_XML_FOR_EACH(node, child, key, content) {
} else if (!content) { if (!strcasecmp(key, "name")) {
/* intentionally left empty */ xstrdup_replace(region->name, content);
} else if (!state->current_region) { } else if (strstr("xywidtheight", key)
wlr_log(WLR_ERROR, "Expecting <region name=\"\" before %s='%s'", && !strchr(content, '%')) {
nodename, content); wlr_log(WLR_ERROR, "Removing invalid region "
} else if (!strcasecmp(nodename, "name")) { "'%s': %s='%s' misses a trailing %%",
/* Prevent leaking memory if config contains multiple names */ region->name, key, content);
if (!state->current_region->name) { wl_list_remove(&region->link);
state->current_region->name = xstrdup(content); zfree(region->name);
} zfree(region);
} else if (strstr("xywidtheight", nodename) && !strchr(content, '%')) { return;
wlr_log(WLR_ERROR, "Removing invalid region '%s': %s='%s' misses" } else if (!strcmp(key, "x")) {
" a trailing %%", state->current_region->name, nodename, content); region->percentage.x = atoi(content);
wl_list_remove(&state->current_region->link); } else if (!strcmp(key, "y")) {
zfree(state->current_region->name); region->percentage.y = atoi(content);
zfree(state->current_region); } else if (!strcmp(key, "width")) {
} else if (!strcmp(nodename, "x")) { region->percentage.width = atoi(content);
state->current_region->percentage.x = atoi(content); } else if (!strcmp(key, "height")) {
} else if (!strcmp(nodename, "y")) { region->percentage.height = atoi(content);
state->current_region->percentage.y = atoi(content);
} else if (!strcmp(nodename, "width")) {
state->current_region->percentage.width = atoi(content);
} else if (!strcmp(nodename, "height")) {
state->current_region->percentage.height = atoi(content);
} else { } else {
wlr_log(WLR_ERROR, "Unexpected data in region parser: %s=\"%s\"", wlr_log(WLR_ERROR, "Unexpected data in region "
nodename, content); "parser: %s=\"%s\"", key, content);
}
}
}
static void
fill_regions(xmlNode *node)
{
/* TODO: make sure <regions> is empty here */
xmlNode *child;
char *key, *content;
LAB_XML_FOR_EACH(node, child, key, content) {
if (!strcasecmp(key, "region")) {
fill_region(child);
}
} }
} }
@ -1057,8 +1068,8 @@ entry(xmlNode *node, char *nodename, char *content, struct parser_state *state)
fill_libinput_category(node); fill_libinput_category(node);
return; return;
} }
if (state->in_regions) { if (!strcasecmp(nodename, "regions")) {
fill_region(nodename, content, state); fill_regions(node);
return; return;
} }
if (state->in_window_switcher_field) { if (state->in_window_switcher_field) {