5 #include <xcb/xcb_event.h>
13 bool is_leaf(node_t *n)
15 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
18 bool is_first_child(node_t *n)
20 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
23 bool is_second_child(node_t *n)
25 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
29 void change_split_ratio(node_t *n, value_change_t chg) {
30 n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
33 node_t *first_extrema(node_t *n)
37 else if (n->first_child == NULL)
40 return first_extrema(n->first_child);
43 node_t *second_extrema(node_t *n)
47 else if (n->second_child == NULL)
50 return second_extrema(n->second_child);
53 node_t *next_leaf(node_t *n)
58 while (is_second_child(p))
60 if (p->parent == NULL)
62 return first_extrema(p->parent->second_child);
65 node_t *prev_leaf(node_t *n)
70 while (is_first_child(p))
72 if (p->parent == NULL)
74 return second_extrema(p->parent->first_child);
77 node_t *find_fence(node_t *n, direction_t dir)
87 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
88 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
89 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
90 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
98 node_t *find_neighbor(node_t *n, direction_t dir)
100 node_t *fence = find_fence(n, dir);
105 if (dir == DIR_UP || dir == DIR_LEFT)
106 return second_extrema(fence->first_child);
107 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
108 return first_extrema(fence->second_child);
113 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
115 node_t *fence = find_fence(n, dir);
120 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
121 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
122 change_split_ratio(fence, CHANGE_INCREASE);
124 change_split_ratio(fence, CHANGE_DECREASE);
128 void rotate_tree(node_t *n, rotate_t rot)
130 if (n == NULL || is_leaf(n))
135 if ((rot == ROTATE_CLOCK_WISE && n->split_type == TYPE_HORIZONTAL)
136 || (rot == ROTATE_COUNTER_CW && n->split_type == TYPE_VERTICAL)
137 || rot == ROTATE_FULL_CYCLE) {
138 tmp = n->first_child;
139 n->first_child = n->second_child;
140 n->second_child = tmp;
141 n->split_ratio = 1.0 - n->split_ratio;
144 if (rot != ROTATE_FULL_CYCLE) {
145 if (n->split_type == TYPE_HORIZONTAL)
146 n->split_type = TYPE_VERTICAL;
147 else if (n->split_type == TYPE_VERTICAL)
148 n->split_type = TYPE_HORIZONTAL;
151 rotate_tree(n->first_child, rot);
152 rotate_tree(n->second_child, rot);
155 void dump_tree(node_t *n, char *rsp, int depth)
160 for (int i = 0; i < depth; i++)
161 sprintf(rsp, "%s", " ");
163 if (n->client == NULL)
164 sprintf(rsp, "%s %.2f\n", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
166 sprintf(rsp, "%X\n", n->client->window);
168 dump_tree(n->first_child, rsp, depth + 1);
169 dump_tree(n->second_child, rsp, depth + 1);
172 void update_root_dimensions(void)
174 desktop_t *d = desk_head;
176 d->root->rectangle = (xcb_rectangle_t) {left_padding, top_padding, screen_width - (left_padding + right_padding), screen_height - (top_padding + bottom_padding)};
181 void apply_layout(desktop_t *d, node_t *n)
187 xcb_rectangle_t rect;
188 if (desk->layout == LAYOUT_TILED)
190 else if (desk->layout == LAYOUT_MONOCLE)
191 rect = d->root->rectangle;
194 values[2] = rect.width;
195 values[3] = rect.height;
196 xcb_configure_window(dpy, n->client->window, MOVE_RESIZE_MASK, values);
199 xcb_rectangle_t rect = n->rectangle;
200 if (n->split_type == TYPE_VERTICAL) {
201 fence = rect.width * n->split_ratio;
202 n->first_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
203 n->second_child->rectangle = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
205 } else if (n->split_type == TYPE_HORIZONTAL) {
206 fence = rect.height * n->split_ratio;
207 n->first_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
208 n->second_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
210 apply_layout(d, n->first_child);
211 apply_layout(d, n->second_child);
215 void insert_node(desktop_t *d, node_t *n)
217 if (d == NULL || n == NULL)
220 node_t *focus = d->focus;
225 node_t *dad = make_node();
226 node_t *fopar = focus->parent;
228 switch (split_mode) {
231 dad->first_child = n;
232 dad->second_child = focus;
233 dad->split_type = TYPE_VERTICAL;
237 node_t *grandpa = fopar->parent;
238 dad->parent = grandpa;
239 if (grandpa != NULL) {
240 if (is_first_child(fopar))
241 grandpa->first_child = dad;
243 grandpa->second_child = dad;
247 dad->split_type = fopar->split_type;
248 dad->split_ratio = fopar->split_ratio;
250 if (is_first_child(focus)) {
251 dad->first_child = n;
252 dad->second_child = fopar;
253 rotate_tree(fopar, ROTATE_CLOCK_WISE);
255 dad->first_child = fopar;
256 dad->second_child = n;
257 rotate_tree(fopar, ROTATE_COUNTER_CW);
265 dad->split_type = TYPE_VERTICAL;
266 dad->first_child = n;
267 dad->second_child = focus;
270 dad->split_type = TYPE_VERTICAL;
271 dad->first_child = focus;
272 dad->second_child = n;
275 dad->split_type = TYPE_HORIZONTAL;
276 dad->first_child = n;
277 dad->second_child = focus;
280 dad->split_type = TYPE_HORIZONTAL;
281 dad->first_child = focus;
282 dad->second_child = n;
285 if (d->root == focus)
287 split_mode = MODE_AUTOMATIC;
293 void focus_node(desktop_t *d, node_t *n)
295 if (d == NULL || n == NULL || d->focus == n)
298 draw_triple_border(d->focus, normal_border_color_pxl);
299 draw_triple_border(n, active_border_color_pxl);
301 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
304 void select_desktop(desktop_t *d)
310 void remove_node(desktop_t *d, node_t *n)
312 if (d == NULL || n == NULL)
315 node_t *p = n->parent;
320 d->last_focus = NULL;
323 node_t *g = p->parent;
324 if (is_first_child(n))
330 if (is_first_child(p))