aboutsummaryrefslogtreecommitdiffstats
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 @@
1#define _POSIX_C_SOURCE 200809L 1#define _POSIX_C_SOURCE 200809L
2#include <ctype.h>
2#include <string.h> 3#include <string.h>
3#include <strings.h> 4#include <strings.h>
4#include "sway/ipc-server.h" 5#include "sway/ipc-server.h"
@@ -28,7 +29,7 @@ static void restore_workspaces(struct sway_container *output) {
28 } 29 }
29 } 30 }
30 31
31 container_sort_workspaces(output); 32 output_sort_workspaces(output);
32} 33}
33 34
34struct sway_container *output_create( 35struct sway_container *output_create(
@@ -102,3 +103,73 @@ struct sway_container *output_create(
102 return output; 103 return output;
103} 104}
104 105
106void output_for_each_workspace(struct sway_container *output,
107 void (*f)(struct sway_container *con, void *data), void *data) {
108 if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
109 return;
110 }
111 for (int i = 0; i < output->children->length; ++i) {
112 struct sway_container *workspace = output->children->items[i];
113 f(workspace, data);
114 }
115}
116
117void output_for_each_container(struct sway_container *output,
118 void (*f)(struct sway_container *con, void *data), void *data) {
119 if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
120 return;
121 }
122 for (int i = 0; i < output->children->length; ++i) {
123 struct sway_container *workspace = output->children->items[i];
124 workspace_for_each_container(workspace, f, data);
125 }
126}
127
128struct sway_container *output_find_workspace(struct sway_container *output,
129 bool (*test)(struct sway_container *con, void *data), void *data) {
130 if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
131 return NULL;
132 }
133 for (int i = 0; i < output->children->length; ++i) {
134 struct sway_container *workspace = output->children->items[i];
135 if (test(workspace, data)) {
136 return workspace;
137 }
138 }
139 return NULL;
140}
141
142struct sway_container *output_find_container(struct sway_container *output,
143 bool (*test)(struct sway_container *con, void *data), void *data) {
144 if (!sway_assert(output->type == C_OUTPUT, "Expected an output")) {
145 return NULL;
146 }
147 struct sway_container *result = NULL;
148 for (int i = 0; i < output->children->length; ++i) {
149 struct sway_container *workspace = output->children->items[i];
150 if ((result = workspace_find_container(workspace, test, data))) {
151 return result;
152 }
153 }
154 return NULL;
155}
156
157static int sort_workspace_cmp_qsort(const void *_a, const void *_b) {
158 struct sway_container *a = *(void **)_a;
159 struct sway_container *b = *(void **)_b;
160
161 if (isdigit(a->name[0]) && isdigit(b->name[0])) {
162 int a_num = strtol(a->name, NULL, 10);
163 int b_num = strtol(b->name, NULL, 10);
164 return (a_num < b_num) ? -1 : (a_num > b_num);
165 } else if (isdigit(a->name[0])) {
166 return -1;
167 } else if (isdigit(b->name[0])) {
168 return 1;
169 }
170 return 0;
171}
172
173void output_sort_workspaces(struct sway_container *output) {
174 list_stable_sort(output->children, sort_workspace_cmp_qsort);
175}