]> icculus.org git repositories - dana/openbox.git/blob - openbox/stacking.c
new stacking code that doesnt suck ass to look at, woot
[dana/openbox.git] / openbox / stacking.c
1 #include "openbox.h"
2 #include "prop.h"
3 #include "focus.h"
4 #include "client.h"
5 #include "group.h"
6 #include "frame.h"
7 #include "window.h"
8 #include <glib.h>
9
10 GList  *stacking_list = NULL;
11
12 void stacking_set_list()
13 {
14     Window *windows, *win_it;
15     GList *it;
16     guint size = g_list_length(stacking_list);
17
18     /* on shutdown, don't update the properties, so that we can read it back
19        in on startup and re-stack the windows as they were before we shut down
20     */
21     if (ob_state == State_Exiting) return;
22
23     /* create an array of the window ids (from bottom to top,
24        reverse order!) */
25     if (size > 0) {
26         windows = g_new(Window, size);
27         win_it = windows;
28         for (it = g_list_last(stacking_list); it != NULL;
29              it = it->prev)
30             if (WINDOW_IS_CLIENT(it->data)) {
31                 *win_it = WINDOW_AS_CLIENT(it->data)->window;
32                 ++win_it;
33             }
34     } else
35         windows = win_it = NULL;
36
37     PROP_SETA32(ob_root, net_client_list_stacking, window,
38                 (guint32*)windows, win_it - windows);
39
40     if (windows)
41         g_free(windows);
42 }
43
44 static void do_restack(GList *wins, GList *before)
45 {
46     GList *it, *next;
47     Window *win;
48     int i;
49
50     /* pls only restack stuff in the same layer at a time */
51     for (it = wins; it; it = next) {
52         next = g_list_next(it);
53         if (!next) break;
54         g_assert (window_layer(it->data) == window_layer(next->data));
55     }
56
57
58     win = g_new(Window, g_list_length(wins) + 1);
59
60     if (before == stacking_list)
61         win[0] = focus_backup;
62     else if (!before)
63         win[0] = window_top(g_list_last(stacking_list)->data);
64     else
65         win[0] = window_top(g_list_previous(before)->data);
66
67     for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
68         win[i] = window_top(it->data);
69         stacking_list = g_list_insert_before(stacking_list, before, it->data);
70     }
71
72     XRestackWindows(ob_display, win, i);
73     g_free(win);
74 }
75
76 static void raise(GList *wins)
77 {
78     GList *it;
79     GList *layer[NUM_STACKLAYER] = {NULL};
80     int i;
81
82     for (it = wins; it; it = g_list_next(it)) {
83         StackLayer l;
84
85         l = window_layer(it->data);
86         layer[l] = g_list_append(layer[l], it->data);
87     }
88
89     it = stacking_list;
90     for (i = NUM_STACKLAYER - 1; i >= 0; --i) {
91         if (layer[i]) {
92             for (; it; it = g_list_next(it)) {
93                 /* look for the top of the layer */
94                 if (window_layer(it->data) <= (StackLayer) i)
95                     break;
96             }
97             do_restack(layer[i], it);
98             g_list_free(layer[i]);
99         }
100     }
101 }
102
103 static void lower(GList *wins)
104 {
105     GList *it;
106     GList *layer[NUM_STACKLAYER] = {NULL};
107     int i;
108
109     for (it = wins; it; it = g_list_next(it)) {
110         StackLayer l;
111
112         l = window_layer(it->data);
113         layer[l] = g_list_append(layer[l], it->data);
114     }
115
116     it = stacking_list;
117     for (i = NUM_STACKLAYER - 1; i >= 0; --i) {
118         if (layer[i]) {
119             for (; it; it = g_list_next(it)) {
120                 /* look for the top of the next layer down */
121                 if (window_layer(it->data) < (StackLayer) i)
122                     break;
123             }
124             do_restack(layer[i], it);
125             g_list_free(layer[i]);
126         }
127     }
128 }
129
130 static GList* pick_windows(ObWindow *win)
131 {
132     GList *ret = NULL;
133     GList *it, *next;
134     GSList *sit;
135     Client *c;
136     int i, n;
137
138     if (!WINDOW_IS_CLIENT(win)) {
139         ret = g_list_append(ret, win);
140         stacking_list = g_list_remove(stacking_list, win);
141         return ret;
142     }
143     c = WINDOW_AS_CLIENT(win);
144
145     /* add transient children in their stacking order */
146     i = 0;
147     n = g_slist_length(c->transients);
148     for (it = stacking_list; i < n && it; it = next) {
149         next = g_list_next(it);
150         if ((sit = g_slist_find(c->transients, it->data))) {
151             ++i;
152             ret = g_list_concat(ret, pick_windows(sit->data));
153         }
154     }
155
156     /* add itself */
157     if (g_list_find(stacking_list, win)) {
158         ret = g_list_append(ret, win);
159         stacking_list = g_list_remove(stacking_list, win);
160     }
161
162     /* add group members in their stacking order */
163     if (c->group) {
164         for (it = stacking_list; it; it = next) {
165             next = g_list_next(it);
166             if ((sit = g_slist_find(c->group->members, it->data))) {
167                 ret = g_list_append(ret, sit->data);
168                 stacking_list = g_list_remove(stacking_list, sit->data);
169             }
170         }
171     }
172
173     if (c->transient_for && c->transient_for != TRAN_GROUP)
174         /* dont add it twice */
175         if (g_list_find(stacking_list, c->transient_for))
176             ret = g_list_concat(ret, pick_windows
177                                 (CLIENT_AS_WINDOW(c->transient_for)));
178
179     return ret;
180 }
181
182 void stacking_raise(ObWindow *window)
183 {
184     GList *wins;
185
186     wins = pick_windows(window);
187     raise(wins);
188 }
189
190 void stacking_lower(ObWindow *window)
191 {
192     GList *wins;
193
194     wins = pick_windows(window);
195     lower(wins);
196 }
197
198 void stacking_add(ObWindow *win)
199 {
200     StackLayer l;
201     GList *wins, *it;
202
203     l = window_layer(win);
204     wins = g_list_append(NULL, win); /* list of 1 element */
205
206     for (it = stacking_list; it; it = g_list_next(it))
207         if (window_layer(it->data) <= l)
208             break;
209     do_restack(wins, it);
210     g_list_free(wins);
211
212     stacking_raise(win);
213 }
214
215 void stacking_add_nonintrusive(ObWindow *win)
216 {
217     Client *client;
218     Client *parent = NULL;
219     GList *it_before = NULL;
220
221     if (!WINDOW_IS_CLIENT(win)) {
222         stacking_add(win); /* no special rules for others */
223         return;
224     }
225
226     client = WINDOW_AS_CLIENT(win);
227
228     /* insert above its highest parent */
229     if (client->transient_for) {
230         if (client->transient_for != TRAN_GROUP) {
231             parent = client->transient_for;
232         } else {
233             GSList *sit;
234             GList *it;
235
236             if (client->group)
237                 for (it = stacking_list; !parent && it; it = it->next) {
238                     if ((sit = g_slist_find(client->group->members, it->data)))
239                 for (sit = client->group->members; !parent && sit;
240                      sit = sit->next) {
241                     Client *c = sit->data;
242                     /* checking transient_for prevents infinate loops! */
243                     if (sit->data == it->data && !c->transient_for)
244                         parent = it->data;
245                 }
246             }
247         }
248     }
249
250     if (!(it_before = g_list_find(stacking_list, parent))) {
251         /* no parent to put above, try find the focused client to go
252            under */
253         if (focus_client && focus_client->layer == client->layer) {
254             if ((it_before = g_list_find(stacking_list, focus_client)))
255                 it_before = it_before->next;
256         }
257     }
258     if (!it_before) {
259         /* out of ideas, just add it normally... */
260         stacking_add(win);
261     } else {
262         GList *wins = g_list_append(NULL, win);
263         do_restack(wins, it_before);
264         g_list_free(wins);
265     }
266 }