[gnome-color-manager: 75/80] Make gcm-picker work with native sensor devices too



commit 3c5fa183c52381940c98bda977db5b45965d468f
Author: Richard Hughes <richard hughsie com>
Date:   Mon Jul 19 11:14:20 2010 +0100

    Make gcm-picker work with native sensor devices too

 libcolor-glib/gcm-common.c        |   30 ++++++
 libcolor-glib/gcm-common.h        |    4 +
 libcolor-glib/gcm-sensor-client.c |   20 ++++-
 libcolor-glib/gcm-sensor.c        |    9 ++
 src/gcm-picker.c                  |  176 ++++++++++++++++++++++---------------
 5 files changed, 166 insertions(+), 73 deletions(-)
---
diff --git a/libcolor-glib/gcm-common.c b/libcolor-glib/gcm-common.c
index f6217fd..0e1a063 100644
--- a/libcolor-glib/gcm-common.c
+++ b/libcolor-glib/gcm-common.c
@@ -34,6 +34,36 @@
 #include <gcm-common.h>
 
 /**
+ * gcm_color_copy_XYZ:
+ * @src: the source color.
+ * @dest: the destination color
+ *
+ * Deep copies a color value.
+ **/
+void
+gcm_color_copy_XYZ (GcmColorXYZ *src, GcmColorXYZ *dest)
+{
+	dest->X = src->X;
+	dest->Y = src->Y;
+	dest->Z = src->Z;
+}
+
+/**
+ * gcm_color_copy_RGB:
+ * @src: the source color.
+ * @dest: the destination color
+ *
+ * Deep copies a color value.
+ **/
+void
+gcm_color_copy_RGB (GcmColorRgb *src, GcmColorRgb *dest)
+{
+	dest->red = src->red;
+	dest->green = src->green;
+	dest->blue = src->blue;
+}
+
+/**
  * gcm_convert_rgb_int_to_rgb:
  **/
 void
diff --git a/libcolor-glib/gcm-common.h b/libcolor-glib/gcm-common.h
index 80c3ef2..1b9adb9 100644
--- a/libcolor-glib/gcm-common.h
+++ b/libcolor-glib/gcm-common.h
@@ -70,6 +70,10 @@ typedef struct {
 	/* any addition fields go *after* the data */
 } GcmVec3;
 
+void		 gcm_color_copy_XYZ		(GcmColorXYZ		*src,
+						 GcmColorXYZ		*dest);
+void		 gcm_color_copy_RGB		(GcmColorRgb		*src,
+						 GcmColorRgb		*dest);
 void		 gcm_convert_rgb_int_to_rgb	(GcmColorRgbInt		*rgb_int,
 						 GcmColorRgb		*rgb);
 void		 gcm_convert_rgb_to_rgb_int	(GcmColorRgb		*rgb,
diff --git a/libcolor-glib/gcm-sensor-client.c b/libcolor-glib/gcm-sensor-client.c
index 11f6420..8319d03 100644
--- a/libcolor-glib/gcm-sensor-client.c
+++ b/libcolor-glib/gcm-sensor-client.c
@@ -33,6 +33,7 @@
 #include <gtk/gtk.h>
 
 #include "gcm-sensor-client.h"
+#include "gcm-sensor-huey.h"
 
 #include "egg-debug.h"
 
@@ -158,6 +159,9 @@ static gboolean
 gcm_sensor_client_device_add (GcmSensorClient *sensor_client, GUdevDevice *device)
 {
 	gboolean ret;
+	GcmSensor *sensor = NULL;
+	GcmSensorKind kind;
+	const gchar *kind_str;
 	GcmSensorClientPrivate *priv = sensor_client->priv;
 
 	/* interesting device? */
@@ -165,20 +169,32 @@ gcm_sensor_client_device_add (GcmSensorClient *sensor_client, GUdevDevice *devic
 	if (!ret)
 		goto out;
 
+	/* is it a sensor we have a internal native driver for? */
+	kind_str = g_udev_device_get_property (device, "GCM_KIND");
+	kind = gcm_sensor_kind_from_string (kind_str);
+	if (kind == GCM_SENSOR_KIND_HUEY) {
+		egg_warning ("creating internal device");
+		sensor = gcm_sensor_huey_new ();
+	} else {
+		sensor = gcm_sensor_new ();
+	}
+
 	/* get data */
 	egg_debug ("adding color management device: %s", g_udev_device_get_sysfs_path (device));
-	priv->sensor = gcm_sensor_new ();
-	ret = gcm_sensor_set_from_device (priv->sensor, device, NULL);
+	ret = gcm_sensor_set_from_device (sensor, device, NULL);
 	if (!ret)
 		goto out;
 
 	/* success */
 	priv->present = TRUE;
+	priv->sensor = g_object_ref (sensor);
 
 	/* signal the addition */
 	egg_debug ("emit: changed");
 	g_signal_emit (sensor_client, signals[SIGNAL_CHANGED], 0);
 out:
+	if (sensor != NULL)
+		g_object_unref (sensor);
 	return ret;
 }
 
diff --git a/libcolor-glib/gcm-sensor.c b/libcolor-glib/gcm-sensor.c
index 61915d1..860a868 100644
--- a/libcolor-glib/gcm-sensor.c
+++ b/libcolor-glib/gcm-sensor.c
@@ -291,6 +291,15 @@ gcm_sensor_startup (GcmSensor *sensor, GError **error)
 		goto out;
 	}
 
+	/* not a native device */
+	if (!sensor->priv->native) {
+		g_set_error_literal (error,
+				     GCM_SENSOR_ERROR,
+				     GCM_SENSOR_ERROR_INTERNAL,
+				     "not a native device, you have to use GcmCalibrate...");
+		goto out;
+	}
+
 	/* coldplug source */
 	if (klass->startup == NULL) {
 		g_set_error_literal (error,
diff --git a/src/gcm-picker.c b/src/gcm-picker.c
index 358b5ce..5c69800 100644
--- a/src/gcm-picker.c
+++ b/src/gcm-picker.c
@@ -45,6 +45,8 @@ static GcmCalibrate *calibrate = NULL;
 static GcmProfileStore *profile_store = NULL;
 static const gchar *profile_filename = NULL;
 static gboolean done_measure = FALSE;
+static GcmSensor *sensor = NULL;
+static GcmColorXYZ last_sample;
 
 enum {
 	GCM_PREFS_COMBO_COLUMN_TEXT,
@@ -80,42 +82,18 @@ gcm_picker_set_pixbuf_color (GdkPixbuf *pixbuf, gchar red, gchar green, gchar bl
 }
 
 /**
- * gcm_picker_measure_cb:
- **/
-static void
-gcm_picker_measure_cb (GtkWidget *widget, gpointer data)
-{
-	GtkWindow *window;
-	gboolean ret;
-	GError *error = NULL;
-
-	/* reset the image */
-	widget = GTK_WIDGET (gtk_builder_get_object (builder, "image_preview"));
-	gtk_image_set_from_file (GTK_IMAGE (widget), DATADIR "/icons/hicolor/64x64/apps/gnome-color-manager.png");
-
-	/* get value */
-	window = GTK_WINDOW (gtk_builder_get_object (builder, "dialog_picker"));
-	ret = gcm_calibrate_spotread (calibrate, window, &error);
-	if (!ret) {
-		egg_warning ("failed to get spot color: %s", error->message);
-		g_error_free (error);
-	}
-}
-
-/**
  * gcm_picker_refresh_results:
  **/
 static void
 gcm_picker_refresh_results (void)
 {
-	GcmXyz *xyz = NULL;
 	GtkImage *image;
 	GtkLabel *label;
 	GdkPixbuf *pixbuf = NULL;
-	gdouble color_xyz[3];
-	guint8 color_rgb[3];
-	gdouble color_lab[3];
-	gdouble color_error[3];
+	GcmColorRgbInt color_rgb;
+	GcmColorLab color_lab;
+	GcmColorXYZ color_xyz;
+	GcmColorXYZ color_error;
 	gchar *text_xyz = NULL;
 	gchar *text_lab = NULL;
 	gchar *text_rgb = NULL;
@@ -127,23 +105,16 @@ gcm_picker_refresh_results (void)
 	cmsHTRANSFORM transform_lab;
 	cmsHTRANSFORM transform_error;
 
-	/* get new value */
-	g_object_get (calibrate, "xyz", &xyz, NULL);
+	/* copy as we're modifying the value */
+	gcm_color_copy_XYZ (&last_sample, &color_xyz);
 
 	/* create new pixbuf of the right size */
 	pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 200, 200);
 
-	/* get values */
-	g_object_get (xyz,
-		      "cie-x", &color_xyz[0],
-		      "cie-y", &color_xyz[1],
-		      "cie-z", &color_xyz[2],
-		      NULL);
-
 	/* lcms scales these for some reason */
-	color_xyz[0] /= 100.0f;
-	color_xyz[1] /= 100.0f;
-	color_xyz[2] /= 100.0f;
+	color_xyz.X /= 100.0f;
+	color_xyz.Y /= 100.0f;
+	color_xyz.Z /= 100.0f;
 
 	/* get profiles */
 	profile_xyz = cmsCreateXYZProfile ();
@@ -155,9 +126,9 @@ gcm_picker_refresh_results (void)
 	transform_lab = cmsCreateTransform (profile_xyz, TYPE_XYZ_DBL, profile_lab, TYPE_Lab_DBL, INTENT_PERCEPTUAL, 0);
 	transform_error = cmsCreateTransform (profile_rgb, TYPE_RGB_8, profile_xyz, TYPE_XYZ_DBL, INTENT_PERCEPTUAL, 0);
 
-	cmsDoTransform (transform_rgb, color_xyz, color_rgb, 1);
-	cmsDoTransform (transform_lab, color_xyz, color_lab, 1);
-	cmsDoTransform (transform_error, color_rgb, color_error, 1);
+	cmsDoTransform (transform_rgb, &color_xyz, &color_rgb, 1);
+	cmsDoTransform (transform_lab, &color_xyz, &color_lab, 1);
+	cmsDoTransform (transform_error, &color_rgb, &color_error, 1);
 
 	/* destroy lcms state */
 	cmsDeleteTransform (transform_rgb);
@@ -169,28 +140,28 @@ gcm_picker_refresh_results (void)
 
 	/* set XYZ */
 	label = GTK_LABEL (gtk_builder_get_object (builder, "label_xyz"));
-	text_xyz = g_strdup_printf ("%.3f, %.3f, %.3f", color_xyz[0], color_xyz[1], color_xyz[2]);
+	text_xyz = g_strdup_printf ("%.3f, %.3f, %.3f", color_xyz.X, color_xyz.Y, color_xyz.Z);
 	gtk_label_set_label (label, text_xyz);
 
 	/* set LAB */
 	label = GTK_LABEL (gtk_builder_get_object (builder, "label_lab"));
-	text_lab = g_strdup_printf ("%.3f, %.3f, %.3f", color_lab[0], color_lab[1], color_lab[2]);
+	text_lab = g_strdup_printf ("%.3f, %.3f, %.3f", color_lab.L, color_lab.a, color_lab.b);
 	gtk_label_set_label (label, text_lab);
 
 	/* set RGB */
 	label = GTK_LABEL (gtk_builder_get_object (builder, "label_rgb"));
 	text_rgb = g_strdup_printf ("%i, %i, %i (#%02X%02X%02X)",
-				    color_rgb[0], color_rgb[1], color_rgb[2],
-				    color_rgb[0], color_rgb[1], color_rgb[2]);
+				    color_rgb.red, color_rgb.green, color_rgb.blue,
+				    color_rgb.red, color_rgb.green, color_rgb.blue);
 	gtk_label_set_label (label, text_rgb);
-	gcm_picker_set_pixbuf_color (pixbuf, color_rgb[0], color_rgb[1], color_rgb[2]);
+	gcm_picker_set_pixbuf_color (pixbuf, color_rgb.red, color_rgb.green, color_rgb.blue);
 
 	/* set error */
 	label = GTK_LABEL (gtk_builder_get_object (builder, "label_error"));
 	text_error = g_strdup_printf ("%.1f%%, %.1f%%, %.1f%%",
-				      ABS ((color_error[0] - color_xyz[0]) / color_xyz[0] * 100),
-				      ABS ((color_error[1] - color_xyz[1]) / color_xyz[1] * 100),
-				      ABS ((color_error[2] - color_xyz[2]) / color_xyz[2] * 100));
+				      ABS ((color_error.X - color_xyz.X) / color_xyz.X * 100),
+				      ABS ((color_error.Y - color_xyz.Y) / color_xyz.Y * 100),
+				      ABS ((color_error.Z - color_xyz.Z) / color_xyz.Z * 100));
 	gtk_label_set_label (label, text_error);
 
 	/* set image */
@@ -201,17 +172,15 @@ gcm_picker_refresh_results (void)
 	g_free (text_lab);
 	g_free (text_rgb);
 	g_free (text_error);
-	if (xyz != NULL)
-		g_object_unref (xyz);
 	if (pixbuf != NULL)
 		g_object_unref (pixbuf);
 }
 
 /**
- * gcm_picker_xyz_notify_cb:
+ * gcm_picker_got_results:
  **/
 static void
-gcm_picker_xyz_notify_cb (GcmCalibrate *calibrate_, GParamSpec *pspec, gpointer user_data)
+gcm_picker_got_results (void)
 {
 	GtkWidget *widget;
 
@@ -222,8 +191,66 @@ gcm_picker_xyz_notify_cb (GcmCalibrate *calibrate_, GParamSpec *pspec, gpointer
 
 	/* we've got results so make sure it's sensitive */
 	done_measure = TRUE;
+}
+
+/**
+ * gcm_picker_measure_cb:
+ **/
+static void
+gcm_picker_measure_cb (GtkWidget *widget, gpointer data)
+{
+	GtkWindow *window;
+	gboolean ret;
+	GError *error = NULL;
+
+	/* reset the image */
+	widget = GTK_WIDGET (gtk_builder_get_object (builder, "image_preview"));
+	gtk_image_set_from_file (GTK_IMAGE (widget), DATADIR "/icons/hicolor/64x64/apps/gnome-color-manager.png");
+
+	if (gcm_sensor_get_is_native (sensor)) {
+		/* sample color */
+		ret = gcm_sensor_sample (sensor, &last_sample, &error);
+		if (!ret) {
+			egg_warning ("failed to measure: %s", error->message);
+			g_error_free (error);
+			goto out;
+		}
+		gcm_picker_refresh_results ();
+		gcm_picker_got_results ();
+	} else {
+		/* get value */
+		window = GTK_WINDOW (gtk_builder_get_object (builder, "dialog_picker"));
+		ret = gcm_calibrate_spotread (calibrate, window, &error);
+		if (!ret) {
+			egg_warning ("failed to get spot color: %s", error->message);
+			g_error_free (error);
+			goto out;
+		}
+	}
+out:
+	return;
+}
+
+/**
+ * gcm_picker_xyz_notify_cb:
+ **/
+static void
+gcm_picker_xyz_notify_cb (GcmCalibrate *calibrate_, GParamSpec *pspec, gpointer user_data)
+{
+	GcmXyz *xyz;
+
+	/* get new value */
+	g_object_get (calibrate, "xyz", &xyz, NULL);
+
+	g_object_get (xyz,
+		      "cie-x", &last_sample.X,
+		      "cie-y", &last_sample.Y,
+		      "cie-z", &last_sample.Z,
+		      NULL);
 
 	gcm_picker_refresh_results ();
+	gcm_picker_got_results ();
+	g_object_unref (xyz);
 }
 
 /**
@@ -261,34 +288,36 @@ gcm_picker_delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
 static void
 gcm_picker_sensor_client_setup_ui (GcmSensorClient *sensor_client)
 {
-	gboolean present;
-	gboolean supports_spot;
 	gboolean ret = FALSE;
 	GtkWidget *widget;
 
-#ifndef HAVE_VTE
-	/* TRANSLATORS: this is displayed if VTE support is not enabled */
-	gtk_label_set_label (GTK_LABEL (info_bar_hardware_label), _("This application was compiled without VTE support."));
-	goto out;
-#endif
-
 	/* no present */
-	present = gcm_sensor_client_get_present (sensor_client);
-	if (!present) {
+	sensor = gcm_sensor_client_get_sensor (sensor_client);
+	if (sensor == NULL) {
 		/* TRANSLATORS: this is displayed the user has not got suitable hardware */
 		gtk_label_set_label (GTK_LABEL (info_bar_hardware_label), _("No colorimeter is attached."));
 		goto out;
 	}
 
-	/* no support */
-	supports_spot = gcm_sensor_supports_spot (gcm_sensor_client_get_sensor (sensor_client));
-	ret = (present && supports_spot);
+#ifndef HAVE_VTE
+	if (!gcm_sensor_get_is_native (sensor)) {
+		 /* TRANSLATORS: this is displayed if VTE support is not enabled */
+		gtk_label_set_label (GTK_LABEL (info_bar_hardware_label), _("This application was compiled without VTE support."));
+		goto out;
+	}
+#endif
 
-	/* change the label */
-	if (present && !supports_spot) {
+#if 0
+	/* no support */
+	ret = gcm_sensor_supports_spot (sensor);
+	if (!ret) {
 		/* TRANSLATORS: this is displayed the user has not got suitable hardware */
 		gtk_label_set_label (GTK_LABEL (info_bar_hardware_label), _("The attached colorimeter is not capable of reading a spot color."));
+		goto out;
 	}
+#else
+	ret = TRUE;
+#endif
 
 out:
 	widget = GTK_WIDGET (gtk_builder_get_object (builder, "button_measure"));
@@ -361,6 +390,7 @@ gcm_prefs_space_combo_changed_cb (GtkWidget *widget, gpointer data)
 
 	profile_filename = gcm_profile_get_filename (profile);
 	egg_debug ("changed picker space %s", profile_filename);
+
 	gcm_picker_refresh_results ();
 out:
 	if (profile != NULL)
@@ -550,6 +580,9 @@ main (int argc, char *argv[])
 	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
 	                                   GCM_DATA G_DIR_SEPARATOR_S "icons");
 
+	/* create a last sample */
+	//gcm_color_xyz_clear (&last_sample);
+
 	/* use the color device */
 	sensor_client = gcm_sensor_client_new ();
 	g_signal_connect (sensor_client, "changed", G_CALLBACK (gcm_picker_sensor_client_changed_cb), NULL);
@@ -582,6 +615,7 @@ main (int argc, char *argv[])
 
 	/* maintain a list of profiles */
 	profile_store = gcm_profile_store_new ();
+	gcm_profile_store_search (profile_store, GCM_PROFILE_STORE_SEARCH_ALL);
 
 	/* default to AdobeRGB */
 	profile_filename = "/usr/share/color/icc/Argyll/ClayRGB1998.icm";
@@ -594,7 +628,7 @@ main (int argc, char *argv[])
 			  G_CALLBACK (gcm_prefs_space_combo_changed_cb), NULL);
 
 	/* setup results expander */
-	gcm_picker_refresh_results ();
+//	gcm_picker_refresh_results (NULL);
 
 	/* setup initial preview window */
 	widget = GTK_WIDGET (gtk_builder_get_object (builder, "image_preview"));



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