]> Dogcows Code - chaz/openbox/blob - openbox/stacking.c
ob_remote has been killed, remove ob_sync from the public header, its only used inter...
[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(ob_root, net_client_list_stacking, window,
34 (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_it = NULL; /* the selected guy if modal */
145 GList *trans_sel_it = NULL; /* the selected guy if not */
146
147 /* remove first so we can't run into ourself */
148 stacking_list = g_list_remove(stacking_list, top);
149
150 i = 0;
151 n = g_slist_length(top->transients);
152 for (it = stacking_list; i < n && it; it = next) {
153 prev = g_list_previous(it);
154 next = g_list_next(it);
155
156 if ((sit = g_slist_find(top->transients, it->data))) {
157 ObClient *c = sit->data;
158
159 ++i;
160
161 if (!c->modal) {
162 if (c != selected) {
163 trans = g_list_concat(trans,
164 pick_windows(c, selected, raise));
165 } else {
166 g_assert(modal_sel_it == NULL);
167 g_assert(trans_sel_it == NULL);
168 trans_sel_it = pick_windows(c, selected, raise);
169 }
170 } else {
171 if (c != selected) {
172 modals = g_list_concat(modals,
173 pick_windows(c, selected, raise));
174 } else {
175 g_assert(modal_sel_it == NULL);
176 g_assert(trans_sel_it == NULL);
177 modal_sel_it = pick_windows(c, selected, raise);
178 }
179 }
180 /* if we dont have a prev then start back at the beginning,
181 otherwise skip back to the prev's next */
182 next = prev ? g_list_next(prev) : stacking_list;
183 }
184 }
185
186 ret = g_list_concat((raise ? modal_sel_it : modals),
187 (raise ? modals : modal_sel_it));
188
189 ret = g_list_concat(ret, (raise ? trans_sel_it : trans));
190 ret = g_list_concat(ret, (raise ? trans : trans_sel_it));
191
192
193 /* add itself */
194 ret = g_list_append(ret, top);
195
196 return ret;
197 }
198
199 static GList *pick_group_windows(ObClient *top, ObClient *selected, gboolean raise)
200 {
201 GList *ret = NULL;
202 GList *it, *next, *prev;
203 GSList *sit;
204 int i, n;
205
206 /* add group members in their stacking order */
207 if (top->group) {
208 i = 0;
209 n = g_slist_length(top->group->members) - 1;
210 for (it = stacking_list; i < n && it; it = next) {
211 prev = g_list_previous(it);
212 next = g_list_next(it);
213
214 if ((sit = g_slist_find(top->group->members, it->data))) {
215 ++i;
216 ret = g_list_concat(ret,
217 pick_windows(sit->data, selected, raise));
218 /* if we dont have a prev then start back at the beginning,
219 otherwise skip back to the prev's next */
220 next = prev ? g_list_next(prev) : stacking_list;
221 }
222 }
223 }
224 return ret;
225 }
226
227 void stacking_raise(ObWindow *window)
228 {
229 GList *wins;
230
231 if (WINDOW_IS_CLIENT(window)) {
232 ObClient *c;
233 ObClient *selected;
234 selected = WINDOW_AS_CLIENT(window);
235 c = client_search_top_transient(selected);
236 wins = pick_windows(c, selected, TRUE);
237 wins = g_list_concat(wins, pick_group_windows(c, selected, TRUE));
238 } else {
239 wins = g_list_append(NULL, window);
240 stacking_list = g_list_remove(stacking_list, window);
241 }
242 do_raise(wins);
243 g_list_free(wins);
244 }
245
246 void stacking_lower(ObWindow *window)
247 {
248 GList *wins;
249
250 if (WINDOW_IS_CLIENT(window)) {
251 ObClient *c;
252 ObClient *selected;
253 selected = WINDOW_AS_CLIENT(window);
254 c = client_search_top_transient(selected);
255 wins = pick_windows(c, selected, FALSE);
256 wins = g_list_concat(pick_group_windows(c, selected, FALSE), wins);
257 } else {
258 wins = g_list_append(NULL, window);
259 stacking_list = g_list_remove(stacking_list, window);
260 }
261 do_lower(wins);
262 g_list_free(wins);
263 }
264
265 void stacking_add(ObWindow *win)
266 {
267 ObStackingLayer l;
268 GList *wins;
269
270 g_assert(screen_support_win != None); /* make sure I dont break this in the
271 future */
272
273 l = window_layer(win);
274 wins = g_list_append(NULL, win); /* list of 1 element */
275
276 stacking_list = g_list_append(stacking_list, win);
277 stacking_raise(win);
278 }
279
280 void stacking_add_nonintrusive(ObWindow *win)
281 {
282 ObClient *client;
283 ObClient *parent = NULL;
284 GList *it_before = NULL;
285
286 if (!WINDOW_IS_CLIENT(win)) {
287 stacking_add(win); /* no special rules for others */
288 return;
289 }
290
291 client = WINDOW_AS_CLIENT(win);
292
293 /* insert above its highest parent */
294 if (client->transient_for) {
295 if (client->transient_for != OB_TRAN_GROUP) {
296 parent = client->transient_for;
297 } else {
298 GSList *sit;
299 GList *it;
300
301 if (client->group)
302 for (it = stacking_list; !parent && it; it = it->next) {
303 if ((sit = g_slist_find(client->group->members, it->data)))
304 for (sit = client->group->members; !parent && sit;
305 sit = sit->next) {
306 ObClient *c = sit->data;
307 /* checking transient_for prevents infinate loops! */
308 if (sit->data == it->data && !c->transient_for)
309 parent = it->data;
310 }
311 }
312 }
313 }
314
315 if (!(it_before = g_list_find(stacking_list, parent))) {
316 /* no parent to put above, try find the focused client to go
317 under */
318 if (focus_client && focus_client->layer == client->layer) {
319 if ((it_before = g_list_find(stacking_list, focus_client)))
320 it_before = it_before->next;
321 }
322 }
323 if (!it_before) {
324 /* out of ideas, just add it normally... */
325 stacking_add(win);
326 } else {
327 GList *wins = g_list_append(NULL, win);
328 do_restack(wins, it_before);
329 g_list_free(wins);
330 }
331 }
This page took 0.051987 seconds and 4 git commands to generate.