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