]> git.sur5r.net Git - i3/i3/blob - src/con.c
Always add to the focus list, fixes crash.
[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->name = strdup("");
38     static int cnt = 0;
39     LOG("opening window %d\n", cnt);
40
41     /* TODO: remove window coloring after test-phase */
42     LOG("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);
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  */
71 void con_attach(Con *con, Con *parent) {
72     con->parent = parent;
73     Con *loop;
74     Con *current = NULL;
75     struct nodes_head *nodes_head = &(parent->nodes_head);
76
77     /* Workspaces are handled differently: they need to be inserted at the
78      * right position. */
79     if (con->type == CT_WORKSPACE) {
80         DLOG("it's a workspace. num = %d\n", con->num);
81         if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
82             TAILQ_INSERT_TAIL(nodes_head, con, nodes);
83         } else {
84             current = TAILQ_FIRST(nodes_head);
85             if (con->num < current->num) {
86                 /* we need to insert the container at the beginning */
87                 TAILQ_INSERT_HEAD(nodes_head, con, nodes);
88             } else {
89                 while (current->num != -1 && con->num > current->num) {
90                     current = TAILQ_NEXT(current, nodes);
91                     if (current == TAILQ_END(nodes_head)) {
92                         current = NULL;
93                         break;
94                     }
95                 }
96                 /* we need to insert con after current, if current is not NULL */
97                 if (current)
98                     TAILQ_INSERT_BEFORE(current, con, nodes);
99                 else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
100             }
101         }
102         goto add_to_focus_head;
103     }
104
105     /* Get the first tiling container in focus stack */
106     TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
107         if (loop->type == CT_FLOATING_CON)
108             continue;
109         current = loop;
110         break;
111     }
112
113     /* Insert the container after the tiling container, if found */
114     if (current) {
115         DLOG("Inserting con = %p after last focused tiling con %p\n",
116              con, current);
117         TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
118     } else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
119
120 add_to_focus_head:
121     /* We insert to the TAIL because con_focus() will correct this.
122      * This way, we have the option to insert Cons without having
123      * to focus them. */
124     TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
125 }
126
127 /*
128  * Detaches the given container from its current parent
129  *
130  */
131 void con_detach(Con *con) {
132     if (con->type == CT_FLOATING_CON) {
133         TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
134         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
135     } else {
136         TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
137         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
138     }
139 }
140
141 /*
142  * Sets input focus to the given container. Will be updated in X11 in the next
143  * run of x_push_changes().
144  *
145  */
146 void con_focus(Con *con) {
147     assert(con != NULL);
148
149     /* 1: set focused-pointer to the new con */
150     /* 2: exchange the position of the container in focus stack of the parent all the way up */
151     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
152     TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
153     if (con->parent->parent != NULL)
154         con_focus(con->parent);
155
156     focused = con;
157     if (con->urgent) {
158         con->urgent = false;
159         workspace_update_urgent_flag(con_get_workspace(con));
160     }
161 }
162
163 /*
164  * Returns true when this node is a leaf node (has no children)
165  *
166  */
167 bool con_is_leaf(Con *con) {
168     return TAILQ_EMPTY(&(con->nodes_head));
169 }
170
171 /*
172  * Returns true if this node accepts a window (if the node swallows windows,
173  * it might already have swallowed enough and cannot hold any more).
174  *
175  */
176 bool con_accepts_window(Con *con) {
177     /* 1: workspaces never accept direct windows */
178     if (con->type == CT_WORKSPACE)
179         return false;
180
181     if (con->orientation != NO_ORIENTATION) {
182         DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
183         return false;
184     }
185
186     /* TODO: if this is a swallowing container, we need to check its max_clients */
187     return (con->window == NULL);
188 }
189
190 /*
191  * Gets the output container (first container with CT_OUTPUT in hierarchy) this
192  * node is on.
193  *
194  */
195 Con *con_get_output(Con *con) {
196     Con *result = con;
197     while (result != NULL && result->type != CT_OUTPUT)
198         result = result->parent;
199     /* We must be able to get an output because focus can never be set higher
200      * in the tree (root node cannot be focused). */
201     assert(result != NULL);
202     return result;
203 }
204
205 /*
206  * Gets the workspace container this node is on.
207  *
208  */
209 Con *con_get_workspace(Con *con) {
210     Con *result = con;
211     while (result != NULL && result->type != CT_WORKSPACE)
212         result = result->parent;
213     assert(result != NULL);
214     return result;
215 }
216
217 /*
218  * helper data structure for the breadth-first-search in
219  * con_get_fullscreen_con()
220  *
221  */
222 struct bfs_entry {
223     Con *con;
224
225     TAILQ_ENTRY(bfs_entry) entries;
226 };
227
228 /*
229  * Returns the first fullscreen node below this node.
230  *
231  */
232 Con *con_get_fullscreen_con(Con *con) {
233     Con *current, *child;
234
235     LOG("looking for fullscreen node\n");
236     /* TODO: is breadth-first-search really appropriate? (check as soon as
237      * fullscreen levels and fullscreen for containers is implemented) */
238     TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
239     struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
240     entry->con = con;
241     TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
242
243     while (!TAILQ_EMPTY(&bfs_head)) {
244         entry = TAILQ_FIRST(&bfs_head);
245         current = entry->con;
246         LOG("checking %p\n", current);
247         if (current != con && current->fullscreen_mode != CF_NONE) {
248             /* empty the queue */
249             while (!TAILQ_EMPTY(&bfs_head)) {
250                 entry = TAILQ_FIRST(&bfs_head);
251                 TAILQ_REMOVE(&bfs_head, entry, entries);
252                 free(entry);
253             }
254             return current;
255         }
256
257         LOG("deleting from queue\n");
258         TAILQ_REMOVE(&bfs_head, entry, entries);
259         free(entry);
260
261         TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
262             entry = smalloc(sizeof(struct bfs_entry));
263             entry->con = child;
264             TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
265         }
266     }
267
268     return NULL;
269 }
270
271 /*
272  * Returns true if the node is floating.
273  *
274  */
275 bool con_is_floating(Con *con) {
276     assert(con != NULL);
277     LOG("checking if con %p is floating\n", con);
278     return (con->floating >= FLOATING_AUTO_ON);
279 }
280
281 /*
282  * Returns the container with the given client window ID or NULL if no such
283  * container exists.
284  *
285  */
286 Con *con_by_window_id(xcb_window_t window) {
287     Con *con;
288     TAILQ_FOREACH(con, &all_cons, all_cons)
289         if (con->window != NULL && con->window->id == window)
290             return con;
291     return NULL;
292 }
293
294 /*
295  * Returns the container with the given frame ID or NULL if no such container
296  * exists.
297  *
298  */
299 Con *con_by_frame_id(xcb_window_t frame) {
300     Con *con;
301     TAILQ_FOREACH(con, &all_cons, all_cons)
302         if (con->frame == frame)
303             return con;
304     return NULL;
305 }
306
307 /*
308  * Returns the first container which wants to swallow this window
309  * TODO: priority
310  *
311  */
312 Con *con_for_window(i3Window *window, Match **store_match) {
313     Con *con;
314     Match *match;
315     LOG("searching con for window %p\n", window);
316     LOG("class == %s\n", window->class_class);
317
318     TAILQ_FOREACH(con, &all_cons, all_cons)
319         TAILQ_FOREACH(match, &(con->swallow_head), matches) {
320             if (!match_matches_window(match, window))
321                 continue;
322             if (store_match != NULL)
323                 *store_match = match;
324             return con;
325         }
326
327     return NULL;
328 }
329
330 /*
331  * Returns the number of children of this container.
332  *
333  */
334 int con_num_children(Con *con) {
335     Con *child;
336     int children = 0;
337
338     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
339         children++;
340
341     return children;
342 }
343
344 /*
345  * Updates the percent attribute of the children of the given container. This
346  * function needs to be called when a window is added or removed from a
347  * container.
348  *
349  */
350 void con_fix_percent(Con *con, int action) {
351     Con *child;
352     int children = con_num_children(con);
353     /* TODO: better document why this math works */
354     double fix;
355     if (action == WINDOW_ADD)
356         fix = (1.0 - (1.0 / (children+1)));
357     else
358         fix = 1.0 / (1.0 - (1.0 / (children+1)));
359
360     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
361         if (child->percent <= 0.0)
362             continue;
363         child->percent *= fix;
364     }
365 }
366
367 /*
368  * Toggles fullscreen mode for the given container. Fullscreen mode will not be
369  * entered when there already is a fullscreen container on this workspace.
370  *
371  */
372 void con_toggle_fullscreen(Con *con) {
373     Con *workspace, *fullscreen;
374     LOG("toggling fullscreen for %p / %s\n", con, con->name);
375     if (con->fullscreen_mode == CF_NONE) {
376         /* 1: check if there already is a fullscreen con */
377         workspace = con_get_workspace(con);
378         if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
379             LOG("Not entering fullscreen mode, container (%p/%s) "
380                 "already is in fullscreen mode\n",
381                 fullscreen, fullscreen->name);
382             return;
383         }
384
385         /* 2: enable fullscreen */
386         con->fullscreen_mode = CF_OUTPUT;
387     } else {
388         /* 1: disable fullscreen */
389         con->fullscreen_mode = CF_NONE;
390     }
391     LOG("mode now: %d\n", con->fullscreen_mode);
392
393     /* update _NET_WM_STATE if this container has a window */
394     /* TODO: when a window is assigned to a container which is already
395      * fullscreened, this state needs to be pushed to the client, too */
396     if (con->window == NULL)
397         return;
398
399     uint32_t values[1];
400     unsigned int num = 0;
401
402     if (con->fullscreen_mode != CF_NONE)
403         values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
404
405     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
406                         atoms[_NET_WM_STATE], ATOM, 32, num, values);
407 }
408
409 /*
410  * Moves the given container to the currently focused container on the given
411  * workspace.
412  * TODO: is there a better place for this function?
413  *
414  */
415 void con_move_to_workspace(Con *con, Con *workspace) {
416     /* 1: save the container which is going to be focused after the current
417      * container is moved away */
418     Con *focus_next = con_next_focused(con);
419
420     /* 2: get the focused container of this workspace by going down as far as
421      * possible */
422     Con *next = workspace;
423
424     while (!TAILQ_EMPTY(&(next->focus_head)))
425         next = TAILQ_FIRST(&(next->focus_head));
426
427     /* 3: we go up one level, but only when next is a normal container */
428     if (next->type != CT_WORKSPACE)
429         next = next->parent;
430
431     DLOG("Re-attaching container to %p / %s\n", next, next->name);
432     /* 4: re-attach the con to the parent of this focused container */
433     con_detach(con);
434     con_attach(con, next);
435
436     /* 5: keep focus on the current workspace */
437     con_focus(focus_next);
438 }
439
440 /*
441  * Returns the orientation of the given container (for stacked containers,
442  * vertical orientation is used regardless of the actual orientation of the
443  * container).
444  *
445  */
446 int con_orientation(Con *con) {
447     /* stacking containers behave like they are in vertical orientation */
448     if (con->layout == L_STACKED)
449         return VERT;
450
451     if (con->layout == L_TABBED)
452         return HORIZ;
453
454     return con->orientation;
455 }
456
457 /*
458  * Returns the container which will be focused next when the given container
459  * is not available anymore. Called in tree_close and con_move_to_workspace
460  * to properly restore focus.
461  *
462  */
463 Con *con_next_focused(Con *con) {
464     Con *next;
465     /* floating containers are attached to a workspace, so we focus either the
466      * next floating container (if any) or the workspace itself. */
467     if (con->type == CT_FLOATING_CON) {
468         DLOG("selecting next for CT_FLOATING_CON\n");
469         next = TAILQ_NEXT(con, floating_windows);
470         if (next == TAILQ_END(&(parent->floating_head))) {
471             Con *ws = con_get_workspace(con);
472             next = ws;
473             DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
474             while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
475                 next = TAILQ_FIRST(&(next->focus_head));
476                 if (next == con) {
477                     DLOG("skipping container itself, we want the next client\n");
478                     next = TAILQ_NEXT(next, focused);
479                 }
480             }
481             if (next == TAILQ_END(&(ws->focus_head))) {
482                 DLOG("Focus list empty, returning ws\n");
483                 next = ws;
484             }
485         }
486         return next;
487     }
488
489     /* try to focus the next container on the same level as this one */
490     next = TAILQ_NEXT(con, focused);
491
492     /* if that was not possible, go up to its parent */
493     if (next == TAILQ_END(&(parent->nodes_head)))
494         next = con->parent;
495
496     /* now go down the focus stack as far as
497      * possible, excluding the current container */
498     while (!TAILQ_EMPTY(&(next->focus_head)) &&
499            TAILQ_FIRST(&(next->focus_head)) != con)
500         next = TAILQ_FIRST(&(next->focus_head));
501
502     return next;
503 }
504
505 /*
506  * Returns a "relative" Rect which contains the amount of pixels that need to
507  * be added to the original Rect to get the final position (obviously the
508  * amount of pixels for normal, 1pixel and borderless are different).
509  *
510  */
511 Rect con_border_style_rect(Con *con) {
512     switch (con_border_style(con)) {
513     case BS_NORMAL:
514         return (Rect){2, 0, -(2 * 2), -2};
515
516     case BS_1PIXEL:
517         return (Rect){1, 1, -2, -2};
518
519     case BS_NONE:
520         return (Rect){0, 0, 0, 0};
521
522     default:
523         assert(false);
524     }
525 }
526
527 /*
528  * Use this function to get a container’s border style. This is important
529  * because when inside a stack, the border style is always BS_NORMAL.
530  * For tabbed mode, the same applies, with one exception: when the container is
531  * borderless and the only element in the tabbed container, the border is not
532  * rendered.
533  *
534  */
535 int con_border_style(Con *con) {
536     Con *fs = con_get_fullscreen_con(con->parent);
537     if (fs == con) {
538         DLOG("this one is fullscreen! overriding BS_NONE\n");
539         return BS_NONE;
540     }
541
542     if (con->parent->layout == L_STACKED)
543         return BS_NORMAL;
544
545     return con->border_style;
546 }
547
548 /*
549  * This function changes the layout of a given container. Use it to handle
550  * special cases like changing a whole workspace to stacked/tabbed (creates a
551  * new split container before).
552  *
553  */
554 void con_set_layout(Con *con, int layout) {
555     /* When the container type is CT_WORKSPACE, the user wants to change the
556      * whole workspace into stacked/tabbed mode. To do this and still allow
557      * intuitive operations (like level-up and then opening a new window), we
558      * need to create a new split container. */
559     if (con->type == CT_WORKSPACE) {
560         DLOG("Creating new split container\n");
561         /* 1: create a new split container */
562         Con *new = con_new(NULL);
563         new->parent = con;
564
565         /* 2: set the requested layout on the split con */
566         new->layout = layout;
567
568         /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
569          * to be set. Otherwise, this con will not be interpreted as a split
570          * container. */
571         new->orientation = HORIZ;
572
573         /* 4: move the existing cons of this workspace below the new con */
574         DLOG("Moving cons\n");
575         Con *child;
576         while (!TAILQ_EMPTY(&(con->nodes_head))) {
577             child = TAILQ_FIRST(&(con->nodes_head));
578             con_detach(child);
579             con_attach(child, new);
580         }
581
582         /* 4: attach the new split container to the workspace */
583         DLOG("Attaching new split to ws\n");
584         con_attach(new, con);
585
586         return;
587     }
588
589     con->layout = layout;
590 }