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