diff options
Diffstat (limited to 'sway/commands/resize.c')
-rw-r--r-- | sway/commands/resize.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/sway/commands/resize.c b/sway/commands/resize.c new file mode 100644 index 00000000..b5585920 --- /dev/null +++ b/sway/commands/resize.c | |||
@@ -0,0 +1,122 @@ | |||
1 | #include <errno.h> | ||
2 | #include <stdbool.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include "commands.h" | ||
6 | #include "log.h" | ||
7 | #include "resize.h" | ||
8 | |||
9 | static struct cmd_results *cmd_resize_set(int argc, char **argv) { | ||
10 | struct cmd_results *error = NULL; | ||
11 | if ((error = checkarg(argc, "resize set", EXPECTED_AT_LEAST, 2))) { | ||
12 | return error; | ||
13 | } | ||
14 | |||
15 | if (strcasecmp(argv[0], "width") == 0 || strcasecmp(argv[0], "height") == 0) { | ||
16 | // handle `reset set width 100 px height 100 px` syntax, also allows | ||
17 | // specifying only one dimension for a `resize set` | ||
18 | int cmd_num = 0; | ||
19 | int dim; | ||
20 | |||
21 | while ((cmd_num + 1) < argc) { | ||
22 | dim = (int)strtol(argv[cmd_num + 1], NULL, 10); | ||
23 | if (errno == ERANGE || dim == 0) { | ||
24 | errno = 0; | ||
25 | return cmd_results_new(CMD_INVALID, "resize set", | ||
26 | "Expected 'resize set <width|height> <amount> [px] [<width|height> <amount> [px]]'"); | ||
27 | } | ||
28 | |||
29 | if (strcasecmp(argv[cmd_num], "width") == 0) { | ||
30 | set_size(dim, true); | ||
31 | } else if (strcasecmp(argv[cmd_num], "height") == 0) { | ||
32 | set_size(dim, false); | ||
33 | } else { | ||
34 | return cmd_results_new(CMD_INVALID, "resize set", | ||
35 | "Expected 'resize set <width|height> <amount> [px] [<width|height> <amount> [px]]'"); | ||
36 | } | ||
37 | |||
38 | cmd_num += 2; | ||
39 | |||
40 | if (cmd_num < argc && strcasecmp(argv[cmd_num], "px") == 0) { | ||
41 | // if this was `resize set width 400 px height 300 px`, disregard the `px` arg | ||
42 | cmd_num++; | ||
43 | } | ||
44 | } | ||
45 | } else { | ||
46 | // handle `reset set 100 px 100 px` syntax | ||
47 | int width = (int)strtol(argv[0], NULL, 10); | ||
48 | if (errno == ERANGE || width == 0) { | ||
49 | errno = 0; | ||
50 | return cmd_results_new(CMD_INVALID, "resize set", | ||
51 | "Expected 'resize set <width> [px] <height> [px]'"); | ||
52 | } | ||
53 | |||
54 | int height_arg = 1; | ||
55 | if (strcasecmp(argv[1], "px") == 0) { | ||
56 | height_arg = 2; | ||
57 | } | ||
58 | |||
59 | int height = (int)strtol(argv[height_arg], NULL, 10); | ||
60 | if (errno == ERANGE || height == 0) { | ||
61 | errno = 0; | ||
62 | return cmd_results_new(CMD_INVALID, "resize set", | ||
63 | "Expected 'resize set <width> [px] <height> [px]'"); | ||
64 | } | ||
65 | |||
66 | set_size(width, true); | ||
67 | set_size(height, false); | ||
68 | } | ||
69 | |||
70 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
71 | } | ||
72 | |||
73 | struct cmd_results *cmd_resize(int argc, char **argv) { | ||
74 | struct cmd_results *error = NULL; | ||
75 | if (config->reading) return cmd_results_new(CMD_FAILURE, "resize", "Can't be used in config file."); | ||
76 | if (!config->active) return cmd_results_new(CMD_FAILURE, "resize", "Can only be used when sway is running."); | ||
77 | |||
78 | if (strcasecmp(argv[0], "set") == 0) { | ||
79 | return cmd_resize_set(argc - 1, &argv[1]); | ||
80 | } | ||
81 | |||
82 | if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { | ||
83 | return error; | ||
84 | } | ||
85 | |||
86 | int dim_arg = argc - 1; | ||
87 | |||
88 | enum resize_dim_types dim_type = RESIZE_DIM_DEFAULT; | ||
89 | if (strcasecmp(argv[dim_arg], "ppt") == 0) { | ||
90 | dim_type = RESIZE_DIM_PPT; | ||
91 | dim_arg--; | ||
92 | } else if (strcasecmp(argv[dim_arg], "px") == 0) { | ||
93 | dim_type = RESIZE_DIM_PX; | ||
94 | dim_arg--; | ||
95 | } | ||
96 | |||
97 | int amount = (int)strtol(argv[dim_arg], NULL, 10); | ||
98 | if (errno == ERANGE || amount == 0) { | ||
99 | errno = 0; | ||
100 | amount = 10; // this is the default resize dimension used by i3 for both px and ppt | ||
101 | sway_log(L_DEBUG, "Tried to get resize dimension out of '%s' but failed; setting dimension to default %d", | ||
102 | argv[dim_arg], amount); | ||
103 | } | ||
104 | |||
105 | bool use_width = false; | ||
106 | if (strcasecmp(argv[1], "width") == 0) { | ||
107 | use_width = true; | ||
108 | } else if (strcasecmp(argv[1], "height") != 0) { | ||
109 | return cmd_results_new(CMD_INVALID, "resize", | ||
110 | "Expected 'resize <shrink|grow> <width|height> [<amount>] [px|ppt]'"); | ||
111 | } | ||
112 | |||
113 | if (strcasecmp(argv[0], "shrink") == 0) { | ||
114 | amount *= -1; | ||
115 | } else if (strcasecmp(argv[0], "grow") != 0) { | ||
116 | return cmd_results_new(CMD_INVALID, "resize", | ||
117 | "Expected 'resize <shrink|grow> <width|height> [<amount>] [px|ppt]'"); | ||
118 | } | ||
119 | |||
120 | resize(amount, use_width, dim_type); | ||
121 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
122 | } | ||