]> git.lizzy.rs Git - bspwm.git/blob - tree.c
New setting: 'adaptative_raise'
[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 += m->left_padding + wg;
243     rect.y += m->top_padding + wg;
244     rect.width -= m->left_padding + m->right_padding + wg;
245     rect.height -= m->top_padding + m->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         if (!adaptative_raise || !might_cover(d, n))
433             window_raise(n->client->window);
434     } else {
435         window_pseudo_raise(d, n->client->window);
436     }
437
438     if (d->focus != n) {
439         d->last_focus = d->focus;
440         d->focus = n;
441     }
442
443     ewmh_update_active_window();
444 }
445
446 void update_current(void)
447 {
448     if (mon->desk->focus == NULL)
449         ewmh_update_active_window();
450     else
451         focus_node(mon, mon->desk, mon->desk->focus, true);
452 }
453
454 void unlink_node(desktop_t *d, node_t *n)
455 {
456     if (d == NULL || n == NULL)
457         return;
458
459     PRINTF("unlink node %X\n", n->client->window);
460
461     node_t *p = n->parent;
462
463     if (p == NULL) {
464         d->root = NULL;
465         d->focus = NULL;
466         d->last_focus = NULL;
467     } else {
468         node_t *b;
469         node_t *g = p->parent;
470         bool n_first_child = is_first_child(n);
471         if (n_first_child) {
472             b = p->second_child;
473             if (n->born_as == MODE_AUTOMATIC)
474                 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
475         } else {
476             b = p->first_child;
477             if (n->born_as == MODE_AUTOMATIC)
478                 rotate_tree(b, ROTATE_CLOCKWISE);
479         }
480         b->parent = g;
481         if (g != NULL) {
482             if (is_first_child(p))
483                 g->first_child = b;
484             else
485                 g->second_child = b;
486         } else {
487             d->root = b;
488         }
489
490         n->parent = NULL;
491         free(p);
492
493         if (n == d->last_focus) {
494             d->last_focus = NULL;
495         } else if (n == d->focus) {
496             if (d->last_focus != NULL)
497                 d->focus = d->last_focus;
498             else
499                 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
500             d->last_focus = NULL;
501         }
502
503         update_vacant_state(b->parent);
504     }
505 }
506
507 void remove_node(desktop_t *d, node_t *n)
508 {
509     if (d == NULL || n == NULL)
510         return;
511
512     PRINTF("remove node %X\n", n->client->window);
513
514     unlink_node(d, n);
515     free(n->client);
516     free(n);
517
518     num_clients--;
519     ewmh_update_client_list();
520
521     if (mon->desk == d)
522         update_current();
523 }
524
525 void swap_nodes(node_t *n1, node_t *n2)
526 {
527     if (n1 == NULL || n2 == NULL || n1 == n2)
528         return;
529
530     PUTS("swap nodes");
531
532     /* (n1 and n2 are leaves) */
533     node_t *pn1 = n1->parent;
534     node_t *pn2 = n2->parent;
535     bool n1_first_child = is_first_child(n1);
536     bool n2_first_child = is_first_child(n2);
537
538     if (pn1 != NULL) {
539         if (n1_first_child)
540             pn1->first_child = n2;
541         else
542             pn1->second_child = n2;
543     }
544
545     if (pn2 != NULL) {
546         if (n2_first_child)
547             pn2->first_child = n1;
548         else
549             pn2->second_child = n1;
550     }
551
552     n1->parent = pn2;
553     n2->parent = pn1;
554
555     if (n1->vacant != n2->vacant) {
556         update_vacant_state(n1->parent);
557         update_vacant_state(n2->parent);
558     }
559 }
560
561 void fit_monitor(monitor_t *m, client_t *c)
562 {
563     xcb_rectangle_t crect = c->floating_rectangle;
564     xcb_rectangle_t mrect = m->rectangle;
565     while (crect.x < mrect.x)
566         crect.x += mrect.width;
567     while (crect.x > (mrect.x + mrect.width - 1))
568         crect.x -= mrect.width;
569     while (crect.y < mrect.y)
570         crect.y += mrect.height;
571     while (crect.y > (mrect.y + mrect.height - 1))
572         crect.y -= mrect.height;
573     c->floating_rectangle = crect;
574 }
575
576 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
577 {
578     if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
579         return;
580
581     PRINTF("transfer node %X\n", n->client->window);
582
583     unlink_node(ds, n);
584     insert_node(md, dd, n);
585     ewmh_set_wm_desktop(n, dd);
586
587     if (ds == ms->desk && dd != md->desk) {
588         window_hide(n->client->window);
589     }
590
591     fit_monitor(md, n->client);
592
593     if (n->client->fullscreen)
594         window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
595
596     if (ds != ms->desk && dd == md->desk) {
597         window_show(n->client->window);
598         focus_node(md, dd, n, true);
599     } else {
600         focus_node(md, dd, n, false);
601     }
602
603     if (ds == ms->desk || dd == md->desk)
604         update_current();
605 }
606
607 void select_monitor(monitor_t *m)
608 {
609     if (m == NULL || mon == m)
610         return;
611
612     PRINTF("select monitor %s\n", m->name);
613
614     focus_node(m, m->desk, m->desk->focus, true);
615
616     last_mon = mon;
617     mon = m;
618
619     ewmh_update_current_desktop();
620 }
621
622 void select_desktop(desktop_t *d)
623 {
624     if (d == NULL || d == mon->desk)
625         return;
626
627     PRINTF("select desktop %s\n", d->name);
628
629     node_t *n = first_extrema(d->root);
630
631     while (n != NULL) {
632         window_show(n->client->window);
633         n = next_leaf(n);
634     }
635
636     n = first_extrema(mon->desk->root);
637
638     while (n != NULL) {
639         window_hide(n->client->window);
640         n = next_leaf(n);
641     }
642
643     mon->last_desk = mon->desk;
644     mon->desk = d;
645
646     update_current();
647     ewmh_update_current_desktop();
648 }
649
650 void cycle_monitor(cycle_dir_t dir)
651 {
652     if (dir == CYCLE_NEXT)
653         select_monitor((mon->next == NULL ? mon_head : mon->next));
654     else if (dir == CYCLE_PREV)
655         select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
656 }
657
658 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
659 {
660     desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
661     if (f == NULL)
662         f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
663
664     while (f != d) {
665         if (skip == DESKTOP_SKIP_NONE 
666                 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
667                 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
668             select_desktop(f);
669             return;
670         }
671         f = (dir == CYCLE_PREV ? f->prev : f->next);
672         if (f == NULL)
673             f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
674     }
675 }
676
677 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
678 {
679     if (n == NULL)
680         return;
681
682     PUTS("cycle leaf");
683
684     node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
685     if (f == NULL)
686         f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
687
688     while (f != n) {
689         bool tiled = is_tiled(f->client);
690         if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
691                 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
692                 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
693             focus_node(m, d, f, true);
694             return;
695         }
696         f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
697         if (f == NULL)
698             f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
699     }
700 }
701
702 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
703 {
704     if (n == NULL)
705         return;
706
707     PUTS("nearest leaf");
708
709     node_t *x = NULL;
710
711     for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
712         if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
713                 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
714                 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
715             if ((dir == NEAREST_OLDER
716                         && (f->client->uid < n->client->uid)
717                         && (x == NULL || f->client->uid > x->client->uid))
718                     || (dir == NEAREST_NEWER
719                         && (f->client->uid > n->client->uid)
720                         && (x == NULL || f->client->uid < x->client->uid)))
721                 x = f;
722
723     focus_node(m, d, x, true);
724 }
725
726 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
727     if (d == NULL || d->root == NULL || is_leaf(d->root))
728         return;
729     node_t *par = d->focus->parent;
730     bool focus_first_child = is_first_child(d->focus);
731     if (dir == CIRCULATE_FORWARD)
732         for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
733             swap_nodes(f, s);
734     else
735         for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
736             swap_nodes(f, s);
737     if (focus_first_child)
738         focus_node(m, d, par->first_child, true);
739     else
740         focus_node(m, d, par->second_child, true);
741 }
742
743 void update_vacant_state(node_t *n)
744 {
745     if (n == NULL)
746         return;
747
748     PUTS("update vacant state");
749
750     /* n is not a leaf */
751     node_t *p = n;
752
753     while (p != NULL) {
754         p->vacant = (p->first_child->vacant && p->second_child->vacant);
755         p = p->parent;
756     }
757 }
758
759 monitor_t *find_monitor(char *name)
760 {
761     for (monitor_t *m = mon_head; m != NULL; m = m->next)
762         if (strcmp(m->name, name) == 0)
763             return m;
764     return NULL;
765 }
766
767 void add_desktop(monitor_t *m, char *name)
768 {
769     desktop_t *d = make_desktop(name);
770     if (m->desk == NULL) {
771         m->desk = d;
772         m->desk_head = d;
773         m->desk_tail = d;
774     } else {
775         m->desk_tail->next = d;
776         d->prev = m->desk_tail;
777         m->desk_tail = d;
778     }
779     num_desktops++;
780     ewmh_update_number_of_desktops();
781     ewmh_update_desktop_names();
782 }
783
784 void add_monitor(xcb_rectangle_t *rect)
785 {
786     monitor_t *m = make_monitor(rect);
787     if (mon == NULL) {
788         mon = m;
789         mon_head = m;
790         mon_tail = m;
791     } else {
792         mon_tail->next = m;
793         m->prev = mon_tail;
794         mon_tail = m;
795     }
796     num_monitors++;
797 }