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