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);
166 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
167 uint32_t values[] = { text_color, background_color, font->id };
168 xcb_change_gc(conn, gc, mask, values);
172 asprintf(&label, "%.*s", client->name_len, client->name);
173 xcb_image_text_8(conn, strlen(label), drawable, gc, 3 /* X */, offset + font->height /* Y = baseline of font */, label);
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 /* Check if we changed client->x or client->y by updating it.
296 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
297 if (client->force_reconfigure |
298 update_if_necessary(&(client->rect.x), container->x) |
299 update_if_necessary(&(client->rect.y), container->y +
300 (container->height / num_clients) * current_client))
301 reposition_client(conn, client);
303 /* TODO: vertical default layout */
304 if (client->force_reconfigure |
305 update_if_necessary(&(client->rect.width), container->width) |
306 update_if_necessary(&(client->rect.height), container->height / num_clients))
307 resize_client(conn, client);
309 client->force_reconfigure = false;
314 i3Font *font = load_font(conn, config.font);
315 int decoration_height = (font->height + 2 + 2);
316 struct Stack_Window *stack_win = &(container->stack_win);
318 /* Check if we need to remap our stack title window, it gets unmapped when the container
319 is empty in src/handlers.c:unmap_notify() */
320 if (stack_win->rect.height == 0)
321 xcb_map_window(conn, stack_win->window);
323 /* Check if we need to reconfigure our stack title window */
324 if (update_if_necessary(&(stack_win->rect.x), container->x) |
325 update_if_necessary(&(stack_win->rect.y), container->y) |
326 update_if_necessary(&(stack_win->rect.width), container->width) |
327 update_if_necessary(&(stack_win->rect.height), decoration_height * num_clients)) {
329 uint32_t values[] = { stack_win->rect.x, stack_win->rect.y,
330 stack_win->rect.width, stack_win->rect.height,
331 XCB_STACK_MODE_ABOVE };
333 xcb_configure_window(conn, stack_win->window,
334 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
335 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
336 XCB_CONFIG_WINDOW_STACK_MODE,
340 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
341 client = container->currently_focused;
343 if (container->workspace->fullscreen_client == NULL) {
344 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
345 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
348 /* Render the decorations of all clients */
349 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
350 /* Check if we changed client->x or client->y by updating it.
351 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
352 if (client->force_reconfigure |
353 update_if_necessary(&(client->rect.x), container->x) |
354 update_if_necessary(&(client->rect.y), container->y + (decoration_height * num_clients)))
355 reposition_client(conn, client);
357 if (client->force_reconfigure |
358 update_if_necessary(&(client->rect.width), container->width) |
359 update_if_necessary(&(client->rect.height), container->height - (decoration_height * num_clients)))
360 resize_client(conn, client);
362 client->force_reconfigure = false;
364 decorate_window(conn, client, stack_win->window, stack_win->gc,
365 current_client++ * decoration_height);
370 static void render_bars(xcb_connection_t *conn, Workspace *r_ws, int width, int *height) {
372 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
373 if (client->force_reconfigure |
374 update_if_necessary(&(client->rect.x), 0) |
375 update_if_necessary(&(client->rect.y), *height))
376 reposition_client(conn, client);
378 if (client->force_reconfigure |
379 update_if_necessary(&(client->rect.width), width) |
380 update_if_necessary(&(client->rect.height), client->desired_height))
381 resize_client(conn, client);
383 client->force_reconfigure = false;
384 *height += client->desired_height;
388 static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int width, int height) {
389 LOG("Rendering internal bar\n");
390 i3Font *font = load_font(conn, config.font);
391 i3Screen *screen = r_ws->screen;
392 enum { SET_NORMAL = 0, SET_FOCUSED = 1 };
393 uint32_t background_color[2],
399 black = get_colorpixel(conn, "#000000");
401 background_color[SET_NORMAL] = get_colorpixel(conn, "#222222");
402 text_color[SET_NORMAL] = get_colorpixel(conn, "#888888");
403 border_color[SET_NORMAL] = get_colorpixel(conn, "#333333");
405 background_color[SET_FOCUSED] = get_colorpixel(conn, "#285577");
406 text_color[SET_FOCUSED] = get_colorpixel(conn, "#ffffff");
407 border_color[SET_FOCUSED] = get_colorpixel(conn, "#4c7899");
409 /* Fill the whole bar in black */
410 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black);
411 xcb_rectangle_t rect = {0, 0, width, height};
412 xcb_poly_fill_rectangle(conn, screen->bar, screen->bargc, 1, &rect);
415 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FONT, font->id);
418 for (int c = 0; c < 10; c++) {
419 if (workspaces[c].screen == screen) {
420 int set = (screen->current_workspace == c ? SET_FOCUSED : SET_NORMAL);
422 xcb_draw_rect(conn, screen->bar, screen->bargc, border_color[set],
423 drawn * height, 1, height - 2, height - 2);
424 xcb_draw_rect(conn, screen->bar, screen->bargc, background_color[set],
425 drawn * height + 1, 2, height - 4, height - 4);
427 snprintf(label, sizeof(label), "%d", c+1);
428 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, text_color[set]);
429 xcb_change_gc_single(conn, screen->bargc, XCB_GC_BACKGROUND, background_color[set]);
430 xcb_image_text_8(conn, strlen(label), screen->bar, screen->bargc, drawn * height + 5 /* X */,
431 font->height + 1 /* Y = baseline of font */, label);
436 LOG("done rendering internal\n");
439 void render_layout(xcb_connection_t *conn) {
441 i3Font *font = load_font(conn, config.font);
443 TAILQ_FOREACH(screen, virtual_screens, screens) {
444 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
445 Workspace *r_ws = &(workspaces[screen->current_workspace]);
447 LOG("Rendering screen %d\n", screen->num);
448 if (r_ws->fullscreen_client != NULL)
449 /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
452 int width = r_ws->rect.width;
453 int height = r_ws->rect.height;
455 /* Reserve space for dock clients */
457 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
458 height -= client->desired_height;
460 /* Space for the internal bar */
461 height -= (font->height + 6);
463 LOG("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
465 int xoffset[r_ws->rows];
466 int yoffset[r_ws->cols];
467 /* Initialize offsets */
468 for (int cols = 0; cols < r_ws->cols; cols++)
469 yoffset[cols] = r_ws->rect.y;
470 for (int rows = 0; rows < r_ws->rows; rows++)
471 xoffset[rows] = r_ws->rect.x;
473 dump_table(conn, r_ws);
474 /* Go through the whole table and render what’s necessary */
475 for (int cols = 0; cols < r_ws->cols; cols++)
476 for (int rows = 0; rows < r_ws->rows; rows++) {
477 Container *container = r_ws->table[cols][rows];
478 int single_width, single_height;
481 LOG("container has %d colspan, %d rowspan\n",
482 container->colspan, container->rowspan);
483 LOG("container at %d, %d\n", xoffset[rows], yoffset[cols]);
484 /* Update position of the container */
485 container->row = rows;
486 container->col = cols;
487 container->x = xoffset[rows];
488 container->y = yoffset[cols];
490 if (container->width_factor == 0)
491 container->width = (width / r_ws->cols);
492 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
493 single_width = container->width;
494 container->width *= container->colspan;
496 if (container->height_factor == 0)
497 container->height = (height / r_ws->rows);
498 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
499 single_height = container->height;
500 container->height *= container->rowspan;
502 /* Render the container if it is not empty */
503 render_container(conn, container);
505 xoffset[rows] += single_width;
506 yoffset[cols] += single_height;
510 render_bars(conn, r_ws, width, &height);
511 render_internal_bar(conn, r_ws, width, 18);