4 * i3 - an improved dynamic tiling window manager
6 * (c) 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
24 /* All functions handling layout/drawing of window decorations */
27 * (Re-)draws window decorations for a given Client
30 void decorate_window(xcb_connection_t *conn, Client *client) {
33 i3Font *font = load_font(conn, pattern);
34 uint32_t background_color,
38 if (client->container->currently_focused == client) {
39 background_color = get_colorpixel(conn, client->frame, "#285577");
40 text_color = get_colorpixel(conn, client->frame, "#ffffff");
41 border_color = get_colorpixel(conn, client->frame, "#4c7899");
43 background_color = get_colorpixel(conn, client->frame, "#222222");
44 text_color = get_colorpixel(conn, client->frame, "#888888");
45 border_color = get_colorpixel(conn, client->frame, "#333333");
48 /* Our plan is the following:
49 - Draw a rect around the whole client in background_color
50 - Draw two lines in a lighter color
51 - Draw the window’s title
53 Note that xcb_image_text apparently adds 1xp border around the font? Can anyone confirm this?
56 /* Draw a green rectangle around the window */
57 mask = XCB_GC_FOREGROUND;
58 values[0] = background_color;
59 xcb_change_gc(conn, client->titlegc, mask, values);
61 xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
62 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
65 /* TODO: this needs to be more beautiful somewhen. maybe stdarg + change_gc(gc, ...) ? */
66 #define DRAW_LINE(colorpixel, x, y, to_x, to_y) { \
67 uint32_t draw_values[1]; \
68 draw_values[0] = colorpixel; \
69 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND, draw_values); \
70 xcb_point_t points[] = {{x, y}, {to_x, to_y}}; \
71 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 2, points); \
74 DRAW_LINE(border_color, 2, 0, client->rect.width, 0);
75 DRAW_LINE(border_color, 2, font->height + 3, 2 + client->rect.width, font->height + 3);
78 mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
80 values[0] = text_color;
81 values[1] = background_color;
84 xcb_change_gc(conn, client->titlegc, mask, values);
88 asprintf(&label, "(%08x) %.*s", client->frame, client->name_len, client->name);
89 xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), client->frame,
90 client->titlegc, 3 /* X */, font->height /* Y = baseline of font */, label);
91 check_error(conn, text_cookie, "Could not draw client's title");
95 static void render_container(xcb_connection_t *connection, Container *container) {
97 i3Font *font = load_font(connection, pattern);
99 if (container->mode == MODE_DEFAULT) {
101 CIRCLEQ_FOREACH(client, &(container->clients), clients)
103 printf("got %d clients in this default container.\n", num_clients);
105 int current_client = 0;
106 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
107 /* TODO: at the moment, every column/row is screen / num_cols. This
108 * needs to be changed to "percentage of the screen" by
109 * default and adjustable by the user if necessary.
112 /* Check if we changed client->x or client->y by updating it…
113 * Note the bitwise OR instead of logical OR to force evaluation of both statements */
114 if (client->force_reconfigure |
115 (client->rect.x != (client->rect.x = container->x + (container->col * container->width))) |
116 (client->rect.y != (client->rect.y = container->y + (container->row * container->height +
117 (container->height / num_clients) * current_client)))) {
118 printf("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
119 /* Note: We can use a pointer to client->x like an array of uint32_ts
120 because it is followed by client->y by definition */
121 xcb_configure_window(connection, client->frame,
122 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
125 /* TODO: vertical default layout */
126 if (client->force_reconfigure |
127 (client->rect.width != (client->rect.width = container->width)) |
128 (client->rect.height != (client->rect.height = container->height / num_clients))) {
129 xcb_configure_window(connection, client->frame,
130 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
131 &(client->rect.width));
133 /* Adjust the position of the child inside its frame.
134 * The coordinates of the child are relative to its frame, we
135 * add a border of 2 pixel to each value */
136 uint32_t mask = XCB_CONFIG_WINDOW_X |
137 XCB_CONFIG_WINDOW_Y |
138 XCB_CONFIG_WINDOW_WIDTH |
139 XCB_CONFIG_WINDOW_HEIGHT;
140 uint32_t values[4] = {2, /* x */
141 font->height + 2 + 2, /* y */
142 client->rect.width - (2 + 2), /* width */
143 client->rect.height - ((font->height + 2 + 2) + 2)}; /* height */
145 printf("child will be at %dx%d with size %dx%d\n",
146 values[0], values[1], values[2], values[3]);
148 xcb_configure_window(connection, client->child, mask, values);
151 if (client->force_reconfigure)
152 client->force_reconfigure = false;
157 /* TODO: Implement stacking */
161 void render_layout(xcb_connection_t *connection) {
164 TAILQ_FOREACH(screen, &virtual_screens, screens) {
165 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
166 Workspace *r_ws = &(workspaces[screen->current_workspace]);
168 printf("Rendering screen %d\n", screen->num);
169 if (r_ws->fullscreen_client != NULL)
170 /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
172 int width = r_ws->rect.width;
173 int height = r_ws->rect.height;
175 printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
176 printf("each of them therefore is %d px width and %d px height\n",
177 width / r_ws->cols, height / r_ws->rows);
179 /* Go through the whole table and render what’s necessary */
180 for (int cols = 0; cols < r_ws->cols; cols++)
181 for (int rows = 0; rows < r_ws->rows; rows++) {
182 Container *container = r_ws->table[cols][rows];
183 printf("container has %d colspan, %d rowspan\n",
184 container->colspan, container->rowspan);
185 /* Update position of the container */
186 container->row = rows;
187 container->col = cols;
188 container->x = r_ws->rect.x;
189 container->y = r_ws->rect.y;
190 container->width = (width / r_ws->cols) * container->colspan;
191 container->height = (height / r_ws->rows) * container->rowspan;
194 render_container(connection, container);
198 xcb_flush(connection);