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