]> git.sur5r.net Git - i3/i3/blob - src/layout.c
Bugfix: When rendering, skip containers which are not yet initialized
[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 #include <math.h>
19
20 #include "config.h"
21 #include "i3.h"
22 #include "xcb.h"
23 #include "table.h"
24 #include "util.h"
25 #include "xinerama.h"
26 #include "layout.h"
27 #include "client.h"
28 #include "floating.h"
29 #include "handlers.h"
30 #include "workspace.h"
31
32 /*
33  * Updates *destination with new_value and returns true if it was changed or false
34  * if it was the same
35  *
36  */
37 static bool update_if_necessary(uint32_t *destination, const uint32_t new_value) {
38         uint32_t old_value = *destination;
39
40         return ((*destination = new_value) != old_value);
41 }
42
43 /*
44  * Gets the unoccupied space (= space which is available for windows which were resized by the user)
45  * for the given row. This is necessary to render both, customly resized windows and never touched
46  * windows correctly, meaning that the aspect ratio will be maintained when opening new windows.
47  *
48  */
49 int get_unoccupied_x(Workspace *workspace) {
50         int unoccupied = workspace->rect.width;
51         float default_factor = ((float)workspace->rect.width / workspace->cols) / workspace->rect.width;
52
53         LOG("get_unoccupied_x(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
54
55         for (int cols = 0; cols < workspace->cols; cols++) {
56                 LOG("width_factor[%d] = %f\n", cols, workspace->width_factor[cols]);
57
58                 if (workspace->width_factor[cols] == 0)
59                         unoccupied -= workspace->rect.width * default_factor;
60         }
61
62         LOG("unoccupied space: %d\n", unoccupied);
63         return unoccupied;
64 }
65
66 /* See get_unoccupied_x() */
67 int get_unoccupied_y(Workspace *workspace) {
68         int height = workspace->rect.height;
69         i3Font *font = load_font(global_conn, config.font);
70
71         /* Reserve space for dock clients */
72         Client *client;
73         SLIST_FOREACH(client, &(workspace->screen->dock_clients), dock_clients)
74                 height -= client->desired_height;
75
76         /* Space for the internal bar */
77         height -= (font->height + 6);
78
79         int unoccupied = height;
80         float default_factor = ((float)height / workspace->rows) / height;
81
82         LOG("get_unoccupied_y(), starting with %d, default_factor = %f\n", unoccupied, default_factor);
83
84         for (int rows = 0; rows < workspace->rows; rows++) {
85                 LOG("height_factor[%d] = %f\n", rows, workspace->height_factor[rows]);
86                 if (workspace->height_factor[rows] == 0)
87                         unoccupied -= height * default_factor;
88         }
89
90         LOG("unoccupied space: %d\n", unoccupied);
91         return unoccupied;
92 }
93
94 /*
95  * Redecorates the given client correctly by checking if it’s in a stacking container and
96  * re-rendering the stack window or just calling decorate_window if it’s not in a stacking
97  * container.
98  *
99  */
100 void redecorate_window(xcb_connection_t *conn, Client *client) {
101         if (client->container != NULL &&
102             (client->container->mode == MODE_STACK ||
103              client->container->mode == MODE_TABBED)) {
104                 render_container(conn, client->container);
105                 /* We clear the frame to generate exposure events, because the color used
106                    in drawing may be different */
107                 xcb_clear_area(conn, true, client->frame, 0, 0, client->rect.width, client->rect.height);
108         } else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
109         xcb_flush(conn);
110 }
111
112 /*
113  * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
114  * When in stacking mode, the window decorations are drawn onto an own window.
115  *
116  */
117 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable,
118                      xcb_gcontext_t gc, int offset_x, int offset_y) {
119         i3Font *font = load_font(conn, config.font);
120         int decoration_height = font->height + 2 + 2;
121         struct Colortriple *color;
122         Client *last_focused;
123
124         /* Clients without a container (docks) won’t get decorated */
125         if (client->dock)
126                 return;
127
128         last_focused = SLIST_FIRST(&(client->workspace->focus_stack));
129         /* Is the window urgent? */
130         if (client->urgent)
131                 color = &(config.client.urgent);
132         else {
133                 if (client_is_floating(client)) {
134                         if (last_focused == client)
135                                 color = &(config.client.focused);
136                         else color = &(config.client.unfocused);
137                 } else {
138                         if (client->container->currently_focused == client) {
139                                 /* Distinguish if the window is currently focused… */
140                                 if (last_focused == client && c_ws == client->workspace)
141                                         color = &(config.client.focused);
142                                 /* …or if it is the focused window in a not focused container */
143                                 else color = &(config.client.focused_inactive);
144                         } else color = &(config.client.unfocused);
145                 }
146         }
147
148         /* Our plan is the following:
149            - Draw a rect around the whole client in color->background
150            - Draw two lines in a lighter color
151            - Draw the window’s title
152          */
153
154         /* Draw a rectangle in background color around the window */
155         if (client->borderless && (client->container == NULL ||
156             (client->container->mode != MODE_STACK &&
157              client->container->mode != MODE_TABBED)))
158                 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
159         else xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, color->background);
160
161         /* In stacking mode, we only render the rect for this specific decoration */
162         if (client->container != NULL && (client->container->mode == MODE_STACK || client->container->mode == MODE_TABBED)) {
163                 /* We need to use the container’s width because it is the more recent value - when
164                    in stacking mode, clients get reconfigured only on demand (the not active client
165                    is not reconfigured), so the client’s rect.width would be wrong */
166                 xcb_rectangle_t rect = {offset_x, offset_y,
167                                         offset_x + client->container->width,
168                                         offset_y + decoration_height };
169                 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
170         } else {
171                 xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
172                 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
173
174                 /* Draw the inner background to have a black frame around clients (such as mplayer)
175                    which cannot be resized exactly in our frames and therefore are centered */
176                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
177                 xcb_rectangle_t crect = {2, decoration_height,
178                                          client->rect.width - (2 + 2), client->rect.height - 2 - decoration_height};
179                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
180         }
181
182         if (client->titlebar_position != TITLEBAR_OFF) {
183                 /* Draw the lines */
184                 xcb_draw_line(conn, drawable, gc, color->border, offset_x, offset_y, offset_x + client->rect.width, offset_y);
185                 if ((client->container == NULL ||
186                     (client->container->mode != MODE_STACK &&
187                      client->container->mode != MODE_TABBED) ||
188                     CIRCLEQ_NEXT_OR_NULL(&(client->container->clients), client, clients) == NULL))
189                         xcb_draw_line(conn, drawable, gc, color->border,
190                                       offset_x + 2, /* x */
191                                       offset_y + font->height + 3, /* y */
192                                       offset_x + client->rect.width - 3, /* to_x */
193                                       offset_y + font->height + 3 /* to_y */);
194         }
195
196         /* If the client has a title, we draw it */
197         if (client->name != NULL) {
198                 /* Draw the font */
199                 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
200                 uint32_t values[] = { color->text, color->background, font->id };
201                 xcb_change_gc(conn, gc, mask, values);
202
203                 /* name_len == -1 means this is a legacy application which does not specify _NET_WM_NAME,
204                    and we don’t handle the old window name (COMPOUND_TEXT) but only _NET_WM_NAME, which
205                    is UTF-8 */
206                 if (client->name_len == -1)
207                         xcb_image_text_8(conn, strlen(client->name), drawable, gc, offset_x + 3 /* X */,
208                                          offset_y + font->height /* Y = baseline of font */, client->name);
209                 else
210                         xcb_image_text_16(conn, client->name_len, drawable, gc, offset_x + 3 /* X */,
211                                           offset_y + font->height /* Y = baseline of font */, (xcb_char2b_t*)client->name);
212         }
213 }
214
215 /*
216  * Pushes the client’s x and y coordinates to X11
217  *
218  */
219 void reposition_client(xcb_connection_t *conn, Client *client) {
220         i3Screen *screen;
221
222         LOG("frame 0x%08x needs to be pushed to %dx%d\n", client->frame, client->rect.x, client->rect.y);
223         /* Note: We can use a pointer to client->x like an array of uint32_ts
224            because it is followed by client->y by definition */
225         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
226
227         if (!client_is_floating(client))
228                 return;
229
230         /* If the client is floating, we need to check if we moved it to a different workspace */
231         if (client->workspace->screen == (screen = get_screen_containing(client->rect.x, client->rect.y)))
232                 return;
233
234         if (screen == NULL) {
235                 LOG("Boundary checking disabled, no screen found for (%d, %d)\n", client->rect.x, client->rect.y);
236                 return;
237         }
238
239         LOG("Client is on workspace %p with screen %p\n", client->workspace, client->workspace->screen);
240         LOG("but screen at %d, %d is %p\n", client->rect.x, client->rect.y, screen);
241         floating_assign_to_workspace(client, workspace_get(screen->current_workspace));
242 }
243
244 /*
245  * Pushes the client’s width/height to X11 and resizes the child window. This
246  * function also updates the client’s position, so if you work on tiling clients
247  * only, you can use this function instead of separate calls to reposition_client
248  * and resize_client to reduce flickering.
249  *
250  */
251 void resize_client(xcb_connection_t *conn, Client *client) {
252         i3Font *font = load_font(conn, config.font);
253
254         LOG("frame 0x%08x needs to be pushed to %dx%d\n", client->frame, client->rect.x, client->rect.y);
255         LOG("resizing client 0x%08x to %d x %d\n", client->frame, client->rect.width, client->rect.height);
256         xcb_configure_window(conn, client->frame,
257                         XCB_CONFIG_WINDOW_X |
258                         XCB_CONFIG_WINDOW_Y |
259                         XCB_CONFIG_WINDOW_WIDTH |
260                         XCB_CONFIG_WINDOW_HEIGHT,
261                         &(client->rect.x));
262
263         /* Adjust the position of the child inside its frame.
264          * The coordinates of the child are relative to its frame, we
265          * add a border of 2 pixel to each value */
266         uint32_t mask = XCB_CONFIG_WINDOW_X |
267                         XCB_CONFIG_WINDOW_Y |
268                         XCB_CONFIG_WINDOW_WIDTH |
269                         XCB_CONFIG_WINDOW_HEIGHT;
270         Rect *rect = &(client->child_rect);
271         switch ((client->container != NULL ? client->container->mode : MODE_DEFAULT)) {
272                 case MODE_STACK:
273                 case MODE_TABBED:
274                         rect->x = 2;
275                         rect->y = 0;
276                         rect->width = client->rect.width - (2 + 2);
277                         rect->height = client->rect.height - 2;
278                         break;
279                 default:
280                         if (client->titlebar_position == TITLEBAR_OFF && client->borderless) {
281                                 rect->x = 0;
282                                 rect->y = 0;
283                                 rect->width = client->rect.width;
284                                 rect->height = client->rect.height;
285                         } else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless) {
286                                 rect->x = 1;
287                                 rect->y = 1;
288                                 rect->width = client->rect.width - 1 - 1;
289                                 rect->height = client->rect.height - 1 - 1;
290                         } else {
291                                 rect->x = 2;
292                                 rect->y = font->height + 2 + 2;
293                                 rect->width = client->rect.width - (2 + 2);
294                                 rect->height = client->rect.height - ((font->height + 2 + 2) + 2);
295                         }
296                         break;
297         }
298
299         rect->width -= (2 * client->border_width);
300         rect->height -= (2 * client->border_width);
301
302         /* Obey the ratio, if any */
303         if (client->proportional_height != 0 &&
304             client->proportional_width != 0) {
305                 LOG("proportional height = %d, width = %d\n", client->proportional_height, client->proportional_width);
306                 double new_height = rect->height + 1;
307                 int new_width = rect->width;
308
309                 while (new_height > rect->height) {
310                         new_height = ((double)client->proportional_height / client->proportional_width) * new_width;
311
312                         if (new_height > rect->height)
313                                 new_width--;
314                 }
315                 /* Center the window */
316                 rect->y += ceil(rect->height / 2) - floor(new_height / 2);
317                 rect->x += ceil(rect->width / 2) - floor(new_width / 2);
318
319                 rect->height = new_height;
320                 rect->width = new_width;
321                 LOG("new_height = %f, new_width = %d\n", new_height, new_width);
322         }
323
324         if (client->height_increment > 1) {
325                 int old_height = rect->height;
326                 rect->height -= (rect->height - client->base_height) % client->height_increment;
327                 LOG("Lost %d pixel due to client's height_increment (%d px)\n",
328                     old_height - rect->height, client->height_increment);
329         }
330
331         if (client->width_increment > 1) {
332                 int old_width = rect->width;
333                 rect->width -= (rect->width - client->base_width) % client->width_increment;
334                 LOG("Lost %d pixel due to client's width_increment (%d px)\n",
335                     old_width - rect->width, client->width_increment);
336         }
337
338         LOG("child will be at %dx%d with size %dx%d\n", rect->x, rect->y, rect->width, rect->height);
339
340         xcb_configure_window(conn, client->child, mask, &(rect->x));
341
342         /* After configuring a child window we need to fake a configure_notify_event (see ICCCM 4.2.3).
343          * This is necessary to inform the client of its position relative to the root window,
344          * not relative to its frame (as done in the configure_notify_event by the x server). */
345         fake_absolute_configure_notify(conn, client);
346
347         /* Force redrawing after resizing the window because any now lost
348          * pixels could contain old garbage. */
349         xcb_expose_event_t generated;
350         generated.window = client->frame;
351         generated.count = 0;
352         handle_expose_event(NULL, conn, &generated);
353 }
354
355 /*
356  * Renders the given container. Is called by render_layout() or individually (for example
357  * when focus changes in a stacking container)
358  *
359  */
360 void render_container(xcb_connection_t *conn, Container *container) {
361         Client *client;
362         int num_clients = 0, current_client = 0;
363
364         CIRCLEQ_FOREACH(client, &(container->clients), clients)
365                 num_clients++;
366
367         if (container->mode == MODE_DEFAULT) {
368                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
369                         /* If the client is in fullscreen mode, it does not get reconfigured */
370                         if (container->workspace->fullscreen_client == client) {
371                                 current_client++;
372                                 continue;
373                         }
374
375                         /* Check if we changed client->x or client->y by updating it.
376                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
377                         if (client->force_reconfigure |
378                             update_if_necessary(&(client->rect.x), container->x) |
379                             update_if_necessary(&(client->rect.y), container->y +
380                                         (container->height / num_clients) * current_client) |
381                             update_if_necessary(&(client->rect.width), container->width) |
382                             update_if_necessary(&(client->rect.height), container->height / num_clients))
383                                 resize_client(conn, client);
384
385                         /* TODO: vertical default layout */
386
387                         client->force_reconfigure = false;
388
389                         current_client++;
390                 }
391         } else {
392                 i3Font *font = load_font(conn, config.font);
393                 int decoration_height = (font->height + 2 + 2);
394                 struct Stack_Window *stack_win = &(container->stack_win);
395                 /* The size for each tab (width), necessary as a separate variable
396                  * because num_clients gets fixed to 1 in tabbed mode. */
397                 int size_each = (num_clients == 0 ? container->width : container->width / num_clients);
398                 int stack_lines = num_clients;
399
400                 /* Check if we need to remap our stack title window, it gets unmapped when the container
401                    is empty in src/handlers.c:unmap_notify() */
402                 if (stack_win->rect.height == 0 && num_clients > 0) {
403                         LOG("remapping stack win\n");
404                         xcb_map_window(conn, stack_win->window);
405                 } else LOG("not remapping stackwin, height = %d, num_clients = %d\n",
406                                 stack_win->rect.height, num_clients);
407
408                 if (container->mode == MODE_TABBED) {
409                         /* By setting num_clients to 1 we force that the stack window will be only one line
410                          * high. The rest of the code is useful in both cases. */
411                         LOG("tabbed mode, setting num_clients = 1\n");
412                         if (stack_lines > 1)
413                                 stack_lines = 1;
414                 }
415
416                 if (container->stack_limit == STACK_LIMIT_COLS) {
417                         stack_lines = ceil((float)num_clients / container->stack_limit_value);
418                 } else if (container->stack_limit == STACK_LIMIT_ROWS) {
419                         stack_lines = min(num_clients, container->stack_limit_value);
420                 }
421
422                 /* Check if we need to reconfigure our stack title window */
423                 if (update_if_necessary(&(stack_win->rect.x), container->x) |
424                     update_if_necessary(&(stack_win->rect.y), container->y) |
425                     update_if_necessary(&(stack_win->rect.width), container->width) |
426                     update_if_necessary(&(stack_win->rect.height), decoration_height * stack_lines)) {
427
428                         /* Configuration can happen in two slightly different ways:
429
430                            If there is no client in fullscreen mode, 5 parameters are passed
431                            (x, y, width, height, stack mode is set to above which means top-most position).
432
433                            If there is a fullscreen client, the fourth parameter is set to to the
434                            fullscreen window as sibling and the stack mode is set to below, which means
435                            that the stack_window will be placed just below the sibling, that is, under
436                            the fullscreen window.
437                          */
438                         uint32_t values[] = { stack_win->rect.x, stack_win->rect.y,
439                                               stack_win->rect.width, stack_win->rect.height,
440                                               XCB_STACK_MODE_ABOVE, XCB_STACK_MODE_BELOW };
441                         uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
442                                         XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
443                                         XCB_CONFIG_WINDOW_STACK_MODE;
444
445                         /* Raise the stack window, but keep it below the first floating client
446                          * and below the fullscreen client (if any) */
447                         Client *first_floating = TAILQ_FIRST(&(container->workspace->floating_clients));
448                         if (first_floating != TAILQ_END(&(container->workspace->floating_clients))) {
449                                 mask |= XCB_CONFIG_WINDOW_SIBLING;
450                                 values[4] = first_floating->frame;
451                         } else if (container->workspace->fullscreen_client != NULL) {
452                                 mask |= XCB_CONFIG_WINDOW_SIBLING;
453                                 values[4] = container->workspace->fullscreen_client->frame;
454                         }
455
456                         xcb_configure_window(conn, stack_win->window, mask, values);
457                 }
458
459                 /* Prepare the pixmap for usage */
460                 cached_pixmap_prepare(conn, &(stack_win->pixmap));
461
462                 int current_row = 0, current_col = 0;
463                 int wrap = 0;
464
465                 if (container->stack_limit == STACK_LIMIT_COLS) {
466                         /* wrap stores the number of rows after which we will
467                          * wrap to a new column. */
468                         wrap = ceil((float)num_clients / container->stack_limit_value);
469                 } else if (container->stack_limit == STACK_LIMIT_ROWS) {
470                         /* When limiting rows, the wrap variable serves a
471                          * slightly different purpose: it holds the number of
472                          * pixels which each client will get. This is constant
473                          * during the following loop, so it saves us some
474                          * divisions and ceil()ing. */
475                         wrap = (stack_win->rect.width / ceil((float)num_clients / container->stack_limit_value));
476                 }
477
478                 /* Render the decorations of all clients */
479                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
480                         /* If the client is in fullscreen mode, it does not get reconfigured */
481                         if (container->workspace->fullscreen_client == client) {
482                                 current_client++;
483                                 continue;
484                         }
485
486                         /* Check if we changed client->x or client->y by updating it.
487                          * Note the bitwise OR instead of logical OR to force evaluation of all statements */
488                         if (client->force_reconfigure |
489                             update_if_necessary(&(client->rect.x), container->x) |
490                             update_if_necessary(&(client->rect.y), container->y + (decoration_height * stack_lines)) |
491                             update_if_necessary(&(client->rect.width), container->width) |
492                             update_if_necessary(&(client->rect.height), container->height - (decoration_height * stack_lines)))
493                                 resize_client(conn, client);
494
495                         client->force_reconfigure = false;
496
497                         int offset_x = 0;
498                         int offset_y = 0;
499                         if (container->mode == MODE_STACK) {
500                                 if (container->stack_limit == STACK_LIMIT_COLS) {
501                                         offset_x = current_col * (stack_win->rect.width / container->stack_limit_value);
502                                         offset_y = current_row * decoration_height;
503                                         current_row++;
504                                         if ((current_row % wrap) == 0) {
505                                                 current_col++;
506                                                 current_row = 0;
507                                         }
508                                 } else if (container->stack_limit == STACK_LIMIT_ROWS) {
509                                         offset_x = current_col * wrap;
510                                         offset_y = current_row * decoration_height;
511                                         current_row++;
512                                         if ((current_row % container->stack_limit_value) == 0) {
513                                                 current_col++;
514                                                 current_row = 0;
515                                         }
516                                 } else {
517                                         offset_y = current_client * decoration_height;
518                                 }
519                                 current_client++;
520                         } else if (container->mode == MODE_TABBED)
521                                 offset_x = current_client++ * size_each;
522                         decorate_window(conn, client, stack_win->pixmap.id, stack_win->pixmap.gc,
523                                         offset_x, offset_y);
524                 }
525
526                 /* Check if we need to fill one column because of an uneven
527                  * amount of windows */
528                 if (container->mode == MODE_STACK) {
529                         if (container->stack_limit == STACK_LIMIT_COLS && (current_col % 2) != 0) {
530                                 xcb_change_gc_single(conn, stack_win->pixmap.gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
531
532                                 int offset_x = current_col * (stack_win->rect.width / container->stack_limit_value);
533                                 int offset_y = current_row * decoration_height;
534                                 xcb_rectangle_t rect = {offset_x, offset_y,
535                                                         offset_x + container->width,
536                                                         offset_y + decoration_height };
537                                 xcb_poly_fill_rectangle(conn, stack_win->pixmap.id, stack_win->pixmap.gc, 1, &rect);
538                         } else if (container->stack_limit == STACK_LIMIT_ROWS && (current_row % 2) != 0) {
539                                 xcb_change_gc_single(conn, stack_win->pixmap.gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
540
541                                 int offset_x = current_col * wrap;
542                                 int offset_y = current_row * decoration_height;
543                                 xcb_rectangle_t rect = {offset_x, offset_y,
544                                                         offset_x + container->width,
545                                                         offset_y + decoration_height };
546                                 xcb_poly_fill_rectangle(conn, stack_win->pixmap.id, stack_win->pixmap.gc, 1, &rect);
547                         }
548                 }
549
550                 xcb_copy_area(conn, stack_win->pixmap.id, stack_win->window, stack_win->pixmap.gc,
551                               0, 0, 0, 0, stack_win->rect.width, stack_win->rect.height);
552         }
553 }
554
555 static void render_bars(xcb_connection_t *conn, Workspace *r_ws, int width, int *height) {
556         Client *client;
557         SLIST_FOREACH(client, &(r_ws->screen->dock_clients), dock_clients) {
558                 LOG("client is at %d, should be at %d\n", client->rect.y, *height);
559                 if (client->force_reconfigure |
560                     update_if_necessary(&(client->rect.x), r_ws->rect.x) |
561                     update_if_necessary(&(client->rect.y), *height))
562                         reposition_client(conn, client);
563
564                 if (client->force_reconfigure |
565                     update_if_necessary(&(client->rect.width), width) |
566                     update_if_necessary(&(client->rect.height), client->desired_height))
567                         resize_client(conn, client);
568
569                 client->force_reconfigure = false;
570                 LOG("desired_height = %d\n", client->desired_height);
571                 *height += client->desired_height;
572         }
573 }
574
575 static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int width, int height) {
576         i3Font *font = load_font(conn, config.font);
577         i3Screen *screen = r_ws->screen;
578         enum { SET_NORMAL = 0, SET_FOCUSED = 1 };
579
580         /* Fill the whole bar in black */
581         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
582         xcb_rectangle_t rect = {0, 0, width, height};
583         xcb_poly_fill_rectangle(conn, screen->bar, screen->bargc, 1, &rect);
584
585         /* Set font */
586         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FONT, font->id);
587
588         int drawn = 0;
589         for (int c = 0; c < num_workspaces; c++) {
590                 if (workspaces[c].screen != screen)
591                         continue;
592
593                 struct Colortriple *color;
594                 Workspace *ws = &workspaces[c];
595
596                 if (screen->current_workspace == c)
597                         color = &(config.bar.focused);
598                 else if (ws->urgent)
599                         color = &(config.bar.urgent);
600                 else color = &(config.bar.unfocused);
601
602                 /* Draw the outer rect */
603                 xcb_draw_rect(conn, screen->bar, screen->bargc, color->border,
604                               drawn,              /* x */
605                               1,                  /* y */
606                               ws->text_width + 5 + 5, /* width = text width + 5 px left + 5px right */
607                               height - 2          /* height = max. height - 1 px upper and 1 px bottom border */);
608
609                 /* Draw the background of this rect */
610                 xcb_draw_rect(conn, screen->bar, screen->bargc, color->background,
611                               drawn + 1,
612                               2,
613                               ws->text_width + 4 + 4,
614                               height - 4);
615
616                 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, color->text);
617                 xcb_change_gc_single(conn, screen->bargc, XCB_GC_BACKGROUND, color->background);
618                 xcb_image_text_16(conn, ws->name_len, screen->bar, screen->bargc, drawn + 5 /* X */,
619                                   font->height + 1 /* Y = baseline of font */,
620                                   (xcb_char2b_t*)ws->name);
621                 drawn += ws->text_width + 12;
622         }
623 }
624
625 /*
626  * Modifies the event mask of all clients on the given workspace to either ignore or to handle
627  * enter notifies. It is handy to ignore notifies because they will be sent when a window is mapped
628  * under the cursor, thus when the user didn’t enter the window actively at all.
629  *
630  */
631 void ignore_enter_notify_forall(xcb_connection_t *conn, Workspace *workspace, bool ignore_enter_notify) {
632         Client *client;
633         uint32_t values[1];
634
635         FOR_TABLE(workspace) {
636                 if (workspace->table[cols][rows] == NULL)
637                         continue;
638
639                 CIRCLEQ_FOREACH(client, &(workspace->table[cols][rows]->clients), clients) {
640                         /* Change event mask for the decorations */
641                         values[0] = FRAME_EVENT_MASK;
642                         if (ignore_enter_notify)
643                                 values[0] &= ~(XCB_EVENT_MASK_ENTER_WINDOW);
644                         xcb_change_window_attributes(conn, client->frame, XCB_CW_EVENT_MASK, values);
645
646                         /* Change event mask for the child itself */
647                         values[0] = CHILD_EVENT_MASK;
648                         if (ignore_enter_notify)
649                                 values[0] &= ~(XCB_EVENT_MASK_ENTER_WINDOW);
650                         xcb_change_window_attributes(conn, client->child, XCB_CW_EVENT_MASK, values);
651                 }
652         }
653 }
654
655 /*
656  * Renders the given workspace on the given screen
657  *
658  */
659 void render_workspace(xcb_connection_t *conn, i3Screen *screen, Workspace *r_ws) {
660         i3Font *font = load_font(conn, config.font);
661         int width = r_ws->rect.width;
662         int height = r_ws->rect.height;
663
664         /* Reserve space for dock clients */
665         Client *client;
666         SLIST_FOREACH(client, &(screen->dock_clients), dock_clients)
667                 height -= client->desired_height;
668
669         /* Space for the internal bar */
670         height -= (font->height + 6);
671
672         int xoffset[r_ws->rows];
673         int yoffset[r_ws->cols];
674         /* Initialize offsets */
675         for (int cols = 0; cols < r_ws->cols; cols++)
676                 yoffset[cols] = r_ws->rect.y;
677         for (int rows = 0; rows < r_ws->rows; rows++)
678                 xoffset[rows] = r_ws->rect.x;
679
680         ignore_enter_notify_forall(conn, r_ws, true);
681
682         /* Go through the whole table and render what’s necessary */
683         FOR_TABLE(r_ws) {
684                 Container *container = r_ws->table[cols][rows];
685                 if (container == NULL)
686                         continue;
687                 int single_width = -1, single_height = -1;
688                 /* Update position of the container */
689                 container->row = rows;
690                 container->col = cols;
691                 container->x = xoffset[rows];
692                 container->y = yoffset[cols];
693                 container->width = 0;
694
695                 for (int c = 0; c < container->colspan; c++) {
696                         if (r_ws->width_factor[cols+c] == 0)
697                                 container->width += (width / r_ws->cols);
698                         else container->width += get_unoccupied_x(r_ws) * r_ws->width_factor[cols+c];
699
700                         if (single_width == -1)
701                                 single_width = container->width;
702                 }
703
704                 LOG("height is %d\n", height);
705
706                 container->height = 0;
707
708                 for (int c = 0; c < container->rowspan; c++) {
709                         if (r_ws->height_factor[rows+c] == 0)
710                                 container->height += (height / r_ws->rows);
711                         else container->height += get_unoccupied_y(r_ws) * r_ws->height_factor[rows+c];
712
713                         if (single_height == -1)
714                                 single_height = container->height;
715                 }
716
717                 /* Render the container if it is not empty */
718                 render_container(conn, container);
719
720                 xoffset[rows] += single_width;
721                 yoffset[cols] += single_height;
722         }
723
724         ignore_enter_notify_forall(conn, r_ws, false);
725
726         render_bars(conn, r_ws, width, &height);
727         render_internal_bar(conn, r_ws, width, font->height + 6);
728 }
729
730 /*
731  * Renders the whole layout, that is: Go through each screen, each workspace, each container
732  * and render each client. This also renders the bars.
733  *
734  * If you don’t need to render *everything*, you should call render_container on the container
735  * you want to refresh.
736  *
737  */
738 void render_layout(xcb_connection_t *conn) {
739         i3Screen *screen;
740
741         if (virtual_screens == NULL)
742                 return;
743
744         TAILQ_FOREACH(screen, virtual_screens, screens)
745                 render_workspace(conn, screen, &(workspaces[screen->current_workspace]));
746
747         xcb_flush(conn);
748 }