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