]> Dogcows Code - chaz/openbox/blob - openbox/stacking.c
Fix some keyboard control issues.
[chaz/openbox] / openbox / stacking.c
1 #include "openbox.h"
2 #include "prop.h"
3 #include "screen.h"
4 #include "focus.h"
5 #include "client.h"
6 #include "group.h"
7 #include "frame.h"
8 #include "window.h"
9
10 GList *stacking_list = NULL;
11
12 void stacking_set_list()
13 {
14 Window *windows = NULL;
15 GList *it;
16 guint i = 0;
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() == OB_STATE_EXITING) return;
22
23 /* create an array of the window ids (from bottom to top,
24 reverse order!) */
25 if (stacking_list) {
26 windows = g_new(Window, g_list_length(stacking_list));
27 for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
28 if (WINDOW_IS_CLIENT(it->data))
29 windows[i++] = WINDOW_AS_CLIENT(it->data)->window;
30 }
31 }
32
33 PROP_SETA32(RootWindow(ob_display, ob_screen),
34 net_client_list_stacking, window, (guint32*)windows, i);
35
36 g_free(windows);
37 }
38
39 static void do_restack(GList *wins, GList *before)
40 {
41 GList *it, *next;
42 Window *win;
43 int i;
44
45 /* pls only restack stuff in the same layer at a time */
46 for (it = wins; it; it = next) {
47 next = g_list_next(it);
48 if (!next) break;
49 g_assert (window_layer(it->data) == window_layer(next->data));
50 }
51
52
53 win = g_new(Window, g_list_length(wins) + 1);
54
55 if (before == stacking_list)
56 win[0] = screen_support_win;
57 else if (!before)
58 win[0] = window_top(g_list_last(stacking_list)->data);
59 else
60 win[0] = window_top(g_list_previous(before)->data);
61
62 for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
63 win[i] = window_top(it->data);
64 g_assert(win[i] != None); /* better not call stacking shit before
65 setting your top level window value */
66 stacking_list = g_list_insert_before(stacking_list, before, it->data);
67 }
68
69 /* XXX some debug checking of the stacking list's order */
70 for (it = stacking_list; ; it = next) {
71 next = g_list_next(it);
72 if (!next) break;
73 g_assert(window_layer(it->data) >= window_layer(next->data));
74 }
75
76 XRestackWindows(ob_display, win, i);
77 g_free(win);
78
79 stacking_set_list();
80 }
81
82 static void do_raise(GList *wins)
83 {
84 GList *it;
85 GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
86 int i;
87
88 for (it = wins; it; it = g_list_next(it)) {
89 ObStackingLayer l;
90
91 l = window_layer(it->data);
92 layer[l] = g_list_append(layer[l], it->data);
93 }
94
95 it = stacking_list;
96 for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
97 if (layer[i]) {
98 for (; it; it = g_list_next(it)) {
99 /* look for the top of the layer */
100 if (window_layer(it->data) <= (ObStackingLayer) i)
101 break;
102 }
103 do_restack(layer[i], it);
104 g_list_free(layer[i]);
105 }
106 }
107 }
108
109 static void do_lower(GList *wins)
110 {
111 GList *it;
112 GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
113 int i;
114
115 for (it = wins; it; it = g_list_next(it)) {
116 ObStackingLayer l;
117
118 l = window_layer(it->data);
119 layer[l] = g_list_append(layer[l], it->data);
120 }
121
122 it = stacking_list;
123 for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
124 if (layer[i]) {
125 for (; it; it = g_list_next(it)) {
126 /* look for the top of the next layer down */
127 if (window_layer(it->data) < (ObStackingLayer) i)
128 break;
129 }
130 do_restack(layer[i], it);
131 g_list_free(layer[i]);
132 }
133 }
134 }
135
136 static GList *pick_windows(ObClient *top, ObClient *selected, gboolean raise)
137 {
138 GList *ret = NULL;
139 GList *it, *next, *prev;
140 GSList *sit;
141 int i, n;
142 GList *modals = NULL;
143 GList *trans = NULL;
144 GList *modal_sel = NULL; /* the selected guys if modal */
145 GList *trans_sel = NULL; /* the selected guys if not */
146
147 /* remove first so we can't run into ourself */
148 if ((it = g_list_find(stacking_list, top)))
149 stacking_list = g_list_delete_link(stacking_list, it);
150 else
151 return NULL;
152
153 i = 0;
154 n = g_slist_length(top->transients);
155 for (it = stacking_list; i < n && it; it = next) {
156 prev = g_list_previous(it);
157 next = g_list_next(it);
158
159 if ((sit = g_slist_find(top->transients, it->data))) {
160 ObClient *c = sit->data;
161 gboolean sel_child;
162
163 ++i;
164
165 if (c == selected)
166 sel_child = TRUE;
167 else
168 sel_child = client_search_transient(c, selected) != NULL;
169
170 if (!c->modal) {
171 if (!sel_child) {
172 trans = g_list_concat(trans,
173 pick_windows(c, selected, raise));
174 } else {
175 trans_sel = g_list_concat(trans_sel,
176 pick_windows(c, selected,
177 raise));
178 }
179 } else {
180 if (!sel_child) {
181 modals = g_list_concat(modals,
182 pick_windows(c, selected, raise));
183 } else {
184 modal_sel = g_list_concat(modal_sel,
185 pick_windows(c, selected,
186 raise));
187 }
188 }
189 /* if we dont have a prev then start back at the beginning,
190 otherwise skip back to the prev's next */
191 next = prev ? g_list_next(prev) : stacking_list;
192 }
193 }
194
195 ret = g_list_concat((raise ? modal_sel : modals),
196 (raise ? modals : modal_sel));
197
198 ret = g_list_concat(ret, (raise ? trans_sel : trans));
199 ret = g_list_concat(ret, (raise ? trans : trans_sel));
200
201
202 /* add itself */
203 ret = g_list_append(ret, top);
204
205 return ret;
206 }
207
208 static GList *pick_group_windows(ObClient *top, ObClient *selected, gboolean raise)
209 {
210 GList *ret = NULL;
211 GList *it, *next, *prev;
212 GSList *sit;
213 int i, n;
214
215 /* add group members in their stacking order */
216 if (top->group) {
217 i = 0;
218 n = g_slist_length(top->group->members) - 1;
219 for (it = stacking_list; i < n && it; it = next) {
220 prev = g_list_previous(it);
221 next = g_list_next(it);
222
223 if ((sit = g_slist_find(top->group->members, it->data))) {
224 ++i;
225 ret = g_list_concat(ret,
226 pick_windows(sit->data, selected, raise));
227 /* if we dont have a prev then start back at the beginning,
228 otherwise skip back to the prev's next */
229 next = prev ? g_list_next(prev) : stacking_list;
230 }
231 }
232 }
233 return ret;
234 }
235
236 void stacking_raise(ObWindow *window)
237 {
238 GList *wins;
239
240 if (WINDOW_IS_CLIENT(window)) {
241 ObClient *c;
242 ObClient *selected;
243 selected = WINDOW_AS_CLIENT(window);
244 c = client_search_top_transient(selected);
245 wins = pick_windows(c, selected, TRUE);
246 /*wins = g_list_concat(wins, pick_group_windows(c, selected, TRUE));*/
247 } else {
248 wins = g_list_append(NULL, window);
249 stacking_list = g_list_remove(stacking_list, window);
250 }
251 do_raise(wins);
252 g_list_free(wins);
253 }
254
255 void stacking_lower(ObWindow *window)
256 {
257 GList *wins;
258
259 if (WINDOW_IS_CLIENT(window)) {
260 ObClient *c;
261 ObClient *selected;
262 selected = WINDOW_AS_CLIENT(window);
263 c = client_search_top_transient(selected);
264 wins = pick_windows(c, selected, FALSE);
265 /*wins = g_list_concat(pick_group_windows(c, selected, FALSE), wins);*/
266 } else {
267 wins = g_list_append(NULL, window);
268 stacking_list = g_list_remove(stacking_list, window);
269 }
270 do_lower(wins);
271 g_list_free(wins);
272 }
273
274 void stacking_add(ObWindow *win)
275 {
276 ObStackingLayer l;
277 GList *wins;
278
279 g_assert(screen_support_win != None); /* make sure I dont break this in the
280 future */
281
282 l = window_layer(win);
283 wins = g_list_append(NULL, win); /* list of 1 element */
284
285 stacking_list = g_list_append(stacking_list, win);
286 stacking_raise(win);
287 }
288
289 void stacking_add_nonintrusive(ObWindow *win)
290 {
291 ObClient *client;
292 ObClient *parent = NULL;
293 GList *it_before = NULL;
294
295 if (!WINDOW_IS_CLIENT(win)) {
296 stacking_add(win); /* no special rules for others */
297 return;
298 }
299
300 client = WINDOW_AS_CLIENT(win);
301
302 /* insert above its highest parent */
303 if (client->transient_for) {
304 if (client->transient_for != OB_TRAN_GROUP) {
305 parent = client->transient_for;
306 } else {
307 GSList *sit;
308 GList *it;
309
310 if (client->group)
311 for (it = stacking_list; !parent && it; it = it->next) {
312 if ((sit = g_slist_find(client->group->members, it->data)))
313 for (sit = client->group->members; !parent && sit;
314 sit = sit->next) {
315 ObClient *c = sit->data;
316 /* checking transient_for prevents infinate loops! */
317 if (sit->data == it->data && !c->transient_for)
318 parent = it->data;
319 }
320 }
321 }
322 }
323
324 if (!(it_before = g_list_find(stacking_list, parent))) {
325 /* no parent to put above, try find the focused client to go
326 under */
327 if (focus_client && focus_client->layer == client->layer) {
328 if ((it_before = g_list_find(stacking_list, focus_client)))
329 it_before = it_before->next;
330 }
331 }
332 if (!it_before) {
333 /* out of ideas, just add it normally... */
334 stacking_add(win);
335 } else {
336 GList *wins = g_list_append(NULL, win);
337 do_restack(wins, it_before);
338 g_list_free(wins);
339 }
340 }
This page took 0.053854 seconds and 4 git commands to generate.