- } while (target == _clients.end() ||
- (*target)->iconic() ||
- (! alldesktops && (*target)->desktop() != _active_desktop) ||
- (sameclass && ! classname.empty() &&
- (*target)->appClass() != classname));
-
- if (target != _clients.end())
- (*target)->focus();
+
+ // must be no window to focus
+ if (target == _active)
+ return;
+
+ // determine if this window is invalid for cycling to
+ const XWindow *t = *target;
+ if (t->iconic()) continue;
+ if (! allscreens && t->getScreen() != this) continue;
+ if (! alldesktops && ! (t->desktop() == _active_desktop ||
+ t->desktop() == 0xffffffff)) continue;
+ if (sameclass && ! classname.empty() &&
+ t->appClass() != classname) continue;
+ if (! t->canFocus()) continue;
+
+ // found a good window!
+ t->focus();
+ return;
+ }