]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/stacking.c
Clients Menus and Slits are all 'ObWindow's now.
[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, ++win_it)
30             if (WINDOW_IS_CLIENT(it->data))
31                 *win_it = window_top(it->data);
32     } else
33         windows = NULL;
34
35     PROP_SETA32(ob_root, net_client_list_stacking, window,
36                 (guint32*)windows, size);
37
38     if (windows)
39         g_free(windows);
40 }
41
42 static GList *find_lowest_transient(Client *c)
43 {
44     GList *it;
45     GSList *sit;
46
47     for (it = g_list_last(stacking_list); it; it = it->prev)
48         for (sit = c->transients; sit; sit = sit->next)
49             if (it->data == sit->data) /* found a transient */
50                 return it;
51     return NULL;
52 }
53
54 static void raise_recursive(ObWindow *window)
55 {
56     Window wins[2];  /* only ever restack 2 windows. */
57     GList *it, *low;
58     GSList *sit;
59
60     g_assert(stacking_list != NULL); /* this would be bad */
61
62     /* remove the window before looking so we can't run into ourselves and our
63        transients can't either. */
64     stacking_list = g_list_remove(stacking_list, window);
65
66     /* raise transients first */
67     if (WINDOW_IS_CLIENT(window)) {
68         Client *client = WINDOW_AS_CLIENT(window);
69         for (sit = client->transients; sit; sit = sit->next)
70             raise_recursive(sit->data);
71     }
72
73     /* find 'it' where it is the positiion in the stacking order where
74        'window' will be inserted *before* */
75
76     if (WINDOW_IS_CLIENT(window))
77         low = find_lowest_transient(WINDOW_AS_CLIENT(window));
78     else
79         low = NULL;
80     /* the stacking list is from highest to lowest */
81     for (it = g_list_last(stacking_list); it; it = it->prev) {
82         if (it == low || window_layer(window) < window_layer(it->data)) {
83             it = it->next;
84             break;
85         }
86         if (it == stacking_list)
87             break;
88     }
89
90     /*
91       if our new position is the top, we want to stack under the focus_backup.
92       otherwise, we want to stack under the previous window in the stack.
93     */
94     if (it == stacking_list)
95         wins[0] = focus_backup;
96     else if (it != NULL)
97         wins[0] = window_top(it->prev->data);
98     else
99         wins[0] = window_top(g_list_last(stacking_list)->data);
100     wins[1] = window_top(window);
101
102     stacking_list = g_list_insert_before(stacking_list, it, window);
103
104     XRestackWindows(ob_display, wins, 2);
105 }
106
107 void stacking_raise(ObWindow *window)
108 {
109     g_assert(stacking_list != NULL); /* this would be bad */
110
111     if (WINDOW_IS_CLIENT(window)) {
112         Client *client = WINDOW_AS_CLIENT(window);
113         /* move up the transient chain as far as possible first */
114         while (client->transient_for) {
115             if (client->transient_for != TRAN_GROUP) {
116                 client = client->transient_for;
117             } else {
118                 GSList *it;
119
120                 /* the check for TRAN_GROUP is to prevent an infinate loop with
121                    2 transients of the same group at the head of the group's
122                    members list */
123                 for (it = client->group->members; it; it = it->next) {
124                     Client *c = it->data;
125
126                     if (c != client && c->transient_for != TRAN_GROUP) {
127                         client = it->data;
128                         break;
129                     }
130                 }
131                 if (it == NULL) break;
132             }
133         }
134         window = CLIENT_AS_WINDOW(client);
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                 client = client->transient_for;
182             } else {
183                 GSList *it;
184
185                 /* the check for TRAN_GROUP is to prevent an infinate loop with
186                    2 transients of the same group at the head of the group's
187                    members list */
188                 for (it = client->group->members; it; it = it->next) {
189                     Client *c = it->data;
190
191                     if (c != client && c->transient_for != TRAN_GROUP) {
192                         client = it->data;
193                         break;
194                     }
195                 }
196                 if (it == NULL) break;
197             }
198         }
199         window = CLIENT_AS_WINDOW(client);
200     }
201
202     lower_recursive(window, NULL);
203
204     stacking_set_list();
205 }