- XEvent fi, fo;
- gboolean isfo = FALSE;
-
- if (XCheckTypedEvent(ob_display, FocusIn, &fi)) {
- event_process(&fi);
-
- /* when we have gotten a fi/fo pair, then see if there are any
- more fo's coming. if there are, then don't fallback just yet
- */
- if ((isfo = XCheckTypedEvent(ob_display, FocusOut, &fo)))
- XPutBackEvent(ob_display, &fo);
-
- /* secret magic way of event_process telling us that no client
- was found for the FocusIn event. ^_^ */
- if (!isfo && fi.xfocus.window == None)
- focus_fallback(Fallback_NoFocus);
- if (fi.xfocus.window == e->xfocus.window)
- return TRUE;
- } else
+ XEvent fe;
+ gboolean fallback = TRUE;
+
+ while (TRUE) {
+ if (!XCheckTypedWindowEvent(ob_display, FocusOut,
+ e->xfocus.window,&fe))
+ if (!XCheckTypedEvent(ob_display, FocusIn, &fe))
+ break;
+ if (fe.type == FocusOut) {
+#ifdef DEBUG_FOCUS
+ g_message("found pending FocusOut");
+#endif
+ if (!INVALID_FOCUSOUT(&fe)) {
+ /* if there is a VALID FocusOut still coming, don't
+ fallback focus yet, we'll deal with it then */
+ XPutBackEvent(ob_display, &fe);
+ fallback = FALSE;
+ break;
+ }
+ } else {
+#ifdef DEBUG_FOCUS
+ g_message("found pending FocusIn");
+#endif
+ /* is the focused window getting a FocusOut/In back to
+ itself? */
+ if (fe.xfocus.window == e->xfocus.window &&
+ !event_ignore(&fe, client)) {
+#ifdef DEBUG_FOCUS
+ g_message("focused window got an Out/In back to "
+ "itself IGNORED both");
+#endif
+ return TRUE;
+ }
+
+ /* once all the FocusOut's have been dealt with, if there
+ is a FocusIn still left and it is valid, then use it */
+ event_process(&fe);
+ /* secret magic way of event_process telling us that no
+ client was found for the FocusIn event. ^_^ */
+ if (fe.xfocus.window != None) {
+ fallback = FALSE;
+ break;
+ }
+ }
+ }
+ if (fallback) {
+#ifdef DEBUG_FOCUS
+ g_message("no valid FocusIn and no FocusOut events found, "
+ "falling back");
+#endif