]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/stacking.c
add anotehr stacking_add function.
[mikachu/openbox.git] / 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, *win_it;
15     GList *it;
16     guint size = g_list_length(stacking_list);
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 (size > 0) {
26         windows = g_new(Window, size);
27         win_it = windows;
28         for (it = g_list_last(stacking_list); it != NULL;
29              it = it->prev)
30             if (WINDOW_IS_CLIENT(it->data)) {
31                 *win_it = WINDOW_AS_CLIENT(it->data)->window;
32                 ++win_it;
33             }
34     } else
35         windows = win_it = NULL;
36
37     PROP_SETA32(ob_root, net_client_list_stacking, window,
38                 (guint32*)windows, win_it - windows);
39
40     if (windows)
41         g_free(windows);
42 }
43
44 static GList *find_lowest_transient(Client *c)
45 {
46     GList *it;
47     GSList *sit;
48
49     for (it = g_list_last(stacking_list); it; it = it->prev)
50         for (sit = c->transients; sit; sit = sit->next)
51             if (it->data == sit->data) /* found a transient */
52                 return it;
53     return NULL;
54 }
55
56 static void raise_recursive(ObWindow *window)
57 {
58     Window wins[2];  /* only ever restack 2 windows. */
59     GList *it, *low;
60     GSList *sit;
61
62     g_assert(stacking_list != NULL); /* this would be bad */
63
64     /* remove the window before looking so we can't run into ourselves and our
65        transients can't either. */
66     stacking_list = g_list_remove(stacking_list, window);
67
68     /* raise transients first */
69     if (WINDOW_IS_CLIENT(window)) {
70         Client *client = WINDOW_AS_CLIENT(window);
71         for (sit = client->transients; sit; sit = sit->next)
72             raise_recursive(sit->data);
73     }
74
75     /* find 'it' where it is the positiion in the stacking order where
76        'window' will be inserted *before* */
77
78     if (WINDOW_IS_CLIENT(window))
79         low = find_lowest_transient(WINDOW_AS_CLIENT(window));
80     else
81         low = NULL;
82     /* the stacking list is from highest to lowest */
83     for (it = g_list_last(stacking_list); it; it = it->prev) {
84         if (it == low || window_layer(window) < window_layer(it->data)) {
85             it = it->next;
86             break;
87         }
88         if (it == stacking_list)
89             break;
90     }
91
92     /*
93       if our new position is the top, we want to stack under the focus_backup.
94       otherwise, we want to stack under the previous window in the stack.
95     */
96     if (it == stacking_list)
97         wins[0] = focus_backup;
98     else if (it != NULL)
99         wins[0] = window_top(it->prev->data);
100     else
101         wins[0] = window_top(g_list_last(stacking_list)->data);
102     wins[1] = window_top(window);
103
104     stacking_list = g_list_insert_before(stacking_list, it, window);
105
106     XRestackWindows(ob_display, wins, 2);
107 }
108
109 void stacking_raise(ObWindow *window)
110 {
111     g_assert(stacking_list != NULL); /* this would be bad */
112
113     if (WINDOW_IS_CLIENT(window)) {
114         Client *client = WINDOW_AS_CLIENT(window);
115         /* move up the transient chain as far as possible first */
116         if (client->transient_for) {
117             if (client->transient_for != TRAN_GROUP) {
118                 stacking_raise(CLIENT_AS_WINDOW(client->transient_for));
119                 return;
120             } else {
121                 GSList *it;
122
123                 /* the check for TRAN_GROUP is to prevent an infinate loop with
124                    2 transients of the same group at the head of the group's
125                    members list */
126                 for (it = client->group->members; it; it = it->next) {
127                     Client *c = it->data;
128
129                     if (c != client && c->transient_for != TRAN_GROUP)
130                         stacking_raise(it->data);
131                 }
132                 if (it == NULL) return;
133             }
134         }
135     }
136
137     raise_recursive(window);
138
139     stacking_set_list();
140 }
141
142 static void lower_recursive(ObWindow *window, ObWindow *above)
143 {
144     Window wins[2];  /* only ever restack 2 windows. */
145     GList *it;
146     GSList *sit;
147
148     /* find 'it' where 'it' is the position in the stacking_list where the
149        'window' will be placed *after* */
150
151     for (it = g_list_last(stacking_list); it != stacking_list; it = it->prev)
152         if (window_layer(window) <= window_layer(it->data) &&
153             it->data != above)
154             break;
155
156     if (it->data != window) { /* not already the bottom */
157         wins[0] = window_top(it->data);
158         wins[1] = window_top(window);
159
160         stacking_list = g_list_remove(stacking_list, window);
161         stacking_list = g_list_insert_before(stacking_list, it->next, window);
162         XRestackWindows(ob_display, wins, 2);
163     }
164
165     if (WINDOW_IS_CLIENT(window)) {
166         Client *client = WINDOW_AS_CLIENT(window);
167         for (sit = client->transients; sit; sit = sit->next)
168             lower_recursive(CLIENT_AS_WINDOW(sit->data), window);
169     }
170 }
171
172 void stacking_lower(ObWindow *window)
173 {
174     g_assert(stacking_list != NULL); /* this would be bad */
175
176     if (WINDOW_IS_CLIENT(window)) {
177         Client *client = WINDOW_AS_CLIENT(window);
178         /* move up the transient chain as far as possible first */
179         while (client->transient_for) {
180             if (client->transient_for != TRAN_GROUP) {
181                 stacking_lower(CLIENT_AS_WINDOW(client->transient_for));
182                 return;
183             } else {
184                 GSList *it;
185
186                 /* the check for TRAN_GROUP is to prevent an infinate loop with
187                    2 transients of the same group at the head of the group's
188                    members list */
189                 for (it = client->group->members; it; it = it->next) {
190                     Client *c = it->data;
191
192                     if (c != client && c->transient_for != TRAN_GROUP)
193                         stacking_lower(it->data);
194                 }
195                 if (it == NULL) return;
196             }
197         }
198         window = CLIENT_AS_WINDOW(client);
199     }
200
201     lower_recursive(window, NULL);
202
203     stacking_set_list();
204 }
205
206 void stacking_add(ObWindow *win)
207 {
208     stacking_list = g_list_append(stacking_list, win);
209     stacking_raise(win);
210 }
211
212 void stacking_add_nonintrusive(ObWindow *win)
213 {
214     Window wins[2];  /* only ever restack 2 windows. */
215
216     if (!WINDOW_IS_CLIENT(win))
217         stacking_add(win); /* no special rules for others */
218     else {
219         Client *client = WINDOW_AS_CLIENT(win);
220         Client *parent = NULL;
221         GList *it_before = NULL;
222
223         /* insert above its highest parent */
224         if (client->transient_for) {
225             if (client->transient_for != TRAN_GROUP) {
226                 parent = client->transient_for;
227             } else {
228                 GSList *sit;
229                 GList *it;
230
231                 /* the check for TRAN_GROUP is to prevent an infinate loop with
232                    2 transients of the same group at the head of the group's
233                    members list */
234                 for (it = stacking_list; !parent && it; it = it->next) {
235                     for (sit = client->group->members; !parent && sit;
236                          sit = sit->next) {
237                         Client *c = sit->data;
238                         if (sit->data == it->data &&
239                             c->transient_for != TRAN_GROUP)
240                             parent = it->data;
241                     }
242                 }
243             }
244         }
245
246         if (!(it_before = g_list_find(stacking_list, parent))) {
247             /* no parent to put above, try find the focused client to go
248                under */
249             if ((it_before = g_list_find(stacking_list, focus_client)))
250                 it_before = it_before->next;
251             else {
252                 /* out of ideas, just add it normally... */
253                 stacking_add(win);
254                 return;
255             }
256         }
257         stacking_list = g_list_insert_before(stacking_list, it_before, win);
258
259         it_before = g_list_find(stacking_list, win)->prev;
260         if (!it_before)
261             wins[0] = focus_backup;
262         else
263             wins[0] = window_top(it_before->data);
264         wins[1] = window_top(win);
265
266         XRestackWindows(ob_display, wins, 2);
267     }
268 }