From 88f372a22adab3c7a7b088d9d400066ed9c7b1af Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sun, 29 Nov 2015 10:28:52 +0200 Subject: [PATCH 1/4] cmd_output: Cleanup cmd_output argument handling --- sway/commands.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 42845f655..1106f0952 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -716,20 +716,23 @@ static struct cmd_results *cmd_output(int argc, char **argv) { if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { return error; } + const char *name = argv[0]; + struct output_config *output = calloc(1, sizeof(struct output_config)); output->x = output->y = output->width = output->height = -1; - output->name = strdup(argv[0]); + output->name = strdup(name); output->enabled = true; // TODO: atoi doesn't handle invalid numbers - if (strcasecmp(argv[1], "disable") == 0) { - output->enabled = false; - } // TODO: Check missing params after each sub-command int i; for (i = 1; i < argc; ++i) { - if (strcasecmp(argv[i], "resolution") == 0 || strcasecmp(argv[i], "res") == 0) { + const char *command = argv[i]; + + if (strcasecmp(command, "disable") == 0) { + output->enabled = false; + } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { char *res = argv[++i]; char *x = strchr(res, 'x'); int width = -1, height = -1; @@ -747,7 +750,7 @@ static struct cmd_results *cmd_output(int argc, char **argv) { } output->width = width; output->height = height; - } else if (strcasecmp(argv[i], "position") == 0 || strcasecmp(argv[i], "pos") == 0) { + } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { char *res = argv[++i]; char *c = strchr(res, ','); int x = -1, y = -1; @@ -765,7 +768,7 @@ static struct cmd_results *cmd_output(int argc, char **argv) { } output->x = x; output->y = y; - } else if (strcasecmp(argv[i], "bg") == 0 || strcasecmp(argv[i], "background") == 0) { + } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { wordexp_t p; if (++i >= argc) { return cmd_results_new(CMD_INVALID, "output", "Missing background file."); From 7d82cd9c0a418385fbde72f4503459ab7afa1bde Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sun, 29 Nov 2015 14:51:42 +0200 Subject: [PATCH 2/4] cmd_output: Use list_seq_find() to find matching config --- include/config.h | 1 + sway/commands.c | 12 +++++------- sway/config.c | 8 ++++++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/config.h b/include/config.h index 6b48063a9..21fbb8f9c 100644 --- a/include/config.h +++ b/include/config.h @@ -98,6 +98,7 @@ bool read_config(FILE *file, bool is_active); * Does variable replacement for a string based on the config's currently loaded variables. */ char *do_var_replacement(char *str); +int output_name_cmp(const void *item, const void *data);; /** Sets up a WLC output handle based on a given output_config. */ void apply_output_config(struct output_config *oc, swayc_t *output); diff --git a/sway/commands.c b/sway/commands.c index 1106f0952..4eaa210fa 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -804,14 +804,12 @@ static struct cmd_results *cmd_output(int argc, char **argv) { } } - for (i = 0; i < config->output_configs->length; ++i) { + i = list_seq_find(config->output_configs, output_name_cmp, name); + if (i >= 0) { + // replace existing config struct output_config *oc = config->output_configs->items[i]; - if (strcmp(oc->name, output->name) == 0) { - // replace existing config - list_del(config->output_configs, i); - free_output_config(oc); - break; - } + list_del(config->output_configs, i); + free_output_config(oc); } list_add(config->output_configs, output); diff --git a/sway/config.c b/sway/config.c index e9785aba6..bb9142c03 100644 --- a/sway/config.c +++ b/sway/config.c @@ -261,6 +261,14 @@ bool read_config(FILE *file, bool is_active) { return success; } +int output_name_cmp(const void *item, const void *data) +{ + const struct output_config *output = item; + const char *name = data; + + return strcmp(output->name, name); +} + void apply_output_config(struct output_config *oc, swayc_t *output) { if (oc && oc->width > 0 && oc->height > 0) { output->width = oc->width; From ffdfaaa985467ea673c9c388bf1a18f7ca83af41 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sun, 29 Nov 2015 15:24:11 +0200 Subject: [PATCH 3/4] apply_output_config: use list_seq_find() to find config --- sway/config.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/sway/config.c b/sway/config.c index bb9142c03..dd466e5b0 100644 --- a/sway/config.c +++ b/sway/config.c @@ -299,12 +299,10 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { if (!oc || !oc->background) { // Look for a * config for background - int i; - for (i = 0; i < config->output_configs->length; ++i) { + int i = list_seq_find(config->output_configs, output_name_cmp, "*"); + if (i >= 0) { oc = config->output_configs->items[i]; - if (strcasecmp("*", oc->name) == 0) { - break; - } + } else { oc = NULL; } } From 7059eccaa12e7f74d8dfd4744a05ec7d438c9f08 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sun, 29 Nov 2015 15:23:27 +0200 Subject: [PATCH 4/4] cmd_output: Log enable/disable state --- sway/commands.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 4eaa210fa..e00cc94de 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -813,9 +813,10 @@ static struct cmd_results *cmd_output(int argc, char **argv) { } list_add(config->output_configs, output); - sway_log(L_DEBUG, "Config stored for output %s (%d x %d @ %d, %d) (bg %s %s)", - output->name, output->width, output->height, output->x, output->y, - output->background, output->background_option); + sway_log(L_DEBUG, "Config stored for output %s (%s) (%d x %d @ %d, %d) (bg %s %s)", + output->name, output->enabled ? "enable" : "disable", output->width, + output->height, output->x, output->y, output->background, + output->background_option); if (output->name) { // Try to find the output container and apply configuration now. If