]> git.sur5r.net Git - i3/i3/blob - src/layout.c
More Debug-output
[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                         printf("before client->rect.width = %d, client->rect.height = %d\n", client->rect.width,
208                                         client->rect.height);
209                         /* TODO: vertical default layout */
210                         if (client->force_reconfigure |
211                             (client->rect.width != (client->rect.width = container->width)) |
212                             (client->rect.height != (client->rect.height = container->height / num_clients))) {
213                                 resize_client(connection, client);
214                         } else printf("no reconfigure necessary\n");
215                         printf("after client->rect.width = %d, client->rect.height = %d\n", client->rect.width,
216                                         client->rect.height);
217
218
219                         if (client->force_reconfigure)
220                                 client->force_reconfigure = false;
221
222                         current_client++;
223                 }
224         } else {
225                 i3Font *font = load_font(connection, config.font);
226                 int decoration_height = (font->height + 2 + 2);
227                 struct Stack_Window *stack_win = &(container->stack_win);
228
229                 /* Check if we need to remap our stack title window, it gets unmapped when the container
230                    is empty in src/handlers.c:unmap_notify() */
231                 if (stack_win->height == 0)
232                         xcb_map_window(connection, stack_win->window);
233
234                 /* Check if we need to reconfigure our stack title window */
235                 if ((stack_win->width != (stack_win->width = container->width)) |
236                     (stack_win->height != (stack_win->height = decoration_height * num_clients))) {
237                         xcb_configure_window(connection, stack_win->window,
238                                 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, &(stack_win->width));
239
240                         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
241                         xcb_configure_window(connection, stack_win->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
242                 }
243
244                 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
245                 client = container->currently_focused;
246
247                 /* Check if we changed client->x or client->y by updating it.
248                  * Note the bitwise OR instead of logical OR to force evaluation of both statements */
249                 if (client->force_reconfigure |
250                     (client->rect.x != (client->rect.x = container->x)) |
251                     (client->rect.y != (client->rect.y = container->y + (decoration_height * num_clients))))
252                         reposition_client(connection, client);
253
254                 if (client->force_reconfigure |
255                     (client->rect.width != (client->rect.width = container->width)) |
256                     (client->rect.height !=
257                      (client->rect.height = container->height - (decoration_height * num_clients))))
258                         resize_client(connection, client);
259
260                 client->force_reconfigure = false;
261
262                 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
263                 xcb_configure_window(connection, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
264
265                 /* Render the decorations of all clients */
266                 CIRCLEQ_FOREACH(client, &(container->clients), clients)
267                         decorate_window(connection, client, stack_win->window, stack_win->gc,
268                                         current_client++ * decoration_height);
269         }
270 }
271
272 static void render_bars(xcb_connection_t *connection, Workspace *r_ws, int width, int height) {
273         Client *client;
274         SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
275                 if (client->force_reconfigure |
276                     (client->rect.x != (client->rect.x = 0)) |
277                     (client->rect.y != (client->rect.y = height)))
278                         reposition_client(connection, client);
279
280                 if (client->force_reconfigure |
281                     (client->rect.width != (client->rect.width = width)) |
282                     (client->rect.height != (client->rect.height = client->desired_height)))
283                         resize_client(connection, client);
284
285                 client->force_reconfigure = false;
286                 height += client->desired_height;
287         }
288 }
289
290 void render_layout(xcb_connection_t *connection) {
291         i3Screen *screen;
292
293         TAILQ_FOREACH(screen, &virtual_screens, screens) {
294                 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
295                 Workspace *r_ws = &(workspaces[screen->current_workspace]);
296
297                 printf("Rendering screen %d\n", screen->num);
298                 if (r_ws->fullscreen_client != NULL)
299                         /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
300                         continue;
301
302                 int width = r_ws->rect.width;
303                 int height = r_ws->rect.height;
304
305                 /* Reserve space for dock clients */
306                 Client *client;
307                 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
308                         height -= client->desired_height;
309
310                 printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
311
312                 int xoffset[r_ws->rows];
313                 int yoffset[r_ws->cols];
314                 /* Initialize offsets */
315                 for (int cols = 0; cols < r_ws->cols; cols++)
316                         yoffset[cols] = r_ws->rect.y;
317                 for (int rows = 0; rows < r_ws->rows; rows++)
318                         xoffset[rows] = r_ws->rect.x;
319
320                 /* Go through the whole table and render what’s necessary */
321                 for (int cols = 0; cols < r_ws->cols; cols++)
322                         for (int rows = 0; rows < r_ws->rows; rows++) {
323                                 Container *container = r_ws->table[cols][rows];
324                                 printf("\n========\ncontainer has %d colspan, %d rowspan\n",
325                                                 container->colspan, container->rowspan);
326                                 printf("container at %d, %d\n", xoffset[rows], yoffset[cols]);
327                                 /* Update position of the container */
328                                 container->row = rows;
329                                 container->col = cols;
330                                 container->x = xoffset[rows];
331                                 container->y = yoffset[cols];
332
333                                 if (container->width_factor == 0)
334                                         container->width = (width / r_ws->cols);
335                                 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
336                                 container->width *= container->colspan;
337
338                                 if (container->height_factor == 0)
339                                         container->height = (height / r_ws->rows);
340                                 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
341                                 container->height *= container->rowspan;
342
343                                 /* Render the container if it is not empty */
344                                 render_container(connection, container);
345
346                                 xoffset[rows] += container->width;
347                                 yoffset[cols] += container->height;
348                                 printf("==========\n");
349                         }
350
351                 render_bars(connection, r_ws, width, height);
352         }
353
354         xcb_flush(connection);
355 }