4 #include <xcb/xcb_event.h>
12 bool is_leaf(node_t *n)
14 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
17 bool is_first_child(node_t *n)
19 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
22 bool is_second_child(node_t *n)
24 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
28 void change_split_ratio(node_t *n, value_change_t chg) {
29 n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
32 node_t *first_extrema(node_t *n)
36 else if (n->first_child == NULL)
39 return first_extrema(n->first_child);
42 node_t *second_extrema(node_t *n)
46 else if (n->second_child == NULL)
49 return second_extrema(n->second_child);
52 node_t *next_leaf(node_t *n)
57 while (is_second_child(p))
59 if (p->parent == NULL)
61 return first_extrema(p->parent->second_child);
64 node_t *prev_leaf(node_t *n)
69 while (is_first_child(p))
71 if (p->parent == NULL)
73 return second_extrema(p->parent->first_child);
76 node_t *find_fence(node_t *n, direction_t dir)
86 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
87 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
88 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
89 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
97 node_t *find_neighbor(node_t *n, direction_t dir)
99 node_t *fence = find_fence(n, dir);
104 if (dir == DIR_UP || dir == DIR_LEFT)
105 return second_extrema(fence->first_child);
106 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
107 return first_extrema(fence->second_child);
112 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
114 node_t *fence = find_fence(n, dir);
119 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
120 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
121 change_split_ratio(fence, CHANGE_INCREASE);
123 change_split_ratio(fence, CHANGE_DECREASE);
127 void rotate_tree(node_t *n, rotate_t rot)
129 if (n == NULL || is_leaf(n))
134 if ((rot == ROTATE_CLOCK_WISE && n->split_type == TYPE_HORIZONTAL)
135 || (rot == ROTATE_COUNTER_CW && n->split_type == TYPE_VERTICAL)
136 || rot == ROTATE_FULL_CYCLE) {
137 tmp = n->first_child;
138 n->first_child = n->second_child;
139 n->second_child = tmp;
140 n->split_ratio = 1.0 - n->split_ratio;
143 if (rot != ROTATE_FULL_CYCLE) {
144 if (n->split_type == TYPE_HORIZONTAL)
145 n->split_type = TYPE_VERTICAL;
146 else if (n->split_type == TYPE_VERTICAL)
147 n->split_type = TYPE_HORIZONTAL;
150 rotate_tree(n->first_child, rot);
151 rotate_tree(n->second_child, rot);
154 void dump_tree(node_t *n, char *rsp, int depth)
159 for (int i = 0; i < depth; i++)
160 sprintf(rsp, "%s", " ");
162 if (n->client == NULL)
163 sprintf(rsp, "%s %.2f\n", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
165 sprintf(rsp, "%X\n", n->client->window);
167 dump_tree(n->first_child, rsp, depth + 1);
168 dump_tree(n->second_child, rsp, depth + 1);
171 void update_root_dimensions(void)
173 desktop_t *d = desk_head;
175 d->root->rectangle = (xcb_rectangle_t) {left_padding, top_padding, screen_width - (left_padding + right_padding), screen_height - (top_padding + bottom_padding)};
180 void apply_layout(desktop_t *d, node_t *n)
186 xcb_rectangle_t rect;
187 if (desk->layout == LAYOUT_TILED)
189 else if (desk->layout == LAYOUT_MONOCLE)
190 rect = d->root->rectangle;
193 values[2] = rect.width;
194 values[3] = rect.height;
195 xcb_configure_window(dpy, n->client->window, MOVE_RESIZE_MASK, values);
198 xcb_rectangle_t rect = n->rectangle;
199 if (n->split_type == TYPE_VERTICAL) {
200 fence = rect.width * n->split_ratio;
201 n->first_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
202 n->second_child->rectangle = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
204 } else if (n->split_type == TYPE_HORIZONTAL) {
205 fence = rect.height * n->split_ratio;
206 n->first_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
207 n->second_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
209 apply_layout(d, n->first_child);
210 apply_layout(d, n->second_child);
214 void insert_node(desktop_t *d, node_t *n)
216 if (d == NULL || n == NULL)
219 node_t *focus = d->focus;
224 node_t *dad = make_node();
225 node_t *fopar = focus->parent;
227 switch (split_mode) {
230 dad->first_child = n;
231 dad->second_child = focus;
232 dad->split_type = TYPE_VERTICAL;
236 node_t *grandpa = fopar->parent;
237 dad->parent = grandpa;
238 if (grandpa != NULL) {
239 if (is_first_child(fopar))
240 grandpa->first_child = dad;
242 grandpa->second_child = dad;
246 dad->split_type = fopar->split_type;
247 dad->split_ratio = fopar->split_ratio;
249 if (is_first_child(focus)) {
250 dad->first_child = n;
251 dad->second_child = fopar;
252 rotate_tree(fopar, ROTATE_CLOCK_WISE);
254 dad->first_child = fopar;
255 dad->second_child = n;
256 rotate_tree(fopar, ROTATE_COUNTER_CW);
264 dad->split_type = TYPE_VERTICAL;
265 dad->first_child = n;
266 dad->second_child = focus;
269 dad->split_type = TYPE_VERTICAL;
270 dad->first_child = focus;
271 dad->second_child = n;
274 dad->split_type = TYPE_HORIZONTAL;
275 dad->first_child = n;
276 dad->second_child = focus;
279 dad->split_type = TYPE_HORIZONTAL;
280 dad->first_child = focus;
281 dad->second_child = n;
284 if (d->root == focus)
286 split_mode = MODE_AUTOMATIC;
292 void focus_node(desktop_t *d, node_t *n)
294 if (d == NULL || n == NULL || d->focus == n)
297 draw_triple_border(d->focus, normal_border_color_pxl);
298 draw_triple_border(n, active_border_color_pxl);
300 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
303 void select_desktop(desktop_t *d)