]> git.lizzy.rs Git - bspwm.git/blob - tree.c
e9e1fcd1972e651d2345c7d9a08b24cd57506d1e
[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 put_status(void)
237 {
238     if (!status_stdout)
239         return;
240     bool urgent = false;
241     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
242         printf("%c%s:", (mon == m ? 'M' : 'm'), m->name);
243         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
244             for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n))
245                 urgent |= n->client->urgent;
246             printf("%c%c%s:", (m->desk == d ? 'D' : (d->root != NULL ? 'd' : '_')), (urgent ? '!' : '_'), d->name);
247         }
248     }
249     printf("L%c:W%X\n", (mon->desk->layout == LAYOUT_TILED ? 'T' : 'M'), (mon->desk->focus == NULL ? 0 : mon->desk->focus->client->window));
250     fflush(stdout);
251 }
252
253 void arrange(monitor_t *m, desktop_t *d)
254 {
255     PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
256
257     xcb_rectangle_t rect = m->rectangle;
258     int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
259     rect.x += m->left_padding + wg;
260     rect.y += m->top_padding + wg;
261     rect.width -= m->left_padding + m->right_padding + wg;
262     rect.height -= m->top_padding + m->bottom_padding + wg;
263     if (focus_follows_mouse)
264         get_pointer_position(&pointer_position);
265     apply_layout(m, d, d->root, rect, rect);
266 }
267
268 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
269 {
270     if (n == NULL)
271         return;
272
273     n->rectangle = rect;
274
275     if (is_leaf(n)) {
276         if (n->client->fullscreen)
277             return;
278
279         if (is_floating(n->client) && n->client->border_width != border_width) {
280             int ds = 2 * (border_width - n->client->border_width);
281             n->client->floating_rectangle.width += ds;
282             n->client->floating_rectangle.height += ds;
283         }
284
285         if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
286             n->client->border_width = 0;
287         else
288             n->client->border_width = border_width;
289
290         xcb_rectangle_t r;
291         if (is_tiled(n->client)) {
292             if (d->layout == LAYOUT_TILED)
293                 r = rect;
294             else if (d->layout == LAYOUT_MONOCLE)
295                 r = root_rect;
296             int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
297             int bleed = wg + 2 * n->client->border_width;
298             r.width = (bleed < r.width ? r.width - bleed : 1);
299             r.height = (bleed < r.height ? r.height - bleed : 1);
300             n->client->tiled_rectangle = r;
301         } else {
302             r = n->client->floating_rectangle;
303         }
304
305         window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
306         window_border_width(n->client->window, n->client->border_width);
307         window_draw_border(n, n == d->focus, m == mon);
308
309     } else {
310         xcb_rectangle_t first_rect;
311         xcb_rectangle_t second_rect;
312
313         if (n->first_child->vacant || n->second_child->vacant) {
314             first_rect = second_rect = rect;
315         } else {
316             unsigned int fence;
317             if (n->split_type == TYPE_VERTICAL) {
318                 fence = rect.width * n->split_ratio;
319                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
320                 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
321
322             } else if (n->split_type == TYPE_HORIZONTAL) {
323                 fence = rect.height * n->split_ratio;
324                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
325                 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
326             }
327         }
328
329         apply_layout(m, d, n->first_child, first_rect, root_rect);
330         apply_layout(m, d, n->second_child, second_rect, root_rect);
331     }
332 }
333
334 void insert_node(monitor_t *m, desktop_t *d, node_t *n)
335 {
336     if (d == NULL || n == NULL)
337         return;
338
339     PRINTF("insert node %X\n", n->client->window);
340
341     node_t *focus = d->focus;
342
343     if (focus == NULL) {
344         d->root = n;
345     } else {
346         node_t *dad = make_node();
347         node_t *fopar = focus->parent;
348         n->parent = dad;
349         n->born_as = split_mode;
350         switch (split_mode) {
351             case MODE_AUTOMATIC:
352                 if (fopar == NULL) {
353                     dad->first_child = n;
354                     dad->second_child = focus;
355                     if (m->rectangle.width > m->rectangle.height)
356                         dad->split_type = TYPE_VERTICAL;
357                     else
358                         dad->split_type = TYPE_HORIZONTAL;
359                     focus->parent = dad;
360                     d->root = dad;
361                 } else {
362                     node_t *grandpa = fopar->parent;
363                     dad->parent = grandpa;
364                     if (grandpa != NULL) {
365                         if (is_first_child(fopar))
366                             grandpa->first_child = dad;
367                         else
368                             grandpa->second_child = dad;
369                     } else {
370                         d->root = dad;
371                     }
372                     dad->split_type = fopar->split_type;
373                     dad->split_ratio = fopar->split_ratio;
374                     fopar->parent = dad;
375                     if (is_first_child(focus)) {
376                         dad->first_child = n;
377                         dad->second_child = fopar;
378                         rotate_tree(fopar, ROTATE_CLOCKWISE);
379                     } else {
380                         dad->first_child = fopar;
381                         dad->second_child = n;
382                         rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
383                     }
384                 }
385                 break;
386             case MODE_MANUAL:
387                 if (fopar != NULL) {
388                     if (is_first_child(focus))
389                         fopar->first_child = dad;
390                     else
391                         fopar->second_child = dad;
392                 }
393                 dad->split_ratio = focus->split_ratio;
394                 dad->parent = fopar;
395                 focus->parent = dad;
396                 switch (split_dir) {
397                     case DIR_LEFT:
398                         dad->split_type = TYPE_VERTICAL;
399                         dad->first_child = n;
400                         dad->second_child = focus;
401                         break;
402                     case DIR_RIGHT:
403                         dad->split_type = TYPE_VERTICAL;
404                         dad->first_child = focus;
405                         dad->second_child = n;
406                         break;
407                     case DIR_UP:
408                         dad->split_type = TYPE_HORIZONTAL;
409                         dad->first_child = n;
410                         dad->second_child = focus;
411                         break;
412                     case DIR_DOWN:
413                         dad->split_type = TYPE_HORIZONTAL;
414                         dad->first_child = focus;
415                         dad->second_child = n;
416                         break;
417                 }
418                 if (d->root == focus)
419                     d->root = dad;
420                 split_mode = MODE_AUTOMATIC;
421                 break;
422         }
423         if (focus->vacant)
424             update_vacant_state(fopar);
425     }
426 }
427
428 void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
429 {
430     if (n == NULL)
431         return;
432
433     PRINTF("focus node %X\n", n->client->window);
434
435     split_mode = MODE_AUTOMATIC;
436     n->client->urgent = false;
437
438     if (is_mapped) {
439         if (mon != m || d->focus != n) {
440             window_draw_border(d->focus, m != mon, m == mon);
441             window_draw_border(n, true, true);
442         }
443         if (focus_follows_mouse)
444             get_pointer_position(&pointer_position);
445         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
446     }
447
448     if (!is_tiled(n->client)) {
449         if (!adaptative_raise || !might_cover(d, n))
450             window_raise(n->client->window);
451     } else {
452         window_pseudo_raise(d, n->client->window);
453     }
454
455     if (d->focus != n) {
456         d->last_focus = d->focus;
457         d->focus = n;
458     }
459
460     ewmh_update_active_window();
461     put_status();
462 }
463
464 void update_current(void)
465 {
466     if (mon->desk->focus == NULL)
467         ewmh_update_active_window();
468     else
469         focus_node(mon, mon->desk, mon->desk->focus, true);
470     put_status();
471 }
472
473 void unlink_node(desktop_t *d, node_t *n)
474 {
475     if (d == NULL || n == NULL)
476         return;
477
478     PRINTF("unlink node %X\n", n->client->window);
479
480     node_t *p = n->parent;
481
482     if (p == NULL) {
483         d->root = NULL;
484         d->focus = NULL;
485         d->last_focus = NULL;
486     } else {
487         node_t *b;
488         node_t *g = p->parent;
489         bool n_first_child = is_first_child(n);
490         if (n_first_child) {
491             b = p->second_child;
492             if (n->born_as == MODE_AUTOMATIC)
493                 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
494         } else {
495             b = p->first_child;
496             if (n->born_as == MODE_AUTOMATIC)
497                 rotate_tree(b, ROTATE_CLOCKWISE);
498         }
499         b->parent = g;
500         if (g != NULL) {
501             if (is_first_child(p))
502                 g->first_child = b;
503             else
504                 g->second_child = b;
505         } else {
506             d->root = b;
507         }
508
509         n->parent = NULL;
510         free(p);
511
512         if (n == d->last_focus) {
513             d->last_focus = NULL;
514         } else if (n == d->focus) {
515             if (d->last_focus != NULL)
516                 d->focus = d->last_focus;
517             else
518                 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
519             d->last_focus = NULL;
520         }
521
522         update_vacant_state(b->parent);
523     }
524 }
525
526 void remove_node(desktop_t *d, node_t *n)
527 {
528     if (d == NULL || n == NULL)
529         return;
530
531     PRINTF("remove node %X\n", n->client->window);
532
533     unlink_node(d, n);
534     free(n->client);
535     free(n);
536
537     num_clients--;
538     ewmh_update_client_list();
539
540     if (mon->desk == d)
541         update_current();
542 }
543
544 void swap_nodes(node_t *n1, node_t *n2)
545 {
546     if (n1 == NULL || n2 == NULL || n1 == n2)
547         return;
548
549     PUTS("swap nodes");
550
551     /* (n1 and n2 are leaves) */
552     node_t *pn1 = n1->parent;
553     node_t *pn2 = n2->parent;
554     bool n1_first_child = is_first_child(n1);
555     bool n2_first_child = is_first_child(n2);
556
557     if (pn1 != NULL) {
558         if (n1_first_child)
559             pn1->first_child = n2;
560         else
561             pn1->second_child = n2;
562     }
563
564     if (pn2 != NULL) {
565         if (n2_first_child)
566             pn2->first_child = n1;
567         else
568             pn2->second_child = n1;
569     }
570
571     n1->parent = pn2;
572     n2->parent = pn1;
573
574     if (n1->vacant != n2->vacant) {
575         update_vacant_state(n1->parent);
576         update_vacant_state(n2->parent);
577     }
578 }
579
580 void fit_monitor(monitor_t *m, client_t *c)
581 {
582     xcb_rectangle_t crect = c->floating_rectangle;
583     xcb_rectangle_t mrect = m->rectangle;
584     while (crect.x < mrect.x)
585         crect.x += mrect.width;
586     while (crect.x > (mrect.x + mrect.width - 1))
587         crect.x -= mrect.width;
588     while (crect.y < mrect.y)
589         crect.y += mrect.height;
590     while (crect.y > (mrect.y + mrect.height - 1))
591         crect.y -= mrect.height;
592     c->floating_rectangle = crect;
593 }
594
595 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
596 {
597     if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
598         return;
599
600     PRINTF("transfer node %X\n", n->client->window);
601
602     unlink_node(ds, n);
603     insert_node(md, dd, n);
604     ewmh_set_wm_desktop(n, dd);
605
606     if (ds == ms->desk && dd != md->desk) {
607         window_hide(n->client->window);
608     }
609
610     fit_monitor(md, n->client);
611
612     if (n->client->fullscreen)
613         window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
614
615     if (ds != ms->desk && dd == md->desk) {
616         window_show(n->client->window);
617         focus_node(md, dd, n, true);
618     } else {
619         focus_node(md, dd, n, false);
620     }
621
622     if (ds == ms->desk || dd == md->desk)
623         update_current();
624 }
625
626 void select_monitor(monitor_t *m)
627 {
628     if (m == NULL || mon == m)
629         return;
630
631     PRINTF("select monitor %s\n", m->name);
632
633     focus_node(m, m->desk, m->desk->focus, true);
634
635     last_mon = mon;
636     mon = m;
637
638     ewmh_update_current_desktop();
639     put_status();
640 }
641
642 void select_desktop(desktop_t *d)
643 {
644     if (d == NULL || d == mon->desk)
645         return;
646
647     PRINTF("select desktop %s\n", d->name);
648
649     node_t *n = first_extrema(d->root);
650
651     while (n != NULL) {
652         window_show(n->client->window);
653         n = next_leaf(n);
654     }
655
656     n = first_extrema(mon->desk->root);
657
658     while (n != NULL) {
659         window_hide(n->client->window);
660         n = next_leaf(n);
661     }
662
663     mon->last_desk = mon->desk;
664     mon->desk = d;
665
666     update_current();
667     ewmh_update_current_desktop();
668     put_status();
669 }
670
671 void cycle_monitor(cycle_dir_t dir)
672 {
673     if (dir == CYCLE_NEXT)
674         select_monitor((mon->next == NULL ? mon_head : mon->next));
675     else if (dir == CYCLE_PREV)
676         select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
677 }
678
679 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
680 {
681     desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
682     if (f == NULL)
683         f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
684
685     while (f != d) {
686         if (skip == DESKTOP_SKIP_NONE 
687                 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
688                 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
689             select_desktop(f);
690             return;
691         }
692         f = (dir == CYCLE_PREV ? f->prev : f->next);
693         if (f == NULL)
694             f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
695     }
696 }
697
698 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
699 {
700     if (n == NULL)
701         return;
702
703     PUTS("cycle leaf");
704
705     node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
706     if (f == NULL)
707         f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
708
709     while (f != n) {
710         bool tiled = is_tiled(f->client);
711         if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
712                 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
713                 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
714             focus_node(m, d, f, true);
715             return;
716         }
717         f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
718         if (f == NULL)
719             f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
720     }
721 }
722
723 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
724 {
725     if (n == NULL)
726         return;
727
728     PUTS("nearest leaf");
729
730     node_t *x = NULL;
731
732     for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
733         if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
734                 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
735                 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
736             if ((dir == NEAREST_OLDER
737                         && (f->client->uid < n->client->uid)
738                         && (x == NULL || f->client->uid > x->client->uid))
739                     || (dir == NEAREST_NEWER
740                         && (f->client->uid > n->client->uid)
741                         && (x == NULL || f->client->uid < x->client->uid)))
742                 x = f;
743
744     focus_node(m, d, x, true);
745 }
746
747 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
748     if (d == NULL || d->root == NULL || is_leaf(d->root))
749         return;
750     node_t *par = d->focus->parent;
751     bool focus_first_child = is_first_child(d->focus);
752     if (dir == CIRCULATE_FORWARD)
753         for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
754             swap_nodes(f, s);
755     else
756         for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
757             swap_nodes(f, s);
758     if (focus_first_child)
759         focus_node(m, d, par->first_child, true);
760     else
761         focus_node(m, d, par->second_child, true);
762 }
763
764 void update_vacant_state(node_t *n)
765 {
766     if (n == NULL)
767         return;
768
769     PUTS("update vacant state");
770
771     /* n is not a leaf */
772     node_t *p = n;
773
774     while (p != NULL) {
775         p->vacant = (p->first_child->vacant && p->second_child->vacant);
776         p = p->parent;
777     }
778 }
779
780 monitor_t *find_monitor(char *name)
781 {
782     for (monitor_t *m = mon_head; m != NULL; m = m->next)
783         if (strcmp(m->name, name) == 0)
784             return m;
785     return NULL;
786 }
787
788 void add_desktop(monitor_t *m, char *name)
789 {
790     desktop_t *d = make_desktop(name);
791     if (m->desk == NULL) {
792         m->desk = d;
793         m->desk_head = d;
794         m->desk_tail = d;
795     } else {
796         m->desk_tail->next = d;
797         d->prev = m->desk_tail;
798         m->desk_tail = d;
799     }
800     num_desktops++;
801     ewmh_update_number_of_desktops();
802     ewmh_update_desktop_names();
803     put_status();
804 }
805
806 void add_monitor(xcb_rectangle_t *rect)
807 {
808     monitor_t *m = make_monitor(rect);
809     if (mon == NULL) {
810         mon = m;
811         mon_head = m;
812         mon_tail = m;
813     } else {
814         mon_tail->next = m;
815         m->prev = mon_tail;
816         mon_tail = m;
817     }
818     num_monitors++;
819 }