From 5ea4a4d3eec897b915003ad7fc2b2e274d1a59c1 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sat, 2 Jun 2018 21:33:16 -0400 Subject: Refactor cmd_output to use config_subcommand --- sway/commands/output/background.c | 105 ++++++++++++++++++++++++++++++++++++++ sway/commands/output/disable.c | 13 +++++ sway/commands/output/dpms.c | 24 +++++++++ sway/commands/output/enable.c | 14 +++++ sway/commands/output/mode.c | 55 ++++++++++++++++++++ sway/commands/output/position.c | 46 +++++++++++++++++ sway/commands/output/scale.c | 23 +++++++++ sway/commands/output/transform.c | 39 ++++++++++++++ 8 files changed, 319 insertions(+) create mode 100644 sway/commands/output/background.c create mode 100644 sway/commands/output/disable.c create mode 100644 sway/commands/output/dpms.c create mode 100644 sway/commands/output/enable.c create mode 100644 sway/commands/output/mode.c create mode 100644 sway/commands/output/position.c create mode 100644 sway/commands/output/scale.c create mode 100644 sway/commands/output/transform.c (limited to 'sway/commands/output') diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c new file mode 100644 index 00000000..f039c9c9 --- /dev/null +++ b/sway/commands/output/background.c @@ -0,0 +1,105 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include "sway/commands.h" +#include "sway/config.h" +#include "log.h" +#include "stringop.h" + +static char *bg_options[] = { + "stretch", + "center", + "fill", + "fit", + "tile", +}; + +struct cmd_results *output_cmd_background(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "output", "Missing output config"); + } + if (!argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing background file or color specification."); + } + if (argc < 2) { + return cmd_results_new(CMD_INVALID, "output", + "Missing background scaling mode or `solid_color`."); + } + + struct output_config *output = config->handler_context.output_config; + + if (strcasecmp(argv[1], "solid_color") == 0) { + output->background = calloc(1, strlen(argv[0]) + 3); + snprintf(output->background, strlen(argv[0]) + 3, "\"%s\"", argv[0]); + output->background_option = strdup("solid_color"); + argc -= 2; argv += 2; + } else { + bool valid = false; + char *mode; + size_t j; + for (j = 0; j < (size_t)argc; ++j) { + mode = argv[j]; + size_t n = sizeof(bg_options) / sizeof(char *); + for (size_t k = 0; k < n; ++k) { + if (strcasecmp(mode, bg_options[k]) == 0) { + valid = true; + break; + } + } + if (valid) { + break; + } + } + if (!valid) { + return cmd_results_new(CMD_INVALID, "output", + "Missing background scaling mode."); + } + + wordexp_t p; + char *src = join_args(argv, j); + if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid syntax (%s).", src); + } + free(src); + src = p.we_wordv[0]; + if (config->reading && *src != '/') { + char *conf = strdup(config->current_config); + if (conf) { + char *conf_path = dirname(conf); + src = malloc(strlen(conf_path) + strlen(src) + 2); + if (src) { + sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); + } else { + wlr_log(L_ERROR, + "Unable to allocate background source"); + } + free(conf); + } else { + wlr_log(L_ERROR, "Unable to allocate background source"); + } + } + if (!src || access(src, F_OK) == -1) { + wordfree(&p); + return cmd_results_new(CMD_INVALID, "output", + "Background file unreadable (%s).", src); + } + + output->background = strdup(src); + output->background_option = strdup(mode); + if (src != p.we_wordv[0]) { + free(src); + } + wordfree(&p); + + argc -= j + 1; argv += j + 1; + } + + config->handler_context.leftovers.argc = argc; + config->handler_context.leftovers.argv = argv; + return NULL; +} + diff --git a/sway/commands/output/disable.c b/sway/commands/output/disable.c new file mode 100644 index 00000000..65517c49 --- /dev/null +++ b/sway/commands/output/disable.c @@ -0,0 +1,13 @@ +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *output_cmd_disable(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "output", "Missing output config"); + } + config->handler_context.output_config->enabled = 0; + + config->handler_context.leftovers.argc = argc; + config->handler_context.leftovers.argv = argv; + return NULL; +} diff --git a/sway/commands/output/dpms.c b/sway/commands/output/dpms.c new file mode 100644 index 00000000..0959ea6b --- /dev/null +++ b/sway/commands/output/dpms.c @@ -0,0 +1,24 @@ +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *output_cmd_dpms(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "output", "Missing output config"); + } + if (!argc) { + return cmd_results_new(CMD_INVALID, "output", "Missing dpms argument."); + } + + if (strcmp(*argv, "on") == 0) { + config->handler_context.output_config->dpms_state = DPMS_ON; + } else if (strcmp(*argv, "off") == 0) { + config->handler_context.output_config->dpms_state = DPMS_OFF; + } else { + return cmd_results_new(CMD_INVALID, "output", + "Invalid dpms state, valid states are on/off."); + } + + config->handler_context.leftovers.argc = argc - 1; + config->handler_context.leftovers.argv = argv + 1; + return NULL; +} diff --git a/sway/commands/output/enable.c b/sway/commands/output/enable.c new file mode 100644 index 00000000..8e3314f8 --- /dev/null +++ b/sway/commands/output/enable.c @@ -0,0 +1,14 @@ +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *output_cmd_enable(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "output", "Missing output config"); + } + config->handler_context.output_config->enabled = 1; + + config->handler_context.leftovers.argc = argc; + config->handler_context.leftovers.argv = argv; + return NULL; +} + diff --git a/sway/commands/output/mode.c b/sway/commands/output/mode.c new file mode 100644 index 00000000..daec6d44 --- /dev/null +++ b/sway/commands/output/mode.c @@ -0,0 +1,55 @@ +#include +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *output_cmd_mode(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "output", "Missing output config"); + } + if (!argc) { + return cmd_results_new(CMD_INVALID, "output", "Missing mode argument."); + } + + struct output_config *output = config->handler_context.output_config; + + char *end; + output->width = strtol(*argv, &end, 10); + if (*end) { + // Format is 1234x4321 + if (*end != 'x') { + return cmd_results_new(CMD_INVALID, "output", + "Invalid mode width."); + } + ++end; + output->height = strtol(end, &end, 10); + if (*end) { + if (*end != '@') { + return cmd_results_new(CMD_INVALID, "output", + "Invalid mode height."); + } + ++end; + output->refresh_rate = strtof(end, &end); + if (strcasecmp("Hz", end) != 0) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid mode refresh rate."); + } + } + } else { + // Format is 1234 4321 + if (!argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing mode argument (height)."); + } + argc--; argv++; + output->height = strtol(*argv, &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid mode height."); + } + } + + config->handler_context.leftovers.argc = argc - 1; + config->handler_context.leftovers.argv = argv + 1; + return NULL; +} + diff --git a/sway/commands/output/position.c b/sway/commands/output/position.c new file mode 100644 index 00000000..c2aeb281 --- /dev/null +++ b/sway/commands/output/position.c @@ -0,0 +1,46 @@ +#include +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *output_cmd_position(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "output", "Missing output config"); + } + if (!argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing position argument."); + } + + char *end; + config->handler_context.output_config->x = strtol(*argv, &end, 10); + if (*end) { + // Format is 1234,4321 + if (*end != ',') { + return cmd_results_new(CMD_INVALID, "output", + "Invalid position x."); + } + ++end; + config->handler_context.output_config->y = strtol(end, &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid position y."); + } + } else { + // Format is 1234 4321 (legacy) + if (!argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing position argument (y)."); + } + argc--; argv++; + config->handler_context.output_config->y = strtol(*argv, &end, 10); + if (*end) { + return cmd_results_new(CMD_INVALID, "output", + "Invalid position y."); + } + } + + config->handler_context.leftovers.argc = argc - 1; + config->handler_context.leftovers.argv = argv + 1; + return NULL; +} + diff --git a/sway/commands/output/scale.c b/sway/commands/output/scale.c new file mode 100644 index 00000000..0b4cc131 --- /dev/null +++ b/sway/commands/output/scale.c @@ -0,0 +1,23 @@ +#include +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *output_cmd_scale(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "output", "Missing output config"); + } + if (!argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing scale argument."); + } + + char *end; + config->handler_context.output_config->scale = strtof(*argv, &end); + if (*end) { + return cmd_results_new(CMD_INVALID, "output", "Invalid scale."); + } + + config->handler_context.leftovers.argc = argc - 1; + config->handler_context.leftovers.argv = argv + 1; + return NULL; +} diff --git a/sway/commands/output/transform.c b/sway/commands/output/transform.c new file mode 100644 index 00000000..f9a94d64 --- /dev/null +++ b/sway/commands/output/transform.c @@ -0,0 +1,39 @@ +#include +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *output_cmd_transform(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "output", "Missing output config"); + } + if (!argc) { + return cmd_results_new(CMD_INVALID, "output", + "Missing transform argument."); + } + + struct output_config *output = config->handler_context.output_config; + if (strcmp(*argv, "normal") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_NORMAL; + } else if (strcmp(*argv, "90") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_90; + } else if (strcmp(*argv, "180") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_180; + } else if (strcmp(*argv, "270") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_270; + } else if (strcmp(*argv, "flipped") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED; + } else if (strcmp(*argv, "flipped-90") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; + } else if (strcmp(*argv, "flipped-180") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; + } else if (strcmp(*argv, "flipped-270") == 0) { + output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; + } else { + return cmd_results_new(CMD_INVALID, "output", + "Invalid output transform."); + } + + config->handler_context.leftovers.argc = argc - 1; + config->handler_context.leftovers.argv = argv + 1; + return NULL; +} -- cgit v1.2.3-54-g00ecf