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