Index: src/delete.c
===================================================================
RCS file: /cvs/gnome/metacity/src/delete.c,v
retrieving revision 1.9
diff -u -r1.9 delete.c
--- src/delete.c	29 Sep 2003 17:35:11 -0000	1.9
+++ src/delete.c	1 Mar 2004 00:38:33 -0000
@@ -396,10 +396,10 @@
        * I don't know how to avoid that though.
        */
       meta_topic (META_DEBUG_FOCUS,
-                  "Focusing MRU window because focus window %s was deleted/killed\n",
+                  "Focusing default window because focus window %s was deleted/killed\n",
                   window->desc);
-      meta_workspace_focus_mru_window (window->screen->active_workspace,
-                                       window);
+      meta_workspace_focus_default_window (window->screen->active_workspace,
+                                           window);
 #else
       meta_topic (META_DEBUG_FOCUS,
                   "Not unfocusing %s on delete/kill\n",
Index: src/screen.c
===================================================================
RCS file: /cvs/gnome/metacity/src/screen.c,v
retrieving revision 1.123
diff -u -r1.123 screen.c
--- src/screen.c	21 Jan 2004 17:33:56 -0000	1.123
+++ src/screen.c	1 Mar 2004 00:38:51 -0000
@@ -1224,9 +1224,9 @@
   /* don't show tab popup, since proper space isn't selected yet */
 }
 
-void
-meta_screen_focus_mouse_window (MetaScreen  *screen,
-                                MetaWindow  *not_this_one)
+MetaWindow*
+meta_screen_get_mouse_window (MetaScreen  *screen,
+                              MetaWindow  *not_this_one)
 {
   MetaWindow *window;
   Window root_return, child_return;
@@ -1256,22 +1256,7 @@
                                                          root_x_return,
                                                          root_y_return);
 
-  /* FIXME I'm a loser on the CurrentTime front */
-  if (window)
-    {
-      meta_topic (META_DEBUG_FOCUS,
-                  "Focusing mouse window %s\n", window->desc);
-
-      meta_window_focus (window, meta_display_get_current_time (screen->display));
-
-      /* Also raise the window if in click-to-focus */
-      if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_CLICK)
-        meta_window_raise (window);
-    }
-  else
-    {
-      meta_topic (META_DEBUG_FOCUS, "No mouse window to focus found\n");
-    }
+  return window;
 }
 
 const MetaXineramaScreenInfo*
Index: src/screen.h
===================================================================
RCS file: /cvs/gnome/metacity/src/screen.h,v
retrieving revision 1.56
diff -u -r1.56 screen.h
--- src/screen.h	28 Feb 2004 01:48:27 -0000	1.56
+++ src/screen.h	1 Mar 2004 00:38:52 -0000
@@ -148,7 +148,7 @@
 
 void          meta_screen_ensure_workspace_popup (MetaScreen *screen);
 
-void          meta_screen_focus_mouse_window   (MetaScreen                 *screen,
+MetaWindow*   meta_screen_get_mouse_window     (MetaScreen                 *screen,
                                                 MetaWindow                 *not_this_one);
 
 const MetaXineramaScreenInfo* meta_screen_get_current_xinerama    (MetaScreen    *screen);
Index: src/window.c
===================================================================
RCS file: /cvs/gnome/metacity/src/window.c,v
retrieving revision 1.325
diff -u -r1.325 window.c
--- src/window.c	28 Feb 2004 02:53:55 -0000	1.325
+++ src/window.c	1 Mar 2004 00:39:10 -0000
@@ -909,17 +909,17 @@
   if (window->has_focus)
     {
       meta_topic (META_DEBUG_FOCUS,
-                  "Focusing top window since we're unmanaging %s\n",
+                  "Focusing default window since we're unmanaging %s\n",
                   window->desc);
-      meta_workspace_focus_mru_window (window->screen->active_workspace, window);
+      meta_workspace_focus_default_window (window->screen->active_workspace, window);
     }
   else if (window->display->expected_focus_window == window)
     {
       meta_topic (META_DEBUG_FOCUS,
-                  "Focusing top window since expected focus window freed %s\n",
+                  "Focusing default window since expected focus window freed %s\n",
                   window->desc);
       window->display->expected_focus_window = NULL;
-      meta_workspace_focus_mru_window (window->screen->active_workspace, window);
+      meta_workspace_focus_default_window (window->screen->active_workspace, window);
     }
   else
     {
@@ -1064,6 +1064,8 @@
 
   meta_icon_cache_free (&window->icon_cache);
   
+  meta_display_increment_focus_sentinel (window->display);
+
   g_free (window->sm_client_id);
   g_free (window->wm_client_machine);
   g_free (window->startup_id);
@@ -1358,7 +1360,7 @@
   GSList *should_hide;
   GSList *unplaced;
   GSList *displays;
-  
+
   meta_topic (META_DEBUG_WINDOW_STATE,
               "Clearing the calc_showing queue\n");
 
@@ -1463,7 +1465,8 @@
   /* for all displays used in the queue, set a sentinel property on
    * the root window so that we can ignore EnterNotify events that
    * occur before the window maps occur.  This avoids a race
-   * condition. */
+   * condition.
+   */
   tmp = should_show;
   while (tmp != NULL)
     {
@@ -1477,6 +1480,23 @@
 
       tmp = tmp->next;
     }
+  /* There's also a potential race condition on window minimize.
+   * So we need to avoid that to, in the same manner.  As with the
+   * case above, this race condition is only for sloppy/mouse focus.
+   */
+  tmp = should_hide;
+  while (tmp != NULL)
+    {
+      MetaWindow *window = tmp->data;
+      
+      if (g_slist_find (displays, window->display) == NULL)
+        {
+          displays = g_slist_prepend (displays, window->display);
+          meta_display_increment_focus_sentinel (window->display);
+        }
+
+      tmp = tmp->next;
+    }
 
   g_slist_free (copy);
 
@@ -1814,9 +1834,9 @@
       if (window->has_focus)
         {
           meta_topic (META_DEBUG_FOCUS,
-                      "Focusing top window due to minimization of focus window %s\n",
+                      "Focusing default window due to minimization of focus window %s\n",
                       window->desc);
-          meta_workspace_focus_mru_window (window->screen->active_workspace, window);
+          meta_workspace_focus_default_window (window->screen->active_workspace, window);
         }
       else
         {
Index: src/workspace.c
===================================================================
RCS file: /cvs/gnome/metacity/src/workspace.c,v
retrieving revision 1.56
diff -u -r1.56 workspace.c
--- src/workspace.c	28 Feb 2004 02:53:55 -0000	1.56
+++ src/workspace.c	1 Mar 2004 00:39:14 -0000
@@ -784,7 +784,21 @@
   if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_CLICK)
     meta_workspace_focus_mru_window (workspace, not_this_one);
   else
-    meta_screen_focus_mouse_window (workspace->screen, not_this_one);
+    {
+      MetaWindow * window;
+      window = meta_screen_get_mouse_window (workspace->screen, not_this_one);
+      if (window)
+        {
+          meta_topic (META_DEBUG_FOCUS,
+                      "Focusing mouse window %s\n", window->desc);
+
+          meta_window_focus (window, meta_display_get_current_time (workspace->screen->display));
+        }
+      else if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_SLOPPY)
+        meta_workspace_focus_mru_window (workspace, not_this_one);
+      else if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_MOUSE)
+        meta_topic (META_DEBUG_FOCUS, "No mouse window to focus found\n");
+    }
 }
 
 /* Focus MRU window (or top window if failed) on active workspace */
