aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/container.c
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-08-17 19:48:34 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-08-18 23:38:54 +1000
commitd6cd79c342495738fc23fbfbf19a01e73cdc42dc (patch)
tree7a5ebeae1d5e15f047f09698978fa84f61756faa /sway/tree/container.c
parentMerge pull request #2460 from RyanDwyer/implement-mousedown (diff)
downloadsway-d6cd79c342495738fc23fbfbf19a01e73cdc42dc.tar.gz
sway-d6cd79c342495738fc23fbfbf19a01e73cdc42dc.tar.zst
sway-d6cd79c342495738fc23fbfbf19a01e73cdc42dc.zip
Implement iterators per container type
This introduces the following `for_each` functions: * root_for_each_workspace * root_for_each_container * output_for_each_workspace * output_for_each_container * workspace_for_each_container And introduces the following `find` functions: * root_find_output * root_find_workspace * root_find_container * output_find_workspace * output_find_container * workspace_find_container * container_find_child And removes the following functions: * container_descendants * container_for_each_descendant * container_find This change is preparing the way for demoting sway_container. Eventually these functions will accept and return sway_outputs, sway_workspaces and sway_containers (meaning a C_CONTAINER or C_VIEW). This change also makes it easy to handle abnormalities like the workspace floating list, root's scratchpad list and (once implemented) root's saved workspaces list for when there's no connected outputs.
Diffstat (limited to 'sway/tree/container.c')
-rw-r--r--sway/tree/container.c62
1 files changed, 21 insertions, 41 deletions
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 337245fd..1ceae175 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -432,8 +432,10 @@ struct sway_container *container_close(struct sway_container *con) {
432 432
433 if (con->type == C_VIEW) { 433 if (con->type == C_VIEW) {
434 view_close(con->sway_view); 434 view_close(con->sway_view);
435 } else { 435 } else if (con->type == C_CONTAINER) {
436 container_for_each_descendant(con, container_close_func, NULL); 436 container_for_each_child(con, container_close_func, NULL);
437 } else if (con->type == C_WORKSPACE) {
438 workspace_for_each_container(con, container_close_func, NULL);
437 } 439 }
438 440
439 return parent; 441 return parent;
@@ -465,23 +467,12 @@ struct sway_container *container_view_create(struct sway_container *sibling,
465 return swayc; 467 return swayc;
466} 468}
467 469
468void container_descendants(struct sway_container *root, 470struct sway_container *container_find_child(struct sway_container *container,
469 enum sway_container_type type,
470 void (*func)(struct sway_container *item, void *data), void *data) {
471 if (!root->children || !root->children->length) {
472 return;
473 }
474 for (int i = 0; i < root->children->length; ++i) {
475 struct sway_container *item = root->children->items[i];
476 if (item->type == type) {
477 func(item, data);
478 }
479 container_descendants(item, type, func, data);
480 }
481}
482
483struct sway_container *container_find(struct sway_container *container,
484 bool (*test)(struct sway_container *view, void *data), void *data) { 471 bool (*test)(struct sway_container *view, void *data), void *data) {
472 if (!sway_assert(container->type == C_CONTAINER ||
473 container->type == C_VIEW, "Expected a container or view")) {
474 return NULL;
475 }
485 if (!container->children) { 476 if (!container->children) {
486 return NULL; 477 return NULL;
487 } 478 }
@@ -489,15 +480,11 @@ struct sway_container *container_find(struct sway_container *container,
489 struct sway_container *child = container->children->items[i]; 480 struct sway_container *child = container->children->items[i];
490 if (test(child, data)) { 481 if (test(child, data)) {
491 return child; 482 return child;
492 } else {
493 struct sway_container *res = container_find(child, test, data);
494 if (res) {
495 return res;
496 }
497 } 483 }
498 } 484 struct sway_container *res = container_find_child(child, test, data);
499 if (container->type == C_WORKSPACE) { 485 if (res) {
500 return container_find(container->sway_workspace->floating, test, data); 486 return res;
487 }
501 } 488 }
502 return NULL; 489 return NULL;
503} 490}
@@ -743,26 +730,20 @@ struct sway_container *container_at(struct sway_container *workspace,
743 return NULL; 730 return NULL;
744} 731}
745 732
746void container_for_each_descendant(struct sway_container *container, 733void container_for_each_child(struct sway_container *container,
747 void (*f)(struct sway_container *container, void *data), 734 void (*f)(struct sway_container *container, void *data),
748 void *data) { 735 void *data) {
749 if (!container) { 736 if (!sway_assert(container->type == C_CONTAINER ||
737 container->type == C_VIEW, "Expected a container or view")) {
750 return; 738 return;
751 } 739 }
740 f(container, data);
752 if (container->children) { 741 if (container->children) {
753 for (int i = 0; i < container->children->length; ++i) { 742 for (int i = 0; i < container->children->length; ++i) {
754 struct sway_container *child = container->children->items[i]; 743 struct sway_container *child = container->children->items[i];
755 container_for_each_descendant(child, f, data); 744 container_for_each_child(child, f, data);
756 } 745 }
757 } 746 }
758 if (container->type == C_WORKSPACE) {
759 struct sway_container *floating = container->sway_workspace->floating;
760 for (int i = 0; i < floating->children->length; ++i) {
761 struct sway_container *child = floating->children->items[i];
762 container_for_each_descendant(child, f, data);
763 }
764 }
765 f(container, data);
766} 747}
767 748
768bool container_has_ancestor(struct sway_container *descendant, 749bool container_has_ancestor(struct sway_container *descendant,
@@ -1198,13 +1179,12 @@ void container_set_dirty(struct sway_container *container) {
1198 list_add(server.dirty_containers, container); 1179 list_add(server.dirty_containers, container);
1199} 1180}
1200 1181
1201static bool find_urgent_iterator(struct sway_container *con, 1182static bool find_urgent_iterator(struct sway_container *con, void *data) {
1202 void *data) {
1203 return con->type == C_VIEW && view_is_urgent(con->sway_view); 1183 return con->type == C_VIEW && view_is_urgent(con->sway_view);
1204} 1184}
1205 1185
1206bool container_has_urgent_child(struct sway_container *container) { 1186bool container_has_urgent_child(struct sway_container *container) {
1207 return container_find(container, find_urgent_iterator, NULL); 1187 return container_find_child(container, find_urgent_iterator, NULL);
1208} 1188}
1209 1189
1210void container_end_mouse_operation(struct sway_container *container) { 1190void container_end_mouse_operation(struct sway_container *container) {
@@ -1236,7 +1216,7 @@ void container_set_fullscreen(struct sway_container *container, bool enable) {
1236 container_set_fullscreen(workspace->sway_workspace->fullscreen, false); 1216 container_set_fullscreen(workspace->sway_workspace->fullscreen, false);
1237 } 1217 }
1238 1218
1239 container_for_each_descendant(container, set_fullscreen_iterator, &enable); 1219 container_for_each_child(container, set_fullscreen_iterator, &enable);
1240 1220
1241 container->is_fullscreen = enable; 1221 container->is_fullscreen = enable;
1242 1222