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 xcb_rectangle_t root_rect = {
175 left_padding + window_gap,
176 top_padding + window_gap,
177 screen_width - (left_padding + right_padding + window_gap),
178 screen_height - (top_padding + bottom_padding + window_gap)
180 desktop_t *d = desk_head;
182 d->root->rectangle = root_rect;
187 void apply_layout(desktop_t *d, node_t *n)
191 if (is_leaf(n) && !n->client->floating && !n->client->fullscreen) {
193 xcb_rectangle_t rect;
194 if (d->layout == LAYOUT_TILED)
196 else if (d->layout == LAYOUT_MONOCLE)
197 rect = d->root->rectangle;
200 values[2] = rect.width - window_gap;
201 values[3] = rect.height - window_gap;
202 xcb_configure_window(dpy, n->client->window, MOVE_RESIZE_MASK, values);
204 xcb_rectangle_t rect = n->rectangle;
205 if (n->first_child->vacant || n->second_child->vacant) {
206 n->first_child->rectangle = n->second_child->rectangle = rect;
209 if (n->split_type == TYPE_VERTICAL) {
210 fence = rect.width * n->split_ratio;
211 n->first_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
212 n->second_child->rectangle = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
214 } else if (n->split_type == TYPE_HORIZONTAL) {
215 fence = rect.height * n->split_ratio;
216 n->first_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
217 n->second_child->rectangle = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
220 apply_layout(d, n->first_child);
221 apply_layout(d, n->second_child);
225 void insert_node(desktop_t *d, node_t *n)
227 if (d == NULL || n == NULL)
230 node_t *focus = d->focus;
235 node_t *dad = make_node();
236 node_t *fopar = focus->parent;
238 switch (split_mode) {
241 dad->first_child = n;
242 dad->second_child = focus;
243 dad->split_type = TYPE_VERTICAL;
247 node_t *grandpa = fopar->parent;
248 dad->parent = grandpa;
249 if (grandpa != NULL) {
250 if (is_first_child(fopar))
251 grandpa->first_child = dad;
253 grandpa->second_child = dad;
257 dad->split_type = fopar->split_type;
258 dad->split_ratio = fopar->split_ratio;
260 if (is_first_child(focus)) {
261 dad->first_child = n;
262 dad->second_child = fopar;
263 rotate_tree(fopar, ROTATE_CLOCK_WISE);
265 dad->first_child = fopar;
266 dad->second_child = n;
267 rotate_tree(fopar, ROTATE_COUNTER_CW);
275 dad->split_type = TYPE_VERTICAL;
276 dad->first_child = n;
277 dad->second_child = focus;
280 dad->split_type = TYPE_VERTICAL;
281 dad->first_child = focus;
282 dad->second_child = n;
285 dad->split_type = TYPE_HORIZONTAL;
286 dad->first_child = n;
287 dad->second_child = focus;
290 dad->split_type = TYPE_HORIZONTAL;
291 dad->first_child = focus;
292 dad->second_child = n;
295 if (d->root == focus)
297 split_mode = MODE_AUTOMATIC;
303 void focus_node(desktop_t *d, node_t *n)
305 if (d == NULL || n == NULL || d->focus == n)
308 draw_triple_border(d->focus, normal_border_color_pxl);
309 draw_triple_border(n, active_border_color_pxl);
311 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
314 void remove_node(desktop_t *d, node_t *n)
316 if (d == NULL || n == NULL)
319 node_t *p = n->parent;
324 d->last_focus = NULL;
327 node_t *g = p->parent;
328 if (is_first_child(n))
334 if (is_first_child(p))
344 /* free(n->client); */
348 void transfer_node(desktop_t *ds, desktop_t *dd, node_t *n)
350 if (ds == NULL || dd == NULL || n == NULL)
356 void update_vacant_state(node_t *n)
361 n->vacant = (n->first_child->vacant && n->second_child->vacant);
362 update_vacant_state(n->parent);
365 void select_desktop(desktop_t *d)