]> git.sur5r.net Git - i3/i3/blob - src/con.c
1d8ca82e587b5667154adae8551975c460b6a225
[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 Con *con_new(Con *parent) {
29     Con *new = scalloc(sizeof(Con));
30     TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
31     new->type = CT_CON;
32     new->name = strdup("");
33     static int cnt = 0;
34     LOG("opening window %d\n", cnt);
35
36     /* TODO: remove window coloring after test-phase */
37     LOG("color %s\n", colors[cnt]);
38     new->name = strdup(colors[cnt]);
39     uint32_t cp = get_colorpixel(colors[cnt]);
40     cnt++;
41     if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
42         cnt = 0;
43
44     x_con_init(new);
45
46     xcb_change_window_attributes(conn, new->frame, XCB_CW_BACK_PIXEL, &cp);
47
48     TAILQ_INIT(&(new->floating_head));
49     TAILQ_INIT(&(new->nodes_head));
50     TAILQ_INIT(&(new->focus_head));
51     TAILQ_INIT(&(new->swallow_head));
52
53     if (parent != NULL)
54         con_attach(new, parent);
55
56     return new;
57 }
58
59 void con_attach(Con *con, Con *parent) {
60     con->parent = parent;
61     Con *current = TAILQ_FIRST(&(parent->focus_head));
62
63     if (current == TAILQ_END(&(parent->focus_head)))
64         TAILQ_INSERT_TAIL(&(parent->nodes_head), con, nodes);
65     else {
66         DLOG("inserting after\n");
67         TAILQ_INSERT_AFTER(&(parent->nodes_head), current, con, nodes);
68     }
69     /* We insert to the TAIL because con_focus() will correct this.
70      * This way, we have the option to insert Cons without having
71      * to focus them. */
72     TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
73 }
74
75 void con_detach(Con *con) {
76     if (con->type == CT_FLOATING_CON) {
77         TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
78         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
79     } else {
80         TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
81         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
82     }
83 }
84
85 /*
86  * Sets input focus to the given container. Will be updated in X11 in the next
87  * run of x_push_changes().
88  *
89  */
90 void con_focus(Con *con) {
91     assert(con != NULL);
92
93     /* 1: set focused-pointer to the new con */
94     /* 2: exchange the position of the container in focus stack of the parent all the way up */
95     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
96     TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
97     if (con->parent->parent != NULL)
98         con_focus(con->parent);
99
100     focused = con;
101     if (con->urgent) {
102         con->urgent = false;
103         workspace_update_urgent_flag(con_get_workspace(con));
104     }
105 }
106
107 /*
108  * Returns true when this node is a leaf node (has no children)
109  *
110  */
111 bool con_is_leaf(Con *con) {
112     return TAILQ_EMPTY(&(con->nodes_head));
113 }
114
115 /*
116  * Returns true if this node accepts a window (if the node swallows windows,
117  * it might already have swallowed enough and cannot hold any more).
118  *
119  */
120 bool con_accepts_window(Con *con) {
121     /* 1: workspaces never accept direct windows */
122     if (con->type == CT_WORKSPACE)
123         return false;
124
125     if (con->orientation != NO_ORIENTATION) {
126         DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
127         return false;
128     }
129
130     /* TODO: if this is a swallowing container, we need to check its max_clients */
131     return (con->window == NULL);
132 }
133
134 /*
135  * Gets the output container (first container with CT_OUTPUT in hierarchy) this
136  * node is on.
137  *
138  */
139 Con *con_get_output(Con *con) {
140     Con *result = con;
141     while (result != NULL && result->type != CT_OUTPUT)
142         result = result->parent;
143     /* We must be able to get an output because focus can never be set higher
144      * in the tree (root node cannot be focused). */
145     assert(result != NULL);
146     return result;
147 }
148
149 /*
150  * Gets the workspace container this node is on.
151  *
152  */
153 Con *con_get_workspace(Con *con) {
154     Con *result = con;
155     while (result != NULL && result->type != CT_WORKSPACE)
156         result = result->parent;
157     assert(result != NULL);
158     return result;
159 }
160
161 /*
162  * helper data structure for the breadth-first-search in
163  * con_get_fullscreen_con()
164  *
165  */
166 struct bfs_entry {
167     Con *con;
168
169     TAILQ_ENTRY(bfs_entry) entries;
170 };
171
172 /*
173  * Returns the first fullscreen node below this node.
174  *
175  */
176 Con *con_get_fullscreen_con(Con *con) {
177     Con *current, *child;
178
179     LOG("looking for fullscreen node\n");
180     /* TODO: is breadth-first-search really appropriate? (check as soon as
181      * fullscreen levels and fullscreen for containers is implemented) */
182     TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
183     struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
184     entry->con = con;
185     TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
186
187     while (!TAILQ_EMPTY(&bfs_head)) {
188         entry = TAILQ_FIRST(&bfs_head);
189         current = entry->con;
190         LOG("checking %p\n", current);
191         if (current != con && current->fullscreen_mode != CF_NONE) {
192             /* empty the queue */
193             while (!TAILQ_EMPTY(&bfs_head)) {
194                 entry = TAILQ_FIRST(&bfs_head);
195                 TAILQ_REMOVE(&bfs_head, entry, entries);
196                 free(entry);
197             }
198             return current;
199         }
200
201         LOG("deleting from queue\n");
202         TAILQ_REMOVE(&bfs_head, entry, entries);
203         free(entry);
204
205         TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
206             entry = smalloc(sizeof(struct bfs_entry));
207             entry->con = child;
208             TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
209         }
210     }
211
212     return NULL;
213 }
214
215 /*
216  * Returns true if the node is floating.
217  *
218  */
219 bool con_is_floating(Con *con) {
220     assert(con != NULL);
221     LOG("checking if con %p is floating\n", con);
222     return (con->floating >= FLOATING_AUTO_ON);
223 }
224
225 Con *con_by_window_id(xcb_window_t window) {
226     Con *con;
227     TAILQ_FOREACH(con, &all_cons, all_cons)
228         if (con->window != NULL && con->window->id == window)
229             return con;
230     return NULL;
231 }
232
233 Con *con_by_frame_id(xcb_window_t frame) {
234     Con *con;
235     TAILQ_FOREACH(con, &all_cons, all_cons)
236         if (con->frame == frame)
237             return con;
238     return NULL;
239 }
240
241 /*
242  * Returns the first container which wants to swallow this window
243  * TODO: priority
244  *
245  */
246 Con *con_for_window(i3Window *window, Match **store_match) {
247     Con *con;
248     Match *match;
249     LOG("searching con for window %p\n", window);
250     LOG("class == %s\n", window->class_class);
251
252     TAILQ_FOREACH(con, &all_cons, all_cons)
253         TAILQ_FOREACH(match, &(con->swallow_head), matches) {
254             if (!match_matches_window(match, window))
255                 continue;
256             if (store_match != NULL)
257                 *store_match = match;
258             return con;
259         }
260
261     return NULL;
262 }
263
264 /*
265  * Updates the percent attribute of the children of the given container. This
266  * function needs to be called when a window is added or removed from a
267  * container.
268  *
269  */
270 void con_fix_percent(Con *con, int action) {
271     Con *child;
272     int children = 0;
273     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
274         children++;
275     /* TODO: better document why this math works */
276     double fix;
277     if (action == WINDOW_ADD)
278         fix = (1.0 - (1.0 / (children+1)));
279     else
280         fix = 1.0 / (1.0 - (1.0 / (children+1)));
281
282     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
283         if (child->percent <= 0.0)
284             continue;
285         child->percent *= fix;
286     }
287 }
288
289 void con_toggle_fullscreen(Con *con) {
290     Con *workspace, *fullscreen;
291     LOG("toggling fullscreen for %p / %s\n", con, con->name);
292     if (con->fullscreen_mode == CF_NONE) {
293         /* 1: check if there already is a fullscreen con */
294         workspace = con_get_workspace(con);
295         if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
296             LOG("Not entering fullscreen mode, container (%p/%s) "
297                 "already is in fullscreen mode\n",
298                 fullscreen, fullscreen->name);
299             return;
300         }
301
302         /* 2: enable fullscreen */
303         con->fullscreen_mode = CF_OUTPUT;
304     } else {
305         /* 1: disable fullscreen */
306         con->fullscreen_mode = CF_NONE;
307     }
308     LOG("mode now: %d\n", con->fullscreen_mode);
309
310     /* update _NET_WM_STATE if this container has a window */
311     /* TODO: when a window is assigned to a container which is already
312      * fullscreened, this state needs to be pushed to the client, too */
313     if (con->window == NULL)
314         return;
315
316     uint32_t values[1];
317     unsigned int num = 0;
318
319     if (con->fullscreen_mode != CF_NONE)
320         values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
321
322     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
323                         atoms[_NET_WM_STATE], ATOM, 32, num, values);
324 }
325
326 /*
327  * TODO: is there a better place for this function?
328  *
329  */
330 void con_move_to_workspace(Con *con, Con *workspace) {
331     /* 1: get the focused container of this workspace by going down as far as
332      * possible */
333     Con *next = workspace;
334
335     while (!TAILQ_EMPTY(&(next->focus_head)))
336         next = TAILQ_FIRST(&(next->focus_head));
337
338     /* 2: we go up one level, but only when next is a normal container */
339     if (next->type != CT_WORKSPACE)
340         next = next->parent;
341
342     DLOG("Re-attaching container to %p / %s\n", next, next->name);
343     /* 3: re-attach the con to the parent of this focused container */
344     con_detach(con);
345     con_attach(con, next);
346 }