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 * Updates *destination with new_value and returns true if it was changed or false
32 static bool update_if_necessary(uint32_t *destination, const uint32_t new_value) {
33 int old_value = *destination;
35 return ((*destination = new_value) != old_value);
39 * Gets the unoccupied space (= space which is available for windows which were resized by the user)
40 * for the given row. This is necessary to render both, customly resized windows and never touched
41 * windows correctly, meaning that the aspect ratio will be maintained when opening new windows.
44 int get_unoccupied_x(Workspace *workspace, int row) {
45 int unoccupied = workspace->rect.width;
46 float default_factor = ((float)workspace->rect.width / workspace->cols) / workspace->rect.width;
48 LOG("get_unoccupied_x(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
50 for (int cols = 0; cols < workspace->cols;) {
51 Container *con = workspace->table[cols][row];
52 LOG("width_factor[%d][%d] = %f, colspan = %d\n", cols, row, con->width_factor, con->colspan);
53 if (con->width_factor == 0)
54 unoccupied -= workspace->rect.width * default_factor * con->colspan;
58 assert(unoccupied != 0);
60 LOG("unoccupied space: %d\n", unoccupied);
64 /* See get_unoccupied_x() */
65 int get_unoccupied_y(Workspace *workspace, int col) {
66 int unoccupied = workspace->rect.height;
67 float default_factor = ((float)workspace->rect.height / workspace->rows) / workspace->rect.height;
69 LOG("get_unoccupied_y(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
71 for (int rows = 0; rows < workspace->rows;) {
72 Container *con = workspace->table[col][rows];
73 LOG("height_factor[%d][%d] = %f, rowspan %d\n", col, rows, con->height_factor, con->rowspan);
74 if (con->height_factor == 0)
75 unoccupied -= workspace->rect.height * default_factor * con->rowspan;
79 assert(unoccupied != 0);
81 LOG("unoccupied space: %d\n", unoccupied);
86 * Redecorates the given client correctly by checking if it’s in a stacking container and
87 * re-rendering the stack window or just calling decorate_window if it’s not in a stacking
91 void redecorate_window(xcb_connection_t *conn, Client *client) {
92 if (client->container->mode == MODE_STACK) {
93 render_container(conn, client->container);
94 /* We clear the frame to generate exposure events, because the color used
95 in drawing may be different */
96 xcb_clear_area(conn, true, client->frame, 0, 0, client->rect.width, client->rect.height);
97 } else decorate_window(conn, client, client->frame, client->titlegc, 0);
102 * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
103 * When in stacking mode, the window decorations are drawn onto an own window.
106 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
107 i3Font *font = load_font(conn, config.font);
108 int decoration_height = font->height + 2 + 2;
109 uint32_t background_color,
113 /* Clients without a container (docks) won’t get decorated */
114 if (client->container == NULL)
117 if (client->container->currently_focused == client) {
118 /* Distinguish if the window is currently focused… */
119 if (CUR_CELL->currently_focused == client)
120 background_color = get_colorpixel(conn, "#285577");
121 /* …or if it is the focused window in a not focused container */
122 else background_color = get_colorpixel(conn, "#555555");
124 text_color = get_colorpixel(conn, "#ffffff");
125 border_color = get_colorpixel(conn, "#4c7899");
127 background_color = get_colorpixel(conn, "#222222");
128 text_color = get_colorpixel(conn, "#888888");
129 border_color = get_colorpixel(conn, "#333333");
132 /* Our plan is the following:
133 - Draw a rect around the whole client in background_color
134 - Draw two lines in a lighter color
135 - Draw the window’s title
138 /* Draw a rectangle in background color around the window */
139 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
141 /* In stacking mode, we only render the rect for this specific decoration */
142 if (client->container->mode == MODE_STACK) {
143 xcb_rectangle_t rect = {0, offset, client->container->width, offset + decoration_height };
144 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
146 /* We need to use the container’s width because it is the more recent value - when
147 in stacking mode, clients get reconfigured only on demand (the not active client
148 is not reconfigured), so the client’s rect.width would be wrong */
149 xcb_rectangle_t rect = {0, 0, client->container->width, client->rect.height};
150 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
152 /* Draw the inner background to have a black frame around clients (such as mplayer)
153 which cannot be resized exactly in our frames and therefore are centered */
154 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
155 xcb_rectangle_t crect = {2, decoration_height,
156 client->rect.width - (2 + 2), client->rect.height - 2 - decoration_height};
157 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
161 xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
162 xcb_draw_line(conn, drawable, gc, border_color, 2, offset + font->height + 3,
163 2 + client->rect.width, offset + font->height + 3);
165 /* If the client has a title, we draw it */
166 if (client->name != NULL) {
168 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
169 uint32_t values[] = { text_color, background_color, font->id };
170 xcb_change_gc(conn, gc, mask, values);
172 xcb_image_text_16(conn, client->name_len, drawable, gc, 3 /* X */,
173 offset + font->height /* Y = baseline of font */, (xcb_char2b_t*)client->name);
178 * Pushes the client’s x and y coordinates to X11
181 static void reposition_client(xcb_connection_t *conn, Client *client) {
182 LOG("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
183 /* Note: We can use a pointer to client->x like an array of uint32_ts
184 because it is followed by client->y by definition */
185 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
189 * Pushes the client’s width/height to X11 and resizes the child window
192 static void resize_client(xcb_connection_t *conn, Client *client) {
193 i3Font *font = load_font(conn, config.font);
195 LOG("resizing client to %d x %d\n", client->rect.width, client->rect.height);
196 xcb_configure_window(conn, client->frame,
197 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
198 &(client->rect.width));
200 /* Adjust the position of the child inside its frame.
201 * The coordinates of the child are relative to its frame, we
202 * add a border of 2 pixel to each value */
203 uint32_t mask = XCB_CONFIG_WINDOW_X |
204 XCB_CONFIG_WINDOW_Y |
205 XCB_CONFIG_WINDOW_WIDTH |
206 XCB_CONFIG_WINDOW_HEIGHT;
207 Rect *rect = &(client->child_rect);
208 switch (client->container->mode) {
212 rect->width = client->rect.width - (2 + 2);
213 rect->height = client->rect.height - 2;
216 if (client->titlebar_position == TITLEBAR_OFF) {
219 rect->width = client->rect.width;
220 rect->height = client->rect.height;
223 rect->y = font->height + 2 + 2;
224 rect->width = client->rect.width - (2 + 2);
225 rect->height = client->rect.height - ((font->height + 2 + 2) + 2);
230 /* Obey the ratio, if any */
231 if (client->proportional_height != 0 &&
232 client->proportional_width != 0) {
233 LOG("proportional height = %d, width = %d\n", client->proportional_height, client->proportional_width);
234 double new_height = rect->height + 1;
235 int new_width = rect->width;
237 while (new_height > rect->height) {
238 new_height = ((double)client->proportional_height / client->proportional_width) * new_width;
240 if (new_height > rect->height)
243 /* Center the window */
244 rect->y += (rect->height / 2) - (new_height / 2);
245 rect->x += (rect->width / 2) - (new_width / 2);
247 rect->height = new_height;
248 rect->width = new_width;
249 LOG("new_height = %f, new_width = %d\n", new_height, new_width);
252 LOG("child will be at %dx%d with size %dx%d\n", rect->x, rect->y, rect->width, rect->height);
254 xcb_configure_window(conn, client->child, mask, &(rect->x));
256 /* After configuring a child window we need to fake a configure_notify_event according
257 to ICCCM 4.2.3. This seems rather broken, especially since X sends exactly the same
258 configure_notify_event automatically according to xtrace. Anyone knows details? */
259 xcb_configure_notify_event_t event;
261 event.event = client->child;
262 event.window = client->child;
263 event.response_type = XCB_CONFIGURE_NOTIFY;
267 event.width = rect->width;
268 event.height = rect->height;
270 event.border_width = 0;
271 event.above_sibling = XCB_NONE;
272 event.override_redirect = false;
274 xcb_send_event(conn, false, client->child, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)&event);
278 * Renders the given container. Is called by render_layout() or individually (for example
279 * when focus changes in a stacking container)
282 void render_container(xcb_connection_t *conn, Container *container) {
284 int num_clients = 0, current_client = 0;
286 if (container->currently_focused == NULL)
289 CIRCLEQ_FOREACH(client, &(container->clients), clients)
292 if (container->mode == MODE_DEFAULT) {
293 LOG("got %d clients in this default container.\n", num_clients);
294 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
295 /* If the client is in fullscreen mode, it does not get reconfigured */
296 if (container->workspace->fullscreen_client == client) {
301 /* Check if we changed client->x or client->y by updating it.
302 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
303 if (client->force_reconfigure |
304 update_if_necessary(&(client->rect.x), container->x) |
305 update_if_necessary(&(client->rect.y), container->y +
306 (container->height / num_clients) * current_client))
307 reposition_client(conn, client);
309 /* TODO: vertical default layout */
310 if (client->force_reconfigure |
311 update_if_necessary(&(client->rect.width), container->width) |
312 update_if_necessary(&(client->rect.height), container->height / num_clients))
313 resize_client(conn, client);
315 client->force_reconfigure = false;
320 i3Font *font = load_font(conn, config.font);
321 int decoration_height = (font->height + 2 + 2);
322 struct Stack_Window *stack_win = &(container->stack_win);
324 /* Check if we need to remap our stack title window, it gets unmapped when the container
325 is empty in src/handlers.c:unmap_notify() */
326 if (stack_win->rect.height == 0)
327 xcb_map_window(conn, stack_win->window);
329 /* Check if we need to reconfigure our stack title window */
330 if (update_if_necessary(&(stack_win->rect.x), container->x) |
331 update_if_necessary(&(stack_win->rect.y), container->y) |
332 update_if_necessary(&(stack_win->rect.width), container->width) |
333 update_if_necessary(&(stack_win->rect.height), decoration_height * num_clients)) {
335 /* Configuration can happen in two slightly different ways:
337 If there is no client in fullscreen mode, 5 parameters are passed
338 (x, y, width, height, stack mode is set to above which means top-most position).
340 If there is a fullscreen client, the fourth parameter is set to to the
341 fullscreen window as sibling and the stack mode is set to below, which means
342 that the stack_window will be placed just below the sibling, that is, under
343 the fullscreen window.
345 uint32_t values[] = { stack_win->rect.x, stack_win->rect.y,
346 stack_win->rect.width, stack_win->rect.height,
347 XCB_STACK_MODE_ABOVE, XCB_STACK_MODE_BELOW };
348 uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
349 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
350 XCB_CONFIG_WINDOW_STACK_MODE;
352 /* If there is no fullscreen client, we raise the stack window */
353 if (container->workspace->fullscreen_client != NULL) {
354 mask |= XCB_CONFIG_WINDOW_SIBLING;
355 values[4] = container->workspace->fullscreen_client->frame;
358 xcb_configure_window(conn, stack_win->window, mask, values);
361 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
362 client = container->currently_focused;
364 if (container->workspace->fullscreen_client == NULL) {
365 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
366 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
369 /* Render the decorations of all clients */
370 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
371 /* If the client is in fullscreen mode, it does not get reconfigured */
372 if (container->workspace->fullscreen_client == client) {
377 /* Check if we changed client->x or client->y by updating it.
378 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
379 if (client->force_reconfigure |
380 update_if_necessary(&(client->rect.x), container->x) |
381 update_if_necessary(&(client->rect.y), container->y + (decoration_height * num_clients)))
382 reposition_client(conn, client);
384 if (client->force_reconfigure |
385 update_if_necessary(&(client->rect.width), container->width) |
386 update_if_necessary(&(client->rect.height), container->height - (decoration_height * num_clients)))
387 resize_client(conn, client);
389 client->force_reconfigure = false;
391 decorate_window(conn, client, stack_win->window, stack_win->gc,
392 current_client++ * decoration_height);
397 static void render_bars(xcb_connection_t *conn, Workspace *r_ws, int width, int *height) {
399 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
400 if (client->force_reconfigure |
401 update_if_necessary(&(client->rect.x), 0) |
402 update_if_necessary(&(client->rect.y), *height))
403 reposition_client(conn, client);
405 if (client->force_reconfigure |
406 update_if_necessary(&(client->rect.width), width) |
407 update_if_necessary(&(client->rect.height), client->desired_height))
408 resize_client(conn, client);
410 client->force_reconfigure = false;
411 *height += client->desired_height;
415 static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int width, int height) {
416 LOG("Rendering internal bar\n");
417 i3Font *font = load_font(conn, config.font);
418 i3Screen *screen = r_ws->screen;
419 enum { SET_NORMAL = 0, SET_FOCUSED = 1 };
420 uint32_t background_color[2],
426 black = get_colorpixel(conn, "#000000");
428 background_color[SET_NORMAL] = get_colorpixel(conn, "#222222");
429 text_color[SET_NORMAL] = get_colorpixel(conn, "#888888");
430 border_color[SET_NORMAL] = get_colorpixel(conn, "#333333");
432 background_color[SET_FOCUSED] = get_colorpixel(conn, "#285577");
433 text_color[SET_FOCUSED] = get_colorpixel(conn, "#ffffff");
434 border_color[SET_FOCUSED] = get_colorpixel(conn, "#4c7899");
436 /* Fill the whole bar in black */
437 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black);
438 xcb_rectangle_t rect = {0, 0, width, height};
439 xcb_poly_fill_rectangle(conn, screen->bar, screen->bargc, 1, &rect);
442 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FONT, font->id);
445 for (int c = 0; c < 10; c++) {
446 if (workspaces[c].screen == screen) {
447 int set = (screen->current_workspace == c ? SET_FOCUSED : SET_NORMAL);
449 xcb_draw_rect(conn, screen->bar, screen->bargc, border_color[set],
450 drawn * height, 1, height - 2, height - 2);
451 xcb_draw_rect(conn, screen->bar, screen->bargc, background_color[set],
452 drawn * height + 1, 2, height - 4, height - 4);
454 snprintf(label, sizeof(label), "%d", c+1);
455 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, text_color[set]);
456 xcb_change_gc_single(conn, screen->bargc, XCB_GC_BACKGROUND, background_color[set]);
457 xcb_image_text_8(conn, strlen(label), screen->bar, screen->bargc, drawn * height + 5 /* X */,
458 font->height + 1 /* Y = baseline of font */, label);
463 LOG("done rendering internal\n");
466 void render_layout(xcb_connection_t *conn) {
468 i3Font *font = load_font(conn, config.font);
470 TAILQ_FOREACH(screen, virtual_screens, screens) {
471 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
472 Workspace *r_ws = &(workspaces[screen->current_workspace]);
474 LOG("Rendering screen %d\n", screen->num);
476 int width = r_ws->rect.width;
477 int height = r_ws->rect.height;
479 /* Reserve space for dock clients */
481 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
482 height -= client->desired_height;
484 /* Space for the internal bar */
485 height -= (font->height + 6);
487 LOG("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
489 int xoffset[r_ws->rows];
490 int yoffset[r_ws->cols];
491 /* Initialize offsets */
492 for (int cols = 0; cols < r_ws->cols; cols++)
493 yoffset[cols] = r_ws->rect.y;
494 for (int rows = 0; rows < r_ws->rows; rows++)
495 xoffset[rows] = r_ws->rect.x;
497 dump_table(conn, r_ws);
498 /* Go through the whole table and render what’s necessary */
499 for (int cols = 0; cols < r_ws->cols; cols++)
500 for (int rows = 0; rows < r_ws->rows; rows++) {
501 Container *container = r_ws->table[cols][rows];
502 int single_width, single_height;
505 LOG("container has %d colspan, %d rowspan\n",
506 container->colspan, container->rowspan);
507 LOG("container at %d, %d\n", xoffset[rows], yoffset[cols]);
508 /* Update position of the container */
509 container->row = rows;
510 container->col = cols;
511 container->x = xoffset[rows];
512 container->y = yoffset[cols];
514 if (container->width_factor == 0)
515 container->width = (width / r_ws->cols);
516 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
517 single_width = container->width;
518 container->width *= container->colspan;
520 if (container->height_factor == 0)
521 container->height = (height / r_ws->rows);
522 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
523 single_height = container->height;
524 container->height *= container->rowspan;
526 /* Render the container if it is not empty */
527 render_container(conn, container);
529 xoffset[rows] += single_width;
530 yoffset[cols] += single_height;
534 render_bars(conn, r_ws, width, &height);
535 render_internal_bar(conn, r_ws, width, font->height + 6);