- winfocus = TAILQ_PREV(win, ws_win_list, entry);
- if (TAILQ_FIRST(&ws->winlist) == win)
- winfocus = TAILQ_NEXT(win, entry);
- else {
- winfocus = TAILQ_PREV(ws->focus, ws_win_list, entry);
- if (winfocus == NULL)
- winfocus = TAILQ_LAST(&ws->winlist, ws_win_list);
+ /* if we are max_stack try harder to focus on something */
+ if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
+ if (win->transient)
+ winfocus = find_window(win->transient);
+ else if (win != ws->focus_prev)
+ winfocus = ws->focus_prev;
+ else if (win != ws->focus)
+ winfocus = ws->focus;
+ }
+
+ /* normal and fallback if haven't found anything to focus on */
+ if (winfocus == NULL) {
+ winfocus = TAILQ_PREV(win, ws_win_list, entry);
+ if (TAILQ_FIRST(&ws->winlist) == win)
+ winfocus = TAILQ_NEXT(win, entry);
+ else {
+ if (ws->focus)
+ winfocus = TAILQ_PREV(ws->focus,
+ ws_win_list, entry);
+ if (winfocus == NULL)
+ winfocus = TAILQ_LAST(&ws->winlist,
+ ws_win_list);
+ }