aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/layout.c
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-08-30 21:00:10 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-09-05 18:01:43 +1000
commit7586f150c058997d9dde387ea7c091ffa7a3c3c7 (patch)
tree63d19027974c1db62ce3a74ca1d2314eb6d5049b /sway/commands/layout.c
parentMerge pull request #2569 from RyanDwyer/deny-reload-repeat (diff)
downloadsway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.tar.gz
sway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.tar.zst
sway-7586f150c058997d9dde387ea7c091ffa7a3c3c7.zip
Implement type safe arguments and demote sway_container
This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
Diffstat (limited to 'sway/commands/layout.c')
-rw-r--r--sway/commands/layout.c186
1 files changed, 115 insertions, 71 deletions
diff --git a/sway/commands/layout.c b/sway/commands/layout.c
index a06832de..8fa1ce98 100644
--- a/sway/commands/layout.c
+++ b/sway/commands/layout.c
@@ -4,21 +4,20 @@
4#include "sway/commands.h" 4#include "sway/commands.h"
5#include "sway/tree/arrange.h" 5#include "sway/tree/arrange.h"
6#include "sway/tree/container.h" 6#include "sway/tree/container.h"
7#include "sway/tree/workspace.h"
7#include "log.h" 8#include "log.h"
8 9
9static bool parse_layout_string(char *s, enum sway_container_layout *ptr) { 10static enum sway_container_layout parse_layout_string(char *s) {
10 if (strcasecmp(s, "splith") == 0) { 11 if (strcasecmp(s, "splith") == 0) {
11 *ptr = L_HORIZ; 12 return L_HORIZ;
12 } else if (strcasecmp(s, "splitv") == 0) { 13 } else if (strcasecmp(s, "splitv") == 0) {
13 *ptr = L_VERT; 14 return L_VERT;
14 } else if (strcasecmp(s, "tabbed") == 0) { 15 } else if (strcasecmp(s, "tabbed") == 0) {
15 *ptr = L_TABBED; 16 return L_TABBED;
16 } else if (strcasecmp(s, "stacking") == 0) { 17 } else if (strcasecmp(s, "stacking") == 0) {
17 *ptr = L_STACKED; 18 return L_STACKED;
18 } else {
19 return false;
20 } 19 }
21 return true; 20 return L_NONE;
22} 21}
23 22
24static const char* expected_syntax = 23static const char* expected_syntax =
@@ -26,84 +25,129 @@ static const char* expected_syntax =
26 "'layout toggle [split|all]' or " 25 "'layout toggle [split|all]' or "
27 "'layout toggle [split|tabbed|stacking|splitv|splith] [split|tabbed|stacking|splitv|splith]...'"; 26 "'layout toggle [split|tabbed|stacking|splitv|splith] [split|tabbed|stacking|splitv|splith]...'";
28 27
28static enum sway_container_layout get_layout_toggle(int argc, char **argv,
29 enum sway_container_layout layout,
30 enum sway_container_layout prev_split_layout) {
31 // "layout toggle"
32 if (argc == 0) {
33 return layout == L_HORIZ ? L_VERT : L_HORIZ;
34 }
35
36 if (argc == 2) {
37 // "layout toggle split" (same as "layout toggle")
38 if (strcasecmp(argv[1], "split") == 0) {
39 return layout == L_HORIZ ? L_VERT : L_HORIZ;
40 }
41 // "layout toggle all"
42 if (strcasecmp(argv[1], "all") == 0) {
43 return layout == L_HORIZ ? L_VERT :
44 layout == L_VERT ? L_STACKED :
45 layout == L_STACKED ? L_TABBED : L_HORIZ;
46 }
47 return L_NONE;
48 }
49
50 enum sway_container_layout parsed;
51 int curr = 1;
52 for (; curr < argc; curr++) {
53 parsed = parse_layout_string(argv[curr]);
54 if (parsed == layout || (strcmp(argv[curr], "split") == 0 &&
55 (layout == L_VERT || layout == L_HORIZ))) {
56 break;
57 }
58 }
59 for (int i = curr + 1; i != curr; ++i) {
60 // cycle round to find next valid layout
61 if (i >= argc) {
62 i = 1;
63 }
64 parsed = parse_layout_string(argv[i]);
65 if (parsed != L_NONE) {
66 return parsed;
67 }
68 if (strcmp(argv[i], "split") == 0) {
69 return layout == L_HORIZ ? L_VERT :
70 layout == L_VERT ? L_HORIZ : prev_split_layout;
71 }
72 // invalid layout strings are silently ignored
73 }
74 return L_NONE;
75}
76
77static enum sway_container_layout get_layout(int argc, char **argv,
78 enum sway_container_layout layout,
79 enum sway_container_layout prev_split_layout) {
80 // Check if assigned directly
81 enum sway_container_layout parsed = parse_layout_string(argv[0]);
82 if (parsed != L_NONE) {
83 return parsed;
84 }
85
86 if (strcasecmp(argv[0], "default") == 0) {
87 return prev_split_layout;
88 }
89
90 if (strcasecmp(argv[0], "toggle") == 0) {
91 argc--; argv++;
92 return get_layout_toggle(argc, argv, layout, prev_split_layout);
93 }
94
95 return L_NONE;
96}
97
29struct cmd_results *cmd_layout(int argc, char **argv) { 98struct cmd_results *cmd_layout(int argc, char **argv) {
30 struct cmd_results *error = NULL; 99 struct cmd_results *error = NULL;
31 if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { 100 if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) {
32 return error; 101 return error;
33 } 102 }
34 struct sway_container *parent = config->handler_context.current_container; 103 struct sway_container *container = config->handler_context.container;
104 struct sway_workspace *workspace = config->handler_context.workspace;
35 105
36 if (container_is_floating(parent)) { 106 if (container && container_is_floating(container)) {
37 return cmd_results_new(CMD_FAILURE, "layout", 107 return cmd_results_new(CMD_FAILURE, "layout",
38 "Unable to change layout of floating windows"); 108 "Unable to change layout of floating windows");
39 } 109 }
40 110
41 while (parent->type == C_VIEW) { 111 // Typically we change the layout of the current container, but if the
42 parent = parent->parent; 112 // current container is a view (it usually is) then we'll change the layout
113 // of the parent instead, as it doesn't make sense for views to have layout.
114 if (container && container->view) {
115 container = container->parent;
43 } 116 }
44 117
45 enum sway_container_layout prev = parent->layout; 118 // We could be working with a container OR a workspace. These are different
46 bool assigned_directly = parse_layout_string(argv[0], &parent->layout); 119 // structures, so we set up pointers to they layouts so we can refer them in
47 if (!assigned_directly) { 120 // an abstract way.
48 if (strcasecmp(argv[0], "default") == 0) { 121 enum sway_container_layout new_layout = L_NONE;
49 parent->layout = parent->prev_split_layout; 122 enum sway_container_layout old_layout = L_NONE;
50 } else if (strcasecmp(argv[0], "toggle") == 0) { 123 if (container) {
51 if (argc == 1) { 124 old_layout = container->layout;
52 parent->layout = 125 new_layout = get_layout(argc, argv,
53 parent->layout == L_STACKED ? L_TABBED : 126 container->layout, container->prev_split_layout);
54 parent->layout == L_TABBED ? parent->prev_split_layout : L_STACKED; 127 } else {
55 } else if (argc == 2) { 128 old_layout = workspace->layout;
56 if (strcasecmp(argv[1], "all") == 0) { 129 new_layout = get_layout(argc, argv,
57 parent->layout = 130 workspace->layout, workspace->prev_split_layout);
58 parent->layout == L_HORIZ ? L_VERT :
59 parent->layout == L_VERT ? L_STACKED :
60 parent->layout == L_STACKED ? L_TABBED : L_HORIZ;
61 } else if (strcasecmp(argv[1], "split") == 0) {
62 parent->layout =
63 parent->layout == L_HORIZ ? L_VERT :
64 parent->layout == L_VERT ? L_HORIZ : parent->prev_split_layout;
65 } else {
66 return cmd_results_new(CMD_INVALID, "layout", expected_syntax);
67 }
68 } else {
69 enum sway_container_layout parsed_layout;
70 int curr = 1;
71 for (; curr < argc; curr++) {
72 bool valid = parse_layout_string(argv[curr], &parsed_layout);
73 if ((valid && parsed_layout == parent->layout) ||
74 (strcmp(argv[curr], "split") == 0 &&
75 (parent->layout == L_VERT || parent->layout == L_HORIZ))) {
76 break;
77 }
78 }
79 for (int i = curr + 1; i != curr; ++i) {
80 // cycle round to find next valid layout
81 if (i >= argc) {
82 i = 1;
83 }
84 if (parse_layout_string(argv[i], &parent->layout)) {
85 break;
86 } else if (strcmp(argv[i], "split") == 0) {
87 parent->layout =
88 parent->layout == L_HORIZ ? L_VERT :
89 parent->layout == L_VERT ? L_HORIZ : parent->prev_split_layout;
90 break;
91 } // invalid layout strings are silently ignored
92 }
93 }
94 } else {
95 return cmd_results_new(CMD_INVALID, "layout", expected_syntax);
96 }
97 } 131 }
98 if (parent->layout == L_NONE) { 132 if (new_layout == L_NONE) {
99 parent->layout = container_get_default_layout(parent); 133 return cmd_results_new(CMD_INVALID, "layout", expected_syntax);
100 } 134 }
101 if (prev != parent->layout) { 135 if (new_layout != old_layout) {
102 if (prev != L_TABBED && prev != L_STACKED) { 136 if (container) {
103 parent->prev_split_layout = prev; 137 if (old_layout != L_TABBED && old_layout != L_STACKED) {
138 container->prev_split_layout = old_layout;
139 }
140 container->layout = new_layout;
141 container_update_representation(container);
142 arrange_container(container);
143 } else {
144 if (old_layout != L_TABBED && old_layout != L_STACKED) {
145 workspace->prev_split_layout = old_layout;
146 }
147 workspace->layout = new_layout;
148 workspace_update_representation(workspace);
149 arrange_workspace(workspace);
104 } 150 }
105 container_notify_subtree_changed(parent);
106 arrange_windows(parent->parent);
107 } 151 }
108 152
109 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 153 return cmd_results_new(CMD_SUCCESS, NULL, NULL);