Index: src/backend/bacon-video-widget-xine.c
===================================================================
--- src/backend/bacon-video-widget-xine.c	(revision 4946)
+++ src/backend/bacon-video-widget-xine.c	(working copy)
@@ -193,6 +193,9 @@
 	/* fullscreen stuff */
 	int screenid;
 
+	/* Bacon resize */
+	BaconResize *bacon_resize;
+
 	guint ao_driver_none : 1;
 	guint show_vfx : 1;
 	guint using_vfx : 1;
@@ -200,7 +203,6 @@
 	guint can_dvd : 1;
 	guint can_vcd : 1;
 	guint can_dvb : 1;
-	guint have_xvidmode : 1;
 	guint auto_resize : 1;
 	guint is_live : 1;
 	guint got_redirect : 1;
@@ -1241,7 +1243,7 @@
 		bvw->priv->vis_name = NULL;
 	}
 
-	bvw->priv->have_xvidmode = bacon_resize_init ();
+	bvw->priv->bacon_resize = bacon_resize_new (widget);
 	bvw->priv->stream = xine_stream_new (bvw->priv->xine,
 			bvw->priv->ao_driver, bvw->priv->vo_driver);
 	setup_config_stream (bvw);
@@ -1657,6 +1659,8 @@
 			XINE_GUI_SEND_WILL_DESTROY_DRAWABLE,
 			(void*)widget->window);
 
+	g_object_unref (bvw->priv->bacon_resize);
+
 	/* Hide all windows */
 	if (GTK_WIDGET_MAPPED (widget))
 		gtk_widget_unmap (widget);
@@ -3024,10 +3028,16 @@
 void
 bacon_video_widget_set_fullscreen (BaconVideoWidget *bvw, gboolean fullscreen)
 {
+	gboolean have_xvidmode;
+
 	g_return_if_fail (bvw != NULL);
 	g_return_if_fail (BACON_IS_VIDEO_WIDGET (bvw));
 
-	if (bvw->priv->have_xvidmode == FALSE &&
+	g_object_get (G_OBJECT (bvw->priv->bacon_resize),
+				"have-xvidmode", &have_xvidmode,
+				NULL);
+
+	if (have_xvidmode == FALSE &&
 			bvw->priv->tvout != TV_OUT_NVTV_NTSC &&
 			bvw->priv->tvout != TV_OUT_NVTV_PAL)
 		return;
@@ -3044,7 +3054,7 @@
 			/* Else if just auto resize is used */
 		} else if (bvw->priv->auto_resize != FALSE) {
 #endif
-			bacon_restore ();
+			bacon_resize_restore (bvw->priv->bacon_resize);
 #ifdef HAVE_NVTV
 		}
 		/* Turn fullscreen on with NVTV if that option is on */
@@ -3055,8 +3065,8 @@
 				bvw->priv->video_height);
 #endif
 		/* Turn fullscreen on when we have xvidmode */
-	} else if (bvw->priv->have_xvidmode != FALSE) {
-		bacon_resize ();
+	} else if (have_xvidmode != FALSE) {
+		bacon_resize_resize (bvw->priv->bacon_resize);
 	}
 }
 
Index: src/backend/bacon-video-widget-gst-0.10.c
===================================================================
--- src/backend/bacon-video-widget-gst-0.10.c	(revision 4946)
+++ src/backend/bacon-video-widget-gst-0.10.c	(working copy)
@@ -162,7 +162,6 @@
   gboolean                     cursor_shown;
   gboolean                     fullscreen_mode;
   gboolean                     auto_resize;
-  gboolean                     have_xvidmode;
   gboolean                     uses_fakesink;
   
   gint                         video_width; /* Movie width */
@@ -202,6 +201,9 @@
   /* for easy codec installation */
   GList                       *missing_plugins;   /* GList of GstMessages */
   gboolean                     plugin_install_in_progress;
+
+  /* Bacon resize */
+  BaconResize                 *bacon_resize;
 };
 
 static void bacon_video_widget_set_property (GObject * object,
@@ -549,7 +551,7 @@
   } 
 #endif
 
-  bvw->priv->have_xvidmode = bacon_resize_init ();
+  bvw->priv->bacon_resize = bacon_resize_new (widget);
 }
 
 static void
@@ -562,6 +564,7 @@
   nvtv_simple_exit();
 #endif
 
+  g_object_unref (bvw->priv->bacon_resize);
   gdk_window_set_user_data (bvw->priv->video_window, NULL);
   gdk_window_destroy (bvw->priv->video_window);
   bvw->priv->video_window = NULL;
@@ -3185,10 +3188,16 @@
 bacon_video_widget_set_fullscreen (BaconVideoWidget * bvw,
                                    gboolean fullscreen)
 {
+  gboolean have_xvidmode;
+
   g_return_if_fail (bvw != NULL);
   g_return_if_fail (BACON_IS_VIDEO_WIDGET (bvw));
 
-  if (bvw->priv->have_xvidmode == FALSE &&
+  g_object_get (G_OBJECT (bvw->priv->bacon_resize),
+        "have-xvidmode", &have_xvidmode,
+        NULL);
+
+  if (have_xvidmode == FALSE &&
       bvw->priv->tv_out_type != TV_OUT_NVTV_NTSC &&
       bvw->priv->tv_out_type != TV_OUT_NVTV_PAL)
     return;
@@ -3205,7 +3214,7 @@
     /* Else if just auto resize is used */
     } else if (bvw->priv->auto_resize != FALSE) {
 #endif
-      bacon_restore ();
+      bacon_resize_restore (bvw->priv->bacon_resize);
 #ifdef HAVE_NVTV
     }
   /* Turn fullscreen on with NVTV if that option is on */
@@ -3216,8 +3225,8 @@
                        bvw->priv->video_height);
 #endif
     /* Turn fullscreen on when we have xvidmode */
-  } else if (bvw->priv->have_xvidmode != FALSE) {
-    bacon_resize ();
+  } else if (have_xvidmode != FALSE) {
+    bacon_resize_resize (bvw->priv->bacon_resize);
   }
 }
 
Index: src/backend/bacon-resize.c
===================================================================
--- src/backend/bacon-resize.c	(revision 4946)
+++ src/backend/bacon-resize.c	(working copy)
@@ -19,9 +19,9 @@
 
 #include "config.h"
 #include "bacon-resize.h"
+#include <glib.h>
 
 #ifdef HAVE_XVIDMODE
-#include <glib.h>
 #include <gdk/gdkx.h>
 #include <gdk/gdk.h>
 #include <X11/X.h>
@@ -31,140 +31,285 @@
 #include <X11/extensions/xf86vmode.h>
 #include <X11/extensions/Xrandr.h>
 #include <X11/extensions/Xrender.h>
+#endif
 
-/* XRandR */
-XRRScreenConfiguration *xr_screen_conf;
-XRRScreenSize *xr_sizes;
-Rotation xr_current_rotation;
-SizeID xr_original_size;
+static GObjectClass *parent_class = NULL;
+static void bacon_resize_class_init	(BaconResizeClass *class);
+static void bacon_resize_init		(BaconResize *ggo);
+static void bacon_resize_set_property	(GObject *object,
+					 guint property_id,
+					 const GValue *value,
+					 GParamSpec *pspec);
+static void bacon_resize_get_property	(GObject *object,
+					 guint property_id,
+					 GValue *value,
+					 GParamSpec *pspec);
+static void bacon_resize_dispose	(GObject *object);
+
+static void set_video_widget		(BaconResize *resize,
+					 GtkWidget *video_widget);
+static void screen_changed_cb		(GtkWidget *video_widget,
+					 GdkScreen *previous_screen,
+					 BaconResize *resize);
+
+struct BaconResizePrivate {
+	gboolean have_xvidmode;
+	gboolean resized;
+	GtkWidget *video_widget;
+#ifdef HAVE_XVIDMODE
+	/* XRandR */
+	XRRScreenConfiguration *xr_screen_conf;
+	XRRScreenSize *xr_sizes;
+	Rotation xr_current_rotation;
+	SizeID xr_original_size;
 #endif
+};
 
-gboolean
-bacon_resize_init (void)
+enum {
+	PROP_0,
+	PROP_HAVE_XVIDMODE,
+	PROP_VIDEO_WIDGET
+};
+
+G_DEFINE_TYPE (BaconResize, bacon_resize, G_TYPE_OBJECT)
+
+static void
+bacon_resize_class_init (BaconResizeClass *klass)
 {
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+	parent_class = g_type_class_peek_parent (klass);
+	g_type_class_add_private (klass, sizeof (BaconResizePrivate));
+
+	object_class->dispose = bacon_resize_dispose;
+
+	object_class->set_property = bacon_resize_set_property;
+	object_class->get_property = bacon_resize_get_property;
+
+	g_object_class_install_property (object_class, PROP_HAVE_XVIDMODE,
+					 g_param_spec_boolean ("have-xvidmode", NULL, NULL,
+							       FALSE, G_PARAM_READABLE));
+
+	g_object_class_install_property (object_class, PROP_VIDEO_WIDGET,
+					 g_param_spec_object ("video-widget", "video-widget",
+						 	      "The related video widget",
+							      GTK_TYPE_WIDGET,
+							      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+bacon_resize_init (BaconResize *resize)
+{
+	resize->priv = G_TYPE_INSTANCE_GET_PRIVATE (resize, BACON_TYPE_RESIZE, BaconResizePrivate);
+
+	resize->priv->have_xvidmode = FALSE;
+	resize->priv->resized = FALSE;
+}
+
+static void
+bacon_resize_dispose (GObject *object)
+{
+	BaconResize *self;
+	g_return_if_fail (object != NULL);
 #ifdef HAVE_XVIDMODE
+	self = BACON_RESIZE (object);
+
+	XRRFreeScreenConfigInfo (self->priv->xr_screen_conf);
+	g_signal_handlers_disconnect_by_func (self->priv->video_widget, screen_changed_cb, self);
+#endif /* HAVE_XVIDMODE */
+
+	G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+bacon_resize_set_property (GObject *object,
+			   guint property_id,
+			   const GValue *value,
+			   GParamSpec *pspec)
+{
+	switch (property_id)
+	{
+	case PROP_VIDEO_WIDGET:
+		set_video_widget (BACON_RESIZE (object), GTK_WIDGET (g_value_get_object (value)));
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+	}
+}
+
+static void
+bacon_resize_get_property (GObject *object,
+			   guint property_id,
+			   GValue *value,
+			   GParamSpec *pspec)
+{
+	switch (property_id)
+	{
+	case PROP_HAVE_XVIDMODE:
+		g_value_set_boolean (value, BACON_RESIZE (object)->priv->have_xvidmode);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+	}
+}
+
+static void
+set_video_widget (BaconResize *resize, GtkWidget *video_widget)
+{
+#ifdef HAVE_XVIDMODE
+	GdkDisplay *display;
+	GdkScreen *screen;
 	int event_basep, error_basep;
+#endif
+	g_return_if_fail (GTK_WIDGET_REALIZED (video_widget));
 
-	/* FIXME multihead */
-	XLockDisplay (GDK_DISPLAY());
+	resize->priv->video_widget = video_widget;
 
-	if (!XF86VidModeQueryExtension (GDK_DISPLAY(), &event_basep, &error_basep))
-		goto bail;
+#ifdef HAVE_XVIDMODE
+	display = gtk_widget_get_display (video_widget);
+	screen = gtk_widget_get_screen (video_widget);
 
-	if (!XRRQueryExtension (GDK_DISPLAY(), &event_basep, &error_basep))
-		goto bail;
+	g_signal_connect (G_OBJECT (video_widget),
+			  "screen-changed", G_CALLBACK (screen_changed_cb), resize);
 
-	xr_screen_conf = XRRGetScreenInfo (GDK_DISPLAY(), GDK_ROOT_WINDOW());
+	XLockDisplay (GDK_DISPLAY_XDISPLAY (display));
 
-	XUnlockDisplay (GDK_DISPLAY());
+	if (!XF86VidModeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &event_basep, &error_basep))
+		goto bail;
 
-	return TRUE;
-
+	if (!XRRQueryExtension (GDK_DISPLAY_XDISPLAY (display), &event_basep, &error_basep))
+		goto bail;
+	
+	resize->priv->xr_screen_conf = XRRGetScreenInfo (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)));
+	resize->priv->have_xvidmode = TRUE;
 bail:
-	XUnlockDisplay (GDK_DISPLAY());
-	return FALSE;
+	XUnlockDisplay (GDK_DISPLAY_XDISPLAY (display));
+#endif /* HAVE_XVIDMODE */
+}
 
+static void screen_changed_cb (GtkWidget *video_widget, GdkScreen *previous_screen, BaconResize *resize)
+{
+#ifdef HAVE_XVIDMODE
+	if (resize->priv->resized == TRUE)
+		bacon_resize_resize (resize);
+	else
+		bacon_resize_restore (resize);
 #endif /* HAVE_XVIDMODE */
-	return FALSE;
 }
 
 void
-bacon_resize (void)
+bacon_resize_resize (BaconResize *resize)
 {
 #ifdef HAVE_XVIDMODE
+	GdkDisplay *display;
+	GdkScreen *screen;
 	int width, height, i, xr_nsize, res, dotclock;
 	XF86VidModeModeLine modeline;
 	XRRScreenSize *xr_sizes;
 	gboolean found = FALSE;
 
-	/* FIXME multihead */
-	XLockDisplay (GDK_DISPLAY());
+	g_return_if_fail (GTK_IS_WIDGET (resize->priv->video_widget));
+	g_return_if_fail (GTK_WIDGET_REALIZED (resize->priv->video_widget));
 
-	res = XF86VidModeGetModeLine (GDK_DISPLAY(), XDefaultScreen (GDK_DISPLAY()), &dotclock, &modeline);
-	if (!res) {
-		XUnlockDisplay (GDK_DISPLAY());
-		return;
-	}
+	display = gtk_widget_get_display (resize->priv->video_widget);
+	screen = gtk_widget_get_screen (resize->priv->video_widget);
 
+	XLockDisplay (GDK_DISPLAY_XDISPLAY (display));
+
+	res = XF86VidModeGetModeLine (GDK_DISPLAY_XDISPLAY (display), GDK_SCREEN_XNUMBER (screen), &dotclock, &modeline);
+	if (!res)
+		goto bail;
+
 	/* Check if there's a viewport */
-	width = gdk_screen_width ();
-	height = gdk_screen_height ();
-	if (width > modeline.hdisplay
-			&& height > modeline.vdisplay) {
-		XUnlockDisplay (GDK_DISPLAY());
-		return;
-	}
+	width = gdk_screen_get_width (screen);
+	height = gdk_screen_get_height (screen);
+	if (width > modeline.hdisplay && height > modeline.vdisplay)
+		goto bail;
 
 	gdk_error_trap_push ();
 	/* Find the xrandr mode that corresponds to the real size */
-	xr_sizes = XRRConfigSizes (xr_screen_conf, &xr_nsize);
-	xr_original_size = XRRConfigCurrentConfiguration
-		(xr_screen_conf, &xr_current_rotation);
+	xr_sizes = XRRConfigSizes (resize->priv->xr_screen_conf, &xr_nsize);
+	resize->priv->xr_original_size = XRRConfigCurrentConfiguration (resize->priv->xr_screen_conf, &(resize->priv->xr_current_rotation));
 	if (gdk_error_trap_pop ())
 		g_warning ("XRRConfigSizes or XRRConfigCurrentConfiguration failed");
 
 	for (i = 0; i < xr_nsize; i++) {
-		if (modeline.hdisplay == xr_sizes[i].width
-		&& modeline.vdisplay == xr_sizes[i].height) {
+		if (modeline.hdisplay == xr_sizes[i].width && modeline.vdisplay == xr_sizes[i].height) {
 			found = TRUE;
 			break;
 		}
 	}
 
-	if (!found) {
-		XUnlockDisplay (GDK_DISPLAY());
-		return;
-	}
+	if (!found)
+		goto bail;
+
 	gdk_error_trap_push ();
-	XRRSetScreenConfig (GDK_DISPLAY(),
-			xr_screen_conf,
-			GDK_ROOT_WINDOW(),
+	XRRSetScreenConfig (GDK_DISPLAY_XDISPLAY (display),
+			resize->priv->xr_screen_conf,
+			GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)),
 			(SizeID) i,
-			xr_current_rotation,
+			resize->priv->xr_current_rotation,
 			CurrentTime);
 	gdk_flush ();
 	if (gdk_error_trap_pop ())
 		g_warning ("XRRSetScreenConfig failed");
+	else
+		resize->priv->resized = TRUE;
 
-	XUnlockDisplay (GDK_DISPLAY());
+bail:
+	XUnlockDisplay (GDK_DISPLAY_XDISPLAY (display));
 #endif /* HAVE_XVIDMODE */
 }
 
 void
-bacon_restore (void)
+bacon_resize_restore (BaconResize *resize)
 {
 #ifdef HAVE_XVIDMODE
+	GdkDisplay *display;
+	GdkScreen *screen;
 	int width, height, res, dotclock;
 	XF86VidModeModeLine modeline;
 
-	/* FIXME multihead */
-	XLockDisplay (GDK_DISPLAY());
+	g_return_if_fail (GTK_IS_WIDGET (resize->priv->video_widget));
+	g_return_if_fail (GTK_WIDGET_REALIZED (resize->priv->video_widget));
 
-	res = XF86VidModeGetModeLine (GDK_DISPLAY(), XDefaultScreen (GDK_DISPLAY()), &dotclock, &modeline);
-	if (!res) {
-		XUnlockDisplay (GDK_DISPLAY());
-		return;
-	}
+	display = gtk_widget_get_display (resize->priv->video_widget);
+	screen = gtk_widget_get_screen (resize->priv->video_widget);
 
+	XLockDisplay (GDK_DISPLAY_XDISPLAY (display));
+
+	res = XF86VidModeGetModeLine (GDK_DISPLAY_XDISPLAY (display), GDK_SCREEN_XNUMBER (screen), &dotclock, &modeline);
+	if (!res)
+		goto bail;
+
 	/* Check if there's a viewport */
-	width = gdk_screen_width ();
-	height = gdk_screen_height ();
-	if (width > modeline.hdisplay
-			&& height > modeline.vdisplay) {
-		XUnlockDisplay (GDK_DISPLAY());
-		return;
-	}
+	width = gdk_screen_get_width (screen);
+	height = gdk_screen_get_height (screen);
+	if (width > modeline.hdisplay && height > modeline.vdisplay)
+		goto bail;
+
 	gdk_error_trap_push ();
-	XRRSetScreenConfig (GDK_DISPLAY(),
-			xr_screen_conf,
-			GDK_ROOT_WINDOW(),
-			xr_original_size,
-			xr_current_rotation,
+	XRRSetScreenConfig (GDK_DISPLAY_XDISPLAY (display),
+			resize->priv->xr_screen_conf,
+			GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)),
+			resize->priv->xr_original_size,
+			resize->priv->xr_current_rotation,
 			CurrentTime);
 	gdk_flush ();
 	if (gdk_error_trap_pop ())
 		g_warning ("XRRSetScreenConfig failed");
-	XUnlockDisplay (GDK_DISPLAY());
+	else
+		resize->priv->resized = FALSE;
+
+bail:
+	XUnlockDisplay (GDK_DISPLAY_XDISPLAY (display));
 #endif
 }
 
+BaconResize *
+bacon_resize_new (GtkWidget *video_widget)
+{
+	BaconResize *resize;
+	resize = BACON_RESIZE (g_object_new (BACON_TYPE_RESIZE, "video-widget", video_widget, NULL));
+	return resize;
+}
Index: src/backend/bacon-resize.h
===================================================================
--- src/backend/bacon-resize.h	(revision 4946)
+++ src/backend/bacon-resize.h	(working copy)
@@ -17,9 +17,39 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
  */
 
+#ifndef BACON_RESIZE_H
+#define BACON_RESIZE_H
+
 #include <glib.h>
+#include <glib-object.h>
+#include <gtk/gtkwidget.h>
 
-gboolean bacon_resize_init (void);
-void bacon_resize (void);
-void bacon_restore (void);
+G_BEGIN_DECLS
 
+#define BACON_TYPE_RESIZE		(bacon_resize_get_type ())
+#define BACON_RESIZE(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), BACON_TYPE_RESIZE, BaconResize))
+#define BACON_RESIZE_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((klass), BACON_TYPE_RESIZE, BaconResizeClass))
+#define BACON_IS_RESIZE(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), BACON_TYPE_RESIZE))
+#define BACON_IS_RESIZE_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), BACON_TYPE_RESIZE))
+
+typedef struct BaconResize		BaconResize;
+typedef struct BaconResizeClass		BaconResizeClass;
+typedef struct BaconResizePrivate	BaconResizePrivate;
+
+struct BaconResize {
+	GObject parent;
+	BaconResizePrivate *priv;
+};
+
+struct BaconResizeClass {
+	GObjectClass parent_class;
+};
+
+GType bacon_resize_get_type	(void);
+BaconResize *bacon_resize_new	(GtkWidget *video_widget);
+void bacon_resize_resize	(BaconResize *resize);
+void bacon_resize_restore	(BaconResize *resize);
+
+G_END_DECLS
+
+#endif /* BACON_RESIZE_H */
