4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * layout.c: Functions handling layout/drawing of window decorations
27 * Gets the unoccupied space (= space which is available for windows which were resized by the user)
28 * for the given row. This is necessary to render both, customly resized windows and never touched
29 * windows correctly, meaning that the aspect ratio will be maintained when opening new windows.
32 int get_unoccupied_x(Workspace *workspace, int row) {
33 int unoccupied = workspace->rect.width;
34 float default_factor = ((float)workspace->rect.width / workspace->cols) / workspace->rect.width;
36 printf("get_unoccupied_x(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
38 for (int cols = 0; cols < workspace->cols;) {
39 Container *con = workspace->table[cols][row];
40 printf("width_factor[%d][%d] = %f, colspan = %d\n", cols, row, con->width_factor, con->colspan);
41 if (con->width_factor == 0)
42 unoccupied -= workspace->rect.width * default_factor * con->colspan;
46 assert(unoccupied != 0);
48 printf("unoccupied space: %d\n", unoccupied);
52 /* See get_unoccupied_x() */
53 int get_unoccupied_y(Workspace *workspace, int col) {
54 int unoccupied = workspace->rect.height;
55 float default_factor = ((float)workspace->rect.height / workspace->rows) / workspace->rect.height;
57 printf("get_unoccupied_y(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
59 for (int rows = 0; rows < workspace->rows;) {
60 Container *con = workspace->table[col][rows];
61 printf("height_factor[%d][%d] = %f, rowspan %d\n", col, rows, con->height_factor, con->rowspan);
62 if (con->height_factor == 0)
63 unoccupied -= workspace->rect.height * default_factor * con->rowspan;
67 assert(unoccupied != 0);
69 printf("unoccupied space: %d\n", unoccupied);
74 * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
75 * When in stacking mode, the window decorations are drawn onto an own window.
78 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
79 i3Font *font = load_font(conn, pattern);
80 uint32_t background_color,
84 /* Clients without a container (docks) won’t get decorated */
85 if (client->container == NULL)
88 if (client->container->currently_focused == client) {
89 background_color = get_colorpixel(conn, client, client->frame, "#285577");
90 text_color = get_colorpixel(conn, client, client->frame, "#ffffff");
91 border_color = get_colorpixel(conn, client, client->frame, "#4c7899");
93 background_color = get_colorpixel(conn, client, client->frame, "#222222");
94 text_color = get_colorpixel(conn, client, client->frame, "#888888");
95 border_color = get_colorpixel(conn, client, client->frame, "#333333");
98 /* Our plan is the following:
99 - Draw a rect around the whole client in background_color
100 - Draw two lines in a lighter color
101 - Draw the window’s title
104 /* Draw a green rectangle around the window */
105 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
107 xcb_rectangle_t rect = {0, offset, client->rect.width, offset + client->rect.height};
108 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
111 xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
112 xcb_draw_line(conn, drawable, gc, border_color, 2, offset + font->height + 3,
113 2 + client->rect.width, offset + font->height + 3);
116 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
117 uint32_t values[] = { text_color, background_color, font->id };
118 xcb_change_gc(conn, gc, mask, values);
122 asprintf(&label, "(%08x) %.*s", client->frame, client->name_len, client->name);
123 xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), drawable,
124 gc, 3 /* X */, offset + font->height /* Y = baseline of font */, label);
125 check_error(conn, text_cookie, "Could not draw client's title");
130 * Pushes the client’s x and y coordinates to X11
133 static void reposition_client(xcb_connection_t *connection, Client *client) {
134 printf("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
135 /* Note: We can use a pointer to client->x like an array of uint32_ts
136 because it is followed by client->y by definition */
137 xcb_configure_window(connection, client->frame,
138 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
142 * Pushes the client’s width/height to X11 and resizes the child window
145 static void resize_client(xcb_connection_t *connection, Client *client) {
146 i3Font *font = load_font(connection, pattern);
148 printf("resizing client to %d x %d\n", client->rect.width, client->rect.height);
149 xcb_configure_window(connection, client->frame,
150 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
151 &(client->rect.width));
153 /* Adjust the position of the child inside its frame.
154 * The coordinates of the child are relative to its frame, we
155 * add a border of 2 pixel to each value */
156 uint32_t mask = XCB_CONFIG_WINDOW_X |
157 XCB_CONFIG_WINDOW_Y |
158 XCB_CONFIG_WINDOW_WIDTH |
159 XCB_CONFIG_WINDOW_HEIGHT;
161 if (client->titlebar_position == TITLEBAR_OFF ||
162 client->container->mode == MODE_STACK) {
165 rect.width = client->rect.width;
166 rect.height = client->rect.height;
169 rect.y = font->height + 2 + 2;
170 rect.width = client->rect.width - (2 + 2);
171 rect.height = client->rect.height - ((font->height + 2 + 2) + 2);
174 printf("child will be at %dx%d with size %dx%d\n", rect.x, rect.y, rect.width, rect.height);
176 xcb_configure_window(connection, client->child, mask, &(rect.x));
180 * Renders the given container. Is called by render_layout() or individually (for example
181 * when focus changes in a stacking container)
184 void render_container(xcb_connection_t *connection, Container *container) {
186 int num_clients = 0, current_client = 0;
188 if (container->currently_focused == NULL)
191 CIRCLEQ_FOREACH(client, &(container->clients), clients)
194 if (container->mode == MODE_DEFAULT) {
195 printf("got %d clients in this default container.\n", num_clients);
196 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
197 /* Check if we changed client->x or client->y by updating it.
198 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
199 if (client->force_reconfigure |
200 (client->rect.x != (client->rect.x = container->x)) |
201 (client->rect.y != (client->rect.y = container->y +
202 (container->height / num_clients) * current_client))) {
203 reposition_client(connection, client);
206 /* TODO: vertical default layout */
207 if (client->force_reconfigure |
208 (client->rect.width != (client->rect.width = container->width)) |
209 (client->rect.height != (client->rect.height = container->height / num_clients))) {
210 resize_client(connection, client);
213 if (client->force_reconfigure)
214 client->force_reconfigure = false;
219 i3Font *font = load_font(connection, pattern);
220 int decoration_height = (font->height + 2 + 2);
221 struct Stack_Window *stack_win = &(container->stack_win);
223 /* Check if we need to reconfigure our stack title window */
224 if ((stack_win->width != (stack_win->width = container->width)) |
225 (stack_win->height != (stack_win->height = decoration_height * num_clients)))
226 xcb_configure_window(connection, stack_win->window,
227 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, &(stack_win->width));
229 /* All clients are repositioned */
230 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
231 /* Check if we changed client->x or client->y by updating it.
232 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
233 if (client->force_reconfigure |
234 (client->rect.x != (client->rect.x = container->x)) |
235 (client->rect.y != (client->rect.y = container->y + (decoration_height * num_clients))))
236 reposition_client(connection, client);
238 if (client->force_reconfigure |
239 (client->rect.width != (client->rect.width = container->width)) |
240 (client->rect.height !=
241 (client->rect.height = container->height - (decoration_height * num_clients))))
242 resize_client(connection, client);
244 client->force_reconfigure = false;
246 decorate_window(connection, client, stack_win->window, stack_win->gc,
247 current_client * decoration_height);
251 /* Raise the focused window */
252 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
253 xcb_configure_window(connection, container->currently_focused->frame,
254 XCB_CONFIG_WINDOW_STACK_MODE, values);
258 static void render_bars(xcb_connection_t *connection, Workspace *r_ws, int width, int height) {
260 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
261 if (client->force_reconfigure |
262 (client->rect.x != (client->rect.x = 0)) |
263 (client->rect.y != (client->rect.y = height)))
264 reposition_client(connection, client);
266 if (client->force_reconfigure |
267 (client->rect.width != (client->rect.width = width)) |
268 (client->rect.height != (client->rect.height = client->desired_height)))
269 resize_client(connection, client);
271 client->force_reconfigure = false;
272 height += client->desired_height;
276 void render_layout(xcb_connection_t *connection) {
279 TAILQ_FOREACH(screen, &virtual_screens, screens) {
280 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
281 Workspace *r_ws = &(workspaces[screen->current_workspace]);
283 printf("Rendering screen %d\n", screen->num);
284 if (r_ws->fullscreen_client != NULL)
285 /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
288 int width = r_ws->rect.width;
289 int height = r_ws->rect.height;
291 /* Reserve space for dock clients */
293 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
294 height -= client->desired_height;
296 printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
298 int xoffset[r_ws->rows];
299 int yoffset[r_ws->cols];
300 /* Initialize offsets */
301 for (int cols = 0; cols < r_ws->cols; cols++)
302 yoffset[cols] = r_ws->rect.y;
303 for (int rows = 0; rows < r_ws->rows; rows++)
304 xoffset[rows] = r_ws->rect.x;
306 /* Go through the whole table and render what’s necessary */
307 for (int cols = 0; cols < r_ws->cols; cols++)
308 for (int rows = 0; rows < r_ws->rows; rows++) {
309 Container *container = r_ws->table[cols][rows];
310 printf("\n========\ncontainer has %d colspan, %d rowspan\n",
311 container->colspan, container->rowspan);
312 printf("container at %d, %d\n", xoffset[rows], yoffset[cols]);
313 /* Update position of the container */
314 container->row = rows;
315 container->col = cols;
316 container->x = xoffset[rows];
317 container->y = yoffset[cols];
319 if (container->width_factor == 0)
320 container->width = (width / r_ws->cols);
321 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
322 container->width *= container->colspan;
324 if (container->height_factor == 0)
325 container->height = (height / r_ws->rows);
326 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
327 container->height *= container->rowspan;
329 /* Render the container if it is not empty */
330 render_container(connection, container);
332 xoffset[rows] += container->width;
333 yoffset[cols] += container->height;
334 printf("==========\n");
337 render_bars(connection, r_ws, width, height);
340 xcb_flush(connection);