diff options
Diffstat (limited to 'sway/commands/output')
-rw-r--r-- | sway/commands/output/background.c | 12 | ||||
-rw-r--r-- | sway/commands/output/dpms.c | 45 | ||||
-rw-r--r-- | sway/commands/output/power.c | 43 | ||||
-rw-r--r-- | sway/commands/output/unplug.c | 54 |
4 files changed, 107 insertions, 47 deletions
diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c index 68ee9fe1..67f212ff 100644 --- a/sway/commands/output/background.c +++ b/sway/commands/output/background.c @@ -102,19 +102,19 @@ struct cmd_results *output_cmd_background(int argc, char **argv) { } char *conf_path = dirname(conf); - char *rel_path = src; - src = malloc(strlen(conf_path) + strlen(src) + 2); - if (!src) { - free(rel_path); + char *real_src = malloc(strlen(conf_path) + strlen(src) + 2); + if (!real_src) { + free(src); free(conf); sway_log(SWAY_ERROR, "Unable to allocate memory"); return cmd_results_new(CMD_FAILURE, "Unable to allocate resources"); } - sprintf(src, "%s/%s", conf_path, rel_path); - free(rel_path); + snprintf(real_src, strlen(conf_path) + strlen(src) + 2, "%s/%s", conf_path, src); + free(src); free(conf); + src = real_src; } bool can_access = access(src, F_OK) != -1; diff --git a/sway/commands/output/dpms.c b/sway/commands/output/dpms.c index 638c0ade..c7adbd58 100644 --- a/sway/commands/output/dpms.c +++ b/sway/commands/output/dpms.c @@ -1,45 +1,8 @@ +#include "log.h" #include "sway/commands.h" -#include "sway/config.h" -#include "sway/output.h" -#include "util.h" -#include <strings.h> struct cmd_results *output_cmd_dpms(int argc, char **argv) { - if (!config->handler_context.output_config) { - return cmd_results_new(CMD_FAILURE, "Missing output config"); - } - if (!argc) { - return cmd_results_new(CMD_INVALID, "Missing dpms argument."); - } - - enum config_dpms current_dpms = DPMS_ON; - - 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."); - } - - struct sway_output *sway_output = all_output_by_name_or_id(oc_name); - if (!sway_output || !sway_output->wlr_output) { - return cmd_results_new(CMD_FAILURE, - "Cannot apply toggle to unknown output %s", oc_name); - } - - if (sway_output->enabled && !sway_output->wlr_output->enabled) { - current_dpms = DPMS_OFF; - } - } - - if (parse_boolean(argv[0], current_dpms == DPMS_ON)) { - config->handler_context.output_config->dpms_state = DPMS_ON; - } else { - config->handler_context.output_config->dpms_state = DPMS_OFF; - } - - config->handler_context.leftovers.argc = argc - 1; - config->handler_context.leftovers.argv = argv + 1; - return NULL; + sway_log(SWAY_INFO, "The \"output dpms\" command is deprecated, " + "use \"output power\" instead"); + return output_cmd_power(argc, argv); } diff --git a/sway/commands/output/power.c b/sway/commands/output/power.c new file mode 100644 index 00000000..e6ae2852 --- /dev/null +++ b/sway/commands/output/power.c @@ -0,0 +1,43 @@ +#include <strings.h> +#include "sway/commands.h" +#include "sway/config.h" +#include "sway/output.h" +#include "util.h" + +struct cmd_results *output_cmd_power(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "Missing output config"); + } + if (argc == 0) { + return cmd_results_new(CMD_INVALID, "Missing power argument"); + } + + bool current = true; + 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"); + } + + struct sway_output *sway_output = all_output_by_name_or_id(oc_name); + if (!sway_output || !sway_output->wlr_output) { + return cmd_results_new(CMD_FAILURE, + "Cannot apply toggle to unknown output %s", oc_name); + } + + if (sway_output->enabled && !sway_output->wlr_output->enabled) { + current = false; + } + } + + if (parse_boolean(argv[0], current)) { + config->handler_context.output_config->power = 1; + } else { + config->handler_context.output_config->power = 0; + } + + config->handler_context.leftovers.argc = argc - 1; + config->handler_context.leftovers.argv = argv + 1; + return NULL; +} 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 <strings.h> +#include <wlr/config.h> +#include <wlr/backend/headless.h> +#include <wlr/backend/wayland.h> +#if WLR_HAS_X11_BACKEND +#include <wlr/backend/x11.h> +#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); +} |