]> Dogcows Code - chaz/openbox/blob - openbox/stacking.c
stacking has been made more reliable with groups and group transients.
[chaz/openbox] / openbox / stacking.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 stacking.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "openbox.h"
21 #include "prop.h"
22 #include "screen.h"
23 #include "focus.h"
24 #include "client.h"
25 #include "group.h"
26 #include "frame.h"
27 #include "window.h"
28
29 GList *stacking_list = NULL;
30
31 void stacking_set_list()
32 {
33 Window *windows = NULL;
34 GList *it;
35 guint i = 0;
36
37 /* on shutdown, don't update the properties, so that we can read it back
38 in on startup and re-stack the windows as they were before we shut down
39 */
40 if (ob_state() == OB_STATE_EXITING) return;
41
42 /* create an array of the window ids (from bottom to top,
43 reverse order!) */
44 if (stacking_list) {
45 windows = g_new(Window, g_list_length(stacking_list));
46 for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
47 if (WINDOW_IS_CLIENT(it->data))
48 windows[i++] = WINDOW_AS_CLIENT(it->data)->window;
49 }
50 }
51
52 PROP_SETA32(RootWindow(ob_display, ob_screen),
53 net_client_list_stacking, window, (gulong*)windows, i);
54
55 g_free(windows);
56 }
57
58 static void do_restack(GList *wins, GList *before)
59 {
60 GList *it;
61 Window *win;
62 gint i;
63
64 #ifdef DEBUG
65 GList *next;
66 /* pls only restack stuff in the same layer at a time */
67 for (it = wins; it; it = next) {
68 next = g_list_next(it);
69 if (!next) break;
70 g_assert (window_layer(it->data) == window_layer(next->data));
71 }
72 if (before)
73 g_assert(window_layer(it->data) >= window_layer(before->data));
74 #endif
75
76 win = g_new(Window, g_list_length(wins) + 1);
77
78 if (before == stacking_list)
79 win[0] = screen_support_win;
80 else if (!before)
81 win[0] = window_top(g_list_last(stacking_list)->data);
82 else
83 win[0] = window_top(g_list_previous(before)->data);
84
85 for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
86 win[i] = window_top(it->data);
87 g_assert(win[i] != None); /* better not call stacking shit before
88 setting your top level window value */
89 stacking_list = g_list_insert_before(stacking_list, before, it->data);
90 }
91
92 #ifdef DEBUG
93 /* some debug checking of the stacking list's order */
94 for (it = stacking_list; ; it = next) {
95 next = g_list_next(it);
96 if (!next) break;
97 g_assert(window_layer(it->data) >= window_layer(next->data));
98 }
99 #endif
100
101 XRestackWindows(ob_display, win, i);
102 g_free(win);
103
104 stacking_set_list();
105 }
106
107 static void do_raise(GList *wins)
108 {
109 GList *it;
110 GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
111 gint i;
112
113 for (it = wins; it; it = g_list_next(it)) {
114 ObStackingLayer 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 = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
122 if (layer[i]) {
123 for (; it; it = g_list_next(it)) {
124 /* look for the top of the layer */
125 if (window_layer(it->data) <= (ObStackingLayer) i)
126 break;
127 }
128 do_restack(layer[i], it);
129 g_list_free(layer[i]);
130 }
131 }
132 }
133
134 static void do_lower(GList *wins)
135 {
136 GList *it;
137 GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
138 gint i;
139
140 for (it = wins; it; it = g_list_next(it)) {
141 ObStackingLayer l;
142
143 l = window_layer(it->data);
144 layer[l] = g_list_append(layer[l], it->data);
145 }
146
147 it = stacking_list;
148 for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
149 if (layer[i]) {
150 for (; it; it = g_list_next(it)) {
151 /* look for the top of the next layer down */
152 if (window_layer(it->data) < (ObStackingLayer) i)
153 break;
154 }
155 do_restack(layer[i], it);
156 g_list_free(layer[i]);
157 }
158 }
159 }
160
161 static GList *pick_windows_recur(ObClient *top, ObClient *selected,
162 gboolean raise)
163 {
164 GList *ret = NULL;
165 GList *it, *next, *prev;
166 GSList *sit;
167 gint i, n;
168 GList *modals = NULL;
169 GList *trans = NULL;
170 GList *modal_sel = NULL; /* the selected guys if modal */
171 GList *trans_sel = NULL; /* the selected guys if not */
172
173 /* remove first so we can't run into ourself */
174 if ((it = g_list_find(stacking_list, top)))
175 stacking_list = g_list_delete_link(stacking_list, it);
176 else
177 return NULL;
178
179 i = 0;
180 n = g_slist_length(top->transients);
181 for (it = stacking_list; i < n && it; it = next) {
182 prev = g_list_previous(it);
183 next = g_list_next(it);
184
185 if ((sit = g_slist_find(top->transients, it->data))) {
186 ObClient *c = sit->data;
187 gboolean sel_child;
188
189 ++i;
190
191 if (c == selected)
192 sel_child = TRUE;
193 else
194 sel_child = client_search_transient(c, selected) != NULL;
195
196 if (!c->modal) {
197 if (!sel_child) {
198 trans = g_list_concat
199 (trans, pick_windows_recur(c, selected, raise));
200 } else {
201 trans_sel = g_list_concat
202 (trans_sel, pick_windows_recur(c, selected, raise));
203 }
204 } else {
205 if (!sel_child) {
206 modals = g_list_concat
207 (modals, pick_windows_recur(c, selected, raise));
208 } else {
209 modal_sel = g_list_concat
210 (modal_sel, pick_windows_recur(c, selected, raise));
211 }
212 }
213 /* if we dont have a prev then start back at the beginning,
214 otherwise skip back to the prev's next */
215 next = prev ? g_list_next(prev) : stacking_list;
216 }
217 }
218
219 ret = g_list_concat((raise ? modal_sel : modals),
220 (raise ? modals : modal_sel));
221
222 ret = g_list_concat(ret, (raise ? trans_sel : trans));
223 ret = g_list_concat(ret, (raise ? trans : trans_sel));
224
225
226 /* add itself */
227 ret = g_list_append(ret, top);
228
229 return ret;
230 }
231
232 static GList *pick_group_windows_recur(ObClient *top, ObClient *selected,
233 gboolean raise, gboolean normal)
234 {
235 GList *ret = NULL;
236 GList *it, *next, *prev;
237 GSList *sit;
238 gint i, n;
239
240 /* add group members in their stacking order */
241 if (top->group) {
242 i = 0;
243 n = g_slist_length(top->group->members) - 1;
244 for (it = stacking_list; i < n && it; it = next) {
245 prev = g_list_previous(it);
246 next = g_list_next(it);
247
248 if ((sit = g_slist_find(top->group->members, it->data))) {
249 ObClient *c;
250 ObClientType t;
251
252 ++i;
253 c = it->data;
254 t = c->type;
255
256 if ((c->desktop == selected->desktop ||
257 c->desktop == DESKTOP_ALL) &&
258 (t == OB_CLIENT_TYPE_TOOLBAR ||
259 t == OB_CLIENT_TYPE_MENU ||
260 t == OB_CLIENT_TYPE_UTILITY ||
261 (normal && t == OB_CLIENT_TYPE_NORMAL)))
262 {
263 ret = g_list_concat(ret,
264 pick_windows_recur(sit->data,
265 selected, raise));
266 /* if we dont have a prev then start back at the beginning,
267 otherwise skip back to the prev's next */
268 next = prev ? g_list_next(prev) : stacking_list;
269 }
270 }
271 }
272 }
273 return ret;
274 }
275
276 static GList *pick_windows(ObClient *selected, gboolean raise, gboolean group)
277 {
278 GList *it;
279 GSList *top, *top_it;
280 GSList *top_reorder = NULL;
281 GList *ret = NULL;
282
283 top = client_search_top_transients(selected);
284
285 /* go thru stacking list backwords so we can use g_slist_prepend */
286 for (it = g_list_last(stacking_list); it && top;
287 it = g_list_previous(it))
288 if ((top_it = g_slist_find(top, it->data))) {
289 top_reorder = g_slist_prepend(top_reorder, top_it->data);
290 top = g_slist_delete_link(top, top_it);
291 }
292 g_assert(top == NULL);
293
294 for (top_it = top_reorder; top_it; top_it = g_slist_next(top_it))
295 ret = g_list_concat(ret,
296 pick_windows_recur(top_it->data, selected, raise));
297
298 for (top_it = top_reorder; top_it; top_it = g_slist_next(top_it))
299 ret = g_list_concat(ret,
300 pick_group_windows_recur(top_it->data,
301 selected, raise, group));
302 return ret;
303 }
304
305 void stacking_raise(ObWindow *window, gboolean group)
306 {
307 GList *wins;
308
309 if (WINDOW_IS_CLIENT(window)) {
310 ObClient *selected;
311 selected = WINDOW_AS_CLIENT(window);
312 wins = pick_windows(selected, TRUE, group);
313 } else {
314 wins = g_list_append(NULL, window);
315 stacking_list = g_list_remove(stacking_list, window);
316 }
317 do_raise(wins);
318 g_list_free(wins);
319 }
320
321 void stacking_lower(ObWindow *window, gboolean group)
322 {
323 GList *wins;
324
325 if (WINDOW_IS_CLIENT(window)) {
326 ObClient *selected;
327 selected = WINDOW_AS_CLIENT(window);
328 wins = pick_windows(selected, FALSE, group);
329 } else {
330 wins = g_list_append(NULL, window);
331 stacking_list = g_list_remove(stacking_list, window);
332 }
333 do_lower(wins);
334 g_list_free(wins);
335 }
336
337 void stacking_below(ObWindow *window, ObWindow *below)
338 {
339 GList *wins, *before;
340
341 if (window_layer(window) != window_layer(below))
342 return;
343
344 wins = g_list_append(NULL, window);
345 stacking_list = g_list_remove(stacking_list, window);
346 before = g_list_next(g_list_find(stacking_list, below));
347 do_restack(wins, before);
348 g_list_free(wins);
349 }
350
351 void stacking_add(ObWindow *win)
352 {
353 g_assert(screen_support_win != None); /* make sure I dont break this in the
354 future */
355
356 stacking_list = g_list_append(stacking_list, win);
357 stacking_raise(win, FALSE);
358 }
359
360 void stacking_add_nonintrusive(ObWindow *win)
361 {
362 ObClient *client;
363 ObClient *parent = NULL;
364 GList *it_below = NULL;
365
366 if (!WINDOW_IS_CLIENT(win)) {
367 stacking_add(win); /* no special rules for others */
368 return;
369 }
370
371 client = WINDOW_AS_CLIENT(win);
372
373 /* insert above its highest parent */
374 if (client->transient_for) {
375 if (client->transient_for != OB_TRAN_GROUP) {
376 parent = client->transient_for;
377 } else {
378 GSList *sit;
379 GList *it;
380
381 if (client->group)
382 for (it = stacking_list; !parent && it; it = g_list_next(it)) {
383 if ((sit = g_slist_find(client->group->members, it->data)))
384 for (sit = client->group->members; !parent && sit;
385 sit = g_slist_next(sit))
386 {
387 ObClient *c = sit->data;
388 /* checking transient_for prevents infinate loops! */
389 if (sit->data == it->data && !c->transient_for)
390 parent = it->data;
391 }
392 }
393 }
394 }
395
396 if (!(it_below = g_list_find(stacking_list, parent))) {
397 /* no parent to put above, try find the focused client to go
398 under */
399 if (focus_client && focus_client->layer == client->layer) {
400 if ((it_below = g_list_find(stacking_list, focus_client)))
401 it_below = it_below->next;
402 }
403 }
404 if (!it_below) {
405 /* out of ideas, just add it normally... */
406 stacking_add(win);
407 } else {
408 /* make sure it's not in the wrong layer though ! */
409 for (; it_below; it_below = g_list_next(it_below))
410 {
411 /* stop when the window is not in a higher layer than the window
412 it is going above (it_below) */
413 if (client->layer >= window_layer(it_below->data))
414 break;
415 }
416 for (; it_below != stacking_list;
417 it_below = g_list_previous(it_below))
418 {
419 /* stop when the window is not in a lower layer than the
420 window it is going under (it_above) */
421 GList *it_above = g_list_previous(it_below);
422 if (client->layer <= window_layer(it_above->data))
423 break;
424 }
425
426 GList *wins = g_list_append(NULL, win);
427 do_restack(wins, it_below);
428 g_list_free(wins);
429 }
430 }
This page took 0.053067 seconds and 5 git commands to generate.