+ 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 && client != focus_client) {
+ /* 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) {
+ if (client == focus_client) {
+ /* it's focused so put it at the top */
+ stacking_list = g_list_append(stacking_list, win);
+ stacking_raise(win);
+ } else {
+ /* 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);
+ }