aboutsummaryrefslogtreecommitdiff
path: root/sway/tree/output.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/tree/output.c')
-rw-r--r--sway/tree/output.c73
1 files changed, 72 insertions, 1 deletions
diff --git a/sway/tree/output.c b/sway/tree/output.c
index 31e3bf9b..6da63064 100644
--- a/sway/tree/output.c
+++ b/sway/tree/output.c
@@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 200809L
+#include <ctype.h>
#include <string.h>
#include <strings.h>
#include "sway/ipc-server.h"
@@ -28,7 +29,7 @@ static void restore_workspaces(struct sway_container *output) {
}
}
- container_sort_workspaces(output);
+ output_sort_workspaces(output);
}
struct sway_container *output_create(
@@ -102,3 +103,73 @@ struct sway_container *output_create(
return output;
}
+void output_for_each_workspace(struct sway_container *output,
+ void (*f)(struct sway_container *con, void *data), void *data) {
+ if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
+ return;
+ }
+ for (int i = 0; i < output->children->length; ++i) {
+ struct sway_container *workspace = output->children->items[i];
+ f(workspace, data);
+ }
+}
+
+void output_for_each_container(struct sway_container *output,
+ void (*f)(struct sway_container *con, void *data), void *data) {
+ if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
+ return;
+ }
+ for (int i = 0; i < output->children->length; ++i) {
+ struct sway_container *workspace = output->children->items[i];
+ workspace_for_each_container(workspace, f, data);
+ }
+}
+
+struct sway_container *output_find_workspace(struct sway_container *output,
+ bool (*test)(struct sway_container *con, void *data), void *data) {
+ if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
+ return NULL;
+ }
+ for (int i = 0; i < output->children->length; ++i) {
+ struct sway_container *workspace = output->children->items[i];
+ if (test(workspace, data)) {
+ return workspace;
+ }
+ }
+ return NULL;
+}
+
+struct sway_container *output_find_container(struct sway_container *output,
+ bool (*test)(struct sway_container *con, void *data), void *data) {
+ if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
+ return NULL;
+ }
+ struct sway_container *result = NULL;
+ for (int i = 0; i < output->children->length; ++i) {
+ struct sway_container *workspace = output->children->items[i];
+ if ((result = workspace_find_container(workspace, test, data))) {
+ return result;
+ }
+ }
+ return NULL;
+}
+
+static int sort_workspace_cmp_qsort(const void *_a, const void *_b) {
+ struct sway_container *a = *(void **)_a;
+ struct sway_container *b = *(void **)_b;
+
+ if (isdigit(a->name[0]) && isdigit(b->name[0])) {
+ int a_num = strtol(a->name, NULL, 10);
+ int b_num = strtol(b->name, NULL, 10);
+ return (a_num < b_num) ? -1 : (a_num > b_num);
+ } else if (isdigit(a->name[0])) {
+ return -1;
+ } else if (isdigit(b->name[0])) {
+ return 1;
+ }
+ return 0;
+}
+
+void output_sort_workspaces(struct sway_container *output) {
+ list_stable_sort(output->children, sort_workspace_cmp_qsort);
+}