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