]> git.sur5r.net Git - i3/i3/blob - src/layout.c
190dc04a839aa788a3675b7d3181e74d17f87dfe
[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         /* Draw the font */
166         uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
167         uint32_t values[] = { text_color, background_color, font->id };
168         xcb_change_gc(conn, gc, mask, values);
169
170         int real_strlen;
171         char *ucs2_label = convert_utf8_to_ucs2(client->name, &real_strlen);
172         xcb_image_text_16(conn, real_strlen, drawable, gc, 3 /* X */,
173                           offset + font->height /* Y = baseline of font */, (xcb_char2b_t*)ucs2_label);
174         free(ucs2_label);
175 }
176
177 /*
178  * Pushes the client’s x and y coordinates to X11
179  *
180  */
181 static void reposition_client(xcb_connection_t *conn, Client *client) {
182         LOG("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
183         /* Note: We can use a pointer to client->x like an array of uint32_ts
184            because it is followed by client->y by definition */
185         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
186 }
187
188 /*
189  * Pushes the client’s width/height to X11 and resizes the child window
190  *
191  */
192 static void resize_client(xcb_connection_t *conn, Client *client) {
193         i3Font *font = load_font(conn, config.font);
194
195         LOG("resizing client to %d x %d\n", client->rect.width, client->rect.height);
196         xcb_configure_window(conn, client->frame,
197                         XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
198                         &(client->rect.width));
199
200         /* Adjust the position of the child inside its frame.
201          * The coordinates of the child are relative to its frame, we
202          * add a border of 2 pixel to each value */
203         uint32_t mask = XCB_CONFIG_WINDOW_X |
204                         XCB_CONFIG_WINDOW_Y |
205                         XCB_CONFIG_WINDOW_WIDTH |
206                         XCB_CONFIG_WINDOW_HEIGHT;
207         Rect *rect = &(client->child_rect);
208         switch (client->container->mode) {
209                 case MODE_STACK:
210                         rect->x = 2;
211                         rect->y = 0;
212                         rect->width = client->rect.width - (2 + 2);
213                         rect->height = client->rect.height - 2;
214                         break;
215                 default:
216                         if (client->titlebar_position == TITLEBAR_OFF) {
217                                 rect->x = 0;
218                                 rect->y = 0;
219                                 rect->width = client->rect.width;
220                                 rect->height = client->rect.height;
221                         } else {
222                                 rect->x = 2;
223                                 rect->y = font->height + 2 + 2;
224                                 rect->width = client->rect.width - (2 + 2);
225                                 rect->height = client->rect.height - ((font->height + 2 + 2) + 2);
226                         }
227                         break;
228         }
229
230         /* Obey the ratio, if any */
231         if (client->proportional_height != 0 &&
232             client->proportional_width != 0) {
233                 LOG("proportional height = %d, width = %d\n", client->proportional_height, client->proportional_width);
234                 double new_height = rect->height + 1;
235                 int new_width = rect->width;
236
237                 while (new_height > rect->height) {
238                         new_height = ((double)client->proportional_height / client->proportional_width) * new_width;
239
240                         if (new_height > rect->height)
241                                 new_width--;
242                 }
243                 /* Center the window */
244                 rect->y += (rect->height / 2) - (new_height / 2);
245                 rect->x += (rect->width / 2) - (new_width / 2);
246
247                 rect->height = new_height;
248                 rect->width = new_width;
249                 LOG("new_height = %f, new_width = %d\n", new_height, new_width);
250         }
251
252         LOG("child will be at %dx%d with size %dx%d\n", rect->x, rect->y, rect->width, rect->height);
253
254         xcb_configure_window(conn, client->child, mask, &(rect->x));
255
256         /* After configuring a child window we need to fake a configure_notify_event according
257            to ICCCM 4.2.3. This seems rather broken, especially since X sends exactly the same
258            configure_notify_event automatically according to xtrace. Anyone knows details? */
259         xcb_configure_notify_event_t event;
260
261         event.event = client->child;
262         event.window = client->child;
263         event.response_type = XCB_CONFIGURE_NOTIFY;
264
265         event.x = rect->x;
266         event.y = rect->y;
267         event.width = rect->width;
268         event.height = rect->height;
269
270         event.border_width = 0;
271         event.above_sibling = XCB_NONE;
272         event.override_redirect = false;
273
274         xcb_send_event(conn, false, client->child, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)&event);
275 }
276
277 /*
278  * Renders the given container. Is called by render_layout() or individually (for example
279  * when focus changes in a stacking container)
280  *
281  */
282 void render_container(xcb_connection_t *conn, Container *container) {
283         Client *client;
284         int num_clients = 0, current_client = 0;
285
286         if (container->currently_focused == NULL)
287                 return;
288
289         CIRCLEQ_FOREACH(client, &(container->clients), clients)
290                 num_clients++;
291
292         if (container->mode == MODE_DEFAULT) {
293                 LOG("got %d clients in this default container.\n", num_clients);
294                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
295                         /* If the client is in fullscreen mode, it does not get reconfigured */
296                         if (container->workspace->fullscreen_client == client) {
297                                 current_client++;
298                                 continue;
299                         }
300
301                         /* Check if we changed client->x or client->y by updating it.
302                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
303                         if (client->force_reconfigure |
304                             update_if_necessary(&(client->rect.x), container->x) |
305                             update_if_necessary(&(client->rect.y), container->y +
306                                         (container->height / num_clients) * current_client))
307                                 reposition_client(conn, client);
308
309                         /* TODO: vertical default layout */
310                         if (client->force_reconfigure |
311                             update_if_necessary(&(client->rect.width), container->width) |
312                             update_if_necessary(&(client->rect.height), container->height / num_clients))
313                                 resize_client(conn, client);
314
315                         client->force_reconfigure = false;
316
317                         current_client++;
318                 }
319         } else {
320                 i3Font *font = load_font(conn, config.font);
321                 int decoration_height = (font->height + 2 + 2);
322                 struct Stack_Window *stack_win = &(container->stack_win);
323
324                 /* Check if we need to remap our stack title window, it gets unmapped when the container
325                    is empty in src/handlers.c:unmap_notify() */
326                 if (stack_win->rect.height == 0)
327                         xcb_map_window(conn, stack_win->window);
328
329                 /* Check if we need to reconfigure our stack title window */
330                 if (update_if_necessary(&(stack_win->rect.x), container->x) |
331                     update_if_necessary(&(stack_win->rect.y), container->y) |
332                     update_if_necessary(&(stack_win->rect.width), container->width) |
333                     update_if_necessary(&(stack_win->rect.height), decoration_height * num_clients)) {
334
335                         /* Configuration can happen in two slightly different ways:
336
337                            If there is no client in fullscreen mode, 5 parameters are passed
338                            (x, y, width, height, stack mode is set to above which means top-most position).
339
340                            If there is a fullscreen client, the fourth parameter is set to to the
341                            fullscreen window as sibling and the stack mode is set to below, which means
342                            that the stack_window will be placed just below the sibling, that is, under
343                            the fullscreen window.
344                          */
345                         uint32_t values[] = { stack_win->rect.x, stack_win->rect.y,
346                                               stack_win->rect.width, stack_win->rect.height,
347                                               XCB_STACK_MODE_ABOVE, XCB_STACK_MODE_BELOW };
348                         uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
349                                         XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
350                                         XCB_CONFIG_WINDOW_STACK_MODE;
351
352                         /* If there is no fullscreen client, we raise the stack window */
353                         if (container->workspace->fullscreen_client != NULL) {
354                                 mask |= XCB_CONFIG_WINDOW_SIBLING;
355                                 values[4] = container->workspace->fullscreen_client->frame;
356                         }
357
358                         xcb_configure_window(conn, stack_win->window, mask, values);
359                 }
360
361                 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
362                 client = container->currently_focused;
363
364                 if (container->workspace->fullscreen_client == NULL) {
365                         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
366                         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
367                 }
368
369                 /* Render the decorations of all clients */
370                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
371                         /* If the client is in fullscreen mode, it does not get reconfigured */
372                         if (container->workspace->fullscreen_client == client) {
373                                 current_client++;
374                                 continue;
375                         }
376
377                         /* Check if we changed client->x or client->y by updating it.
378                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
379                         if (client->force_reconfigure |
380                             update_if_necessary(&(client->rect.x), container->x) |
381                             update_if_necessary(&(client->rect.y), container->y + (decoration_height * num_clients)))
382                                 reposition_client(conn, client);
383
384                         if (client->force_reconfigure |
385                             update_if_necessary(&(client->rect.width), container->width) |
386                             update_if_necessary(&(client->rect.height), container->height - (decoration_height * num_clients)))
387                                 resize_client(conn, client);
388
389                         client->force_reconfigure = false;
390
391                         decorate_window(conn, client, stack_win->window, stack_win->gc,
392                                         current_client++ * decoration_height);
393                 }
394         }
395 }
396
397 static void render_bars(xcb_connection_t *conn, Workspace *r_ws, int width, int *height) {
398         Client *client;
399         SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
400                 if (client->force_reconfigure |
401                     update_if_necessary(&(client->rect.x), 0) |
402                     update_if_necessary(&(client->rect.y), *height))
403                         reposition_client(conn, client);
404
405                 if (client->force_reconfigure |
406                     update_if_necessary(&(client->rect.width), width) |
407                     update_if_necessary(&(client->rect.height), client->desired_height))
408                         resize_client(conn, client);
409
410                 client->force_reconfigure = false;
411                 *height += client->desired_height;
412         }
413 }
414
415 static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int width, int height) {
416         LOG("Rendering internal bar\n");
417         i3Font *font = load_font(conn, config.font);
418         i3Screen *screen = r_ws->screen;
419         enum { SET_NORMAL = 0, SET_FOCUSED = 1 };
420         uint32_t background_color[2],
421                  text_color[2],
422                  border_color[2],
423                  black;
424         char label[3];
425
426         black = get_colorpixel(conn, "#000000");
427
428         background_color[SET_NORMAL] = get_colorpixel(conn, "#222222");
429         text_color[SET_NORMAL] = get_colorpixel(conn, "#888888");
430         border_color[SET_NORMAL] = get_colorpixel(conn, "#333333");
431
432         background_color[SET_FOCUSED] = get_colorpixel(conn, "#285577");
433         text_color[SET_FOCUSED] = get_colorpixel(conn, "#ffffff");
434         border_color[SET_FOCUSED] = get_colorpixel(conn, "#4c7899");
435
436         /* Fill the whole bar in black */
437         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black);
438         xcb_rectangle_t rect = {0, 0, width, height};
439         xcb_poly_fill_rectangle(conn, screen->bar, screen->bargc, 1, &rect);
440
441         /* Set font */
442         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FONT, font->id);
443
444         int drawn = 0;
445         for (int c = 0; c < 10; c++) {
446                 if (workspaces[c].screen == screen) {
447                         int set = (screen->current_workspace == c ? SET_FOCUSED : SET_NORMAL);
448
449                         xcb_draw_rect(conn, screen->bar, screen->bargc, border_color[set],
450                                       drawn * height, 1, height - 2, height - 2);
451                         xcb_draw_rect(conn, screen->bar, screen->bargc, background_color[set],
452                                       drawn * height + 1, 2, height - 4, height - 4);
453
454                         snprintf(label, sizeof(label), "%d", c+1);
455                         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, text_color[set]);
456                         xcb_change_gc_single(conn, screen->bargc, XCB_GC_BACKGROUND, background_color[set]);
457                         xcb_image_text_8(conn, strlen(label), screen->bar, screen->bargc, drawn * height + 5 /* X */,
458                                                         font->height + 1 /* Y = baseline of font */, label);
459                         drawn++;
460                 }
461         }
462
463         LOG("done rendering internal\n");
464 }
465
466 void render_layout(xcb_connection_t *conn) {
467         i3Screen *screen;
468         i3Font *font = load_font(conn, config.font);
469
470         TAILQ_FOREACH(screen, virtual_screens, screens) {
471                 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
472                 Workspace *r_ws = &(workspaces[screen->current_workspace]);
473
474                 LOG("Rendering screen %d\n", screen->num);
475
476                 int width = r_ws->rect.width;
477                 int height = r_ws->rect.height;
478
479                 /* Reserve space for dock clients */
480                 Client *client;
481                 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
482                         height -= client->desired_height;
483
484                 /* Space for the internal bar */
485                 height -= (font->height + 6);
486
487                 LOG("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
488
489                 int xoffset[r_ws->rows];
490                 int yoffset[r_ws->cols];
491                 /* Initialize offsets */
492                 for (int cols = 0; cols < r_ws->cols; cols++)
493                         yoffset[cols] = r_ws->rect.y;
494                 for (int rows = 0; rows < r_ws->rows; rows++)
495                         xoffset[rows] = r_ws->rect.x;
496
497                 dump_table(conn, r_ws);
498                 /* Go through the whole table and render what’s necessary */
499                 for (int cols = 0; cols < r_ws->cols; cols++)
500                         for (int rows = 0; rows < r_ws->rows; rows++) {
501                                 Container *container = r_ws->table[cols][rows];
502                                 int single_width, single_height;
503                                 LOG("\n");
504                                 LOG("========\n");
505                                 LOG("container has %d colspan, %d rowspan\n",
506                                                 container->colspan, container->rowspan);
507                                 LOG("container at %d, %d\n", xoffset[rows], yoffset[cols]);
508                                 /* Update position of the container */
509                                 container->row = rows;
510                                 container->col = cols;
511                                 container->x = xoffset[rows];
512                                 container->y = yoffset[cols];
513
514                                 if (container->width_factor == 0)
515                                         container->width = (width / r_ws->cols);
516                                 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
517                                 single_width = container->width;
518                                 container->width *= container->colspan;
519
520                                 if (container->height_factor == 0)
521                                         container->height = (height / r_ws->rows);
522                                 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
523                                 single_height = container->height;
524                                 container->height *= container->rowspan;
525
526                                 /* Render the container if it is not empty */
527                                 render_container(conn, container);
528
529                                 xoffset[rows] += single_width;
530                                 yoffset[cols] += single_height;
531                                 LOG("==========\n");
532                         }
533
534                 render_bars(conn, r_ws, width, &height);
535                 render_internal_bar(conn, r_ws, width, 18);
536         }
537
538         xcb_flush(conn);
539 }