- if (!(it_before = g_list_find(stacking_list, parent))) {
- /* no parent to put above, try find the focused client to go
- under */
- if ((it_before = g_list_find(stacking_list, focus_client)))
- it_before = it_before->next;
- else {
- /* out of ideas, just add it normally... */
- stacking_add(win);
- return;
- }
+ if (!(it_below = 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_below = g_list_find(stacking_list, focus_client)))
+ it_below = it_below->next;
+ }
+ }
+ if (!it_below) {
+ /* out of ideas, just add it normally... */
+ stacking_add(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;