]> git.lizzy.rs Git - bspwm.git/blob - tree.c
007484de957f6fcd946cc1076446777fb51f55d5
[bspwm.git] / tree.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <xcb/xcb.h>
6 #include <xcb/xcb_event.h>
7 #include "settings.h"
8 #include "helpers.h"
9 #include "window.h"
10 #include "types.h"
11 #include "bspwm.h"
12 #include "ewmh.h"
13 #include "tree.h"
14
15 bool is_leaf(node_t *n)
16 {
17     return (n != NULL && n->first_child == NULL && n->second_child == NULL);
18 }
19
20 bool is_tiled(client_t *c)
21 {
22     if (c == NULL)
23         return false;
24     return (!c->floating && !c->transient && !c->fullscreen);
25 }
26
27 bool is_floating(client_t *c)
28 {
29     if (c == NULL)
30         return false;
31     return (c->floating && !c->fullscreen);
32 }
33
34 bool is_first_child(node_t *n)
35 {
36     return (n != NULL && n->parent != NULL && n->parent->first_child == n);
37 }
38
39 bool is_second_child(node_t *n)
40 {
41     return (n != NULL && n->parent != NULL && n->parent->second_child == n);
42 }
43
44 void change_split_ratio(node_t *n, value_change_t chg) {
45     n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
46 }
47
48 node_t *first_extrema(node_t *n)
49 {
50     if (n == NULL)
51         return NULL;
52     else if (n->first_child == NULL)
53         return n;
54     else
55         return first_extrema(n->first_child);
56 }
57
58 node_t *second_extrema(node_t *n)
59 {
60     if (n == NULL)
61         return NULL;
62     else if (n->second_child == NULL)
63         return n;
64     else
65         return second_extrema(n->second_child);
66 }
67
68 node_t *next_leaf(node_t *n)
69 {
70     if (n == NULL)
71         return NULL;
72     node_t *p = n;
73     while (is_second_child(p))
74         p = p->parent;
75     if (p->parent == NULL)
76         return NULL;
77     return first_extrema(p->parent->second_child);
78 }
79
80 node_t *prev_leaf(node_t *n)
81 {
82     if (n == NULL)
83         return NULL;
84     node_t *p = n;
85     while (is_first_child(p))
86         p = p->parent;
87     if (p->parent == NULL)
88         return NULL;
89     return second_extrema(p->parent->first_child);
90 }
91
92 node_t *find_fence(node_t *n, direction_t dir)
93 {
94     node_t *p;
95
96     if (n == NULL)
97         return NULL;
98
99     p = n->parent;
100
101     while (p != NULL) {
102         if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
103                 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
104                 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
105                 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
106             return p;
107         p = p->parent;
108     }
109
110     return NULL;
111 }
112
113 node_t *find_neighbor(node_t *n, direction_t dir)
114 {
115     node_t *fence = find_fence(n, dir);
116
117     if (fence == NULL)
118         return NULL;
119
120     if (dir == DIR_UP || dir == DIR_LEFT)
121         return second_extrema(fence->first_child);
122     else if (dir == DIR_DOWN || dir == DIR_RIGHT)
123         return first_extrema(fence->second_child);
124
125     return NULL;
126 }
127
128 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
129 {
130     node_t *fence = find_fence(n, dir);
131
132     if (fence == NULL)
133         return;
134
135     if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
136             || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
137         change_split_ratio(fence, CHANGE_INCREASE);
138     else
139         change_split_ratio(fence, CHANGE_DECREASE);
140 }
141
142 void rotate_tree(node_t *n, rotate_t rot)
143 {
144     if (n == NULL || is_leaf(n))
145         return;
146
147     node_t *tmp;
148
149     if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
150             || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
151             || rot == ROTATE_FULL_CYCLE) {
152         tmp = n->first_child;
153         n->first_child = n->second_child;
154         n->second_child = tmp;
155         n->split_ratio = 1.0 - n->split_ratio;
156     }
157
158     if (rot != ROTATE_FULL_CYCLE) {
159         if (n->split_type == TYPE_HORIZONTAL)
160             n->split_type = TYPE_VERTICAL;
161         else if (n->split_type == TYPE_VERTICAL)
162             n->split_type = TYPE_HORIZONTAL;
163     }
164
165     rotate_tree(n->first_child, rot);
166     rotate_tree(n->second_child, rot);
167 }
168
169 void dump_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
170 {
171     if (n == NULL)
172         return;
173
174     char line[MAXLEN];
175
176     for (unsigned int i = 0; i < depth; i++)
177         strncat(rsp, "  ", REMLEN(rsp));
178
179     if (is_leaf(n))
180         snprintf(line, sizeof(line), "%s %X %s%s%s%s%s", n->client->class_name, n->client->window, (n->client->floating ? "f" : "-"), (n->client->transient ? "t" : "-"), (n->client->fullscreen ? "F" : "-"), (n->client->urgent ? "u" : "-"), (n->client->locked ? "l" : "-"));
181     else
182         snprintf(line, sizeof(line), "%s %.2f", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
183
184     strncat(rsp, line, REMLEN(rsp));
185
186     if (n == d->focus)
187         strncat(rsp, " *\n", REMLEN(rsp));
188     else
189         strncat(rsp, "\n", REMLEN(rsp));
190
191     dump_tree(d, n->first_child, rsp, depth + 1);
192     dump_tree(d, n->second_child, rsp, depth + 1);
193 }
194
195 void refresh_current(void) {
196     if (mon->desk->focus == NULL)
197         ewmh_update_active_window();
198     else
199         focus_node(mon, mon->desk, mon->desk->focus, true);
200 }
201
202 void list_monitors(list_option_t opt, char *rsp)
203 {
204     monitor_t *m = mon_head;
205
206     while (m != NULL) {
207         strncat(rsp, m->name, REMLEN(rsp));
208         if (mon == m)
209             strncat(rsp, " #\n", REMLEN(rsp));
210         else
211             strncat(rsp, "\n", REMLEN(rsp));
212         if (opt == LIST_OPTION_VERBOSE)
213             list_desktops(m, opt, 1, rsp);
214         m = m->next;
215     }
216 }
217
218 void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
219 {
220     desktop_t *d = m->desk_head;
221
222     while (d != NULL) {
223         for (unsigned int i = 0; i < depth; i++)
224             strncat(rsp, "  ", REMLEN(rsp));
225         strncat(rsp, d->name, REMLEN(rsp));
226         if (m->desk == d)
227             strncat(rsp, " @\n", REMLEN(rsp));
228         else
229             strncat(rsp, "\n", REMLEN(rsp));
230         if (opt == LIST_OPTION_VERBOSE)
231             dump_tree(d, d->root, rsp, depth + 1);
232         d = d->next;
233     }
234 }
235
236 void arrange(monitor_t *m, desktop_t *d)
237 {
238     PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
239
240     xcb_rectangle_t rect = m->rectangle;
241     int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
242     rect.x += left_padding + wg;
243     rect.y += top_padding + wg;
244     rect.width -= left_padding + right_padding + wg;
245     rect.height -= top_padding + bottom_padding + wg;
246     if (focus_follows_mouse)
247         get_pointer_position(&pointer_position);
248     apply_layout(m, d, d->root, rect, rect);
249 }
250
251 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
252 {
253     if (n == NULL)
254         return;
255
256     n->rectangle = rect;
257
258     if (is_leaf(n)) {
259         if (n->client->fullscreen)
260             return;
261
262         if (is_floating(n->client) && n->client->border_width != border_width) {
263             int ds = 2 * (border_width - n->client->border_width);
264             n->client->floating_rectangle.width += ds;
265             n->client->floating_rectangle.height += ds;
266         }
267
268         if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
269             n->client->border_width = 0;
270         else
271             n->client->border_width = border_width;
272
273         xcb_rectangle_t r;
274         if (is_tiled(n->client)) {
275             if (d->layout == LAYOUT_TILED)
276                 r = rect;
277             else if (d->layout == LAYOUT_MONOCLE)
278                 r = root_rect;
279             int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
280             int bleed = wg + 2 * n->client->border_width;
281             r.width = (bleed < r.width ? r.width - bleed : 1);
282             r.height = (bleed < r.height ? r.height - bleed : 1);
283             n->client->tiled_rectangle = r;
284         } else {
285             r = n->client->floating_rectangle;
286         }
287
288         window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
289         window_border_width(n->client->window, n->client->border_width);
290         window_draw_border(n, n == d->focus, m == mon);
291
292     } else {
293         xcb_rectangle_t first_rect;
294         xcb_rectangle_t second_rect;
295
296         if (n->first_child->vacant || n->second_child->vacant) {
297             first_rect = second_rect = rect;
298         } else {
299             unsigned int fence;
300             if (n->split_type == TYPE_VERTICAL) {
301                 fence = rect.width * n->split_ratio;
302                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
303                 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
304
305             } else if (n->split_type == TYPE_HORIZONTAL) {
306                 fence = rect.height * n->split_ratio;
307                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
308                 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
309             }
310         }
311
312         apply_layout(m, d, n->first_child, first_rect, root_rect);
313         apply_layout(m, d, n->second_child, second_rect, root_rect);
314     }
315 }
316
317 void insert_node(monitor_t *m, desktop_t *d, node_t *n)
318 {
319     if (d == NULL || n == NULL)
320         return;
321
322     PRINTF("insert node %X\n", n->client->window);
323
324     node_t *focus = d->focus;
325
326     if (focus == NULL) {
327         d->root = n;
328     } else {
329         node_t *dad = make_node();
330         node_t *fopar = focus->parent;
331         n->parent = dad;
332         n->born_as = split_mode;
333         switch (split_mode) {
334             case MODE_AUTOMATIC:
335                 if (fopar == NULL) {
336                     dad->first_child = n;
337                     dad->second_child = focus;
338                     if (m->rectangle.width > m->rectangle.height)
339                         dad->split_type = TYPE_VERTICAL;
340                     else
341                         dad->split_type = TYPE_HORIZONTAL;
342                     focus->parent = dad;
343                     d->root = dad;
344                 } else {
345                     node_t *grandpa = fopar->parent;
346                     dad->parent = grandpa;
347                     if (grandpa != NULL) {
348                         if (is_first_child(fopar))
349                             grandpa->first_child = dad;
350                         else
351                             grandpa->second_child = dad;
352                     } else {
353                         d->root = dad;
354                     }
355                     dad->split_type = fopar->split_type;
356                     dad->split_ratio = fopar->split_ratio;
357                     fopar->parent = dad;
358                     if (is_first_child(focus)) {
359                         dad->first_child = n;
360                         dad->second_child = fopar;
361                         rotate_tree(fopar, ROTATE_CLOCKWISE);
362                     } else {
363                         dad->first_child = fopar;
364                         dad->second_child = n;
365                         rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
366                     }
367                 }
368                 break;
369             case MODE_MANUAL:
370                 if (fopar != NULL) {
371                     if (is_first_child(focus))
372                         fopar->first_child = dad;
373                     else
374                         fopar->second_child = dad;
375                 }
376                 dad->split_ratio = focus->split_ratio;
377                 dad->parent = fopar;
378                 focus->parent = dad;
379                 switch (split_dir) {
380                     case DIR_LEFT:
381                         dad->split_type = TYPE_VERTICAL;
382                         dad->first_child = n;
383                         dad->second_child = focus;
384                         break;
385                     case DIR_RIGHT:
386                         dad->split_type = TYPE_VERTICAL;
387                         dad->first_child = focus;
388                         dad->second_child = n;
389                         break;
390                     case DIR_UP:
391                         dad->split_type = TYPE_HORIZONTAL;
392                         dad->first_child = n;
393                         dad->second_child = focus;
394                         break;
395                     case DIR_DOWN:
396                         dad->split_type = TYPE_HORIZONTAL;
397                         dad->first_child = focus;
398                         dad->second_child = n;
399                         break;
400                 }
401                 if (d->root == focus)
402                     d->root = dad;
403                 split_mode = MODE_AUTOMATIC;
404                 break;
405         }
406         if (focus->vacant)
407             update_vacant_state(fopar);
408     }
409 }
410
411 void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
412 {
413     if (n == NULL)
414         return;
415
416     PRINTF("focus node %X\n", n->client->window);
417
418     split_mode = MODE_AUTOMATIC;
419     n->client->urgent = false;
420
421     if (is_mapped) {
422         if (mon != m || d->focus != n) {
423             window_draw_border(d->focus, m != mon, m == mon);
424             window_draw_border(n, true, true);
425         }
426         if (focus_follows_mouse)
427             get_pointer_position(&pointer_position);
428         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
429     }
430
431     if (!is_tiled(n->client))
432         window_raise(n->client->window);
433     else
434         window_pseudo_raise(d, n->client->window);
435
436     if (d->focus != n) {
437         d->last_focus = d->focus;
438         d->focus = n;
439     }
440
441     ewmh_update_active_window();
442 }
443
444 void update_current(void)
445 {
446     if (mon->desk->focus == NULL)
447         ewmh_update_active_window();
448     else
449         focus_node(mon, mon->desk, mon->desk->focus, true);
450 }
451
452 void unlink_node(desktop_t *d, node_t *n)
453 {
454     if (d == NULL || n == NULL)
455         return;
456
457     PRINTF("unlink node %X\n", n->client->window);
458
459     node_t *p = n->parent;
460
461     if (p == NULL) {
462         d->root = NULL;
463         d->focus = NULL;
464         d->last_focus = NULL;
465     } else {
466         node_t *b;
467         node_t *g = p->parent;
468         bool n_first_child = is_first_child(n);
469         if (n_first_child) {
470             b = p->second_child;
471             if (n->born_as == MODE_AUTOMATIC)
472                 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
473         } else {
474             b = p->first_child;
475             if (n->born_as == MODE_AUTOMATIC)
476                 rotate_tree(b, ROTATE_CLOCKWISE);
477         }
478         b->parent = g;
479         if (g != NULL) {
480             if (is_first_child(p))
481                 g->first_child = b;
482             else
483                 g->second_child = b;
484         } else {
485             d->root = b;
486         }
487
488         n->parent = NULL;
489         free(p);
490
491         if (n == d->last_focus) {
492             d->last_focus = NULL;
493         } else if (n == d->focus) {
494             if (d->last_focus != NULL)
495                 d->focus = d->last_focus;
496             else
497                 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
498             d->last_focus = NULL;
499         }
500
501         update_vacant_state(b->parent);
502     }
503 }
504
505 void remove_node(desktop_t *d, node_t *n)
506 {
507     if (d == NULL || n == NULL)
508         return;
509
510     PRINTF("remove node %X\n", n->client->window);
511
512     unlink_node(d, n);
513     free(n->client);
514     free(n);
515
516     num_clients--;
517     ewmh_update_client_list();
518
519     if (mon->desk == d)
520         update_current();
521 }
522
523 void swap_nodes(node_t *n1, node_t *n2)
524 {
525     if (n1 == NULL || n2 == NULL || n1 == n2)
526         return;
527
528     PUTS("swap nodes");
529
530     /* (n1 and n2 are leaves) */
531     node_t *pn1 = n1->parent;
532     node_t *pn2 = n2->parent;
533     bool n1_first_child = is_first_child(n1);
534     bool n2_first_child = is_first_child(n2);
535
536     if (pn1 != NULL) {
537         if (n1_first_child)
538             pn1->first_child = n2;
539         else
540             pn1->second_child = n2;
541     }
542
543     if (pn2 != NULL) {
544         if (n2_first_child)
545             pn2->first_child = n1;
546         else
547             pn2->second_child = n1;
548     }
549
550     n1->parent = pn2;
551     n2->parent = pn1;
552
553     if (n1->vacant != n2->vacant) {
554         update_vacant_state(n1->parent);
555         update_vacant_state(n2->parent);
556     }
557 }
558
559 void fit_monitor(monitor_t *m, client_t *c)
560 {
561     xcb_rectangle_t cr = c->floating_rectangle;
562     xcb_rectangle_t mr = m->rectangle;
563     if (cr.x < mr.x)
564         cr.x += mr.x;
565     if (cr.y < mr.y)
566         cr.y += mr.y;
567 }
568
569 void translate_coordinates(monitor_t *ms, monitor_t *md, client_t *c)
570 {
571     if (ms == md)
572         return;
573
574     uint32_t vx = md->rectangle.x - ms->rectangle.x;
575     uint32_t vy = md->rectangle.y - ms->rectangle.y;
576     c->floating_rectangle.x += vx;
577     c->floating_rectangle.y += vy;
578 }
579
580 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
581 {
582     if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
583         return;
584
585     PRINTF("transfer node %X\n", n->client->window);
586
587     unlink_node(ds, n);
588     insert_node(md, dd, n);
589     ewmh_set_wm_desktop(n, dd);
590
591     if (ds == ms->desk && dd != md->desk) {
592         window_hide(n->client->window);
593     }
594
595     translate_coordinates(ms, md, n->client);
596     if (n->client->fullscreen)
597         window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
598
599     if (ds != ms->desk && dd == md->desk) {
600         window_show(n->client->window);
601         focus_node(md, dd, n, true);
602     } else {
603         focus_node(md, dd, n, false);
604     }
605
606     if (ds == ms->desk || dd == md->desk)
607         update_current();
608 }
609
610 void select_monitor(monitor_t *m)
611 {
612     if (m == NULL || mon == m)
613         return;
614
615     PRINTF("select monitor %s\n", m->name);
616
617     focus_node(m, m->desk, m->desk->focus, true);
618
619     last_mon = mon;
620     mon = m;
621
622     ewmh_update_current_desktop();
623 }
624
625 void select_desktop(desktop_t *d)
626 {
627     if (d == NULL || d == mon->desk)
628         return;
629
630     PRINTF("select desktop %s\n", d->name);
631
632     node_t *n = first_extrema(d->root);
633
634     while (n != NULL) {
635         window_show(n->client->window);
636         n = next_leaf(n);
637     }
638
639     n = first_extrema(mon->desk->root);
640
641     while (n != NULL) {
642         window_hide(n->client->window);
643         n = next_leaf(n);
644     }
645
646     mon->last_desk = mon->desk;
647     mon->desk = d;
648
649     update_current();
650     ewmh_update_current_desktop();
651 }
652
653 void cycle_monitor(cycle_dir_t dir)
654 {
655     if (dir == CYCLE_NEXT)
656         select_monitor((mon->next == NULL ? mon_head : mon->next));
657     else if (dir == CYCLE_PREV)
658         select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
659 }
660
661 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
662 {
663     desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
664     if (f == NULL)
665         f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
666
667     while (f != d) {
668         if (skip == DESKTOP_SKIP_NONE 
669                 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
670                 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
671             select_desktop(f);
672             return;
673         }
674         f = (dir == CYCLE_PREV ? f->prev : f->next);
675         if (f == NULL)
676             f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
677     }
678 }
679
680 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
681 {
682     if (n == NULL)
683         return;
684
685     PUTS("cycle leaf");
686
687     node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
688     if (f == NULL)
689         f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
690
691     while (f != n) {
692         bool tiled = is_tiled(f->client);
693         if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
694                 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
695                 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
696             focus_node(m, d, f, true);
697             return;
698         }
699         f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
700         if (f == NULL)
701             f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
702     }
703 }
704
705 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
706 {
707     if (n == NULL)
708         return;
709
710     PUTS("nearest leaf");
711
712     node_t *x = NULL;
713
714     for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
715         if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
716                 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
717                 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
718             if ((dir == NEAREST_OLDER
719                         && (f->client->uid < n->client->uid)
720                         && (x == NULL || f->client->uid > x->client->uid))
721                     || (dir == NEAREST_NEWER
722                         && (f->client->uid > n->client->uid)
723                         && (x == NULL || f->client->uid < x->client->uid)))
724                 x = f;
725
726     focus_node(m, d, x, true);
727 }
728
729 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
730     if (d == NULL || d->root == NULL || is_leaf(d->root))
731         return;
732     node_t *par = d->focus->parent;
733     bool focus_first_child = is_first_child(d->focus);
734     if (dir == CIRCULATE_FORWARD)
735         for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
736             swap_nodes(f, s);
737     else
738         for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
739             swap_nodes(f, s);
740     if (focus_first_child)
741         focus_node(m, d, par->first_child, true);
742     else
743         focus_node(m, d, par->second_child, true);
744 }
745
746 void update_vacant_state(node_t *n)
747 {
748     if (n == NULL)
749         return;
750
751     PUTS("update vacant state");
752
753     /* n is not a leaf */
754     node_t *p = n;
755
756     while (p != NULL) {
757         p->vacant = (p->first_child->vacant && p->second_child->vacant);
758         p = p->parent;
759     }
760 }
761
762 monitor_t *find_monitor(char *name)
763 {
764     for (monitor_t *m = mon_head; m != NULL; m = m->next)
765         if (strcmp(m->name, name) == 0)
766             return m;
767     return NULL;
768 }
769
770 void add_desktop(monitor_t *m, char *name)
771 {
772     desktop_t *d = make_desktop(name);
773     if (m->desk == NULL) {
774         m->desk = d;
775         m->desk_head = d;
776         m->desk_tail = d;
777     } else {
778         m->desk_tail->next = d;
779         d->prev = m->desk_tail;
780         m->desk_tail = d;
781     }
782     num_desktops++;
783     ewmh_update_number_of_desktops();
784     ewmh_update_desktop_names();
785 }
786
787 void add_monitor(xcb_rectangle_t *rect)
788 {
789     monitor_t *m = make_monitor(rect);
790     if (mon == NULL) {
791         mon = m;
792         mon_head = m;
793         mon_tail = m;
794     } else {
795         mon_tail->next = m;
796         m->prev = mon_tail;
797         mon_tail = m;
798     }
799     num_monitors++;
800 }