diff options
author | 2016-07-04 13:53:49 -0500 | |
---|---|---|
committer | 2016-07-04 13:53:49 -0500 | |
commit | e07b8c8d7cfaaf6667a766c9d42d59fc14e98872 (patch) | |
tree | 93c374e26425460bfa7ad1bc1528a1b9b5725a8e /sway/commands.c | |
parent | fix typo in ipc-json.c (diff) | |
parent | Merge pull request #734 from jplatte/patch-1 (diff) | |
download | sway-e07b8c8d7cfaaf6667a766c9d42d59fc14e98872.tar.gz sway-e07b8c8d7cfaaf6667a766c9d42d59fc14e98872.tar.zst sway-e07b8c8d7cfaaf6667a766c9d42d59fc14e98872.zip |
Merge branch 'master' into get-tree-command
Diffstat (limited to 'sway/commands.c')
-rw-r--r-- | sway/commands.c | 115 |
1 files changed, 96 insertions, 19 deletions
diff --git a/sway/commands.c b/sway/commands.c index 55f46f79..2248e1c7 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -83,6 +83,7 @@ static sway_cmd cmd_orientation; | |||
83 | static sway_cmd cmd_output; | 83 | static sway_cmd cmd_output; |
84 | static sway_cmd cmd_reload; | 84 | static sway_cmd cmd_reload; |
85 | static sway_cmd cmd_resize; | 85 | static sway_cmd cmd_resize; |
86 | static sway_cmd cmd_resize_set; | ||
86 | static sway_cmd cmd_scratchpad; | 87 | static sway_cmd cmd_scratchpad; |
87 | static sway_cmd cmd_set; | 88 | static sway_cmd cmd_set; |
88 | static sway_cmd cmd_smart_gaps; | 89 | static sway_cmd cmd_smart_gaps; |
@@ -2000,36 +2001,112 @@ static struct cmd_results *cmd_resize(int argc, char **argv) { | |||
2000 | struct cmd_results *error = NULL; | 2001 | struct cmd_results *error = NULL; |
2001 | if (config->reading) return cmd_results_new(CMD_FAILURE, "resize", "Can't be used in config file."); | 2002 | if (config->reading) return cmd_results_new(CMD_FAILURE, "resize", "Can't be used in config file."); |
2002 | if (!config->active) return cmd_results_new(CMD_FAILURE, "resize", "Can only be used when sway is running."); | 2003 | if (!config->active) return cmd_results_new(CMD_FAILURE, "resize", "Can only be used when sway is running."); |
2004 | |||
2005 | if (strcasecmp(argv[0], "set") == 0) { | ||
2006 | return cmd_resize_set(argc - 1, &argv[1]); | ||
2007 | } | ||
2008 | |||
2003 | if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { | 2009 | if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { |
2004 | return error; | 2010 | return error; |
2005 | } | 2011 | } |
2006 | 2012 | ||
2007 | int amount = (int)strtol(argv[argc - 1], NULL, 10); | 2013 | int dim_arg = argc - 1; |
2014 | |||
2015 | enum resize_dim_types dim_type = RESIZE_DIM_DEFAULT; | ||
2016 | if (strcasecmp(argv[dim_arg], "ppt") == 0) { | ||
2017 | dim_type = RESIZE_DIM_PPT; | ||
2018 | dim_arg--; | ||
2019 | } else if (strcasecmp(argv[dim_arg], "px") == 0) { | ||
2020 | dim_type = RESIZE_DIM_PX; | ||
2021 | dim_arg--; | ||
2022 | } | ||
2023 | |||
2024 | int amount = (int)strtol(argv[dim_arg], NULL, 10); | ||
2008 | if (errno == ERANGE || amount == 0) { | 2025 | if (errno == ERANGE || amount == 0) { |
2009 | errno = 0; | 2026 | errno = 0; |
2010 | return cmd_results_new(CMD_INVALID, "resize", "Number is out of range."); | 2027 | amount = 10; // this is the default resize dimension used by i3 for both px and ppt |
2028 | sway_log(L_DEBUG, "Tried to get resize dimension out of '%s' but failed; setting dimension to default %d", | ||
2029 | argv[dim_arg], amount); | ||
2011 | } | 2030 | } |
2012 | 2031 | ||
2013 | if (strcmp(argv[0], "shrink") == 0 || strcmp(argv[0], "grow") == 0) { | 2032 | bool use_width = false; |
2014 | if (strcmp(argv[0], "shrink") == 0) { | 2033 | if (strcasecmp(argv[1], "width") == 0) { |
2015 | amount *= -1; | 2034 | use_width = true; |
2016 | } | 2035 | } else if (strcasecmp(argv[1], "height") != 0) { |
2036 | return cmd_results_new(CMD_INVALID, "resize", | ||
2037 | "Expected 'resize <shrink|grow> <width|height> [<amount>] [px|ppt]'"); | ||
2038 | } | ||
2017 | 2039 | ||
2018 | if (strcmp(argv[1], "width") == 0) { | 2040 | if (strcasecmp(argv[0], "shrink") == 0) { |
2019 | resize_tiled(amount, true); | 2041 | amount *= -1; |
2020 | } else if (strcmp(argv[1], "height") == 0) { | 2042 | } else if (strcasecmp(argv[0], "grow") != 0) { |
2021 | resize_tiled(amount, false); | 2043 | return cmd_results_new(CMD_INVALID, "resize", |
2022 | } else { | 2044 | "Expected 'resize <shrink|grow> <width|height> [<amount>] [px|ppt]'"); |
2023 | return cmd_results_new(CMD_INVALID, "resize", | 2045 | } |
2024 | "Expected 'resize <shrink|grow> <width|height> <amount>' or 'resize <width|height> <amount>'"); | 2046 | |
2047 | resize(amount, use_width, dim_type); | ||
2048 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
2049 | } | ||
2050 | |||
2051 | static struct cmd_results *cmd_resize_set(int argc, char **argv) { | ||
2052 | struct cmd_results *error = NULL; | ||
2053 | if ((error = checkarg(argc, "resize set", EXPECTED_AT_LEAST, 2))) { | ||
2054 | return error; | ||
2055 | } | ||
2056 | |||
2057 | if (strcasecmp(argv[0], "width") == 0 || strcasecmp(argv[0], "height") == 0) { | ||
2058 | // handle `reset set width 100 px height 100 px` syntax, also allows | ||
2059 | // specifying only one dimension for a `resize set` | ||
2060 | int cmd_num = 0; | ||
2061 | int dim; | ||
2062 | |||
2063 | while ((cmd_num + 1) < argc) { | ||
2064 | dim = (int)strtol(argv[cmd_num + 1], NULL, 10); | ||
2065 | if (errno == ERANGE || dim == 0) { | ||
2066 | errno = 0; | ||
2067 | return cmd_results_new(CMD_INVALID, "resize set", | ||
2068 | "Expected 'resize set <width|height> <amount> [px] [<width|height> <amount> [px]]'"); | ||
2069 | } | ||
2070 | |||
2071 | if (strcasecmp(argv[cmd_num], "width") == 0) { | ||
2072 | set_size(dim, true); | ||
2073 | } else if (strcasecmp(argv[cmd_num], "height") == 0) { | ||
2074 | set_size(dim, false); | ||
2075 | } else { | ||
2076 | return cmd_results_new(CMD_INVALID, "resize set", | ||
2077 | "Expected 'resize set <width|height> <amount> [px] [<width|height> <amount> [px]]'"); | ||
2078 | } | ||
2079 | |||
2080 | cmd_num += 2; | ||
2081 | |||
2082 | if (cmd_num < argc && strcasecmp(argv[cmd_num], "px") == 0) { | ||
2083 | // if this was `resize set width 400 px height 300 px`, disregard the `px` arg | ||
2084 | cmd_num++; | ||
2085 | } | ||
2025 | } | 2086 | } |
2026 | } else if (strcmp(argv[0], "width") == 0) { | ||
2027 | set_size_tiled(amount, true); | ||
2028 | } else if (strcmp(argv[0], "height") == 0) { | ||
2029 | set_size_tiled(amount, false); | ||
2030 | } else { | 2087 | } else { |
2031 | return cmd_results_new(CMD_INVALID, "resize", | 2088 | // handle `reset set 100 px 100 px` syntax |
2032 | "Expected 'resize <shrink|grow> <width|height> <amount>' or 'resize <width|height> <amount>'"); | 2089 | int width = (int)strtol(argv[0], NULL, 10); |
2090 | if (errno == ERANGE || width == 0) { | ||
2091 | errno = 0; | ||
2092 | return cmd_results_new(CMD_INVALID, "resize set", | ||
2093 | "Expected 'resize set <width> [px] <height> [px]'"); | ||
2094 | } | ||
2095 | |||
2096 | int height_arg = 1; | ||
2097 | if (strcasecmp(argv[1], "px") == 0) { | ||
2098 | height_arg = 2; | ||
2099 | } | ||
2100 | |||
2101 | int height = (int)strtol(argv[height_arg], NULL, 10); | ||
2102 | if (errno == ERANGE || height == 0) { | ||
2103 | errno = 0; | ||
2104 | return cmd_results_new(CMD_INVALID, "resize set", | ||
2105 | "Expected 'resize set <width> [px] <height> [px]'"); | ||
2106 | } | ||
2107 | |||
2108 | set_size(width, true); | ||
2109 | set_size(height, false); | ||
2033 | } | 2110 | } |
2034 | 2111 | ||
2035 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | 2112 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); |