[gnome-color-manager/colord] Add added and removed signals to GcmX11Screen



commit 30cf0f2907ba198aa3eb5783e31b3a2215fb49f2
Author: Richard Hughes <richard hughsie com>
Date:   Sun Jan 16 19:32:13 2011 +0000

    Add added and removed signals to GcmX11Screen

 src/gcm-x11-output.c |   15 +++++++++
 src/gcm-x11-output.h |    1 +
 src/gcm-x11-screen.c |   79 ++++++++++++++++++++++++++++++++++++++++++++++----
 src/gcm-x11-screen.h |    4 ++
 4 files changed, 93 insertions(+), 6 deletions(-)
---
diff --git a/src/gcm-x11-output.c b/src/gcm-x11-output.c
index 5e04d44..a750b14 100644
--- a/src/gcm-x11-output.c
+++ b/src/gcm-x11-output.c
@@ -116,6 +116,21 @@ gcm_x11_output_get_name (GcmX11Output *output)
 }
 
 /**
+ * gcm_x11_output_get_id:
+ * @output: a valid %GcmX11Output instance
+ *
+ * Gets the panel id.
+ *
+ * Return value: the panel id
+ **/
+guint
+gcm_x11_output_get_id (GcmX11Output *output)
+{
+	g_return_val_if_fail (GCM_IS_X11_OUTPUT (output), 0);
+	return output->priv->id;
+}
+
+/**
  * gcm_x11_output_set_id:
  * @output: a valid %GcmX11Output instance
  * @id: the xrandr ID for the output
diff --git a/src/gcm-x11-output.h b/src/gcm-x11-output.h
index f587b48..bc54e0c 100644
--- a/src/gcm-x11-output.h
+++ b/src/gcm-x11-output.h
@@ -58,6 +58,7 @@ void		 gcm_x11_output_set_name		(GcmX11Output		*output,
 const gchar	*gcm_x11_output_get_name		(GcmX11Output		*output);
 void		 gcm_x11_output_set_id			(GcmX11Output		*output,
 							 guint			 id);
+guint		 gcm_x11_output_get_id			(GcmX11Output		*output);
 void		 gcm_x11_output_set_crtc_id		(GcmX11Output		*output,
 							 guint			 crtc_id);
 void		 gcm_x11_output_set_gamma_size		(GcmX11Output		*output,
diff --git a/src/gcm-x11-screen.c b/src/gcm-x11-screen.c
index 2ee4fba..9f845ba 100644
--- a/src/gcm-x11-screen.c
+++ b/src/gcm-x11-screen.c
@@ -61,6 +61,8 @@ struct _GcmX11ScreenPrivate
 
 enum {
 	SIGNAL_CHANGED,
+	SIGNAL_ADDED,
+	SIGNAL_REMOVED,
 	SIGNAL_LAST
 };
 
@@ -94,12 +96,34 @@ gcm_x11_screen_on_event_cb (GdkXEvent *xevent, GdkEvent *event, gpointer data)
 }
 
 /**
+ * gcm_x11_screen_get_output_for_id:
+ **/
+static GcmX11Output *
+gcm_x11_screen_get_output_for_id (GcmX11Screen *screen, guint id)
+{
+	GcmX11Output *output = NULL;
+	GcmX11Output *output_tmp;
+	GcmX11ScreenPrivate *priv = screen->priv;
+	guint i;
+
+	/* find by id */
+	for (i=0; i<priv->outputs->len; i++) {
+		output_tmp = g_ptr_array_index (screen->priv->outputs, i);
+		if (id == gcm_x11_output_get_id (output)) {
+			output = output_tmp;
+			break;
+		}
+	}
+	return output;
+}
+
+/**
  * gcm_x11_screen_refresh:
  **/
 static gboolean
 gcm_x11_screen_refresh (GcmX11Screen *screen, GError **error)
 {
-	gint i;
+	guint i;
 	gboolean connected;
 	gboolean ret = FALSE;
 	GcmX11ScreenPrivate *priv = screen->priv;
@@ -109,9 +133,13 @@ gcm_x11_screen_refresh (GcmX11Screen *screen, GError **error)
 	XRROutputInfo *output_info;
 	XRRCrtcInfo *crtc_info;
 	gint gamma_size;
+	gboolean emit_added;
 
-	/* clear old outputs */
-	g_ptr_array_set_size (priv->outputs, 0);
+	/* mark all outputs as disconnected */
+	for (i=0; i<priv->outputs->len; i++) {
+		output = g_ptr_array_index (priv->outputs, i);
+		gcm_x11_output_set_connected (output, FALSE);
+	}
 
 	/* get new resources */
 	gdk_error_trap_push ();
@@ -125,7 +153,7 @@ gcm_x11_screen_refresh (GcmX11Screen *screen, GError **error)
 	}
 
 	/* add each output */
-	for (i=0; i < resources->noutput; i++) {
+	for (i=0; i < (guint) resources->noutput; i++) {
 		rr_output = resources->outputs[i];
 
 		/* get information about the output */
@@ -158,8 +186,15 @@ gcm_x11_screen_refresh (GcmX11Screen *screen, GError **error)
 				continue;
 			}
 
+			/* try and find an existing device */
+			emit_added = FALSE;
+			output = gcm_x11_screen_get_output_for_id (screen, rr_output);
+			if (output == NULL) {
+				output = gcm_x11_output_new ();
+				emit_added = TRUE;
+			}
+
 			/* create new object and set properties */
-			output = gcm_x11_output_new ();
 			gcm_x11_output_set_name (output, output_info->name);
 			gcm_x11_output_set_display (output, priv->xdisplay);
 			gcm_x11_output_set_id (output, rr_output);
@@ -171,7 +206,11 @@ gcm_x11_screen_refresh (GcmX11Screen *screen, GError **error)
 			gcm_x11_output_set_size (output, crtc_info->width, crtc_info->height);
 
 			/* add it to the array */
-			g_ptr_array_add (priv->outputs, output);
+			if (emit_added) {
+				g_ptr_array_add (priv->outputs, output);
+				g_debug ("emit added: %s", output_info->name);
+				g_signal_emit (screen, signals[SIGNAL_ADDED], 0, output);
+			}
 
 			/* free client side X resources */
 			XRRFreeCrtcInfo (crtc_info);
@@ -179,6 +218,16 @@ gcm_x11_screen_refresh (GcmX11Screen *screen, GError **error)
 		XRRFreeOutputInfo (output_info);
 	}
 
+	/* remove any disconnected output */
+	for (i=0; i<priv->outputs->len; i++) {
+		output = g_ptr_array_index (priv->outputs, i);
+		if (!gcm_x11_output_get_connected (output)) {
+			g_debug ("emit added: %s", output_info->name);
+			g_signal_emit (screen, signals[SIGNAL_REMOVED], 0, output);
+			g_ptr_array_remove (priv->outputs, output);
+		}
+	}
+
 	/* success */
 	ret = TRUE;
 out:
@@ -695,6 +744,24 @@ gcm_x11_screen_class_init (GcmX11ScreenClass *klass)
 			      G_STRUCT_OFFSET (GcmX11ScreenClass, changed),
 			      NULL, NULL, g_cclosure_marshal_VOID__VOID,
 			      G_TYPE_NONE, 0);
+	/**
+	 * GcmX11Screen::added:
+	 **/
+	signals[SIGNAL_ADDED] =
+		g_signal_new ("added",
+			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (GcmX11ScreenClass, added),
+			      NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
+			      G_TYPE_NONE, 1, GCM_TYPE_X11_OUTPUT);
+	/**
+	 * GcmX11Screen::removed:
+	 **/
+	signals[SIGNAL_REMOVED] =
+		g_signal_new ("removed",
+			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (GcmX11ScreenClass, removed),
+			      NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
+			      G_TYPE_NONE, 1, GCM_TYPE_X11_OUTPUT);
 
 	g_type_class_add_private (klass, sizeof (GcmX11ScreenPrivate));
 }
diff --git a/src/gcm-x11-screen.h b/src/gcm-x11-screen.h
index 5055866..dc13567 100644
--- a/src/gcm-x11-screen.h
+++ b/src/gcm-x11-screen.h
@@ -50,6 +50,10 @@ struct _GcmX11ScreenClass
 {
 	GObjectClass		 parent_class;
 	void			(* changed)		(GcmX11Screen		*screen);
+	void			(* added)		(GcmX11Screen		*screen,
+							 GcmX11Output		*output);
+	void			(* removed)		(GcmX11Screen		*screen,
+							 GcmX11Output		*output);
 };
 
 GType		 gcm_x11_screen_get_type		(void);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]