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