2012-08-20 22:38:29 +02:00
|
|
|
#include <stdio.h>
|
2012-09-05 18:37:54 +02:00
|
|
|
#include <stdlib.h>
|
2012-08-20 22:38:29 +02:00
|
|
|
#include <math.h>
|
|
|
|
#include <xcb/xcb.h>
|
|
|
|
#include <xcb/xcb_event.h>
|
2012-09-04 21:01:50 +02:00
|
|
|
#include "settings.h"
|
|
|
|
#include "helpers.h"
|
2012-08-20 22:38:29 +02:00
|
|
|
#include "utils.h"
|
|
|
|
#include "types.h"
|
2012-09-04 21:01:50 +02:00
|
|
|
#include "bspwm.h"
|
2012-08-17 22:18:26 +02:00
|
|
|
#include "tree.h"
|
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
bool is_leaf(node_t *n)
|
2012-08-19 10:34:08 +02:00
|
|
|
{
|
|
|
|
return (n != NULL && n->first_child == NULL && n->second_child == NULL);
|
|
|
|
}
|
2012-09-01 21:55:35 +02:00
|
|
|
|
|
|
|
bool is_first_child(node_t *n)
|
|
|
|
{
|
|
|
|
return (n != NULL && n->parent != NULL && n->parent->first_child == n);
|
|
|
|
}
|
|
|
|
|
2012-09-04 11:14:01 +02:00
|
|
|
bool is_second_child(node_t *n)
|
|
|
|
{
|
|
|
|
return (n != NULL && n->parent != NULL && n->parent->second_child == n);
|
|
|
|
}
|
|
|
|
|
2012-09-05 18:37:54 +02:00
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
void change_split_ratio(node_t *n, value_change_t chg) {
|
2012-08-20 12:20:12 +02:00
|
|
|
n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
|
|
|
|
}
|
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
node_t *first_extrema(node_t *n)
|
2012-08-17 22:18:26 +02:00
|
|
|
{
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
else if (n->first_child == NULL)
|
|
|
|
return n;
|
|
|
|
else
|
|
|
|
return first_extrema(n->first_child);
|
|
|
|
}
|
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
node_t *second_extrema(node_t *n)
|
2012-08-17 22:18:26 +02:00
|
|
|
{
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
else if (n->second_child == NULL)
|
|
|
|
return n;
|
|
|
|
else
|
|
|
|
return second_extrema(n->second_child);
|
|
|
|
}
|
|
|
|
|
2012-09-04 11:14:01 +02:00
|
|
|
node_t *next_leaf(node_t *n)
|
|
|
|
{
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
node_t *p = n;
|
|
|
|
while (is_second_child(p))
|
|
|
|
p = p->parent;
|
|
|
|
if (p->parent == NULL)
|
|
|
|
return NULL;
|
|
|
|
return first_extrema(p->parent->second_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
node_t *prev_leaf(node_t *n)
|
|
|
|
{
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
node_t *p = n;
|
|
|
|
while (is_first_child(p))
|
|
|
|
p = p->parent;
|
|
|
|
if (p->parent == NULL)
|
|
|
|
return NULL;
|
|
|
|
return second_extrema(p->parent->first_child);
|
|
|
|
}
|
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
node_t *find_fence(node_t *n, direction_t dir)
|
2012-08-17 22:18:26 +02:00
|
|
|
{
|
2012-08-29 18:37:31 +02:00
|
|
|
node_t *p;
|
2012-08-18 14:55:16 +02:00
|
|
|
|
2012-08-20 12:20:12 +02:00
|
|
|
if (n == NULL)
|
2012-08-17 22:18:26 +02:00
|
|
|
return NULL;
|
2012-08-18 14:55:16 +02:00
|
|
|
|
2012-08-20 12:20:12 +02:00
|
|
|
p = n->parent;
|
|
|
|
|
2012-08-18 22:36:46 +02:00
|
|
|
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;
|
2012-08-18 14:55:16 +02:00
|
|
|
p = p->parent;
|
2012-08-17 22:18:26 +02:00
|
|
|
}
|
2012-08-18 22:36:46 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
node_t *find_neighbor(node_t *n, direction_t dir)
|
2012-08-18 22:36:46 +02:00
|
|
|
{
|
2012-08-29 18:37:31 +02:00
|
|
|
node_t *fence = find_fence(n, dir);
|
2012-08-18 22:36:46 +02:00
|
|
|
|
|
|
|
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;
|
2012-08-17 22:18:26 +02:00
|
|
|
}
|
2012-08-18 12:19:58 +02:00
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
void move_fence(node_t *n, direction_t dir, fence_move_t mov)
|
2012-08-20 12:20:12 +02:00
|
|
|
{
|
2012-08-29 18:37:31 +02:00
|
|
|
node_t *fence = find_fence(n, dir);
|
2012-09-05 18:37:54 +02:00
|
|
|
|
2012-08-20 12:20:12 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
void rotate_tree(node_t *n, rotate_t rot)
|
2012-08-18 12:19:58 +02:00
|
|
|
{
|
2012-09-05 14:07:06 +02:00
|
|
|
if (n == NULL || is_leaf(n))
|
2012-08-18 12:19:58 +02:00
|
|
|
return;
|
2012-09-05 14:07:06 +02:00
|
|
|
|
|
|
|
node_t *tmp;
|
|
|
|
|
2012-08-18 12:19:58 +02:00
|
|
|
if ((rot == ROTATE_CLOCK_WISE && n->split_type == TYPE_HORIZONTAL)
|
|
|
|
|| (rot == ROTATE_COUNTER_CW && 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;
|
|
|
|
}
|
2012-09-05 14:07:06 +02:00
|
|
|
|
2012-08-18 12:19:58 +02:00
|
|
|
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;
|
|
|
|
}
|
2012-09-05 14:07:06 +02:00
|
|
|
|
|
|
|
rotate_tree(n->first_child, rot);
|
|
|
|
rotate_tree(n->second_child, rot);
|
2012-08-18 12:19:58 +02:00
|
|
|
}
|
2012-08-18 22:36:46 +02:00
|
|
|
|
2012-08-29 18:37:31 +02:00
|
|
|
void dump_tree(node_t *n, char *rsp, int depth)
|
2012-08-18 22:36:46 +02:00
|
|
|
{
|
|
|
|
if (n == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-09-05 14:07:06 +02:00
|
|
|
for (int i = 0; i < depth; i++)
|
2012-08-19 21:45:49 +02:00
|
|
|
sprintf(rsp, "%s", " ");
|
2012-08-18 22:36:46 +02:00
|
|
|
|
|
|
|
if (n->client == NULL)
|
|
|
|
sprintf(rsp, "%s %.2f\n", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
|
|
|
|
else
|
|
|
|
sprintf(rsp, "%X\n", n->client->window);
|
|
|
|
|
|
|
|
dump_tree(n->first_child, rsp, depth + 1);
|
|
|
|
dump_tree(n->second_child, rsp, depth + 1);
|
|
|
|
}
|
2012-09-04 21:01:50 +02:00
|
|
|
|
|
|
|
void update_root_dimensions(void)
|
|
|
|
{
|
2012-09-12 14:56:51 +02:00
|
|
|
root_rect.x = left_padding + window_gap;
|
|
|
|
root_rect.y = top_padding + window_gap;
|
|
|
|
root_rect.width = screen_width - (left_padding + right_padding + window_gap);
|
|
|
|
root_rect.height = screen_height - (top_padding + bottom_padding + window_gap);
|
2012-09-04 21:01:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void apply_layout(desktop_t *d, node_t *n)
|
|
|
|
{
|
2012-09-11 16:29:43 +02:00
|
|
|
if (d == NULL || n == NULL)
|
2012-09-04 21:01:50 +02:00
|
|
|
return;
|
2012-09-12 14:56:51 +02:00
|
|
|
if (is_leaf(n) && is_tiled(n->client)) {
|
2012-09-05 14:07:06 +02:00
|
|
|
xcb_rectangle_t rect;
|
2012-09-06 14:43:59 +02:00
|
|
|
if (d->layout == LAYOUT_TILED)
|
2012-09-05 14:07:06 +02:00
|
|
|
rect = n->rectangle;
|
2012-09-06 14:43:59 +02:00
|
|
|
else if (d->layout == LAYOUT_MONOCLE)
|
2012-09-05 14:07:06 +02:00
|
|
|
rect = d->root->rectangle;
|
2012-09-12 14:56:51 +02:00
|
|
|
window_move_resize(n->client->window, rect.x + border_width, rect.y + border_width, rect.width - window_gap - 2 * border_width, rect.height - window_gap - 2 * border_width);
|
2012-09-04 21:01:50 +02:00
|
|
|
} else {
|
|
|
|
xcb_rectangle_t rect = n->rectangle;
|
2012-09-12 14:56:51 +02:00
|
|
|
if (n->parent == NULL) {
|
|
|
|
rect = root_rect;
|
|
|
|
} else if (n->first_child->vacant || n->second_child->vacant) {
|
2012-09-07 14:24:03 +02:00
|
|
|
n->first_child->rectangle = n->second_child->rectangle = rect;
|
|
|
|
} else {
|
|
|
|
unsigned int fence;
|
|
|
|
if (n->split_type == TYPE_VERTICAL) {
|
|
|
|
fence = rect.width * n->split_ratio;
|
|
|
|
n->first_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
|
|
|
|
n->second_child->rectangle = (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;
|
|
|
|
n->first_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
|
|
|
|
n->second_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
|
|
|
|
}
|
2012-09-04 21:01:50 +02:00
|
|
|
}
|
|
|
|
apply_layout(d, n->first_child);
|
|
|
|
apply_layout(d, n->second_child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-05 14:07:06 +02:00
|
|
|
void insert_node(desktop_t *d, node_t *n)
|
|
|
|
{
|
|
|
|
if (d == NULL || n == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
switch (split_mode) {
|
|
|
|
case MODE_AUTOMATIC:
|
|
|
|
if (fopar == NULL) {
|
|
|
|
dad->first_child = n;
|
|
|
|
dad->second_child = focus;
|
|
|
|
dad->split_type = TYPE_VERTICAL;
|
|
|
|
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;
|
|
|
|
if (is_first_child(focus)) {
|
|
|
|
dad->first_child = n;
|
|
|
|
dad->second_child = fopar;
|
|
|
|
rotate_tree(fopar, ROTATE_CLOCK_WISE);
|
|
|
|
} else {
|
|
|
|
dad->first_child = fopar;
|
|
|
|
dad->second_child = n;
|
|
|
|
rotate_tree(fopar, ROTATE_COUNTER_CW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MODE_MANUAL:
|
|
|
|
focus->parent = dad;
|
|
|
|
switch (split_dir) {
|
|
|
|
case DIR_LEFT:
|
2012-09-05 18:37:54 +02:00
|
|
|
dad->split_type = TYPE_VERTICAL;
|
|
|
|
dad->first_child = n;
|
|
|
|
dad->second_child = focus;
|
|
|
|
break;
|
2012-09-05 14:07:06 +02:00
|
|
|
case DIR_RIGHT:
|
2012-09-05 18:37:54 +02:00
|
|
|
dad->split_type = TYPE_VERTICAL;
|
|
|
|
dad->first_child = focus;
|
|
|
|
dad->second_child = n;
|
|
|
|
break;
|
2012-09-05 14:07:06 +02:00
|
|
|
case DIR_UP:
|
2012-09-05 18:37:54 +02:00
|
|
|
dad->split_type = TYPE_HORIZONTAL;
|
|
|
|
dad->first_child = n;
|
|
|
|
dad->second_child = focus;
|
|
|
|
break;
|
2012-09-05 14:07:06 +02:00
|
|
|
case DIR_DOWN:
|
2012-09-05 18:37:54 +02:00
|
|
|
dad->split_type = TYPE_HORIZONTAL;
|
|
|
|
dad->first_child = focus;
|
|
|
|
dad->second_child = n;
|
|
|
|
break;
|
2012-09-05 14:07:06 +02:00
|
|
|
}
|
|
|
|
if (d->root == focus)
|
|
|
|
d->root = dad;
|
|
|
|
split_mode = MODE_AUTOMATIC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void focus_node(desktop_t *d, node_t *n)
|
|
|
|
{
|
2012-09-11 22:48:26 +02:00
|
|
|
if (d == NULL || n == NULL || desk->focus == n)
|
2012-09-05 14:07:06 +02:00
|
|
|
return;
|
|
|
|
|
2012-09-11 22:48:26 +02:00
|
|
|
select_desktop(d);
|
2012-09-12 14:56:51 +02:00
|
|
|
|
2012-09-11 22:48:26 +02:00
|
|
|
if (d->focus != n) {
|
|
|
|
draw_triple_border(d->focus, normal_border_color_pxl);
|
|
|
|
draw_triple_border(n, active_border_color_pxl);
|
|
|
|
}
|
2012-09-05 14:07:06 +02:00
|
|
|
|
|
|
|
xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
|
|
|
|
}
|
2012-09-04 21:01:50 +02:00
|
|
|
|
2012-09-11 16:29:43 +02:00
|
|
|
void unlink_node(desktop_t *d, node_t *n)
|
2012-09-05 18:37:54 +02:00
|
|
|
{
|
|
|
|
if (d == NULL || n == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node_t *p = n->parent;
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
d->root = NULL;
|
|
|
|
d->focus = NULL;
|
|
|
|
d->last_focus = NULL;
|
|
|
|
} else {
|
|
|
|
node_t *b;
|
|
|
|
node_t *g = p->parent;
|
|
|
|
if (is_first_child(n))
|
|
|
|
b = p->second_child;
|
|
|
|
else
|
|
|
|
b = p->first_child;
|
|
|
|
b->parent = g;
|
|
|
|
if (g != NULL) {
|
|
|
|
if (is_first_child(p))
|
|
|
|
g->first_child = b;
|
|
|
|
else
|
|
|
|
g->second_child = b;
|
|
|
|
} else {
|
|
|
|
d->root = b;
|
|
|
|
}
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2012-09-11 16:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void remove_node(desktop_t *d, node_t *n)
|
|
|
|
{
|
|
|
|
unlink_node(d, n);
|
|
|
|
free(n->client);
|
|
|
|
free(n);
|
2012-09-05 18:37:54 +02:00
|
|
|
}
|
|
|
|
|
2012-09-06 14:26:13 +02:00
|
|
|
void transfer_node(desktop_t *ds, desktop_t *dd, node_t *n)
|
|
|
|
{
|
2012-09-12 14:56:51 +02:00
|
|
|
if (n == NULL || ds == NULL || dd == NULL || dd == ds)
|
2012-09-06 14:26:13 +02:00
|
|
|
return;
|
2012-09-11 16:29:43 +02:00
|
|
|
unlink_node(ds, n);
|
2012-09-06 14:26:13 +02:00
|
|
|
insert_node(dd, n);
|
|
|
|
}
|
2012-09-06 14:43:59 +02:00
|
|
|
|
2012-09-11 16:29:43 +02:00
|
|
|
void select_desktop(desktop_t *d)
|
|
|
|
{
|
2012-09-11 22:48:26 +02:00
|
|
|
if (d == NULL || d == desk)
|
2012-09-11 16:29:43 +02:00
|
|
|
return;
|
2012-09-11 22:48:26 +02:00
|
|
|
|
|
|
|
if (d->focus != NULL)
|
|
|
|
xcb_map_window(dpy, d->focus->client->window);
|
|
|
|
|
|
|
|
node_t *n = first_extrema(d->root);
|
|
|
|
|
|
|
|
while (n != NULL) {
|
|
|
|
if (n != d->focus)
|
|
|
|
xcb_map_window(dpy, n->client->window);
|
|
|
|
n = next_leaf(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
n = first_extrema(desk->root);
|
|
|
|
while (n != NULL) {
|
|
|
|
if (n != desk->focus)
|
|
|
|
xcb_unmap_window(dpy, n->client->window);
|
|
|
|
n = next_leaf(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desk->focus != NULL)
|
|
|
|
xcb_unmap_window(dpy, desk->focus->client->window);
|
|
|
|
|
|
|
|
last_desk = desk;
|
|
|
|
desk = d;
|
2012-09-11 16:29:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-12 14:56:51 +02:00
|
|
|
void cycle_leaf(desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
|
|
|
|
{
|
|
|
|
if (n == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node_t *f = (dir == DIR_PREV ? prev_leaf(n) : next_leaf(n));
|
|
|
|
|
|
|
|
while (f != NULL) {
|
|
|
|
bool tiled = is_tiled(f->client);
|
|
|
|
if (skip == SKIP_NONE || (skip == SKIP_TILED && !tiled) || (skip == SKIP_FLOATING && tiled))
|
|
|
|
focus_node(d, f);
|
|
|
|
f = (dir == DIR_PREV ? prev_leaf(f) : next_leaf(f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:55:03 +02:00
|
|
|
void update_vacant_state(node_t *n)
|
|
|
|
{
|
|
|
|
if (n == NULL)
|
|
|
|
return;
|
|
|
|
if (!is_leaf(n))
|
|
|
|
n->vacant = (n->first_child->vacant && n->second_child->vacant);
|
|
|
|
update_vacant_state(n->parent);
|
|
|
|
}
|
2012-09-12 14:56:51 +02:00
|
|
|
|
|
|
|
bool is_tiled(client_t *c)
|
|
|
|
{
|
|
|
|
if (c == NULL)
|
|
|
|
return false;
|
|
|
|
return (!c->floating && !c->transient && !c->fullscreen);
|
|
|
|
}
|