+
+ PUTS("cycle leaf");
+
+ node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
+ if (f == NULL)
+ f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
+
+ while (f != n) {
+ bool tiled = is_tiled(f->client);
+ if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
+ || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
+ || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
+ focus_node(m, d, f, true);
+ return;
+ }
+ f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
+ if (f == NULL)
+ f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
+ }
+}
+
+void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
+{
+ if (n == NULL)
+ return;
+
+ PUTS("nearest leaf");
+
+ node_t *x = NULL;
+
+ for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
+ if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
+ || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
+ || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
+ if ((dir == NEAREST_OLDER
+ && (f->client->uid < n->client->uid)
+ && (x == NULL || f->client->uid > x->client->uid))
+ || (dir == NEAREST_NEWER
+ && (f->client->uid > n->client->uid)
+ && (x == NULL || f->client->uid < x->client->uid)))
+ x = f;
+
+ focus_node(m, d, x, true);
+}
+
+void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
+ if (d == NULL || d->root == NULL || is_leaf(d->root))
+ return;
+ node_t *par = d->focus->parent;
+ bool focus_first_child = is_first_child(d->focus);
+ if (dir == CIRCULATE_FORWARD)
+ for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
+ swap_nodes(f, s);
+ else
+ for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
+ swap_nodes(f, s);
+ if (focus_first_child)
+ focus_node(m, d, par->first_child, true);
+ else
+ focus_node(m, d, par->second_child, true);
+}
+
+void update_vacant_state(node_t *n)
+{
+ if (n == NULL)
+ return;
+
+ PUTS("update vacant state");
+
+ /* n is not a leaf */
+ node_t *p = n;
+
+ while (p != NULL) {
+ p->vacant = (p->first_child->vacant && p->second_child->vacant);
+ p = p->parent;
+ }
+}
+
+void fit_monitor(monitor_t *m, client_t *c)
+{
+ xcb_rectangle_t crect = c->floating_rectangle;
+ xcb_rectangle_t mrect = m->rectangle;
+ while (crect.x < mrect.x)
+ crect.x += mrect.width;
+ while (crect.x > (mrect.x + mrect.width - 1))
+ crect.x -= mrect.width;
+ while (crect.y < mrect.y)
+ crect.y += mrect.height;
+ while (crect.y > (mrect.y + mrect.height - 1))
+ crect.y -= mrect.height;
+ c->floating_rectangle = crect;
+}
+
+void put_status(void)
+{
+ if (status_fifo == NULL)
+ return;
+ bool urgent = false;
+ for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+ fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
+ for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
+ for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n))
+ urgent |= n->client->urgent;
+ fprintf(status_fifo, "%c%c%s:", (m->desk == d ? 'D' : (d->root != NULL ? 'd' : '_')), (urgent ? '!' : '_'), d->name);
+ }
+ }
+ 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));
+ fflush(status_fifo);
+}
+
+void list_monitors(list_option_t opt, char *rsp)
+{
+ char line[MAXLEN];
+ for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+ snprintf(line, sizeof(line), "%s %ux%u%+i%+i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y);
+ strncat(rsp, line, REMLEN(rsp));
+ if (m == mon)
+ strncat(rsp, " #\n", REMLEN(rsp));
+ else if (m == last_mon)
+ strncat(rsp, " ~\n", REMLEN(rsp));
+ else
+ strncat(rsp, "\n", REMLEN(rsp));
+ if (opt == LIST_OPTION_VERBOSE)
+ list_desktops(m, opt, 1, rsp);
+ }
+}
+
+void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
+{
+ char line[MAXLEN];
+ for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+ for (unsigned int i = 0; i < depth; i++)
+ strncat(rsp, " ", REMLEN(rsp));
+ snprintf(line, sizeof(line), "%s %c", d->name, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
+ strncat(rsp, line, REMLEN(rsp));
+ if (d == m->desk)
+ strncat(rsp, " @\n", REMLEN(rsp));
+ else if (d == m->last_desk)
+ strncat(rsp, " ~\n", REMLEN(rsp));
+ else
+ strncat(rsp, "\n", REMLEN(rsp));
+ if (opt == LIST_OPTION_VERBOSE)
+ list(d, d->root, rsp, depth + 1);
+ }
+}
+
+void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
+{
+ if (n == NULL)
+ return;
+
+ char line[MAXLEN];
+
+ for (unsigned int i = 0; i < depth; i++)
+ strncat(rsp, " ", REMLEN(rsp));
+
+ if (is_leaf(n)) {
+ client_t *c = n->client;
+ 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' : '-'));
+ } else {
+ snprintf(line, sizeof(line), "%c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), n->split_ratio);
+ }
+
+ strncat(rsp, line, REMLEN(rsp));
+
+ if (n == d->focus)
+ strncat(rsp, " *\n", REMLEN(rsp));
+ else if (n == d->last_focus)
+ strncat(rsp, " ~\n", REMLEN(rsp));
+ else
+ strncat(rsp, "\n", REMLEN(rsp));
+
+ list(d, n->first_child, rsp, depth + 1);
+ list(d, n->second_child, rsp, depth + 1);
+}
+
+void restore(char *file_path)
+{
+ if (file_path == NULL)
+ return;
+
+ FILE *snapshot = fopen(file_path, "r");
+ if (snapshot == NULL) {
+ warn("restore: can't open file\n");
+ return;
+ }
+
+ char line[MAXLEN];
+ monitor_t *m = NULL;
+ desktop_t *d = NULL;
+ node_t *n = NULL;
+ num_clients = 0;
+ unsigned int level, last_level = 0, max_uid = 0;
+ bool aborted = false;
+
+ while (!aborted && fgets(line, sizeof(line), snapshot) != NULL) {
+ unsigned int len = strlen(line);
+ level = 0;
+ while (level < strlen(line) && isspace(line[level]))
+ level++;
+ if (level == 0) {
+ if (m == NULL)
+ m = mon_head;
+ else
+ m = m->next;
+ if (len >= 2)
+ switch (line[len - 2]) {
+ case '#':
+ mon = m;
+ break;
+ case '~':
+ last_mon = m;
+ break;
+ }
+ } else if (level == 2) {
+ if (d == NULL)
+ d = m->desk_head;
+ else
+ d = d->next;
+ int i = len - 1;
+ while (i > 0 && !isupper(line[i]))
+ i--;
+ if (line[i] == 'M')
+ d->layout = LAYOUT_MONOCLE;
+ else if (line[i] == 'T')
+ d->layout = LAYOUT_TILED;
+ if (len >= 2)
+ switch (line[len - 2]) {
+ case '@':
+ m->desk = d;
+ break;
+ case '~':
+ m->last_desk = d;
+ break;
+ }
+ } else {
+ node_t *birth = make_node();
+ if (level == 4) {
+ empty_desktop(d);
+ d->root = birth;
+ } else {
+ if (level > last_level) {
+ n->first_child = birth;
+ } else {
+ do {
+ n = n->parent;
+ } while (n != NULL && n->second_child != NULL);
+ if (n == NULL) {
+ warn("restore: file is malformed\n");
+ aborted = true;
+ }
+ n->second_child = birth;
+ }
+ birth->parent = n;
+ }
+ n = birth;
+
+ if (isupper(line[level])) {
+ char st;
+ sscanf(line + level, "%c %lf", &st, &n->split_ratio);
+ if (st == 'H')
+ n->split_type = TYPE_HORIZONTAL;
+ else if (st == 'V')
+ n->split_type = TYPE_VERTICAL;
+ } else {
+ client_t *c = make_client(XCB_NONE);
+ num_clients++;
+ char ba, floating, transient, fullscreen, urgent, locked;
+ 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);
+ if (ba == 'a')
+ c->born_as = MODE_AUTOMATIC;
+ else if (ba == 'm')
+ c->born_as = MODE_MANUAL;
+ c->floating = (floating == '-' ? false : true);
+ c->transient = (transient == '-' ? false : true);
+ c->fullscreen = (fullscreen == '-' ? false : true);
+ c->urgent = (urgent == '-' ? false : true);
+ c->locked = (locked == '-' ? false : true);
+ if (c->uid > max_uid)
+ max_uid = c->uid;
+ n->client = c;
+ if (len >= 2)
+ switch (line[len - 2]) {
+ case '*':
+ d->focus = n;
+ break;
+ case '~':
+ d->last_focus = n;
+ break;
+ }
+ }
+ }
+ last_level = level;
+ }
+
+ if (!aborted) {
+ client_uid = max_uid + 1;
+ for (monitor_t *m = mon_head; m != NULL; m = m->next)
+ for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
+ for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n)) {
+ uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
+ xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
+ if (n->client->floating) {
+ n->vacant = true;
+ update_vacant_state(n->parent);
+ }
+ }
+ }
+
+ fclose(snapshot);