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
28 * Gets the unoccupied space (= space which is available for windows which were resized by the user)
29 * for the given row. This is necessary to render both, customly resized windows and never touched
30 * windows correctly, meaning that the aspect ratio will be maintained when opening new windows.
33 int get_unoccupied_x(Workspace *workspace, int row) {
34 int unoccupied = workspace->rect.width;
35 float default_factor = ((float)workspace->rect.width / workspace->cols) / workspace->rect.width;
37 printf("get_unoccupied_x(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
39 for (int cols = 0; cols < workspace->cols;) {
40 Container *con = workspace->table[cols][row];
41 printf("width_factor[%d][%d] = %f, colspan = %d\n", cols, row, con->width_factor, con->colspan);
42 if (con->width_factor == 0)
43 unoccupied -= workspace->rect.width * default_factor * con->colspan;
47 assert(unoccupied != 0);
49 printf("unoccupied space: %d\n", unoccupied);
53 /* See get_unoccupied_x() */
54 int get_unoccupied_y(Workspace *workspace, int col) {
55 int unoccupied = workspace->rect.height;
56 float default_factor = ((float)workspace->rect.height / workspace->rows) / workspace->rect.height;
58 printf("get_unoccupied_y(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
60 for (int rows = 0; rows < workspace->rows;) {
61 Container *con = workspace->table[col][rows];
62 printf("height_factor[%d][%d] = %f, rowspan %d\n", col, rows, con->height_factor, con->rowspan);
63 if (con->height_factor == 0)
64 unoccupied -= workspace->rect.height * default_factor * con->rowspan;
68 assert(unoccupied != 0);
70 printf("unoccupied space: %d\n", unoccupied);
75 * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
76 * When in stacking mode, the window decorations are drawn onto an own window.
79 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
80 i3Font *font = load_font(conn, config.font);
81 uint32_t background_color,
85 /* Clients without a container (docks) won’t get decorated */
86 if (client->container == NULL)
89 if (client->container->currently_focused == client) {
90 background_color = get_colorpixel(conn, client, client->frame, "#285577");
91 text_color = get_colorpixel(conn, client, client->frame, "#ffffff");
92 border_color = get_colorpixel(conn, client, client->frame, "#4c7899");
94 background_color = get_colorpixel(conn, client, client->frame, "#222222");
95 text_color = get_colorpixel(conn, client, client->frame, "#888888");
96 border_color = get_colorpixel(conn, client, client->frame, "#333333");
99 /* Our plan is the following:
100 - Draw a rect around the whole client in background_color
101 - Draw two lines in a lighter color
102 - Draw the window’s title
105 /* Draw a green rectangle around the window */
106 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
108 xcb_rectangle_t rect = {0, offset, client->rect.width, offset + client->rect.height};
109 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
112 xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
113 xcb_draw_line(conn, drawable, gc, border_color, 2, offset + font->height + 3,
114 2 + client->rect.width, offset + font->height + 3);
117 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
118 uint32_t values[] = { text_color, background_color, font->id };
119 xcb_change_gc(conn, gc, mask, values);
123 asprintf(&label, "(%08x) %.*s", client->frame, client->name_len, client->name);
124 xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), drawable,
125 gc, 3 /* X */, offset + font->height /* Y = baseline of font */, label);
126 check_error(conn, text_cookie, "Could not draw client's title");
131 * Pushes the client’s x and y coordinates to X11
134 static void reposition_client(xcb_connection_t *connection, Client *client) {
135 printf("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
136 /* Note: We can use a pointer to client->x like an array of uint32_ts
137 because it is followed by client->y by definition */
138 xcb_configure_window(connection, client->frame,
139 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
143 * Pushes the client’s width/height to X11 and resizes the child window
146 static void resize_client(xcb_connection_t *connection, Client *client) {
147 i3Font *font = load_font(connection, config.font);
149 printf("resizing client to %d x %d\n", client->rect.width, client->rect.height);
150 xcb_configure_window(connection, client->frame,
151 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
152 &(client->rect.width));
154 /* Adjust the position of the child inside its frame.
155 * The coordinates of the child are relative to its frame, we
156 * add a border of 2 pixel to each value */
157 uint32_t mask = XCB_CONFIG_WINDOW_X |
158 XCB_CONFIG_WINDOW_Y |
159 XCB_CONFIG_WINDOW_WIDTH |
160 XCB_CONFIG_WINDOW_HEIGHT;
162 if (client->titlebar_position == TITLEBAR_OFF ||
163 client->container->mode == MODE_STACK) {
166 rect.width = client->rect.width;
167 rect.height = client->rect.height;
170 rect.y = font->height + 2 + 2;
171 rect.width = client->rect.width - (2 + 2);
172 rect.height = client->rect.height - ((font->height + 2 + 2) + 2);
175 printf("child will be at %dx%d with size %dx%d\n", rect.x, rect.y, rect.width, rect.height);
177 xcb_configure_window(connection, client->child, mask, &(rect.x));
181 * Renders the given container. Is called by render_layout() or individually (for example
182 * when focus changes in a stacking container)
185 void render_container(xcb_connection_t *connection, Container *container) {
187 int num_clients = 0, current_client = 0;
189 if (container->currently_focused == NULL)
192 CIRCLEQ_FOREACH(client, &(container->clients), clients)
195 if (container->mode == MODE_DEFAULT) {
196 printf("got %d clients in this default container.\n", num_clients);
197 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
198 /* Check if we changed client->x or client->y by updating it.
199 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
200 if (client->force_reconfigure |
201 (client->rect.x != (client->rect.x = container->x)) |
202 (client->rect.y != (client->rect.y = container->y +
203 (container->height / num_clients) * current_client))) {
204 reposition_client(connection, client);
207 /* TODO: vertical default layout */
208 if (client->force_reconfigure |
209 (client->rect.width != (client->rect.width = container->width)) |
210 (client->rect.height != (client->rect.height = container->height / num_clients))) {
211 resize_client(connection, client);
214 if (client->force_reconfigure)
215 client->force_reconfigure = false;
220 i3Font *font = load_font(connection, config.font);
221 int decoration_height = (font->height + 2 + 2);
222 struct Stack_Window *stack_win = &(container->stack_win);
224 /* Check if we need to remap our stack title window, it gets unmapped when the container
225 is empty in src/handlers.c:unmap_notify() */
226 if (stack_win->height == 0)
227 xcb_map_window(connection, stack_win->window);
229 /* Check if we need to reconfigure our stack title window */
230 if ((stack_win->width != (stack_win->width = container->width)) |
231 (stack_win->height != (stack_win->height = decoration_height * num_clients)))
232 xcb_configure_window(connection, stack_win->window,
233 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, &(stack_win->width));
235 /* All clients are repositioned */
236 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
237 /* Check if we changed client->x or client->y by updating it.
238 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
239 if (client->force_reconfigure |
240 (client->rect.x != (client->rect.x = container->x)) |
241 (client->rect.y != (client->rect.y = container->y + (decoration_height * num_clients))))
242 reposition_client(connection, client);
244 if (client->force_reconfigure |
245 (client->rect.width != (client->rect.width = container->width)) |
246 (client->rect.height !=
247 (client->rect.height = container->height - (decoration_height * num_clients))))
248 resize_client(connection, client);
250 client->force_reconfigure = false;
252 decorate_window(connection, client, stack_win->window, stack_win->gc,
253 current_client * decoration_height);
257 /* Raise the focused window */
258 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
259 xcb_configure_window(connection, container->currently_focused->frame,
260 XCB_CONFIG_WINDOW_STACK_MODE, values);
264 static void render_bars(xcb_connection_t *connection, Workspace *r_ws, int width, int height) {
266 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
267 if (client->force_reconfigure |
268 (client->rect.x != (client->rect.x = 0)) |
269 (client->rect.y != (client->rect.y = height)))
270 reposition_client(connection, client);
272 if (client->force_reconfigure |
273 (client->rect.width != (client->rect.width = width)) |
274 (client->rect.height != (client->rect.height = client->desired_height)))
275 resize_client(connection, client);
277 client->force_reconfigure = false;
278 height += client->desired_height;
282 void render_layout(xcb_connection_t *connection) {
285 TAILQ_FOREACH(screen, &virtual_screens, screens) {
286 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
287 Workspace *r_ws = &(workspaces[screen->current_workspace]);
289 printf("Rendering screen %d\n", screen->num);
290 if (r_ws->fullscreen_client != NULL)
291 /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
294 int width = r_ws->rect.width;
295 int height = r_ws->rect.height;
297 /* Reserve space for dock clients */
299 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
300 height -= client->desired_height;
302 printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
304 int xoffset[r_ws->rows];
305 int yoffset[r_ws->cols];
306 /* Initialize offsets */
307 for (int cols = 0; cols < r_ws->cols; cols++)
308 yoffset[cols] = r_ws->rect.y;
309 for (int rows = 0; rows < r_ws->rows; rows++)
310 xoffset[rows] = r_ws->rect.x;
312 /* Go through the whole table and render what’s necessary */
313 for (int cols = 0; cols < r_ws->cols; cols++)
314 for (int rows = 0; rows < r_ws->rows; rows++) {
315 Container *container = r_ws->table[cols][rows];
316 printf("\n========\ncontainer has %d colspan, %d rowspan\n",
317 container->colspan, container->rowspan);
318 printf("container at %d, %d\n", xoffset[rows], yoffset[cols]);
319 /* Update position of the container */
320 container->row = rows;
321 container->col = cols;
322 container->x = xoffset[rows];
323 container->y = yoffset[cols];
325 if (container->width_factor == 0)
326 container->width = (width / r_ws->cols);
327 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
328 container->width *= container->colspan;
330 if (container->height_factor == 0)
331 container->height = (height / r_ws->rows);
332 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
333 container->height *= container->rowspan;
335 /* Render the container if it is not empty */
336 render_container(connection, container);
338 xoffset[rows] += container->width;
339 yoffset[cols] += container->height;
340 printf("==========\n");
343 render_bars(connection, r_ws, width, height);
346 xcb_flush(connection);