Index: display.c
===================================================================
RCS file: /cvs/gnome/metacity/src/display.c,v
retrieving revision 1.184
diff -p -u -r1.184 display.c
--- display.c	16 May 2003 21:59:07 -0000	1.184
+++ display.c	17 May 2003 01:24:16 -0000
@@ -474,6 +474,7 @@ meta_display_open (const char *name)
   display->ungrab_should_not_cause_focus_window = None;
   
   display->current_time = CurrentTime;
+  display->ignore_enternotify_until = 0;
   
   display->grab_op = META_GRAB_OP_NONE;
   display->grab_window = NULL;
@@ -1435,7 +1436,8 @@ event_callback (XEvent   *event,
         meta_window_handle_mouse_grab_op_event (window, event);
       /* do this even if window->has_focus to avoid races */
       else if (window && !serial_is_ignored (display, event->xany.serial) &&
-               event->xcrossing.detail != NotifyInferior)
+               event->xcrossing.detail != NotifyInferior &&
+	       event->xcrossing.time > display->ignore_enternotify_until)
         {
           switch (meta_prefs_get_focus_mode ())
             {
@@ -1804,7 +1806,7 @@ event_callback (XEvent   *event,
                                                         space);
 
                   if (workspace)
-                    meta_workspace_activate (workspace);
+                    meta_workspace_activate (workspace, NULL);
                   else
                     meta_verbose ("Don't know about workspace %d\n", space);
                 }
Index: display.h
===================================================================
RCS file: /cvs/gnome/metacity/src/display.h,v
retrieving revision 1.101
diff -p -u -r1.101 display.h
--- display.h	15 Mar 2003 02:16:21 -0000	1.101
+++ display.h	17 May 2003 01:24:17 -0000
@@ -249,6 +249,7 @@ struct _MetaDisplay
   MetaResizePopup *grab_resize_popup;
   GTimeVal    grab_last_moveresize_time;
   Time        grab_motion_notify_time;
+  Time        ignore_enternotify_until;
 #ifdef HAVE_XKB
   int         xkb_base_event_type;
 #endif
Index: keybindings.c
===================================================================
RCS file: /cvs/gnome/metacity/src/keybindings.c,v
retrieving revision 1.85
diff -p -u -r1.85 keybindings.c
--- keybindings.c	17 May 2003 00:00:55 -0000	1.85
+++ keybindings.c	17 May 2003 01:24:19 -0000
@@ -2328,7 +2328,7 @@ switch_to_workspace (MetaDisplay *displa
         }
     }
       
-  meta_workspace_activate (workspace);
+  meta_workspace_activate (workspace, NULL);
 
   if (move_window)
     {
@@ -3022,7 +3022,7 @@ do_handle_move_to_workspace  (MetaDispla
       /* Activate second, so the window is never unmapped */
       meta_window_change_workspace (window, workspace);
       if (flip)
-        meta_workspace_activate (workspace);
+        meta_workspace_activate (workspace, window);
     }
   else
     {
Index: screen.c
===================================================================
RCS file: /cvs/gnome/metacity/src/screen.c,v
retrieving revision 1.106
diff -p -u -r1.106 screen.c
--- screen.c	16 May 2003 21:59:07 -0000	1.106
+++ screen.c	17 May 2003 01:24:21 -0000
@@ -599,7 +599,7 @@ meta_screen_new (MetaDisplay *display,
   /* Screens must have at least one workspace at all times,
    * so create that required workspace.
    */
-  meta_workspace_activate (meta_workspace_new (screen));
+  meta_workspace_activate (meta_workspace_new (screen), NULL);
   update_num_workspaces (screen);
   
   set_workspace_names (screen);
@@ -636,7 +636,7 @@ meta_screen_new (MetaDisplay *display,
                                                 current_workspace);
     
     if (space != NULL)
-      meta_workspace_activate (space);
+      meta_workspace_activate (space, NULL);
   }
   
   meta_verbose ("Added screen %d ('%s') root 0x%lx\n",
@@ -999,7 +999,7 @@ update_num_workspaces (MetaScreen *scree
     }
 
   if (need_change_space)
-    meta_workspace_activate (last_remaining);
+    meta_workspace_activate (last_remaining, NULL);
 
   /* Should now be safe to free the workspaces */
   tmp = extras;
Index: window.c
===================================================================
RCS file: /cvs/gnome/metacity/src/window.c,v
retrieving revision 1.276
diff -p -u -r1.276 window.c
--- window.c	16 May 2003 21:59:08 -0000	1.276
+++ window.c	17 May 2003 01:24:27 -0000
@@ -3065,8 +3065,8 @@ meta_window_focus (MetaWindow  *window,
                    Time         timestamp)
 {  
   meta_topic (META_DEBUG_FOCUS,
-              "Setting input focus to window %s, input: %d take_focus: %d\n",
-              window->desc, window->input, window->take_focus);
+              "Setting input focus to window %s (Time %i), input: %d take_focus: %d\n",
+              window->desc, timestamp, window->input, window->take_focus);
   
   if (window->display->grab_window &&
       window->display->grab_window->all_keys_grabbed)
Index: workspace.c
===================================================================
RCS file: /cvs/gnome/metacity/src/workspace.c,v
retrieving revision 1.42
diff -p -u -r1.42 workspace.c
--- workspace.c	23 Feb 2003 17:09:46 -0000	1.42
+++ workspace.c	17 May 2003 01:24:27 -0000
@@ -196,7 +196,8 @@ meta_workspace_queue_calc_showing  (Meta
 }
 
 void
-meta_workspace_activate (MetaWorkspace *workspace)
+meta_workspace_activate (MetaWorkspace *workspace,
+                         MetaWindow    *focus_this)
 {
   MetaWorkspace *old;
   
@@ -218,8 +219,25 @@ meta_workspace_activate (MetaWorkspace *
   meta_workspace_queue_calc_showing (old);
   meta_workspace_queue_calc_showing (workspace);
 
-  meta_topic (META_DEBUG_FOCUS, "Focusing default window on new workspace\n");
-  meta_screen_focus_default_window (workspace->screen, NULL);
+  if (focus_this)
+    {
+      /* to avoid a race condition where EnterNotify events created by
+       * the workspace switch will cause the focused window to change
+       * in unexpected ways on a workspace switch, we set the display
+       * to ignore EnterNotify events until 500 time units have
+       * passed.  This is a horrible ugly hack (TM).
+       */ 
+
+      Time now = meta_display_get_current_time (focus_this->display);
+      focus_this->display->ignore_enternotify_until = now + 500;
+      meta_window_focus (focus_this,
+			 now);
+    }
+  else
+    {
+      meta_topic (META_DEBUG_FOCUS, "Focusing default window on new workspace\n");
+      meta_screen_focus_default_window (workspace->screen, NULL);
+    }
 }
 
 int
Index: workspace.h
===================================================================
RCS file: /cvs/gnome/metacity/src/workspace.h,v
retrieving revision 1.14
diff -p -u -r1.14 workspace.h
--- workspace.h	23 Feb 2003 17:09:46 -0000	1.14
+++ workspace.h	17 May 2003 01:24:28 -0000
@@ -58,7 +58,8 @@ void           meta_workspace_relocate_w
 /* don't confuse with meta_window_visible_on_workspace() */
 gboolean       meta_workspace_contains_window (MetaWorkspace *workspace,
                                                MetaWindow  *window);
-void           meta_workspace_activate      (MetaWorkspace *workspace);
+void           meta_workspace_activate      (MetaWorkspace *workspace,
+                                             MetaWindow    *focus_this);
 int            meta_workspace_index         (MetaWorkspace *workspace);
 GList*         meta_workspace_list_windows  (MetaWorkspace *workspace);
 
