Index: nautilus-directory-background.c
===================================================================
RCS file: /cvs/gnome/nautilus/libnautilus-private/nautilus-directory-background.c,v
retrieving revision 1.95
diff -u -p -r1.95 nautilus-directory-background.c
--- nautilus-directory-background.c	31 Jan 2005 19:35:24 -0000	1.95
+++ nautilus-directory-background.c	12 Mar 2005 12:35:45 -0000
@@ -365,7 +365,13 @@ desktop_background_destroyed_callback (E
 static void
 desktop_background_gconf_notify_cb (GConfClient *client, guint notification_id, GConfEntry *entry, gpointer data)
 {
-	call_settings_changed (EEL_BACKGROUND (data));
+	/* 
+	 * Wallpaper capplet changes picture, background color and placement with
+	 * gconf_change_set API, but unfortunately, this operation is not atomic in
+	 * GConf as it should be. So we update background after small timeout to
+	 * let GConf change all values.
+	 */
+	g_timeout_add (300, (GSourceFunc)call_settings_changed, EEL_BACKGROUND (data));
 }
 
 static void
@@ -382,109 +388,82 @@ nautilus_file_background_receive_gconf_c
                           G_CALLBACK (desktop_background_destroyed_callback), NULL);
 }
 
-/* Create a persistent pixmap. We create a separate display
- * and set the closedown mode on it to RetainPermanent
- * (copied from gnome-source/control-panels/capplets/background-properties/render-background.c)
- */
-static GdkPixmap *
-make_root_pixmap (GdkScreen *screen, gint width, gint height)
-{
-	Display *display;
-        const char *display_name;
-	Pixmap result;
-	GdkPixmap *gdk_pixmap;
-	int screen_num;
-
-	screen_num = gdk_screen_get_number (screen);
-
-	gdk_flush ();
-
-	display_name = gdk_display_get_name (gdk_screen_get_display (screen));
-	display = XOpenDisplay (display_name);
-
-        if (display == NULL) {
-                g_warning ("Unable to open display '%s' when setting background pixmap\n",
-                           (display_name) ? display_name : "NULL");
-                return NULL;
-        }
-
-	XSetCloseDownMode (display, RetainPermanent);
-
-	result = XCreatePixmap (display,
-				RootWindow (display, screen_num),
-				width, height,
-				DefaultDepth (display, screen_num));
-
-	XCloseDisplay (display);
-
-	gdk_pixmap = gdk_pixmap_foreign_new (result);
-	gdk_drawable_set_colormap (GDK_DRAWABLE (gdk_pixmap),
-				   gdk_drawable_get_colormap (gdk_screen_get_root_window (screen)));
-
-	return gdk_pixmap;
-}
-
 /* Set the root pixmap, and properties pointing to it. We
  * do this atomically with XGrabServer to make sure that
  * we won't leak the pixmap if somebody else it setting
  * it at the same time. (This assumes that they follow the
  * same conventions we do
- * (copied from gnome-source/control-panels/capplets/background-properties/render-background.c)
+ * (copied from gnome-control-center/libbackground/applier.c)
  */
 static void 
 set_root_pixmap (GdkPixmap *pixmap, GdkScreen *screen)
 {
-	int      result;
-	gint     format;
-	gulong   nitems;
-	gulong   bytes_after;
-	guchar  *data_esetroot;
-	Pixmap   pixmap_id;
-	Atom     type;
+	Atom type;
+	gulong nitems, bytes_after;
+	gint format;
+	guchar *data_esetroot;
+	Pixmap pixmap_id;
 	Display *display;
-	int      screen_num;
+	int screen_num;
 
 	screen_num = gdk_screen_get_number (screen);
 
-	data_esetroot = NULL;
-	display = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen));
+	if (pixmap != NULL)
+		pixmap_id = GDK_WINDOW_XWINDOW (pixmap);
+	else
+		pixmap_id = 0;
+
+	display = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
 
 	XGrabServer (display);
 
-	result = XGetWindowProperty (display, RootWindow (display, screen_num),
-				     gdk_x11_get_xatom_by_name ("ESETROOT_PMAP_ID"),
-				     0L, 1L, False, XA_PIXMAP,
-				     &type, &format, &nitems, &bytes_after,
-				     &data_esetroot);
-
-	if (data_esetroot != NULL) {
-		if (result == Success && type == XA_PIXMAP && format == 32 && nitems == 1) {
-			gdk_error_trap_push ();
-			XKillClient (display, *(Pixmap *)data_esetroot);
-			gdk_flush ();
-			gdk_error_trap_pop ();
+	XGetWindowProperty (display, RootWindow (display, screen_num),
+			    XInternAtom (display, "ESETROOT_PMAP_ID", False),
+			    0L, 1L, False, XA_PIXMAP,
+			    &type, &format, &nitems, &bytes_after,
+			    &data_esetroot);
+
+	if (type == XA_PIXMAP) {
+		if (format == 32 && nitems == 1) {
+			Pixmap old_pixmap;
+
+			old_pixmap = *((Pixmap *) data_esetroot);
+
+			if (pixmap != NULL && old_pixmap != pixmap_id) {
+				gdk_error_trap_push ();
+				XKillClient (display, old_pixmap);
+				gdk_flush ();
+				gdk_error_trap_pop ();
+			}
+			else if (pixmap == NULL)
+				pixmap_id = old_pixmap;
 		}
+
 		XFree (data_esetroot);
 	}
 
-	pixmap_id = GDK_WINDOW_XWINDOW (pixmap);
-
-	XChangeProperty (display, RootWindow (display, screen_num),
-			 gdk_x11_get_xatom_by_name ("ESETROOT_PMAP_ID"), XA_PIXMAP,
-			 32, PropModeReplace,
-			 (guchar *) &pixmap_id, 1);
-	XChangeProperty (display, RootWindow (display, screen_num),
-			 gdk_x11_get_xatom_by_name ("_XROOTPMAP_ID"), XA_PIXMAP,
-			 32, PropModeReplace,
-			 (guchar *) &pixmap_id, 1);
+	if (pixmap != NULL) {
+		XChangeProperty (display, RootWindow (display, screen_num),
+				 XInternAtom (display, "ESETROOT_PMAP_ID", FALSE),
+				 XA_PIXMAP, 32, PropModeReplace,
+				 (guchar *) &pixmap_id, 1);
+		XChangeProperty (display, RootWindow (display, screen_num),
+				 XInternAtom (display, "_XROOTPMAP_ID", FALSE),
+				 XA_PIXMAP, 32, PropModeReplace,
+				 (guchar *) &pixmap_id, 1);
+
+		XSetWindowBackgroundPixmap (display, RootWindow (display, screen_num),
+					    pixmap_id);
+	} else if (pixmap == NULL) {
+		XDeleteProperty (display, RootWindow (display, screen_num),
+				 XInternAtom (display, "ESETROOT_PMAP_ID", FALSE));
+		XDeleteProperty (display, RootWindow (display, screen_num),
+				 XInternAtom (display, "_XROOTPMAP_ID", FALSE));
+	}
 
-	XSetWindowBackgroundPixmap (display, RootWindow (display, screen_num), pixmap_id);
 	XClearWindow (display, RootWindow (display, screen_num));
-
 	XUngrabServer (display);
-	
-	XFlush (display);
-}
+	XFlush (display);}
 
 /* Free the root pixmap */
 static void
@@ -494,11 +473,11 @@ image_loading_done_callback (EelBackgrou
 	int           entire_height;
 	int           pixmap_width;
 	int           pixmap_height;
-	GdkGC        *gc;
 	GdkPixmap    *pixmap;
 	GdkWindow    *background_window;
 	GdkScreen    *screen;
-        GdkColor parsed_color;
+	GdkColor parsed_color, color;
+	gboolean changed_size;
         char * color_string;
 
         if (GPOINTER_TO_INT (disconnect_signal)) {
@@ -515,6 +494,11 @@ image_loading_done_callback (EelBackgrou
 	entire_width = gdk_screen_get_width (screen);
 	entire_height = gdk_screen_get_height (screen);
 
+	background_window = gdk_screen_get_root_window (screen);
+	if (background_window == NULL) {
+	        return;
+	}
+
 	if (eel_background_get_suggested_pixmap_size (background, entire_width, entire_height,
                                                       &pixmap_width, &pixmap_height)) {
                 eel_background_pre_draw (background, entire_width, entire_height);
@@ -528,7 +512,6 @@ image_loading_done_callback (EelBackgrou
 	} else {
                 pixmap_width = pixmap_height = 1;
 
-		background_window = gdk_screen_get_root_window (screen);
 		color_string = eel_background_get_color (background);
 
 		if (background_window != NULL && color_string != NULL) {
@@ -540,18 +523,12 @@ image_loading_done_callback (EelBackgrou
 		g_free (color_string);
 	}
 
-        pixmap = make_root_pixmap (screen, pixmap_width, pixmap_height);
-        if (pixmap == NULL) {
-                return;
-        }
-        
-        gc = gdk_gc_new (pixmap);
-        eel_background_draw (background, pixmap, gc,
-                             0, 0, 0, 0,
-                             pixmap_width, pixmap_height);
-        g_object_unref (gc);
+        pixmap = eel_background_get_pixmap_and_color (background, background_window, entire_width, entire_height, &color, &changed_size);
+
         set_root_pixmap (pixmap, screen);
-        g_object_unref (pixmap);
+	
+	if (pixmap != NULL)
+	        g_object_unref (pixmap);
 }
 
 static void
@@ -657,10 +634,6 @@ background_changed_callback (EelBackgrou
 
 	g_free (color);
 	g_free (image);
-	
-	if (background_is_desktop (background)) {
-		nautilus_file_update_root_pixmaps (background);
-	}
 }
 
 static gboolean
