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