aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/node.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/tree/node.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/tree/node.c')
-rw-r--r--sway/tree/node.c151
1 files changed, 151 insertions, 0 deletions
diff --git a/sway/tree/node.c b/sway/tree/node.c
new file mode 100644
index 00000000..74661c1a
--- /dev/null
+++ b/sway/tree/node.c
@@ -0,0 +1,151 @@
1#define _POSIX_C_SOURCE 200809L
2#include "sway/output.h"
3#include "sway/server.h"
4#include "sway/tree/container.h"
5#include "sway/tree/node.h"
6#include "sway/tree/root.h"
7#include "sway/tree/workspace.h"
8#include "log.h"
9
10void node_init(struct sway_node *node, enum sway_node_type type, void *thing) {
11 static size_t next_id = 1;
12 node->id = next_id++;
13 node->type = type;
14 node->sway_root = thing;
15 wl_signal_init(&node->events.destroy);
16}
17
18const char *node_type_to_str(enum sway_node_type type) {
19 switch (type) {
20 case N_ROOT:
21 return "N_ROOT";
22 case N_OUTPUT:
23 return "N_OUTPUT";
24 case N_WORKSPACE:
25 return "N_WORKSPACE";
26 case N_CONTAINER:
27 return "N_CONTAINER";
28 }
29 return "";
30}
31
32void node_set_dirty(struct sway_node *node) {
33 if (node->dirty) {
34 return;
35 }
36 node->dirty = true;
37 list_add(server.dirty_nodes, node);
38}
39
40bool node_is_view(struct sway_node *node) {
41 return node->type == N_CONTAINER && node->sway_container->view;
42}
43
44char *node_get_name(struct sway_node *node) {
45 switch (node->type) {
46 case N_ROOT:
47 return "root";
48 case N_OUTPUT:
49 return node->sway_output->wlr_output->name;
50 case N_WORKSPACE:
51 return node->sway_workspace->name;
52 case N_CONTAINER:
53 return node->sway_container->title;
54 }
55 return NULL;
56}
57
58void node_get_box(struct sway_node *node, struct wlr_box *box) {
59 switch (node->type) {
60 case N_ROOT:
61 root_get_box(root, box);
62 break;
63 case N_OUTPUT:
64 output_get_box(node->sway_output, box);
65 break;
66 case N_WORKSPACE:
67 workspace_get_box(node->sway_workspace, box);
68 break;
69 case N_CONTAINER:
70 container_get_box(node->sway_container, box);
71 break;
72 }
73}
74
75struct sway_output *node_get_output(struct sway_node *node) {
76 switch (node->type) {
77 case N_CONTAINER:
78 return node->sway_container->workspace->output;
79 case N_WORKSPACE:
80 return node->sway_workspace->output;
81 case N_OUTPUT:
82 return node->sway_output;
83 case N_ROOT:
84 return NULL;
85 }
86 return NULL;
87}
88
89enum sway_container_layout node_get_layout(struct sway_node *node) {
90 switch (node->type) {
91 case N_CONTAINER:
92 return node->sway_container->layout;
93 case N_WORKSPACE:
94 return node->sway_workspace->layout;
95 case N_OUTPUT:
96 case N_ROOT:
97 return L_NONE;
98 }
99 return L_NONE;
100}
101
102struct sway_node *node_get_parent(struct sway_node *node) {
103 switch (node->type) {
104 case N_CONTAINER: {
105 struct sway_container *con = node->sway_container;
106 if (con->parent) {
107 return &con->parent->node;
108 }
109 if (con->workspace) {
110 return &con->workspace->node;
111 }
112 }
113 return NULL;
114 case N_WORKSPACE: {
115 struct sway_workspace *ws = node->sway_workspace;
116 if (ws->output) {
117 return &ws->output->node;
118 }
119 }
120 return NULL;
121 case N_OUTPUT:
122 return &root->node;
123 case N_ROOT:
124 return NULL;
125 }
126 return NULL;
127}
128
129list_t *node_get_children(struct sway_node *node) {
130 switch (node->type) {
131 case N_CONTAINER:
132 return node->sway_container->children;
133 case N_WORKSPACE:
134 return node->sway_workspace->tiling;
135 case N_OUTPUT:
136 case N_ROOT:
137 return NULL;
138 }
139 return NULL;
140}
141
142bool node_has_ancestor(struct sway_node *node, struct sway_node *ancestor) {
143 struct sway_node *parent = node_get_parent(node);
144 while (parent) {
145 if (parent == ancestor) {
146 return true;
147 }
148 parent = node_get_parent(parent);
149 }
150 return false;
151}