aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/gaps.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands/gaps.c')
-rw-r--r--sway/commands/gaps.c183
1 files changed, 183 insertions, 0 deletions
diff --git a/sway/commands/gaps.c b/sway/commands/gaps.c
new file mode 100644
index 00000000..aacb792b
--- /dev/null
+++ b/sway/commands/gaps.c
@@ -0,0 +1,183 @@
1#include <string.h>
2#include "sway/commands.h"
3#include "sway/config.h"
4#include "sway/tree/arrange.h"
5#include "log.h"
6#include "stringop.h"
7#include <math.h>
8
9enum gaps_op {
10 GAPS_OP_SET,
11 GAPS_OP_ADD,
12 GAPS_OP_SUBTRACT
13};
14
15enum gaps_scope {
16 GAPS_SCOPE_ALL,
17 GAPS_SCOPE_WORKSPACE,
18 GAPS_SCOPE_CURRENT
19};
20
21struct cmd_results *cmd_gaps(int argc, char **argv) {
22 struct cmd_results *error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1);
23 if (error) {
24 return error;
25 }
26
27 if (strcmp(argv[0], "edge_gaps") == 0) {
28 if ((error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 2))) {
29 return error;
30 }
31
32 if (strcmp(argv[1], "on") == 0) {
33 config->edge_gaps = true;
34 arrange_root();
35 } else if (strcmp(argv[1], "off") == 0) {
36 config->edge_gaps = false;
37 arrange_root();
38 } else if (strcmp(argv[1], "toggle") == 0) {
39 if (!config->active) {
40 return cmd_results_new(CMD_INVALID, "gaps",
41 "Cannot toggle gaps while not running.");
42 }
43 config->edge_gaps = !config->edge_gaps;
44 arrange_root();
45 } else {
46 return cmd_results_new(CMD_INVALID, "gaps",
47 "gaps edge_gaps on|off|toggle");
48 }
49 } else {
50 int amount_idx = 0; // the current index in argv
51 enum gaps_op op = GAPS_OP_SET;
52 enum gaps_scope scope = GAPS_SCOPE_ALL;
53 bool inner = true;
54
55 if (strcmp(argv[0], "inner") == 0) {
56 amount_idx++;
57 inner = true;
58 } else if (strcmp(argv[0], "outer") == 0) {
59 amount_idx++;
60 inner = false;
61 }
62
63 // If one of the long variants of the gaps command is used
64 // (which starts with inner|outer) check the number of args
65 if (amount_idx > 0) { // if we've seen inner|outer
66 if (argc > 2) { // check the longest variant
67 error = checkarg(argc, "gaps", EXPECTED_EQUAL_TO, 4);
68 if (error) {
69 return error;
70 }
71 } else { // check the next longest format
72 error = checkarg(argc, "gaps", EXPECTED_EQUAL_TO, 2);
73 if (error) {
74 return error;
75 }
76 }
77 } else {
78 error = checkarg(argc, "gaps", EXPECTED_EQUAL_TO, 1);
79 if (error) {
80 return error;
81 }
82 }
83
84 if (argc == 4) {
85 // Long format: all|workspace|current.
86 if (strcmp(argv[amount_idx], "all") == 0) {
87 amount_idx++;
88 scope = GAPS_SCOPE_ALL;
89 } else if (strcmp(argv[amount_idx], "workspace") == 0) {
90 amount_idx++;
91 scope = GAPS_SCOPE_WORKSPACE;
92 } else if (strcmp(argv[amount_idx], "current") == 0) {
93 amount_idx++;
94 scope = GAPS_SCOPE_CURRENT;
95 }
96
97 // Long format: set|plus|minus
98 if (strcmp(argv[amount_idx], "set") == 0) {
99 amount_idx++;
100 op = GAPS_OP_SET;
101 } else if (strcmp(argv[amount_idx], "plus") == 0) {
102 amount_idx++;
103 op = GAPS_OP_ADD;
104 } else if (strcmp(argv[amount_idx], "minus") == 0) {
105 amount_idx++;
106 op = GAPS_OP_SUBTRACT;
107 }
108 }
109
110 char *end;
111 double val = strtod(argv[amount_idx], &end);
112
113 if (strlen(end) && val == 0.0) { // invalid <amount>
114 // guess which variant of the command was attempted
115 if (argc == 1) {
116 return cmd_results_new(CMD_INVALID, "gaps", "gaps <amount>");
117 }
118 if (argc == 2) {
119 return cmd_results_new(CMD_INVALID, "gaps",
120 "gaps inner|outer <amount>");
121 }
122 return cmd_results_new(CMD_INVALID, "gaps",
123 "gaps inner|outer all|workspace|current set|plus|minus <amount>");
124 }
125
126 if (amount_idx == 0) { // gaps <amount>
127 config->gaps_inner = val;
128 config->gaps_outer = val;
129 arrange_root();
130 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
131 }
132 // Other variants. The middle-length variant (gaps inner|outer <amount>)
133 // just defaults the scope to "all" and defaults the op to "set".
134
135 double total;
136 switch (op) {
137 case GAPS_OP_SUBTRACT: {
138 total = (inner ? config->gaps_inner : config->gaps_outer) - val;
139 if (total < 0) {
140 total = 0;
141 }
142 break;
143 }
144 case GAPS_OP_ADD: {
145 total = (inner ? config->gaps_inner : config->gaps_outer) + val;
146 break;
147 }
148 case GAPS_OP_SET: {
149 total = val;
150 break;
151 }
152 }
153
154 if (scope == GAPS_SCOPE_ALL) {
155 if (inner) {
156 config->gaps_inner = total;
157 } else {
158 config->gaps_outer = total;
159 }
160 arrange_root();
161 } else {
162 struct sway_container *c =
163 config->handler_context.current_container;
164 if (scope == GAPS_SCOPE_WORKSPACE && c->type != C_WORKSPACE) {
165 c = container_parent(c, C_WORKSPACE);
166 }
167 c->has_gaps = true;
168 if (inner) {
169 c->gaps_inner = total;
170 } else {
171 c->gaps_outer = total;
172 }
173
174 if (c->parent) {
175 arrange_children_of(c->parent);
176 } else {
177 arrange_root();
178 }
179 }
180 }
181
182 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
183}