diff options
Diffstat (limited to 'sway/tree/node.c')
-rw-r--r-- | sway/tree/node.c | 151 |
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 | |||
10 | void 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 | |||
18 | const 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 | |||
32 | void 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 | |||
40 | bool node_is_view(struct sway_node *node) { | ||
41 | return node->type == N_CONTAINER && node->sway_container->view; | ||
42 | } | ||
43 | |||
44 | char *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 | |||
58 | void 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 | |||
75 | struct 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 | |||
89 | enum 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 | |||
102 | struct 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 | |||
129 | list_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 | |||
142 | bool 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 | } | ||