]> Dogcows Code - chaz/openbox/blob - openbox/stacking.c
2fa4b2e6d9dc8c83d15c2641193da527ccf9c6fa
[chaz/openbox] / 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 = NULL;
15 GList *it;
16 guint i = 0;
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 (stacking_list) {
26 windows = g_new(Window, g_list_length(stacking_list));
27 for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
28 if (WINDOW_IS_CLIENT(it->data))
29 windows[i++] = WINDOW_AS_CLIENT(it->data)->window;
30 }
31 }
32
33 PROP_SETA32(ob_root, net_client_list_stacking, window,
34 (guint32*)windows, i);
35
36 g_free(windows);
37 }
38
39 static void do_restack(GList *wins, GList *before)
40 {
41 GList *it, *next;
42 Window *win;
43 int i;
44
45 /* pls only restack stuff in the same layer at a time */
46 for (it = wins; it; it = next) {
47 next = g_list_next(it);
48 if (!next) break;
49 g_assert (window_layer(it->data) == window_layer(next->data));
50 }
51
52
53 win = g_new(Window, g_list_length(wins) + 1);
54
55 if (before == stacking_list)
56 win[0] = focus_backup;
57 else if (!before)
58 win[0] = window_top(g_list_last(stacking_list)->data);
59 else
60 win[0] = window_top(g_list_previous(before)->data);
61
62 for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
63 win[i] = window_top(it->data);
64 stacking_list = g_list_insert_before(stacking_list, before, it->data);
65 }
66
67 /* XXX some debug checking of the stacking list's order */
68 for (it = stacking_list; ; it = next) {
69 next = g_list_next(it);
70 if (!next) break;
71 g_assert(window_layer(it->data) >= window_layer(next->data));
72 }
73
74 XRestackWindows(ob_display, win, i);
75 g_free(win);
76
77 stacking_set_list();
78 }
79
80 static void raise(GList *wins)
81 {
82 GList *it;
83 GList *layer[NUM_STACKLAYER] = {NULL};
84 int i;
85
86 for (it = wins; it; it = g_list_next(it)) {
87 StackLayer l;
88
89 l = window_layer(it->data);
90 layer[l] = g_list_append(layer[l], it->data);
91 }
92
93 it = stacking_list;
94 for (i = NUM_STACKLAYER - 1; i >= 0; --i) {
95 if (layer[i]) {
96 for (; it; it = g_list_next(it)) {
97 /* look for the top of the layer */
98 if (window_layer(it->data) <= (StackLayer) i)
99 break;
100 }
101 do_restack(layer[i], it);
102 g_list_free(layer[i]);
103 }
104 }
105 }
106
107 static void lower(GList *wins)
108 {
109 GList *it;
110 GList *layer[NUM_STACKLAYER] = {NULL};
111 int i;
112
113 for (it = wins; it; it = g_list_next(it)) {
114 StackLayer l;
115
116 l = window_layer(it->data);
117 layer[l] = g_list_append(layer[l], it->data);
118 }
119
120 it = stacking_list;
121 for (i = NUM_STACKLAYER - 1; i >= 0; --i) {
122 if (layer[i]) {
123 for (; it; it = g_list_next(it)) {
124 /* look for the top of the next layer down */
125 if (window_layer(it->data) < (StackLayer) i)
126 break;
127 }
128 do_restack(layer[i], it);
129 g_list_free(layer[i]);
130 }
131 }
132 }
133
134 static GList *pick_windows(ObWindow *win)
135 {
136 GList *ret = NULL;
137 GList *it, *next;
138 GSList *sit;
139 Client *c;
140 int i, n;
141
142 if (!WINDOW_IS_CLIENT(win)) {
143 ret = g_list_append(ret, win);
144 stacking_list = g_list_remove(stacking_list, win);
145 return ret;
146 }
147
148 c = WINDOW_AS_CLIENT(win);
149
150 /* remove first so we can't run into ourself */
151 stacking_list = g_list_remove(stacking_list, win);
152
153 /* add transient children in their stacking order */
154 i = 0;
155 n = g_slist_length(c->transients);
156 for (it = stacking_list; i < n && it; it = next) {
157 next = g_list_next(it);
158 if ((sit = g_slist_find(c->transients, it->data))) {
159 ++i;
160 ret = g_list_concat(ret, pick_windows(sit->data));
161 it = stacking_list;
162 }
163 }
164
165 /* add itself */
166 ret = g_list_append(ret, win);
167
168 return ret;
169 }
170
171 static GList *pick_group_windows(ObWindow *win)
172 {
173 GList *ret = NULL;
174 GList *it, *next;
175 GSList *sit;
176 Client *c;
177 int i, n;
178
179 if (!WINDOW_IS_CLIENT(win))
180 return NULL;
181
182 c = WINDOW_AS_CLIENT(win);
183
184 /* add group members in their stacking order */
185 if (c->group) {
186 i = 0;
187 n = g_slist_length(c->group->members) - 1;
188 for (it = stacking_list; i < n && it; it = next) {
189 next = g_list_next(it);
190 if ((sit = g_slist_find(c->group->members, it->data))) {
191 ++i;
192 ret = g_list_concat(ret, pick_windows(sit->data));
193 it = stacking_list;
194 }
195 }
196 }
197 return ret;
198 }
199
200 static ObWindow *top_transient(ObWindow *window)
201 {
202 Client *client;
203
204 if (!WINDOW_IS_CLIENT(window))
205 return window;
206
207 client = WINDOW_AS_CLIENT(window);
208
209 /* move up the transient chain as far as possible */
210 if (client->transient_for) {
211 if (client->transient_for != TRAN_GROUP) {
212 return top_transient(CLIENT_AS_WINDOW(client->transient_for));
213 } else {
214 GSList *it;
215
216 for (it = client->group->members; it; it = it->next) {
217 Client *c = it->data;
218
219 /* checking transient_for prevents infinate loops! */
220 if (c != client && !c->transient_for)
221 break;
222 }
223 if (it)
224 return it->data;
225 }
226 }
227
228 return window;
229 }
230
231 void stacking_raise(ObWindow *window)
232 {
233 GList *wins;
234
235 window = top_transient(window);
236 wins = pick_windows(window);
237 wins = g_list_concat(wins, pick_group_windows(window));
238 raise(wins);
239 g_list_free(wins);
240 }
241
242 void stacking_lower(ObWindow *window)
243 {
244 GList *wins;
245
246 window = top_transient(window);
247 wins = pick_windows(window);
248 wins = g_list_concat(pick_group_windows(window), wins);
249 lower(wins);
250 g_list_free(wins);
251 }
252
253 void stacking_add(ObWindow *win)
254 {
255 StackLayer l;
256 GList *wins, *it;
257
258 l = window_layer(win);
259 wins = g_list_append(NULL, win); /* list of 1 element */
260
261 stacking_list = g_list_append(stacking_list, win);
262 stacking_raise(win);
263 }
264
265 void stacking_add_nonintrusive(ObWindow *win)
266 {
267 Client *client;
268 Client *parent = NULL;
269 GList *it_before = NULL;
270
271 if (!WINDOW_IS_CLIENT(win)) {
272 stacking_add(win); /* no special rules for others */
273 return;
274 }
275
276 client = WINDOW_AS_CLIENT(win);
277
278 /* insert above its highest parent */
279 if (client->transient_for) {
280 if (client->transient_for != TRAN_GROUP) {
281 parent = client->transient_for;
282 } else {
283 GSList *sit;
284 GList *it;
285
286 if (client->group)
287 for (it = stacking_list; !parent && it; it = it->next) {
288 if ((sit = g_slist_find(client->group->members, it->data)))
289 for (sit = client->group->members; !parent && sit;
290 sit = sit->next) {
291 Client *c = sit->data;
292 /* checking transient_for prevents infinate loops! */
293 if (sit->data == it->data && !c->transient_for)
294 parent = it->data;
295 }
296 }
297 }
298 }
299
300 if (!(it_before = g_list_find(stacking_list, parent))) {
301 /* no parent to put above, try find the focused client to go
302 under */
303 if (focus_client && focus_client->layer == client->layer) {
304 if ((it_before = g_list_find(stacking_list, focus_client)))
305 it_before = it_before->next;
306 }
307 }
308 if (!it_before) {
309 /* out of ideas, just add it normally... */
310 stacking_add(win);
311 } else {
312 GList *wins = g_list_append(NULL, win);
313 do_restack(wins, it_before);
314 g_list_free(wins);
315 }
316 }
This page took 0.043962 seconds and 3 git commands to generate.