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