]> git.sur5r.net Git - i3/i3/blob - src/render.c
ddb3751a91bacdcf50be54eea6a79cba4e337f0b
[i3/i3] / src / render.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #include "all.h"
6
7 /* change this to 'true' if you want to have additional borders around every
8  * container (for debugging purposes) */
9 static bool show_debug_borders = false;
10
11 /*
12  * "Renders" the given container (and its children), meaning that all rects are
13  * updated correctly. Note that this function does not call any xcb_*
14  * functions, so the changes are completely done in memory only (and
15  * side-effect free). As soon as you call x_push_changes(), the changes will be
16  * updated in X11.
17  *
18  */
19 void render_con(Con *con) {
20     printf("currently rendering node %p / %s / layout %d\n",
21             con, con->name, con->layout);
22     int children = con_num_children(con);
23     printf("children: %d, orientation = %d\n", children, con->orientation);
24
25     /* Copy container rect, subtract container border */
26     /* This is the actually usable space inside this container for clients */
27     Rect rect = con->rect;
28
29     /* Display a border if this is a leaf node. For container nodes, we don’t
30      * draw borders (except when in debug mode) */
31     if (show_debug_borders) {
32         rect.x += 2;
33         rect.y += 2;
34         rect.width -= 2 * 2;
35         rect.height -= 2 * 2;
36     }
37
38     int x = rect.x;
39     int y = rect.y;
40
41     int i = 0;
42
43     printf("mapped = true\n");
44     con->mapped = true;
45
46     /* if this container contains a window, set the coordinates */
47     if (con->window) {
48         /* depending on the border style, the rect of the child window
49          * needs to be smaller */
50         Rect *inset = &(con->window_rect);
51         *inset = (Rect){0, 0, con->rect.width, con->rect.height};
52         *inset = rect_add(*inset, con_border_style_rect(con));
53
54         /* Obey the aspect ratio, if any */
55         if (con->proportional_height != 0 &&
56             con->proportional_width != 0) {
57             DLOG("proportional height = %d, width = %d\n", con->proportional_height, con->proportional_width);
58             double new_height = inset->height + 1;
59             int new_width = inset->width;
60
61             while (new_height > inset->height) {
62                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
63
64                 if (new_height > inset->height)
65                     new_width--;
66             }
67             /* Center the window */
68             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
69             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
70
71             inset->height = new_height;
72             inset->width = new_width;
73             DLOG("new_height = %f, new_width = %d\n", new_height, new_width);
74         }
75
76         if (con->height_increment > 1) {
77             int old_height = inset->height;
78             inset->height -= (inset->height - con->base_height) % con->height_increment;
79             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
80                 old_height - inset->height, con->height_increment, con->base_height);
81         }
82
83         if (con->width_increment > 1) {
84             int old_width = inset->width;
85             inset->width -= (inset->width - con->base_width) % con->width_increment;
86             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
87                 old_width - inset->width, con->width_increment, con->base_width);
88         }
89
90         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
91     }
92
93     /* Check for fullscreen nodes */
94     Con *fullscreen = con_get_fullscreen_con(con);
95     if (fullscreen) {
96         LOG("got fs node: %p\n", fullscreen);
97         fullscreen->rect = rect;
98         x_raise_con(fullscreen);
99         render_con(fullscreen);
100         return;
101     }
102
103     Con *child;
104     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
105
106         /* default layout */
107         if (con->layout == L_DEFAULT) {
108             double percentage = 1.0 / children;
109             if (child->percent > 0.0)
110                 percentage = child->percent;
111             printf("child %p / %s requests percentage %f\n",
112                     child, child->name, percentage);
113
114             if (con->orientation == HORIZ) {
115                 child->rect.x = x;
116                 child->rect.y = y;
117                 child->rect.width = percentage * rect.width;
118                 child->rect.height = rect.height;
119                 x += child->rect.width;
120             } else {
121                 child->rect.x = x;
122                 child->rect.y = y;
123                 child->rect.width = rect.width;
124                 child->rect.height = percentage * rect.height;
125                 y += child->rect.height;
126             }
127
128             /* first we have the decoration, if this is a leaf node */
129             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
130                 printf("that child is a leaf node, subtracting deco\n");
131                 /* TODO: make a function for relative coords? */
132                 child->deco_rect.x = child->rect.x - con->rect.x;
133                 child->deco_rect.y = child->rect.y - con->rect.y;
134
135                 child->rect.y += 17;
136                 child->rect.height -= 17;
137
138                 child->deco_rect.width = child->rect.width;
139                 child->deco_rect.height = 17;
140             }
141         }
142
143         if (con->layout == L_STACKED) {
144             printf("stacked con\n");
145             child->rect.x = x;
146             child->rect.y = y;
147             child->rect.width = rect.width;
148             child->rect.height = rect.height;
149
150             child->rect.y += (17 * children);
151             child->rect.height -= (17 * children);
152
153             child->deco_rect.x = x - con->rect.x;
154             child->deco_rect.y = y - con->rect.y + (i * 17);
155             child->deco_rect.width = child->rect.width;
156             child->deco_rect.height = 17;
157         }
158
159         printf("child at (%d, %d) with (%d x %d)\n",
160                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
161         printf("x now %d, y now %d\n", x, y);
162         x_raise_con(child);
163         render_con(child);
164         i++;
165     }
166
167     /* in a stacking container, we ensure the focused client is raised */
168     if (con->layout == L_STACKED) {
169         Con *foc = TAILQ_FIRST(&(con->focus_head));
170         if (foc != TAILQ_END(&(con->focus_head))) {
171             LOG("con %p is stacking, raising %p\n", con, foc);
172             x_raise_con(foc);
173             /* by rendering the stacked container again, we handle the case
174              * that we have a non-leaf-container inside the stack. */
175             render_con(foc);
176         }
177     }
178
179     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
180         LOG("render floating:\n");
181         LOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
182         x_raise_con(child);
183         render_con(child);
184     }
185
186     printf("-- level up\n");
187 }