]> Dogcows Code - chaz/openbox/blob - openbox/stacking.c
core when the list gets out of order
[chaz/openbox] / 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 void do_restack(GList *wins, GList *before)
45 {
46 GList *it, *next;
47 Window *win;
48 int i;
49
50 /* pls only restack stuff in the same layer at a time */
51 for (it = wins; it; it = next) {
52 next = g_list_next(it);
53 if (!next) break;
54 g_assert (window_layer(it->data) == window_layer(next->data));
55 }
56
57
58 win = g_new(Window, g_list_length(wins) + 1);
59
60 if (before == stacking_list)
61 win[0] = focus_backup;
62 else if (!before)
63 win[0] = window_top(g_list_last(stacking_list)->data);
64 else
65 win[0] = window_top(g_list_previous(before)->data);
66
67 for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
68 win[i] = window_top(it->data);
69 stacking_list = g_list_insert_before(stacking_list, before, it->data);
70 }
71
72 /* XXX some debug checking of the stacking list's order */
73 for (it = stacking_list; ; it = next) {
74 next = g_list_next(it);
75 if (!next) break;
76 g_assert(window_layer(it->data) >= window_layer(next->data));
77 }
78
79 XRestackWindows(ob_display, win, i);
80 g_free(win);
81 }
82
83 static void raise(GList *wins)
84 {
85 GList *it;
86 GList *layer[NUM_STACKLAYER] = {NULL};
87 int i;
88
89 for (it = wins; it; it = g_list_next(it)) {
90 StackLayer l;
91
92 l = window_layer(it->data);
93 layer[l] = g_list_append(layer[l], it->data);
94 }
95
96 it = stacking_list;
97 for (i = NUM_STACKLAYER - 1; i >= 0; --i) {
98 if (layer[i]) {
99 for (; it; it = g_list_next(it)) {
100 /* look for the top of the layer */
101 if (window_layer(it->data) <= (StackLayer) i)
102 break;
103 }
104 do_restack(layer[i], it);
105 g_list_free(layer[i]);
106 }
107 }
108 }
109
110 static void lower(GList *wins)
111 {
112 GList *it;
113 GList *layer[NUM_STACKLAYER] = {NULL};
114 int i;
115
116 for (it = wins; it; it = g_list_next(it)) {
117 StackLayer l;
118
119 l = window_layer(it->data);
120 layer[l] = g_list_append(layer[l], it->data);
121 }
122
123 it = stacking_list;
124 for (i = NUM_STACKLAYER - 1; i >= 0; --i) {
125 if (layer[i]) {
126 for (; it; it = g_list_next(it)) {
127 /* look for the top of the next layer down */
128 if (window_layer(it->data) < (StackLayer) i)
129 break;
130 }
131 do_restack(layer[i], it);
132 g_list_free(layer[i]);
133 }
134 }
135 }
136
137 static GList* pick_windows(ObWindow *win)
138 {
139 GList *ret = NULL;
140 GList *it, *next;
141 GSList *sit;
142 Client *c;
143 int i, n;
144
145 if (!WINDOW_IS_CLIENT(win)) {
146 ret = g_list_append(ret, win);
147 stacking_list = g_list_remove(stacking_list, win);
148 return ret;
149 }
150 c = WINDOW_AS_CLIENT(win);
151
152 /* add transient children in their stacking order */
153 i = 0;
154 n = g_slist_length(c->transients);
155 for (it = stacking_list; i < n && it; it = next) {
156 next = g_list_next(it);
157 if ((sit = g_slist_find(c->transients, it->data))) {
158 ++i;
159 ret = g_list_concat(ret, pick_windows(sit->data));
160 }
161 }
162
163 /* add itself */
164 if (g_list_find(stacking_list, win)) {
165 ret = g_list_append(ret, win);
166 stacking_list = g_list_remove(stacking_list, win);
167 }
168
169 /* add group members in their stacking order */
170 if (c->group) {
171 for (it = stacking_list; it; it = next) {
172 next = g_list_next(it);
173 if ((sit = g_slist_find(c->group->members, it->data))) {
174 ret = g_list_append(ret, sit->data);
175 stacking_list = g_list_remove(stacking_list, sit->data);
176 }
177 }
178 }
179
180 if (c->transient_for && c->transient_for != TRAN_GROUP)
181 /* dont add it twice */
182 if (g_list_find(stacking_list, c->transient_for))
183 ret = g_list_concat(ret, pick_windows
184 (CLIENT_AS_WINDOW(c->transient_for)));
185
186 return ret;
187 }
188
189 void stacking_raise(ObWindow *window)
190 {
191 GList *wins;
192
193 wins = pick_windows(window);
194 raise(wins);
195 }
196
197 void stacking_lower(ObWindow *window)
198 {
199 GList *wins;
200
201 wins = pick_windows(window);
202 lower(wins);
203 }
204
205 void stacking_add(ObWindow *win)
206 {
207 StackLayer l;
208 GList *wins, *it;
209
210 l = window_layer(win);
211 wins = g_list_append(NULL, win); /* list of 1 element */
212
213 for (it = stacking_list; it; it = g_list_next(it))
214 if (window_layer(it->data) <= l)
215 break;
216 do_restack(wins, it);
217 g_list_free(wins);
218
219 stacking_raise(win);
220 }
221
222 void stacking_add_nonintrusive(ObWindow *win)
223 {
224 Client *client;
225 Client *parent = NULL;
226 GList *it_before = NULL;
227
228 if (!WINDOW_IS_CLIENT(win)) {
229 stacking_add(win); /* no special rules for others */
230 return;
231 }
232
233 client = WINDOW_AS_CLIENT(win);
234
235 /* insert above its highest parent */
236 if (client->transient_for) {
237 if (client->transient_for != TRAN_GROUP) {
238 parent = client->transient_for;
239 } else {
240 GSList *sit;
241 GList *it;
242
243 if (client->group)
244 for (it = stacking_list; !parent && it; it = it->next) {
245 if ((sit = g_slist_find(client->group->members, it->data)))
246 for (sit = client->group->members; !parent && sit;
247 sit = sit->next) {
248 Client *c = sit->data;
249 /* checking transient_for prevents infinate loops! */
250 if (sit->data == it->data && !c->transient_for)
251 parent = it->data;
252 }
253 }
254 }
255 }
256
257 if (!(it_before = g_list_find(stacking_list, parent))) {
258 /* no parent to put above, try find the focused client to go
259 under */
260 if (focus_client && focus_client->layer == client->layer) {
261 if ((it_before = g_list_find(stacking_list, focus_client)))
262 it_before = it_before->next;
263 }
264 }
265 if (!it_before) {
266 /* out of ideas, just add it normally... */
267 stacking_add(win);
268 } else {
269 GList *wins = g_list_append(NULL, win);
270 do_restack(wins, it_before);
271 g_list_free(wins);
272 }
273 }
This page took 0.044833 seconds and 5 git commands to generate.