+void stacking_lower(ObWindow *window)
+{
+ if (WINDOW_IS_CLIENT(window)) {
+ ObClient *selected;
+ selected = WINDOW_AS_CLIENT(window);
+ restack_windows(selected, FALSE);
+ } else {
+ GList *wins;
+ wins = g_list_append(NULL, window);
+ stacking_list = g_list_remove(stacking_list, window);
+ do_lower(wins);
+ g_list_free(wins);
+ }
+}
+
+void stacking_below(ObWindow *window, ObWindow *below)
+{
+ GList *wins, *before;
+
+ if (window_layer(window) != window_layer(below))
+ return;
+
+ wins = g_list_append(NULL, window);
+ stacking_list = g_list_remove(stacking_list, window);
+ before = g_list_next(g_list_find(stacking_list, below));
+ do_restack(wins, before);
+ g_list_free(wins);
+}
+
+void stacking_add(ObWindow *win)
+{
+ g_assert(screen_support_win != None); /* make sure I dont break this in the
+ future */
+
+ stacking_list = g_list_append(stacking_list, win);
+ stacking_raise(win);
+}
+
+static GList *find_highest_relative(ObClient *client)
+{
+ GList *ret = NULL;
+
+ if (client->transient_for) {
+ GList *it;
+ GSList *top;
+
+ /* get all top level relatives of this client */
+ top = client_search_all_top_parents_layer(client);
+
+ /* go from the top of the stacking order down */
+ for (it = stacking_list; !ret && it; it = g_list_next(it)) {
+ if (WINDOW_IS_CLIENT(it->data)) {
+ ObClient *c = it->data;
+ /* only look at windows in the same layer */
+ if (c->layer == client->layer) {
+ GSList *sit;
+
+ /* go through each top level parent and see it this window
+ is related to them */
+ for (sit = top; !ret && sit; sit = g_slist_next(sit)) {
+ ObClient *topc = sit->data;
+
+ /* are they related ? */
+ if (topc == c || client_search_transient(topc, c))
+ ret = it;
+ }
+ }
+ }
+ }
+ }
+ return ret;
+}
+
+void stacking_add_nonintrusive(ObWindow *win)
+{
+ ObClient *client;
+ GList *it_below = 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 (or its highest child !) */
+ it_below = find_highest_relative(client);
+
+ if (!it_below) {
+ /* nothing to put it directly above, so try find the focused client to
+ put it underneath it */
+ if (focus_client && focus_client->layer == client->layer) {
+ if ((it_below = g_list_find(stacking_list, focus_client)))
+ it_below = it_below->next;
+ }
+ }
+ if (!it_below) {
+ /* there is no window to put this directly above, so put it at the
+ bottom */
+ stacking_list = g_list_prepend(stacking_list, win);
+ stacking_lower(win);
+ } else {
+ /* make sure it's not in the wrong layer though ! */
+ for (; it_below; it_below = g_list_next(it_below))
+ {
+ /* stop when the window is not in a higher layer than the window
+ it is going above (it_below) */
+ if (client->layer >= window_layer(it_below->data))
+ break;
+ }
+ for (; it_below != stacking_list;
+ it_below = g_list_previous(it_below))
+ {
+ /* stop when the window is not in a lower layer than the
+ window it is going under (it_above) */
+ GList *it_above = g_list_previous(it_below);
+ if (client->layer <= window_layer(it_above->data))
+ break;
+ }
+
+ GList *wins = g_list_append(NULL, win);
+ do_restack(wins, it_below);
+ g_list_free(wins);
+ }
+}