watch for windows without frames, they can be in the transient order before they...
[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 <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;
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         if (((Client*)sit->data)->frame)
85             raise_recursive(sit->data);
86
87     /* find 'it' where it is the positiion in the stacking order where
88        'client' will be inserted *before* */
89
90     it = find_lowest_transient(client);
91     if (it)
92         it = it->next;
93     else {
94         /* the stacking list is from highest to lowest */
95         for (it = stacking_list; it; it = it->next) {
96             if (client->layer >= ((Client*)it->data)->layer)
97                 break;
98         }
99     }
100
101     /*
102       if our new position is the top, we want to stack under the focus_backup.
103       otherwise, we want to stack under the previous window in the stack.
104     */
105     if (it == stacking_list)
106         wins[0] = focus_backup;
107     else if (it != NULL)
108         wins[0] = ((Client*)it->prev->data)->frame->window;
109     else
110         wins[0] = ((Client*)g_list_last(stacking_list)->data)->frame->window;
111     wins[1] = client->frame->window;
112
113     stacking_list = g_list_insert_before(stacking_list, it, client);
114
115     XRestackWindows(ob_display, wins, 2);
116 }
117
118 void stacking_raise(Client *client)
119 {
120     g_assert(stacking_list != NULL); /* this would be bad */
121
122     /* move up the transient chain as far as possible first */
123     while (client->transient_for) {
124         if (client->transient_for != TRAN_GROUP) {
125             client = client->transient_for;
126         } else {
127             GSList *it;
128
129             /* the check for TRAN_GROUP is to prevent an infinate loop with
130                2 transients of the same group at the head of the group's
131                members list */
132             for (it = client->group->members; it; it = it->next) {
133                 Client *c = it->data;
134
135                 if (c != client && c->transient_for != TRAN_GROUP) {
136                     client = it->data;
137                     break;
138                 }
139             }
140             if (it == NULL) break;
141         }
142     }
143
144     raise_recursive(client);
145
146     stacking_set_list();
147 }
148
149 static void lower_recursive(Client *client, Client *above)
150 {
151     Window wins[2];  /* only ever restack 2 windows. */
152     GList *it;
153     GSList *sit;
154
155     /* find 'it' where 'it' is the position in the stacking_list where the
156        'client' will be placed *after* */
157
158     for (it = g_list_last(stacking_list); it != stacking_list; it = it->prev)
159         if (client->layer <= ((Client*)it->data)->layer && it->data != above)
160             break;
161
162     if (it->data != client) { /* not already the bottom */
163         wins[0] = ((Client*)it->data)->frame->window;
164         wins[1] = client->frame->window;
165
166         stacking_list = g_list_remove(stacking_list, client);
167         stacking_list = g_list_insert_before(stacking_list, it->next, client);
168         XRestackWindows(ob_display, wins, 2);
169     }
170
171     for (sit = client->transients; sit; sit = sit->next)
172         lower_recursive(sit->data, client);
173 }
174
175 void stacking_lower(Client *client)
176 {
177     g_assert(stacking_list != NULL); /* this would be bad */
178
179     /* move up the transient chain as far as possible first */
180     while (client->transient_for) {
181         if (client->transient_for != TRAN_GROUP) {
182             client = client->transient_for;
183         } else {
184             GSList *it;
185
186             /* the check for TRAN_GROUP is to prevent an infinate loop with
187                2 transients of the same group at the head of the group's
188                members list */
189             for (it = client->group->members; it; it = it->next) {
190                 Client *c = it->data;
191
192                 if (c != client && c->transient_for != TRAN_GROUP) {
193                     client = it->data;
194                     break;
195                 }
196             }
197             if (it == NULL) break;
198         }
199     }
200
201     lower_recursive(client, NULL);
202
203     stacking_set_list();
204 }
205
206 void stacking_raise_internal(Window win)
207 {
208     Window wins[2];  /* only ever restack 2 windows. */
209
210     wins[0] = top_window;
211     wins[1] = win;
212
213     XRestackWindows(ob_display, wins, 2);
214 }