]> icculus.org git repositories - dana/openbox.git/blob - openbox/stacking.c
remove some useless code
[dana/openbox.git] / 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) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "openbox.h"
20 #include "prop.h"
21 #include "screen.h"
22 #include "focus.h"
23 #include "client.h"
24 #include "group.h"
25 #include "frame.h"
26 #include "window.h"
27
28 GList  *stacking_list = NULL;
29
30 void stacking_set_list()
31 {
32     Window *windows = NULL;
33     GList *it;
34     guint i = 0;
35
36     /* on shutdown, don't update the properties, so that we can read it back
37        in on startup and re-stack the windows as they were before we shut down
38     */
39     if (ob_state() == OB_STATE_EXITING) return;
40
41     /* create an array of the window ids (from bottom to top,
42        reverse order!) */
43     if (stacking_list) {
44         windows = g_new(Window, g_list_length(stacking_list));
45         for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
46             if (WINDOW_IS_CLIENT(it->data))
47                 windows[i++] = WINDOW_AS_CLIENT(it->data)->window;
48         }
49     }
50
51     PROP_SETA32(RootWindow(ob_display, ob_screen),
52                 net_client_list_stacking, window, (gulong*)windows, i);
53
54     g_free(windows);
55 }
56
57 static void do_restack(GList *wins, GList *before)
58 {
59     GList *it;
60     Window *win;
61     gint i;
62
63 #ifdef DEBUG
64     GList *next;
65     /* pls only restack stuff in the same layer at a time */
66     for (it = wins; it; it = next) {
67         next = g_list_next(it);
68         if (!next) break;
69         g_assert (window_layer(it->data) == window_layer(next->data));
70     }
71     if (before)
72         g_assert(window_layer(it->data) >= window_layer(before->data));
73 #endif
74
75     win = g_new(Window, g_list_length(wins) + 1);
76
77     if (before == stacking_list)
78         win[0] = screen_support_win;
79     else if (!before)
80         win[0] = window_top(g_list_last(stacking_list)->data);
81     else
82         win[0] = window_top(g_list_previous(before)->data);
83
84     for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
85         win[i] = window_top(it->data);
86         g_assert(win[i] != None); /* better not call stacking shit before
87                                      setting your top level window value */
88         stacking_list = g_list_insert_before(stacking_list, before, it->data);
89     }
90
91 #ifdef DEBUG
92     /* some debug checking of the stacking list's order */
93     for (it = stacking_list; ; it = next) {
94         next = g_list_next(it);
95         if (!next) break;
96         g_assert(window_layer(it->data) >= window_layer(next->data));
97     }
98 #endif
99
100     XRestackWindows(ob_display, win, i);
101     g_free(win);
102
103     stacking_set_list();
104 }
105
106 static void do_raise(GList *wins)
107 {
108     GList *it;
109     GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
110     gint i;
111
112     for (it = wins; it; it = g_list_next(it)) {
113         ObStackingLayer l;
114
115         l = window_layer(it->data);
116         layer[l] = g_list_append(layer[l], it->data);
117     }
118
119     it = stacking_list;
120     for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
121         if (layer[i]) {
122             for (; it; it = g_list_next(it)) {
123                 /* look for the top of the layer */
124                 if (window_layer(it->data) <= (ObStackingLayer) i)
125                     break;
126             }
127             do_restack(layer[i], it);
128             g_list_free(layer[i]);
129         }
130     }
131 }
132
133 static void do_lower(GList *wins)
134 {
135     GList *it;
136     GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
137     gint i;
138
139     for (it = wins; it; it = g_list_next(it)) {
140         ObStackingLayer l;
141
142         l = window_layer(it->data);
143         layer[l] = g_list_append(layer[l], it->data);
144     }
145
146     it = stacking_list;
147     for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
148         if (layer[i]) {
149             for (; it; it = g_list_next(it)) {
150                 /* look for the top of the next layer down */
151                 if (window_layer(it->data) < (ObStackingLayer) i)
152                     break;
153             }
154             do_restack(layer[i], it);
155             g_list_free(layer[i]);
156         }
157     }
158 }
159
160 static GList *pick_windows(ObClient *top, ObClient *selected, gboolean raise)
161 {
162     GList *ret = NULL;
163     GList *it, *next, *prev;
164     GSList *sit;
165     gint i, n;
166     GList *modals = NULL;
167     GList *trans = NULL;
168     GList *modal_sel = NULL; /* the selected guys if modal */
169     GList *trans_sel = NULL; /* the selected guys if not */
170
171     /* remove first so we can't run into ourself */
172     if ((it = g_list_find(stacking_list, top)))
173         stacking_list = g_list_delete_link(stacking_list, it);
174     else
175         return NULL;
176
177     i = 0;
178     n = g_slist_length(top->transients);
179     for (it = stacking_list; i < n && it; it = next) {
180         prev = g_list_previous(it);
181         next = g_list_next(it);
182
183         if ((sit = g_slist_find(top->transients, it->data))) {
184             ObClient *c = sit->data;
185             gboolean sel_child;
186
187             ++i;
188
189             if (c == selected)
190                 sel_child = TRUE;
191             else
192                 sel_child = client_search_transient(c, selected) != NULL;
193
194             if (!c->modal) {
195                 if (!sel_child) {
196                     trans = g_list_concat(trans,
197                                           pick_windows(c, selected, raise));
198                 } else {
199                     trans_sel = g_list_concat(trans_sel,
200                                                  pick_windows(c, selected,
201                                                               raise));
202                 }
203             } else {
204                 if (!sel_child) {
205                     modals = g_list_concat(modals,
206                                            pick_windows(c, selected, raise));
207                 } else {
208                     modal_sel = g_list_concat(modal_sel,
209                                                  pick_windows(c, selected,
210                                                               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(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(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 void stacking_raise(ObWindow *window, gboolean group)
277 {
278     GList *wins;
279
280     if (WINDOW_IS_CLIENT(window)) {
281         ObClient *c;
282         ObClient *selected;
283         selected = WINDOW_AS_CLIENT(window);
284         c = client_search_top_transient(selected);
285         wins = pick_windows(c, selected, TRUE);
286         wins = g_list_concat(wins, pick_group_windows(c, selected, TRUE, group));
287     } else {
288         wins = g_list_append(NULL, window);
289         stacking_list = g_list_remove(stacking_list, window);
290     }
291     do_raise(wins);
292     g_list_free(wins);
293 }
294
295 void stacking_lower(ObWindow *window, gboolean group)
296 {
297     GList *wins;
298
299     if (WINDOW_IS_CLIENT(window)) {
300         ObClient *c;
301         ObClient *selected;
302         selected = WINDOW_AS_CLIENT(window);
303         c = client_search_top_transient(selected);
304         wins = pick_windows(c, selected, FALSE);
305         wins = g_list_concat(pick_group_windows(c, selected, FALSE, group), wins);
306     } else {
307         wins = g_list_append(NULL, window);
308         stacking_list = g_list_remove(stacking_list, window);
309     }
310     do_lower(wins);
311     g_list_free(wins);
312 }
313
314 void stacking_below(ObWindow *window, ObWindow *below)
315 {
316     GList *wins, *before;
317
318     if (window_layer(window) != window_layer(below))
319         return;
320
321     wins = g_list_append(NULL, window);
322     stacking_list = g_list_remove(stacking_list, window);
323     before = g_list_next(g_list_find(stacking_list, below));
324     do_restack(wins, before);
325     g_list_free(wins);
326 }
327
328 void stacking_add(ObWindow *win)
329 {
330     g_assert(screen_support_win != None); /* make sure I dont break this in the
331                                              future */
332
333     stacking_list = g_list_append(stacking_list, win);
334     stacking_raise(win, FALSE);
335 }
336
337 void stacking_add_nonintrusive(ObWindow *win)
338 {
339     ObClient *client;
340     ObClient *parent = NULL;
341     GList *it_before = NULL;
342
343     if (!WINDOW_IS_CLIENT(win)) {
344         stacking_add(win); /* no special rules for others */
345         return;
346     }
347
348     client = WINDOW_AS_CLIENT(win);
349
350     /* insert above its highest parent */
351     if (client->transient_for) {
352         if (client->transient_for != OB_TRAN_GROUP) {
353             parent = client->transient_for;
354         } else {
355             GSList *sit;
356             GList *it;
357
358             if (client->group)
359                 for (it = stacking_list; !parent && it; it = g_list_next(it)) {
360                     if ((sit = g_slist_find(client->group->members, it->data)))
361                 for (sit = client->group->members; !parent && sit;
362                      sit = g_slist_next(sit))
363                 {
364                     ObClient *c = sit->data;
365                     /* checking transient_for prevents infinate loops! */
366                     if (sit->data == it->data && !c->transient_for)
367                         parent = it->data;
368                 }
369             }
370         }
371     }
372
373     if (!(it_before = g_list_find(stacking_list, parent))) {
374         /* no parent to put above, try find the focused client to go
375            under */
376         if (focus_client && focus_client->layer == client->layer) {
377             if ((it_before = g_list_find(stacking_list, focus_client)))
378                 it_before = it_before->next;
379         }
380     }
381     if (!it_before) {
382         /* out of ideas, just add it normally... */
383         stacking_add(win);
384     } else {
385         GList *wins = g_list_append(NULL, win);
386         do_restack(wins, it_before);
387         g_list_free(wins);
388     }
389 }