From b00b05f792c4b5095d5aa08547e1502a24785e20 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Thu, 29 Sep 2022 23:18:39 +0000 Subject: [PATCH] sway/commands/output: Add command for unplugging non-physical outputs --- include/sway/commands.h | 1 + sway/commands/output.c | 1 + sway/commands/output/unplug.c | 54 +++++++++++++++++++++++++++++++++++ sway/meson.build | 1 + 4 files changed, 57 insertions(+) create mode 100644 sway/commands/output/unplug.c diff --git a/include/sway/commands.h b/include/sway/commands.h index 013a7b82..07941bb4 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -294,6 +294,7 @@ sway_cmd output_cmd_scale_filter; sway_cmd output_cmd_subpixel; sway_cmd output_cmd_toggle; sway_cmd output_cmd_transform; +sway_cmd output_cmd_unplug; sway_cmd seat_cmd_attach; sway_cmd seat_cmd_cursor; diff --git a/sway/commands/output.c b/sway/commands/output.c index c102344d..df32c673 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -27,6 +27,7 @@ static const struct cmd_handler output_handlers[] = { { "subpixel", output_cmd_subpixel }, { "toggle", output_cmd_toggle }, { "transform", output_cmd_transform }, + { "unplug", output_cmd_unplug }, }; struct cmd_results *cmd_output(int argc, char **argv) { diff --git a/sway/commands/output/unplug.c b/sway/commands/output/unplug.c new file mode 100644 index 00000000..dfef626f --- /dev/null +++ b/sway/commands/output/unplug.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#if WLR_HAS_X11_BACKEND +#include +#endif +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/output.h" + +static bool is_backend_allowed(struct wlr_backend *backend) { + if (wlr_backend_is_headless(backend)) { + return true; + } + if (wlr_backend_is_wl(backend)) { + return true; + } +#if WLR_HAS_X11_BACKEND + if (wlr_backend_is_x11(backend)) { + return true; + } +#endif + return false; +} + +/** + * This command is intended for developer use only. + */ +struct cmd_results *output_cmd_unplug(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "Missing output config"); + } + + const char *oc_name = config->handler_context.output_config->name; + if (strcmp(oc_name, "*") == 0) { + return cmd_results_new(CMD_INVALID, "Won't unplug all outputs"); + } + + struct sway_output *sway_output = all_output_by_name_or_id(oc_name); + if (!sway_output) { + return cmd_results_new(CMD_INVALID, + "Cannot unplug unknown output %s", oc_name); + } + + if (!is_backend_allowed(sway_output->wlr_output->backend)) { + return cmd_results_new(CMD_INVALID, + "Can only unplug outputs with headless, wayland or x11 backend"); + } + + wlr_output_destroy(sway_output->wlr_output); + + return cmd_results_new(CMD_SUCCESS, NULL); +} diff --git a/sway/meson.build b/sway/meson.build index ced7419c..c7b9d6e6 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -198,6 +198,7 @@ sway_sources = files( 'commands/output/subpixel.c', 'commands/output/toggle.c', 'commands/output/transform.c', + 'commands/output/unplug.c', 'tree/arrange.c', 'tree/container.c',