-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <math.h>
+/* Copyright (c) 2012, Bastien Dejean
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
#include <float.h>
-#include <xcb/xcb.h>
-#include <xcb/xcb_event.h>
-#include "settings.h"
-#include "helpers.h"
-#include "window.h"
-#include "types.h"
+#include <limits.h>
#include "bspwm.h"
+#include "desktop.h"
#include "ewmh.h"
+#include "history.h"
+#include "monitor.h"
+#include "query.h"
+#include "subscribe.h"
+#include "settings.h"
+#include "stack.h"
+#include "window.h"
#include "tree.h"
-bool is_leaf(node_t *n)
+void arrange(monitor_t *m, desktop_t *d)
{
- return (n != NULL && n->first_child == NULL && n->second_child == NULL);
+ if (d->root == NULL)
+ return;
+
+ PRINTF("arrange %s %s\n", m->name, d->name);
+
+ xcb_rectangle_t rect = m->rectangle;
+ int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
+ rect.x += m->left_padding + d->left_padding + wg;
+ rect.y += m->top_padding + d->top_padding + wg;
+ rect.width -= m->left_padding + d->left_padding + d->right_padding + m->right_padding + wg;
+ rect.height -= m->top_padding + d->top_padding + d->bottom_padding + m->bottom_padding + wg;
+ apply_layout(m, d, d->root, rect, rect);
}
-bool is_tiled(client_t *c)
+void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
{
- if (c == NULL)
- return false;
- return (!c->floating && !c->transient && !c->fullscreen);
+ if (n == NULL)
+ return;
+
+ n->rectangle = rect;
+
+ if (is_leaf(n)) {
+
+ unsigned int bw;
+ if ((borderless_monocle && is_tiled(n->client) &&
+ !n->client->pseudo_tiled &&
+ d->layout == LAYOUT_MONOCLE) ||
+ n->client->fullscreen)
+ bw = 0;
+ else
+ bw = n->client->border_width;
+
+ xcb_rectangle_t r;
+ if (!n->client->fullscreen) {
+ if (!n->client->floating) {
+ int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
+ if (n->client->pseudo_tiled) {
+ /* pseudo-tiled clients */
+ r = n->client->floating_rectangle;
+ if (center_pseudo_tiled) {
+ r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
+ r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
+ } else {
+ r.x = rect.x;
+ r.y = rect.y;
+ }
+ } else {
+ /* tiled clients */
+ r = rect;
+ int bleed = wg + 2 * bw;
+ r.width = (bleed < r.width ? r.width - bleed : 1);
+ r.height = (bleed < r.height ? r.height - bleed : 1);
+ }
+ n->client->tiled_rectangle = r;
+ } else {
+ /* floating clients */
+ r = n->client->floating_rectangle;
+ }
+ } else {
+ /* fullscreen clients */
+ r = m->rectangle;
+ }
+
+ window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
+ window_border_width(n->client->window, bw);
+ window_draw_border(n, d->focus == n, m == mon);
+
+ if (pointer_follows_focus && mon->desk->focus == n && frozen_pointer->action == ACTION_NONE) {
+ center_pointer(r);
+ }
+
+ } else {
+ xcb_rectangle_t first_rect;
+ xcb_rectangle_t second_rect;
+
+ if (d->layout == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
+ first_rect = second_rect = rect;
+ } else {
+ unsigned int fence;
+ if (n->split_type == TYPE_VERTICAL) {
+ fence = rect.width * n->split_ratio;
+ first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
+ second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
+ } else {
+ fence = rect.height * n->split_ratio;
+ first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
+ second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
+ }
+ }
+
+ apply_layout(m, d, n->first_child, first_rect, root_rect);
+ apply_layout(m, d, n->second_child, second_rect, root_rect);
+ }
}
-bool is_floating(client_t *c)
+void insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
{
- if (c == NULL)
- return false;
- return (c->floating && !c->fullscreen);
+ if (d == NULL || n == NULL)
+ return;
+
+ PRINTF("insert node %X\n", n->client->window);
+
+ /* n: new leaf node */
+ /* c: new container node */
+ /* f: focus or insertion anchor */
+ /* p: parent of focus */
+ /* g: grand parent of focus */
+
+ if (f == NULL)
+ f = d->root;
+
+ if (f == NULL) {
+ d->root = n;
+ } else {
+ node_t *c = make_node();
+ node_t *p = f->parent;
+ if ((f->client->private ||
+ (p != NULL && p->privacy_level > 0)) &&
+ f->split_mode == MODE_AUTOMATIC) {
+ node_t *closest = NULL;
+ node_t *public = NULL;
+ closest_public(d, f, &closest, &public);
+ if (public != NULL) {
+ f = public;
+ p = f->parent;
+ } else {
+ if (closest != NULL) {
+ f = closest;
+ p = f->parent;
+ }
+ f->split_mode = MODE_MANUAL;
+ xcb_rectangle_t rect = f->client->tiled_rectangle;
+ f->split_dir = (rect.width >= rect.height ? DIR_LEFT : DIR_UP);
+ if (f->client->private) {
+ get_opposite(f->split_dir, &f->split_dir);
+ update_privacy_level(f, false);
+ }
+ }
+ }
+ if (p != NULL && f->split_mode == MODE_AUTOMATIC &&
+ (p->first_child->vacant || p->second_child->vacant)) {
+ f = p;
+ p = f->parent;
+ }
+ n->parent = c;
+ c->birth_rotation = f->birth_rotation;
+ switch (f->split_mode) {
+ case MODE_AUTOMATIC:
+ if (p == NULL) {
+ if (initial_polarity == FIRST_CHILD) {
+ c->first_child = n;
+ c->second_child = f;
+ } else {
+ c->first_child = f;
+ c->second_child = n;
+ }
+ if (m->rectangle.width > m->rectangle.height)
+ c->split_type = TYPE_VERTICAL;
+ else
+ c->split_type = TYPE_HORIZONTAL;
+ f->parent = c;
+ d->root = c;
+ } else {
+ node_t *g = p->parent;
+ c->parent = g;
+ if (g != NULL) {
+ if (is_first_child(p))
+ g->first_child = c;
+ else
+ g->second_child = c;
+ } else {
+ d->root = c;
+ }
+ c->split_type = p->split_type;
+ c->split_ratio = p->split_ratio;
+ p->parent = c;
+ int rot;
+ if (is_first_child(f)) {
+ c->first_child = n;
+ c->second_child = p;
+ rot = 90;
+ } else {
+ c->first_child = p;
+ c->second_child = n;
+ rot = 270;
+ }
+ if (!is_floating(n->client))
+ rotate_tree(p, rot);
+ n->birth_rotation = rot;
+ }
+ break;
+ case MODE_MANUAL:
+ if (p != NULL) {
+ if (is_first_child(f))
+ p->first_child = c;
+ else
+ p->second_child = c;
+ }
+ c->split_ratio = f->split_ratio;
+ c->parent = p;
+ f->parent = c;
+ f->birth_rotation = 0;
+ switch (f->split_dir) {
+ case DIR_LEFT:
+ c->split_type = TYPE_VERTICAL;
+ c->first_child = n;
+ c->second_child = f;
+ break;
+ case DIR_RIGHT:
+ c->split_type = TYPE_VERTICAL;
+ c->first_child = f;
+ c->second_child = n;
+ break;
+ case DIR_UP:
+ c->split_type = TYPE_HORIZONTAL;
+ c->first_child = n;
+ c->second_child = f;
+ break;
+ case DIR_DOWN:
+ c->split_type = TYPE_HORIZONTAL;
+ c->first_child = f;
+ c->second_child = n;
+ break;
+ }
+ if (d->root == f)
+ d->root = c;
+ f->split_mode = MODE_AUTOMATIC;
+ break;
+ }
+ if (f->vacant)
+ update_vacant_state(f->parent);
+ if (f->client != NULL && f->client->private)
+ update_privacy_level(f, true);
+ }
+ if (n->client->private)
+ update_privacy_level(n, true);
+ if (d->focus == NULL)
+ d->focus = n;
+ if (n->client->sticky)
+ m->num_sticky++;
+ put_status(SBSC_MASK_REPORT);
}
-bool is_first_child(node_t *n)
+void pseudo_focus(monitor_t *m, desktop_t *d, node_t *n)
{
- return (n != NULL && n->parent != NULL && n->parent->first_child == n);
+ if (n != NULL) {
+ stack(n, STACK_ABOVE);
+ if (d->focus != n) {
+ window_draw_border(d->focus, false, m == mon);
+ window_draw_border(n, true, m == mon);
+ }
+ }
+ d->focus = n;
}
-bool is_second_child(node_t *n)
+void focus_node(monitor_t *m, desktop_t *d, node_t *n)
{
- return (n != NULL && n->parent != NULL && n->parent->second_child == n);
+ if (mon->desk != d || n == NULL)
+ clear_input_focus();
+
+ if (m->num_sticky > 0 && d != m->desk) {
+ node_t *a = first_extrema(m->desk->root);
+ sticky_still = false;
+ while (a != NULL) {
+ node_t *b = next_leaf(a, m->desk->root);
+ if (a->client->sticky)
+ transfer_node(m, m->desk, a, m, d, d->focus);
+ a = b;
+ }
+ sticky_still = true;
+ if (n == NULL && d->focus != NULL)
+ n = d->focus;
+ }
+
+ if (n != NULL) {
+ if (d->focus != NULL && n != d->focus && d->focus->client->fullscreen) {
+ set_fullscreen(d->focus, false);
+ arrange(m, d);
+ }
+ if (n->client->urgent) {
+ n->client->urgent = false;
+ put_status(SBSC_MASK_REPORT);
+ }
+ }
+
+ if (mon != m) {
+ for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
+ window_draw_border(cd->focus, true, false);
+ for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
+ if (cd != d)
+ window_draw_border(cd->focus, true, true);
+ if (d->focus == n)
+ window_draw_border(n, true, true);
+ }
+
+ if (d->focus != n) {
+ window_draw_border(d->focus, false, true);
+ window_draw_border(n, true, true);
+ }
+
+ focus_desktop(m, d);
+
+ d->focus = n;
+
+ if (n == NULL) {
+ history_add(m, d, NULL);
+ ewmh_update_active_window();
+ return;
+ } else {
+ stack(n, STACK_ABOVE);
+ }
+
+ PRINTF("focus node %X\n", n->client->window);
+
+ history_add(m, d, n);
+ set_input_focus(n);
+
+ if (pointer_follows_focus) {
+ center_pointer(get_rectangle(n->client));
+ }
+
+ ewmh_update_active_window();
}
-void change_split_ratio(node_t *n, value_change_t chg)
+void update_current(void)
{
- n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
+ focus_node(mon, mon->desk, mon->desk->focus);
}
-node_t *first_extrema(node_t *n)
+node_t *make_node(void)
{
- if (n == NULL)
- return NULL;
- else if (n->first_child == NULL)
- return n;
- else
- return first_extrema(n->first_child);
+ node_t *n = malloc(sizeof(node_t));
+ n->parent = n->first_child = n->second_child = NULL;
+ n->split_ratio = split_ratio;
+ n->split_mode = MODE_AUTOMATIC;
+ n->split_type = TYPE_VERTICAL;
+ n->birth_rotation = 0;
+ n->privacy_level = 0;
+ n->client = NULL;
+ n->vacant = false;
+ return n;
}
-node_t *second_extrema(node_t *n)
+client_t *make_client(xcb_window_t win, unsigned int border_width)
{
- if (n == NULL)
- return NULL;
- else if (n->second_child == NULL)
- return n;
- else
- return second_extrema(n->second_child);
+ client_t *c = malloc(sizeof(client_t));
+ c->window = win;
+ snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
+ snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
+ c->border_width = border_width;
+ c->pseudo_tiled = c->floating = c->fullscreen = false;
+ c->locked = c->sticky = c->urgent = c->private = c->icccm_focus = false;
+ xcb_icccm_get_wm_protocols_reply_t protocols;
+ if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protocols, NULL) == 1) {
+ if (has_proto(WM_TAKE_FOCUS, &protocols))
+ c->icccm_focus = true;
+ xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
+ }
+ c->num_states = 0;
+ xcb_ewmh_get_atoms_reply_t wm_state;
+ if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
+ for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_STATE; i++)
+ ewmh_wm_state_add(c, wm_state.atoms[i]);
+ xcb_ewmh_get_atoms_reply_wipe(&wm_state);
+ }
+ return c;
}
-node_t *next_leaf(node_t *n, node_t *r)
+bool is_leaf(node_t *n)
{
- if (n == NULL)
- return NULL;
- node_t *p = n;
- while (is_second_child(p) && p != r)
- p = p->parent;
- if (p == r)
- return NULL;
- return first_extrema(p->parent->second_child);
+ return (n != NULL && n->first_child == NULL && n->second_child == NULL);
}
-node_t *prev_leaf(node_t *n, node_t *r)
-{
- if (n == NULL)
- return NULL;
- node_t *p = n;
- while (is_first_child(p) && p != r)
- p = p->parent;
- if (p == r)
- return NULL;
- return second_extrema(p->parent->first_child);
-}
-
-node_t *find_fence(node_t *n, direction_t dir)
+bool is_tiled(client_t *c)
{
- node_t *p;
-
- if (n == NULL)
- return NULL;
-
- p = n->parent;
-
- while (p != NULL) {
- if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
- || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
- || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
- || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
- return p;
- p = p->parent;
- }
-
- return NULL;
+ if (c == NULL)
+ return false;
+ return (!c->floating && !c->fullscreen);
}
-node_t *find_neighbor(node_t *n, direction_t dir)
+bool is_floating(client_t *c)
{
- node_t *fence = find_fence(n, dir);
-
- if (fence == NULL)
- return NULL;
-
- if (dir == DIR_UP || dir == DIR_LEFT)
- return second_extrema(fence->first_child);
- else if (dir == DIR_DOWN || dir == DIR_RIGHT)
- return first_extrema(fence->second_child);
-
- return NULL;
+ if (c == NULL)
+ return false;
+ return (c->floating && !c->fullscreen);
}
-void get_opposite(direction_t dir, direction_t* val)
+bool is_first_child(node_t *n)
{
- switch (dir) {
- case DIR_RIGHT:
- *val = DIR_LEFT;
- break;
- case DIR_DOWN:
- *val = DIR_UP;
- break;
- case DIR_LEFT:
- *val = DIR_RIGHT;
- break;
- case DIR_UP:
- *val = DIR_DOWN;
- break;
- }
+ return (n != NULL && n->parent != NULL && n->parent->first_child == n);
}
-node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir)
-{
- if (n == NULL)
- return NULL;
-
- node_t *target = NULL;
- if (is_tiled(n->client)) {
- target = find_fence(n, dir);
- if (target == NULL)
- return NULL;
- if (dir == DIR_UP || dir == DIR_LEFT)
- target = target->first_child;
- else if (dir == DIR_DOWN || dir == DIR_RIGHT)
- target = target->second_child;
- } else {
- target = d->root;
- }
- node_t *nearest = NULL;
- direction_t dir2;
- xcb_point_t pt;
- xcb_point_t pt2;
- get_side_handle(n->client, dir, &pt);
- get_opposite(dir, &dir2);
- double ds = DBL_MAX;
- for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
- if (is_tiled(a->client) != is_tiled(n->client) || a == n)
- continue;
- get_side_handle(a->client, dir2, &pt2);
- double ds2 = distance(pt, pt2);
- PRINTF("distance %X %g\n", a->client->window, ds2);
- if (ds2 < ds) {
- ds = ds2;
- nearest = a;
- }
- }
- return nearest;
-}
-
-int tiled_area(node_t *n)
+bool is_second_child(node_t *n)
{
- if (n == NULL)
- return -1;
- xcb_rectangle_t rect = n->client->tiled_rectangle;
- return rect.width * rect.height;
+ return (n != NULL && n->parent != NULL && n->parent->second_child == n);
}
-node_t *find_by_area(desktop_t *d, swap_arg_t a)
+void reset_mode(coordinates_t *loc)
{
- if (d == NULL)
- return NULL;
-
- node_t *r = NULL;
- int r_area = tiled_area(r);
-
- for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
- int f_area = tiled_area(f);
- if (r == NULL) {
- r = f;
- r_area = f_area;
- } else if ((a == SWAP_BIGGEST && f_area > r_area)
- || (a == SWAP_SMALLEST && f_area < r_area)) {
- r = f;
- r_area = f_area;
- }
- }
-
- return r;
+ if (loc->node != NULL) {
+ loc->node->split_mode = MODE_AUTOMATIC;
+ window_draw_border(loc->node, loc->desktop->focus == loc->node, mon == loc->monitor);
+ } else if (loc->desktop != NULL) {
+ for (node_t *a = first_extrema(loc->desktop->root); a != NULL; a = next_leaf(a, loc->desktop->root)) {
+ a->split_mode = MODE_AUTOMATIC;
+ window_draw_border(a, loc->desktop->focus == a, mon == loc->monitor);
+ }
+ }
}
-void move_fence(node_t *n, direction_t dir, fence_move_t mov)
+node_t *brother_tree(node_t *n)
{
- node_t *fence = find_fence(n, dir);
-
- if (fence == NULL)
- return;
-
- if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
- || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
- change_split_ratio(fence, CHANGE_INCREASE);
- else
- change_split_ratio(fence, CHANGE_DECREASE);
+ if (n == NULL || n->parent == NULL)
+ return NULL;
+ if (is_first_child(n))
+ return n->parent->second_child;
+ else
+ return n->parent->first_child;
}
-void rotate_tree(node_t *n, rotate_t rot)
+void closest_public(desktop_t *d, node_t *n, node_t **closest, node_t **public)
{
- if (n == NULL || is_leaf(n))
- return;
-
- node_t *tmp;
-
- if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
- || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
- || rot == ROTATE_FULL_CYCLE) {
- tmp = n->first_child;
- n->first_child = n->second_child;
- n->second_child = tmp;
- n->split_ratio = 1.0 - n->split_ratio;
- }
-
- if (rot != ROTATE_FULL_CYCLE) {
- if (n->split_type == TYPE_HORIZONTAL)
- n->split_type = TYPE_VERTICAL;
- else if (n->split_type == TYPE_VERTICAL)
- n->split_type = TYPE_HORIZONTAL;
- }
-
- rotate_tree(n->first_child, rot);
- rotate_tree(n->second_child, rot);
+ if (n == NULL)
+ return;
+ node_t *prev = prev_leaf(n, d->root);
+ node_t *next = next_leaf(n, d->root);
+ while (prev != NULL || next != NULL) {
+#define TESTLOOP(n) \
+ if (n != NULL) { \
+ if (is_tiled(n->client)) { \
+ if (n->privacy_level == 0) { \
+ if (n->parent == NULL || n->parent->privacy_level == 0) { \
+ *public = n; \
+ return; \
+ } else if (*closest == NULL) { \
+ *closest = n; \
+ } \
+ } \
+ } \
+ n = n##_leaf(n, d->root); \
+ }
+ TESTLOOP(prev)
+ TESTLOOP(next)
+#undef TESTLOOP
+ }
}
-void rotate_brother(node_t *n)
+node_t *first_extrema(node_t *n)
{
- if (n == NULL || n->parent == NULL)
- return;
- if (is_first_child(n))
- rotate_tree(n->parent->second_child, n->birth_rotation);
- else
- rotate_tree(n->parent->first_child, n->birth_rotation);
+ if (n == NULL)
+ return NULL;
+ else if (n->first_child == NULL)
+ return n;
+ else
+ return first_extrema(n->first_child);
}
-void unrotate_tree(node_t *n, rotate_t rot)
+node_t *second_extrema(node_t *n)
{
- switch(rot) {
- case ROTATE_CLOCKWISE:
- rotate_tree(n, ROTATE_COUNTER_CLOCKWISE);
- break;
- case ROTATE_COUNTER_CLOCKWISE:
- rotate_tree(n, ROTATE_CLOCKWISE);
- break;
- case ROTATE_IDENTITY:
- case ROTATE_FULL_CYCLE:
- break;
- }
+ if (n == NULL)
+ return NULL;
+ else if (n->second_child == NULL)
+ return n;
+ else
+ return second_extrema(n->second_child);
}
-void unrotate_brother(node_t *n)
+node_t *next_leaf(node_t *n, node_t *r)
{
- if (n == NULL || n->parent == NULL)
- return;
- if (is_first_child(n))
- unrotate_tree(n->parent->second_child, n->birth_rotation);
- else
- unrotate_tree(n->parent->first_child, n->birth_rotation);
+ if (n == NULL)
+ return NULL;
+ node_t *p = n;
+ while (is_second_child(p) && p != r)
+ p = p->parent;
+ if (p == r)
+ return NULL;
+ return first_extrema(p->parent->second_child);
}
-void flip_tree(node_t *n, flip_t flp)
+node_t *prev_leaf(node_t *n, node_t *r)
{
- if (n == NULL || is_leaf(n))
- return;
-
- node_t *tmp;
-
- if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL)
- || (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
- tmp = n->first_child;
- n->first_child = n->second_child;
- n->second_child = tmp;
- n->split_ratio = 1.0 - n->split_ratio;
- }
-
- flip_tree(n->first_child, flp);
- flip_tree(n->second_child, flp);
+ if (n == NULL)
+ return NULL;
+ node_t *p = n;
+ while (is_first_child(p) && p != r)
+ p = p->parent;
+ if (p == r)
+ return NULL;
+ return second_extrema(p->parent->first_child);
}
-void list_history(char *rsp)
+node_t *next_tiled_leaf(desktop_t *d, node_t *n, node_t *r)
{
- char line[MAXLEN];
- for (monitor_t *m = mon_head; m != NULL; m = m->next)
- for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
- snprintf(line, sizeof(line), "%s\n", d->name);
- strncat(rsp, line, REMLEN(rsp));
- for (node_list_t *a = d->history->tail; a != NULL; a = a->prev) {
- snprintf(line, sizeof(line), " %X\n", a->node->client->window);
- strncat(rsp, line, REMLEN(rsp));
- }
- }
+ node_t *next = next_leaf(n, r);
+ if (next == NULL || is_tiled(next->client))
+ return next;
+ else
+ return next_tiled_leaf(d, next, r);
}
-int balance_tree(node_t *n)
+node_t *prev_tiled_leaf(desktop_t *d, node_t *n, node_t *r)
{
- if (n == NULL || n->vacant) {
- return 0;
- } else if (is_leaf(n)) {
- return 1;
- } else {
- int b1 = balance_tree(n->first_child);
- int b2 = balance_tree(n->second_child);
- int b = b1 + b2;
- if (b1 > 0 && b2 > 0)
- n->split_ratio = (double) b1 / b;
- return b;
- }
+ node_t *prev = prev_leaf(n, r);
+ if (prev == NULL || is_tiled(prev->client))
+ return prev;
+ else
+ return prev_tiled_leaf(d, prev, r);
}
-void arrange(monitor_t *m, desktop_t *d)
+/* bool is_adjacent(node_t *a, node_t *r) */
+/* { */
+/* node_t *f = r->parent; */
+/* node_t *p = a; */
+/* bool first_child = is_first_child(r); */
+/* while (p != r) { */
+/* if (p->parent->split_type == f->split_type && is_first_child(p) == first_child) */
+/* return false; */
+/* p = p->parent; */
+/* } */
+/* return true; */
+/* } */
+
+/* Returns true if *b* is adjacent to *a* in the direction *dir* */
+bool is_adjacent(node_t *a, node_t *b, direction_t dir)
{
- PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
-
- xcb_rectangle_t rect = m->rectangle;
- int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
- rect.x += m->left_padding + wg;
- rect.y += m->top_padding + wg;
- rect.width -= m->left_padding + m->right_padding + wg;
- rect.height -= m->top_padding + m->bottom_padding + wg;
- apply_layout(m, d, d->root, rect, rect);
+ switch (dir) {
+ case DIR_RIGHT:
+ return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
+ break;
+ case DIR_DOWN:
+ return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
+ break;
+ case DIR_LEFT:
+ return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
+ break;
+ case DIR_UP:
+ return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
+ break;
+ }
+ return false;
}
-void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
+node_t *find_fence(node_t *n, direction_t dir)
{
- if (n == NULL)
- return;
-
- n->rectangle = rect;
+ node_t *p;
- if (is_leaf(n)) {
- if (n->client->fullscreen)
- return;
+ if (n == NULL)
+ return NULL;
- if (is_floating(n->client) && n->client->border_width != border_width) {
- int ds = 2 * (border_width - n->client->border_width);
- n->client->floating_rectangle.width += ds;
- n->client->floating_rectangle.height += ds;
- }
-
- if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
- n->client->border_width = 0;
- else
- n->client->border_width = border_width;
-
- xcb_rectangle_t r;
- if (is_tiled(n->client)) {
- if (d->layout == LAYOUT_TILED)
- r = rect;
- else if (d->layout == LAYOUT_MONOCLE)
- r = root_rect;
- int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
- int bleed = wg + 2 * n->client->border_width;
- r.width = (bleed < r.width ? r.width - bleed : 1);
- r.height = (bleed < r.height ? r.height - bleed : 1);
- n->client->tiled_rectangle = r;
- } else {
- r = n->client->floating_rectangle;
- }
+ p = n->parent;
- window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
- window_border_width(n->client->window, n->client->border_width);
- window_draw_border(n, n == d->focus, m == mon);
-
- } else {
- xcb_rectangle_t first_rect;
- xcb_rectangle_t second_rect;
-
- if (n->first_child->vacant || n->second_child->vacant) {
- first_rect = second_rect = rect;
- } else {
- unsigned int fence;
- if (n->split_type == TYPE_VERTICAL) {
- fence = rect.width * n->split_ratio;
- first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
- second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
-
- } else if (n->split_type == TYPE_HORIZONTAL) {
- fence = rect.height * n->split_ratio;
- first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
- second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
- }
- }
+ while (p != NULL) {
+ if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
+ (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
+ (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
+ (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
+ return p;
+ p = p->parent;
+ }
- apply_layout(m, d, n->first_child, first_rect, root_rect);
- apply_layout(m, d, n->second_child, second_rect, root_rect);
- }
+ return NULL;
}
-void insert_node(monitor_t *m, desktop_t *d, node_t *n)
+node_t *nearest_neighbor(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
{
- if (d == NULL || n == NULL)
- return;
-
- PRINTF("insert node %X\n", n->client->window);
-
- node_t *focus = d->focus;
-
- if (focus == NULL) {
- d->root = n;
- } else {
- node_t *dad = make_node();
- node_t *fopar = focus->parent;
- n->parent = dad;
- dad->birth_rotation = focus->birth_rotation;
- switch (split_mode) {
- case MODE_AUTOMATIC:
- if (fopar == NULL) {
- dad->first_child = n;
- dad->second_child = focus;
- if (m->rectangle.width > m->rectangle.height)
- dad->split_type = TYPE_VERTICAL;
- else
- dad->split_type = TYPE_HORIZONTAL;
- focus->parent = dad;
- d->root = dad;
- } else {
- node_t *grandpa = fopar->parent;
- dad->parent = grandpa;
- if (grandpa != NULL) {
- if (is_first_child(fopar))
- grandpa->first_child = dad;
- else
- grandpa->second_child = dad;
- } else {
- d->root = dad;
- }
- dad->split_type = fopar->split_type;
- dad->split_ratio = fopar->split_ratio;
- fopar->parent = dad;
- rotate_t rot;
- if (is_first_child(focus)) {
- dad->first_child = n;
- dad->second_child = fopar;
- rot = ROTATE_CLOCKWISE;
- } else {
- dad->first_child = fopar;
- dad->second_child = n;
- rot = ROTATE_COUNTER_CLOCKWISE;
- }
- rotate_tree(fopar, rot);
- n->birth_rotation = rot;
- }
- break;
- case MODE_MANUAL:
- if (fopar != NULL) {
- if (is_first_child(focus))
- fopar->first_child = dad;
- else
- fopar->second_child = dad;
- }
- dad->split_ratio = focus->split_ratio;
- dad->parent = fopar;
- focus->parent = dad;
- focus->birth_rotation = ROTATE_IDENTITY;
- switch (split_dir) {
- case DIR_LEFT:
- dad->split_type = TYPE_VERTICAL;
- dad->first_child = n;
- dad->second_child = focus;
- break;
- case DIR_RIGHT:
- dad->split_type = TYPE_VERTICAL;
- dad->first_child = focus;
- dad->second_child = n;
- break;
- case DIR_UP:
- dad->split_type = TYPE_HORIZONTAL;
- dad->first_child = n;
- dad->second_child = focus;
- break;
- case DIR_DOWN:
- dad->split_type = TYPE_HORIZONTAL;
- dad->first_child = focus;
- dad->second_child = n;
- break;
- }
- if (d->root == focus)
- d->root = dad;
- split_mode = MODE_AUTOMATIC;
- break;
+ if (n == NULL || n->client->fullscreen ||
+ (d->layout == LAYOUT_MONOCLE && is_tiled(n->client)))
+ return NULL;
+
+ node_t *nearest = NULL;
+ if (history_aware_focus)
+ nearest = nearest_from_history(m, d, n, dir, sel);
+ if (nearest == NULL) {
+ if (focus_by_distance) {
+ nearest = nearest_from_distance(m, d, n, dir, sel);
+ } else {
+ nearest = nearest_from_tree(m, d, n, dir, sel);
}
- if (focus->vacant)
- update_vacant_state(fopar);
}
- put_status();
+ return nearest;
}
-void focus_node(monitor_t *m, desktop_t *d, node_t *n)
+node_t *nearest_from_tree(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
{
- if (n == NULL && d->root != NULL)
- return;
-
- if (mon != m) {
- for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
- window_draw_border(cd->focus, true, false);
- for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
- if (cd != d)
- window_draw_border(cd->focus, true, true);
- if (d->focus == n)
- window_draw_border(n, true, true);
- }
-
- if (d->focus != n) {
- window_draw_border(d->focus, false, true);
- window_draw_border(n, true, true);
- }
-
- select_desktop(m, d);
-
- if (n == NULL) {
- ewmh_update_active_window();
- return;
- }
-
- PRINTF("focus node %X\n", n->client->window);
-
- split_mode = MODE_AUTOMATIC;
- n->client->urgent = false;
+ if (n == NULL)
+ return NULL;
- xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
+ node_t *fence = find_fence(n, dir);
- if (d->focus != n) {
- d->focus = n;
- history_add(d->history, n);
- }
+ if (fence == NULL)
+ return NULL;
- if (!is_tiled(n->client)) {
- if (!adaptative_raise || !might_cover(d, n))
- window_raise(n->client->window);
- } else {
- stack_tiled(d);
- }
+ node_t *nearest = NULL;
- if (focus_follows_pointer) {
- xcb_window_t win = XCB_NONE;
- query_pointer(&win, NULL);
- if (win != n->client->window)
- enable_motion_recorder();
- else
- disable_motion_recorder();
- }
+ if (dir == DIR_UP || dir == DIR_LEFT)
+ nearest = second_extrema(fence->first_child);
+ else if (dir == DIR_DOWN || dir == DIR_RIGHT)
+ nearest = first_extrema(fence->second_child);
- ewmh_update_active_window();
-}
+ coordinates_t ref = {m, d, n};
+ coordinates_t loc = {m, d, nearest};
-void update_current(void)
-{
- focus_node(mon, mon->desk, mon->desk->focus);
+ if (node_matches(&loc, &ref, sel))
+ return nearest;
+ else
+ return NULL;
}
-void unlink_node(desktop_t *d, node_t *n)
+node_t *nearest_from_history(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
{
- if (d == NULL || n == NULL)
- return;
-
- PRINTF("unlink node %X\n", n->client->window);
-
- node_t *p = n->parent;
-
- if (p == NULL) {
- d->root = NULL;
- d->focus = NULL;
- } else {
- node_t *b;
- node_t *g = p->parent;
- if (is_first_child(n)) {
- b = p->second_child;
- if (!n->vacant)
- unrotate_tree(b, n->birth_rotation);
- } else {
- b = p->first_child;
- if (!n->vacant)
- unrotate_tree(b, n->birth_rotation);
- }
- b->parent = g;
- if (g != NULL) {
- if (is_first_child(p))
- g->first_child = b;
- else
- g->second_child = b;
- } else {
- d->root = b;
- }
-
- b->birth_rotation = p->birth_rotation;
- n->parent = NULL;
- free(p);
-
- if (n == d->focus)
- d->focus = history_get(d->history, 1);
-
- update_vacant_state(b->parent);
- }
- history_remove(d->history, n);
- put_status();
+ if (n == NULL || !is_tiled(n->client))
+ return NULL;
+
+ node_t *target = find_fence(n, dir);
+ if (target == NULL)
+ return NULL;
+ if (dir == DIR_UP || dir == DIR_LEFT)
+ target = target->first_child;
+ else if (dir == DIR_DOWN || dir == DIR_RIGHT)
+ target = target->second_child;
+
+ node_t *nearest = NULL;
+ int min_rank = INT_MAX;
+ coordinates_t ref = {m, d, n};
+
+ for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
+ if (a->vacant || !is_adjacent(n, a, dir) || a == n)
+ continue;
+ coordinates_t loc = {m, d, a};
+ if (!node_matches(&loc, &ref, sel))
+ continue;
+
+ int rank = history_rank(d, a);
+ if (rank >= 0 && rank < min_rank) {
+ nearest = a;
+ min_rank = rank;
+ }
+ }
+
+ return nearest;
}
-void remove_node(desktop_t *d, node_t *n)
+node_t *nearest_from_distance(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
{
- if (d == NULL || n == NULL)
- return;
-
- PRINTF("remove node %X\n", n->client->window);
-
- unlink_node(d, n);
- free(n->client);
- free(n);
-
- num_clients--;
- ewmh_update_client_list();
-
- if (mon->desk == d)
- update_current();
+ if (n == NULL)
+ return NULL;
+
+ node_t *target = NULL;
+
+ if (is_tiled(n->client)) {
+ target = find_fence(n, dir);
+ if (target == NULL)
+ return NULL;
+ if (dir == DIR_UP || dir == DIR_LEFT)
+ target = target->first_child;
+ else if (dir == DIR_DOWN || dir == DIR_RIGHT)
+ target = target->second_child;
+ } else {
+ target = d->root;
+ }
+
+ node_t *nearest = NULL;
+ direction_t dir2;
+ xcb_point_t pt;
+ xcb_point_t pt2;
+ get_side_handle(n->client, dir, &pt);
+ get_opposite(dir, &dir2);
+ double ds = DBL_MAX;
+ coordinates_t ref = {m, d, n};
+
+ for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
+ coordinates_t loc = {m, d, a};
+ if (a == n ||
+ !node_matches(&loc, &ref, sel) ||
+ is_tiled(a->client) != is_tiled(n->client) ||
+ (is_tiled(a->client) && !is_adjacent(n, a, dir)))
+ continue;
+
+ get_side_handle(a->client, dir2, &pt2);
+ double ds2 = distance(pt, pt2);
+ if (ds2 < ds) {
+ ds = ds2;
+ nearest = a;
+ }
+ }
+
+ return nearest;
}
-void destroy_tree(node_t *n)
+void get_opposite(direction_t src, direction_t *dst)
{
- if (n == NULL)
- return;
- node_t *first_tree = n->first_child;
- node_t *second_tree = n->second_child;
- if (n->client != NULL)
- free(n->client);
- free(n);
- destroy_tree(first_tree);
- destroy_tree(second_tree);
+ switch (src) {
+ case DIR_RIGHT:
+ *dst = DIR_LEFT;
+ break;
+ case DIR_DOWN:
+ *dst = DIR_UP;
+ break;
+ case DIR_LEFT:
+ *dst = DIR_RIGHT;
+ break;
+ case DIR_UP:
+ *dst = DIR_DOWN;
+ break;
+ }
}
-void swap_nodes(node_t *n1, node_t *n2)
+int tiled_area(node_t *n)
{
- if (n1 == NULL || n2 == NULL || n1 == n2)
- return;
-
- PUTS("swap nodes");
-
- /* (n1 and n2 are leaves) */
- node_t *pn1 = n1->parent;
- node_t *pn2 = n2->parent;
- bool n1_first_child = is_first_child(n1);
- bool n2_first_child = is_first_child(n2);
- rotate_t br1 = n1->birth_rotation;
- rotate_t br2 = n2->birth_rotation;
-
- if (pn1 != NULL) {
- if (n1_first_child)
- pn1->first_child = n2;
- else
- pn1->second_child = n2;
- }
-
- if (pn2 != NULL) {
- if (n2_first_child)
- pn2->first_child = n1;
- else
- pn2->second_child = n1;
- }
-
- n1->parent = pn2;
- n2->parent = pn1;
- n1->birth_rotation = br2;
- n2->birth_rotation = br1;
-
- if (n1->vacant != n2->vacant) {
- update_vacant_state(n1->parent);
- update_vacant_state(n2->parent);
- }
-
- /* If we ever need to generalize: */
- /* if (d1 != d2) { */
- /* if (d1->root == n1) */
- /* d1->root = n2; */
- /* if (d1->focus == n1) */
- /* d1->focus = n2; */
- /* if (d1->last_focus == n1) */
- /* d1->last_focus = n2; */
- /* if (d2->root == n2) */
- /* d2->root = n1; */
- /* if (d2->focus == n2) */
- /* d2->focus = n1; */
- /* if (d2->last_focus == n2) */
- /* d2->last_focus = n1; */
- /* } */
+ if (n == NULL)
+ return -1;
+ xcb_rectangle_t rect = n->client->tiled_rectangle;
+ return rect.width * rect.height;
}
-void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
+node_t *find_biggest(monitor_t *m, desktop_t *d, node_t *n, client_select_t sel)
{
- if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
- return;
-
- PRINTF("transfer node %X\n", n->client->window);
-
- unlink_node(ds, n);
- insert_node(md, dd, n);
- ewmh_set_wm_desktop(n, dd);
-
- if (ds == ms->desk && dd != md->desk) {
- if (n == ds->focus)
- clear_input_focus();
- window_hide(n->client->window);
- }
-
- fit_monitor(md, n->client);
-
- if (n->client->fullscreen)
- window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
-
- if (ds != ms->desk && dd == md->desk)
- window_show(n->client->window);
-
- dd->focus = n;
- history_add(dd->history, n);
-
- if (ds == ms->desk || dd == md->desk)
- update_current();
+ if (d == NULL)
+ return NULL;
+
+ node_t *r = NULL;
+ int r_area = tiled_area(r);
+ coordinates_t ref = {m, d, n};
+
+ for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
+ coordinates_t loc = {m, d, f};
+ if (!is_tiled(f->client) || !node_matches(&loc, &ref, sel))
+ continue;
+ int f_area = tiled_area(f);
+ if (r == NULL) {
+ r = f;
+ r_area = f_area;
+ } else if (f_area > r_area) {
+ r = f;
+ r_area = f_area;
+ }
+ }
+
+ return r;
}
-void select_monitor(monitor_t *m)
+void rotate_tree(node_t *n, int deg)
{
- if (mon == m)
- return;
-
- PRINTF("select monitor %s\n", m->name);
-
- last_mon = mon;
- mon = m;
-
- ewmh_update_current_desktop();
- put_status();
+ if (n == NULL || is_leaf(n) || deg == 0)
+ return;
+
+ node_t *tmp;
+
+ if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
+ (deg == 270 && n->split_type == TYPE_VERTICAL) ||
+ deg == 180) {
+ tmp = n->first_child;
+ n->first_child = n->second_child;
+ n->second_child = tmp;
+ n->split_ratio = 1.0 - n->split_ratio;
+ }
+
+ if (deg != 180) {
+ if (n->split_type == TYPE_HORIZONTAL)
+ n->split_type = TYPE_VERTICAL;
+ else if (n->split_type == TYPE_VERTICAL)
+ n->split_type = TYPE_HORIZONTAL;
+ }
+
+ rotate_tree(n->first_child, deg);
+ rotate_tree(n->second_child, deg);
}
-void select_desktop(monitor_t *m, desktop_t *d)
+void rotate_brother(node_t *n)
{
- select_monitor(m);
-
- if (d == mon->desk)
- return;
-
- PRINTF("select desktop %s\n", d->name);
-
- clear_input_focus();
-
- if (visible) {
- node_t *n = first_extrema(d->root);
-
- while (n != NULL) {
- window_show(n->client->window);
- n = next_leaf(n, d->root);
- }
-
- n = first_extrema(mon->desk->root);
-
- while (n != NULL) {
- window_hide(n->client->window);
- n = next_leaf(n, mon->desk->root);
- }
- }
-
- mon->last_desk = mon->desk;
- mon->desk = d;
-
- ewmh_update_current_desktop();
- put_status();
+ rotate_tree(brother_tree(n), n->birth_rotation);
}
-void cycle_monitor(cycle_dir_t dir)
+void unrotate_tree(node_t *n, int rot)
{
- monitor_t *m = NULL;
- if (dir == CYCLE_NEXT)
- m = (mon->next == NULL ? mon_head : mon->next);
- else if (dir == CYCLE_PREV)
- m = (mon->prev == NULL ? mon_tail : mon->prev);
- focus_node(m, m->desk, m->desk->focus);
+ if (rot == 0)
+ return;
+ rotate_tree(n, 360 - rot);
}
-void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
+void unrotate_brother(node_t *n)
{
- desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
- if (f == NULL)
- f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
-
- while (f != d) {
- if (skip == DESKTOP_SKIP_NONE
- || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
- || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
- focus_node(m, f, f->focus);
- return;
- }
- f = (dir == CYCLE_PREV ? f->prev : f->next);
- if (f == NULL)
- f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
- }
+ unrotate_tree(brother_tree(n), n->birth_rotation);
}
-void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
+void flip_tree(node_t *n, flip_t flp)
{
- if (n == NULL)
- return;
+ if (n == NULL || is_leaf(n))
+ return;
- PUTS("cycle leaf");
+ node_t *tmp;
- node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
- if (f == NULL)
- f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
+ if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
+ (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
+ tmp = n->first_child;
+ n->first_child = n->second_child;
+ n->second_child = tmp;
+ n->split_ratio = 1.0 - n->split_ratio;
+ }
- 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);
- return;
- }
- f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
- if (f == NULL)
- f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
- }
+ flip_tree(n->first_child, flp);
+ flip_tree(n->second_child, flp);
}
-void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
+void equalize_tree(node_t *n)
{
- 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, d->root))
- 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;
+ if (n == NULL || n->vacant) {
+ return;
+ } else {
+ n->split_ratio = split_ratio;
+ equalize_tree(n->first_child);
+ equalize_tree(n->second_child);
+ }
+}
- focus_node(m, d, x);
+int balance_tree(node_t *n)
+{
+ if (n == NULL || n->vacant) {
+ return 0;
+ } else if (is_leaf(n)) {
+ return 1;
+ } else {
+ int b1 = balance_tree(n->first_child);
+ int b2 = balance_tree(n->second_child);
+ int b = b1 + b2;
+ if (b1 > 0 && b2 > 0)
+ n->split_ratio = (double) b1 / b;
+ return b;
+ }
}
-void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
+void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
{
- 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, d->root); f != NULL; s = prev_leaf(f, d->root), f = prev_leaf(s, d->root))
- swap_nodes(f, s);
- else
- for (node_t *f = first_extrema(d->root), *s = next_leaf(f, d->root); s != NULL; f = next_leaf(s, d->root), s = next_leaf(f, d->root))
- swap_nodes(f, s);
- if (focus_first_child)
- focus_node(m, d, par->first_child);
- else
- focus_node(m, d, par->second_child);
+ if (d == NULL || n == NULL)
+ return;
+
+ PRINTF("unlink node %X\n", n->client->window);
+
+ node_t *p = n->parent;
+
+ if (p == NULL) {
+ d->root = NULL;
+ d->focus = NULL;
+ } else {
+ if (n->client->private)
+ update_privacy_level(n, false);
+
+ node_t *b;
+ node_t *g = p->parent;
+
+ if (is_first_child(n)) {
+ b = p->second_child;
+ if (!n->vacant)
+ unrotate_tree(b, n->birth_rotation);
+ } else {
+ b = p->first_child;
+ if (!n->vacant)
+ unrotate_tree(b, n->birth_rotation);
+ }
+
+ b->parent = g;
+
+ if (g != NULL) {
+ if (is_first_child(p))
+ g->first_child = b;
+ else
+ g->second_child = b;
+ } else {
+ d->root = b;
+ }
+
+ b->birth_rotation = p->birth_rotation;
+ n->parent = NULL;
+ free(p);
+ update_vacant_state(b->parent);
+
+ if (n == d->focus) {
+ d->focus = history_get_node(d, n);
+ // fallback to the first extrema (`n` is not reachable)
+ if (d->focus == NULL)
+ d->focus = first_extrema(d->root);
+ }
+ }
+ if (n->client->sticky)
+ m->num_sticky--;
+ put_status(SBSC_MASK_REPORT);
}
-void update_vacant_state(node_t *n)
+void remove_node(monitor_t *m, desktop_t *d, node_t *n)
{
- if (n == NULL)
- return;
+ if (n == NULL)
+ return;
- PUTS("update vacant state");
+ PRINTF("remove node %X\n", n->client->window);
- /* n is not a leaf */
- node_t *p = n;
+ bool focused = (n == mon->desk->focus);
+ unlink_node(m, d, n);
+ history_remove(d, n);
+ remove_stack_node(n);
+ free(n->client);
+ free(n);
- while (p != NULL) {
- p->vacant = (p->first_child->vacant && p->second_child->vacant);
- p = p->parent;
- }
+ num_clients--;
+ ewmh_update_client_list();
+
+ if (focused)
+ update_current();
}
-void fit_monitor(monitor_t *m, client_t *c)
+void destroy_tree(node_t *n)
{
- 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;
+ if (n == NULL)
+ return;
+ node_t *first_tree = n->first_child;
+ node_t *second_tree = n->second_child;
+ if (n->client != NULL) {
+ free(n->client);
+ num_clients--;
+ }
+ free(n);
+ destroy_tree(first_tree);
+ destroy_tree(second_tree);
}
-void put_status(void)
+bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
{
- if (status_fifo == NULL)
- return;
- if (status_prefix != NULL)
- fprintf(status_fifo, "%s", status_prefix);
- 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, d->root))
- urgent |= n->client->urgent;
- fprintf(status_fifo, "%c%s:", m->desk == d ? (urgent ? 'U' : 'D') : (d->root == NULL ? 'E' : (urgent ? 'u' : 'd')), d->name);
- }
- }
- fprintf(status_fifo, "L%s\n", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
- fflush(status_fifo);
+ if (n1 == NULL || n2 == NULL ||n1 == n2 ||
+ (d1 != d2 && (n1->client->sticky || n2->client->sticky)))
+ return false;
+
+ PRINTF("swap nodes %X %X\n", n1->client->window, n2->client->window);
+
+ node_t *pn1 = n1->parent;
+ node_t *pn2 = n2->parent;
+ bool n1_first_child = is_first_child(n1);
+ bool n2_first_child = is_first_child(n2);
+ int br1 = n1->birth_rotation;
+ int br2 = n2->birth_rotation;
+ int pl1 = n1->privacy_level;
+ int pl2 = n2->privacy_level;
+
+ if (pn1 != NULL) {
+ if (n1_first_child)
+ pn1->first_child = n2;
+ else
+ pn1->second_child = n2;
+ }
+
+ if (pn2 != NULL) {
+ if (n2_first_child)
+ pn2->first_child = n1;
+ else
+ pn2->second_child = n1;
+ }
+
+ n1->parent = pn2;
+ n2->parent = pn1;
+ n1->birth_rotation = br2;
+ n2->birth_rotation = br1;
+ n1->privacy_level = pl2;
+ n2->privacy_level = pl1;
+
+ if (n1->vacant != n2->vacant) {
+ update_vacant_state(n1->parent);
+ update_vacant_state(n2->parent);
+ }
+
+ if (n1->client->private != n2->client->private) {
+ n1->client->private = !n1->client->private;
+ n2->client->private = !n2->client->private;
+ }
+
+ if (d1 != d2) {
+ if (d1->root == n1)
+ d1->root = n2;
+ if (d1->focus == n1)
+ d1->focus = n2;
+ if (d2->root == n2)
+ d2->root = n1;
+ if (d2->focus == n2)
+ d2->focus = n1;
+
+ if (m1 != m2) {
+ translate_client(m2, m1, n2->client);
+ translate_client(m1, m2, n1->client);
+ }
+
+ ewmh_set_wm_desktop(n1, d2);
+ ewmh_set_wm_desktop(n2, d1);
+ history_swap_nodes(m1, d1, n1, m2, d2, n2);
+
+ if (m1->desk != d1 && m2->desk == d2) {
+ window_show(n1->client->window);
+ window_hide(n2->client->window);
+ } else if (m1->desk == d1 && m2->desk != d2) {
+ window_hide(n1->client->window);
+ window_show(n2->client->window);
+ }
+
+ update_input_focus();
+ }
+
+ return true;
}
-void list_monitors(list_option_t opt, char *rsp)
+bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
{
- 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);
- }
+ if (ns == NULL || ns == nd || (sticky_still && ns->client->sticky))
+ return false;
+
+ PRINTF("transfer node %X\n", ns->client->window);
+
+ bool focused = (ns == mon->desk->focus);
+ bool active = (ns == ds->focus);
+
+ if (focused)
+ clear_input_focus();
+
+ unlink_node(ms, ds, ns);
+ insert_node(md, dd, ns, nd);
+
+ if (md != ms)
+ translate_client(ms, md, ns->client);
+
+ if (ds != dd) {
+ ewmh_set_wm_desktop(ns, dd);
+ if (!ns->client->sticky) {
+ if (ds == ms->desk && dd != md->desk)
+ window_hide(ns->client->window);
+ else if (ds != ms->desk && dd == md->desk)
+ window_show(ns->client->window);
+ }
+ if (ns->client->fullscreen && dd->focus != ns)
+ set_fullscreen(ns, false);
+ }
+
+ history_transfer_node(md, dd, ns);
+ stack(ns, STACK_BELOW);
+
+ if (ds == dd) {
+ if (focused)
+ focus_node(md, dd, ns);
+ else if (active)
+ pseudo_focus(md, dd, ns);
+ } else {
+ if (focused)
+ update_current();
+ else if (ns == mon->desk->focus)
+ update_input_focus();
+ }
+
+ arrange(ms, ds);
+ if (ds != dd)
+ arrange(md, dd);
+
+ return true;
}
-void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
+node_t *closest_node(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t sel)
{
- 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);
- }
+ if (n == NULL)
+ return NULL;
+
+ node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
+ if (f == NULL)
+ f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
+
+ coordinates_t ref = {m, d, n};
+ while (f != n) {
+ coordinates_t loc = {m, d, f};
+ if (node_matches(&loc, &ref, sel))
+ return f;
+ f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
+ if (f == NULL)
+ f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
+ }
+ return NULL;
}
-void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
+void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
{
- 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", (n->birth_rotation == ROTATE_CLOCKWISE ? 'a' : (n->birth_rotation == ROTATE_COUNTER_CLOCKWISE ? 'c' : '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 %c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), (n->birth_rotation == ROTATE_CLOCKWISE ? 'a' : (n->birth_rotation == ROTATE_COUNTER_CLOCKWISE ? 'c' : 'm')), n->split_ratio);
- }
-
- strncat(rsp, line, REMLEN(rsp));
-
- if (n == d->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);
+ if (d == NULL || d->root == NULL || d->focus == NULL || is_leaf(d->root))
+ return;
+ node_t *p = 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_tiled_leaf(d, s, d->root); f != NULL; s = prev_tiled_leaf(d, f, d->root), f = prev_tiled_leaf(d, s, d->root))
+ swap_nodes(m, d, f, m, d, s);
+ else
+ for (node_t *f = first_extrema(d->root), *s = next_tiled_leaf(d, f, d->root); s != NULL; f = next_tiled_leaf(d, s, d->root), s = next_tiled_leaf(d, f, d->root))
+ swap_nodes(m, d, f, m, d, s);
+ if (focus_first_child)
+ focus_node(m, d, p->first_child);
+ else
+ focus_node(m, d, p->second_child);
}
-void restore_layout(char *file_path)
+void update_vacant_state(node_t *n)
{
- if (file_path == NULL)
- return;
+ if (n == NULL)
+ return;
- FILE *snapshot = fopen(file_path, "r");
- if (snapshot == NULL) {
- warn("restore: can't open file\n");
- return;
- }
+ PUTS("update vacant state");
- 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;
-
- char br;
- if (isupper(line[level])) {
- char st;
- sscanf(line + level, "%c %c %lf", &st, &br, &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 floating, transient, fullscreen, urgent, locked;
- sscanf(line + level, "%c %s %X %u %u %hux%hu%hi%hi %c%c%c%c%c", &br, 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);
- 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 && line[len - 2] == '*')
- d->focus = n;
- }
- if (br == 'a')
- n->birth_rotation = ROTATE_CLOCKWISE;
- else if (br == 'c')
- n->birth_rotation = ROTATE_COUNTER_CLOCKWISE;
- else if (br == 'm')
- n->birth_rotation = ROTATE_IDENTITY;
- }
- last_level = level;
- }
+ /* n is not a leaf */
+ node_t *p = n;
- fclose(snapshot);
-
- 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, d->root)) {
- 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);
- }
- }
- ewmh_update_current_desktop();
- }
+ while (p != NULL) {
+ p->vacant = (p->first_child->vacant && p->second_child->vacant);
+ p = p->parent;
+ }
}
-void restore_history(char *file_path)
+void update_privacy_level(node_t *n, bool value)
{
- if (file_path == NULL)
- return;
-
- FILE *snapshot = fopen(file_path, "r");
- if (snapshot == NULL) {
- warn("restore history: can't open file\n");
- return;
- }
-
- char line[MAXLEN];
- desktop_t *d = NULL;
- unsigned int level;
-
- while (fgets(line, sizeof(line), snapshot) != NULL) {
- unsigned int i = strlen(line) - 1;
- while (i > 0 && isspace(line[i]))
- line[i--] = '\0';
- level = 0;
- while (level < strlen(line) && isspace(line[level]))
- level++;
- PRINTF("level %u: %s\n", level, line + level);
- if (level == 0) {
- desktop_location_t loc;
- if (locate_desktop(line + level, &loc))
- d = loc.desktop;
- } else if (d != NULL) {
- xcb_window_t win;
- if (sscanf(line + level, "%X", &win) == 1) {
- window_location_t loc;
- if (locate_window(win, &loc))
- history_add(d->history, loc.node);
- }
- }
- }
-
- fclose(snapshot);
+ int v = (value ? 1 : -1);
+ for (node_t *p = n; p != NULL; p = p->parent)
+ p->privacy_level += v;
}