bspwm/tree.c

809 lines
22 KiB
C
Raw Normal View History

2012-08-20 22:38:29 +02:00
#include <stdio.h>
2012-09-05 18:37:54 +02:00
#include <stdlib.h>
#include <string.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"
2012-10-03 20:07:33 +03:00
#include "helpers.h"
2012-09-18 17:21:04 +02:00
#include "window.h"
2012-08-20 22:38:29 +02:00
#include "types.h"
2012-09-04 21:01:50 +02:00
#include "bspwm.h"
#include "ewmh.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-18 17:21:04 +02:00
bool is_tiled(client_t *c)
{
if (c == NULL)
return false;
return (!c->floating && !c->transient && !c->fullscreen);
}
bool is_floating(client_t *c)
{
if (c == NULL)
return false;
2012-09-18 19:18:02 +02:00
return (c->floating && !c->fullscreen);
2012-09-18 17:21:04 +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-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;
2012-09-30 18:41:28 +03:00
if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
2012-08-20 12:20:12 +02:00
|| (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;
if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
|| (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
2012-08-18 12:19:58 +02:00
|| 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-09-26 12:11:45 +02:00
void magnetise_tree(node_t *n, corner_t corner)
{
2012-09-30 18:41:28 +03:00
if (n == NULL || is_leaf(n))
2012-09-26 12:11:45 +02:00
return;
PUTS("magnetise tree");
switch (n->split_type) {
case TYPE_HORIZONTAL:
if (corner == TOP_LEFT || corner == TOP_RIGHT)
change_split_ratio(n, CHANGE_DECREASE);
else
change_split_ratio(n, CHANGE_INCREASE);
break;
case TYPE_VERTICAL:
if (corner == TOP_LEFT || corner == BOTTOM_LEFT)
change_split_ratio(n, CHANGE_DECREASE);
else
change_split_ratio(n, CHANGE_INCREASE);
break;
default:
break;
}
magnetise_tree(n->first_child, corner);
magnetise_tree(n->second_child, corner);
}
2012-10-17 16:18:40 +02:00
void dump_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
2012-08-18 22:36:46 +02:00
{
if (n == NULL)
return;
char line[MAXLEN];
2012-10-17 16:18:40 +02:00
for (unsigned int i = 0; i < depth; i++)
strncat(rsp, " ", REMLEN(rsp));
if (is_leaf(n))
snprintf(line, sizeof(line), "%s %X %s%s%s%s%s", n->client->class_name, n->client->window, (n->client->floating ? "f" : "-"), (n->client->transient ? "t" : "-"), (n->client->fullscreen ? "F" : "-"), (n->client->urgent ? "u" : "-"), (n->client->locked ? "l" : "-"));
else
snprintf(line, sizeof(line), "%s %.2f", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
strncat(rsp, line, REMLEN(rsp));
2012-08-18 22:36:46 +02:00
2012-09-13 21:45:40 +02:00
if (n == d->focus)
strncat(rsp, " *\n", REMLEN(rsp));
2012-08-18 22:36:46 +02:00
else
strncat(rsp, "\n", REMLEN(rsp));
2012-08-18 22:36:46 +02:00
2012-09-13 21:45:40 +02:00
dump_tree(d, n->first_child, rsp, depth + 1);
dump_tree(d, n->second_child, rsp, depth + 1);
2012-08-18 22:36:46 +02:00
}
2012-09-04 21:01:50 +02:00
void refresh_current(void) {
2012-10-17 16:18:40 +02:00
if (mon->desk->focus == NULL)
ewmh_update_active_window();
else
2012-10-17 16:18:40 +02:00
focus_node(mon, mon->desk, mon->desk->focus, true);
}
2012-10-17 16:18:40 +02:00
void list_monitors(list_option_t opt, char *rsp)
{
2012-10-17 16:18:40 +02:00
monitor_t *m = mon_head;
while (m != NULL) {
strncat(rsp, m->name, REMLEN(rsp));
if (mon == m)
strncat(rsp, " #\n", REMLEN(rsp));
else
strncat(rsp, "\n", REMLEN(rsp));
if (opt == LIST_OPTION_VERBOSE)
list_desktops(m, opt, 1, rsp);
m = m->next;
}
}
void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
{
desktop_t *d = m->desk_head;
while (d != NULL) {
2012-10-17 16:18:40 +02:00
for (unsigned int i = 0; i < depth; i++)
strncat(rsp, " ", REMLEN(rsp));
strncat(rsp, d->name, REMLEN(rsp));
2012-10-17 16:18:40 +02:00
if (m->desk == d)
strncat(rsp, " @\n", REMLEN(rsp));
else
strncat(rsp, "\n", REMLEN(rsp));
2012-10-17 16:18:40 +02:00
if (opt == LIST_OPTION_VERBOSE)
dump_tree(d, d->root, rsp, depth + 1);
d = d->next;
}
}
2012-10-17 16:18:40 +02:00
void arrange(monitor_t *m, desktop_t *d)
2012-09-04 21:01:50 +02:00
{
2012-10-17 16:18:40 +02:00
xcb_rectangle_t rect = m->rectangle;
rect.x += left_padding + window_gap;
rect.y += top_padding + window_gap;
rect.width -= left_padding + right_padding + window_gap;
rect.height -= top_padding + bottom_padding + window_gap;
2012-10-23 15:30:30 +02:00
if (focus_follows_mouse)
get_pointer_position(&pointer_position);
2012-10-18 11:15:43 +02:00
apply_layout(m, d, d->root, rect, rect);
2012-10-17 16:18:40 +02:00
}
2012-10-18 11:15:43 +02:00
void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
2012-10-17 16:18:40 +02:00
{
if (n == NULL)
2012-09-04 21:01:50 +02:00
return;
n->rectangle = rect;
if (is_leaf(n)) {
2012-09-18 17:21:04 +02:00
if (n->client->fullscreen)
return;
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;
}
2012-09-29 10:10:58 +02:00
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 bleed = window_gap + 2 * n->client->border_width;
r.width = (bleed < r.width ? r.width - bleed : 1);
r.height = (bleed < r.height ? r.height - bleed : 1);
2012-09-21 17:39:22 +02:00
n->client->tiled_rectangle = r;
} else {
2012-09-21 17:39:22 +02:00
r = n->client->floating_rectangle;
}
2012-09-18 17:21:04 +02:00
window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
window_border_width(n->client->window, n->client->border_width);
2012-10-17 16:18:40 +02:00
window_draw_border(n, n == d->focus, m == mon);
2012-09-18 17:21:04 +02:00
if (d->layout == LAYOUT_MONOCLE && n == d->focus)
window_raise(n->client->window);
2012-09-18 17:21:04 +02:00
2012-09-04 21:01:50 +02:00
} 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;
2012-09-07 14:24:03 +02:00
} 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};
2012-09-07 14:24:03 +02:00
} 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};
2012-09-07 14:24:03 +02:00
}
2012-09-04 21:01:50 +02:00
}
2012-10-18 11:15:43 +02:00
apply_layout(m, d, n->first_child, first_rect, root_rect);
apply_layout(m, d, n->second_child, second_rect, root_rect);
2012-09-04 21:01:50 +02:00
}
}
2012-09-05 14:07:06 +02:00
void insert_node(desktop_t *d, node_t *n)
{
if (d == NULL || n == NULL)
return;
2012-09-21 12:15:25 +02:00
PRINTF("insert node %X\n", n->client->window);
2012-09-18 17:21:04 +02:00
2012-09-05 14:07:06 +02:00
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;
n->born_as = split_mode;
2012-09-05 14:07:06 +02:00
switch (split_mode) {
case MODE_AUTOMATIC:
if (fopar == NULL) {
dad->first_child = n;
dad->second_child = focus;
2012-09-24 20:15:25 +02:00
if (focus->rectangle.width > focus->rectangle.height)
dad->split_type = TYPE_VERTICAL;
else
dad->split_type = TYPE_HORIZONTAL;
2012-09-05 14:07:06 +02:00
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_CLOCKWISE);
2012-09-05 14:07:06 +02:00
} else {
dad->first_child = fopar;
dad->second_child = n;
rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
2012-09-05 14:07:06 +02:00
}
}
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;
2012-09-05 14:07:06 +02:00
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;
}
if (focus->vacant)
update_vacant_state(fopar);
2012-09-05 14:07:06 +02:00
}
}
2012-10-17 16:18:40 +02:00
void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
2012-09-05 14:07:06 +02:00
{
if (n == NULL)
2012-09-05 14:07:06 +02:00
return;
2012-09-21 12:15:25 +02:00
PRINTF("focus node %X\n", n->client->window);
2012-09-18 17:21:04 +02:00
split_mode = MODE_AUTOMATIC;
n->client->urgent = false;
2012-09-18 17:21:04 +02:00
if (is_mapped) {
2012-10-17 16:18:40 +02:00
if (mon != m || d->focus != n) {
window_draw_border(d->focus, m != mon, m == mon);
window_draw_border(n, true, true);
2012-09-18 17:21:04 +02:00
}
xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
}
2012-09-18 17:21:04 +02:00
if (!is_tiled(n->client) || d->layout == LAYOUT_MONOCLE)
window_raise(n->client->window);
else if (is_tiled(n->client))
window_lower(n->client->window);
if (d->focus != n) {
d->last_focus = d->focus;
d->focus = n;
}
2012-09-18 17:21:04 +02:00
2012-10-17 16:18:40 +02:00
ewmh_update_active_window();
2012-09-05 14:07:06 +02:00
}
2012-09-04 21:01:50 +02:00
void update_current(void)
{
2012-10-17 16:18:40 +02:00
if (mon->desk->focus == NULL)
ewmh_update_active_window();
else
2012-10-17 16:18:40 +02:00
focus_node(mon, mon->desk, mon->desk->focus, true);
}
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;
2012-09-21 12:15:25 +02:00
PRINTF("unlink node %X\n", n->client->window);
2012-09-18 17:21:04 +02:00
2012-09-05 18:37:54 +02:00
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;
bool n_first_child = is_first_child(n);
if (n_first_child) {
2012-09-05 18:37:54 +02:00
b = p->second_child;
if (n->born_as == MODE_AUTOMATIC)
rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
} else {
2012-09-05 18:37:54 +02:00
b = p->first_child;
if (n->born_as == MODE_AUTOMATIC)
rotate_tree(b, ROTATE_CLOCKWISE);
}
2012-09-05 18:37:54 +02:00
b->parent = g;
if (g != NULL) {
if (is_first_child(p))
g->first_child = b;
else
g->second_child = b;
} else {
d->root = b;
}
2012-09-13 21:45:40 +02:00
n->parent = NULL;
2012-09-05 18:37:54 +02:00
free(p);
2012-09-13 21:45:40 +02:00
if (n == d->last_focus) {
d->last_focus = NULL;
} else if (n == d->focus) {
2012-09-24 15:17:22 +02:00
if (d->last_focus != NULL)
2012-09-18 17:21:04 +02:00
d->focus = d->last_focus;
else
d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
2012-09-18 17:21:04 +02:00
d->last_focus = NULL;
}
update_vacant_state(b->parent);
2012-09-05 18:37:54 +02:00
}
2012-09-11 16:29:43 +02:00
}
void remove_node(desktop_t *d, node_t *n)
{
2012-09-18 17:21:04 +02:00
if (d == NULL || n == NULL)
return;
PRINTF("remove node %X\n", n->client->window);
2012-09-18 17:21:04 +02:00
2012-09-11 16:29:43 +02:00
unlink_node(d, n);
free(n->client);
free(n);
2012-09-18 17:21:04 +02:00
num_clients--;
2012-09-18 17:21:04 +02:00
ewmh_update_client_list();
2012-10-17 16:18:40 +02:00
if (mon->desk == d)
update_current();
}
void swap_nodes(node_t *n1, node_t *n2)
{
if (n1 == NULL || n2 == NULL || n1 == n2)
return;
2012-09-19 23:52:38 +02:00
PUTS("swap nodes");
2012-09-18 17:21:04 +02:00
/* (n1 and n2 are leaves) */
node_t *pn1 = n1->parent;
node_t *pn2 = n2->parent;
2012-09-18 19:48:34 +02:00
bool n1_first_child = is_first_child(n1);
bool n2_first_child = is_first_child(n2);
if (pn1 != NULL) {
2012-09-18 19:48:34 +02:00
if (n1_first_child)
pn1->first_child = n2;
else
pn1->second_child = n2;
}
if (pn2 != NULL) {
2012-09-18 19:48:34 +02:00
if (n2_first_child)
pn2->first_child = n1;
else
pn2->second_child = n1;
}
n1->parent = pn2;
n2->parent = pn1;
if (n1->vacant != n2->vacant) {
update_vacant_state(n1->parent);
update_vacant_state(n2->parent);
}
}
2012-10-17 16:18:40 +02:00
void fit_monitor(monitor_t *m, client_t *c)
{
xcb_rectangle_t cr = c->floating_rectangle;
xcb_rectangle_t mr = m->rectangle;
if (cr.x < mr.x)
cr.x += mr.x;
if (cr.y < mr.y)
cr.y += mr.y;
}
void translate_coordinates(monitor_t *ms, monitor_t *md, client_t *c)
{
if (ms == md)
return;
uint32_t vx = md->rectangle.x - ms->rectangle.x;
uint32_t vy = md->rectangle.y - ms->rectangle.y;
c->floating_rectangle.x += vx;
c->floating_rectangle.y += vy;
}
void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
2012-09-06 14:26:13 +02:00
{
2012-10-17 16:18:40 +02:00
if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
2012-09-06 14:26:13 +02:00
return;
2012-09-18 17:21:04 +02:00
2012-09-21 12:15:25 +02:00
PRINTF("transfer node %X\n", n->client->window);
2012-09-18 17:21:04 +02:00
2012-09-11 16:29:43 +02:00
unlink_node(ds, n);
2012-10-10 21:36:19 +02:00
insert_node(dd, n);
ewmh_set_wm_desktop(n, dd);
2012-10-17 16:18:40 +02:00
if (ds == ms->desk && dd != md->desk) {
window_hide(n->client->window);
2012-09-21 12:15:25 +02:00
}
2012-10-17 16:18:40 +02:00
translate_coordinates(ms, 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);
2012-10-17 16:18:40 +02:00
focus_node(md, dd, n, true);
2012-09-19 23:52:38 +02:00
} else {
2012-10-17 16:18:40 +02:00
focus_node(md, dd, n, false);
2012-09-19 23:52:38 +02:00
}
2012-10-17 16:18:40 +02:00
if (ds == ms->desk || dd == md->desk)
update_current();
2012-09-06 14:26:13 +02:00
}
2012-09-06 14:43:59 +02:00
2012-10-17 16:18:40 +02:00
void select_monitor(monitor_t *m)
{
if (m == NULL || mon == m)
return;
PRINTF("select monitor %s\n", m->name);
focus_node(m, m->desk, m->desk->focus, true);
last_mon = mon;
mon = m;
ewmh_update_current_desktop();
}
2012-09-11 16:29:43 +02:00
void select_desktop(desktop_t *d)
{
2012-10-17 16:18:40 +02:00
if (d == NULL || d == mon->desk)
2012-09-11 16:29:43 +02:00
return;
2012-09-21 17:39:22 +02:00
PRINTF("select desktop %s\n", d->name);
2012-09-18 17:21:04 +02:00
node_t *n = first_extrema(d->root);
while (n != NULL) {
window_show(n->client->window);
n = next_leaf(n);
}
2012-10-17 16:18:40 +02:00
n = first_extrema(mon->desk->root);
while (n != NULL) {
window_hide(n->client->window);
n = next_leaf(n);
}
2012-10-17 16:18:40 +02:00
mon->last_desk = mon->desk;
mon->desk = d;
2012-09-18 17:21:04 +02:00
update_current();
2012-09-18 17:21:04 +02:00
ewmh_update_current_desktop();
2012-09-11 16:29:43 +02:00
}
2012-10-17 16:18:40 +02:00
void cycle_monitor(cycle_dir_t dir)
{
if (dir == CYCLE_NEXT)
select_monitor((mon->next == NULL ? mon_head : mon->next));
else if (dir == CYCLE_PREV)
select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
}
void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
{
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)) {
select_desktop(f);
return;
}
f = (dir == CYCLE_PREV ? f->prev : f->next);
if (f == NULL)
f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
}
}
2012-10-23 12:17:19 +02:00
void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
{
if (n == NULL)
return;
2012-09-19 23:52:38 +02:00
PUTS("cycle leaf");
2012-09-18 17:21:04 +02:00
2012-09-22 16:32:35 +02:00
node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
if (f == NULL)
2012-09-22 16:32:35 +02:00
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)) {
2012-10-23 12:17:19 +02:00
focus_node(m, d, f, true);
2012-09-13 21:45:40 +02:00
return;
}
2012-09-22 16:32:35 +02:00
f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
if (f == NULL)
2012-09-22 16:32:35 +02:00
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);
}
2012-09-07 13:55:03 +02:00
void update_vacant_state(node_t *n)
{
if (n == NULL)
return;
2012-09-18 17:21:04 +02:00
2012-09-19 23:52:38 +02:00
PUTS("update vacant state");
2012-09-18 17:21:04 +02:00
2012-09-13 21:55:29 +02:00
/* n is not a leaf */
2012-09-18 17:21:04 +02:00
node_t *p = n;
while (p != NULL) {
p->vacant = (p->first_child->vacant && p->second_child->vacant);
p = p->parent;
}
2012-09-07 13:55:03 +02:00
}
2012-10-17 16:18:40 +02:00
monitor_t *find_monitor(char *name)
{
2012-10-17 16:18:40 +02:00
for (monitor_t *m = mon_head; m != NULL; m = m->next)
if (strcmp(m->name, name) == 0)
return m;
2012-09-18 17:21:04 +02:00
return NULL;
}
2012-10-17 16:18:40 +02:00
void add_desktop(monitor_t *m, char *name)
2012-09-18 17:21:04 +02:00
{
desktop_t *d = make_desktop(name);
2012-10-17 16:18:40 +02:00
if (m->desk == NULL) {
m->desk = d;
m->desk_head = d;
m->desk_tail = d;
} else {
m->desk_tail->next = d;
d->prev = m->desk_tail;
m->desk_tail = d;
}
2012-09-18 17:21:04 +02:00
num_desktops++;
ewmh_update_number_of_desktops();
ewmh_update_desktop_names();
}
2012-10-17 16:18:40 +02:00
void add_monitor(xcb_rectangle_t *rect)
{
monitor_t *m = make_monitor(rect);
if (mon == NULL) {
mon = m;
mon_head = m;
mon_tail = m;
} else {
mon_tail->next = m;
m->prev = mon_tail;
mon_tail = m;
}
num_monitors++;
}