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