Toggle all outputs

This commit is contained in:
Celio Grand 2024-01-02 13:49:33 +08:00
parent 0aceff7469
commit e239a9aee9
2 changed files with 30 additions and 22 deletions

View file

@ -16,8 +16,14 @@ struct cmd_results *output_cmd_power(int argc, char **argv) {
if (strcasecmp(argv[0], "toggle") == 0) { if (strcasecmp(argv[0], "toggle") == 0) {
const char *oc_name = config->handler_context.output_config->name; const char *oc_name = config->handler_context.output_config->name;
if (strcmp(oc_name, "*") == 0) { if (strcmp(oc_name, "*") == 0) {
return cmd_results_new(CMD_INVALID, for (int i = 0; i < config->output_configs->length; i++) {
"Cannot apply toggle to all outputs"); 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); struct sway_output *sway_output = all_output_by_name_or_id(oc_name);

View file

@ -7,29 +7,31 @@ struct cmd_results *output_cmd_toggle(int argc, char **argv) {
return cmd_results_new(CMD_FAILURE, "Missing output config"); return cmd_results_new(CMD_FAILURE, "Missing output config");
} }
struct output_config *oc = config->handler_context.output_config; if (strcmp(config->handler_context.output_config->name, "*") == 0) {
for (int i = 0; i < config->output_configs->length; i++) {
if (strcmp(oc->name, "*") == 0) { struct output_config *oc = config->output_configs->items[i];
return cmd_results_new(CMD_INVALID, oc->enabled = !oc->enabled;
"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;
} else { } 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.argc = argc;
config->handler_context.leftovers.argv = argv; config->handler_context.leftovers.argv = argv;
return NULL; return NULL;