aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/layout.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands/layout.c')
-rw-r--r--sway/commands/layout.c173
1 files changed, 16 insertions, 157 deletions
diff --git a/sway/commands/layout.c b/sway/commands/layout.c
index 57a86565..4c49a627 100644
--- a/sway/commands/layout.c
+++ b/sway/commands/layout.c
@@ -1,196 +1,55 @@
1#include <string.h> 1#include <string.h>
2#include <strings.h> 2#include <strings.h>
3#include "sway/commands.h" 3#include "sway/commands.h"
4#include "sway/container.h" 4#include "sway/tree/container.h"
5#include "sway/layout.h" 5#include "sway/tree/layout.h"
6 6#include "log.h"
7/**
8 * handle "layout auto" command group
9 */
10static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char **argv);
11 7
12struct cmd_results *cmd_layout(int argc, char **argv) { 8struct cmd_results *cmd_layout(int argc, char **argv) {
13 struct cmd_results *error = NULL; 9 struct cmd_results *error = NULL;
14 if (config->reading) return cmd_results_new(CMD_FAILURE, "layout", "Can't be used in config file.");
15 if (!config->active) return cmd_results_new(CMD_FAILURE, "layout", "Can only be used when sway is running.");
16 if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { 10 if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) {
17 return error; 11 return error;
18 } 12 }
19 swayc_t *parent = current_container; 13 struct sway_container *parent = config->handler_context.current_container;
14
15 // TODO: floating
16 /*
20 if (parent->is_floating) { 17 if (parent->is_floating) {
21 return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows"); 18 return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows");
22 } 19 }
20 */
23 21
24 while (parent->type == C_VIEW) { 22 while (parent->type == C_VIEW) {
25 parent = parent->parent; 23 parent = parent->parent;
26 } 24 }
27 25
28 enum swayc_layouts old_layout = parent->layout; 26 // TODO: stacks and tabs
29 27
30 if (strcasecmp(argv[0], "default") == 0) { 28 if (strcasecmp(argv[0], "default") == 0) {
31 swayc_change_layout(parent, parent->prev_layout); 29 container_set_layout(parent, parent->prev_layout);
32 if (parent->layout == L_NONE) { 30 if (parent->layout == L_NONE) {
33 swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT); 31 container_set_layout(parent, container_get_default_layout(parent));
34 swayc_change_layout(parent, default_layout(output));
35 } 32 }
36 } else { 33 } else {
37 if (parent->layout != L_TABBED && parent->layout != L_STACKED) { 34 if (parent->layout != L_TABBED && parent->layout != L_STACKED) {
38 parent->prev_layout = parent->layout; 35 parent->prev_layout = parent->layout;
39 } 36 }
40 37
41 if (strcasecmp(argv[0], "tabbed") == 0) { 38 if (strcasecmp(argv[0], "splith") == 0) {
42 if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ 39 container_set_layout(parent, L_HORIZ);
43 parent = new_container(parent, L_TABBED);
44 }
45
46 swayc_change_layout(parent, L_TABBED);
47 } else if (strcasecmp(argv[0], "stacking") == 0) {
48 if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)) {
49 parent = new_container(parent, L_STACKED);
50 }
51
52 swayc_change_layout(parent, L_STACKED);
53 } else if (strcasecmp(argv[0], "splith") == 0) {
54 swayc_change_layout(parent, L_HORIZ);
55 } else if (strcasecmp(argv[0], "splitv") == 0) { 40 } else if (strcasecmp(argv[0], "splitv") == 0) {
56 swayc_change_layout(parent, L_VERT); 41 container_set_layout(parent, L_VERT);
57 } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) { 42 } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) {
58 if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE 43 if (parent->layout == L_HORIZ && (parent->workspace_layout == L_NONE
59 || parent->workspace_layout == L_HORIZ)) { 44 || parent->workspace_layout == L_HORIZ)) {
60 swayc_change_layout(parent, L_VERT); 45 container_set_layout(parent, L_VERT);
61 } else { 46 } else {
62 swayc_change_layout(parent, L_HORIZ); 47 container_set_layout(parent, L_HORIZ);
63 } 48 }
64 } else if (strcasecmp(argv[0], "auto") == 0) {
65 return cmd_layout_auto(parent, argc, argv);
66 } 49 }
67 } 50 }
68 51
69 update_layout_geometry(parent, old_layout);
70 update_geometry(parent);
71
72 arrange_windows(parent, parent->width, parent->height); 52 arrange_windows(parent, parent->width, parent->height);
73 53
74 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 54 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
75} 55}
76
77static struct cmd_results *cmd_layout_auto(swayc_t *container, int argc, char **argv) {
78 // called after checking that argv[0] is auto, so just continue parsing from there
79 struct cmd_results *error = NULL;
80 const char *cmd_name = "layout auto";
81 const char *set_inc_cmd_name = "layout auto [master|ncol] [set|inc]";
82 const char *err_msg = "Allowed arguments are <right|left|top|bottom|next|prev|master|ncol>";
83
84 bool need_layout_update = false;
85 enum swayc_layouts old_layout = container->layout;
86 enum swayc_layouts layout = old_layout;
87
88 if ((error = checkarg(argc, "layout auto", EXPECTED_MORE_THAN, 1))) {
89 return error;
90 }
91
92 if (strcasecmp(argv[1], "left") == 0) {
93 layout = L_AUTO_LEFT;
94 } else if (strcasecmp(argv[1], "right") == 0) {
95 layout = L_AUTO_RIGHT;
96 } else if (strcasecmp(argv[1], "top") == 0) {
97 layout = L_AUTO_TOP;
98 } else if (strcasecmp(argv[1], "bottom") == 0) {
99 layout = L_AUTO_BOTTOM;
100 } else if (strcasecmp(argv[1], "next") == 0) {
101 if (is_auto_layout(container->layout) && container->layout < L_AUTO_LAST) {
102 layout = container->layout + 1;
103 } else {
104 layout = L_AUTO_FIRST;
105 }
106 } else if (strcasecmp(argv[1], "prev") == 0) {
107 if (is_auto_layout(container->layout) && container->layout > L_AUTO_FIRST) {
108 layout = container->layout - 1;
109 } else {
110 layout = L_AUTO_LAST;
111 }
112 } else {
113 bool is_nmaster;
114 bool is_set;
115 if (strcasecmp(argv[1], "master") == 0) {
116 is_nmaster = true;
117 } else if (strcasecmp(argv[1], "ncol") == 0) {
118 is_nmaster = false;
119 } else {
120 return cmd_results_new(CMD_INVALID, cmd_name, "Invalid %s command. %s",
121 cmd_name, err_msg);
122 }
123 if ((error = checkarg(argc, "auto <master|ncol>", EXPECTED_EQUAL_TO, 4))) {
124 return error;
125 }
126 if (strcasecmp(argv[2], "set") == 0) {
127 is_set = true;
128 } else if (strcasecmp(argv[2], "inc") == 0) {
129 is_set = false;
130 } else {
131 return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command. %s, "
132 "Argument must be on of <set|inc>",
133 set_inc_cmd_name);
134 }
135 char *end;
136 int n = (int)strtol(argv[3], &end, 10);
137 if (*end) {
138 return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command "
139 "(argument must be an integer)", set_inc_cmd_name);
140 }
141 if (is_auto_layout(container->layout)) {
142 int inc = 0; /* difference between current master/ncol and requested value */
143 if (is_nmaster) {
144 if (is_set) {
145 if (n < 0) {
146 return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command "
147 "(master must be >= 0)", set_inc_cmd_name);
148 }
149 inc = n - (int)container->nb_master;
150 } else { /* inc command */
151 if ((int)container->nb_master + n >= 0) {
152 inc = n;
153 }
154 }
155 if (inc) {
156 for (int i = container->nb_master;
157 i >= 0 && i < container->children->length
158 && i != (int)container->nb_master + inc;) {
159 ((swayc_t *)container->children->items[i])->height = -1;
160 ((swayc_t *)container->children->items[i])->width = -1;
161 i += inc > 0 ? 1 : -1;
162 }
163 container->nb_master += inc;
164 need_layout_update = true;
165 }
166 } else { /* ncol modification */
167 if (is_set) {
168 if (n <= 0) {
169 return cmd_results_new(CMD_INVALID, set_inc_cmd_name, "Invalid %s command "
170 "(ncol must be > 0)", set_inc_cmd_name);
171 }
172 inc = n - (int)container->nb_slave_groups;
173 } else { /* inc command */
174 if ((int)container->nb_slave_groups + n > 0) {
175 inc = n;
176 }
177 }
178 if (inc) {
179 container->nb_slave_groups += inc;
180 need_layout_update = true;
181 }
182 }
183 }
184 }
185
186 if (layout != old_layout) {
187 swayc_change_layout(container, layout);
188 update_layout_geometry(container, old_layout);
189 need_layout_update = true;
190 }
191 if (need_layout_update) {
192 update_geometry(container);
193 arrange_windows(container, container->width, container->height);
194 }
195 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
196}