all my changes while i was offline.
[dana/openbox-history.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 <glib.h>
8
9 GList  *stacking_list = NULL;
10
11 static Window top_window = None;
12
13 void stacking_startup()
14 {
15     XSetWindowAttributes attrib;
16     attrib.override_redirect = TRUE;
17     top_window =  XCreateWindow(ob_display, ob_root,
18                                 -100, -100, 1, 1, 0,
19                                 CopyFromParent, InputOutput, CopyFromParent,
20                                 CWOverrideRedirect, &attrib);
21     XMapWindow(ob_display, top_window);
22 }
23
24 void stacking_shutdown()
25 {
26     XDestroyWindow(ob_display, top_window);
27 }
28
29 void stacking_set_list()
30 {
31     Window *windows, *win_it;
32     GList *it;
33     guint size = g_list_length(stacking_list);
34
35     /* on shutdown, don't update the properties, so that we can read it back
36        in on startup and re-stack the windows as they were before we shut down
37     */
38     if (ob_state == State_Exiting) return;
39
40     /* create an array of the window ids (from bottom to top,
41        reverse order!) */
42     if (size > 0) {
43         windows = g_new(Window, size);
44         win_it = windows;
45         for (it = g_list_last(stacking_list); it != NULL;
46              it = it->prev, ++win_it)
47             *win_it = ((Client*)it->data)->window;
48     } else
49         windows = NULL;
50
51     PROP_SETA32(ob_root, net_client_list_stacking, window,
52                 (guint32*)windows, size);
53
54     if (windows)
55         g_free(windows);
56 }
57
58 static GList *find_lowest_transient(Client *c)
59 {
60     GList *it;
61     GSList *sit;
62
63     for (it = g_list_last(stacking_list); it; it = it->prev)
64         for (sit = c->transients; sit; sit = sit->next)
65             if (it->data == sit->data) /* found a transient */
66                 return it;
67     return NULL;
68 }
69
70 static void raise_recursive(Client *client)
71 {
72     Window wins[2];  /* only ever restack 2 windows. */
73     GList *it, *low;
74     GSList *sit;
75
76     g_assert(stacking_list != NULL); /* this would be bad */
77
78     /* remove the client before looking so we can't run into ourselves and our
79        transients can't either. */
80     stacking_list = g_list_remove(stacking_list, client);
81
82     /* raise transients first */
83     for (sit = client->transients; sit; sit = sit->next)
84         raise_recursive(sit->data);
85
86     /* find 'it' where it is the positiion in the stacking order where
87        'client' will be inserted *before* */
88
89     low = find_lowest_transient(client);
90     /* the stacking list is from highest to lowest */
91     for (it = g_list_last(stacking_list); it; it = it->prev) {
92         if (it == low || client->layer < ((Client*)it->data)->layer) {
93             it = it->next;
94             break;
95         }
96         if (it == stacking_list)
97             break;
98     }
99
100     /*
101       if our new position is the top, we want to stack under the focus_backup.
102       otherwise, we want to stack under the previous window in the stack.
103     */
104     if (it == stacking_list)
105         wins[0] = focus_backup;
106     else if (it != NULL)
107         wins[0] = ((Client*)it->prev->data)->frame->window;
108     else
109         wins[0] = ((Client*)g_list_last(stacking_list)->data)->frame->window;
110     wins[1] = client->frame->window;
111
112     stacking_list = g_list_insert_before(stacking_list, it, client);
113
114     XRestackWindows(ob_display, wins, 2);
115 }
116
117 void stacking_raise(Client *client)
118 {
119     g_assert(stacking_list != NULL); /* this would be bad */
120
121     /* move up the transient chain as far as possible first */
122     while (client->transient_for) {
123         if (client->transient_for != TRAN_GROUP) {
124             client = client->transient_for;
125         } else {
126             GSList *it;
127
128             /* the check for TRAN_GROUP is to prevent an infinate loop with
129                2 transients of the same group at the head of the group's
130                members list */
131             for (it = client->group->members; it; it = it->next) {
132                 Client *c = it->data;
133
134                 if (c != client && c->transient_for != TRAN_GROUP) {
135                     client = it->data;
136                     break;
137                 }
138             }
139             if (it == NULL) break;
140         }
141     }
142
143     raise_recursive(client);
144
145     stacking_set_list();
146 }
147
148 static void lower_recursive(Client *client, Client *above)
149 {
150     Window wins[2];  /* only ever restack 2 windows. */
151     GList *it;
152     GSList *sit;
153
154     /* find 'it' where 'it' is the position in the stacking_list where the
155        'client' will be placed *after* */
156
157     for (it = g_list_last(stacking_list); it != stacking_list; it = it->prev)
158         if (client->layer <= ((Client*)it->data)->layer && it->data != above)
159             break;
160
161     if (it->data != client) { /* not already the bottom */
162         wins[0] = ((Client*)it->data)->frame->window;
163         wins[1] = client->frame->window;
164
165         stacking_list = g_list_remove(stacking_list, client);
166         stacking_list = g_list_insert_before(stacking_list, it->next, client);
167         XRestackWindows(ob_display, wins, 2);
168     }
169
170     for (sit = client->transients; sit; sit = sit->next)
171         lower_recursive(sit->data, client);
172 }
173
174 void stacking_lower(Client *client)
175 {
176     g_assert(stacking_list != NULL); /* this would be bad */
177
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
200     lower_recursive(client, NULL);
201
202     stacking_set_list();
203 }
204
205 void stacking_raise_internal(Window win)
206 {
207     Window wins[2];  /* only ever restack 2 windows. */
208
209     wins[0] = top_window;
210     wins[1] = win;
211
212     XRestackWindows(ob_display, wins, 2);
213 }