aboutsummaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--include/sway/config.h2
-rw-r--r--include/sway/tree/container.h3
-rw-r--r--include/sway/tree/workspace.h7
-rw-r--r--sway/commands/gaps.c300
-rw-r--r--sway/commands/layout.c3
-rw-r--r--sway/commands/workspace.c38
-rw-r--r--sway/tree/container.c2
-rw-r--r--sway/tree/workspace.c18
8 files changed, 209 insertions, 164 deletions
diff --git a/include/sway/config.h b/include/sway/config.h
index af5c7a18..82be26ca 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -173,6 +173,8 @@ struct output_config {
173struct workspace_config { 173struct workspace_config {
174 char *workspace; 174 char *workspace;
175 char *output; 175 char *output;
176 int gaps_inner;
177 int gaps_outer;
176}; 178};
177 179
178struct bar_config { 180struct bar_config {
diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h
index 5e281a2f..b865a0f2 100644
--- a/include/sway/tree/container.h
+++ b/include/sway/tree/container.h
@@ -94,9 +94,6 @@ struct sway_container {
94 94
95 // The gaps currently applied to the container. 95 // The gaps currently applied to the container.
96 double current_gaps; 96 double current_gaps;
97 bool has_gaps;
98 double gaps_inner;
99 double gaps_outer;
100 97
101 struct sway_workspace *workspace; // NULL when hidden in the scratchpad 98 struct sway_workspace *workspace; // NULL when hidden in the scratchpad
102 struct sway_container *parent; // NULL if container in root of workspace 99 struct sway_container *parent; // NULL if container in root of workspace
diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h
index c8220b39..efdae5a1 100644
--- a/include/sway/tree/workspace.h
+++ b/include/sway/tree/workspace.h
@@ -32,10 +32,9 @@ struct sway_workspace {
32 enum sway_container_layout layout; 32 enum sway_container_layout layout;
33 enum sway_container_layout prev_split_layout; 33 enum sway_container_layout prev_split_layout;
34 34
35 double current_gaps; 35 int current_gaps;
36 bool has_gaps; 36 int gaps_inner;
37 double gaps_inner; 37 int gaps_outer;
38 double gaps_outer;
39 38
40 struct sway_output *output; // NULL if no outputs are connected 39 struct sway_output *output; // NULL if no outputs are connected
41 list_t *floating; // struct sway_container 40 list_t *floating; // struct sway_container
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}
diff --git a/sway/commands/layout.c b/sway/commands/layout.c
index ef3ec1cb..c2ce2e78 100644
--- a/sway/commands/layout.c
+++ b/sway/commands/layout.c
@@ -138,15 +138,14 @@ struct cmd_results *cmd_layout(int argc, char **argv) {
138 } 138 }
139 container->layout = new_layout; 139 container->layout = new_layout;
140 container_update_representation(container); 140 container_update_representation(container);
141 arrange_container(container);
142 } else { 141 } else {
143 if (old_layout != L_TABBED && old_layout != L_STACKED) { 142 if (old_layout != L_TABBED && old_layout != L_STACKED) {
144 workspace->prev_split_layout = old_layout; 143 workspace->prev_split_layout = old_layout;
145 } 144 }
146 workspace->layout = new_layout; 145 workspace->layout = new_layout;
147 workspace_update_representation(workspace); 146 workspace_update_representation(workspace);
148 arrange_workspace(workspace);
149 } 147 }
148 arrange_workspace(workspace);
150 } 149 }
151 150
152 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 151 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c
index 2ce7872d..63f29641 100644
--- a/sway/commands/workspace.c
+++ b/sway/commands/workspace.c
@@ -20,6 +20,8 @@ static struct workspace_config *workspace_config_find_or_create(char *ws_name) {
20 return NULL; 20 return NULL;
21 } 21 }
22 wsc->workspace = strdup(ws_name); 22 wsc->workspace = strdup(ws_name);
23 wsc->gaps_inner = -1;
24 wsc->gaps_outer = -1;
23 list_add(config->workspace_configs, wsc); 25 list_add(config->workspace_configs, wsc);
24 return wsc; 26 return wsc;
25} 27}
@@ -37,6 +39,7 @@ struct cmd_results *cmd_workspace(int argc, char **argv) {
37 } 39 }
38 40
39 int output_location = -1; 41 int output_location = -1;
42 int gaps_location = -1;
40 43
41 for (int i = 0; i < argc; ++i) { 44 for (int i = 0; i < argc; ++i) {
42 if (strcasecmp(argv[i], "output") == 0) { 45 if (strcasecmp(argv[i], "output") == 0) {
@@ -44,6 +47,12 @@ struct cmd_results *cmd_workspace(int argc, char **argv) {
44 break; 47 break;
45 } 48 }
46 } 49 }
50 for (int i = 0; i < argc; ++i) {
51 if (strcasecmp(argv[i], "gaps") == 0) {
52 gaps_location = i;
53 break;
54 }
55 }
47 if (output_location >= 0) { 56 if (output_location >= 0) {
48 if ((error = checkarg(argc, "workspace", EXPECTED_EQUAL_TO, output_location + 2))) { 57 if ((error = checkarg(argc, "workspace", EXPECTED_EQUAL_TO, output_location + 2))) {
49 return error; 58 return error;
@@ -57,6 +66,35 @@ struct cmd_results *cmd_workspace(int argc, char **argv) {
57 } 66 }
58 free(wsc->output); 67 free(wsc->output);
59 wsc->output = strdup(argv[output_location + 1]); 68 wsc->output = strdup(argv[output_location + 1]);
69 } else if (gaps_location >= 0) {
70 if ((error = checkarg(argc, "workspace", EXPECTED_EQUAL_TO, gaps_location + 3))) {
71 return error;
72 }
73 char *ws_name = join_args(argv, argc - 3);
74 struct workspace_config *wsc = workspace_config_find_or_create(ws_name);
75 free(ws_name);
76 if (!wsc) {
77 return cmd_results_new(CMD_FAILURE, "workspace gaps",
78 "Unable to allocate workspace output");
79 }
80 int *prop = NULL;
81 if (strcasecmp(argv[gaps_location + 1], "inner") == 0) {
82 prop = &wsc->gaps_inner;
83 } else if (strcasecmp(argv[gaps_location + 1], "outer") == 0) {
84 prop = &wsc->gaps_outer;
85 } else {
86 return cmd_results_new(CMD_FAILURE, "workspace gaps",
87 "Expected 'workspace <ws> gaps inner|outer <px>'");
88 }
89 char *end;
90 int val = strtol(argv[gaps_location + 2], &end, 10);
91
92 if (strlen(end)) {
93 free(end);
94 return cmd_results_new(CMD_FAILURE, "workspace gaps",
95 "Expected 'workspace <ws> gaps inner|outer <px>'");
96 }
97 *prop = val >= 0 ? val : 0;
60 } else { 98 } else {
61 if (config->reading || !config->active) { 99 if (config->reading || !config->active) {
62 return cmd_results_new(CMD_DEFER, "workspace", NULL); 100 return cmd_results_new(CMD_DEFER, "workspace", NULL);
diff --git a/sway/tree/container.c b/sway/tree/container.c
index baaa82fd..e329f835 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -1022,7 +1022,7 @@ void container_add_gaps(struct sway_container *c) {
1022 1022
1023 struct sway_workspace *ws = c->workspace; 1023 struct sway_workspace *ws = c->workspace;
1024 1024
1025 c->current_gaps = ws->has_gaps ? ws->gaps_inner : config->gaps_inner; 1025 c->current_gaps = ws->gaps_inner;
1026 c->x += c->current_gaps; 1026 c->x += c->current_gaps;
1027 c->y += c->current_gaps; 1027 c->y += c->current_gaps;
1028 c->width -= 2 * c->current_gaps; 1028 c->width -= 2 * c->current_gaps;
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c
index d65a3e4c..9dd5c815 100644
--- a/sway/tree/workspace.c
+++ b/sway/tree/workspace.c
@@ -68,6 +68,20 @@ struct sway_workspace *workspace_create(struct sway_output *output,
68 ws->output_priority = create_list(); 68 ws->output_priority = create_list();
69 workspace_output_add_priority(ws, output); 69 workspace_output_add_priority(ws, output);
70 70
71 ws->gaps_outer = config->gaps_outer;
72 ws->gaps_inner = config->gaps_inner;
73 if (name) {
74 struct workspace_config *wsc = workspace_find_config(name);
75 if (wsc) {
76 if (wsc->gaps_outer != -1) {
77 ws->gaps_outer = wsc->gaps_outer;
78 }
79 if (wsc->gaps_inner != -1) {
80 ws->gaps_inner = wsc->gaps_inner;
81 }
82 }
83 }
84
71 output_add_workspace(output, ws); 85 output_add_workspace(output, ws);
72 output_sort_workspaces(output); 86 output_sort_workspaces(output);
73 87
@@ -632,13 +646,13 @@ void workspace_add_gaps(struct sway_workspace *ws) {
632 return; 646 return;
633 } 647 }
634 648
635 ws->current_gaps = ws->has_gaps ? ws->gaps_outer : config->gaps_outer; 649 ws->current_gaps = ws->gaps_outer;
636 650
637 if (ws->layout == L_TABBED || ws->layout == L_STACKED) { 651 if (ws->layout == L_TABBED || ws->layout == L_STACKED) {
638 // We have to add inner gaps for this, because children of tabbed and 652 // We have to add inner gaps for this, because children of tabbed and
639 // stacked containers don't apply their own gaps - they assume the 653 // stacked containers don't apply their own gaps - they assume the
640 // tabbed/stacked container is using gaps. 654 // tabbed/stacked container is using gaps.
641 ws->current_gaps += ws->has_gaps ? ws->gaps_inner : config->gaps_inner; 655 ws->current_gaps += ws->gaps_inner;
642 } 656 }
643 657
644 ws->x += ws->current_gaps; 658 ws->x += ws->current_gaps;