Skip to content

Commit

Permalink
Implement splith/splitv
Browse files Browse the repository at this point in the history
Ref #2
  • Loading branch information
ddevault committed Aug 9, 2015
1 parent 9f554b1 commit 2231586
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
40 changes: 29 additions & 11 deletions sway/commands.c
Expand Up @@ -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[] = {
Expand All @@ -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) {
Expand Down
42 changes: 31 additions & 11 deletions sway/layout.c
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion sway/layout.h
Expand Up @@ -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
8 changes: 8 additions & 0 deletions sway/list.c
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions sway/list.h
Expand Up @@ -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);

Expand Down

0 comments on commit 2231586

Please sign in to comment.