Skip to content

Commit 2231586

Browse files
committed
Implement splith/splitv
Ref #2
1 parent 9f554b1 commit 2231586

File tree

5 files changed

+71
-23
lines changed

5 files changed

+71
-23
lines changed

sway/commands.c

+29-11
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,38 @@ int cmd_set(struct sway_config *config, int argc, char **argv) {
146146
return 0;
147147
}
148148

149-
int cmd_log_colors(struct sway_config *config, int argc, char **argv) {
150-
if (argc != 1) {
151-
sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, 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]);
149+
int _do_split(struct sway_config *config, int argc, char **argv, int layout) {
150+
if (argc != 0) {
151+
sway_log(L_ERROR, "Invalid splitv command (expected 0 arguments, got %d)", argc);
157152
return 1;
158153
}
159-
160-
sway_log_colors(!strcasecmp(argv[0], "yes"));
154+
swayc_t *focused = get_focused_container(&root_container);
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);
161171
return 0;
162172
}
163173

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+
}
164181

165182
/* Keep alphabetized */
166183
struct cmd_handler handlers[] = {
@@ -169,8 +186,9 @@ struct cmd_handler handlers[] = {
169186
{ "exit", cmd_exit },
170187
{ "focus_follows_mouse", cmd_focus_follows_mouse },
171188
{ "layout", cmd_layout },
172-
{ "log_colors", cmd_log_colors },
173189
{ "set", cmd_set },
190+
{ "splith", cmd_splith },
191+
{ "splitv", cmd_splitv }
174192
};
175193

176194
char **split_directive(char *line, int *argc) {

sway/layout.c

+31-11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void arrange_windows(swayc_t *container, int width, int height) {
7272
switch (container->layout) {
7373
case L_HORIZ:
7474
default:
75+
sway_log(L_DEBUG, "Arranging %p horizontally", container);
7576
for (i = 0; i < container->children->length; ++i) {
7677
swayc_t *child = container->children->items[i];
7778
double percent = child->weight / total_weight;
@@ -85,6 +86,7 @@ void arrange_windows(swayc_t *container, int width, int height) {
8586
}
8687
break;
8788
case L_VERT:
89+
sway_log(L_DEBUG, "Arranging %p vertically", container);
8890
for (i = 0; i < container->children->length; ++i) {
8991
swayc_t *child = container->children->items[i];
9092
double percent = child->weight / total_weight;
@@ -166,6 +168,22 @@ void add_view(wlc_handle view_handle) {
166168
arrange_windows(parent, -1, -1);
167169
}
168170

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+
169187
void destroy_view(swayc_t *view) {
170188
if (view == NULL) {
171189
sway_log(L_DEBUG, "Warning: NULL passed into destroy_view");
@@ -234,30 +252,32 @@ void add_child(swayc_t *parent, swayc_t *child) {
234252
list_add(parent->children, child);
235253
}
236254

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+
237266
void add_output(wlc_handle output) {
238267
sway_log(L_DEBUG, "Adding output %d", output);
239268
const struct wlc_size* size = wlc_output_get_resolution(output);
240269

241-
swayc_t *container = calloc(1, sizeof(swayc_t));
242-
container->weight = 1;
243-
container->handle = output;
270+
swayc_t *container = create_container(&root_container, output);
244271
container->type = C_OUTPUT;
245-
container->children = create_list();
246-
container->parent = &root_container;
247-
container->layout = L_NONE;
248272
container->width = size->w;
249273
container->height = size->h;
250274
add_child(&root_container, container);
251275

252-
swayc_t *workspace = calloc(1, sizeof(swayc_t));
253-
workspace->weight = 1;
254-
workspace->handle = -1;
276+
swayc_t *workspace = create_container(container, -1);
255277
workspace->type = C_WORKSPACE;
256-
workspace->parent = container;
257278
workspace->width = size->w; // TODO: gaps
258279
workspace->height = size->h;
259280
workspace->layout = L_HORIZ; // TODO: Get default layout from config
260-
workspace->children = create_list();
261281
add_child(container, workspace);
262282

263283
if (root_container.focused == NULL) {

sway/layout.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ void focus_view(swayc_t *view);
5353
void arrange_windows(swayc_t *container, int width, int height);
5454
swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data);
5555
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);
5758
swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent);
5859

5960
#endif

sway/list.c

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ void list_add(list_t *list, void *item) {
2727
list->items[list->length++] = item;
2828
}
2929

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+
3038
void list_del(list_t *list, int index) {
3139
list->length--;
3240
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->capacity - index - 1));

sway/list.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct {
1010
list_t *create_list();
1111
void list_free(list_t *list);
1212
void list_add(list_t *list, void *item);
13+
void list_insert(list_t *list, int index, void *item);
1314
void list_del(list_t *list, int index);
1415
void list_cat(list_t *list, list_t *source);
1516

0 commit comments

Comments
 (0)