+static void do_lower(GList *wins)
+{
+ GList *it;
+ GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
+ gint i;
+
+ for (it = wins; it; it = g_list_next(it)) {
+ ObStackingLayer l;
+
+ l = window_layer(it->data);
+ layer[l] = g_list_append(layer[l], it->data);
+ }
+
+ it = stacking_list;
+ for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
+ if (layer[i]) {
+ for (; it; it = g_list_next(it)) {
+ /* look for the top of the next layer down */
+ if (window_layer(it->data) < (ObStackingLayer) i)
+ break;
+ }
+ do_restack(layer[i], it);
+ g_list_free(layer[i]);
+ }
+ }
+}
+
+static void restack_windows(ObClient *selected, gboolean raise)
+{
+ GList *it, *last, *below, *above, *next;
+ GList *wins = NULL;
+
+ GList *group_helpers = NULL;
+ GList *group_modals = NULL;
+ GList *group_trans = NULL;
+ GList *modals = NULL;
+ GList *trans = NULL;
+
+ if (raise) {
+ ObClient *p;
+
+ /* if a window is modal for another single window, then raise it to the
+ top too, the same is done with the focus order */
+ while (selected->modal && (p = client_direct_parent(selected)))
+ selected = p;
+ }
+
+ /* remove first so we can't run into ourself */
+ it = g_list_find(stacking_list, selected);
+ g_assert(it);
+ stacking_list = g_list_delete_link(stacking_list, it);
+
+ /* go from the bottom of the stacking list up. don't move any other windows
+ when lowering, we call this for each window independently */
+ if (raise) {
+ for (it = g_list_last(stacking_list); it; it = next) {
+ next = g_list_previous(it);
+
+ if (WINDOW_IS_CLIENT(it->data)) {
+ ObClient *ch = it->data;
+
+ /* only move windows in the same stacking layer */
+ if (ch->layer == selected->layer &&
+ /* looking for windows that are transients, and so would
+ remain above the selected window */
+ client_search_transient(selected, ch))
+ {
+ if (client_is_direct_child(selected, ch)) {
+ if (ch->modal)
+ modals = g_list_prepend(modals, ch);
+ else
+ trans = g_list_prepend(trans, ch);
+ }
+ else if (client_helper(ch)) {
+ if (selected->transient) {
+ /* helpers do not stay above transient windows */
+ continue;
+ }
+ group_helpers = g_list_prepend(group_helpers, ch);
+ }
+ else {
+ if (ch->modal)
+ group_modals = g_list_prepend(group_modals, ch);
+ else
+ group_trans = g_list_prepend(group_trans, ch);
+ }
+ stacking_list = g_list_delete_link(stacking_list, it);
+ }
+ }
+ }
+ }
+
+ /* put modals above other direct transients */
+ wins = g_list_concat(modals, trans);
+
+ /* put helpers below direct transients */
+ wins = g_list_concat(wins, group_helpers);
+
+ /* put the selected window right below these children */
+ wins = g_list_append(wins, selected);
+
+ /* if selected window is transient for group then raise it above others */
+ if (selected->transient_for_group) {
+ /* if it's modal, raise it above those also */
+ if (selected->modal) {
+ wins = g_list_concat(wins, group_modals);
+ group_modals = NULL;
+ }
+ wins = g_list_concat(wins, group_trans);
+ group_trans = NULL;
+ }
+
+ /* find where to put the selected window, start from bottom of list,
+ this is the window below everything we are re-adding to the list */
+ last = NULL;
+ for (it = g_list_last(stacking_list); it; it = g_list_previous(it))
+ {
+ if (window_layer(it->data) < selected->layer) {
+ last = it;
+ continue;
+ }
+ /* if lowering, stop at the beginning of the layer */
+ if (!raise)
+ break;
+ /* if raising, stop at the end of the layer */
+ if (window_layer(it->data) > selected->layer)
+ break;
+
+ last = it;
+ }
+
+ /* save this position in the stacking list */
+ below = last;
+
+ /* find where to put the group transients, start from the top of list */
+ for (it = stacking_list; it; it = g_list_next(it)) {
+ /* skip past higher layers */
+ if (window_layer(it->data) > selected->layer)
+ continue;
+ /* if we reach the end of the layer (how?) then don't go further */
+ if (window_layer(it->data) < selected->layer)