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