6 #include <xcb/xcb_event.h>
16 bool is_leaf(node_t *n)
18 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
21 bool is_tiled(client_t *c)
25 return (!c->floating && !c->transient && !c->fullscreen);
28 bool is_floating(client_t *c)
32 return (c->floating && !c->fullscreen);
35 bool is_first_child(node_t *n)
37 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
40 bool is_second_child(node_t *n)
42 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
45 void change_split_ratio(node_t *n, value_change_t chg) {
46 n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
49 node_t *first_extrema(node_t *n)
53 else if (n->first_child == NULL)
56 return first_extrema(n->first_child);
59 node_t *second_extrema(node_t *n)
63 else if (n->second_child == NULL)
66 return second_extrema(n->second_child);
69 node_t *next_leaf(node_t *n)
74 while (is_second_child(p))
76 if (p->parent == NULL)
78 return first_extrema(p->parent->second_child);
81 node_t *prev_leaf(node_t *n)
86 while (is_first_child(p))
88 if (p->parent == NULL)
90 return second_extrema(p->parent->first_child);
93 node_t *find_fence(node_t *n, direction_t dir)
103 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
104 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
105 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
106 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
114 node_t *find_neighbor(node_t *n, direction_t dir)
116 node_t *fence = find_fence(n, dir);
121 if (dir == DIR_UP || dir == DIR_LEFT)
122 return second_extrema(fence->first_child);
123 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
124 return first_extrema(fence->second_child);
129 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
131 node_t *fence = find_fence(n, dir);
136 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
137 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
138 change_split_ratio(fence, CHANGE_INCREASE);
140 change_split_ratio(fence, CHANGE_DECREASE);
143 void rotate_tree(node_t *n, rotate_t rot)
145 if (n == NULL || is_leaf(n))
150 if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
151 || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
152 || rot == ROTATE_FULL_CYCLE) {
153 tmp = n->first_child;
154 n->first_child = n->second_child;
155 n->second_child = tmp;
156 n->split_ratio = 1.0 - n->split_ratio;
159 if (rot != ROTATE_FULL_CYCLE) {
160 if (n->split_type == TYPE_HORIZONTAL)
161 n->split_type = TYPE_VERTICAL;
162 else if (n->split_type == TYPE_VERTICAL)
163 n->split_type = TYPE_HORIZONTAL;
166 rotate_tree(n->first_child, rot);
167 rotate_tree(n->second_child, rot);
170 void magnetise_tree(node_t *n, corner_t corner)
172 if (n == NULL || is_leaf(n))
175 PUTS("magnetise tree");
177 switch (n->split_type) {
178 case TYPE_HORIZONTAL:
179 if (corner == TOP_LEFT || corner == TOP_RIGHT)
180 change_split_ratio(n, CHANGE_DECREASE);
182 change_split_ratio(n, CHANGE_INCREASE);
185 if (corner == TOP_LEFT || corner == BOTTOM_LEFT)
186 change_split_ratio(n, CHANGE_DECREASE);
188 change_split_ratio(n, CHANGE_INCREASE);
194 magnetise_tree(n->first_child, corner);
195 magnetise_tree(n->second_child, corner);
198 void dump_tree(desktop_t *d, node_t *n, char *rsp, int depth)
205 for (int i = 0; i < depth; i++)
209 sprintf(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" : "-"));
211 sprintf(line, "%s %.2f", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
220 dump_tree(d, n->first_child, rsp, depth + 1);
221 dump_tree(d, n->second_child, rsp, depth + 1);
224 void refresh_current(void) {
225 if (desk->focus == NULL)
226 ewmh_update_active_window();
228 focus_node(desk, desk->focus, true);
231 void list_desktops(char *rsp)
233 desktop_t *d = desk_head;
236 strcat(rsp, d->name);
241 dump_tree(d, d->root, rsp, 1);
246 void update_root_dimensions(void)
248 root_rect.x = left_padding + window_gap;
249 root_rect.y = top_padding + window_gap;
250 root_rect.width = screen_width - (left_padding + right_padding + window_gap);
251 root_rect.height = screen_height - (top_padding + bottom_padding + window_gap);
254 void apply_layout(desktop_t *d, node_t *n, xcb_rectangle_t rect)
256 if (d == NULL || n == NULL)
262 if (n->client->fullscreen)
265 if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
266 n->client->border_width = 0;
268 n->client->border_width = border_width;
271 if (is_tiled(n->client)) {
272 if (d->layout == LAYOUT_TILED)
274 else if (d->layout == LAYOUT_MONOCLE)
276 int bleed = window_gap + 2 * n->client->border_width;
277 r.width = (bleed < r.width ? r.width - bleed : 1);
278 r.height = (bleed < r.height ? r.height - bleed : 1);
279 n->client->tiled_rectangle = r;
281 r = n->client->floating_rectangle;
284 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
285 window_border_width(n->client->window, n->client->border_width);
286 window_draw_border(n, n == d->focus);
288 if (d->layout == LAYOUT_MONOCLE && n == d->focus)
289 window_raise(n->client->window);
292 xcb_rectangle_t first_rect;
293 xcb_rectangle_t second_rect;
295 if (n->first_child->vacant || n->second_child->vacant) {
296 first_rect = second_rect = rect;
299 if (n->split_type == TYPE_VERTICAL) {
300 fence = rect.width * n->split_ratio;
301 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
302 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
304 } else if (n->split_type == TYPE_HORIZONTAL) {
305 fence = rect.height * n->split_ratio;
306 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
307 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
311 apply_layout(d, n->first_child, first_rect);
312 apply_layout(d, n->second_child, second_rect);
316 void insert_node(desktop_t *d, node_t *n)
318 if (d == NULL || n == NULL)
321 PRINTF("insert node %X\n", n->client->window);
323 node_t *focus = d->focus;
328 node_t *dad = make_node();
329 node_t *fopar = focus->parent;
331 n->born_as = split_mode;
332 switch (split_mode) {
335 dad->first_child = n;
336 dad->second_child = focus;
337 if (focus->rectangle.width > focus->rectangle.height)
338 dad->split_type = TYPE_VERTICAL;
340 dad->split_type = TYPE_HORIZONTAL;
344 node_t *grandpa = fopar->parent;
345 dad->parent = grandpa;
346 if (grandpa != NULL) {
347 if (is_first_child(fopar))
348 grandpa->first_child = dad;
350 grandpa->second_child = dad;
354 dad->split_type = fopar->split_type;
355 dad->split_ratio = fopar->split_ratio;
357 if (is_first_child(focus)) {
358 dad->first_child = n;
359 dad->second_child = fopar;
360 rotate_tree(fopar, ROTATE_CLOCKWISE);
362 dad->first_child = fopar;
363 dad->second_child = n;
364 rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
370 if (is_first_child(focus))
371 fopar->first_child = dad;
373 fopar->second_child = dad;
375 dad->split_ratio = focus->split_ratio;
380 dad->split_type = TYPE_VERTICAL;
381 dad->first_child = n;
382 dad->second_child = focus;
385 dad->split_type = TYPE_VERTICAL;
386 dad->first_child = focus;
387 dad->second_child = n;
390 dad->split_type = TYPE_HORIZONTAL;
391 dad->first_child = n;
392 dad->second_child = focus;
395 dad->split_type = TYPE_HORIZONTAL;
396 dad->first_child = focus;
397 dad->second_child = n;
400 if (d->root == focus)
402 split_mode = MODE_AUTOMATIC;
406 update_vacant_state(fopar);
410 void focus_node(desktop_t *d, node_t *n, bool is_mapped)
415 PRINTF("focus node %X\n", n->client->window);
417 split_mode = MODE_AUTOMATIC;
418 n->client->urgent = false;
422 window_draw_border(d->focus, false);
423 window_draw_border(n, true);
425 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
428 if (!is_tiled(n->client) || d->layout == LAYOUT_MONOCLE)
429 window_raise(n->client->window);
430 else if (is_tiled(n->client))
431 window_lower(n->client->window);
434 d->last_focus = d->focus;
438 ewmh_update_active_window();
441 void update_current(void)
443 if (desk->focus == NULL)
444 ewmh_update_active_window();
446 focus_node(desk, desk->focus, true);
449 void unlink_node(desktop_t *d, node_t *n)
451 if (d == NULL || n == NULL)
454 PRINTF("unlink node %X\n", n->client->window);
456 node_t *p = n->parent;
461 d->last_focus = NULL;
464 node_t *g = p->parent;
465 bool n_first_child = is_first_child(n);
468 if (n->born_as == MODE_AUTOMATIC)
469 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
472 if (n->born_as == MODE_AUTOMATIC)
473 rotate_tree(b, ROTATE_CLOCKWISE);
477 if (is_first_child(p))
488 if (n == d->last_focus) {
489 d->last_focus = NULL;
490 } else if (n == d->focus) {
491 if (d->last_focus != NULL)
492 d->focus = d->last_focus;
494 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
495 d->last_focus = NULL;
498 update_vacant_state(b->parent);
502 void remove_node(desktop_t *d, node_t *n)
504 if (d == NULL || n == NULL)
507 PRINTF("remove node %X\n", n->client->window);
514 ewmh_update_client_list();
520 void swap_nodes(node_t *n1, node_t *n2)
522 if (n1 == NULL || n2 == NULL || n1 == n2)
527 /* (n1 and n2 are leaves) */
528 node_t *pn1 = n1->parent;
529 node_t *pn2 = n2->parent;
530 bool n1_first_child = is_first_child(n1);
531 bool n2_first_child = is_first_child(n2);
535 pn1->first_child = n2;
537 pn1->second_child = n2;
542 pn2->first_child = n1;
544 pn2->second_child = n1;
550 if (n1->vacant != n2->vacant) {
551 update_vacant_state(n1->parent);
552 update_vacant_state(n2->parent);
556 void transfer_node(desktop_t *ds, desktop_t *dd, node_t *n)
558 if (n == NULL || ds == NULL || dd == NULL || dd == ds)
561 PRINTF("transfer node %X\n", n->client->window);
566 window_hide(n->client->window);
572 window_show(n->client->window);
573 focus_node(dd, n, true);
575 focus_node(dd, n, false);
578 if (ds == desk || dd == desk)
582 void select_desktop(desktop_t *d)
584 if (d == NULL || d == desk)
587 PRINTF("select desktop %s\n", d->name);
589 node_t *n = first_extrema(d->root);
592 window_show(n->client->window);
596 n = first_extrema(desk->root);
599 window_hide(n->client->window);
607 ewmh_update_current_desktop();
610 void cycle_desktop(cycle_dir_t dir)
612 if (dir == CYCLE_NEXT)
613 select_desktop((desk->next == NULL ? desk_head : desk->next));
614 else if (dir == CYCLE_PREV)
615 select_desktop((desk->prev == NULL ? desk_tail : desk->prev));
618 void cycle_leaf(desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
625 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
627 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
630 bool tiled = is_tiled(f->client);
631 if (skip == SKIP_NONE || (skip == SKIP_TILED && !tiled) || (skip == SKIP_FLOATING && tiled)
632 || (skip == SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
633 || (skip == SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
634 focus_node(d, f, true);
637 f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
639 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
643 void update_vacant_state(node_t *n)
648 PUTS("update vacant state");
650 /* n is not a leaf */
654 p->vacant = (p->first_child->vacant && p->second_child->vacant);
659 desktop_t *find_desktop(char *name)
661 desktop_t *d = desk_head;
663 if (strcmp(d->name, name) == 0)
670 void add_desktop(char *name)
672 desktop_t *d = make_desktop(name);
677 ewmh_update_number_of_desktops();
678 ewmh_update_desktop_names();