]> git.sur5r.net Git - i3/i3/blob - src/con.c
00001d4c356f2da46c6fd9da9a75f03c1e15f57a
[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  * Returns true when this node is a leaf node (has no children)
79  *
80  */
81 bool con_is_leaf(Con *con) {
82     return TAILQ_EMPTY(&(con->nodes_head));
83 }
84
85 /*
86  * Returns true if this node accepts a window (if the node swallows windows,
87  * it might already have swallowed enough and cannot hold any more).
88  *
89  */
90 bool con_accepts_window(Con *con) {
91     /* 1: workspaces never accept direct windows */
92     if (con->parent->type == CT_OUTPUT)
93         return false;
94
95     /* TODO: if this is a swallowing container, we need to check its max_clients */
96     return (con->window == NULL);
97 }
98
99 /*
100  * Gets the output container (first container with CT_OUTPUT in hierarchy) this
101  * node is on.
102  *
103  */
104 Con *con_get_output(Con *con) {
105     Con *result = con;
106     while (result != NULL && result->type != CT_OUTPUT)
107         result = result->parent;
108     /* We must be able to get an output because focus can never be set higher
109      * in the tree (root node cannot be focused). */
110     assert(result != NULL);
111     return result;
112 }
113
114 /*
115  * Gets the workspace container this node is on.
116  *
117  */
118 Con *con_get_workspace(Con *con) {
119     Con *result = con;
120     while (result != NULL && result->parent->type != CT_OUTPUT)
121         result = result->parent;
122     assert(result != NULL);
123     return result;
124 }
125
126 /*
127  * Returns the first fullscreen node below this node.
128  *
129  */
130 Con *con_get_fullscreen_con(Con *con) {
131     Con *current;
132
133     LOG("looking for fullscreen node\n");
134     /* TODO: is breadth-first-search really appropriate? (check as soon as
135      * fullscreen levels and fullscreen for containers is implemented) */
136     Con **queue = NULL;
137     int queue_len = 0;
138     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
139         queue_len++;
140         queue = srealloc(queue, queue_len * sizeof(Con*));
141         queue[queue_len-1] = current;
142     }
143
144     while (queue_len > 0) {
145         current = queue[queue_len-1];
146         LOG("checking %p\n", current);
147         if (current->fullscreen_mode != CF_NONE) {
148             free(queue);
149             return current;
150         }
151         LOG("deleting from queue\n");
152         queue_len--;
153         queue = realloc(queue, queue_len * sizeof(Con*));
154     }
155
156     return NULL;
157 }
158
159 /*
160  * Returns true if the node is floating.
161  *
162  */
163 bool con_is_floating(Con *con) {
164     assert(con != NULL);
165     LOG("checking if con %p is floating\n", con);
166     return (con->floating >= FLOATING_AUTO_ON);
167 }
168
169 Con *con_by_window_id(xcb_window_t window) {
170     Con *con;
171     TAILQ_FOREACH(con, &all_cons, all_cons)
172         if (con->window != NULL && con->window->id == window)
173             return con;
174     return NULL;
175 }
176
177 Con *con_by_frame_id(xcb_window_t frame) {
178     Con *con;
179     TAILQ_FOREACH(con, &all_cons, all_cons)
180         if (con->frame == frame)
181             return con;
182     return NULL;
183 }
184
185 static bool match_matches_window(Match *match, i3Window *window) {
186     /* TODO: pcre, full matching, … */
187     if (match->class != NULL && strcasecmp(match->class, window->class) == 0) {
188         LOG("match made by window class (%s)\n", window->class);
189         return true;
190     }
191
192     if (match->id != XCB_NONE && window->id == match->id) {
193         LOG("match made by window id (%d)\n", window->id);
194         return true;
195     }
196
197     LOG("window %d (%s) could not be matched\n", window->id, window->class);
198
199     return false;
200 }
201
202 /*
203  * Returns the first container which wants to swallow this window
204  * TODO: priority
205  *
206  */
207 Con *con_for_window(i3Window *window, Match **store_match) {
208     Con *con;
209     Match *match;
210     LOG("searching con for window %p\n", window);
211     LOG("class == %s\n", window->class);
212
213     TAILQ_FOREACH(con, &all_cons, all_cons)
214         TAILQ_FOREACH(match, &(con->swallow_head), matches) {
215             if (!match_matches_window(match, window))
216                 continue;
217             if (store_match != NULL)
218                 *store_match = match;
219             return con;
220         }
221
222     return NULL;
223 }
224
225 /*
226  * Updates the percent attribute of the children of the given container. This
227  * function needs to be called when a window is added or removed from a
228  * container.
229  *
230  */
231 void con_fix_percent(Con *con, int action) {
232     Con *child;
233     int children = 0;
234     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
235         children++;
236     /* TODO: better document why this math works */
237     double fix;
238     if (action == WINDOW_ADD)
239         fix = (1.0 - (1.0 / (children+1)));
240     else
241         fix = 1.0 / (1.0 - (1.0 / (children+1)));
242
243     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
244         if (child->percent <= 0.0)
245             continue;
246         child->percent *= fix;
247     }
248 }