diff options
-rw-r--r-- | sway/commands.c | 40 | ||||
-rw-r--r-- | sway/layout.c | 42 | ||||
-rw-r--r-- | sway/layout.h | 3 | ||||
-rw-r--r-- | sway/list.c | 8 | ||||
-rw-r--r-- | sway/list.h | 1 |
5 files changed, 71 insertions, 23 deletions
diff --git a/sway/commands.c b/sway/commands.c index efa72f19..322c9519 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -146,21 +146,38 @@ int cmd_set(struct sway_config *config, int argc, char **argv) { | |||
146 | return 0; | 146 | return 0; |
147 | } | 147 | } |
148 | 148 | ||
149 | int cmd_log_colors(struct sway_config *config, int argc, char **argv) { | 149 | int _do_split(struct sway_config *config, int argc, char **argv, int layout) { |
150 | if (argc != 1) { | 150 | if (argc != 0) { |
151 | sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, got %d)", argc); | 151 | sway_log(L_ERROR, "Invalid splitv command (expected 0 arguments, got %d)", argc); |
152 | return 1; | ||
153 | } | ||
154 | |||
155 | if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) { | ||
156 | sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); | ||
157 | return 1; | 152 | return 1; |
158 | } | 153 | } |
159 | 154 | swayc_t *focused = get_focused_container(&root_container); | |
160 | sway_log_colors(!strcasecmp(argv[0], "yes")); | 155 | swayc_t *parent = focused->parent; |
156 | sway_log(L_DEBUG, "Splitting %p vertically with %p", parent, focused); | ||
157 | int index = remove_container_from_parent(parent, focused); | ||
158 | swayc_t *new_container = create_container(parent, -1); | ||
159 | new_container->layout = layout; | ||
160 | new_container->weight = focused->weight; | ||
161 | new_container->width = focused->width; | ||
162 | new_container->height = focused->height; | ||
163 | new_container->x = focused->x; | ||
164 | new_container->y = focused->y; | ||
165 | focused->weight = 1; | ||
166 | focused->parent = new_container; | ||
167 | list_insert(parent->children, index, new_container); | ||
168 | list_add(new_container->children, focused); | ||
169 | focus_view(focused); | ||
170 | arrange_windows(parent, -1, -1); | ||
161 | return 0; | 171 | return 0; |
162 | } | 172 | } |
163 | 173 | ||
174 | int cmd_splitv(struct sway_config *config, int argc, char **argv) { | ||
175 | return _do_split(config, argc, argv, L_VERT); | ||
176 | } | ||
177 | |||
178 | int cmd_splith(struct sway_config *config, int argc, char **argv) { | ||
179 | return _do_split(config, argc, argv, L_HORIZ); | ||
180 | } | ||
164 | 181 | ||
165 | /* Keep alphabetized */ | 182 | /* Keep alphabetized */ |
166 | struct cmd_handler handlers[] = { | 183 | struct cmd_handler handlers[] = { |
@@ -169,8 +186,9 @@ struct cmd_handler handlers[] = { | |||
169 | { "exit", cmd_exit }, | 186 | { "exit", cmd_exit }, |
170 | { "focus_follows_mouse", cmd_focus_follows_mouse }, | 187 | { "focus_follows_mouse", cmd_focus_follows_mouse }, |
171 | { "layout", cmd_layout }, | 188 | { "layout", cmd_layout }, |
172 | { "log_colors", cmd_log_colors }, | ||
173 | { "set", cmd_set }, | 189 | { "set", cmd_set }, |
190 | { "splith", cmd_splith }, | ||
191 | { "splitv", cmd_splitv } | ||
174 | }; | 192 | }; |
175 | 193 | ||
176 | char **split_directive(char *line, int *argc) { | 194 | char **split_directive(char *line, int *argc) { |
diff --git a/sway/layout.c b/sway/layout.c index 68d7cf7e..2ce0a559 100644 --- a/sway/layout.c +++ b/sway/layout.c | |||
@@ -72,6 +72,7 @@ void arrange_windows(swayc_t *container, int width, int height) { | |||
72 | switch (container->layout) { | 72 | switch (container->layout) { |
73 | case L_HORIZ: | 73 | case L_HORIZ: |
74 | default: | 74 | default: |
75 | sway_log(L_DEBUG, "Arranging %p horizontally", container); | ||
75 | for (i = 0; i < container->children->length; ++i) { | 76 | for (i = 0; i < container->children->length; ++i) { |
76 | swayc_t *child = container->children->items[i]; | 77 | swayc_t *child = container->children->items[i]; |
77 | double percent = child->weight / total_weight; | 78 | double percent = child->weight / total_weight; |
@@ -85,6 +86,7 @@ void arrange_windows(swayc_t *container, int width, int height) { | |||
85 | } | 86 | } |
86 | break; | 87 | break; |
87 | case L_VERT: | 88 | case L_VERT: |
89 | sway_log(L_DEBUG, "Arranging %p vertically", container); | ||
88 | for (i = 0; i < container->children->length; ++i) { | 90 | for (i = 0; i < container->children->length; ++i) { |
89 | swayc_t *child = container->children->items[i]; | 91 | swayc_t *child = container->children->items[i]; |
90 | double percent = child->weight / total_weight; | 92 | double percent = child->weight / total_weight; |
@@ -166,6 +168,22 @@ void add_view(wlc_handle view_handle) { | |||
166 | arrange_windows(parent, -1, -1); | 168 | arrange_windows(parent, -1, -1); |
167 | } | 169 | } |
168 | 170 | ||
171 | int remove_container_from_parent(swayc_t *parent, swayc_t *container) { | ||
172 | int i; | ||
173 | for (i = 0; i < parent->children->length; ++i) { | ||
174 | if (parent->children->items[i] == container) { | ||
175 | list_del(parent->children, i); | ||
176 | break; | ||
177 | } | ||
178 | } | ||
179 | |||
180 | if (parent->focused == container) { | ||
181 | parent->focused = NULL; | ||
182 | } | ||
183 | |||
184 | return i; | ||
185 | } | ||
186 | |||
169 | void destroy_view(swayc_t *view) { | 187 | void destroy_view(swayc_t *view) { |
170 | if (view == NULL) { | 188 | if (view == NULL) { |
171 | sway_log(L_DEBUG, "Warning: NULL passed into destroy_view"); | 189 | sway_log(L_DEBUG, "Warning: NULL passed into destroy_view"); |
@@ -234,30 +252,32 @@ void add_child(swayc_t *parent, swayc_t *child) { | |||
234 | list_add(parent->children, child); | 252 | list_add(parent->children, child); |
235 | } | 253 | } |
236 | 254 | ||
255 | swayc_t *create_container(swayc_t *parent, wlc_handle handle) { | ||
256 | swayc_t *c = calloc(1, sizeof(swayc_t)); | ||
257 | c->weight = 1; | ||
258 | c->handle = handle; | ||
259 | c->parent = parent; | ||
260 | c->layout = L_NONE; | ||
261 | c->type = C_CONTAINER; | ||
262 | c->children = create_list(); | ||
263 | return c; | ||
264 | } | ||
265 | |||
237 | void add_output(wlc_handle output) { | 266 | void add_output(wlc_handle output) { |
238 | sway_log(L_DEBUG, "Adding output %d", output); | 267 | sway_log(L_DEBUG, "Adding output %d", output); |
239 | const struct wlc_size* size = wlc_output_get_resolution(output); | 268 | const struct wlc_size* size = wlc_output_get_resolution(output); |
240 | 269 | ||
241 | swayc_t *container = calloc(1, sizeof(swayc_t)); | 270 | swayc_t *container = create_container(&root_container, output); |
242 | container->weight = 1; | ||
243 | container->handle = output; | ||
244 | container->type = C_OUTPUT; | 271 | container->type = C_OUTPUT; |
245 | container->children = create_list(); | ||
246 | container->parent = &root_container; | ||
247 | container->layout = L_NONE; | ||
248 | container->width = size->w; | 272 | container->width = size->w; |
249 | container->height = size->h; | 273 | container->height = size->h; |
250 | add_child(&root_container, container); | 274 | add_child(&root_container, container); |
251 | 275 | ||
252 | swayc_t *workspace = calloc(1, sizeof(swayc_t)); | 276 | swayc_t *workspace = create_container(container, -1); |
253 | workspace->weight = 1; | ||
254 | workspace->handle = -1; | ||
255 | workspace->type = C_WORKSPACE; | 277 | workspace->type = C_WORKSPACE; |
256 | workspace->parent = container; | ||
257 | workspace->width = size->w; // TODO: gaps | 278 | workspace->width = size->w; // TODO: gaps |
258 | workspace->height = size->h; | 279 | workspace->height = size->h; |
259 | workspace->layout = L_HORIZ; // TODO: Get default layout from config | 280 | workspace->layout = L_HORIZ; // TODO: Get default layout from config |
260 | workspace->children = create_list(); | ||
261 | add_child(container, workspace); | 281 | add_child(container, workspace); |
262 | 282 | ||
263 | if (root_container.focused == NULL) { | 283 | if (root_container.focused == NULL) { |
diff --git a/sway/layout.h b/sway/layout.h index 0e34cb5c..08f3f78b 100644 --- a/sway/layout.h +++ b/sway/layout.h | |||
@@ -53,7 +53,8 @@ void focus_view(swayc_t *view); | |||
53 | void arrange_windows(swayc_t *container, int width, int height); | 53 | void arrange_windows(swayc_t *container, int width, int height); |
54 | swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data); | 54 | swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data); |
55 | swayc_t *get_focused_container(swayc_t *parent); | 55 | swayc_t *get_focused_container(swayc_t *parent); |
56 | 56 | int remove_container_from_parent(swayc_t *parent, swayc_t *container); | |
57 | swayc_t *create_container(swayc_t *parent, wlc_handle handle); | ||
57 | swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); | 58 | swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); |
58 | 59 | ||
59 | #endif | 60 | #endif |
diff --git a/sway/list.c b/sway/list.c index 82d6c144..55052f85 100644 --- a/sway/list.c +++ b/sway/list.c | |||
@@ -27,6 +27,14 @@ void list_add(list_t *list, void *item) { | |||
27 | list->items[list->length++] = item; | 27 | list->items[list->length++] = item; |
28 | } | 28 | } |
29 | 29 | ||
30 | void list_insert(list_t *list, int index, void *item) { | ||
31 | if (list->length == list->capacity) { | ||
32 | list->capacity += 10; | ||
33 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
34 | } | ||
35 | list->items[list->length++] = item; | ||
36 | } | ||
37 | |||
30 | void list_del(list_t *list, int index) { | 38 | void list_del(list_t *list, int index) { |
31 | list->length--; | 39 | list->length--; |
32 | memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->capacity - index - 1)); | 40 | memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->capacity - index - 1)); |
diff --git a/sway/list.h b/sway/list.h index 2f60b0df..93649fd5 100644 --- a/sway/list.h +++ b/sway/list.h | |||
@@ -10,6 +10,7 @@ typedef struct { | |||
10 | list_t *create_list(); | 10 | list_t *create_list(); |
11 | void list_free(list_t *list); | 11 | void list_free(list_t *list); |
12 | void list_add(list_t *list, void *item); | 12 | void list_add(list_t *list, void *item); |
13 | void list_insert(list_t *list, int index, void *item); | ||
13 | void list_del(list_t *list, int index); | 14 | void list_del(list_t *list, int index); |
14 | void list_cat(list_t *list, list_t *source); | 15 | void list_cat(list_t *list, list_t *source); |
15 | 16 | ||