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) { return 0; } -int cmd_log_colors(struct sway_config *config, int argc, char **argv) { - if (argc != 1) { - sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, got %d)", argc); - return 1; - } - - if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) { - sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); +int _do_split(struct sway_config *config, int argc, char **argv, int layout) { + if (argc != 0) { + sway_log(L_ERROR, "Invalid splitv command (expected 0 arguments, got %d)", argc); return 1; } - - sway_log_colors(!strcasecmp(argv[0], "yes")); + swayc_t *focused = get_focused_container(&root_container); + swayc_t *parent = focused->parent; + sway_log(L_DEBUG, "Splitting %p vertically with %p", parent, focused); + int index = remove_container_from_parent(parent, focused); + swayc_t *new_container = create_container(parent, -1); + new_container->layout = layout; + new_container->weight = focused->weight; + new_container->width = focused->width; + new_container->height = focused->height; + new_container->x = focused->x; + new_container->y = focused->y; + focused->weight = 1; + focused->parent = new_container; + list_insert(parent->children, index, new_container); + list_add(new_container->children, focused); + focus_view(focused); + arrange_windows(parent, -1, -1); return 0; } +int cmd_splitv(struct sway_config *config, int argc, char **argv) { + return _do_split(config, argc, argv, L_VERT); +} + +int cmd_splith(struct sway_config *config, int argc, char **argv) { + return _do_split(config, argc, argv, L_HORIZ); +} /* Keep alphabetized */ struct cmd_handler handlers[] = { @@ -169,8 +186,9 @@ struct cmd_handler handlers[] = { { "exit", cmd_exit }, { "focus_follows_mouse", cmd_focus_follows_mouse }, { "layout", cmd_layout }, - { "log_colors", cmd_log_colors }, { "set", cmd_set }, + { "splith", cmd_splith }, + { "splitv", cmd_splitv } }; 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) { switch (container->layout) { case L_HORIZ: default: + sway_log(L_DEBUG, "Arranging %p horizontally", container); for (i = 0; i < container->children->length; ++i) { swayc_t *child = container->children->items[i]; double percent = child->weight / total_weight; @@ -85,6 +86,7 @@ void arrange_windows(swayc_t *container, int width, int height) { } break; case L_VERT: + sway_log(L_DEBUG, "Arranging %p vertically", container); for (i = 0; i < container->children->length; ++i) { swayc_t *child = container->children->items[i]; double percent = child->weight / total_weight; @@ -166,6 +168,22 @@ void add_view(wlc_handle view_handle) { arrange_windows(parent, -1, -1); } +int remove_container_from_parent(swayc_t *parent, swayc_t *container) { + int i; + for (i = 0; i < parent->children->length; ++i) { + if (parent->children->items[i] == container) { + list_del(parent->children, i); + break; + } + } + + if (parent->focused == container) { + parent->focused = NULL; + } + + return i; +} + void destroy_view(swayc_t *view) { if (view == NULL) { sway_log(L_DEBUG, "Warning: NULL passed into destroy_view"); @@ -234,30 +252,32 @@ void add_child(swayc_t *parent, swayc_t *child) { list_add(parent->children, child); } +swayc_t *create_container(swayc_t *parent, wlc_handle handle) { + swayc_t *c = calloc(1, sizeof(swayc_t)); + c->weight = 1; + c->handle = handle; + c->parent = parent; + c->layout = L_NONE; + c->type = C_CONTAINER; + c->children = create_list(); + return c; +} + void add_output(wlc_handle output) { sway_log(L_DEBUG, "Adding output %d", output); const struct wlc_size* size = wlc_output_get_resolution(output); - swayc_t *container = calloc(1, sizeof(swayc_t)); - container->weight = 1; - container->handle = output; + swayc_t *container = create_container(&root_container, output); container->type = C_OUTPUT; - container->children = create_list(); - container->parent = &root_container; - container->layout = L_NONE; container->width = size->w; container->height = size->h; add_child(&root_container, container); - swayc_t *workspace = calloc(1, sizeof(swayc_t)); - workspace->weight = 1; - workspace->handle = -1; + swayc_t *workspace = create_container(container, -1); workspace->type = C_WORKSPACE; - workspace->parent = container; workspace->width = size->w; // TODO: gaps workspace->height = size->h; workspace->layout = L_HORIZ; // TODO: Get default layout from config - workspace->children = create_list(); add_child(container, workspace); 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); void arrange_windows(swayc_t *container, int width, int height); swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data); swayc_t *get_focused_container(swayc_t *parent); - +int remove_container_from_parent(swayc_t *parent, swayc_t *container); +swayc_t *create_container(swayc_t *parent, wlc_handle handle); swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); #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) { list->items[list->length++] = item; } +void list_insert(list_t *list, int index, void *item) { + if (list->length == list->capacity) { + list->capacity += 10; + list->items = realloc(list->items, sizeof(void*) * list->capacity); + } + list->items[list->length++] = item; +} + void list_del(list_t *list, int index) { list->length--; 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 { list_t *create_list(); void list_free(list_t *list); void list_add(list_t *list, void *item); +void list_insert(list_t *list, int index, void *item); void list_del(list_t *list, int index); void list_cat(list_t *list, list_t *source);