commands/destroy_output: Add new command

destroy_output does the opposite of create_output, destroying any output
by the specified name.
This commit is contained in:
Kenny Levinsen 2024-10-11 00:21:53 +02:00
parent dd063a0ef7
commit 3a00566df9
3 changed files with 46 additions and 0 deletions

View file

@ -125,6 +125,7 @@ sway_cmd cmd_create_output;
sway_cmd cmd_default_border; sway_cmd cmd_default_border;
sway_cmd cmd_default_floating_border; sway_cmd cmd_default_floating_border;
sway_cmd cmd_default_orientation; sway_cmd cmd_default_orientation;
sway_cmd cmd_destroy_output;
sway_cmd cmd_exec; sway_cmd cmd_exec;
sway_cmd cmd_exec_always; sway_cmd cmd_exec_always;
sway_cmd cmd_exit; sway_cmd cmd_exit;

View file

@ -115,6 +115,7 @@ static const struct cmd_handler command_handlers[] = {
{ "allow_tearing", cmd_allow_tearing }, { "allow_tearing", cmd_allow_tearing },
{ "border", cmd_border }, { "border", cmd_border },
{ "create_output", cmd_create_output }, { "create_output", cmd_create_output },
{ "destroy_output", cmd_destroy_output },
{ "exit", cmd_exit }, { "exit", cmd_exit },
{ "floating", cmd_floating }, { "floating", cmd_floating },
{ "fullscreen", cmd_fullscreen }, { "fullscreen", cmd_fullscreen },

View file

@ -6,6 +6,7 @@
#include <wlr/backend/x11.h> #include <wlr/backend/x11.h>
#endif #endif
#include "sway/commands.h" #include "sway/commands.h"
#include "sway/output.h"
#include "sway/server.h" #include "sway/server.h"
#include "log.h" #include "log.h"
@ -30,6 +31,20 @@ static void create_output(struct wlr_backend *backend, void *data) {
#endif #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. * 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); 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");
}