aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/gaps.c
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-09-29 11:06:07 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-09-29 11:08:19 +1000
commit415a48ac6387a62a59adb8ed1168e851509a0ce3 (patch)
treefd0027a67439414e164318182be29a3529805543 /sway/commands/gaps.c
parentMerge pull request #2724 from RedSoxFan/update-man-pages (diff)
downloadsway-415a48ac6387a62a59adb8ed1168e851509a0ce3.tar.gz
sway-415a48ac6387a62a59adb8ed1168e851509a0ce3.tar.zst
sway-415a48ac6387a62a59adb8ed1168e851509a0ce3.zip
Make gaps implementation consistent with i3-gaps
This changes our gaps implementation to behave like i3-gaps. Our previous implementation allowed you to set gaps on a per container basis. This isn't supported by i3-gaps and doesn't seem to have a practical use case. The gaps_outer and gaps_inner properties on containers are now removed as they just read the gaps_inner from the workspace. `gaps inner|outer <px>` no longer changes the gaps for all workspaces. It only sets defaults for new workspaces. `gaps inner|outer current|workspace|all set|plus|minus <px>` is now runtime only, and the workspace option is now removed. `current` now sets gaps for the current workspace as opposed to the current container. `workspace <ws> gaps inner|outer <px>` is now implemented. This sets defaults for a workspace. This also fixes a bug where changing the layout of a split container from linear to tabbed would cause gaps to not be applied to it until you switch to another workspace and back.
Diffstat (limited to 'sway/commands/gaps.c')
-rw-r--r--sway/commands/gaps.c300
1 files changed, 148 insertions, 152 deletions
diff --git a/sway/commands/gaps.c b/sway/commands/gaps.c
index d676e475..3791fcb4 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,167 @@ 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>");
116 }
117 if (argc == 2) {
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 71
125 if (amount_idx == 0) { // gaps <amount> 72 if (inner) {
126 config->gaps_inner = val; 73 config->gaps_inner = amount;
127 config->gaps_outer = val; 74 } else {
128 arrange_root(); 75 config->gaps_outer = amount;
129 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 76 }
130 } 77 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
131 // Other variants. The middle-length variant (gaps inner|outer <amount>) 78}
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 79
153 if (scope == GAPS_SCOPE_ALL) { 80static void configure_gaps(struct sway_workspace *ws, void *_data) {
154 if (inner) { 81 struct gaps_data *data = _data;
155 config->gaps_inner = total; 82 int *prop = data->inner ? &ws->gaps_inner : &ws->gaps_outer;
156 } else { 83
157 config->gaps_outer = total; 84 switch (data->operation) {
158 } 85 case GAPS_OP_SET:
159 arrange_root(); 86 *prop = data->amount;
160 } else { 87 break;
161 if (scope == GAPS_SCOPE_WORKSPACE) { 88 case GAPS_OP_ADD:
162 struct sway_workspace *ws = config->handler_context.workspace; 89 *prop += data->amount;
163 ws->has_gaps = true; 90 break;
164 if (inner) { 91 case GAPS_OP_SUBTRACT:
165 ws->gaps_inner = total; 92 *prop -= data->amount;
166 } else { 93 break;
167 ws->gaps_outer = total; 94 }
168 } 95 arrange_workspace(ws);
169 arrange_workspace(ws); 96}
170 } else { 97
171 struct sway_container *c = config->handler_context.container; 98// gaps inner|outer current|all set|plus|minus <px>
172 c->has_gaps = true; 99static struct cmd_results *gaps_set_runtime(int argc, char **argv) {
173 if (inner) { 100 struct cmd_results *error = checkarg(argc, "gaps", EXPECTED_EQUAL_TO, 4);
174 c->gaps_inner = total; 101 if (error) {
175 } else { 102 return error;
176 c->gaps_outer = total; 103 }
177 } 104
178 arrange_workspace(c->workspace); 105 struct gaps_data data;
179 } 106
180 } 107 if (strcasecmp(argv[0], "inner") == 0) {
108 data.inner = true;
109 } else if (strcasecmp(argv[0], "outer") == 0) {
110 data.inner = false;
111 } else {
112 return cmd_results_new(CMD_INVALID, "gaps",
113 "Expected 'gaps inner|outer current|all set|plus|minus <px>'");
114 }
115
116 bool all;
117 if (strcasecmp(argv[1], "current") == 0) {
118 all = false;
119 } else if (strcasecmp(argv[1], "all") == 0) {
120 all = true;
121 } else {
122 return cmd_results_new(CMD_INVALID, "gaps",
123 "Expected 'gaps inner|outer current|all set|plus|minus <px>'");
124 }
125
126 if (strcasecmp(argv[2], "set") == 0) {
127 data.operation = GAPS_OP_SET;
128 } else if (strcasecmp(argv[2], "plus") == 0) {
129 data.operation = GAPS_OP_ADD;
130 } else if (strcasecmp(argv[2], "minus") == 0) {
131 data.operation = GAPS_OP_SUBTRACT;
132 } else {
133 return cmd_results_new(CMD_INVALID, "gaps",
134 "Expected 'gaps inner|outer current|all set|plus|minus <px>'");
135 }
136
137 char *end;
138 data.amount = strtol(argv[3], &end, 10);
139 if (strlen(end) && strcasecmp(end, "px") != 0) {
140 return cmd_results_new(CMD_INVALID, "gaps",
141 "Expected 'gaps inner|outer current|all set|plus|minus <px>'");
142 }
143
144 if (all) {
145 root_for_each_workspace(configure_gaps, &data);
146 } else {
147 configure_gaps(config->handler_context.workspace, &data);
181 } 148 }
182 149
183 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 150 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
184} 151}
152
153// gaps edge_gaps on|off|toggle
154// gaps inner|outer <px> - sets defaults for workspaces
155// gaps inner|outer current|all set|plus|minus <px> - runtime only
156struct cmd_results *cmd_gaps(int argc, char **argv) {
157 struct cmd_results *error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 2);
158 if (error) {
159 return error;
160 }
161
162 if (strcmp(argv[0], "edge_gaps") == 0) {
163 return gaps_edge_gaps(argc, argv);
164 }
165
166 if (argc == 2) {
167 return gaps_set_defaults(argc, argv);
168 }
169 if (argc == 4) {
170 if (config->active) {
171 return gaps_set_runtime(argc, argv);
172 } else {
173 return cmd_results_new(CMD_INVALID, "gaps",
174 "This syntax can only be used when sway is running");
175 }
176 }
177 return cmd_results_new(CMD_INVALID, "gaps",
178 "Expected 'gaps inner|outer <px>' or "
179 "'gaps inner|outer current|all set|plus|minus <px>'");
180}