]> git.sur5r.net Git - i3/i3/blob - src/con.c
Don't mess with sizes when moving to other ws.
[i3/i3] / src / con.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * con.c contains all functions which deal with containers directly (creating
8  * containers, searching containers, getting specific properties from
9  * containers, …).
10  *
11  */
12 #include "all.h"
13
14 char *colors[] = {
15     "#ff0000",
16     "#00FF00",
17     "#0000FF",
18     "#ff00ff",
19     "#00ffff",
20     "#ffff00",
21     "#aa0000",
22     "#00aa00",
23     "#0000aa",
24     "#aa00aa"
25 };
26
27 /*
28  * Create a new container (and attach it to the given parent, if not NULL).
29  * This function initializes the data structures and creates the appropriate
30  * X11 IDs using x_con_init().
31  *
32  */
33 Con *con_new(Con *parent) {
34     Con *new = scalloc(sizeof(Con));
35     TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
36     new->type = CT_CON;
37     new->border_style = config.default_border;
38     static int cnt = 0;
39     DLOG("opening window %d\n", cnt);
40
41     /* TODO: remove window coloring after test-phase */
42     DLOG("color %s\n", colors[cnt]);
43     new->name = strdup(colors[cnt]);
44     //uint32_t cp = get_colorpixel(colors[cnt]);
45     cnt++;
46     if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
47         cnt = 0;
48
49     x_con_init(new);
50
51     // TODO: this needs to be integrated into src/x.c and updated on config file reloads
52     xcb_change_window_attributes(conn, new->frame, XCB_CW_BACK_PIXEL, &config.client.background);
53
54     TAILQ_INIT(&(new->floating_head));
55     TAILQ_INIT(&(new->nodes_head));
56     TAILQ_INIT(&(new->focus_head));
57     TAILQ_INIT(&(new->swallow_head));
58
59     if (parent != NULL)
60         con_attach(new, parent, false);
61
62     return new;
63 }
64
65 /*
66  * Attaches the given container to the given parent. This happens when moving
67  * a container or when inserting a new container at a specific place in the
68  * tree.
69  *
70  * ignore_focus is to just insert the Con at the end (useful when creating a
71  * new split container *around* some containers, that is, detaching and
72  * attaching them in order without wanting to mess with the focus in between).
73  *
74  */
75 void con_attach(Con *con, Con *parent, bool ignore_focus) {
76     con->parent = parent;
77     Con *loop;
78     Con *current = NULL;
79     struct nodes_head *nodes_head = &(parent->nodes_head);
80
81     /* Workspaces are handled differently: they need to be inserted at the
82      * right position. */
83     if (con->type == CT_WORKSPACE) {
84         DLOG("it's a workspace. num = %d\n", con->num);
85         if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
86             TAILQ_INSERT_TAIL(nodes_head, con, nodes);
87         } else {
88             current = TAILQ_FIRST(nodes_head);
89             if (con->num < current->num) {
90                 /* we need to insert the container at the beginning */
91                 TAILQ_INSERT_HEAD(nodes_head, con, nodes);
92             } else {
93                 while (current->num != -1 && con->num > current->num) {
94                     current = TAILQ_NEXT(current, nodes);
95                     if (current == TAILQ_END(nodes_head)) {
96                         current = NULL;
97                         break;
98                     }
99                 }
100                 /* we need to insert con after current, if current is not NULL */
101                 if (current)
102                     TAILQ_INSERT_BEFORE(current, con, nodes);
103                 else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
104             }
105         }
106         goto add_to_focus_head;
107     }
108
109     if (con->type == CT_FLOATING_CON) {
110         DLOG("Inserting into floating containers\n");
111         TAILQ_INSERT_TAIL(&(parent->floating_head), con, floating_windows);
112     } else {
113         if (!ignore_focus) {
114             /* Get the first tiling container in focus stack */
115             TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
116                 if (loop->type == CT_FLOATING_CON)
117                     continue;
118                 current = loop;
119                 break;
120             }
121         }
122
123         /* Insert the container after the tiling container, if found */
124         if (current) {
125             DLOG("Inserting con = %p after last focused tiling con %p\n",
126                  con, current);
127             TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
128         } else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
129     }
130
131 add_to_focus_head:
132     /* We insert to the TAIL because con_focus() will correct this.
133      * This way, we have the option to insert Cons without having
134      * to focus them. */
135     TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
136 }
137
138 /*
139  * Detaches the given container from its current parent
140  *
141  */
142 void con_detach(Con *con) {
143     if (con->type == CT_FLOATING_CON) {
144         TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
145         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
146     } else {
147         TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
148         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
149     }
150 }
151
152 /*
153  * Sets input focus to the given container. Will be updated in X11 in the next
154  * run of x_push_changes().
155  *
156  */
157 void con_focus(Con *con) {
158     assert(con != NULL);
159     DLOG("con_focus = %p\n", con);
160
161     /* 1: set focused-pointer to the new con */
162     /* 2: exchange the position of the container in focus stack of the parent all the way up */
163     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
164     TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
165     if (con->parent->parent != NULL)
166         con_focus(con->parent);
167
168     focused = con;
169     if (con->urgent) {
170         con->urgent = false;
171         workspace_update_urgent_flag(con_get_workspace(con));
172     }
173     DLOG("con_focus done = %p\n", con);
174 }
175
176 /*
177  * Returns true when this node is a leaf node (has no children)
178  *
179  */
180 bool con_is_leaf(Con *con) {
181     return TAILQ_EMPTY(&(con->nodes_head));
182 }
183
184 /*
185  * Returns true if this node accepts a window (if the node swallows windows,
186  * it might already have swallowed enough and cannot hold any more).
187  *
188  */
189 bool con_accepts_window(Con *con) {
190     /* 1: workspaces never accept direct windows */
191     if (con->type == CT_WORKSPACE)
192         return false;
193
194     if (con->orientation != NO_ORIENTATION) {
195         DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
196         return false;
197     }
198
199     /* TODO: if this is a swallowing container, we need to check its max_clients */
200     return (con->window == NULL);
201 }
202
203 /*
204  * Gets the output container (first container with CT_OUTPUT in hierarchy) this
205  * node is on.
206  *
207  */
208 Con *con_get_output(Con *con) {
209     Con *result = con;
210     while (result != NULL && result->type != CT_OUTPUT)
211         result = result->parent;
212     /* We must be able to get an output because focus can never be set higher
213      * in the tree (root node cannot be focused). */
214     assert(result != NULL);
215     return result;
216 }
217
218 /*
219  * Gets the workspace container this node is on.
220  *
221  */
222 Con *con_get_workspace(Con *con) {
223     Con *result = con;
224     while (result != NULL && result->type != CT_WORKSPACE)
225         result = result->parent;
226     assert(result != NULL);
227     return result;
228 }
229
230 /*
231  * helper data structure for the breadth-first-search in
232  * con_get_fullscreen_con()
233  *
234  */
235 struct bfs_entry {
236     Con *con;
237
238     TAILQ_ENTRY(bfs_entry) entries;
239 };
240
241 /*
242  * Returns the first fullscreen node below this node.
243  *
244  */
245 Con *con_get_fullscreen_con(Con *con) {
246     Con *current, *child;
247
248     /* TODO: is breadth-first-search really appropriate? (check as soon as
249      * fullscreen levels and fullscreen for containers is implemented) */
250     TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
251     struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
252     entry->con = con;
253     TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
254
255     while (!TAILQ_EMPTY(&bfs_head)) {
256         entry = TAILQ_FIRST(&bfs_head);
257         current = entry->con;
258         if (current != con && current->fullscreen_mode != CF_NONE) {
259             /* empty the queue */
260             while (!TAILQ_EMPTY(&bfs_head)) {
261                 entry = TAILQ_FIRST(&bfs_head);
262                 TAILQ_REMOVE(&bfs_head, entry, entries);
263                 free(entry);
264             }
265             return current;
266         }
267
268         TAILQ_REMOVE(&bfs_head, entry, entries);
269         free(entry);
270
271         TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
272             entry = smalloc(sizeof(struct bfs_entry));
273             entry->con = child;
274             TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
275         }
276
277         TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
278             entry = smalloc(sizeof(struct bfs_entry));
279             entry->con = child;
280             TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
281         }
282     }
283
284     return NULL;
285 }
286
287 /*
288  * Returns true if the node is floating.
289  *
290  */
291 bool con_is_floating(Con *con) {
292     assert(con != NULL);
293     DLOG("checking if con %p is floating\n", con);
294     return (con->floating >= FLOATING_AUTO_ON);
295 }
296
297 /*
298  * Checks if the given container is either floating or inside some floating
299  * container. It returns the FLOATING_CON container.
300  *
301  */
302 Con *con_inside_floating(Con *con) {
303     assert(con != NULL);
304     if (con->type == CT_FLOATING_CON)
305         return con;
306
307     if (con->floating >= FLOATING_AUTO_ON)
308         return con->parent;
309
310     if (con->type == CT_WORKSPACE)
311         return NULL;
312
313     return con_inside_floating(con->parent);
314 }
315
316 /*
317  * Returns the container with the given client window ID or NULL if no such
318  * container exists.
319  *
320  */
321 Con *con_by_window_id(xcb_window_t window) {
322     Con *con;
323     TAILQ_FOREACH(con, &all_cons, all_cons)
324         if (con->window != NULL && con->window->id == window)
325             return con;
326     return NULL;
327 }
328
329 /*
330  * Returns the container with the given frame ID or NULL if no such container
331  * exists.
332  *
333  */
334 Con *con_by_frame_id(xcb_window_t frame) {
335     Con *con;
336     TAILQ_FOREACH(con, &all_cons, all_cons)
337         if (con->frame == frame)
338             return con;
339     return NULL;
340 }
341
342 /*
343  * Returns the first container which wants to swallow this window
344  * TODO: priority
345  *
346  */
347 Con *con_for_window(i3Window *window, Match **store_match) {
348     Con *con;
349     Match *match;
350     DLOG("searching con for window %p\n", window);
351     DLOG("class == %s\n", window->class_class);
352
353     TAILQ_FOREACH(con, &all_cons, all_cons)
354         TAILQ_FOREACH(match, &(con->swallow_head), matches) {
355             if (!match_matches_window(match, window))
356                 continue;
357             if (store_match != NULL)
358                 *store_match = match;
359             return con;
360         }
361
362     return NULL;
363 }
364
365 /*
366  * Returns the number of children of this container.
367  *
368  */
369 int con_num_children(Con *con) {
370     Con *child;
371     int children = 0;
372
373     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
374         children++;
375
376     return children;
377 }
378
379 /*
380  * Updates the percent attribute of the children of the given container. This
381  * function needs to be called when a window is added or removed from a
382  * container.
383  *
384  */
385 void con_fix_percent(Con *con) {
386     Con *child;
387     int children = con_num_children(con);
388
389     // calculate how much we have distributed and how many containers
390     // with a percentage set we have
391     double total = 0.0;
392     int children_with_percent = 0;
393     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
394         if (child->percent > 0.0) {
395             total += child->percent;
396             ++children_with_percent;
397         }
398     }
399
400     // if there were children without a percentage set, set to a value that
401     // will make those children proportional to all others
402     if (children_with_percent != children) {
403         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
404             if (child->percent <= 0.0) {
405                 if (children_with_percent == 0)
406                     total += (child->percent = 1.0);
407                 else total += (child->percent = total / children_with_percent);
408             }
409         }
410     }
411
412     // if we got a zero, just distribute the space equally, otherwise
413     // distribute according to the proportions we got
414     if (total == 0.0) {
415         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
416             child->percent = 1.0 / children;
417     } else if (total != 1.0) {
418         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
419             child->percent /= total;
420     }
421 }
422
423 /*
424  * Toggles fullscreen mode for the given container. Fullscreen mode will not be
425  * entered when there already is a fullscreen container on this workspace.
426  *
427  */
428 void con_toggle_fullscreen(Con *con) {
429     Con *workspace, *fullscreen;
430
431     if (con->type == CT_WORKSPACE) {
432         DLOG("You cannot make a workspace fullscreen.\n");
433         return;
434     }
435
436     DLOG("toggling fullscreen for %p / %s\n", con, con->name);
437     if (con->fullscreen_mode == CF_NONE) {
438         /* 1: check if there already is a fullscreen con */
439         workspace = con_get_workspace(con);
440         if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
441             LOG("Not entering fullscreen mode, container (%p/%s) "
442                 "already is in fullscreen mode\n",
443                 fullscreen, fullscreen->name);
444         } else {
445             /* 2: enable fullscreen */
446             con->fullscreen_mode = CF_OUTPUT;
447         }
448     } else {
449         /* 1: disable fullscreen */
450         con->fullscreen_mode = CF_NONE;
451     }
452     DLOG("mode now: %d\n", con->fullscreen_mode);
453
454     /* update _NET_WM_STATE if this container has a window */
455     /* TODO: when a window is assigned to a container which is already
456      * fullscreened, this state needs to be pushed to the client, too */
457     if (con->window == NULL)
458         return;
459
460     uint32_t values[1];
461     unsigned int num = 0;
462
463     if (con->fullscreen_mode != CF_NONE)
464         values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
465
466     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
467                         atoms[_NET_WM_STATE], ATOM, 32, num, values);
468 }
469
470 /*
471  * Moves the given container to the currently focused container on the given
472  * workspace.
473  * TODO: is there a better place for this function?
474  *
475  */
476 void con_move_to_workspace(Con *con, Con *workspace) {
477     if (con_is_floating(con)) {
478         DLOG("Using FLOATINGCON instead\n");
479         con = con->parent;
480     }
481
482     /* 1: save the container which is going to be focused after the current
483      * container is moved away */
484     Con *focus_next = con_next_focused(con);
485
486     /* 2: get the focused container of this workspace */
487     Con *next = con_descend_focused(workspace);
488
489     /* 3: we go up one level, but only when next is a normal container */
490     if (next->type != CT_WORKSPACE)
491         next = next->parent;
492
493     /* 4: if the target container is floating, we get the workspace instead.
494      * Only tiling windows need to get inserted next to the current container.
495      * */
496     Con *floatingcon = con_inside_floating(next);
497     if (floatingcon != NULL) {
498         DLOG("floatingcon, going up even further\n");
499         next = floatingcon->parent;
500     }
501
502     DLOG("Re-attaching container to %p / %s\n", next, next->name);
503     /* 5: re-attach the con to the parent of this focused container */
504     Con *parent = con->parent;
505     con_detach(con);
506     con_attach(con, next, false);
507
508     /* 6: fix the percentages */
509     con_fix_percent(parent);
510     con->percent = 0.0;
511     con_fix_percent(next);
512
513     /* 7: keep focus on the current workspace */
514     con_focus(focus_next);
515
516     /* 8: check if the parent container is empty now and close it */
517     if (parent->type != CT_WORKSPACE &&
518         TAILQ_EMPTY(&(parent->nodes_head))) {
519         DLOG("Closing empty parent container\n");
520         /* TODO: check if this container would swallow any other client and
521          * don’t close it automatically. */
522         tree_close(parent, false, false);
523     }
524 }
525
526 /*
527  * Returns the orientation of the given container (for stacked containers,
528  * vertical orientation is used regardless of the actual orientation of the
529  * container).
530  *
531  */
532 int con_orientation(Con *con) {
533     /* stacking containers behave like they are in vertical orientation */
534     if (con->layout == L_STACKED)
535         return VERT;
536
537     if (con->layout == L_TABBED)
538         return HORIZ;
539
540     return con->orientation;
541 }
542
543 /*
544  * Returns the container which will be focused next when the given container
545  * is not available anymore. Called in tree_close and con_move_to_workspace
546  * to properly restore focus.
547  *
548  */
549 Con *con_next_focused(Con *con) {
550     Con *next;
551     /* floating containers are attached to a workspace, so we focus either the
552      * next floating container (if any) or the workspace itself. */
553     if (con->type == CT_FLOATING_CON) {
554         DLOG("selecting next for CT_FLOATING_CON\n");
555         next = TAILQ_NEXT(con, floating_windows);
556         if (next == TAILQ_END(&(parent->floating_head))) {
557             Con *ws = con_get_workspace(con);
558             next = ws;
559             DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
560             while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
561                 next = TAILQ_FIRST(&(next->focus_head));
562                 if (next == con) {
563                     DLOG("skipping container itself, we want the next client\n");
564                     next = TAILQ_NEXT(next, focused);
565                 }
566             }
567             if (next == TAILQ_END(&(ws->focus_head))) {
568                 DLOG("Focus list empty, returning ws\n");
569                 next = ws;
570             }
571         }
572         return next;
573     }
574
575     /* try to focus the next container on the same level as this one */
576     next = TAILQ_NEXT(con, focused);
577
578     /* if that was not possible, go up to its parent */
579     if (next == TAILQ_END(&(parent->nodes_head)))
580         next = con->parent;
581
582     /* now go down the focus stack as far as
583      * possible, excluding the current container */
584     while (!TAILQ_EMPTY(&(next->focus_head)) &&
585            TAILQ_FIRST(&(next->focus_head)) != con)
586         next = TAILQ_FIRST(&(next->focus_head));
587
588     return next;
589 }
590
591 /*
592  * Get the next/previous container in the specified orientation. This may
593  * travel up until it finds a container with suitable orientation.
594  *
595  */
596 Con *con_get_next(Con *con, char way, orientation_t orientation) {
597     DLOG("con_get_next(way=%c, orientation=%d)\n", way, orientation);
598     /* 1: get the first parent with the same orientation */
599     Con *cur = con;
600     while (con_orientation(cur->parent) != orientation) {
601         DLOG("need to go one level further up\n");
602         if (cur->parent->type == CT_WORKSPACE) {
603             LOG("that's a workspace, we can't go further up\n");
604             return NULL;
605         }
606         cur = cur->parent;
607     }
608
609     /* 2: chose next (or previous) */
610     Con *next;
611     if (way == 'n') {
612         next = TAILQ_NEXT(cur, nodes);
613         /* if we are at the end of the list, we need to wrap */
614         if (next == TAILQ_END(&(parent->nodes_head)))
615             return NULL;
616     } else {
617         next = TAILQ_PREV(cur, nodes_head, nodes);
618         /* if we are at the end of the list, we need to wrap */
619         if (next == TAILQ_END(&(cur->nodes_head)))
620             return NULL;
621     }
622     DLOG("next = %p\n", next);
623
624     return next;
625 }
626
627 /*
628  * Returns the focused con inside this client, descending the tree as far as
629  * possible. This comes in handy when attaching a con to a workspace at the
630  * currently focused position, for example.
631  *
632  */
633 Con *con_descend_focused(Con *con) {
634     Con *next = con;
635     while (!TAILQ_EMPTY(&(next->focus_head)))
636         next = TAILQ_FIRST(&(next->focus_head));
637     return next;
638 }
639
640 /*
641  * Returns a "relative" Rect which contains the amount of pixels that need to
642  * be added to the original Rect to get the final position (obviously the
643  * amount of pixels for normal, 1pixel and borderless are different).
644  *
645  */
646 Rect con_border_style_rect(Con *con) {
647     switch (con_border_style(con)) {
648     case BS_NORMAL:
649         return (Rect){2, 0, -(2 * 2), -2};
650
651     case BS_1PIXEL:
652         return (Rect){1, 1, -2, -2};
653
654     case BS_NONE:
655         return (Rect){0, 0, 0, 0};
656
657     default:
658         assert(false);
659     }
660 }
661
662 /*
663  * Use this function to get a container’s border style. This is important
664  * because when inside a stack, the border style is always BS_NORMAL.
665  * For tabbed mode, the same applies, with one exception: when the container is
666  * borderless and the only element in the tabbed container, the border is not
667  * rendered.
668  *
669  */
670 int con_border_style(Con *con) {
671     Con *fs = con_get_fullscreen_con(con->parent);
672     if (fs == con) {
673         DLOG("this one is fullscreen! overriding BS_NONE\n");
674         return BS_NONE;
675     }
676
677     if (con->parent->layout == L_STACKED)
678         return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
679
680     if (con->parent->layout == L_TABBED && con->border_style != BS_NORMAL)
681         return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
682
683     return con->border_style;
684 }
685
686 /*
687  * This function changes the layout of a given container. Use it to handle
688  * special cases like changing a whole workspace to stacked/tabbed (creates a
689  * new split container before).
690  *
691  */
692 void con_set_layout(Con *con, int layout) {
693     /* When the container type is CT_WORKSPACE, the user wants to change the
694      * whole workspace into stacked/tabbed mode. To do this and still allow
695      * intuitive operations (like level-up and then opening a new window), we
696      * need to create a new split container. */
697     if (con->type == CT_WORKSPACE) {
698         DLOG("Creating new split container\n");
699         /* 1: create a new split container */
700         Con *new = con_new(NULL);
701         new->parent = con;
702
703         /* 2: set the requested layout on the split con */
704         new->layout = layout;
705
706         /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
707          * to be set. Otherwise, this con will not be interpreted as a split
708          * container. */
709         new->orientation = HORIZ;
710
711         Con *old_focused = TAILQ_FIRST(&(con->focus_head));
712         if (old_focused == TAILQ_END(&(con->focus_head)))
713             old_focused = NULL;
714
715         /* 4: move the existing cons of this workspace below the new con */
716         DLOG("Moving cons\n");
717         Con *child;
718         while (!TAILQ_EMPTY(&(con->nodes_head))) {
719             child = TAILQ_FIRST(&(con->nodes_head));
720             con_detach(child);
721             con_attach(child, new, true);
722         }
723
724         /* 4: attach the new split container to the workspace */
725         DLOG("Attaching new split to ws\n");
726         con_attach(new, con, false);
727
728         if (old_focused)
729             con_focus(old_focused);
730
731         tree_flatten(croot);
732
733         return;
734     }
735
736     con->layout = layout;
737 }