diff --git a/sway/commands/output.c b/sway/commands/output.c index 64afc225f..d9ea24a91 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -97,8 +97,6 @@ struct cmd_results *cmd_output(int argc, char **argv) { config->handler_context.leftovers.argc = 0; config->handler_context.leftovers.argv = NULL; - bool background = output->background; - store_output_config(output); // If reloading, the output configs will be applied after reading the @@ -106,9 +104,6 @@ struct cmd_results *cmd_output(int argc, char **argv) { // workspace name is not given to re-enabled outputs. if (!config->reloading && !config->validating) { apply_output_config_to_outputs(); - if (background) { - spawn_swaybg(); - } } return cmd_results_new(CMD_SUCCESS, NULL); diff --git a/sway/config.c b/sway/config.c index 71382b864..8cf02c4e8 100644 --- a/sway/config.c +++ b/sway/config.c @@ -528,7 +528,6 @@ bool load_main_config(const char *file, bool is_active, bool validating) { sway_switch_retrigger_bindings_for_all(); reset_outputs(); - spawn_swaybg(); config->reloading = false; if (config->swaynag_config_errors.client != NULL) { diff --git a/sway/config/output.c b/sway/config/output.c index 15ad7e50d..faf976e2c 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -472,6 +472,10 @@ bool apply_output_config(struct output_config *oc, struct sway_output *output) { // this output came online, and some config items (like map_to_output) are // dependent on an output being present. input_manager_configure_all_inputs(); + + // If we've changed the config of an output restart the backgrounds as well + spawn_swaybg(); + return true; } @@ -507,13 +511,13 @@ static void default_output_config(struct output_config *oc, struct output_config *find_output_config(struct sway_output *sway_output) { // Start with a default config for this output - struct output_config *result = new_output_config("merge"); + char *name = sway_output->wlr_output->name; + struct output_config *result = new_output_config(name); default_output_config(result, sway_output->wlr_output); // Apply all matches in order char id[128]; output_get_identifier(id, sizeof(id), sway_output); - char *name = sway_output->wlr_output->name; for (int i = 0; i < config->output_configs->length; ++i) { struct output_config *oc = config->output_configs->items[i]; if (!fnmatch(oc->name, name, 0) || !fnmatch(oc->name, id, 0)) { @@ -640,21 +644,8 @@ bool spawn_swaybg(void) { return true; } - size_t length = 2; - for (int i = 0; i < config->output_configs->length; i++) { - struct output_config *oc = config->output_configs->items[i]; - if (!oc->background) { - continue; - } - if (strcmp(oc->background_option, "solid_color") == 0) { - length += 4; - } else if (oc->background_fallback) { - length += 8; - } else { - length += 6; - } - } - + // At most the program, 8 arguments per output and the terminating NULL + size_t length = 2 + wl_list_length(&root->all_outputs) * 8; char **cmd = calloc(length, sizeof(char *)); if (!cmd) { sway_log(SWAY_ERROR, "Failed to allocate spawn_swaybg command"); @@ -663,8 +654,13 @@ bool spawn_swaybg(void) { size_t i = 0; cmd[i++] = config->swaybg_command; - for (int j = 0; j < config->output_configs->length; j++) { - struct output_config *oc = config->output_configs->items[j]; + + // Iterate all the outputs and write the command + list_t *output_configs = create_list(); + struct sway_output *sway_output, *tmp; + wl_list_for_each_safe(sway_output, tmp, &root->all_outputs, link) { + struct output_config *oc = find_output_config(sway_output); + list_add(output_configs, oc); if (!oc->background) { continue; } @@ -685,7 +681,7 @@ bool spawn_swaybg(void) { cmd[i++] = oc->background_fallback; } } - assert(i <= length); + assert(i < length); } for (size_t k = 0; k < i; k++) { @@ -693,6 +689,12 @@ bool spawn_swaybg(void) { } bool result = _spawn_swaybg(cmd); + free(cmd); + for (int k = 0; k < output_configs->length; k++) { + free_output_config(output_configs->items[k]); + } + list_free(output_configs); + return result; }