-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.c
executable file
·633 lines (545 loc) · 15.3 KB
/
funcs.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <string.h>
#include <stdbool.h>
#include <xcb/xcb.h>
#include "types.h"
#include "funcs.h"
#include "config.h"
int occurences(const char *str, int c)
{
int count = 0;
for (char *p; p = strchr(str, c); count++, str = ++p);
return count;
}
char **split_string(const char *str)
{
int i = 0;
int words = 1 + occurences(str, ' ');
char **result = malloc((1 + words) * sizeof *result);
do
{
const char *next = strchr(str, ' ');
next = next ? next : str + strlen(str);
int len = next - str;
result[i] = malloc((len + 1) * sizeof(char));
strncpy(result[i], str, len);
result[i][len] = '\0';
str += len + 1;
} while (++i < words);
result[i] = NULL;
return result;
}
int random_color()
{
unsigned int max = 0xffffff;
return rand() % max;
}
unsigned int *select_len(node_t *node, layout_t layout)
{
return layout == HORIZONTAL ? &(node->w) : &(node->h);
}
int *select_coord(node_t *node, layout_t layout)
{
return layout == HORIZONTAL ? &(node->x) : &(node->y);
}
int count_children(node_t *node)
{
int count = 0;
for (node_t *p = node->children; p; p = p->next, count++);
return count;
}
int len_of_children(node_t *node)
{
int count = 0;
layout_t layout = node->layout;
for (node_t *p = node->children; p; p = p->next)
{
count += *select_len(p, layout);
}
return count;
}
bool isroot(node_t *node)
{
return node->parent == NULL;
}
bool iscontainer(node_t *node)
{
return node->children != NULL;
}
bool isfirst(node_t *node)
{
return node->prev == NULL;
}
bool islast(node_t *node)
{
return node->next == NULL;
}
bool isonlychild(node_t *node)
{
return isfirst(node) && islast(node);
}
void reverse_children(node_t *container)
{
node_t *cur = get_last(container);
if (isfirst(cur)) return;
container->children = cur;
for (node_t *p; cur; cur = p)
{
p = cur->prev;
cur->prev = cur->next;
cur->next = p;
}
}
// Computes square of the distance between two points
int dist(int x1, int y1, int x2, int y2)
{
int x = x1-x2;
int y = y1-y2;
return x*x + y*y;
}
void get_coordinates(node_t *node, int *x, int *y)
{
if (!node || !node->parent) return;
*x += node->x;
*y += node->y;
get_coordinates(node->parent, x, y);
}
node_t *get_next_container(node_t *node, layout_t layout, int direction)
{
if (isroot(node)) return NULL;
node_t *next;
if (direction == -1) next = node->prev;
else if (direction == 1) next = node->next;
else return NULL;
if (node->parent->layout == layout && next) return next;
if (!isroot(node->parent)) return get_next_container(node->parent, layout, direction);
return NULL;
}
node_t *get_innermost_client(node_t *container, int x, int y)
{
if (!container) return NULL;
if (!iscontainer(container)) return container;
// x0 and y0 are container's coordinates relative to the master window's origin
int x0 = 0;
int y0 = 0;
get_coordinates(container, &x0, &y0);
// Find container's child nearest to (x,y)
node_t *p = container->children;
int diff = dist(x, y, x0 + p->x, y0 + p->y);
for (; p->next && diff > dist(x, y, x0 + p->next->x, y0 + p->next->y); p = p->next)
diff = dist(x, y, x0 + p->next->x, y0 + p->next->y);
return get_innermost_client(p, x, y);
}
node_t *get_adjacent_node(node_t *node, bool prefer_containers)
{
if (!node) return NULL;
if (node->next && prefer_containers && iscontainer(node->next)) return node->next;
if (node->prev && prefer_containers && iscontainer(node->prev)) return node->prev;
if (node->next && !prefer_containers && !iscontainer(node->next)) return node->next;
if (node->prev && !prefer_containers && !iscontainer(node->prev)) return node->prev;
if (node->next) return node->next;
if (node->prev) return node->prev;
return get_adjacent_node(node->parent, prefer_containers);
}
node_t *get_closest(node_t *node)
{
int x = 0;
int y = 0;
get_coordinates(node, &x, &y);
return get_innermost_client(get_adjacent_node(node, false), x, y);
}
node_t *get_right(node_t *node)
{
int x = 0;
int y = 0;
get_coordinates(node, &x, &y);
node_t *next = get_next_container(node, HORIZONTAL, 1);
return get_innermost_client(next, x, y);
}
node_t *get_left(node_t *node)
{
int x = 0;
int y = 0;
get_coordinates(node, &x, &y);
node_t *next = get_next_container(node, HORIZONTAL, -1);
return get_innermost_client(next, x, y);
}
node_t *get_top(node_t *node)
{
int x = 0;
int y = 0;
get_coordinates(node, &x, &y);
node_t *next = get_next_container(node, VERTICAL, -1);
return get_innermost_client(next, x, y);
}
node_t *get_bottom(node_t *node)
{
int x = 0;
int y = 0;
get_coordinates(node, &x, &y);
node_t *next = get_next_container(node, VERTICAL, 1);
return get_innermost_client(next, x, y);
}
node_t *get_parent(node_t *node)
{
if (!node->parent || isonlychild(node)) return NULL;
return node->parent;
}
node_t *get_child(node_t *node)
{
if (!iscontainer(node)) return NULL;
return node->children;
}
node_t *get_last(node_t *container)
{
node_t *p;
for (p = container->children; p && p->next; p = p->next);
return p;
}
node_t *get_by_direction(node_t *node, char *direction)
{
if (strcmp(direction, LEFT) == 0) return get_left(node);
if (strcmp(direction, RIGHT) == 0) return get_right(node);
if (strcmp(direction, UP) == 0) return get_top(node);
if (strcmp(direction, DOWN) == 0) return get_bottom(node);
if (strcmp(direction, PARENT) == 0) return get_parent(node);
if (strcmp(direction, CHILD) == 0) return get_child(node);
return NULL;
}
void swap_windows(node_t *first, node_t *second)
{
xcb_window_t wtmp;
SWAP(wtmp, first->id, second->id);
layout_t ltmp;
SWAP(ltmp, first->layout, second->layout);
node_t *ntmp;
SWAP(ntmp, first->children, second->children);
for (node_t *p = first->children; p; p = p->next)
p->parent = first;
for (node_t *p = second->children; p; p = p->next)
p->parent = second;
}
// Inserts given node next to previous (which is a child of container)
// and makes it container's child. If previous is NULL, node is inserted as a first child
void insert_node(node_t *node, node_t *container, node_t *previous)
{
if (!node) return;
node->parent = container;
if (!container) return;
node->prev = previous;
if (!previous)
{
node->next = container->children;
container->children = node;
}
else
{
node->next = previous->next;
previous->next = node;
}
if (node->next) node->next->prev = node;
}
void rescale_children(xinfo_t *xinfo, node_t *container, node_t *ignored)
{
if (!iscontainer(container)) return;
layout_t layout = container->layout;
int ignored_len = ignored && ignored->parent == container ? *select_len(ignored, layout) : 0;
int new_len = *select_len(container, layout) - ignored_len;
int old_len = len_of_children(container) - ignored_len;
float scale_factor = 1.0 * new_len / old_len;
// Change children's size
int actual_new_len = 0;
int opposite_len = *select_len(container, !layout);
for (node_t *p = container->children; p; p = p->next)
{
if (p != ignored)
{
int *child_len = select_len(p, layout);
actual_new_len += (*child_len = 0.5 + *child_len * scale_factor);
}
*select_len(p, !layout) = opposite_len;
if (p->children) rescale_children(xinfo, p, ignored);
}
// Add a few pixels from truncated decimal parts of child_len to ignored/first node
*select_len(ignored ? ignored : container->children, layout) += new_len - actual_new_len;
// Change children's position and update its window
int pos = 0;
for (node_t *p = container->children; p; p = p->next)
{
*select_coord(p, layout) = pos;
*select_coord(p, !layout) = 0;
pos += *select_len(p, layout);
update_window(xinfo, p);
}
// Must update windows after calling this function
}
void copy_position(node_t *to, node_t *from)
{
to->x = from->x;
to->y = from->y;
}
void copy_size(node_t *to, node_t *from)
{
to->w = from->w;
to->h = from->h;
}
void push_node(xinfo_t *xinfo, node_t *node)
{
// Supplant given node with another - given node's new parent
detach_node(node);
node_t *container = calloc(1, sizeof *container);
copy_position(container, node);
copy_size(container, node);
container->layout = node->layout;
insert_node(container, node->parent, node->prev);
open_window(xinfo, container);
node->parent = container;
node->prev = NULL;
node->next = NULL;
node->x = 0;
node->y = 0;
container->children = node;
}
void push_root(context_t *context, xinfo_t *xinfo, node_t *root)
{
push_node(xinfo, root);
context->root[context->tab] = root->parent;
root->parent->layout = !root->layout;
}
void pull_node(xinfo_t *xinfo, node_t *node)
{
node_t *container = node->parent;
copy_position(node, container);
detach_node(node);
insert_node(node, container->parent, container->prev);
xcb_reparent_window(xinfo->conn, node->id, node->parent->id, node->x, node->y);
}
void pull_nodes(xinfo_t *xinfo, node_t *container)
{
node_t *p = container->children;
for (node_t *n; p; p = n)
{
n = p->next;
pull_node(xinfo, p);
}
delete_node(xinfo, container);
}
void init_node(node_t *node, node_t *container, node_t *next)
{
if (!node || !container) return;
// Insert node in container
layout_t layout = container->layout;
if (!iscontainer(node)) node->layout = layout;
insert_node(node, container, next ? next->prev : NULL);
// Compute node's dimensions
*select_len(node, layout) = *select_len(container, layout) / count_children(container);
*select_len(node, !layout) = *select_len(container, !layout);
// Must call rescale_children on container after this function
}
// Open X simple window in an empty preconfigured node
void open_window(xinfo_t *xinfo, node_t *node)
{
node->id = xcb_generate_id(xinfo->conn);
xcb_window_t parent = node->parent ? node->parent->id : xinfo->window;
unsigned int mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_EVENT_MASK;
unsigned int event_mask = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
unsigned int background = node->parent ? random_color() : 0xffffff; // TODO
int values[] = {background, BORDER_COLOR, event_mask};
xcb_create_window(xinfo->conn, XCB_COPY_FROM_PARENT, node->id, parent, \
node->x, node->y, node->w, node->h, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, \
xinfo->screen->root_visual, mask, values);
xcb_map_window(xinfo->conn, node->id);
}
void update_window(xinfo_t *xinfo, node_t *node)
{
unsigned mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | \
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
int width = node->w;
int height = node->h;
if (node->isactive)
{
width -= 2*BORDER;
height -= 2*BORDER;
}
unsigned values[] = {node->x, node->y, width, height};
if (!isroot(node))
{
xcb_reparent_window(xinfo->conn, node->id, node->parent->id, node->x, node->y);
}
xcb_configure_window(xinfo->conn, node->id, mask, values);
}
void update_root(xinfo_t *xinfo, node_t *root)
{
root->w = xinfo->width;
root->h = xinfo->height;
update_window(xinfo, root);
}
void set_active(context_t *context, xinfo_t *xinfo, node_t *node)
{
int tab = context->tab;
unsigned mask = XCB_CONFIG_WINDOW_WIDTH |\
XCB_CONFIG_WINDOW_HEIGHT |\
XCB_CONFIG_WINDOW_BORDER_WIDTH;
unsigned values[3];
// Make previous active window unactive
if (context->active_client[tab])
{
node_t *old = context->active_client[tab];
old->isactive = false;
values[0] = old->w;
values[1] = old->h;
values[2] = 0;
xcb_configure_window(xinfo->conn, old->id, mask, values);
}
context->active_client[tab] = node;
// Add border to new active and change active container
if (node)
{
node->isactive = true;
context->active_container[tab] = node->parent;
values[0] = node->w - 2*BORDER;
values[1] = node->h - 2*BORDER;
values[2] = BORDER;
xcb_configure_window(xinfo->conn, node->id, mask, values);
}
else
{
context->active_container[tab] = context->root[tab];
fprintf(stderr, NOTE "set_active: active client is set to NULL\n" END);
}
}
// Removes given node from the linked list of its siblings
void detach_node(node_t *node)
{
if (isroot(node)) return;
if (isfirst(node))
{
node->parent->children = node->next;
if (node->next)
node->next->prev = NULL;
}
else
{
node->prev->next = node->next;
if (node->next)
node->next->prev = node->prev;
}
}
// Removes node and destroys its window
void delete_node(xinfo_t *xinfo, node_t *node)
{
detach_node(node);
for (node_t *p = node->children; p; p = p->next)
delete_node(xinfo, p);
if (isroot(node)) return;
xcb_destroy_window(xinfo->conn, node->id);
if (node->pid)
{
kill(node->pid, SIGTERM);
waitpid(node->pid, NULL, 0);
}
free(node);
}
void set_opposite_layout(node_t *node)
{
if (!node) return;
layout_t layout = !node->layout;
node->layout = layout;
if (isonlychild(node) && !isroot(node)) node->parent->layout = layout;
if (iscontainer(node))
{
// Reverse children in every second level of hierarchy to
// achieve effect of rotation. Change layout in condition
// below to change direction of rotation
if (node->layout == HORIZONTAL) reverse_children(node);
// Switch each child's layout
for (node_t *p = node->children; p; p = p->next)
{
int tmp;
SWAP(tmp, p->x, p->y);
SWAP(tmp, p->w, p->h);
set_opposite_layout(p);
}
}
}
// Removes redundancy from the node tree below the provided container
// and returns new container since the provided one may get deleted
node_t *remove_redundant(xinfo_t *xinfo, node_t *container)
{
int children = count_children(container);
node_t *new_container = container;
// If container has at least 2 children, it's not redundant
if (children > 1) return new_container;
// If container is empty and non-root, it should be deleted
// if empty and root, there's nothing to do
if (children == 0)
{
if (!isroot(container))
{
new_container = container->parent;
delete_node(xinfo, container);
}
return new_container;
}
// Remove redundant nested containers
while (children == 1)
{
node_t *child = container->children;
if (!iscontainer(child)) break;
// else child is a redundant intermediate container
// Remove child from the tree
container->children = child->children;
container->layout = child->layout;
for (node_t *p = container->children; p; p = p->next)
{
p->parent = container;
update_window(xinfo, p);
}
// Remove child from screen and memory
xcb_destroy_window(xinfo->conn, child->id);
free(child);
children = count_children(container);
}
if (isroot(container)) return container;
// Merge container with its parent if appropriate
if (container->layout == container->parent->layout || children == 1)
{
new_container = container->parent;
pull_nodes(xinfo, container);
}
return new_container;
}
void open_client_window(xinfo_t *xinfo, node_t *node, char *template)
{
if (!node || !node->parent) return;
char *tmp = strstr(template, "%d");
if (!tmp)
{
fprintf(stderr, ERROR "Wrong command: missing XID placeholder\n" END);
return;
}
else if (strstr(++tmp, "%d"))
{
fprintf(stderr, ERROR "Wrong command: too many XID placeholders\n" END);
return;
}
char *command = malloc((strlen(template) + 8) * sizeof *command);
sprintf(command, template, node->parent->id);
char **args = split_string(command);
pid_t child = fork();
if (child == 0)
{
execv(args[0], args);
fprintf(stderr, ERROR "Error launching client\n" END);
exit(1);
}
xcb_generic_event_t *ge;
while ((ge = xcb_wait_for_event(xinfo->conn))->response_type != XCB_REPARENT_NOTIFY);
xcb_reparent_notify_event_t *rne = (xcb_reparent_notify_event_t *) ge;
node->id = rne->window;
node->pid = child;
}