6 #include <xcb/xcb_event.h>
15 bool is_leaf(node_t *n)
17 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
20 bool is_tiled(client_t *c)
24 return (!c->floating && !c->transient && !c->fullscreen);
27 bool is_floating(client_t *c)
31 return (c->floating && !c->fullscreen);
34 bool is_first_child(node_t *n)
36 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
39 bool is_second_child(node_t *n)
41 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
44 void change_split_ratio(node_t *n, value_change_t chg) {
45 n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
48 node_t *first_extrema(node_t *n)
52 else if (n->first_child == NULL)
55 return first_extrema(n->first_child);
58 node_t *second_extrema(node_t *n)
62 else if (n->second_child == NULL)
65 return second_extrema(n->second_child);
68 node_t *next_leaf(node_t *n)
73 while (is_second_child(p))
75 if (p->parent == NULL)
77 return first_extrema(p->parent->second_child);
80 node_t *prev_leaf(node_t *n)
85 while (is_first_child(p))
87 if (p->parent == NULL)
89 return second_extrema(p->parent->first_child);
92 node_t *find_fence(node_t *n, direction_t dir)
102 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
103 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
104 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
105 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
113 node_t *find_neighbor(node_t *n, direction_t dir)
115 node_t *fence = find_fence(n, dir);
120 if (dir == DIR_UP || dir == DIR_LEFT)
121 return second_extrema(fence->first_child);
122 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
123 return first_extrema(fence->second_child);
128 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
130 node_t *fence = find_fence(n, dir);
135 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
136 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
137 change_split_ratio(fence, CHANGE_INCREASE);
139 change_split_ratio(fence, CHANGE_DECREASE);
142 void rotate_tree(node_t *n, rotate_t rot)
144 if (n == NULL || is_leaf(n))
149 if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
150 || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
151 || rot == ROTATE_FULL_CYCLE) {
152 tmp = n->first_child;
153 n->first_child = n->second_child;
154 n->second_child = tmp;
155 n->split_ratio = 1.0 - n->split_ratio;
158 if (rot != ROTATE_FULL_CYCLE) {
159 if (n->split_type == TYPE_HORIZONTAL)
160 n->split_type = TYPE_VERTICAL;
161 else if (n->split_type == TYPE_VERTICAL)
162 n->split_type = TYPE_HORIZONTAL;
165 rotate_tree(n->first_child, rot);
166 rotate_tree(n->second_child, rot);
169 void magnetise_tree(node_t *n, corner_t corner)
171 if (n == NULL || is_leaf(n))
174 PUTS("magnetise tree");
176 switch (n->split_type) {
177 case TYPE_HORIZONTAL:
178 if (corner == TOP_LEFT || corner == TOP_RIGHT)
179 change_split_ratio(n, CHANGE_DECREASE);
181 change_split_ratio(n, CHANGE_INCREASE);
184 if (corner == TOP_LEFT || corner == BOTTOM_LEFT)
185 change_split_ratio(n, CHANGE_DECREASE);
187 change_split_ratio(n, CHANGE_INCREASE);
193 magnetise_tree(n->first_child, corner);
194 magnetise_tree(n->second_child, corner);
197 void dump_tree(desktop_t *d, node_t *n, char *rsp, int depth)
204 for (int i = 0; i < depth; i++)
205 strncat(rsp, " ", REMLEN(rsp));
208 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" : "-"));
210 snprintf(line, sizeof(line), "%s %.2f", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
212 strncat(rsp, line, REMLEN(rsp));
215 strncat(rsp, " *\n", REMLEN(rsp));
217 strncat(rsp, "\n", REMLEN(rsp));
219 dump_tree(d, n->first_child, rsp, depth + 1);
220 dump_tree(d, n->second_child, rsp, depth + 1);
223 void refresh_current(void) {
224 if (desk->focus == NULL)
225 ewmh_update_active_window();
227 focus_node(desk, desk->focus, true);
230 void list_desktops(char *rsp)
232 desktop_t *d = desk_head;
235 strncat(rsp, d->name, REMLEN(rsp));
237 strncat(rsp, " @\n", REMLEN(rsp));
239 strncat(rsp, "\n", REMLEN(rsp));
240 dump_tree(d, d->root, rsp, 1);
245 void update_root_dimensions(void)
247 root_rect.x = left_padding + window_gap;
248 root_rect.y = top_padding + window_gap;
249 root_rect.width = screen_width - (left_padding + right_padding + window_gap);
250 root_rect.height = screen_height - (top_padding + bottom_padding + window_gap);
253 void apply_layout(desktop_t *d, node_t *n, xcb_rectangle_t rect)
255 if (d == NULL || n == NULL)
261 if (n->client->fullscreen)
264 if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
265 n->client->border_width = 0;
267 n->client->border_width = border_width;
270 if (is_tiled(n->client)) {
271 if (d->layout == LAYOUT_TILED)
273 else if (d->layout == LAYOUT_MONOCLE)
275 int bleed = window_gap + 2 * n->client->border_width;
276 r.width = (bleed < r.width ? r.width - bleed : 1);
277 r.height = (bleed < r.height ? r.height - bleed : 1);
278 n->client->tiled_rectangle = r;
280 r = n->client->floating_rectangle;
283 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
284 window_border_width(n->client->window, n->client->border_width);
285 window_draw_border(n, n == d->focus);
287 if (d->layout == LAYOUT_MONOCLE && n == d->focus)
288 window_raise(n->client->window);
291 xcb_rectangle_t first_rect;
292 xcb_rectangle_t second_rect;
294 if (n->first_child->vacant || n->second_child->vacant) {
295 first_rect = second_rect = rect;
298 if (n->split_type == TYPE_VERTICAL) {
299 fence = rect.width * n->split_ratio;
300 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
301 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
303 } else if (n->split_type == TYPE_HORIZONTAL) {
304 fence = rect.height * n->split_ratio;
305 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
306 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
310 apply_layout(d, n->first_child, first_rect);
311 apply_layout(d, n->second_child, second_rect);
315 void insert_node(desktop_t *d, node_t *n)
317 if (d == NULL || n == NULL)
320 PRINTF("insert node %X\n", n->client->window);
322 node_t *focus = d->focus;
327 node_t *dad = make_node();
328 node_t *fopar = focus->parent;
330 n->born_as = split_mode;
331 switch (split_mode) {
334 dad->first_child = n;
335 dad->second_child = focus;
336 if (focus->rectangle.width > focus->rectangle.height)
337 dad->split_type = TYPE_VERTICAL;
339 dad->split_type = TYPE_HORIZONTAL;
343 node_t *grandpa = fopar->parent;
344 dad->parent = grandpa;
345 if (grandpa != NULL) {
346 if (is_first_child(fopar))
347 grandpa->first_child = dad;
349 grandpa->second_child = dad;
353 dad->split_type = fopar->split_type;
354 dad->split_ratio = fopar->split_ratio;
356 if (is_first_child(focus)) {
357 dad->first_child = n;
358 dad->second_child = fopar;
359 rotate_tree(fopar, ROTATE_CLOCKWISE);
361 dad->first_child = fopar;
362 dad->second_child = n;
363 rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
369 if (is_first_child(focus))
370 fopar->first_child = dad;
372 fopar->second_child = dad;
374 dad->split_ratio = focus->split_ratio;
379 dad->split_type = TYPE_VERTICAL;
380 dad->first_child = n;
381 dad->second_child = focus;
384 dad->split_type = TYPE_VERTICAL;
385 dad->first_child = focus;
386 dad->second_child = n;
389 dad->split_type = TYPE_HORIZONTAL;
390 dad->first_child = n;
391 dad->second_child = focus;
394 dad->split_type = TYPE_HORIZONTAL;
395 dad->first_child = focus;
396 dad->second_child = n;
399 if (d->root == focus)
401 split_mode = MODE_AUTOMATIC;
405 update_vacant_state(fopar);
409 void focus_node(desktop_t *d, node_t *n, bool is_mapped)
414 PRINTF("focus node %X\n", n->client->window);
416 split_mode = MODE_AUTOMATIC;
417 n->client->urgent = false;
421 window_draw_border(d->focus, false);
422 window_draw_border(n, true);
424 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
427 if (!is_tiled(n->client) || d->layout == LAYOUT_MONOCLE)
428 window_raise(n->client->window);
429 else if (is_tiled(n->client))
430 window_lower(n->client->window);
433 d->last_focus = d->focus;
437 ewmh_update_active_window();
440 void update_current(void)
442 if (desk->focus == NULL)
443 ewmh_update_active_window();
445 focus_node(desk, desk->focus, true);
448 void unlink_node(desktop_t *d, node_t *n)
450 if (d == NULL || n == NULL)
453 PRINTF("unlink node %X\n", n->client->window);
455 node_t *p = n->parent;
460 d->last_focus = NULL;
463 node_t *g = p->parent;
464 bool n_first_child = is_first_child(n);
467 if (n->born_as == MODE_AUTOMATIC)
468 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
471 if (n->born_as == MODE_AUTOMATIC)
472 rotate_tree(b, ROTATE_CLOCKWISE);
476 if (is_first_child(p))
487 if (n == d->last_focus) {
488 d->last_focus = NULL;
489 } else if (n == d->focus) {
490 if (d->last_focus != NULL)
491 d->focus = d->last_focus;
493 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
494 d->last_focus = NULL;
497 update_vacant_state(b->parent);
501 void remove_node(desktop_t *d, node_t *n)
503 if (d == NULL || n == NULL)
506 PRINTF("remove node %X\n", n->client->window);
513 ewmh_update_client_list();
519 void swap_nodes(node_t *n1, node_t *n2)
521 if (n1 == NULL || n2 == NULL || n1 == n2)
526 /* (n1 and n2 are leaves) */
527 node_t *pn1 = n1->parent;
528 node_t *pn2 = n2->parent;
529 bool n1_first_child = is_first_child(n1);
530 bool n2_first_child = is_first_child(n2);
534 pn1->first_child = n2;
536 pn1->second_child = n2;
541 pn2->first_child = n1;
543 pn2->second_child = n1;
549 if (n1->vacant != n2->vacant) {
550 update_vacant_state(n1->parent);
551 update_vacant_state(n2->parent);
555 void transfer_node(desktop_t *ds, desktop_t *dd, node_t *n)
557 if (n == NULL || ds == NULL || dd == NULL || dd == ds)
560 PRINTF("transfer node %X\n", n->client->window);
565 window_hide(n->client->window);
571 window_show(n->client->window);
572 focus_node(dd, n, true);
574 focus_node(dd, n, false);
577 if (ds == desk || dd == desk)
581 void select_desktop(desktop_t *d)
583 if (d == NULL || d == desk)
586 PRINTF("select desktop %s\n", d->name);
588 node_t *n = first_extrema(d->root);
591 window_show(n->client->window);
595 n = first_extrema(desk->root);
598 window_hide(n->client->window);
606 ewmh_update_current_desktop();
609 void cycle_desktop(cycle_dir_t dir)
611 if (dir == CYCLE_NEXT)
612 select_desktop((desk->next == NULL ? desk_head : desk->next));
613 else if (dir == CYCLE_PREV)
614 select_desktop((desk->prev == NULL ? desk_tail : desk->prev));
617 void cycle_leaf(desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
624 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
626 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
629 bool tiled = is_tiled(f->client);
630 if (skip == SKIP_NONE || (skip == SKIP_TILED && !tiled) || (skip == SKIP_FLOATING && tiled)
631 || (skip == SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
632 || (skip == SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
633 focus_node(d, f, true);
636 f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
638 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
642 void update_vacant_state(node_t *n)
647 PUTS("update vacant state");
649 /* n is not a leaf */
653 p->vacant = (p->first_child->vacant && p->second_child->vacant);
658 desktop_t *find_desktop(char *name)
660 desktop_t *d = desk_head;
662 if (strcmp(d->name, name) == 0)
669 void add_desktop(char *name)
671 desktop_t *d = make_desktop(name);
676 ewmh_update_number_of_desktops();
677 ewmh_update_desktop_names();