+static ObWindow *top_transient(ObWindow *window)
+{
+ Client *client;
+
+ if (!WINDOW_IS_CLIENT(window))
+ return window;
+
+ client = WINDOW_AS_CLIENT(window);
+
+ /* move up the transient chain as far as possible */
+ if (client->transient_for) {
+ if (client->transient_for != TRAN_GROUP) {
+ return top_transient(CLIENT_AS_WINDOW(client->transient_for));
+ } else {
+ GSList *it;
+
+ for (it = client->group->members; it; it = it->next) {
+ Client *c = it->data;
+
+ /* checking transient_for prevents infinate loops! */
+ if (c != client && !c->transient_for)
+ break;
+ }
+ if (it)
+ return it->data;
+ }
+ }
+
+ return window;
+}
+
+void stacking_raise(ObWindow *window)
+{
+ GList *wins;
+
+ window = top_transient(window);
+ wins = pick_windows(window);
+ wins = g_list_concat(wins, pick_group_windows(window));
+ raise(wins);
+ g_list_free(wins);
+}
+
+void stacking_lower(ObWindow *window)
+{
+ GList *wins;
+
+ window = top_transient(window);
+ wins = pick_windows(window);
+ wins = g_list_concat(pick_group_windows(window), wins);
+ lower(wins);
+ g_list_free(wins);
+}
+
+void stacking_add(ObWindow *win)
+{
+ StackLayer l;
+ GList *wins, *it;
+
+ l = window_layer(win);
+ wins = g_list_append(NULL, win); /* list of 1 element */
+
+ stacking_list = g_list_append(stacking_list, win);
+ stacking_raise(win);
+}
+
+void stacking_add_nonintrusive(ObWindow *win)
+{
+ Client *client;
+ Client *parent = NULL;
+ GList *it_before = NULL;
+
+ if (!WINDOW_IS_CLIENT(win)) {
+ stacking_add(win); /* no special rules for others */
+ return;
+ }
+
+ client = WINDOW_AS_CLIENT(win);
+
+ /* insert above its highest parent */
+ if (client->transient_for) {
+ if (client->transient_for != TRAN_GROUP) {
+ parent = client->transient_for;
+ } else {
+ GSList *sit;
+ GList *it;
+
+ if (client->group)
+ for (it = stacking_list; !parent && it; it = it->next) {
+ if ((sit = g_slist_find(client->group->members, it->data)))
+ for (sit = client->group->members; !parent && sit;
+ sit = sit->next) {
+ Client *c = sit->data;
+ /* checking transient_for prevents infinate loops! */
+ if (sit->data == it->data && !c->transient_for)
+ parent = it->data;
+ }
+ }
+ }
+ }
+
+ if (!(it_before = g_list_find(stacking_list, parent))) {
+ /* no parent to put above, try find the focused client to go
+ under */
+ if (focus_client && focus_client->layer == client->layer) {
+ if ((it_before = g_list_find(stacking_list, focus_client)))
+ it_before = it_before->next;
+ }
+ }
+ if (!it_before) {
+ /* out of ideas, just add it normally... */
+ stacking_add(win);
+ } else {
+ GList *wins = g_list_append(NULL, win);
+ do_restack(wins, it_before);
+ g_list_free(wins);
+ }
+}