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