]> git.lizzy.rs Git - bspwm.git/blob - tree.c
Lower the motion recorder when querying
[bspwm.git] / tree.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <math.h>
6 #include <xcb/xcb.h>
7 #include <xcb/xcb_event.h>
8 #include "settings.h"
9 #include "helpers.h"
10 #include "window.h"
11 #include "types.h"
12 #include "bspwm.h"
13 #include "ewmh.h"
14 #include "tree.h"
15
16 bool is_leaf(node_t *n)
17 {
18     return (n != NULL && n->first_child == NULL && n->second_child == NULL);
19 }
20
21 bool is_tiled(client_t *c)
22 {
23     if (c == NULL)
24         return false;
25     return (!c->floating && !c->transient && !c->fullscreen);
26 }
27
28 bool is_floating(client_t *c)
29 {
30     if (c == NULL)
31         return false;
32     return (c->floating && !c->fullscreen);
33 }
34
35 bool is_first_child(node_t *n)
36 {
37     return (n != NULL && n->parent != NULL && n->parent->first_child == n);
38 }
39
40 bool is_second_child(node_t *n)
41 {
42     return (n != NULL && n->parent != NULL && n->parent->second_child == n);
43 }
44
45 void change_split_ratio(node_t *n, value_change_t chg) {
46     n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
47 }
48
49 node_t *first_extrema(node_t *n)
50 {
51     if (n == NULL)
52         return NULL;
53     else if (n->first_child == NULL)
54         return n;
55     else
56         return first_extrema(n->first_child);
57 }
58
59 node_t *second_extrema(node_t *n)
60 {
61     if (n == NULL)
62         return NULL;
63     else if (n->second_child == NULL)
64         return n;
65     else
66         return second_extrema(n->second_child);
67 }
68
69 node_t *next_leaf(node_t *n)
70 {
71     if (n == NULL)
72         return NULL;
73     node_t *p = n;
74     while (is_second_child(p))
75         p = p->parent;
76     if (p->parent == NULL)
77         return NULL;
78     return first_extrema(p->parent->second_child);
79 }
80
81 node_t *prev_leaf(node_t *n)
82 {
83     if (n == NULL)
84         return NULL;
85     node_t *p = n;
86     while (is_first_child(p))
87         p = p->parent;
88     if (p->parent == NULL)
89         return NULL;
90     return second_extrema(p->parent->first_child);
91 }
92
93 node_t *find_fence(node_t *n, direction_t dir)
94 {
95     node_t *p;
96
97     if (n == NULL)
98         return NULL;
99
100     p = n->parent;
101
102     while (p != NULL) {
103         if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
104                 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
105                 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
106                 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
107             return p;
108         p = p->parent;
109     }
110
111     return NULL;
112 }
113
114 node_t *find_neighbor(node_t *n, direction_t dir)
115 {
116     node_t *fence = find_fence(n, dir);
117
118     if (fence == NULL)
119         return NULL;
120
121     if (dir == DIR_UP || dir == DIR_LEFT)
122         return second_extrema(fence->first_child);
123     else if (dir == DIR_DOWN || dir == DIR_RIGHT)
124         return first_extrema(fence->second_child);
125
126     return NULL;
127 }
128
129 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
130 {
131     node_t *fence = find_fence(n, dir);
132
133     if (fence == NULL)
134         return;
135
136     if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
137             || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
138         change_split_ratio(fence, CHANGE_INCREASE);
139     else
140         change_split_ratio(fence, CHANGE_DECREASE);
141 }
142
143 void rotate_tree(node_t *n, rotate_t rot)
144 {
145     if (n == NULL || is_leaf(n))
146         return;
147
148     node_t *tmp;
149
150     if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
151             || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
152             || rot == ROTATE_FULL_CYCLE) {
153         tmp = n->first_child;
154         n->first_child = n->second_child;
155         n->second_child = tmp;
156         n->split_ratio = 1.0 - n->split_ratio;
157     }
158
159     if (rot != ROTATE_FULL_CYCLE) {
160         if (n->split_type == TYPE_HORIZONTAL)
161             n->split_type = TYPE_VERTICAL;
162         else if (n->split_type == TYPE_VERTICAL)
163             n->split_type = TYPE_HORIZONTAL;
164     }
165
166     rotate_tree(n->first_child, rot);
167     rotate_tree(n->second_child, rot);
168 }
169
170 void flip_tree(node_t *n, flip_t flp)
171 {
172     if (n == NULL || is_leaf(n))
173         return;
174
175     node_t *tmp;
176
177     if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL)
178             || (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
179         tmp = n->first_child;
180         n->first_child = n->second_child;
181         n->second_child = tmp;
182         n->split_ratio = 1.0 - n->split_ratio;
183     }
184
185     flip_tree(n->first_child, flp);
186     flip_tree(n->second_child, flp);
187 }
188
189 void arrange(monitor_t *m, desktop_t *d)
190 {
191     PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
192
193     xcb_rectangle_t rect = m->rectangle;
194     int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
195     rect.x += m->left_padding + wg;
196     rect.y += m->top_padding + wg;
197     rect.width -= m->left_padding + m->right_padding + wg;
198     rect.height -= m->top_padding + m->bottom_padding + wg;
199     apply_layout(m, d, d->root, rect, rect);
200 }
201
202 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
203 {
204     if (n == NULL)
205         return;
206
207     n->rectangle = rect;
208
209     if (is_leaf(n)) {
210         if (n->client->fullscreen)
211             return;
212
213         if (is_floating(n->client) && n->client->border_width != border_width) {
214             int ds = 2 * (border_width - n->client->border_width);
215             n->client->floating_rectangle.width += ds;
216             n->client->floating_rectangle.height += ds;
217         }
218
219         if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
220             n->client->border_width = 0;
221         else
222             n->client->border_width = border_width;
223
224         xcb_rectangle_t r;
225         if (is_tiled(n->client)) {
226             if (d->layout == LAYOUT_TILED)
227                 r = rect;
228             else if (d->layout == LAYOUT_MONOCLE)
229                 r = root_rect;
230             int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
231             int bleed = wg + 2 * n->client->border_width;
232             r.width = (bleed < r.width ? r.width - bleed : 1);
233             r.height = (bleed < r.height ? r.height - bleed : 1);
234             n->client->tiled_rectangle = r;
235         } else {
236             r = n->client->floating_rectangle;
237         }
238
239         window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
240         window_border_width(n->client->window, n->client->border_width);
241         window_draw_border(n, n == d->focus, m == mon);
242
243     } else {
244         xcb_rectangle_t first_rect;
245         xcb_rectangle_t second_rect;
246
247         if (n->first_child->vacant || n->second_child->vacant) {
248             first_rect = second_rect = rect;
249         } else {
250             unsigned int fence;
251             if (n->split_type == TYPE_VERTICAL) {
252                 fence = rect.width * n->split_ratio;
253                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
254                 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
255
256             } else if (n->split_type == TYPE_HORIZONTAL) {
257                 fence = rect.height * n->split_ratio;
258                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
259                 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
260             }
261         }
262
263         apply_layout(m, d, n->first_child, first_rect, root_rect);
264         apply_layout(m, d, n->second_child, second_rect, root_rect);
265     }
266 }
267
268 void insert_node(monitor_t *m, desktop_t *d, node_t *n)
269 {
270     if (d == NULL || n == NULL)
271         return;
272
273     PRINTF("insert node %X\n", n->client->window);
274
275     node_t *focus = d->focus;
276
277     if (focus == NULL) {
278         d->root = n;
279     } else {
280         node_t *dad = make_node();
281         node_t *fopar = focus->parent;
282         n->parent = dad;
283         n->client->born_as = split_mode;
284         switch (split_mode) {
285             case MODE_AUTOMATIC:
286                 if (fopar == NULL) {
287                     dad->first_child = n;
288                     dad->second_child = focus;
289                     if (m->rectangle.width > m->rectangle.height)
290                         dad->split_type = TYPE_VERTICAL;
291                     else
292                         dad->split_type = TYPE_HORIZONTAL;
293                     focus->parent = dad;
294                     d->root = dad;
295                 } else {
296                     node_t *grandpa = fopar->parent;
297                     dad->parent = grandpa;
298                     if (grandpa != NULL) {
299                         if (is_first_child(fopar))
300                             grandpa->first_child = dad;
301                         else
302                             grandpa->second_child = dad;
303                     } else {
304                         d->root = dad;
305                     }
306                     dad->split_type = fopar->split_type;
307                     dad->split_ratio = fopar->split_ratio;
308                     fopar->parent = dad;
309                     if (is_first_child(focus)) {
310                         dad->first_child = n;
311                         dad->second_child = fopar;
312                         rotate_tree(fopar, ROTATE_CLOCKWISE);
313                     } else {
314                         dad->first_child = fopar;
315                         dad->second_child = n;
316                         rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
317                     }
318                 }
319                 break;
320             case MODE_MANUAL:
321                 if (fopar != NULL) {
322                     if (is_first_child(focus))
323                         fopar->first_child = dad;
324                     else
325                         fopar->second_child = dad;
326                 }
327                 dad->split_ratio = focus->split_ratio;
328                 dad->parent = fopar;
329                 focus->parent = dad;
330                 switch (split_dir) {
331                     case DIR_LEFT:
332                         dad->split_type = TYPE_VERTICAL;
333                         dad->first_child = n;
334                         dad->second_child = focus;
335                         break;
336                     case DIR_RIGHT:
337                         dad->split_type = TYPE_VERTICAL;
338                         dad->first_child = focus;
339                         dad->second_child = n;
340                         break;
341                     case DIR_UP:
342                         dad->split_type = TYPE_HORIZONTAL;
343                         dad->first_child = n;
344                         dad->second_child = focus;
345                         break;
346                     case DIR_DOWN:
347                         dad->split_type = TYPE_HORIZONTAL;
348                         dad->first_child = focus;
349                         dad->second_child = n;
350                         break;
351                 }
352                 if (d->root == focus)
353                     d->root = dad;
354                 split_mode = MODE_AUTOMATIC;
355                 break;
356         }
357         if (focus->vacant)
358             update_vacant_state(fopar);
359     }
360 }
361
362 void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
363 {
364     if (n == NULL)
365         return;
366
367     PRINTF("focus node %X\n", n->client->window);
368
369     split_mode = MODE_AUTOMATIC;
370     n->client->urgent = false;
371
372     if (is_mapped) {
373         if (mon != m) {
374             for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
375                 window_draw_border(cd->focus, true, false);
376             for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
377                 if (cd != d)
378                     window_draw_border(cd->focus, true, true);
379             if (d->focus == n)
380                 window_draw_border(n, true, true);
381         }
382         if (d->focus != n) {
383             window_draw_border(d->focus, false, true);
384             window_draw_border(n, true, true);
385         }
386         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
387     }
388
389     if (focus_follows_pointer) {
390         xcb_window_t win = XCB_NONE;
391         query_pointer(&win, NULL);
392         if (win != n->client->window)
393             enable_motion_recorder();
394         else
395             disable_motion_recorder();
396     }
397
398     if (!is_tiled(n->client)) {
399         if (!adaptative_raise || !might_cover(d, n))
400             window_raise(n->client->window);
401     } else {
402         window_pseudo_raise(d, n->client->window);
403     }
404
405     if (d->focus != n) {
406         d->last_focus = d->focus;
407         d->focus = n;
408     }
409
410     ewmh_update_active_window();
411     put_status();
412 }
413
414 void update_current(void)
415 {
416     if (mon->desk->focus == NULL)
417         ewmh_update_active_window();
418     else
419         focus_node(mon, mon->desk, mon->desk->focus, true);
420     put_status();
421 }
422
423 void unlink_node(desktop_t *d, node_t *n)
424 {
425     if (d == NULL || n == NULL)
426         return;
427
428     PRINTF("unlink node %X\n", n->client->window);
429
430     node_t *p = n->parent;
431
432     if (p == NULL) {
433         d->root = NULL;
434         d->focus = NULL;
435         d->last_focus = NULL;
436     } else {
437         node_t *b;
438         node_t *g = p->parent;
439         bool n_first_child = is_first_child(n);
440         if (n_first_child) {
441             b = p->second_child;
442             if (n->client->born_as == MODE_AUTOMATIC)
443                 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
444         } else {
445             b = p->first_child;
446             if (n->client->born_as == MODE_AUTOMATIC)
447                 rotate_tree(b, ROTATE_CLOCKWISE);
448         }
449         b->parent = g;
450         if (g != NULL) {
451             if (is_first_child(p))
452                 g->first_child = b;
453             else
454                 g->second_child = b;
455         } else {
456             d->root = b;
457         }
458
459         n->parent = NULL;
460         free(p);
461
462         if (n == d->last_focus) {
463             d->last_focus = NULL;
464         } else if (n == d->focus) {
465             if (d->last_focus != NULL)
466                 d->focus = d->last_focus;
467             else
468                 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
469             d->last_focus = NULL;
470         }
471
472         update_vacant_state(b->parent);
473     }
474 }
475
476 void remove_node(desktop_t *d, node_t *n)
477 {
478     if (d == NULL || n == NULL)
479         return;
480
481     PRINTF("remove node %X\n", n->client->window);
482
483     unlink_node(d, n);
484     free(n->client);
485     free(n);
486
487     num_clients--;
488     ewmh_update_client_list();
489
490     if (mon->desk == d)
491         update_current();
492 }
493
494 void destroy_tree(node_t *n)
495 {
496     if (n == NULL)
497         return;
498     node_t *first_tree = n->first_child;
499     node_t *second_tree = n->second_child;
500     if (n->client != NULL)
501         free(n->client);
502     free(n);
503     destroy_tree(first_tree);
504     destroy_tree(second_tree);
505 }
506
507 void swap_nodes(desktop_t *d1, node_t *n1, desktop_t *d2, node_t *n2)
508 {
509     if (n1 == NULL || n2 == NULL || n1 == n2)
510         return;
511
512     PUTS("swap nodes");
513
514     /* (n1 and n2 are leaves) */
515     node_t *pn1 = n1->parent;
516     node_t *pn2 = n2->parent;
517     bool n1_first_child = is_first_child(n1);
518     bool n2_first_child = is_first_child(n2);
519
520     if (pn1 != NULL) {
521         if (n1_first_child)
522             pn1->first_child = n2;
523         else
524             pn1->second_child = n2;
525     }
526
527     if (pn2 != NULL) {
528         if (n2_first_child)
529             pn2->first_child = n1;
530         else
531             pn2->second_child = n1;
532     }
533
534     n1->parent = pn2;
535     n2->parent = pn1;
536
537     if (n1->vacant != n2->vacant) {
538         update_vacant_state(n1->parent);
539         update_vacant_state(n2->parent);
540     }
541
542     if (d1 != d2) {
543         if (d1->root == n1)
544             d1->root = n2;
545         if (d1->focus == n1)
546             d1->focus = n2;
547         if (d1->last_focus == n1)
548             d1->last_focus = n2;
549         if (d2->root == n2)
550             d2->root = n1;
551         if (d2->focus == n2)
552             d2->focus = n1;
553         if (d2->last_focus == n2)
554             d2->last_focus = n1;
555     }
556 }
557
558 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
559 {
560     if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
561         return;
562
563     PRINTF("transfer node %X\n", n->client->window);
564
565     unlink_node(ds, n);
566     insert_node(md, dd, n);
567     ewmh_set_wm_desktop(n, dd);
568
569     if (ds == ms->desk && dd != md->desk) {
570         window_hide(n->client->window);
571     }
572
573     fit_monitor(md, n->client);
574
575     if (n->client->fullscreen)
576         window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
577
578     if (ds != ms->desk && dd == md->desk) {
579         window_show(n->client->window);
580         focus_node(md, dd, n, true);
581     } else {
582         focus_node(md, dd, n, false);
583     }
584
585     if (ds == ms->desk || dd == md->desk)
586         update_current();
587 }
588
589 void select_monitor(monitor_t *m)
590 {
591     if (m == NULL || mon == m)
592         return;
593
594     PRINTF("select monitor %s\n", m->name);
595
596     focus_node(m, m->desk, m->desk->focus, true);
597
598     last_mon = mon;
599     mon = m;
600
601     ewmh_update_current_desktop();
602     put_status();
603 }
604
605 void select_desktop(desktop_t *d)
606 {
607     if (d == NULL || d == mon->desk)
608         return;
609
610     PRINTF("select desktop %s\n", d->name);
611
612     if (visible) {
613         node_t *n = first_extrema(d->root);
614
615         while (n != NULL) {
616             window_show(n->client->window);
617             n = next_leaf(n);
618         }
619
620         n = first_extrema(mon->desk->root);
621
622         while (n != NULL) {
623             window_hide(n->client->window);
624             n = next_leaf(n);
625         }
626     }
627
628     mon->last_desk = mon->desk;
629     mon->desk = d;
630
631     update_current();
632     ewmh_update_current_desktop();
633     put_status();
634 }
635
636 void cycle_monitor(cycle_dir_t dir)
637 {
638     if (dir == CYCLE_NEXT)
639         select_monitor((mon->next == NULL ? mon_head : mon->next));
640     else if (dir == CYCLE_PREV)
641         select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
642 }
643
644 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
645 {
646     desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
647     if (f == NULL)
648         f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
649
650     while (f != d) {
651         if (skip == DESKTOP_SKIP_NONE 
652                 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
653                 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
654             select_desktop(f);
655             return;
656         }
657         f = (dir == CYCLE_PREV ? f->prev : f->next);
658         if (f == NULL)
659             f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
660     }
661 }
662
663 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
664 {
665     if (n == NULL)
666         return;
667
668     PUTS("cycle leaf");
669
670     node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
671     if (f == NULL)
672         f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
673
674     while (f != n) {
675         bool tiled = is_tiled(f->client);
676         if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
677                 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
678                 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
679             focus_node(m, d, f, true);
680             return;
681         }
682         f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
683         if (f == NULL)
684             f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
685     }
686 }
687
688 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
689 {
690     if (n == NULL)
691         return;
692
693     PUTS("nearest leaf");
694
695     node_t *x = NULL;
696
697     for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
698         if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
699                 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
700                 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
701             if ((dir == NEAREST_OLDER
702                         && (f->client->uid < n->client->uid)
703                         && (x == NULL || f->client->uid > x->client->uid))
704                     || (dir == NEAREST_NEWER
705                         && (f->client->uid > n->client->uid)
706                         && (x == NULL || f->client->uid < x->client->uid)))
707                 x = f;
708
709     focus_node(m, d, x, true);
710 }
711
712 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
713     if (d == NULL || d->root == NULL || is_leaf(d->root))
714         return;
715     node_t *par = d->focus->parent;
716     bool focus_first_child = is_first_child(d->focus);
717     if (dir == CIRCULATE_FORWARD)
718         for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
719             swap_nodes(d, f, d, s);
720     else
721         for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
722             swap_nodes(d, f, d, s);
723     if (focus_first_child)
724         focus_node(m, d, par->first_child, true);
725     else
726         focus_node(m, d, par->second_child, true);
727 }
728
729 void update_vacant_state(node_t *n)
730 {
731     if (n == NULL)
732         return;
733
734     PUTS("update vacant state");
735
736     /* n is not a leaf */
737     node_t *p = n;
738
739     while (p != NULL) {
740         p->vacant = (p->first_child->vacant && p->second_child->vacant);
741         p = p->parent;
742     }
743 }
744
745 void fit_monitor(monitor_t *m, client_t *c)
746 {
747     xcb_rectangle_t crect = c->floating_rectangle;
748     xcb_rectangle_t mrect = m->rectangle;
749     while (crect.x < mrect.x)
750         crect.x += mrect.width;
751     while (crect.x > (mrect.x + mrect.width - 1))
752         crect.x -= mrect.width;
753     while (crect.y < mrect.y)
754         crect.y += mrect.height;
755     while (crect.y > (mrect.y + mrect.height - 1))
756         crect.y -= mrect.height;
757     c->floating_rectangle = crect;
758 }
759
760 void put_status(void)
761 {
762     if (status_fifo == NULL)
763         return;
764     bool urgent = false;
765     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
766         fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
767         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
768             for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n))
769                 urgent |= n->client->urgent;
770             fprintf(status_fifo, "%c%c%s:", (m->desk == d ? 'D' : (d->root != NULL ? 'd' : '_')), (urgent ? '!' : '_'), d->name);
771         }
772     }
773     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));
774     fflush(status_fifo);
775 }
776
777 void list_monitors(list_option_t opt, char *rsp)
778 {
779     char line[MAXLEN];
780     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
781         snprintf(line, sizeof(line), "%s %ux%u%+i%+i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y);
782         strncat(rsp, line, REMLEN(rsp));
783         if (m == mon)
784             strncat(rsp, " #\n", REMLEN(rsp));
785         else if (m == last_mon)
786             strncat(rsp, " ~\n", REMLEN(rsp));
787         else
788             strncat(rsp, "\n", REMLEN(rsp));
789         if (opt == LIST_OPTION_VERBOSE)
790             list_desktops(m, opt, 1, rsp);
791     }
792 }
793
794 void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
795 {
796     char line[MAXLEN];
797     for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
798         for (unsigned int i = 0; i < depth; i++)
799             strncat(rsp, "  ", REMLEN(rsp));
800         snprintf(line, sizeof(line), "%s %c", d->name, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
801         strncat(rsp, line, REMLEN(rsp));
802         if (d == m->desk)
803             strncat(rsp, " @\n", REMLEN(rsp));
804         else if (d == m->last_desk)
805             strncat(rsp, " ~\n", REMLEN(rsp));
806         else
807             strncat(rsp, "\n", REMLEN(rsp));
808         if (opt == LIST_OPTION_VERBOSE)
809             list(d, d->root, rsp, depth + 1);
810     }
811 }
812
813 void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
814 {
815     if (n == NULL)
816         return;
817
818     char line[MAXLEN];
819
820     for (unsigned int i = 0; i < depth; i++)
821         strncat(rsp, "  ", REMLEN(rsp));
822
823     if (is_leaf(n)) {
824         client_t *c = n->client;
825         snprintf(line, sizeof(line), "%c %s %X %u %u %ux%u%+i%+i %c%c%c%c%c", (c->born_as == MODE_AUTOMATIC ? 'a' : 'm'), c->class_name, c->window, c->uid, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'));
826     } else {
827         snprintf(line, sizeof(line), "%c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), n->split_ratio);
828     }
829
830     strncat(rsp, line, REMLEN(rsp));
831
832     if (n == d->focus)
833         strncat(rsp, " *\n", REMLEN(rsp));
834     else if (n == d->last_focus)
835         strncat(rsp, " ~\n", REMLEN(rsp));
836     else
837         strncat(rsp, "\n", REMLEN(rsp));
838
839     list(d, n->first_child, rsp, depth + 1);
840     list(d, n->second_child, rsp, depth + 1);
841 }
842
843 void restore(char *file_path)
844 {
845     if (file_path == NULL)
846         return;
847
848     FILE *snapshot = fopen(file_path, "r");
849     if (snapshot == NULL) {
850         warn("restore: can't open file\n");
851         return;
852     }
853
854     char line[MAXLEN];
855     monitor_t *m = NULL;
856     desktop_t *d = NULL;
857     node_t *n = NULL;
858     num_clients = 0;
859     unsigned int level, last_level = 0, max_uid = 0;
860     bool aborted = false;
861
862     while (!aborted && fgets(line, sizeof(line), snapshot) != NULL) {
863         unsigned int len = strlen(line);
864         level = 0;
865         while (level < strlen(line) && isspace(line[level]))
866             level++;
867         if (level == 0) {
868             if (m == NULL)
869                 m = mon_head;
870             else
871                 m = m->next;
872             if (len >= 2)
873                 switch (line[len - 2]) {
874                     case '#':
875                         mon = m;
876                         break;
877                     case '~':
878                         last_mon = m;
879                         break;
880                 }
881         } else if (level == 2) {
882             if (d == NULL)
883                 d = m->desk_head;
884             else
885                 d = d->next;
886             int i = len - 1;
887             while (i > 0 && !isupper(line[i]))
888                 i--;
889             if (line[i] == 'M')
890                 d->layout = LAYOUT_MONOCLE;
891             else if (line[i] == 'T')
892                 d->layout = LAYOUT_TILED;
893             if (len >= 2)
894                 switch (line[len - 2]) {
895                     case '@':
896                         m->desk = d;
897                         break;
898                     case '~':
899                         m->last_desk = d;
900                         break;
901                 }
902         } else {
903             node_t *birth = make_node();
904             if (level == 4) {
905                 empty_desktop(d);
906                 d->root = birth;
907             } else {
908                 if (level > last_level) {
909                     n->first_child = birth;
910                 } else {
911                     do {
912                         n = n->parent;
913                     } while (n != NULL && n->second_child != NULL);
914                     if (n == NULL) {
915                         warn("restore: file is malformed\n");
916                         aborted = true;
917                     }
918                     n->second_child = birth;
919                 }
920                 birth->parent = n;
921             }
922             n = birth;
923
924             if (isupper(line[level])) {
925                 char st;
926                 sscanf(line + level, "%c %lf", &st, &n->split_ratio);
927                 if (st == 'H')
928                     n->split_type = TYPE_HORIZONTAL;
929                 else if (st == 'V')
930                     n->split_type = TYPE_VERTICAL;
931             } else {
932                 client_t *c = make_client(XCB_NONE);
933                 num_clients++;
934                 char ba, floating, transient, fullscreen, urgent, locked;
935                 sscanf(line + level, "%c %s %X %u %u %hux%hu%hi%hi %c%c%c%c%c", &ba, c->class_name, &c->window, &c->uid, &c->border_width, &c->floating_rectangle.width, &c->floating_rectangle.height, &c->floating_rectangle.x, &c->floating_rectangle.y, &floating, &transient, &fullscreen, &urgent, &locked);
936                 if (ba == 'a')
937                     c->born_as = MODE_AUTOMATIC;
938                 else if (ba == 'm')
939                     c->born_as = MODE_MANUAL;
940                 c->floating = (floating == '-' ? false : true);
941                 c->transient = (transient == '-' ? false : true);
942                 c->fullscreen = (fullscreen == '-' ? false : true);
943                 c->urgent = (urgent == '-' ? false : true);
944                 c->locked = (locked == '-' ? false : true);
945                 if (c->uid > max_uid)
946                     max_uid = c->uid;
947                 n->client = c;
948                 if (len >= 2)
949                     switch (line[len - 2]) {
950                         case '*':
951                             d->focus = n;
952                             break;
953                         case '~':
954                             d->last_focus = n;
955                             break;
956                     }
957             }
958         }
959         last_level = level;
960     }
961
962     if (!aborted) {
963         client_uid = max_uid + 1;
964         for (monitor_t *m = mon_head; m != NULL; m = m->next)
965             for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
966                 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n)) {
967                     uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
968                     xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
969                     if (n->client->floating) {
970                         n->vacant = true;
971                         update_vacant_state(n->parent);
972                     }
973                 }
974     }
975
976     fclose(snapshot);
977 }