update copyright step 2
[mikachu/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) 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(ObClient *top, ObClient *selected, gboolean raise)
162 {
163     GList *ret = NULL;
164     GList *it, *next, *prev;
165     GSList *sit;
166     gint i, n;
167     GList *modals = NULL;
168     GList *trans = NULL;
169     GList *modal_sel = NULL; /* the selected guys if modal */
170     GList *trans_sel = NULL; /* the selected guys if not */
171
172     /* remove first so we can't run into ourself */
173     if ((it = g_list_find(stacking_list, top)))
174         stacking_list = g_list_delete_link(stacking_list, it);
175     else
176         return NULL;
177
178     i = 0;
179     n = g_slist_length(top->transients);
180     for (it = stacking_list; i < n && it; it = next) {
181         prev = g_list_previous(it);
182         next = g_list_next(it);
183
184         if ((sit = g_slist_find(top->transients, it->data))) {
185             ObClient *c = sit->data;
186             gboolean sel_child;
187
188             ++i;
189
190             if (c == selected)
191                 sel_child = TRUE;
192             else
193                 sel_child = client_search_transient(c, selected) != NULL;
194
195             if (!c->modal) {
196                 if (!sel_child) {
197                     trans = g_list_concat(trans,
198                                           pick_windows(c, selected, raise));
199                 } else {
200                     trans_sel = g_list_concat(trans_sel,
201                                                  pick_windows(c, selected,
202                                                               raise));
203                 }
204             } else {
205                 if (!sel_child) {
206                     modals = g_list_concat(modals,
207                                            pick_windows(c, selected, raise));
208                 } else {
209                     modal_sel = g_list_concat(modal_sel,
210                                                  pick_windows(c, selected,
211                                                               raise));
212                 }
213             }
214             /* if we dont have a prev then start back at the beginning,
215                otherwise skip back to the prev's next */
216             next = prev ? g_list_next(prev) : stacking_list;
217         }
218     }
219
220     ret = g_list_concat((raise ? modal_sel : modals),
221                         (raise ? modals : modal_sel));
222
223     ret = g_list_concat(ret, (raise ? trans_sel : trans));
224     ret = g_list_concat(ret, (raise ? trans : trans_sel));
225
226
227     /* add itself */
228     ret = g_list_append(ret, top);
229
230     return ret;
231 }
232
233 static GList *pick_group_windows(ObClient *top, ObClient *selected,
234                                  gboolean raise, gboolean normal)
235 {
236     GList *ret = NULL;
237     GList *it, *next, *prev;
238     GSList *sit;
239     gint i, n;
240
241     /* add group members in their stacking order */
242     if (top->group) {
243         i = 0;
244         n = g_slist_length(top->group->members) - 1;
245         for (it = stacking_list; i < n && it; it = next) {
246             prev = g_list_previous(it);
247             next = g_list_next(it);
248
249             if ((sit = g_slist_find(top->group->members, it->data))) {
250                 ObClient *c;
251                 ObClientType t;
252
253                 ++i;
254                 c = it->data;
255                 t = c->type;
256
257                 if ((c->desktop == selected->desktop ||
258                      c->desktop == DESKTOP_ALL) &&
259                     (t == OB_CLIENT_TYPE_TOOLBAR ||
260                      t == OB_CLIENT_TYPE_MENU ||
261                      t == OB_CLIENT_TYPE_UTILITY ||
262                      (normal && t == OB_CLIENT_TYPE_NORMAL)))
263                 {
264                     ret = g_list_concat(ret,
265                                         pick_windows(sit->data,
266                                                      selected, raise)); 
267                     /* if we dont have a prev then start back at the beginning,
268                        otherwise skip back to the prev's next */
269                     next = prev ? g_list_next(prev) : stacking_list;
270                 }
271             }
272         }
273     }
274     return ret;
275 }
276
277 void stacking_raise(ObWindow *window, gboolean group)
278 {
279     GList *wins;
280
281     if (WINDOW_IS_CLIENT(window)) {
282         ObClient *c;
283         ObClient *selected;
284         selected = WINDOW_AS_CLIENT(window);
285         c = client_search_top_transient(selected);
286         wins = pick_windows(c, selected, TRUE);
287         wins = g_list_concat(wins, pick_group_windows(c, selected, TRUE, group));
288     } else {
289         wins = g_list_append(NULL, window);
290         stacking_list = g_list_remove(stacking_list, window);
291     }
292     do_raise(wins);
293     g_list_free(wins);
294 }
295
296 void stacking_lower(ObWindow *window, gboolean group)
297 {
298     GList *wins;
299
300     if (WINDOW_IS_CLIENT(window)) {
301         ObClient *c;
302         ObClient *selected;
303         selected = WINDOW_AS_CLIENT(window);
304         c = client_search_top_transient(selected);
305         wins = pick_windows(c, selected, FALSE);
306         wins = g_list_concat(pick_group_windows(c, selected, FALSE, group), wins);
307     } else {
308         wins = g_list_append(NULL, window);
309         stacking_list = g_list_remove(stacking_list, window);
310     }
311     do_lower(wins);
312     g_list_free(wins);
313 }
314
315 void stacking_below(ObWindow *window, ObWindow *below)
316 {
317     GList *wins, *before;
318
319     if (window_layer(window) != window_layer(below))
320         return;
321
322     wins = g_list_append(NULL, window);
323     stacking_list = g_list_remove(stacking_list, window);
324     before = g_list_next(g_list_find(stacking_list, below));
325     do_restack(wins, before);
326     g_list_free(wins);
327 }
328
329 void stacking_add(ObWindow *win)
330 {
331     g_assert(screen_support_win != None); /* make sure I dont break this in the
332                                              future */
333
334     stacking_list = g_list_append(stacking_list, win);
335     stacking_raise(win, FALSE);
336 }
337
338 void stacking_add_nonintrusive(ObWindow *win)
339 {
340     ObClient *client;
341     ObClient *parent = NULL;
342     GList *it_before = NULL;
343
344     if (!WINDOW_IS_CLIENT(win)) {
345         stacking_add(win); /* no special rules for others */
346         return;
347     }
348
349     client = WINDOW_AS_CLIENT(win);
350
351     /* insert above its highest parent */
352     if (client->transient_for) {
353         if (client->transient_for != OB_TRAN_GROUP) {
354             parent = client->transient_for;
355         } else {
356             GSList *sit;
357             GList *it;
358
359             if (client->group)
360                 for (it = stacking_list; !parent && it; it = g_list_next(it)) {
361                     if ((sit = g_slist_find(client->group->members, it->data)))
362                 for (sit = client->group->members; !parent && sit;
363                      sit = g_slist_next(sit))
364                 {
365                     ObClient *c = sit->data;
366                     /* checking transient_for prevents infinate loops! */
367                     if (sit->data == it->data && !c->transient_for)
368                         parent = it->data;
369                 }
370             }
371         }
372     }
373
374     if (!(it_before = g_list_find(stacking_list, parent))) {
375         /* no parent to put above, try find the focused client to go
376            under */
377         if (focus_client && focus_client->layer == client->layer) {
378             if ((it_before = g_list_find(stacking_list, focus_client)))
379                 it_before = it_before->next;
380         }
381     }
382     if (!it_before) {
383         /* out of ideas, just add it normally... */
384         stacking_add(win);
385     } else {
386         GList *wins = g_list_append(NULL, win);
387         do_restack(wins, it_before);
388         g_list_free(wins);
389     }
390 }