aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/split.c
diff options
context:
space:
mode:
authorLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
committerLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
commitb374c35758777f98e5ddbe4b0dc43bd7c80f36d7 (patch)
tree04bb4cfc3da7d2e0de7fbc38db42f65c66d2df4c /sway/commands/split.c
parentMerge pull request #874 from yohanesu75/ipc-client-fix (diff)
downloadsway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.gz
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.zst
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.zip
refactor commands.c
Diffstat (limited to 'sway/commands/split.c')
-rw-r--r--sway/commands/split.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/sway/commands/split.c b/sway/commands/split.c
new file mode 100644
index 00000000..b13c9213
--- /dev/null
+++ b/sway/commands/split.c
@@ -0,0 +1,97 @@
1#include <string.h>
2#include <wlc/wlc-render.h>
3#include "border.h"
4#include "commands.h"
5#include "container.h"
6#include "focus.h"
7#include "layout.h"
8#include "log.h"
9
10static struct cmd_results *_do_split(int argc, char **argv, int layout) {
11 char *name = layout == L_VERT ? "splitv" :
12 layout == L_HORIZ ? "splith" : "split";
13 struct cmd_results *error = NULL;
14 if (config->reading) return cmd_results_new(CMD_FAILURE, name, "Can't be used in config file.");
15 if (!config->active) return cmd_results_new(CMD_FAILURE, name, "Can only be used when sway is running.");
16 if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) {
17 return error;
18 }
19 swayc_t *focused = get_focused_container(&root_container);
20
21 // Case of floating window, don't split
22 if (focused->is_floating) {
23 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
24 }
25 /* Case that focus is on an workspace with 0/1 children.change its layout */
26 if (focused->type == C_WORKSPACE && focused->children->length <= 1) {
27 sway_log(L_DEBUG, "changing workspace layout");
28 focused->layout = layout;
29 } else if (focused->type != C_WORKSPACE && focused->parent->children->length == 1) {
30 /* Case of no siblings. change parent layout */
31 sway_log(L_DEBUG, "changing container layout");
32 focused->parent->layout = layout;
33 } else {
34 /* regular case where new split container is build around focused container
35 * or in case of workspace, container inherits its children */
36 sway_log(L_DEBUG, "Adding new container around current focused container");
37 sway_log(L_INFO, "FOCUSED SIZE: %.f %.f", focused->width, focused->height);
38 swayc_t *parent = new_container(focused, layout);
39 set_focused_container(focused);
40 arrange_windows(parent, -1, -1);
41 }
42
43 // update container every time
44 // if it is tabbed/stacked then the title must change
45 // if the indicator color is different then the border must change
46 update_container_border(focused);
47 swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT);
48 // schedule render to make changes take effect right away,
49 // otherwise we would have to wait for the view to render,
50 // which is unpredictable.
51 wlc_output_schedule_render(output->handle);
52
53 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
54}
55
56struct cmd_results *cmd_split(int argc, char **argv) {
57 struct cmd_results *error = NULL;
58 if (config->reading) return cmd_results_new(CMD_FAILURE, "split", "Can't be used in config file.");
59 if (!config->active) return cmd_results_new(CMD_FAILURE, "split", "Can only be used when sway is running.");
60 if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) {
61 return error;
62 }
63 if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) {
64 _do_split(argc - 1, argv + 1, L_VERT);
65 } else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) {
66 _do_split(argc - 1, argv + 1, L_HORIZ);
67 } else if (strcasecmp(argv[0], "t") == 0 || strcasecmp(argv[0], "toggle") == 0) {
68 swayc_t *focused = get_focused_container(&root_container);
69 if (focused->parent->layout == L_VERT) {
70 _do_split(argc - 1, argv + 1, L_HORIZ);
71 } else {
72 _do_split(argc - 1, argv + 1, L_VERT);
73 }
74 } else {
75 error = cmd_results_new(CMD_FAILURE, "split",
76 "Invalid split command (expected either horizontal or vertical).");
77 return error;
78 }
79 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
80}
81
82struct cmd_results *cmd_splitv(int argc, char **argv) {
83 return _do_split(argc, argv, L_VERT);
84}
85
86struct cmd_results *cmd_splith(int argc, char **argv) {
87 return _do_split(argc, argv, L_HORIZ);
88}
89
90struct cmd_results *cmd_splitt(int argc, char **argv) {
91 swayc_t *focused = get_focused_container(&root_container);
92 if (focused->parent->layout == L_VERT) {
93 return _do_split(argc, argv, L_HORIZ);
94 } else {
95 return _do_split(argc, argv, L_VERT);
96 }
97}