]> git.sur5r.net Git - i3/i3/blob - src/layout.c
Implement slog() and the LOG() macro, convert printf() to LOG()
[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         /* TODO: utf8? */
171         char *label;
172         asprintf(&label, "%.*s", client->name_len, client->name);
173         xcb_image_text_8(conn, strlen(label), drawable, gc, 3 /* X */, offset + font->height /* Y = baseline of font */, label);
174         free(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                         /* Check if we changed client->x or client->y by updating it.
296                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
297                         if (client->force_reconfigure |
298                             update_if_necessary(&(client->rect.x), container->x) |
299                             update_if_necessary(&(client->rect.y), container->y +
300                                         (container->height / num_clients) * current_client))
301                                 reposition_client(conn, client);
302
303                         /* TODO: vertical default layout */
304                         if (client->force_reconfigure |
305                             update_if_necessary(&(client->rect.width), container->width) |
306                             update_if_necessary(&(client->rect.height), container->height / num_clients))
307                                 resize_client(conn, client);
308
309                         client->force_reconfigure = false;
310
311                         current_client++;
312                 }
313         } else {
314                 i3Font *font = load_font(conn, config.font);
315                 int decoration_height = (font->height + 2 + 2);
316                 struct Stack_Window *stack_win = &(container->stack_win);
317
318                 /* Check if we need to remap our stack title window, it gets unmapped when the container
319                    is empty in src/handlers.c:unmap_notify() */
320                 if (stack_win->rect.height == 0)
321                         xcb_map_window(conn, stack_win->window);
322
323                 /* Check if we need to reconfigure our stack title window */
324                 if (update_if_necessary(&(stack_win->rect.x), container->x) |
325                     update_if_necessary(&(stack_win->rect.y), container->y) |
326                     update_if_necessary(&(stack_win->rect.width), container->width) |
327                     update_if_necessary(&(stack_win->rect.height), decoration_height * num_clients)) {
328
329                         uint32_t values[] = { stack_win->rect.x, stack_win->rect.y,
330                                               stack_win->rect.width, stack_win->rect.height,
331                                               XCB_STACK_MODE_ABOVE };
332
333                         xcb_configure_window(conn, stack_win->window,
334                                              XCB_CONFIG_WINDOW_X     | XCB_CONFIG_WINDOW_Y      |
335                                              XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
336                                              XCB_CONFIG_WINDOW_STACK_MODE,
337                                              values);
338                 }
339
340                 /* Reconfigure the currently focused client, if necessary. It is the only visible one */
341                 client = container->currently_focused;
342
343                 if (container->workspace->fullscreen_client == NULL) {
344                         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
345                         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
346                 }
347
348                 /* Render the decorations of all clients */
349                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
350                         /* Check if we changed client->x or client->y by updating it.
351                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
352                         if (client->force_reconfigure |
353                             update_if_necessary(&(client->rect.x), container->x) |
354                             update_if_necessary(&(client->rect.y), container->y + (decoration_height * num_clients)))
355                                 reposition_client(conn, client);
356
357                         if (client->force_reconfigure |
358                             update_if_necessary(&(client->rect.width), container->width) |
359                             update_if_necessary(&(client->rect.height), container->height - (decoration_height * num_clients)))
360                                 resize_client(conn, client);
361
362                         client->force_reconfigure = false;
363
364                         decorate_window(conn, client, stack_win->window, stack_win->gc,
365                                         current_client++ * decoration_height);
366                 }
367         }
368 }
369
370 static void render_bars(xcb_connection_t *conn, Workspace *r_ws, int width, int *height) {
371         Client *client;
372         SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) {
373                 if (client->force_reconfigure |
374                     update_if_necessary(&(client->rect.x), 0) |
375                     update_if_necessary(&(client->rect.y), *height))
376                         reposition_client(conn, client);
377
378                 if (client->force_reconfigure |
379                     update_if_necessary(&(client->rect.width), width) |
380                     update_if_necessary(&(client->rect.height), client->desired_height))
381                         resize_client(conn, client);
382
383                 client->force_reconfigure = false;
384                 *height += client->desired_height;
385         }
386 }
387
388 static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int width, int height) {
389         LOG("Rendering internal bar\n");
390         i3Font *font = load_font(conn, config.font);
391         i3Screen *screen = r_ws->screen;
392         enum { SET_NORMAL = 0, SET_FOCUSED = 1 };
393         uint32_t background_color[2],
394                  text_color[2],
395                  border_color[2],
396                  black;
397         char label[3];
398
399         black = get_colorpixel(conn, "#000000");
400
401         background_color[SET_NORMAL] = get_colorpixel(conn, "#222222");
402         text_color[SET_NORMAL] = get_colorpixel(conn, "#888888");
403         border_color[SET_NORMAL] = get_colorpixel(conn, "#333333");
404
405         background_color[SET_FOCUSED] = get_colorpixel(conn, "#285577");
406         text_color[SET_FOCUSED] = get_colorpixel(conn, "#ffffff");
407         border_color[SET_FOCUSED] = get_colorpixel(conn, "#4c7899");
408
409         /* Fill the whole bar in black */
410         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black);
411         xcb_rectangle_t rect = {0, 0, width, height};
412         xcb_poly_fill_rectangle(conn, screen->bar, screen->bargc, 1, &rect);
413
414         /* Set font */
415         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FONT, font->id);
416
417         int drawn = 0;
418         for (int c = 0; c < 10; c++) {
419                 if (workspaces[c].screen == screen) {
420                         int set = (screen->current_workspace == c ? SET_FOCUSED : SET_NORMAL);
421
422                         xcb_draw_rect(conn, screen->bar, screen->bargc, border_color[set],
423                                       drawn * height, 1, height - 2, height - 2);
424                         xcb_draw_rect(conn, screen->bar, screen->bargc, background_color[set],
425                                       drawn * height + 1, 2, height - 4, height - 4);
426
427                         snprintf(label, sizeof(label), "%d", c+1);
428                         xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, text_color[set]);
429                         xcb_change_gc_single(conn, screen->bargc, XCB_GC_BACKGROUND, background_color[set]);
430                         xcb_image_text_8(conn, strlen(label), screen->bar, screen->bargc, drawn * height + 5 /* X */,
431                                                         font->height + 1 /* Y = baseline of font */, label);
432                         drawn++;
433                 }
434         }
435
436         LOG("done rendering internal\n");
437 }
438
439 void render_layout(xcb_connection_t *conn) {
440         i3Screen *screen;
441         i3Font *font = load_font(conn, config.font);
442
443         TAILQ_FOREACH(screen, virtual_screens, screens) {
444                 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
445                 Workspace *r_ws = &(workspaces[screen->current_workspace]);
446
447                 LOG("Rendering screen %d\n", screen->num);
448                 if (r_ws->fullscreen_client != NULL)
449                         /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
450                         continue;
451
452                 int width = r_ws->rect.width;
453                 int height = r_ws->rect.height;
454
455                 /* Reserve space for dock clients */
456                 Client *client;
457                 SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients)
458                         height -= client->desired_height;
459
460                 /* Space for the internal bar */
461                 height -= (font->height + 6);
462
463                 LOG("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
464
465                 int xoffset[r_ws->rows];
466                 int yoffset[r_ws->cols];
467                 /* Initialize offsets */
468                 for (int cols = 0; cols < r_ws->cols; cols++)
469                         yoffset[cols] = r_ws->rect.y;
470                 for (int rows = 0; rows < r_ws->rows; rows++)
471                         xoffset[rows] = r_ws->rect.x;
472
473                 dump_table(conn, r_ws);
474                 /* Go through the whole table and render what’s necessary */
475                 for (int cols = 0; cols < r_ws->cols; cols++)
476                         for (int rows = 0; rows < r_ws->rows; rows++) {
477                                 Container *container = r_ws->table[cols][rows];
478                                 int single_width, single_height;
479                                 LOG("\n");
480                                 LOG("========\n");
481                                 LOG("container has %d colspan, %d rowspan\n",
482                                                 container->colspan, container->rowspan);
483                                 LOG("container at %d, %d\n", xoffset[rows], yoffset[cols]);
484                                 /* Update position of the container */
485                                 container->row = rows;
486                                 container->col = cols;
487                                 container->x = xoffset[rows];
488                                 container->y = yoffset[cols];
489
490                                 if (container->width_factor == 0)
491                                         container->width = (width / r_ws->cols);
492                                 else container->width = get_unoccupied_x(r_ws, rows) * container->width_factor;
493                                 single_width = container->width;
494                                 container->width *= container->colspan;
495
496                                 if (container->height_factor == 0)
497                                         container->height = (height / r_ws->rows);
498                                 else container->height = get_unoccupied_y(r_ws, cols) * container->height_factor;
499                                 single_height = container->height;
500                                 container->height *= container->rowspan;
501
502                                 /* Render the container if it is not empty */
503                                 render_container(conn, container);
504
505                                 xoffset[rows] += single_width;
506                                 yoffset[cols] += single_height;
507                                 LOG("==========\n");
508                         }
509
510                 render_bars(conn, r_ws, width, &height);
511                 render_internal_bar(conn, r_ws, width, 18);
512         }
513
514         xcb_flush(conn);
515 }