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
29 * Updates *destination with new_value and returns true if it was changed or false
33 static bool update_if_necessary(uint32_t *destination, const uint32_t new_value) {
34 int old_value = *destination;
36 return ((*destination = new_value) != old_value);
40 * Gets the unoccupied space (= space which is available for windows which were resized by the user)
41 * for the given row. This is necessary to render both, customly resized windows and never touched
42 * windows correctly, meaning that the aspect ratio will be maintained when opening new windows.
45 int get_unoccupied_x(Workspace *workspace, int row) {
46 int unoccupied = workspace->rect.width;
47 float default_factor = ((float)workspace->rect.width / workspace->cols) / workspace->rect.width;
49 LOG("get_unoccupied_x(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
51 for (int cols = 0; cols < workspace->cols;) {
52 Container *con = workspace->table[cols][row];
53 LOG("width_factor[%d][%d] = %f, colspan = %d\n", cols, row, con->width_factor, con->colspan);
54 if (con->width_factor == 0) {
55 LOG("- %d * %f * %d = %f\n", workspace->rect.width, default_factor, con->colspan, workspace->rect.width * default_factor * con->colspan);
56 unoccupied -= workspace->rect.width * default_factor * con->colspan;
61 LOG("unoccupied space: %d\n", unoccupied);
65 /* See get_unoccupied_x() */
66 int get_unoccupied_y(Workspace *workspace, int col) {
67 int unoccupied = workspace->rect.height;
68 float default_factor = ((float)workspace->rect.height / workspace->rows) / workspace->rect.height;
70 LOG("get_unoccupied_y(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
72 for (int rows = 0; rows < workspace->rows;) {
73 Container *con = workspace->table[col][rows];
74 LOG("height_factor[%d][%d] = %f, rowspan %d\n", col, rows, con->height_factor, con->rowspan);
75 if (con->height_factor == 0)
76 unoccupied -= workspace->rect.height * default_factor * con->rowspan;
80 LOG("unoccupied space: %d\n", unoccupied);
85 * Redecorates the given client correctly by checking if it’s in a stacking container and
86 * re-rendering the stack window or just calling decorate_window if it’s not in a stacking
90 void redecorate_window(xcb_connection_t *conn, Client *client) {
91 if (client->container->mode == MODE_STACK) {
92 render_container(conn, client->container);
93 /* We clear the frame to generate exposure events, because the color used
94 in drawing may be different */
95 xcb_clear_area(conn, true, client->frame, 0, 0, client->rect.width, client->rect.height);
96 } else decorate_window(conn, client, client->frame, client->titlegc, 0);
101 * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
102 * When in stacking mode, the window decorations are drawn onto an own window.
105 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
106 i3Font *font = load_font(conn, config.font);
107 int decoration_height = font->height + 2 + 2;
108 uint32_t background_color,
112 /* Clients without a container (docks) won’t get decorated */
113 if (client->container == NULL)
116 if (client->container->currently_focused == client) {
117 /* Distinguish if the window is currently focused… */
118 if (CUR_CELL->currently_focused == client)
119 background_color = get_colorpixel(conn, "#285577");
120 /* …or if it is the focused window in a not focused container */
121 else background_color = get_colorpixel(conn, "#555555");
123 text_color = get_colorpixel(conn, "#ffffff");
124 border_color = get_colorpixel(conn, "#4c7899");
126 background_color = get_colorpixel(conn, "#222222");
127 text_color = get_colorpixel(conn, "#888888");
128 border_color = get_colorpixel(conn, "#333333");
131 /* Our plan is the following:
132 - Draw a rect around the whole client in background_color
133 - Draw two lines in a lighter color
134 - Draw the window’s title
137 /* Draw a rectangle in background color around the window */
138 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
140 /* In stacking mode, we only render the rect for this specific decoration */
141 if (client->container->mode == MODE_STACK) {
142 xcb_rectangle_t rect = {0, offset, client->container->width, offset + decoration_height };
143 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
145 /* We need to use the container’s width because it is the more recent value - when
146 in stacking mode, clients get reconfigured only on demand (the not active client
147 is not reconfigured), so the client’s rect.width would be wrong */
148 xcb_rectangle_t rect = {0, 0, client->container->width, client->rect.height};
149 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
151 /* Draw the inner background to have a black frame around clients (such as mplayer)
152 which cannot be resized exactly in our frames and therefore are centered */
153 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
154 xcb_rectangle_t crect = {2, decoration_height,
155 client->rect.width - (2 + 2), client->rect.height - 2 - decoration_height};
156 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
160 xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
161 xcb_draw_line(conn, drawable, gc, border_color, 2, offset + font->height + 3,
162 2 + client->rect.width, offset + font->height + 3);
164 /* If the client has a title, we draw it */
165 if (client->name != NULL) {
167 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
168 uint32_t values[] = { text_color, background_color, font->id };
169 xcb_change_gc(conn, gc, mask, values);
171 /* name_len == -1 means this is a legacy application which does not specify _NET_WM_NAME,
172 and we don’t handle the old window name (COMPOUND_TEXT) but only _NET_WM_NAME, which
174 if (client->name_len == -1)
175 xcb_image_text_8(conn, strlen(client->name), drawable, gc, 3 /* X */,
176 offset + font->height /* Y = baseline of font */, client->name);
178 xcb_image_text_16(conn, client->name_len, drawable, gc, 3 /* X */,
179 offset + font->height /* Y = baseline of font */, (xcb_char2b_t*)client->name);
184 * Pushes the client’s x and y coordinates to X11
187 static void reposition_client(xcb_connection_t *conn, Client *client) {
188 LOG("frame 0x%08x needs to be pushed to %dx%d\n", client->frame, client->rect.x, client->rect.y);
189 /* Note: We can use a pointer to client->x like an array of uint32_ts
190 because it is followed by client->y by definition */
191 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
195 * Pushes the client’s width/height to X11 and resizes the child window
198 static void resize_client(xcb_connection_t *conn, Client *client) {
199 i3Font *font = load_font(conn, config.font);
201 LOG("resizing client 0x%08x to %d x %d\n", client->frame, client->rect.width, client->rect.height);
202 xcb_configure_window(conn, client->frame,
203 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
204 &(client->rect.width));
206 /* Adjust the position of the child inside its frame.
207 * The coordinates of the child are relative to its frame, we
208 * add a border of 2 pixel to each value */
209 uint32_t mask = XCB_CONFIG_WINDOW_X |
210 XCB_CONFIG_WINDOW_Y |
211 XCB_CONFIG_WINDOW_WIDTH |
212 XCB_CONFIG_WINDOW_HEIGHT;
213 Rect *rect = &(client->child_rect);
214 switch ((client->container != NULL ? client->container->mode : MODE_DEFAULT)) {
218 rect->width = client->rect.width - (2 + 2);
219 rect->height = client->rect.height - 2;
222 if (client->titlebar_position == TITLEBAR_OFF) {
225 rect->width = client->rect.width;
226 rect->height = client->rect.height;
229 rect->y = font->height + 2 + 2;
230 rect->width = client->rect.width - (2 + 2);
231 rect->height = client->rect.height - ((font->height + 2 + 2) + 2);
236 /* Obey the ratio, if any */
237 if (client->proportional_height != 0 &&
238 client->proportional_width != 0) {
239 LOG("proportional height = %d, width = %d\n", client->proportional_height, client->proportional_width);
240 double new_height = rect->height + 1;
241 int new_width = rect->width;
243 while (new_height > rect->height) {
244 new_height = ((double)client->proportional_height / client->proportional_width) * new_width;
246 if (new_height > rect->height)
249 /* Center the window */
250 rect->y += ceil(rect->height / 2) - floor(new_height / 2);
251 rect->x += ceil(rect->width / 2) - floor(new_width / 2);
253 rect->height = new_height;
254 rect->width = new_width;
255 LOG("new_height = %f, new_width = %d\n", new_height, new_width);
258 LOG("child will be at %dx%d with size %dx%d\n", rect->x, rect->y, rect->width, rect->height);
260 xcb_configure_window(conn, client->child, mask, &(rect->x));
262 /* After configuring a child window we need to fake a configure_notify_event (see ICCCM 4.2.3).
263 * This is necessary to inform the client of its position relative to the root window,
264 * not relative to its frame (as done in the configure_notify_event by the x server). */
265 fake_absolute_configure_notify(conn, client);
269 * Renders the given container. Is called by render_layout() or individually (for example
270 * when focus changes in a stacking container)
273 void render_container(xcb_connection_t *conn, Container *container) {
275 int num_clients = 0, current_client = 0;
277 if (container->currently_focused == NULL)
280 CIRCLEQ_FOREACH(client, &(container->clients), clients)
283 if (container->mode == MODE_DEFAULT) {
284 LOG("got %d clients in this default container.\n", num_clients);
285 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
286 /* If the client is in fullscreen mode, it does not get reconfigured */
287 if (container->workspace->fullscreen_client == client) {
292 /* Check if we changed client->x or client->y by updating it.
293 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
294 if (client->force_reconfigure |
295 update_if_necessary(&(client->rect.x), container->x) |
296 update_if_necessary(&(client->rect.y), container->y +
297 (container->height / num_clients) * current_client))
298 reposition_client(conn, client);
300 /* TODO: vertical default layout */
301 if (client->force_reconfigure |
302 update_if_necessary(&(client->rect.width), container->width) |
303 update_if_necessary(&(client->rect.height), container->height / num_clients))
304 resize_client(conn, client);
306 client->force_reconfigure = false;
311 i3Font *font = load_font(conn, config.font);
312 int decoration_height = (font->height + 2 + 2);
313 struct Stack_Window *stack_win = &(container->stack_win);
315 /* Check if we need to remap our stack title window, it gets unmapped when the container
316 is empty in src/handlers.c:unmap_notify() */
317 if (stack_win->rect.height == 0)
318 xcb_map_window(conn, stack_win->window);
320 /* Check if we need to reconfigure our stack title window */
321 if (update_if_necessary(&(stack_win->rect.x), container->x) |
322 update_if_necessary(&(stack_win->rect.y), container->y) |
323 update_if_necessary(&(stack_win->rect.width), container->width) |
324 update_if_necessary(&(stack_win->rect.height), decoration_height * num_clients)) {
326 /* Configuration can happen in two slightly different ways:
328 If there is no client in fullscreen mode, 5 parameters are passed
329 (x, y, width, height, stack mode is set to above which means top-most position).
331 If there is a fullscreen client, the fourth parameter is set to to the
332 fullscreen window as sibling and the stack mode is set to below, which means
333 that the stack_window will be placed just below the sibling, that is, under
334 the fullscreen window.
336 uint32_t values[] = { stack_win->rect.x, stack_win->rect.y,
337 stack_win->rect.width, stack_win->rect.height,
338 XCB_STACK_MODE_ABOVE, XCB_STACK_MODE_BELOW };
339 uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
340 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
341 XCB_CONFIG_WINDOW_STACK_MODE;
343 /* If there is no fullscreen client, we raise the stack window */
344 if (container->workspace->fullscreen_client != NULL) {
345 mask |= XCB_CONFIG_WINDOW_SIBLING;
346 values[4] = container->workspace->fullscreen_client->frame;
349 xcb_configure_window(conn, stack_win->window, mask, values);
352 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
353 client = container->currently_focused;
355 /* Render the decorations of all clients */
356 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
357 /* If the client is in fullscreen mode, it does not get reconfigured */
358 if (container->workspace->fullscreen_client == client) {
363 /* Check if we changed client->x or client->y by updating it.
364 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
365 if (client->force_reconfigure |
366 update_if_necessary(&(client->rect.x), container->x) |
367 update_if_necessary(&(client->rect.y), container->y + (decoration_height * num_clients)))
368 reposition_client(conn, client);
370 if (client->force_reconfigure |
371 update_if_necessary(&(client->rect.width), container->width) |
372 update_if_necessary(&(client->rect.height), container->height - (decoration_height * num_clients)))
373 resize_client(conn, client);
375 client->force_reconfigure = false;
377 decorate_window(conn, client, stack_win->window, stack_win->gc,
378 current_client++ * decoration_height);
383 static void render_bars(xcb_connection_t *conn, Workspace *r_ws, int width, int *height) {
385 SLIST_FOREACH(client, &(r_ws->screen->dock_clients), dock_clients) {
386 LOG("client is at %d, should be at %d\n", client->rect.y, *height);
387 if (client->force_reconfigure |
388 update_if_necessary(&(client->rect.x), 0) |
389 update_if_necessary(&(client->rect.y), *height))
390 reposition_client(conn, client);
392 if (client->force_reconfigure |
393 update_if_necessary(&(client->rect.width), width) |
394 update_if_necessary(&(client->rect.height), client->desired_height))
395 resize_client(conn, client);
397 client->force_reconfigure = false;
398 LOG("desired_height = %d\n", client->desired_height);
399 *height += client->desired_height;
403 static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int width, int height) {
404 LOG("Rendering internal bar\n");
405 i3Font *font = load_font(conn, config.font);
406 i3Screen *screen = r_ws->screen;
407 enum { SET_NORMAL = 0, SET_FOCUSED = 1 };
408 uint32_t background_color[2],
414 black = get_colorpixel(conn, "#000000");
416 background_color[SET_NORMAL] = get_colorpixel(conn, "#222222");
417 text_color[SET_NORMAL] = get_colorpixel(conn, "#888888");
418 border_color[SET_NORMAL] = get_colorpixel(conn, "#333333");
420 background_color[SET_FOCUSED] = get_colorpixel(conn, "#285577");
421 text_color[SET_FOCUSED] = get_colorpixel(conn, "#ffffff");
422 border_color[SET_FOCUSED] = get_colorpixel(conn, "#4c7899");
424 /* Fill the whole bar in black */
425 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black);
426 xcb_rectangle_t rect = {0, 0, width, height};
427 xcb_poly_fill_rectangle(conn, screen->bar, screen->bargc, 1, &rect);
430 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FONT, font->id);
433 for (int c = 0; c < 10; c++) {
434 if (workspaces[c].screen != screen)
437 int set = (screen->current_workspace == c ? SET_FOCUSED : SET_NORMAL);
439 xcb_draw_rect(conn, screen->bar, screen->bargc, border_color[set],
440 drawn * height, 1, height - 2, height - 2);
441 xcb_draw_rect(conn, screen->bar, screen->bargc, background_color[set],
442 drawn * height + 1, 2, height - 4, height - 4);
444 snprintf(label, sizeof(label), "%d", c+1);
445 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, text_color[set]);
446 xcb_change_gc_single(conn, screen->bargc, XCB_GC_BACKGROUND, background_color[set]);
447 xcb_image_text_8(conn, strlen(label), screen->bar, screen->bargc, drawn * height + 5 /* X */,
448 font->height + 1 /* Y = baseline of font */, label);
452 LOG("done rendering internal\n");
456 * Modifies the event mask of all clients on the given workspace to either ignore or to handle
457 * enter notifies. It is handy to ignore notifies because they will be sent when a window is mapped
458 * under the cursor, thus when the user didn’t enter the window actively at all.
461 void ignore_enter_notify_forall(xcb_connection_t *conn, Workspace *workspace, bool ignore_enter_notify) {
465 LOG("Ignore enter_notify = %d\n", ignore_enter_notify);
468 CIRCLEQ_FOREACH(client, &(workspace->table[cols][rows]->clients), clients) {
469 /* Change event mask for the decorations */
470 values[0] = FRAME_EVENT_MASK;
471 if (ignore_enter_notify)
472 values[0] &= ~(XCB_EVENT_MASK_ENTER_WINDOW);
473 xcb_change_window_attributes(conn, client->frame, XCB_CW_EVENT_MASK, values);
475 /* Change event mask for the child itself */
476 values[0] = CHILD_EVENT_MASK;
477 if (ignore_enter_notify)
478 values[0] &= ~(XCB_EVENT_MASK_ENTER_WINDOW);
479 xcb_change_window_attributes(conn, client->child, XCB_CW_EVENT_MASK, values);
484 * Renders the given workspace on the given screen
487 void render_workspace(xcb_connection_t *conn, i3Screen *screen, Workspace *r_ws) {
488 i3Font *font = load_font(conn, config.font);
489 int width = r_ws->rect.width;
490 int height = r_ws->rect.height;
492 /* Reserve space for dock clients */
494 SLIST_FOREACH(client, &(screen->dock_clients), dock_clients)
495 height -= client->desired_height;
497 /* Space for the internal bar */
498 height -= (font->height + 6);
500 LOG("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
502 int xoffset[r_ws->rows];
503 int yoffset[r_ws->cols];
504 /* Initialize offsets */
505 for (int cols = 0; cols < r_ws->cols; cols++)
506 yoffset[cols] = r_ws->rect.y;
507 for (int rows = 0; rows < r_ws->rows; rows++)
508 xoffset[rows] = r_ws->rect.x;
510 dump_table(conn, r_ws);
512 ignore_enter_notify_forall(conn, r_ws, true);
514 /* Go through the whole table and render what’s necessary */
516 Container *container = r_ws->table[cols][rows];
517 int single_width, single_height;
520 LOG("container has %d colspan, %d rowspan\n",
521 container->colspan, container->rowspan);
522 LOG("container at %d, %d\n", xoffset[rows], yoffset[cols]);
523 /* Update position of the container */
524 container->row = rows;
525 container->col = cols;
526 container->x = xoffset[rows];
527 container->y = yoffset[cols];
529 if (container->width_factor == 0)
530 container->width = (width / r_ws->cols);
531 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
532 single_width = container->width;
533 container->width *= container->colspan;
535 if (container->height_factor == 0)
536 container->height = (height / r_ws->rows);
537 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
538 single_height = container->height;
539 container->height *= container->rowspan;
541 /* Render the container if it is not empty */
542 render_container(conn, container);
544 xoffset[rows] += single_width;
545 yoffset[cols] += single_height;
549 ignore_enter_notify_forall(conn, r_ws, false);
551 render_bars(conn, r_ws, width, &height);
552 render_internal_bar(conn, r_ws, width, font->height + 6);
556 * Renders the whole layout, that is: Go through each screen, each workspace, each container
557 * and render each client. This also renders the bars.
559 * If you don’t need to render *everything*, you should call render_container on the container
560 * you want to refresh.
563 void render_layout(xcb_connection_t *conn) {
566 TAILQ_FOREACH(screen, virtual_screens, screens) {
567 LOG("Rendering screen %d\n", screen->num);
568 render_workspace(conn, screen, &(workspaces[screen->current_workspace]));