config/output: Accept a list of output_configs to use

Instead of using a single finalized output config per output, accept a
regular list of output configs like the one ultimately stored for
configuration purposes. This allows the output management code to test
an augmented configuration while still using the same output config
logic, without having to mutate the stored configuration.

This in turn allows us to make a few APIs private. A bug note about an
existing issue with derade to off is added as well.

(cherry picked from 29b3f00e6f)
This commit is contained in:
Kenny Levinsen 2024-09-09 15:28:22 +02:00 committed by Simon Ser
parent 3b27392a47
commit a9c295fd67
3 changed files with 67 additions and 75 deletions

View file

@ -297,14 +297,6 @@ struct output_config {
char *background_fallback; char *background_fallback;
}; };
/**
* An output config pre-matched to an output
*/
struct matched_output_config {
struct sway_output *output;
struct output_config *config;
};
/** /**
* Stores size of gaps for each side * Stores size of gaps for each side
*/ */
@ -694,14 +686,11 @@ const char *sway_output_scale_filter_to_string(enum scale_filter_mode scale_filt
struct output_config *new_output_config(const char *name); struct output_config *new_output_config(const char *name);
bool apply_output_configs(struct matched_output_config *configs, bool apply_output_configs(struct output_config **ocs, size_t ocs_len,
size_t configs_len, bool test_only, bool degrade_to_off); bool test_only, bool degrade_to_off);
void apply_all_output_configs(void); void apply_all_output_configs(void);
void sort_output_configs_by_priority(struct matched_output_config *configs,
size_t configs_len);
/** /**
* store_output_config stores a new output config. An output may be matched by * store_output_config stores a new output config. An output may be matched by
* three different config types, in order of precedence: Identifier, name and * three different config types, in order of precedence: Identifier, name and

View file

@ -593,9 +593,11 @@ static bool finalize_output_config(struct output_config *oc, struct sway_output
return true; return true;
} }
// find_output_config returns a merged output_config containing all stored // find_output_config_from_list returns a merged output_config containing all
// configuration that applies to the specified output. // stored configuration that applies to the specified output.
struct output_config *find_output_config(struct sway_output *sway_output) { static struct output_config *find_output_config_from_list(
struct output_config **configs, size_t configs_len,
struct sway_output *sway_output) {
const char *name = sway_output->wlr_output->name; const char *name = sway_output->wlr_output->name;
struct output_config *result = new_output_config(name); struct output_config *result = new_output_config(name);
if (result == NULL) { if (result == NULL) {
@ -620,8 +622,8 @@ struct output_config *find_output_config(struct sway_output *sway_output) {
struct output_config *oc = NULL; struct output_config *oc = NULL;
const char *names[] = {"*", name, id, NULL}; const char *names[] = {"*", name, id, NULL};
for (const char **name = &names[0]; *name; name++) { for (const char **name = &names[0]; *name; name++) {
for (int idx = 0; idx < config->output_configs->length; idx++) { for (size_t idx = 0; idx < configs_len; idx++) {
oc = config->output_configs->items[idx]; oc = configs[idx];
if (strcmp(oc->name, *name) == 0) { if (strcmp(oc->name, *name) == 0) {
merge_output_config(result, oc); merge_output_config(result, oc);
} }
@ -631,6 +633,12 @@ struct output_config *find_output_config(struct sway_output *sway_output) {
return result; return result;
} }
struct output_config *find_output_config(struct sway_output *sway_output) {
return find_output_config_from_list(
(struct output_config **)config->output_configs->items,
config->output_configs->length, sway_output);
}
static bool config_has_manual_mode(struct output_config *oc) { static bool config_has_manual_mode(struct output_config *oc) {
if (!oc) { if (!oc) {
return false; return false;
@ -643,6 +651,14 @@ static bool config_has_manual_mode(struct output_config *oc) {
return false; return false;
} }
/**
* An output config pre-matched to an output
*/
struct matched_output_config {
struct sway_output *output;
struct output_config *config;
};
struct search_context { struct search_context {
struct wlr_output_swapchain_manager *swapchain_mgr; struct wlr_output_swapchain_manager *swapchain_mgr;
struct wlr_backend_output_state *states; struct wlr_backend_output_state *states;
@ -869,12 +885,12 @@ static int compare_matched_output_config_priority(const void *a, const void *b)
return 0; return 0;
} }
void sort_output_configs_by_priority(struct matched_output_config *configs, static void sort_output_configs_by_priority(
size_t configs_len) { struct matched_output_config *configs, size_t configs_len) {
qsort(configs, configs_len, sizeof(*configs), compare_matched_output_config_priority); qsort(configs, configs_len, sizeof(*configs), compare_matched_output_config_priority);
} }
bool apply_output_configs(struct matched_output_config *configs, static bool apply_resolved_output_configs(struct matched_output_config *configs,
size_t configs_len, bool test_only, bool degrade_to_off) { size_t configs_len, bool test_only, bool degrade_to_off) {
struct wlr_backend_output_state *states = calloc(configs_len, sizeof(*states)); struct wlr_backend_output_state *states = calloc(configs_len, sizeof(*states));
if (!states) { if (!states) {
@ -982,11 +998,12 @@ out:
return ok; return ok;
} }
void apply_all_output_configs(void) { bool apply_output_configs(struct output_config **ocs, size_t ocs_len,
bool test_only, bool degrade_to_off) {
size_t configs_len = wl_list_length(&root->all_outputs); size_t configs_len = wl_list_length(&root->all_outputs);
struct matched_output_config *configs = calloc(configs_len, sizeof(*configs)); struct matched_output_config *configs = calloc(configs_len, sizeof(*configs));
if (!configs) { if (!configs) {
return; return false;
} }
int config_idx = 0; int config_idx = 0;
@ -999,16 +1016,22 @@ void apply_all_output_configs(void) {
struct matched_output_config *config = &configs[config_idx++]; struct matched_output_config *config = &configs[config_idx++];
config->output = sway_output; config->output = sway_output;
config->config = find_output_config(sway_output); config->config = find_output_config_from_list(ocs, ocs_len, sway_output);
} }
sort_output_configs_by_priority(configs, configs_len); sort_output_configs_by_priority(configs, configs_len);
apply_output_configs(configs, configs_len, false, true); bool ok = apply_resolved_output_configs(configs, configs_len, test_only, degrade_to_off);
for (size_t idx = 0; idx < configs_len; idx++) { for (size_t idx = 0; idx < configs_len; idx++) {
struct matched_output_config *cfg = &configs[idx]; struct matched_output_config *cfg = &configs[idx];
free_output_config(cfg->config); free_output_config(cfg->config);
} }
free(configs); free(configs);
return ok;
}
void apply_all_output_configs(void) {
apply_output_configs((struct output_config **)config->output_configs->items,
config->output_configs->length, false, true);
} }
void free_output_config(struct output_config *oc) { void free_output_config(struct output_config *oc) {

View file

@ -594,9 +594,8 @@ void handle_gamma_control_set_gamma(struct wl_listener *listener, void *data) {
} }
static struct output_config *output_config_for_config_head( static struct output_config *output_config_for_config_head(
struct wlr_output_configuration_head_v1 *config_head, struct wlr_output_configuration_head_v1 *config_head) {
struct sway_output *output) { struct output_config *oc = new_output_config(config_head->state.output->name);
struct output_config *oc = new_output_config(output->wlr_output->name);
oc->enabled = config_head->state.enabled; oc->enabled = config_head->state.enabled;
if (!oc->enabled) { if (!oc->enabled) {
return oc; return oc;
@ -622,67 +621,48 @@ static struct output_config *output_config_for_config_head(
} }
static void output_manager_apply(struct sway_server *server, static void output_manager_apply(struct sway_server *server,
struct wlr_output_configuration_v1 *config, bool test_only) { struct wlr_output_configuration_v1 *cfg, bool test_only) {
size_t configs_len = wl_list_length(&root->all_outputs); bool ok = false;
struct matched_output_config *configs = calloc(configs_len, sizeof(*configs)); size_t configs_len = config->output_configs->length + wl_list_length(&cfg->heads);
struct output_config **configs = calloc(configs_len, sizeof(*configs));
if (!configs) { if (!configs) {
return; goto done;
}
size_t start_new_configs = config->output_configs->length;
for (size_t idx = 0; idx < start_new_configs; idx++) {
configs[idx] = config->output_configs->items[idx];
} }
int config_idx = 0; size_t config_idx = start_new_configs;
struct sway_output *sway_output; struct wlr_output_configuration_head_v1 *config_head;
wl_list_for_each(sway_output, &root->all_outputs, link) { wl_list_for_each(config_head, &cfg->heads, link) {
if (sway_output == root->fallback_output) { // Generate the configuration and store it as a temporary
configs_len--; // config. We keep a record of it so we can remove it later.
continue; struct output_config *oc = output_config_for_config_head(config_head);
} configs[config_idx++] = oc;
struct matched_output_config *cfg = &configs[config_idx++];
cfg->output = sway_output;
struct wlr_output_configuration_head_v1 *config_head;
wl_list_for_each(config_head, &config->heads, link) {
if (config_head->state.output == sway_output->wlr_output) {
cfg->config = output_config_for_config_head(config_head, sway_output);
break;
}
}
if (!cfg->config) {
cfg->config = find_output_config(sway_output);
}
} }
sort_output_configs_by_priority(configs, configs_len); // Try to commit without degrade to off enabled. Note that this will fail
bool ok = apply_output_configs(configs, configs_len, test_only, false); // if any output configured for enablement fails to be enabled, even if it
for (size_t idx = 0; idx < configs_len; idx++) { // was not part of the config heads we were asked to configure.
struct matched_output_config *cfg = &configs[idx]; ok = apply_output_configs(configs, configs_len, test_only, false);
for (size_t idx = start_new_configs; idx < configs_len; idx++) {
// Only store new configs for successful non-test commits. Old configs, struct output_config *cfg = configs[idx];
// test-only and failed commits just get freed.
bool store_config = false;
if (!test_only && ok) { if (!test_only && ok) {
struct wlr_output_configuration_head_v1 *config_head; store_output_config(cfg);
wl_list_for_each(config_head, &config->heads, link) {
if (config_head->state.output == cfg->output->wlr_output) {
store_config = true;
break;
}
}
}
if (store_config) {
store_output_config(cfg->config);
} else { } else {
free_output_config(cfg->config); free_output_config(cfg);
} }
} }
free(configs); free(configs);
done:
if (ok) { if (ok) {
wlr_output_configuration_v1_send_succeeded(config); wlr_output_configuration_v1_send_succeeded(cfg);
} else { } else {
wlr_output_configuration_v1_send_failed(config); wlr_output_configuration_v1_send_failed(cfg);
} }
wlr_output_configuration_v1_destroy(config); wlr_output_configuration_v1_destroy(cfg);
} }
void handle_output_manager_apply(struct wl_listener *listener, void *data) { void handle_output_manager_apply(struct wl_listener *listener, void *data) {