From e239a9aee998ac60e48ce25cf40e0a303bc78857 Mon Sep 17 00:00:00 2001 From: Celio Grand Date: Tue, 2 Jan 2024 13:49:33 +0800 Subject: [PATCH] Toggle all outputs --- sway/commands/output/power.c | 10 +++++++-- sway/commands/output/toggle.c | 42 ++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/sway/commands/output/power.c b/sway/commands/output/power.c index e6ae2852..3ba685f3 100644 --- a/sway/commands/output/power.c +++ b/sway/commands/output/power.c @@ -16,8 +16,14 @@ struct cmd_results *output_cmd_power(int argc, char **argv) { if (strcasecmp(argv[0], "toggle") == 0) { const char *oc_name = config->handler_context.output_config->name; if (strcmp(oc_name, "*") == 0) { - return cmd_results_new(CMD_INVALID, - "Cannot apply toggle to all outputs"); + for (int i = 0; i < config->output_configs->length; i++) { + struct output_config *oc = config->output_configs->items[i]; + oc->power = !oc->power; + } + + config->handler_context.leftovers.argc = argc - 1; + config->handler_context.leftovers.argv = argv + 1; + return NULL; } struct sway_output *sway_output = all_output_by_name_or_id(oc_name); diff --git a/sway/commands/output/toggle.c b/sway/commands/output/toggle.c index 6342d526..4987cd74 100644 --- a/sway/commands/output/toggle.c +++ b/sway/commands/output/toggle.c @@ -7,29 +7,31 @@ struct cmd_results *output_cmd_toggle(int argc, char **argv) { return cmd_results_new(CMD_FAILURE, "Missing output config"); } - struct output_config *oc = config->handler_context.output_config; - - if (strcmp(oc->name, "*") == 0) { - return cmd_results_new(CMD_INVALID, - "Cannot apply toggle to all outputs."); - } - - struct sway_output *sway_output = all_output_by_name_or_id(oc->name); - - if (sway_output == NULL) { - return cmd_results_new(CMD_FAILURE, - "Cannot apply toggle to unknown output %s", oc->name); - } - - oc = find_output_config(sway_output); - - if (!oc || oc->enabled != 0) { - config->handler_context.output_config->enabled = 0; + if (strcmp(config->handler_context.output_config->name, "*") == 0) { + for (int i = 0; i < config->output_configs->length; i++) { + struct output_config *oc = config->output_configs->items[i]; + oc->enabled = !oc->enabled; + } } else { - config->handler_context.output_config->enabled = 1; + struct output_config *oc = config->handler_context.output_config; + struct sway_output *sway_output = all_output_by_name_or_id(oc->name); + + if (sway_output == NULL) { + return cmd_results_new(CMD_FAILURE, + "Cannot apply toggle to unknown output %s", oc->name); + } + + oc = find_output_config(sway_output); + + if (!oc || oc->enabled != 0) { + config->handler_context.output_config->enabled = 0; + } else { + config->handler_context.output_config->enabled = 1; + } + + free(oc); } - free(oc); config->handler_context.leftovers.argc = argc; config->handler_context.leftovers.argv = argv; return NULL;