From 5664103902d688b263c4f44f620b70f409f4e23c Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Thu, 5 Sep 2024 22:25:44 +0200 Subject: [PATCH] config/output: Support multiple matches in find_output_config Simplify find_output_config and inline the search through the output configs instead of using list_seq_find with a comparator function. The new implementation will merge any amount of matched configs in order, which will be relied upon in a future commit. (cherry picked from a0c03499348a4a3d4d2e9a387bf366ccbcf68186) --- sway/config/output.c | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/sway/config/output.c b/sway/config/output.c index 230c2f5d..d7e7657a 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -26,13 +26,6 @@ #include #endif -int output_name_cmp(const void *item, const void *data) { - const struct output_config *output = item; - const char *name = data; - - return strcmp(output->name, name); -} - void output_get_identifier(char *identifier, size_t len, struct sway_output *output) { struct wlr_output *wlr_output = output->wlr_output; @@ -624,8 +617,6 @@ static void default_output_config(struct output_config *oc, // configuration that applies to the specified output. struct output_config *find_output_config(struct sway_output *sway_output) { const char *name = sway_output->wlr_output->name; - struct output_config *oc = NULL; - struct output_config *result = new_output_config(name); if (config->reloading) { default_output_config(result, sway_output->wlr_output); @@ -634,25 +625,21 @@ struct output_config *find_output_config(struct sway_output *sway_output) { char id[128]; output_get_identifier(id, sizeof(id), sway_output); - int i; - bool match = false; - if ((i = list_seq_find(config->output_configs, output_name_cmp, "*")) >= 0) { - match = true; - oc = config->output_configs->items[i]; - merge_output_config(result, oc); - } - if ((i = list_seq_find(config->output_configs, output_name_cmp, name)) >= 0) { - match = true; - oc = config->output_configs->items[i]; - merge_output_config(result, oc); - } - if ((i = list_seq_find(config->output_configs, output_name_cmp, id)) >= 0) { - match = true; - oc = config->output_configs->items[i]; - merge_output_config(result, oc); + // We take a new config and merge on top, in order, the wildcard config, + // output config by name, and output config by identifier to form the final + // config. If there are multiple matches, they are merged in order. + struct output_config *oc = NULL; + const char *names[] = {"*", name, id, NULL}; + for (const char **name = &names[0]; *name; name++) { + for (int idx = 0; idx < config->output_configs->length; idx++) { + oc = config->output_configs->items[idx]; + if (strcmp(oc->name, *name) == 0) { + merge_output_config(result, oc); + } + } } - if (!match && !config->reloading) { + if (oc == NULL && !config->reloading) { // No name, identifier, or wildcard config. Since we are not // reloading with defaults, the output config will be empty, so // just return NULL