-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_tree.c
461 lines (394 loc) · 10.7 KB
/
window_tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "window_tree.h"
#include <assert.h> // for assert
#include <inttypes.h> // for PRIuPTR
#include <stdbool.h> // for bool
#include <stdint.h> // for uintptr_t, uint16_t
#include <stdio.h> // for NULL, fprintf, snprintf, FILE, fclose, fopen
#include <stdlib.h> // for calloc, free
#ifdef DEBUGMSG
#define PDEBUG(Args...) \
do { fprintf(stderr, "ct: "); fprintf(stderr, ##Args); } while(0)
#define D(x) x
#else
#define PDEBUG(Args...)
#define D(x)
#endif
// container helper functions, local only
static container_t* container_new()
{
container_t *tmp = calloc(1, sizeof(container_t));
return tmp;
}
static container_t* container_new_workspace(xcb_rectangle_t geo)
{
container_t *tmp = container_new();
if (tmp == NULL) return NULL;
tmp->type = CONTAINER_WORKSPACE;
tmp->sgeo = geo;
return tmp;
}
static container_t* container_new_client(client_t *client, bool floating)
{
container_t *tmp = container_new();
if (tmp == NULL) return NULL;
tmp->type = CONTAINER_CLIENT;
tmp->client = client;
tmp->floating = floating;
return tmp;
}
static container_t* container_new_tiling(tiling_t tile)
{
container_t *tmp = container_new();
if (tmp == NULL) return NULL;
tmp->type = CONTAINER_TILING;
tmp->tile = tile;
// tmp->tgeo = geo;
tmp->tiles = 0;
return tmp;
}
static container_t* wtree_data(wtree_t *node)
{
return ((container_t*)(node->data));
}
/***************************************************************/
// memory handling functions
// client constructor
wtree_t* wtree_new_client(client_t *client, bool floating)
{
wtree_t *tmp;
container_t *cont;
if ((cont = container_new_client(client, floating)) == NULL)
return NULL;
tmp = tree_new(NULL, NULL, NULL, NULL, cont);
return tmp;
}
// tiling node constructor
wtree_t* wtree_new_tiling(tiling_t tile)
{
wtree_t *tmp;
container_t *cont;
if ((cont = container_new_tiling(tile)) == NULL)
return NULL;
tmp = tree_new(NULL, NULL, NULL, NULL, cont);
return tmp;
}
// workspace node constructor
wtree_t* wtree_new_workspace(xcb_rectangle_t geo)
{
wtree_t *tmp;
container_t *cont;
if ((cont = container_new_workspace(geo)) == NULL)
return NULL;
tmp = tree_new(NULL, NULL, NULL, NULL, cont);
return tmp;
}
// deconstructor, free node and its data
void wtree_free(wtree_t *node)
{
assert(node != NULL);
assert(node->data != NULL);
free(node->data);
node->data = NULL;
free(node);
}
/*******************************************************/
// container handling functions
// is node a client node
bool wtree_is_client_type(wtree_t *node)
{
assert(node != NULL);
return (wtree_data(node)->type == CONTAINER_CLIENT);
}
// is node a tiling node
bool wtree_is_tiling_type(wtree_t *node)
{
assert(node != NULL);
return (wtree_data(node)->type == CONTAINER_TILING);
}
// is node a workspace node
bool wtree_is_workspace_type(wtree_t *node)
{
assert(node != NULL);
return (wtree_data(node)->type == CONTAINER_WORKSPACE);
}
/* WORKSPACE NODE FUNCTIONS ****************************************/
client_t *wtree_focuswin(wtree_t *node)
{
return wtree_data(node)->focuswin;
}
void wtree_set_focuswin(wtree_t *node, client_t* client)
{
wtree_data(node)->focuswin = client;
}
xcb_rectangle_t wtree_screen_geo(wtree_t *node)
{
return wtree_data(node)->sgeo;
}
void wtree_set_screen_geo(wtree_t *node, xcb_rectangle_t geo)
{
wtree_data(node)->sgeo = geo;
}
/* TILING NODE FUNCTIONS *******************************************/
/* change count of clients in node */
static void wtree_plus(wtree_t *node)
{
++(wtree_data(node)->tiles);
PDEBUG("node+: %p (%d)\n", (void*)node, (wtree_data(node)->tiles));
if (wtree_data(node)->tiles == 1 && ! wtree_is_workspace_type(node->parent)) {
// We now got a tile in here, tell parent
wtree_plus(node->parent);
}
}
static void wtree_minus(wtree_t *node)
{
assert(wtree_data(node)->tiles != 0);
--(wtree_data(node)->tiles);
PDEBUG("node-: %p (%d)\n", (void*)node, (wtree_data(node)->tiles));
if (wtree_data(node)->tiles == 0 && !wtree_is_workspace_type(node->parent)) {
// We now haven't any tiles left, update parent
wtree_minus(node->parent);
}
}
// return number of children
uint16_t wtree_tiles(wtree_t *node)
{
return wtree_data(node)->tiles;
}
// return mode of tiling
tiling_t wtree_tiling(wtree_t *node)
{
assert(node != NULL);
return wtree_data(node)->tile;
}
// set tiling mode
void wtree_set_tiling(wtree_t *node, tiling_t tiling)
{
assert(node != NULL);
wtree_data(node)->tile = tiling;
}
/* CLIENT NODE FUNCTIONS *******************************************/
// return client of node
client_t *wtree_client(wtree_t *node)
{
return wtree_data(node)->client;
}
bool wtree_is_floating(wtree_t *node)
{
return wtree_data(node)->floating;
}
// toggle floating, return new state
bool wtree_toggle_floating(wtree_t *node)
{
bool floats = ! wtree_data(node)->floating;
wtree_data(node)->floating = floats;
// update number of nodes participating in tiling
if (floats)
wtree_minus(node->parent);
else
wtree_plus(node->parent);
return floats;
}
// tiling of parent
tiling_t wtree_parent_tiling(wtree_t *node)
{
assert(node != NULL);
assert(node->parent != NULL);
return wtree_data(node->parent)->tile;
}
void wtree_set_parent_tiling(wtree_t *node, tiling_t tiling)
{
assert(node != NULL);
assert(node->parent != NULL);
wtree_set_tiling(node->parent, tiling);
}
/*************************************************************/
// node action functions
// add _node_ after _current_ node
void wtree_add_sibling(wtree_t *current, wtree_t *node)
{
tree_add(current, node);
if (wtree_is_client_type(node) && ! wtree_data(node)->floating)
wtree_plus(node->parent);
}
// append child node to parent
void wtree_append_child(wtree_t *parent, wtree_t *node)
{
node->parent = parent;
// other vars
if (parent->child == NULL) {
parent->child = node;
if (wtree_is_client_type(node) && ! wtree_data(node)->floating)
wtree_plus(parent);
} else {
wtree_t *sib = parent->child;
while (sib->next) sib = sib->next;
wtree_add_sibling(sib, node);
}
}
// interject tiler between client and client->parent
/* replace current client-node with tiler, add cl-node to that */
void wtree_inter_tile(wtree_t *client, tiling_t mode)
{
wtree_t *tiler = wtree_new_tiling(mode);
// update parent tiles count if needed
// append_child will add it back
if (! wtree_is_floating(client)) {
tree_replace(client, tiler);
wtree_minus(tiler->parent);
} else {
tree_replace(client, tiler);
}
// add client to tiler
wtree_append_child(tiler, client);
}
/* add tiling-node as sibling to current and client-node as child to tiling-node
* current -> tiling-node (tmp) -> client-node (node) */
void wtree_add_tile_sibling(wtree_t *current, wtree_t *node,
tiling_t tiling)
{
wtree_t *tiler = wtree_new_tiling(tiling);
// add tiler as sibling to current
wtree_add_sibling(current, tiler);
// add node to tiler
wtree_append_child(tiler, node);
}
void wtree_append_tile_child(wtree_t *parent, wtree_t *node,
tiling_t tiling)
{
wtree_t *tiler = wtree_new_tiling(tiling);
// add tiler as sibling to current
PDEBUG("--1--\n");
wtree_append_child(parent, tiler);
PDEBUG("--2--\n");
// add node to tiler
wtree_append_child(tiler, node);
PDEBUG("--3--\n");
}
/* unlink node from tree, no children handling */
void wtree_remove(wtree_t *node)
{
assert(node != NULL);
tree_t *parent = tree_parent(node);
// extract node from tree
tree_extract(node);
// update parent node
if (parent && wtree_is_tiling_type(parent)) {
// decrement child counter
if (wtree_is_client_type(node) && ! wtree_data(node)->floating)
wtree_minus(parent);
// in case of non-root-parent tile, remove it
if (tree_child(parent) == NULL) {
wtree_remove(parent);
wtree_free(parent);
}
}
}
wtree_t *wtree_next(wtree_t *node)
{
do {
node = tree_walk_down_right(node);
} while (node != NULL && ! wtree_is_client_type(node));
return node;
}
// recursive general function to apply _action_ on each client beneath client
// *pre-order*
void wtree_traverse_clients(wtree_t *node, void(*action)(client_t *))
{
if (node == NULL)
return;
assert((node->next != node->prev) || node->next == NULL);
if (wtree_is_client_type(node))
action(wtree_client(node));
if (node->next != NULL)
wtree_traverse_clients(node->next, action);
if (node->child != NULL)
wtree_traverse_clients(node->child, action);
}
// recursive function to search for a client_t that fulfils _compare_
/* pre-order */
client_t *wtree_find_client(wtree_t *node, bool(*compare)(client_t*, void *), void *arg)
{
if (node == NULL)
return NULL;
if (wtree_is_client_type(node) && compare(wtree_client(node), arg))
return wtree_client(node);
client_t *client;
if ((client = wtree_find_client(node->next, compare, arg)))
return client;
if ((client = wtree_find_client(node->child, compare, arg)))
return client;
return NULL;
}
/*******************************************************/
// recursive helper function to print tree
static void wtree_print_tree_r(FILE *file, wtree_t *cur, int *i)
{
if (cur == NULL)
return;
char *num = calloc(10, 1);
if (wtree_is_tiling_type(cur)) {
switch (wtree_tiling(cur)) {
case TILING_VERTICAL:
snprintf(num, 10, "V%d", *i);
break;
case TILING_HORIZONTAL:
snprintf(num, 10, "H%d", *i);
break;
default:
snprintf(num, 10, "X%d", *i);
break;
}
fprintf(file, "%"PRIuPTR" [label=\"%s (%d)\" shape=triangle];\n",
(uintptr_t)cur, num, wtree_tiles(cur));
} else if (wtree_is_workspace_type(cur)) {
snprintf(num, 10, "root");
fprintf(file, "%"PRIuPTR" [label=\"%s\" shape=box];\n",
(uintptr_t)cur, num);
} else {
snprintf(num, 10, "%d", *i);
if (wtree_is_floating(cur))
fprintf(file, "%"PRIuPTR" [label=\"%s\" shape=doublecircle];\n",
(uintptr_t)cur, num);
else
fprintf(file, "%"PRIuPTR" [label=\"%s\" shape=circle];\n",
(uintptr_t)cur, num);
}
if (cur->parent) {
fprintf(file, "%"PRIuPTR":n -> %"PRIuPTR":s;\n",
(uintptr_t)cur, (uintptr_t)cur->parent);
}
if (cur->next) {
fprintf(file, "%"PRIuPTR":e -> %"PRIuPTR":w;\n",
(uintptr_t)cur, (uintptr_t)cur->next);
fprintf(file, "{ rank = same; %"PRIuPTR" ; %"PRIuPTR" }\n;",
(uintptr_t)cur, (uintptr_t)cur->next);
}
if (cur->prev) {
fprintf(file, "%"PRIuPTR" -> %"PRIuPTR":e;\n",
(uintptr_t)cur, (uintptr_t)cur->prev);
}
if (cur->child) {
fprintf(file, "%"PRIuPTR":s -> %"PRIuPTR":n;\n",
(uintptr_t)cur, (uintptr_t)cur->child);
}
free(num);
num = NULL;
if (cur->next) {
++(*i);
wtree_print_tree_r(file, cur->next, i);
}
if (cur->child) {
++(*i);
wtree_print_tree_r(file, cur->child, i);
}
}
// print tree
void wtree_print_tree(wtree_t *cur)
{
FILE *file = fopen("/tmp/graph.dot", "w");
int i = 0;
fprintf(file, "digraph G {\nnodesep=1.2;\n");
wtree_print_tree_r(file, cur, &i);
fprintf(file, "}\n");
fclose(file);
}