]> git.sur5r.net Git - i3/i3/blob - src/layout.c
1d4d3ddb963fc2b460060ca3036258aaef9e3666
[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         LOG("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                 LOG("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         LOG("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         LOG("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                 LOG("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         LOG("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                 /* We clear the frame to generate exposure events, because the color used
95                    in drawing may be different */
96                 xcb_clear_area(conn, true, client->frame, 0, 0, client->rect.width, client->rect.height);
97         } else decorate_window(conn, client, client->frame, client->titlegc, 0);
98         xcb_flush(conn);
99 }
100
101 /*
102  * (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
103  * When in stacking mode, the window decorations are drawn onto an own window.
104  *
105  */
106 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
107         i3Font *font = load_font(conn, config.font);
108         int decoration_height = font->height + 2 + 2;
109         uint32_t background_color,
110                  text_color,
111                  border_color;
112
113         /* Clients without a container (docks) won’t get decorated */
114         if (client->container == NULL)
115                 return;
116
117         if (client->container->currently_focused == client) {
118                 /* Distinguish if the window is currently focused… */
119                 if (CUR_CELL->currently_focused == client)
120                         background_color = get_colorpixel(conn, "#285577");
121                 /* …or if it is the focused window in a not focused container */
122                 else background_color = get_colorpixel(conn, "#555555");
123
124                 text_color = get_colorpixel(conn, "#ffffff");
125                 border_color = get_colorpixel(conn, "#4c7899");
126         } else {
127                 background_color = get_colorpixel(conn, "#222222");
128                 text_color = get_colorpixel(conn, "#888888");
129                 border_color = get_colorpixel(conn, "#333333");
130         }
131
132         /* Our plan is the following:
133            - Draw a rect around the whole client in background_color
134            - Draw two lines in a lighter color
135            - Draw the window’s title
136          */
137
138         /* Draw a rectangle in background color around the window */
139         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
140
141         /* In stacking mode, we only render the rect for this specific decoration */
142         if (client->container->mode == MODE_STACK) {
143                 xcb_rectangle_t rect = {0, offset, client->container->width, offset + decoration_height };
144                 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
145         } else {
146                 /* We need to use the container’s width because it is the more recent value - when
147                    in stacking mode, clients get reconfigured only on demand (the not active client
148                    is not reconfigured), so the client’s rect.width would be wrong */
149                 xcb_rectangle_t rect = {0, 0, client->container->width, client->rect.height};
150                 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
151
152                 /* Draw the inner background to have a black frame around clients (such as mplayer)
153                    which cannot be resized exactly in our frames and therefore are centered */
154                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
155                 xcb_rectangle_t crect = {2, decoration_height,
156                                          client->rect.width - (2 + 2), client->rect.height - 2 - decoration_height};
157                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
158         }
159
160         /* Draw the lines */
161         xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
162         xcb_draw_line(conn, drawable, gc, border_color, 2, offset + font->height + 3,
163                       2 + client->rect.width, offset + font->height + 3);
164
165         /* If the client has a title, we draw it */
166         if (client->name != NULL) {
167                 /* Draw the font */
168                 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
169                 uint32_t values[] = { text_color, background_color, font->id };
170                 xcb_change_gc(conn, gc, mask, values);
171
172                 /* name_len == -1 means this is a legacy application which does not specify _NET_WM_NAME,
173                    and we don’t handle the old window name (COMPOUND_TEXT) but only _NET_WM_NAME, which
174                    is UTF-8 */
175                 if (client->name_len == -1)
176                         xcb_image_text_8(conn, strlen(client->name), drawable, gc, 3 /* X */,
177                                          offset + font->height /* Y = baseline of font */, client->name);
178                 else
179                         xcb_image_text_16(conn, client->name_len, drawable, gc, 3 /* X */,
180                                           offset + font->height /* Y = baseline of font */, (xcb_char2b_t*)client->name);
181         }
182 }
183
184 /*
185  * Pushes the client’s x and y coordinates to X11
186  *
187  */
188 static void reposition_client(xcb_connection_t *conn, Client *client) {
189         LOG("frame 0x%08x needs to be pushed to %dx%d\n", client->frame, client->rect.x, client->rect.y);
190         /* Note: We can use a pointer to client->x like an array of uint32_ts
191            because it is followed by client->y by definition */
192         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
193 }
194
195 /*
196  * Pushes the client’s width/height to X11 and resizes the child window
197  *
198  */
199 static void resize_client(xcb_connection_t *conn, Client *client) {
200         i3Font *font = load_font(conn, config.font);
201
202         LOG("resizing client 0x%08x to %d x %d\n", client->frame, client->rect.width, client->rect.height);
203         xcb_configure_window(conn, client->frame,
204                         XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
205                         &(client->rect.width));
206
207         /* Adjust the position of the child inside its frame.
208          * The coordinates of the child are relative to its frame, we
209          * add a border of 2 pixel to each value */
210         uint32_t mask = XCB_CONFIG_WINDOW_X |
211                         XCB_CONFIG_WINDOW_Y |
212                         XCB_CONFIG_WINDOW_WIDTH |
213                         XCB_CONFIG_WINDOW_HEIGHT;
214         Rect *rect = &(client->child_rect);
215         switch ((client->container != NULL ? client->container->mode : MODE_DEFAULT)) {
216                 case MODE_STACK:
217                         rect->x = 2;
218                         rect->y = 0;
219                         rect->width = client->rect.width - (2 + 2);
220                         rect->height = client->rect.height - 2;
221                         break;
222                 default:
223                         if (client->titlebar_position == TITLEBAR_OFF) {
224                                 rect->x = 0;
225                                 rect->y = 0;
226                                 rect->width = client->rect.width;
227                                 rect->height = client->rect.height;
228                         } else {
229                                 rect->x = 2;
230                                 rect->y = font->height + 2 + 2;
231                                 rect->width = client->rect.width - (2 + 2);
232                                 rect->height = client->rect.height - ((font->height + 2 + 2) + 2);
233                         }
234                         break;
235         }
236
237         /* Obey the ratio, if any */
238         if (client->proportional_height != 0 &&
239             client->proportional_width != 0) {
240                 LOG("proportional height = %d, width = %d\n", client->proportional_height, client->proportional_width);
241                 double new_height = rect->height + 1;
242                 int new_width = rect->width;
243
244                 while (new_height > rect->height) {
245                         new_height = ((double)client->proportional_height / client->proportional_width) * new_width;
246
247                         if (new_height > rect->height)
248                                 new_width--;
249                 }
250                 /* Center the window */
251                 rect->y += (rect->height / 2) - (new_height / 2);
252                 rect->x += (rect->width / 2) - (new_width / 2);
253
254                 rect->height = new_height;
255                 rect->width = new_width;
256                 LOG("new_height = %f, new_width = %d\n", new_height, new_width);
257         }
258
259         LOG("child will be at %dx%d with size %dx%d\n", rect->x, rect->y, rect->width, rect->height);
260
261         xcb_configure_window(conn, client->child, mask, &(rect->x));
262
263         /* After configuring a child window we need to fake a configure_notify_event according
264            to ICCCM 4.2.3. This seems rather broken, especially since X sends exactly the same
265            configure_notify_event automatically according to xtrace. Anyone knows details? */
266         fake_configure_notify(conn, *rect, client->child);
267 }
268
269 /*
270  * Renders the given container. Is called by render_layout() or individually (for example
271  * when focus changes in a stacking container)
272  *
273  */
274 void render_container(xcb_connection_t *conn, Container *container) {
275         Client *client;
276         int num_clients = 0, current_client = 0;
277
278         if (container->currently_focused == NULL)
279                 return;
280
281         CIRCLEQ_FOREACH(client, &(container->clients), clients)
282                 num_clients++;
283
284         if (container->mode == MODE_DEFAULT) {
285                 LOG("got %d clients in this default container.\n", num_clients);
286                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
287                         /* If the client is in fullscreen mode, it does not get reconfigured */
288                         if (container->workspace->fullscreen_client == client) {
289                                 current_client++;
290                                 continue;
291                         }
292
293                         /* Check if we changed client->x or client->y by updating it.
294                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
295                         if (client->force_reconfigure |
296                             update_if_necessary(&(client->rect.x), container->x) |
297                             update_if_necessary(&(client->rect.y), container->y +
298                                         (container->height / num_clients) * current_client))
299                                 reposition_client(conn, client);
300
301                         /* TODO: vertical default layout */
302                         if (client->force_reconfigure |
303                             update_if_necessary(&(client->rect.width), container->width) |
304                             update_if_necessary(&(client->rect.height), container->height / num_clients))
305                                 resize_client(conn, client);
306
307                         client->force_reconfigure = false;
308
309                         current_client++;
310                 }
311         } else {
312                 i3Font *font = load_font(conn, config.font);
313                 int decoration_height = (font->height + 2 + 2);
314                 struct Stack_Window *stack_win = &(container->stack_win);
315
316                 /* Check if we need to remap our stack title window, it gets unmapped when the container
317                    is empty in src/handlers.c:unmap_notify() */
318                 if (stack_win->rect.height == 0)
319                         xcb_map_window(conn, stack_win->window);
320
321                 /* Check if we need to reconfigure our stack title window */
322                 if (update_if_necessary(&(stack_win->rect.x), container->x) |
323                     update_if_necessary(&(stack_win->rect.y), container->y) |
324                     update_if_necessary(&(stack_win->rect.width), container->width) |
325                     update_if_necessary(&(stack_win->rect.height), decoration_height * num_clients)) {
326
327                         /* Configuration can happen in two slightly different ways:
328
329                            If there is no client in fullscreen mode, 5 parameters are passed
330                            (x, y, width, height, stack mode is set to above which means top-most position).
331
332                            If there is a fullscreen client, the fourth parameter is set to to the
333                            fullscreen window as sibling and the stack mode is set to below, which means
334                            that the stack_window will be placed just below the sibling, that is, under
335                            the fullscreen window.
336                          */
337                         uint32_t values[] = { stack_win->rect.x, stack_win->rect.y,
338                                               stack_win->rect.width, stack_win->rect.height,
339                                               XCB_STACK_MODE_ABOVE, XCB_STACK_MODE_BELOW };
340                         uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
341                                         XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
342                                         XCB_CONFIG_WINDOW_STACK_MODE;
343
344                         /* If there is no fullscreen client, we raise the stack window */
345                         if (container->workspace->fullscreen_client != NULL) {
346                                 mask |= XCB_CONFIG_WINDOW_SIBLING;
347                                 values[4] = container->workspace->fullscreen_client->frame;
348                         }
349
350                         xcb_configure_window(conn, stack_win->window, mask, values);
351                 }
352
353                 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
354                 client = container->currently_focused;
355
356                 if (container->workspace->fullscreen_client == NULL) {
357                         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
358                         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
359                 }
360
361                 /* Render the decorations of all clients */
362                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
363                         /* If the client is in fullscreen mode, it does not get reconfigured */
364                         if (container->workspace->fullscreen_client == client) {
365                                 current_client++;
366                                 continue;
367                         }
368
369                         /* Check if we changed client->x or client->y by updating it.
370                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
371                         if (client->force_reconfigure |
372                             update_if_necessary(&(client->rect.x), container->x) |
373                             update_if_necessary(&(client->rect.y), container->y + (decoration_height * num_clients)))
374                                 reposition_client(conn, client);
375
376                         if (client->force_reconfigure |
377                             update_if_necessary(&(client->rect.width), container->width) |
378                             update_if_necessary(&(client->rect.height), container->height - (decoration_height * num_clients)))
379                                 resize_client(conn, client);
380
381                         client->force_reconfigure = false;
382
383                         decorate_window(conn, client, stack_win->window, stack_win->gc,
384                                         current_client++ * decoration_height);
385                 }
386         }
387 }
388
389 static void render_bars(xcb_connection_t *conn, Workspace *r_ws, int width, int *height) {
390         Client *client;
391         SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
392                 LOG("client is at %d, should be at %d\n", client->rect.y, *height);
393                 if (client->force_reconfigure |
394                     update_if_necessary(&(client->rect.x), 0) |
395                     update_if_necessary(&(client->rect.y), *height))
396                         reposition_client(conn, client);
397
398                 if (client->force_reconfigure |
399                     update_if_necessary(&(client->rect.width), width) |
400                     update_if_necessary(&(client->rect.height), client->desired_height))
401                         resize_client(conn, client);
402
403                 client->force_reconfigure = false;
404                 *height += client->desired_height;
405         }
406 }
407
408 static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int width, int height) {
409         LOG("Rendering internal bar\n");
410         i3Font *font = load_font(conn, config.font);
411         i3Screen *screen = r_ws->screen;
412         enum { SET_NORMAL = 0, SET_FOCUSED = 1 };
413         uint32_t background_color[2],
414                  text_color[2],
415                  border_color[2],
416                  black;
417         char label[3];
418
419         black = get_colorpixel(conn, "#000000");
420
421         background_color[SET_NORMAL] = get_colorpixel(conn, "#222222");
422         text_color[SET_NORMAL] = get_colorpixel(conn, "#888888");
423         border_color[SET_NORMAL] = get_colorpixel(conn, "#333333");
424
425         background_color[SET_FOCUSED] = get_colorpixel(conn, "#285577");
426         text_color[SET_FOCUSED] = get_colorpixel(conn, "#ffffff");
427         border_color[SET_FOCUSED] = get_colorpixel(conn, "#4c7899");
428
429         /* Fill the whole bar in black */
430         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black);
431         xcb_rectangle_t rect = {0, 0, width, height};
432         xcb_poly_fill_rectangle(conn, screen->bar, screen->bargc, 1, &rect);
433
434         /* Set font */
435         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FONT, font->id);
436
437         int drawn = 0;
438         for (int c = 0; c < 10; c++) {
439                 if (workspaces[c].screen == screen) {
440                         int set = (screen->current_workspace == c ? SET_FOCUSED : SET_NORMAL);
441
442                         xcb_draw_rect(conn, screen->bar, screen->bargc, border_color[set],
443                                       drawn * height, 1, height - 2, height - 2);
444                         xcb_draw_rect(conn, screen->bar, screen->bargc, background_color[set],
445                                       drawn * height + 1, 2, height - 4, height - 4);
446
447                         snprintf(label, sizeof(label), "%d", c+1);
448                         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, text_color[set]);
449                         xcb_change_gc_single(conn, screen->bargc, XCB_GC_BACKGROUND, background_color[set]);
450                         xcb_image_text_8(conn, strlen(label), screen->bar, screen->bargc, drawn * height + 5 /* X */,
451                                                         font->height + 1 /* Y = baseline of font */, label);
452                         drawn++;
453                 }
454         }
455
456         LOG("done rendering internal\n");
457 }
458
459 /*
460  * Modifies the event mask of all clients on the given workspace to either ignore or to handle
461  * enter notifies. It is handy to ignore notifies because they will be sent when a window is mapped
462  * under the cursor, thus when the user didn’t enter the window actively at all.
463  *
464  */
465 void ignore_enter_notify_forall(xcb_connection_t *conn, Workspace *workspace, bool ignore_enter_notify) {
466         Client *client;
467         uint32_t values[1];
468
469         LOG("Ignore enter_notify = %d\n", ignore_enter_notify);
470
471         FOR_TABLE(workspace)
472                 CIRCLEQ_FOREACH(client, &(workspace->table[cols][rows]->clients), clients) {
473                         /* Change event mask for the decorations */
474                         values[0] = FRAME_EVENT_MASK;
475                         if (ignore_enter_notify)
476                                 values[0] &= ~(XCB_EVENT_MASK_ENTER_WINDOW);
477                         xcb_change_window_attributes(conn, client->frame, XCB_CW_EVENT_MASK, values);
478
479                         /* Change event mask for the child itself */
480                         values[0] = CHILD_EVENT_MASK;
481                         if (ignore_enter_notify)
482                                 values[0] &= ~(XCB_EVENT_MASK_ENTER_WINDOW);
483                         xcb_change_window_attributes(conn, client->child, XCB_CW_EVENT_MASK, values);
484                 }
485 }
486
487 void render_layout(xcb_connection_t *conn) {
488         i3Screen *screen;
489         i3Font *font = load_font(conn, config.font);
490
491         TAILQ_FOREACH(screen, virtual_screens, screens) {
492                 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
493                 Workspace *r_ws = &(workspaces[screen->current_workspace]);
494
495                 LOG("Rendering screen %d\n", screen->num);
496
497                 int width = r_ws->rect.width;
498                 int height = r_ws->rect.height;
499
500                 /* Reserve space for dock clients */
501                 Client *client;
502                 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
503                         height -= client->desired_height;
504
505                 /* Space for the internal bar */
506                 height -= (font->height + 6);
507
508                 LOG("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
509
510                 int xoffset[r_ws->rows];
511                 int yoffset[r_ws->cols];
512                 /* Initialize offsets */
513                 for (int cols = 0; cols < r_ws->cols; cols++)
514                         yoffset[cols] = r_ws->rect.y;
515                 for (int rows = 0; rows < r_ws->rows; rows++)
516                         xoffset[rows] = r_ws->rect.x;
517
518                 dump_table(conn, r_ws);
519
520                 ignore_enter_notify_forall(conn, r_ws, true);
521
522                 /* Go through the whole table and render what’s necessary */
523                 FOR_TABLE(r_ws) {
524                         Container *container = r_ws->table[cols][rows];
525                         int single_width, single_height;
526                         LOG("\n");
527                         LOG("========\n");
528                         LOG("container has %d colspan, %d rowspan\n",
529                                         container->colspan, container->rowspan);
530                         LOG("container at %d, %d\n", xoffset[rows], yoffset[cols]);
531                         /* Update position of the container */
532                         container->row = rows;
533                         container->col = cols;
534                         container->x = xoffset[rows];
535                         container->y = yoffset[cols];
536
537                         if (container->width_factor == 0)
538                                 container->width = (width / r_ws->cols);
539                         else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
540                         single_width = container->width;
541                         container->width *= container->colspan;
542
543                         if (container->height_factor == 0)
544                                 container->height = (height / r_ws->rows);
545                         else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
546                         single_height = container->height;
547                         container->height *= container->rowspan;
548
549                         /* Render the container if it is not empty */
550                         render_container(conn, container);
551
552                         xoffset[rows] += single_width;
553                         yoffset[cols] += single_height;
554                         LOG("==========\n");
555                 }
556
557                 ignore_enter_notify_forall(conn, r_ws, false);
558
559                 render_bars(conn, r_ws, width, &height);
560                 render_internal_bar(conn, r_ws, width, font->height + 6);
561         }
562
563         xcb_flush(conn);
564 }