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