]> git.sur5r.net Git - i3/i3/blob - src/layout.c
a6507f3a016af3d4d370d52e87440db625a8145d
[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 "i3.h"
21 #include "xcb.h"
22 #include "table.h"
23 #include "util.h"
24 #include "xinerama.h"
25 #include "layout.h"
26
27 /*
28  * Updates *destination with new_value and returns true if it was changed or false
29  * if it was the same
30  *
31  */
32 static bool update_if_necessary(uint32_t *destination, const uint32_t new_value) {
33         int old_value = *destination;
34
35         return ((*destination = new_value) != old_value);
36 }
37
38 /*
39  * Gets the unoccupied space (= space which is available for windows which were resized by the user)
40  * for the given row. This is necessary to render both, customly resized windows and never touched
41  * windows correctly, meaning that the aspect ratio will be maintained when opening new windows.
42  *
43  */
44 int get_unoccupied_x(Workspace *workspace, int row) {
45         int unoccupied = workspace->rect.width;
46         float default_factor = ((float)workspace->rect.width / workspace->cols) / workspace->rect.width;
47
48         printf("get_unoccupied_x(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
49
50         for (int cols = 0; cols < workspace->cols;) {
51                 Container *con = workspace->table[cols][row];
52                 printf("width_factor[%d][%d] = %f, colspan = %d\n", cols, row, con->width_factor, con->colspan);
53                 if (con->width_factor == 0)
54                         unoccupied -= workspace->rect.width * default_factor * con->colspan;
55                 cols += con->colspan;
56         }
57
58         assert(unoccupied != 0);
59
60         printf("unoccupied space: %d\n", unoccupied);
61         return unoccupied;
62 }
63
64 /* See get_unoccupied_x() */
65 int get_unoccupied_y(Workspace *workspace, int col) {
66         int unoccupied = workspace->rect.height;
67         float default_factor = ((float)workspace->rect.height / workspace->rows) / workspace->rect.height;
68
69         printf("get_unoccupied_y(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
70
71         for (int rows = 0; rows < workspace->rows;) {
72                 Container *con = workspace->table[col][rows];
73                 printf("height_factor[%d][%d] = %f, rowspan %d\n", col, rows, con->height_factor, con->rowspan);
74                 if (con->height_factor == 0)
75                         unoccupied -= workspace->rect.height * default_factor * con->rowspan;
76                 rows += con->rowspan;
77         }
78
79         assert(unoccupied != 0);
80
81         printf("unoccupied space: %d\n", unoccupied);
82         return unoccupied;
83 }
84
85 /*
86  * Redecorates the given client correctly by checking if it’s in a stacking container and
87  * re-rendering the stack window or just calling decorate_window if it’s not in a stacking
88  * container.
89  *
90  */
91 void redecorate_window(xcb_connection_t *conn, Client *client) {
92         if (client->container->mode == MODE_STACK) {
93                 render_container(conn, client->container);
94                 xcb_flush(conn);
95         } else decorate_window(conn, client, client->frame, client->titlegc, 0);
96 }
97
98 /*
99  * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
100  * When in stacking mode, the window decorations are drawn onto an own window.
101  *
102  */
103 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
104         i3Font *font = load_font(conn, config.font);
105         uint32_t background_color,
106                  text_color,
107                  border_color;
108
109         /* Clients without a container (docks) won’t get decorated */
110         if (client->container == NULL)
111                 return;
112
113         if (client->container->currently_focused == client) {
114                 /* Distinguish if the window is currently focused… */
115                 if (CUR_CELL->currently_focused == client)
116                         background_color = get_colorpixel(conn, "#285577");
117                 /* …or if it is the focused window in a not focused container */
118                 else background_color = get_colorpixel(conn, "#555555");
119
120                 text_color = get_colorpixel(conn, "#ffffff");
121                 border_color = get_colorpixel(conn, "#4c7899");
122         } else {
123                 background_color = get_colorpixel(conn, "#222222");
124                 text_color = get_colorpixel(conn, "#888888");
125                 border_color = get_colorpixel(conn, "#333333");
126         }
127
128         /* Our plan is the following:
129            - Draw a rect around the whole client in background_color
130            - Draw two lines in a lighter color
131            - Draw the window’s title
132          */
133
134         /* Draw a rectangle in background color around the window */
135         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
136
137         /* We need to use the container’s width because it is the more recent value - when
138            in stacking mode, clients get reconfigured only on demand (the not active client
139            is not reconfigured), so the client’s rect.width would be wrong */
140         xcb_rectangle_t rect = {0, offset, client->container->width, offset + client->rect.height};
141         xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
142
143         /* Draw the lines */
144         xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
145         xcb_draw_line(conn, drawable, gc, border_color, 2, offset + font->height + 3,
146                       2 + client->rect.width, offset + font->height + 3);
147
148         /* Draw the font */
149         uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
150         uint32_t values[] = { text_color, background_color, font->id };
151         xcb_change_gc(conn, gc, mask, values);
152
153         /* TODO: utf8? */
154         char *label;
155         asprintf(&label, "%.*s", client->name_len, client->name);
156         xcb_image_text_8(conn, strlen(label), drawable, gc, 3 /* X */, offset + font->height /* Y = baseline of font */, label);
157         free(label);
158 }
159
160 /*
161  * Pushes the client’s x and y coordinates to X11
162  *
163  */
164 static void reposition_client(xcb_connection_t *conn, Client *client) {
165         printf("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
166         /* Note: We can use a pointer to client->x like an array of uint32_ts
167            because it is followed by client->y by definition */
168         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
169 }
170
171 /*
172  * Pushes the client’s width/height to X11 and resizes the child window
173  *
174  */
175 static void resize_client(xcb_connection_t *conn, Client *client) {
176         i3Font *font = load_font(conn, config.font);
177
178         printf("resizing client to %d x %d\n", client->rect.width, client->rect.height);
179         xcb_configure_window(conn, client->frame,
180                         XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
181                         &(client->rect.width));
182
183         /* Adjust the position of the child inside its frame.
184          * The coordinates of the child are relative to its frame, we
185          * add a border of 2 pixel to each value */
186         uint32_t mask = XCB_CONFIG_WINDOW_X |
187                         XCB_CONFIG_WINDOW_Y |
188                         XCB_CONFIG_WINDOW_WIDTH |
189                         XCB_CONFIG_WINDOW_HEIGHT;
190         Rect *rect = &(client->child_rect);
191         switch (client->container->mode) {
192                 case MODE_STACK:
193                         rect->x = 2;
194                         rect->y = 0;
195                         rect->width = client->rect.width - (2 + 2);
196                         rect->height = client->rect.height - 2;
197                         break;
198                 default:
199                         if (client->titlebar_position == TITLEBAR_OFF) {
200                                 rect->x = 0;
201                                 rect->y = 0;
202                                 rect->width = client->rect.width;
203                                 rect->height = client->rect.height;
204                         } else {
205                                 rect->x = 2;
206                                 rect->y = font->height + 2 + 2;
207                                 rect->width = client->rect.width - (2 + 2);
208                                 rect->height = client->rect.height - ((font->height + 2 + 2) + 2);
209                         }
210                         break;
211         }
212
213         printf("child will be at %dx%d with size %dx%d\n", rect->x, rect->y, rect->width, rect->height);
214
215         xcb_configure_window(conn, client->child, mask, &(rect->x));
216
217         /* After configuring a child window we need to fake a configure_notify_event according
218            to ICCCM 4.2.3. This seems rather broken, especially since X sends exactly the same
219            configure_notify_event automatically according to xtrace. Anyone knows details? */
220         xcb_configure_notify_event_t event;
221
222         event.event = client->child;
223         event.window = client->child;
224         event.response_type = XCB_CONFIGURE_NOTIFY;
225
226         event.x = rect->x;
227         event.y = rect->y;
228         event.width = rect->width;
229         event.height = rect->height;
230
231         event.border_width = 0;
232         event.above_sibling = XCB_NONE;
233         event.override_redirect = false;
234
235         xcb_send_event(conn, false, client->child, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)&event);
236 }
237
238 /*
239  * Renders the given container. Is called by render_layout() or individually (for example
240  * when focus changes in a stacking container)
241  *
242  */
243 void render_container(xcb_connection_t *conn, Container *container) {
244         Client *client;
245         int num_clients = 0, current_client = 0;
246
247         if (container->currently_focused == NULL)
248                 return;
249
250         CIRCLEQ_FOREACH(client, &(container->clients), clients)
251                 num_clients++;
252
253         if (container->mode == MODE_DEFAULT) {
254                 printf("got %d clients in this default container.\n", num_clients);
255                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
256                         /* Check if we changed client->x or client->y by updating it.
257                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
258                         if (client->force_reconfigure |
259                             update_if_necessary(&(client->rect.x), container->x) |
260                             update_if_necessary(&(client->rect.y), container->y +
261                                         (container->height / num_clients) * current_client))
262                                 reposition_client(conn, client);
263
264                         /* TODO: vertical default layout */
265                         if (client->force_reconfigure |
266                             update_if_necessary(&(client->rect.width), container->width) |
267                             update_if_necessary(&(client->rect.height), container->height / num_clients))
268                                 resize_client(conn, client);
269
270                         client->force_reconfigure = false;
271
272                         current_client++;
273                 }
274         } else {
275                 i3Font *font = load_font(conn, config.font);
276                 int decoration_height = (font->height + 2 + 2);
277                 struct Stack_Window *stack_win = &(container->stack_win);
278
279                 /* Check if we need to remap our stack title window, it gets unmapped when the container
280                    is empty in src/handlers.c:unmap_notify() */
281                 if (stack_win->rect.height == 0)
282                         xcb_map_window(conn, stack_win->window);
283
284                 /* Check if we need to reconfigure our stack title window */
285                 if (update_if_necessary(&(stack_win->rect.x), container->x) |
286                     update_if_necessary(&(stack_win->rect.y), container->y) |
287                     update_if_necessary(&(stack_win->rect.width), container->width) |
288                     update_if_necessary(&(stack_win->rect.height), decoration_height * num_clients)) {
289
290                         uint32_t values[] = { stack_win->rect.x, stack_win->rect.y,
291                                               stack_win->rect.width, stack_win->rect.height,
292                                               XCB_STACK_MODE_ABOVE };
293
294                         xcb_configure_window(conn, stack_win->window,
295                                              XCB_CONFIG_WINDOW_X     | XCB_CONFIG_WINDOW_Y      |
296                                              XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
297                                              XCB_CONFIG_WINDOW_STACK_MODE,
298                                              values);
299                 }
300
301                 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
302                 client = container->currently_focused;
303
304                 if (container->workspace->fullscreen_client == NULL) {
305                         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
306                         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
307                 }
308
309                 /* Render the decorations of all clients */
310                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
311                         /* Check if we changed client->x or client->y by updating it.
312                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
313                         if (client->force_reconfigure |
314                             update_if_necessary(&(client->rect.x), container->x) |
315                             update_if_necessary(&(client->rect.y), container->y + (decoration_height * num_clients)))
316                                 reposition_client(conn, client);
317
318                         if (client->force_reconfigure |
319                             update_if_necessary(&(client->rect.width), container->width) |
320                             update_if_necessary(&(client->rect.height), container->height - (decoration_height * num_clients)))
321                                 resize_client(conn, client);
322
323                         client->force_reconfigure = false;
324
325                         decorate_window(conn, client, stack_win->window, stack_win->gc,
326                                         current_client++ * decoration_height);
327                 }
328         }
329 }
330
331 static void render_bars(xcb_connection_t *conn, Workspace *r_ws, int width, int *height) {
332         Client *client;
333         SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
334                 if (client->force_reconfigure |
335                     update_if_necessary(&(client->rect.x), 0) |
336                     update_if_necessary(&(client->rect.y), *height))
337                         reposition_client(conn, client);
338
339                 if (client->force_reconfigure |
340                     update_if_necessary(&(client->rect.width), width) |
341                     update_if_necessary(&(client->rect.height), client->desired_height))
342                         resize_client(conn, client);
343
344                 client->force_reconfigure = false;
345                 *height += client->desired_height;
346         }
347 }
348
349 static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int width, int height) {
350         printf("Rendering internal bar\n");
351         i3Font *font = load_font(conn, config.font);
352         i3Screen *screen = r_ws->screen;
353         enum { SET_NORMAL = 0, SET_FOCUSED = 1 };
354         uint32_t background_color[2],
355                  text_color[2],
356                  border_color[2],
357                  black;
358         char label[3];
359
360         black = get_colorpixel(conn, "#000000");
361
362         background_color[SET_NORMAL] = get_colorpixel(conn, "#222222");
363         text_color[SET_NORMAL] = get_colorpixel(conn, "#888888");
364         border_color[SET_NORMAL] = get_colorpixel(conn, "#333333");
365
366         background_color[SET_FOCUSED] = get_colorpixel(conn, "#285577");
367         text_color[SET_FOCUSED] = get_colorpixel(conn, "#ffffff");
368         border_color[SET_FOCUSED] = get_colorpixel(conn, "#4c7899");
369
370         /* Fill the whole bar in black */
371         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black);
372         xcb_rectangle_t rect = {0, 0, width, height};
373         xcb_poly_fill_rectangle(conn, screen->bar, screen->bargc, 1, &rect);
374
375         /* Set font */
376         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FONT, font->id);
377
378         int drawn = 0;
379         for (int c = 0; c < 10; c++) {
380                 if (workspaces[c].screen == screen) {
381                         int set = (screen->current_workspace == c ? SET_FOCUSED : SET_NORMAL);
382
383                         xcb_draw_rect(conn, screen->bar, screen->bargc, border_color[set],
384                                       drawn * height, 1, height - 2, height - 2);
385                         xcb_draw_rect(conn, screen->bar, screen->bargc, background_color[set],
386                                       drawn * height + 1, 2, height - 4, height - 4);
387
388                         snprintf(label, sizeof(label), "%d", c+1);
389                         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, text_color[set]);
390                         xcb_change_gc_single(conn, screen->bargc, XCB_GC_BACKGROUND, background_color[set]);
391                         xcb_image_text_8(conn, strlen(label), screen->bar, screen->bargc, drawn * height + 5 /* X */,
392                                                         font->height + 1 /* Y = baseline of font */, label);
393                         drawn++;
394                 }
395         }
396
397         printf("done rendering internal\n");
398 }
399
400 void render_layout(xcb_connection_t *conn) {
401         i3Screen *screen;
402         i3Font *font = load_font(conn, config.font);
403
404         TAILQ_FOREACH(screen, virtual_screens, screens) {
405                 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
406                 Workspace *r_ws = &(workspaces[screen->current_workspace]);
407
408                 printf("Rendering screen %d\n", screen->num);
409                 if (r_ws->fullscreen_client != NULL)
410                         /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
411                         continue;
412
413                 int width = r_ws->rect.width;
414                 int height = r_ws->rect.height;
415
416                 /* Reserve space for dock clients */
417                 Client *client;
418                 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
419                         height -= client->desired_height;
420
421                 /* Space for the internal bar */
422                 height -= (font->height + 6);
423
424                 printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
425
426                 int xoffset[r_ws->rows];
427                 int yoffset[r_ws->cols];
428                 /* Initialize offsets */
429                 for (int cols = 0; cols < r_ws->cols; cols++)
430                         yoffset[cols] = r_ws->rect.y;
431                 for (int rows = 0; rows < r_ws->rows; rows++)
432                         xoffset[rows] = r_ws->rect.x;
433
434                 /* Go through the whole table and render what’s necessary */
435                 for (int cols = 0; cols < r_ws->cols; cols++)
436                         for (int rows = 0; rows < r_ws->rows; rows++) {
437                                 Container *container = r_ws->table[cols][rows];
438                                 printf("\n========\ncontainer has %d colspan, %d rowspan\n",
439                                                 container->colspan, container->rowspan);
440                                 printf("container at %d, %d\n", xoffset[rows], yoffset[cols]);
441                                 /* Update position of the container */
442                                 container->row = rows;
443                                 container->col = cols;
444                                 container->x = xoffset[rows];
445                                 container->y = yoffset[cols];
446
447                                 if (container->width_factor == 0)
448                                         container->width = (width / r_ws->cols);
449                                 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
450                                 container->width *= container->colspan;
451
452                                 if (container->height_factor == 0)
453                                         container->height = (height / r_ws->rows);
454                                 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
455                                 container->height *= container->rowspan;
456
457                                 /* Render the container if it is not empty */
458                                 render_container(conn, container);
459
460                                 xoffset[rows] += container->width;
461                                 yoffset[cols] += container->height;
462                                 printf("==========\n");
463                         }
464
465                 render_bars(conn, r_ws, width, &height);
466                 render_internal_bar(conn, r_ws, width, 18);
467         }
468
469         xcb_flush(conn);
470 }