]> Dogcows Code - chaz/openbox/blob - openbox/stacking.c
add stacking_below which moves a window to immediately below another window. use...
[chaz/openbox] / openbox / stacking.c
1 #include "openbox.h"
2 #include "prop.h"
3 #include "screen.h"
4 #include "focus.h"
5 #include "client.h"
6 #include "group.h"
7 #include "frame.h"
8 #include "window.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() == OB_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(RootWindow(ob_display, ob_screen),
34 net_client_list_stacking, window, (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 #ifdef DEBUG
46 /* pls only restack stuff in the same layer at a time */
47 for (it = wins; it; it = next) {
48 next = g_list_next(it);
49 if (!next) break;
50 g_assert (window_layer(it->data) == window_layer(next->data));
51 }
52 if (before)
53 g_assert(window_layer(it->data) >= window_layer(before->data));
54 #endif
55
56 win = g_new(Window, g_list_length(wins) + 1);
57
58 if (before == stacking_list)
59 win[0] = screen_support_win;
60 else if (!before)
61 win[0] = window_top(g_list_last(stacking_list)->data);
62 else
63 win[0] = window_top(g_list_previous(before)->data);
64
65 for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
66 win[i] = window_top(it->data);
67 g_assert(win[i] != None); /* better not call stacking shit before
68 setting your top level window value */
69 stacking_list = g_list_insert_before(stacking_list, before, it->data);
70 }
71
72 #ifdef DEBUG
73 /* some debug checking of the stacking list's order */
74 for (it = stacking_list; ; it = next) {
75 next = g_list_next(it);
76 if (!next) break;
77 g_assert(window_layer(it->data) >= window_layer(next->data));
78 }
79 #endif
80
81 XRestackWindows(ob_display, win, i);
82 g_free(win);
83
84 stacking_set_list();
85 }
86
87 static void do_raise(GList *wins)
88 {
89 GList *it;
90 GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
91 int i;
92
93 for (it = wins; it; it = g_list_next(it)) {
94 ObStackingLayer l;
95
96 l = window_layer(it->data);
97 layer[l] = g_list_append(layer[l], it->data);
98 }
99
100 it = stacking_list;
101 for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
102 if (layer[i]) {
103 for (; it; it = g_list_next(it)) {
104 /* look for the top of the layer */
105 if (window_layer(it->data) <= (ObStackingLayer) i)
106 break;
107 }
108 do_restack(layer[i], it);
109 g_list_free(layer[i]);
110 }
111 }
112 }
113
114 static void do_lower(GList *wins)
115 {
116 GList *it;
117 GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
118 int i;
119
120 for (it = wins; it; it = g_list_next(it)) {
121 ObStackingLayer l;
122
123 l = window_layer(it->data);
124 layer[l] = g_list_append(layer[l], it->data);
125 }
126
127 it = stacking_list;
128 for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
129 if (layer[i]) {
130 for (; it; it = g_list_next(it)) {
131 /* look for the top of the next layer down */
132 if (window_layer(it->data) < (ObStackingLayer) i)
133 break;
134 }
135 do_restack(layer[i], it);
136 g_list_free(layer[i]);
137 }
138 }
139 }
140
141 static GList *pick_windows(ObClient *top, ObClient *selected, gboolean raise)
142 {
143 GList *ret = NULL;
144 GList *it, *next, *prev;
145 GSList *sit;
146 int i, n;
147 GList *modals = NULL;
148 GList *trans = NULL;
149 GList *modal_sel = NULL; /* the selected guys if modal */
150 GList *trans_sel = NULL; /* the selected guys if not */
151
152 /* remove first so we can't run into ourself */
153 if ((it = g_list_find(stacking_list, top)))
154 stacking_list = g_list_delete_link(stacking_list, it);
155 else
156 return NULL;
157
158 i = 0;
159 n = g_slist_length(top->transients);
160 for (it = stacking_list; i < n && it; it = next) {
161 prev = g_list_previous(it);
162 next = g_list_next(it);
163
164 if ((sit = g_slist_find(top->transients, it->data))) {
165 ObClient *c = sit->data;
166 gboolean sel_child;
167
168 ++i;
169
170 if (c == selected)
171 sel_child = TRUE;
172 else
173 sel_child = client_search_transient(c, selected) != NULL;
174
175 if (!c->modal) {
176 if (!sel_child) {
177 trans = g_list_concat(trans,
178 pick_windows(c, selected, raise));
179 } else {
180 trans_sel = g_list_concat(trans_sel,
181 pick_windows(c, selected,
182 raise));
183 }
184 } else {
185 if (!sel_child) {
186 modals = g_list_concat(modals,
187 pick_windows(c, selected, raise));
188 } else {
189 modal_sel = g_list_concat(modal_sel,
190 pick_windows(c, selected,
191 raise));
192 }
193 }
194 /* if we dont have a prev then start back at the beginning,
195 otherwise skip back to the prev's next */
196 next = prev ? g_list_next(prev) : stacking_list;
197 }
198 }
199
200 ret = g_list_concat((raise ? modal_sel : modals),
201 (raise ? modals : modal_sel));
202
203 ret = g_list_concat(ret, (raise ? trans_sel : trans));
204 ret = g_list_concat(ret, (raise ? trans : trans_sel));
205
206
207 /* add itself */
208 ret = g_list_append(ret, top);
209
210 return ret;
211 }
212
213 static GList *pick_group_windows(ObClient *top, ObClient *selected,
214 gboolean raise)
215 {
216 GList *ret = NULL;
217 GList *it, *next, *prev;
218 GSList *sit;
219 int i, n;
220
221 /* add group members in their stacking order */
222 if (top->group) {
223 i = 0;
224 n = g_slist_length(top->group->members) - 1;
225 for (it = stacking_list; i < n && it; it = next) {
226 prev = g_list_previous(it);
227 next = g_list_next(it);
228
229 if ((sit = g_slist_find(top->group->members, it->data))) {
230 ++i;
231 ret = g_list_concat(ret,
232 pick_windows(sit->data, selected, raise));
233 /* if we dont have a prev then start back at the beginning,
234 otherwise skip back to the prev's next */
235 next = prev ? g_list_next(prev) : stacking_list;
236 }
237 }
238 }
239 return ret;
240 }
241
242 void stacking_raise(ObWindow *window)
243 {
244 GList *wins;
245
246 if (WINDOW_IS_CLIENT(window)) {
247 ObClient *c;
248 ObClient *selected;
249 selected = WINDOW_AS_CLIENT(window);
250 c = client_search_top_transient(selected);
251 wins = pick_windows(c, selected, TRUE);
252 /*wins = g_list_concat(wins, pick_group_windows(c, selected, TRUE));*/
253 } else {
254 wins = g_list_append(NULL, window);
255 stacking_list = g_list_remove(stacking_list, window);
256 }
257 do_raise(wins);
258 g_list_free(wins);
259 }
260
261 void stacking_lower(ObWindow *window)
262 {
263 GList *wins;
264
265 if (WINDOW_IS_CLIENT(window)) {
266 ObClient *c;
267 ObClient *selected;
268 selected = WINDOW_AS_CLIENT(window);
269 c = client_search_top_transient(selected);
270 wins = pick_windows(c, selected, FALSE);
271 /*wins = g_list_concat(pick_group_windows(c, selected, FALSE), wins);*/
272 } else {
273 wins = g_list_append(NULL, window);
274 stacking_list = g_list_remove(stacking_list, window);
275 }
276 do_lower(wins);
277 g_list_free(wins);
278 }
279
280 void stacking_below(ObWindow *window, ObWindow *below)
281 {
282 GList *wins, *before;
283
284 if (window_layer(window) != window_layer(below))
285 return;
286
287 wins = g_list_append(NULL, window);
288 stacking_list = g_list_remove(stacking_list, window);
289 before = g_list_next(g_list_find(stacking_list, below));
290 do_restack(wins, before);
291 g_list_free(wins);
292 }
293
294 void stacking_add(ObWindow *win)
295 {
296 ObStackingLayer l;
297 GList *wins;
298
299 g_assert(screen_support_win != None); /* make sure I dont break this in the
300 future */
301
302 l = window_layer(win);
303 wins = g_list_append(NULL, win); /* list of 1 element */
304
305 stacking_list = g_list_append(stacking_list, win);
306 stacking_raise(win);
307 }
308
309 void stacking_add_nonintrusive(ObWindow *win)
310 {
311 ObClient *client;
312 ObClient *parent = NULL;
313 GList *it_before = NULL;
314
315 if (!WINDOW_IS_CLIENT(win)) {
316 stacking_add(win); /* no special rules for others */
317 return;
318 }
319
320 client = WINDOW_AS_CLIENT(win);
321
322 /* insert above its highest parent */
323 if (client->transient_for) {
324 if (client->transient_for != OB_TRAN_GROUP) {
325 parent = client->transient_for;
326 } else {
327 GSList *sit;
328 GList *it;
329
330 if (client->group)
331 for (it = stacking_list; !parent && it; it = it->next) {
332 if ((sit = g_slist_find(client->group->members, it->data)))
333 for (sit = client->group->members; !parent && sit;
334 sit = sit->next) {
335 ObClient *c = sit->data;
336 /* checking transient_for prevents infinate loops! */
337 if (sit->data == it->data && !c->transient_for)
338 parent = it->data;
339 }
340 }
341 }
342 }
343
344 if (!(it_before = g_list_find(stacking_list, parent))) {
345 /* no parent to put above, try find the focused client to go
346 under */
347 if (focus_client && focus_client->layer == client->layer) {
348 if ((it_before = g_list_find(stacking_list, focus_client)))
349 it_before = it_before->next;
350 }
351 }
352 if (!it_before) {
353 /* out of ideas, just add it normally... */
354 stacking_add(win);
355 } else {
356 GList *wins = g_list_append(NULL, win);
357 do_restack(wins, it_before);
358 g_list_free(wins);
359 }
360 }
This page took 0.048328 seconds and 4 git commands to generate.