]> git.sur5r.net Git - i3/i3/blob - src/layout.c
a3935996d9dc7920ae88cec6eb8ef63323f2b151
[i3/i3] / src / layout.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * layout.c: Functions handling layout/drawing of window decorations
11  *
12  */
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <xcb/xcb.h>
17 #include <assert.h>
18
19 #include "config.h"
20 #include "font.h"
21 #include "i3.h"
22 #include "xcb.h"
23 #include "table.h"
24 #include "util.h"
25 #include "xinerama.h"
26
27 /*
28  * Gets the unoccupied space (= space which is available for windows which were resized by the user)
29  * for the given row. This is necessary to render both, customly resized windows and never touched
30  * windows correctly, meaning that the aspect ratio will be maintained when opening new windows.
31  *
32  */
33 int get_unoccupied_x(Workspace *workspace, int row) {
34         int unoccupied = workspace->rect.width;
35         float default_factor = ((float)workspace->rect.width / workspace->cols) / workspace->rect.width;
36
37         printf("get_unoccupied_x(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
38
39         for (int cols = 0; cols < workspace->cols;) {
40                 Container *con = workspace->table[cols][row];
41                 printf("width_factor[%d][%d] = %f, colspan = %d\n", cols, row, con->width_factor, con->colspan);
42                 if (con->width_factor == 0)
43                         unoccupied -= workspace->rect.width * default_factor * con->colspan;
44                 cols += con->colspan;
45         }
46
47         assert(unoccupied != 0);
48
49         printf("unoccupied space: %d\n", unoccupied);
50         return unoccupied;
51 }
52
53 /* See get_unoccupied_x() */
54 int get_unoccupied_y(Workspace *workspace, int col) {
55         int unoccupied = workspace->rect.height;
56         float default_factor = ((float)workspace->rect.height / workspace->rows) / workspace->rect.height;
57
58         printf("get_unoccupied_y(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
59
60         for (int rows = 0; rows < workspace->rows;) {
61                 Container *con = workspace->table[col][rows];
62                 printf("height_factor[%d][%d] = %f, rowspan %d\n", col, rows, con->height_factor, con->rowspan);
63                 if (con->height_factor == 0)
64                         unoccupied -= workspace->rect.height * default_factor * con->rowspan;
65                 rows += con->rowspan;
66         }
67
68         assert(unoccupied != 0);
69
70         printf("unoccupied space: %d\n", unoccupied);
71         return unoccupied;
72 }
73
74 /*
75  * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
76  * When in stacking mode, the window decorations are drawn onto an own window.
77  *
78  */
79 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
80         i3Font *font = load_font(conn, config.font);
81         uint32_t background_color,
82                  text_color,
83                  border_color;
84
85         /* Clients without a container (docks) won’t get decorated */
86         if (client->container == NULL)
87                 return;
88
89         if (client->container->currently_focused == client) {
90                 background_color = get_colorpixel(conn, client, client->frame, "#285577");
91                 text_color = get_colorpixel(conn, client, client->frame, "#ffffff");
92                 border_color = get_colorpixel(conn, client, client->frame, "#4c7899");
93         } else {
94                 background_color = get_colorpixel(conn, client, client->frame, "#222222");
95                 text_color = get_colorpixel(conn, client, client->frame, "#888888");
96                 border_color = get_colorpixel(conn, client, client->frame, "#333333");
97         }
98
99         /* Our plan is the following:
100            - Draw a rect around the whole client in background_color
101            - Draw two lines in a lighter color
102            - Draw the window’s title
103          */
104
105         /* Draw a green rectangle around the window */
106         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
107
108         xcb_rectangle_t rect = {0, offset, client->rect.width, offset + client->rect.height};
109         xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
110
111         /* Draw the lines */
112         xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
113         xcb_draw_line(conn, drawable, gc, border_color, 2, offset + font->height + 3,
114                       2 + client->rect.width, offset + font->height + 3);
115
116         /* Draw the font */
117         uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
118         uint32_t values[] = { text_color, background_color, font->id };
119         xcb_change_gc(conn, gc, mask, values);
120
121         /* TODO: utf8? */
122         char *label;
123         asprintf(&label, "(%08x) %.*s", client->frame, client->name_len, client->name);
124         xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), drawable,
125                                         gc, 3 /* X */, offset + font->height /* Y = baseline of font */, label);
126         check_error(conn, text_cookie, "Could not draw client's title");
127         free(label);
128 }
129
130 /*
131  * Pushes the client’s x and y coordinates to X11
132  *
133  */
134 static void reposition_client(xcb_connection_t *connection, Client *client) {
135         printf("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
136         /* Note: We can use a pointer to client->x like an array of uint32_ts
137            because it is followed by client->y by definition */
138         xcb_configure_window(connection, client->frame,
139                         XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
140 }
141
142 /*
143  * Pushes the client’s width/height to X11 and resizes the child window
144  *
145  */
146 static void resize_client(xcb_connection_t *connection, Client *client) {
147         i3Font *font = load_font(connection, config.font);
148
149         printf("resizing client to %d x %d\n", client->rect.width, client->rect.height);
150         xcb_configure_window(connection, client->frame,
151                         XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
152                         &(client->rect.width));
153
154         /* Adjust the position of the child inside its frame.
155          * The coordinates of the child are relative to its frame, we
156          * add a border of 2 pixel to each value */
157         uint32_t mask = XCB_CONFIG_WINDOW_X |
158                         XCB_CONFIG_WINDOW_Y |
159                         XCB_CONFIG_WINDOW_WIDTH |
160                         XCB_CONFIG_WINDOW_HEIGHT;
161         Rect rect;
162         if (client->titlebar_position == TITLEBAR_OFF ||
163             client->container->mode == MODE_STACK) {
164                 rect.x = 0;
165                 rect.y = 0;
166                 rect.width = client->rect.width;
167                 rect.height = client->rect.height;
168         } else {
169                 rect.x = 2;
170                 rect.y = font->height + 2 + 2;
171                 rect.width = client->rect.width - (2 + 2);
172                 rect.height = client->rect.height - ((font->height + 2 + 2) + 2);
173         }
174
175         printf("child will be at %dx%d with size %dx%d\n", rect.x, rect.y, rect.width, rect.height);
176
177         xcb_configure_window(connection, client->child, mask, &(rect.x));
178 }
179
180 /*
181  * Renders the given container. Is called by render_layout() or individually (for example
182  * when focus changes in a stacking container)
183  *
184  */
185 void render_container(xcb_connection_t *connection, Container *container) {
186         Client *client;
187         int num_clients = 0, current_client = 0;
188
189         if (container->currently_focused == NULL)
190                 return;
191
192         CIRCLEQ_FOREACH(client, &(container->clients), clients)
193                 num_clients++;
194
195         if (container->mode == MODE_DEFAULT) {
196                 printf("got %d clients in this default container.\n", num_clients);
197                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
198                         /* Check if we changed client->x or client->y by updating it.
199                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
200                         if (client->force_reconfigure |
201                             (client->rect.x != (client->rect.x = container->x)) |
202                             (client->rect.y != (client->rect.y = container->y +
203                                           (container->height / num_clients) * current_client))) {
204                                 reposition_client(connection, client);
205                         }
206
207                         /* TODO: vertical default layout */
208                         if (client->force_reconfigure |
209                             (client->rect.width != (client->rect.width = container->width)) |
210                             (client->rect.height != (client->rect.height = container->height / num_clients))) {
211                                 resize_client(connection, client);
212                         }
213
214                         if (client->force_reconfigure)
215                                 client->force_reconfigure = false;
216
217                         current_client++;
218                 }
219         } else {
220                 i3Font *font = load_font(connection, config.font);
221                 int decoration_height = (font->height + 2 + 2);
222                 struct Stack_Window *stack_win = &(container->stack_win);
223
224                 /* Check if we need to remap our stack title window, it gets unmapped when the container
225                    is empty in src/handlers.c:unmap_notify() */
226                 if (stack_win->height == 0)
227                         xcb_map_window(connection, stack_win->window);
228
229                 /* Check if we need to reconfigure our stack title window */
230                 if ((stack_win->width != (stack_win->width = container->width)) |
231                     (stack_win->height != (stack_win->height = decoration_height * num_clients)))
232                         xcb_configure_window(connection, stack_win->window,
233                                 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, &(stack_win->width));
234
235                 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
236                 client = container->currently_focused;
237
238                 /* Check if we changed client->x or client->y by updating it.
239                  * Note the bitwise OR instead of logical OR to force evaluation of both statements */
240                 if (client->force_reconfigure |
241                     (client->rect.x != (client->rect.x = container->x)) |
242                     (client->rect.y != (client->rect.y = container->y + (decoration_height * num_clients))))
243                         reposition_client(connection, client);
244
245                 if (client->force_reconfigure |
246                     (client->rect.width != (client->rect.width = container->width)) |
247                     (client->rect.height !=
248                      (client->rect.height = container->height - (decoration_height * num_clients))))
249                         resize_client(connection, client);
250
251                 client->force_reconfigure = false;
252
253                 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
254                 xcb_configure_window(connection, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
255
256                 /* Render the decorations of all clients */
257                 CIRCLEQ_FOREACH(client, &(container->clients), clients)
258                         decorate_window(connection, client, stack_win->window, stack_win->gc,
259                                         current_client++ * decoration_height);
260         }
261 }
262
263 static void render_bars(xcb_connection_t *connection, Workspace *r_ws, int width, int height) {
264         Client *client;
265         SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
266                 if (client->force_reconfigure |
267                     (client->rect.x != (client->rect.x = 0)) |
268                     (client->rect.y != (client->rect.y = height)))
269                         reposition_client(connection, client);
270
271                 if (client->force_reconfigure |
272                     (client->rect.width != (client->rect.width = width)) |
273                     (client->rect.height != (client->rect.height = client->desired_height)))
274                         resize_client(connection, client);
275
276                 client->force_reconfigure = false;
277                 height += client->desired_height;
278         }
279 }
280
281 void render_layout(xcb_connection_t *connection) {
282         i3Screen *screen;
283
284         TAILQ_FOREACH(screen, &virtual_screens, screens) {
285                 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
286                 Workspace *r_ws = &(workspaces[screen->current_workspace]);
287
288                 printf("Rendering screen %d\n", screen->num);
289                 if (r_ws->fullscreen_client != NULL)
290                         /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
291                         continue;
292
293                 int width = r_ws->rect.width;
294                 int height = r_ws->rect.height;
295
296                 /* Reserve space for dock clients */
297                 Client *client;
298                 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
299                         height -= client->desired_height;
300
301                 printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
302
303                 int xoffset[r_ws->rows];
304                 int yoffset[r_ws->cols];
305                 /* Initialize offsets */
306                 for (int cols = 0; cols < r_ws->cols; cols++)
307                         yoffset[cols] = r_ws->rect.y;
308                 for (int rows = 0; rows < r_ws->rows; rows++)
309                         xoffset[rows] = r_ws->rect.x;
310
311                 /* Go through the whole table and render what’s necessary */
312                 for (int cols = 0; cols < r_ws->cols; cols++)
313                         for (int rows = 0; rows < r_ws->rows; rows++) {
314                                 Container *container = r_ws->table[cols][rows];
315                                 printf("\n========\ncontainer has %d colspan, %d rowspan\n",
316                                                 container->colspan, container->rowspan);
317                                 printf("container at %d, %d\n", xoffset[rows], yoffset[cols]);
318                                 /* Update position of the container */
319                                 container->row = rows;
320                                 container->col = cols;
321                                 container->x = xoffset[rows];
322                                 container->y = yoffset[cols];
323
324                                 if (container->width_factor == 0)
325                                         container->width = (width / r_ws->cols);
326                                 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
327                                 container->width *= container->colspan;
328
329                                 if (container->height_factor == 0)
330                                         container->height = (height / r_ws->rows);
331                                 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
332                                 container->height *= container->rowspan;
333
334                                 /* Render the container if it is not empty */
335                                 render_container(connection, container);
336
337                                 xoffset[rows] += container->width;
338                                 yoffset[cols] += container->height;
339                                 printf("==========\n");
340                         }
341
342                 render_bars(connection, r_ws, width, height);
343         }
344
345         xcb_flush(connection);
346 }