summaryrefslogtreecommitdiffstats
path: root/sway/tree/container.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/tree/container.c')
-rw-r--r--sway/tree/container.c182
1 files changed, 91 insertions, 91 deletions
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 805d5644..d31966b3 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -33,48 +33,15 @@ static list_t *get_bfs_queue() {
33 return bfs_queue; 33 return bfs_queue;
34} 34}
35 35
36static void notify_new_container(swayc_t *container) { 36static void notify_new_container(struct sway_container *container) {
37 wl_signal_emit(&root_container.sway_root->events.new_container, container); 37 wl_signal_emit(&root_container.sway_root->events.new_container, container);
38 ipc_event_window(container, "new"); 38 ipc_event_window(container, "new");
39} 39}
40 40
41swayc_t *swayc_by_test(swayc_t *container, 41static struct sway_container *new_swayc(enum sway_container_type type) {
42 bool (*test)(swayc_t *view, void *data), void *data) {
43 if (!container->children) {
44 return NULL;
45 }
46 // TODO: floating windows
47 for (int i = 0; i < container->children->length; ++i) {
48 swayc_t *child = container->children->items[i];
49 if (test(child, data)) {
50 return child;
51 } else {
52 swayc_t *res = swayc_by_test(child, test, data);
53 if (res) {
54 return res;
55 }
56 }
57 }
58 return NULL;
59}
60
61void swayc_descendants_of_type(swayc_t *root, enum swayc_types type,
62 void (*func)(swayc_t *item, void *data), void *data) {
63 for (int i = 0; i < root->children->length; ++i) {
64 swayc_t *item = root->children->items[i];
65 if (item->type == type) {
66 func(item, data);
67 }
68 if (item->children && item->children->length) {
69 swayc_descendants_of_type(item, type, func, data);
70 }
71 }
72}
73
74static swayc_t *new_swayc(enum swayc_types type) {
75 // next id starts at 1 because 0 is assigned to root_container in layout.c 42 // next id starts at 1 because 0 is assigned to root_container in layout.c
76 static size_t next_id = 1; 43 static size_t next_id = 1;
77 swayc_t *c = calloc(1, sizeof(swayc_t)); 44 struct sway_container *c = calloc(1, sizeof(struct sway_container));
78 if (!c) { 45 if (!c) {
79 return NULL; 46 return NULL;
80 } 47 }
@@ -91,7 +58,7 @@ static swayc_t *new_swayc(enum swayc_types type) {
91 return c; 58 return c;
92} 59}
93 60
94static void free_swayc(swayc_t *cont) { 61static void free_swayc(struct sway_container *cont) {
95 if (!sway_assert(cont, "free_swayc passed NULL")) { 62 if (!sway_assert(cont, "free_swayc passed NULL")) {
96 return; 63 return;
97 } 64 }
@@ -119,7 +86,7 @@ static void free_swayc(swayc_t *cont) {
119 free(cont); 86 free(cont);
120} 87}
121 88
122swayc_t *new_output(struct sway_output *sway_output) { 89struct sway_container *sway_container_output_create(struct sway_output *sway_output) {
123 struct wlr_box size; 90 struct wlr_box size;
124 wlr_output_effective_resolution(sway_output->wlr_output, &size.width, 91 wlr_output_effective_resolution(sway_output->wlr_output, &size.width,
125 &size.height); 92 &size.height);
@@ -154,7 +121,7 @@ swayc_t *new_output(struct sway_output *sway_output) {
154 return NULL; 121 return NULL;
155 } 122 }
156 123
157 swayc_t *output = new_swayc(C_OUTPUT); 124 struct sway_container *output = new_swayc(C_OUTPUT);
158 output->sway_output = sway_output; 125 output->sway_output = sway_output;
159 output->name = strdup(name); 126 output->name = strdup(name);
160 if (output->name == NULL) { 127 if (output->name == NULL) {
@@ -169,7 +136,7 @@ swayc_t *new_output(struct sway_output *sway_output) {
169 // Create workspace 136 // Create workspace
170 char *ws_name = workspace_next_name(output->name); 137 char *ws_name = workspace_next_name(output->name);
171 wlr_log(L_DEBUG, "Creating default workspace %s", ws_name); 138 wlr_log(L_DEBUG, "Creating default workspace %s", ws_name);
172 swayc_t *ws = new_workspace(output, ws_name); 139 struct sway_container *ws = sway_container_workspace_create(output, ws_name);
173 // Set each seat's focus if not already set 140 // Set each seat's focus if not already set
174 struct sway_seat *seat = NULL; 141 struct sway_seat *seat = NULL;
175 wl_list_for_each(seat, &input_manager->seats, link) { 142 wl_list_for_each(seat, &input_manager->seats, link) {
@@ -183,12 +150,12 @@ swayc_t *new_output(struct sway_output *sway_output) {
183 return output; 150 return output;
184} 151}
185 152
186swayc_t *new_workspace(swayc_t *output, const char *name) { 153struct sway_container *sway_container_workspace_create(struct sway_container *output, const char *name) {
187 if (!sway_assert(output, "new_workspace called with null output")) { 154 if (!sway_assert(output, "sway_container_workspace_create called with null output")) {
188 return NULL; 155 return NULL;
189 } 156 }
190 wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name); 157 wlr_log(L_DEBUG, "Added workspace %s for output %s", name, output->name);
191 swayc_t *workspace = new_swayc(C_WORKSPACE); 158 struct sway_container *workspace = new_swayc(C_WORKSPACE);
192 159
193 workspace->x = output->x; 160 workspace->x = output->x;
194 workspace->y = output->y; 161 workspace->y = output->y;
@@ -205,12 +172,12 @@ swayc_t *new_workspace(swayc_t *output, const char *name) {
205 return workspace; 172 return workspace;
206} 173}
207 174
208swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) { 175struct sway_container *sway_container_view_create(struct sway_container *sibling, struct sway_view *sway_view) {
209 if (!sway_assert(sibling, "new_view called with NULL sibling/parent")) { 176 if (!sway_assert(sibling, "sway_container_view_create called with NULL sibling/parent")) {
210 return NULL; 177 return NULL;
211 } 178 }
212 const char *title = view_get_title(sway_view); 179 const char *title = view_get_title(sway_view);
213 swayc_t *swayc = new_swayc(C_VIEW); 180 struct sway_container *swayc = new_swayc(C_VIEW);
214 wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d %s", 181 wlr_log(L_DEBUG, "Adding new view %p:%s to container %p %d %s",
215 swayc, title, sibling, sibling ? sibling->type : 0, sibling->name); 182 swayc, title, sibling, sibling ? sibling->type : 0, sibling->name);
216 // Setup values 183 // Setup values
@@ -230,8 +197,8 @@ swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view) {
230 return swayc; 197 return swayc;
231} 198}
232 199
233swayc_t *destroy_output(swayc_t *output) { 200struct sway_container *sway_container_output_destroy(struct sway_container *output) {
234 if (!sway_assert(output, "null output passed to destroy_output")) { 201 if (!sway_assert(output, "null output passed to sway_container_output_destroy")) {
235 return NULL; 202 return NULL;
236 } 203 }
237 204
@@ -243,7 +210,7 @@ swayc_t *destroy_output(swayc_t *output) {
243 int p = root_container.children->items[0] == output; 210 int p = root_container.children->items[0] == output;
244 // Move workspace from this output to another output 211 // Move workspace from this output to another output
245 while (output->children->length) { 212 while (output->children->length) {
246 swayc_t *child = output->children->items[0]; 213 struct sway_container *child = output->children->items[0];
247 remove_child(child); 214 remove_child(child);
248 add_child(root_container.children->items[p], child); 215 add_child(root_container.children->items[p], child);
249 } 216 }
@@ -262,12 +229,12 @@ swayc_t *destroy_output(swayc_t *output) {
262 return &root_container; 229 return &root_container;
263} 230}
264 231
265swayc_t *destroy_view(swayc_t *view) { 232struct sway_container *sway_container_view_destroy(struct sway_container *view) {
266 if (!view) { 233 if (!view) {
267 return NULL; 234 return NULL;
268 } 235 }
269 wlr_log(L_DEBUG, "Destroying view '%s'", view->name); 236 wlr_log(L_DEBUG, "Destroying view '%s'", view->name);
270 swayc_t *parent = view->parent; 237 struct sway_container *parent = view->parent;
271 free_swayc(view); 238 free_swayc(view);
272 239
273 // TODO WLR: Destroy empty containers 240 // TODO WLR: Destroy empty containers
@@ -279,7 +246,52 @@ swayc_t *destroy_view(swayc_t *view) {
279 return parent; 246 return parent;
280} 247}
281 248
282swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { 249struct sway_container *swayc_change_layout(struct sway_container *container, enum sway_container_layout layout) {
250 if (container->type == C_WORKSPACE) {
251 container->workspace_layout = layout;
252 if (layout == L_HORIZ || layout == L_VERT) {
253 container->layout = layout;
254 }
255 } else {
256 container->layout = layout;
257 }
258 return container;
259}
260
261void sway_container_descendents(struct sway_container *root, enum sway_container_type type,
262 void (*func)(struct sway_container *item, void *data), void *data) {
263 for (int i = 0; i < root->children->length; ++i) {
264 struct sway_container *item = root->children->items[i];
265 if (item->type == type) {
266 func(item, data);
267 }
268 if (item->children && item->children->length) {
269 sway_container_descendents(item, type, func, data);
270 }
271 }
272}
273
274struct sway_container *sway_container_find(struct sway_container *container,
275 bool (*test)(struct sway_container *view, void *data), void *data) {
276 if (!container->children) {
277 return NULL;
278 }
279 // TODO: floating windows
280 for (int i = 0; i < container->children->length; ++i) {
281 struct sway_container *child = container->children->items[i];
282 if (test(child, data)) {
283 return child;
284 } else {
285 struct sway_container *res = sway_container_find(child, test, data);
286 if (res) {
287 return res;
288 }
289 }
290 }
291 return NULL;
292}
293
294struct sway_container *sway_container_parent(struct sway_container *container, enum sway_container_type type) {
283 if (!sway_assert(container, "container is NULL")) { 295 if (!sway_assert(container, "container is NULL")) {
284 return NULL; 296 return NULL;
285 } 297 }
@@ -292,7 +304,29 @@ swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) {
292 return container; 304 return container;
293} 305}
294 306
295swayc_t *swayc_at(swayc_t *parent, double lx, double ly, 307void sway_container_for_each(struct sway_container *container, void (*f)(struct sway_container *view, void *data), void *data) {
308 if (container) {
309 int i;
310 if (container->children) {
311 for (i = 0; i < container->children->length; ++i) {
312 struct sway_container *child = container->children->items[i];
313 sway_container_for_each(child, f, data);
314 }
315 }
316 // TODO
317 /*
318 if (container->floating) {
319 for (i = 0; i < container->floating->length; ++i) {
320 struct sway_container *child = container->floating->items[i];
321 container_map(child, f, data);
322 }
323 }
324 */
325 f(container, data);
326 }
327}
328
329struct sway_container *sway_container_at(struct sway_container *parent, double lx, double ly,
296 struct wlr_surface **surface, double *sx, double *sy) { 330 struct wlr_surface **surface, double *sx, double *sy) {
297 list_t *queue = get_bfs_queue(); 331 list_t *queue = get_bfs_queue();
298 if (!queue) { 332 if (!queue) {
@@ -301,13 +335,13 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly,
301 335
302 list_add(queue, parent); 336 list_add(queue, parent);
303 337
304 swayc_t *swayc = NULL; 338 struct sway_container *swayc = NULL;
305 while (queue->length) { 339 while (queue->length) {
306 swayc = queue->items[0]; 340 swayc = queue->items[0];
307 list_del(queue, 0); 341 list_del(queue, 0);
308 if (swayc->type == C_VIEW) { 342 if (swayc->type == C_VIEW) {
309 struct sway_view *sview = swayc->sway_view; 343 struct sway_view *sview = swayc->sway_view;
310 swayc_t *soutput = swayc_parent_by_type(swayc, C_OUTPUT); 344 struct sway_container *soutput = sway_container_parent(swayc, C_OUTPUT);
311 struct wlr_box *output_box = 345 struct wlr_box *output_box =
312 wlr_output_layout_get_box( 346 wlr_output_layout_get_box(
313 root_container.sway_root->output_layout, 347 root_container.sway_root->output_layout,
@@ -377,29 +411,7 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly,
377 return NULL; 411 return NULL;
378} 412}
379 413
380void container_map(swayc_t *container, void (*f)(swayc_t *view, void *data), void *data) { 414void sway_container_for_each_bfs(struct sway_container *con, void (*f)(struct sway_container *con, void *data),
381 if (container) {
382 int i;
383 if (container->children) {
384 for (i = 0; i < container->children->length; ++i) {
385 swayc_t *child = container->children->items[i];
386 container_map(child, f, data);
387 }
388 }
389 // TODO
390 /*
391 if (container->floating) {
392 for (i = 0; i < container->floating->length; ++i) {
393 swayc_t *child = container->floating->items[i];
394 container_map(child, f, data);
395 }
396 }
397 */
398 f(container, data);
399 }
400}
401
402void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data),
403 void *data) { 415 void *data) {
404 list_t *queue = get_bfs_queue(); 416 list_t *queue = get_bfs_queue();
405 if (!queue) { 417 if (!queue) {
@@ -413,7 +425,7 @@ void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data),
413 425
414 list_add(queue, con); 426 list_add(queue, con);
415 427
416 swayc_t *current = NULL; 428 struct sway_container *current = NULL;
417 while (queue->length) { 429 while (queue->length) {
418 current = queue->items[0]; 430 current = queue->items[0];
419 list_del(queue, 0); 431 list_del(queue, 0);
@@ -422,15 +434,3 @@ void container_for_each_bfs(swayc_t *con, void (*f)(swayc_t *con, void *data),
422 list_cat(queue, current->children); 434 list_cat(queue, current->children);
423 } 435 }
424} 436}
425
426swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) {
427 if (container->type == C_WORKSPACE) {
428 container->workspace_layout = layout;
429 if (layout == L_HORIZ || layout == L_VERT) {
430 container->layout = layout;
431 }
432 } else {
433 container->layout = layout;
434 }
435 return container;
436}