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