diff --git a/include/sway/commands.h b/include/sway/commands.h index 5210d3ba7..62450b479 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -125,6 +125,7 @@ sway_cmd cmd_create_output; sway_cmd cmd_default_border; sway_cmd cmd_default_floating_border; sway_cmd cmd_default_orientation; +sway_cmd cmd_destroy_output; sway_cmd cmd_exec; sway_cmd cmd_exec_always; sway_cmd cmd_exit; diff --git a/sway/commands.c b/sway/commands.c index c2c12ee65..09cfb4727 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -115,6 +115,7 @@ static const struct cmd_handler command_handlers[] = { { "allow_tearing", cmd_allow_tearing }, { "border", cmd_border }, { "create_output", cmd_create_output }, + { "destroy_output", cmd_destroy_output }, { "exit", cmd_exit }, { "floating", cmd_floating }, { "fullscreen", cmd_fullscreen }, diff --git a/sway/commands/create_output.c b/sway/commands/create_output.c index 79283fd11..422bbcf89 100644 --- a/sway/commands/create_output.c +++ b/sway/commands/create_output.c @@ -6,6 +6,7 @@ #include #endif #include "sway/commands.h" +#include "sway/output.h" #include "sway/server.h" #include "log.h" @@ -30,6 +31,20 @@ static void create_output(struct wlr_backend *backend, void *data) { #endif } +static bool output_is_destroyable(struct wlr_output *output) { + if (wlr_output_is_wl(output)) { + return true; + } else if (wlr_output_is_headless(output)) { + return true; + } +#if WLR_HAS_X11_BACKEND + else if (wlr_output_is_x11(output)) { + return true; + } +#endif + return false; +} + /** * This command is intended for developer use only. */ @@ -47,3 +62,32 @@ struct cmd_results *cmd_create_output(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL); } + +struct cmd_results *cmd_destroy_output(int argc, char **argv) { + sway_assert(wlr_backend_is_multi(server.backend), + "Expected a multi backend"); + + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "destroy_output", EXPECTED_EQUAL_TO, 1))) { + return error; + } + char *output_name = argv[0]; + + struct sway_output *sway_output; + wl_list_for_each(sway_output, &root->all_outputs, link) { + if (sway_output == root->fallback_output || + strcmp(sway_output->wlr_output->name, output_name) != 0) { + continue; + } + + if (!output_is_destroyable(sway_output->wlr_output)) { + return cmd_results_new(CMD_INVALID, + "Can only destroy outputs for Wayland, X11 or headless backends"); + } + + wlr_output_destroy(sway_output->wlr_output); + return cmd_results_new(CMD_SUCCESS, NULL); + } + + return cmd_results_new(CMD_INVALID, "No matching output found"); +}