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 /* This macro copies the old value of the given variable, changes the variable to contain
28 th new one and returns true if it changed */
29 #define HAS_CHANGED(value, new) (old_value = value, old_value != (value = new))
34 * Gets the unoccupied space (= space which is available for windows which were resized by the user)
35 * for the given row. This is necessary to render both, customly resized windows and never touched
36 * windows correctly, meaning that the aspect ratio will be maintained when opening new windows.
39 int get_unoccupied_x(Workspace *workspace, int row) {
40 int unoccupied = workspace->rect.width;
41 float default_factor = ((float)workspace->rect.width / workspace->cols) / workspace->rect.width;
43 printf("get_unoccupied_x(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
45 for (int cols = 0; cols < workspace->cols;) {
46 Container *con = workspace->table[cols][row];
47 printf("width_factor[%d][%d] = %f, colspan = %d\n", cols, row, con->width_factor, con->colspan);
48 if (con->width_factor == 0)
49 unoccupied -= workspace->rect.width * default_factor * con->colspan;
53 assert(unoccupied != 0);
55 printf("unoccupied space: %d\n", unoccupied);
59 /* See get_unoccupied_x() */
60 int get_unoccupied_y(Workspace *workspace, int col) {
61 int unoccupied = workspace->rect.height;
62 float default_factor = ((float)workspace->rect.height / workspace->rows) / workspace->rect.height;
64 printf("get_unoccupied_y(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
66 for (int rows = 0; rows < workspace->rows;) {
67 Container *con = workspace->table[col][rows];
68 printf("height_factor[%d][%d] = %f, rowspan %d\n", col, rows, con->height_factor, con->rowspan);
69 if (con->height_factor == 0)
70 unoccupied -= workspace->rect.height * default_factor * con->rowspan;
74 assert(unoccupied != 0);
76 printf("unoccupied space: %d\n", unoccupied);
81 * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
82 * When in stacking mode, the window decorations are drawn onto an own window.
85 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
86 i3Font *font = load_font(conn, config.font);
87 uint32_t background_color,
91 /* Clients without a container (docks) won’t get decorated */
92 if (client->container == NULL)
95 if (client->container->currently_focused == client) {
96 background_color = get_colorpixel(conn, client, client->frame, "#285577");
97 text_color = get_colorpixel(conn, client, client->frame, "#ffffff");
98 border_color = get_colorpixel(conn, client, client->frame, "#4c7899");
100 background_color = get_colorpixel(conn, client, client->frame, "#222222");
101 text_color = get_colorpixel(conn, client, client->frame, "#888888");
102 border_color = get_colorpixel(conn, client, client->frame, "#333333");
105 /* Our plan is the following:
106 - Draw a rect around the whole client in background_color
107 - Draw two lines in a lighter color
108 - Draw the window’s title
111 /* Draw a rectangle in background color around the window */
112 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
114 xcb_rectangle_t rect = {0, offset, client->rect.width, offset + client->rect.height};
115 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
118 xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
119 xcb_draw_line(conn, drawable, gc, border_color, 2, offset + font->height + 3,
120 2 + client->rect.width, offset + font->height + 3);
123 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
124 uint32_t values[] = { text_color, background_color, font->id };
125 xcb_change_gc(conn, gc, mask, values);
129 asprintf(&label, "(%08x) %.*s", client->frame, client->name_len, client->name);
130 xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), drawable,
131 gc, 3 /* X */, offset + font->height /* Y = baseline of font */, label);
132 check_error(conn, text_cookie, "Could not draw client's title");
137 * Pushes the client’s x and y coordinates to X11
140 static void reposition_client(xcb_connection_t *connection, Client *client) {
141 printf("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
142 /* Note: We can use a pointer to client->x like an array of uint32_ts
143 because it is followed by client->y by definition */
144 xcb_configure_window(connection, client->frame,
145 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
149 * Pushes the client’s width/height to X11 and resizes the child window
152 static void resize_client(xcb_connection_t *connection, Client *client) {
153 i3Font *font = load_font(connection, config.font);
155 printf("resizing client to %d x %d\n", client->rect.width, client->rect.height);
156 xcb_configure_window(connection, client->frame,
157 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
158 &(client->rect.width));
160 /* Adjust the position of the child inside its frame.
161 * The coordinates of the child are relative to its frame, we
162 * add a border of 2 pixel to each value */
163 uint32_t mask = XCB_CONFIG_WINDOW_X |
164 XCB_CONFIG_WINDOW_Y |
165 XCB_CONFIG_WINDOW_WIDTH |
166 XCB_CONFIG_WINDOW_HEIGHT;
168 switch (client->container->mode) {
172 rect.width = client->rect.width - (2 + 2);
173 rect.height = client->rect.height - 2;
176 if (client->titlebar_position == TITLEBAR_OFF) {
179 rect.width = client->rect.width;
180 rect.height = client->rect.height;
183 rect.y = font->height + 2 + 2;
184 rect.width = client->rect.width - (2 + 2);
185 rect.height = client->rect.height - ((font->height + 2 + 2) + 2);
190 printf("child will be at %dx%d with size %dx%d\n", rect.x, rect.y, rect.width, rect.height);
192 xcb_configure_window(connection, client->child, mask, &(rect.x));
196 * Renders the given container. Is called by render_layout() or individually (for example
197 * when focus changes in a stacking container)
200 void render_container(xcb_connection_t *connection, Container *container) {
202 int num_clients = 0, current_client = 0;
204 if (container->currently_focused == NULL)
207 CIRCLEQ_FOREACH(client, &(container->clients), clients)
210 if (container->mode == MODE_DEFAULT) {
211 printf("got %d clients in this default container.\n", num_clients);
212 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
213 /* Check if we changed client->x or client->y by updating it.
214 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
215 if (client->force_reconfigure |
216 HAS_CHANGED(client->rect.x, container->x) |
217 HAS_CHANGED(client->rect.y, container->y +
218 (container->height / num_clients) * current_client))
219 reposition_client(connection, client);
221 /* TODO: vertical default layout */
222 if (client->force_reconfigure |
223 HAS_CHANGED(client->rect.width, container->width) |
224 HAS_CHANGED(client->rect.height, container->height / num_clients)) {
225 resize_client(connection, client);
226 } else printf("no reconfigure necessary\n");
227 printf("after client->rect.width = %d, client->rect.height = %d\n", client->rect.width,
228 client->rect.height);
231 if (client->force_reconfigure)
232 client->force_reconfigure = false;
237 i3Font *font = load_font(connection, config.font);
238 int decoration_height = (font->height + 2 + 2);
239 struct Stack_Window *stack_win = &(container->stack_win);
241 /* Check if we need to remap our stack title window, it gets unmapped when the container
242 is empty in src/handlers.c:unmap_notify() */
243 if (stack_win->height == 0)
244 xcb_map_window(connection, stack_win->window);
246 /* Check if we need to reconfigure our stack title window */
247 if (HAS_CHANGED(stack_win->width, container->width) |
248 HAS_CHANGED(stack_win->height, decoration_height * num_clients)) {
249 xcb_configure_window(connection, stack_win->window,
250 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, &(stack_win->width));
252 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
253 xcb_configure_window(connection, stack_win->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
256 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
257 client = container->currently_focused;
259 /* Check if we changed client->x or client->y by updating it.
260 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
261 if (client->force_reconfigure |
262 HAS_CHANGED(client->rect.x, container->x) |
263 HAS_CHANGED(client->rect.y, container->y + (decoration_height * num_clients)))
264 reposition_client(connection, client);
266 if (client->force_reconfigure |
267 HAS_CHANGED(client->rect.width, container->width) |
268 HAS_CHANGED(client->rect.height, container->height - (decoration_height * num_clients)))
269 resize_client(connection, client);
271 client->force_reconfigure = false;
273 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
274 xcb_configure_window(connection, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
276 /* Render the decorations of all clients */
277 CIRCLEQ_FOREACH(client, &(container->clients), clients)
278 decorate_window(connection, client, stack_win->window, stack_win->gc,
279 current_client++ * decoration_height);
283 static void render_bars(xcb_connection_t *connection, Workspace *r_ws, int width, int height) {
285 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
286 if (client->force_reconfigure |
287 HAS_CHANGED(client->rect.x, 0) |
288 HAS_CHANGED(client->rect.y, height))
289 reposition_client(connection, client);
291 if (client->force_reconfigure |
292 HAS_CHANGED(client->rect.width, width) |
293 HAS_CHANGED(client->rect.height, client->desired_height))
294 resize_client(connection, client);
296 client->force_reconfigure = false;
297 height += client->desired_height;
301 void render_layout(xcb_connection_t *connection) {
304 TAILQ_FOREACH(screen, &virtual_screens, screens) {
305 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
306 Workspace *r_ws = &(workspaces[screen->current_workspace]);
308 printf("Rendering screen %d\n", screen->num);
309 if (r_ws->fullscreen_client != NULL)
310 /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
313 int width = r_ws->rect.width;
314 int height = r_ws->rect.height;
316 /* Reserve space for dock clients */
318 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
319 height -= client->desired_height;
321 printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
323 int xoffset[r_ws->rows];
324 int yoffset[r_ws->cols];
325 /* Initialize offsets */
326 for (int cols = 0; cols < r_ws->cols; cols++)
327 yoffset[cols] = r_ws->rect.y;
328 for (int rows = 0; rows < r_ws->rows; rows++)
329 xoffset[rows] = r_ws->rect.x;
331 /* Go through the whole table and render what’s necessary */
332 for (int cols = 0; cols < r_ws->cols; cols++)
333 for (int rows = 0; rows < r_ws->rows; rows++) {
334 Container *container = r_ws->table[cols][rows];
335 printf("\n========\ncontainer has %d colspan, %d rowspan\n",
336 container->colspan, container->rowspan);
337 printf("container at %d, %d\n", xoffset[rows], yoffset[cols]);
338 /* Update position of the container */
339 container->row = rows;
340 container->col = cols;
341 container->x = xoffset[rows];
342 container->y = yoffset[cols];
344 if (container->width_factor == 0)
345 container->width = (width / r_ws->cols);
346 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
347 container->width *= container->colspan;
349 if (container->height_factor == 0)
350 container->height = (height / r_ws->rows);
351 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
352 container->height *= container->rowspan;
354 /* Render the container if it is not empty */
355 render_container(connection, container);
357 xoffset[rows] += container->width;
358 yoffset[cols] += container->height;
359 printf("==========\n");
362 render_bars(connection, r_ws, width, height);
365 xcb_flush(connection);