[gnome-color-manager] trivial: move a lot of the udev-specific code into it's own class device file



commit 66550182e791da488b343476adc74ec328eb73c0
Author: Richard Hughes <richard hughsie com>
Date:   Thu Feb 4 15:18:07 2010 +0000

    trivial: move a lot of the udev-specific code into it's own class device file

 src/Makefile.am                               |    4 +-
 src/gcm-client.c                              |  110 +++---------
 src/gcm-device-sysfs.c                        |  160 -----------------
 src/gcm-device-udev.c                         |  239 +++++++++++++++++++++++++
 src/{gcm-device-sysfs.h => gcm-device-udev.h} |   33 ++--
 src/gcm-device.c                              |    6 +-
 6 files changed, 287 insertions(+), 265 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 84bf635..97a6462 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -53,8 +53,8 @@ libgcmshared_a_SOURCES =				\
 	gcm-device.h					\
 	gcm-device-xrandr.c				\
 	gcm-device-xrandr.h				\
-	gcm-device-sysfs.c				\
-	gcm-device-sysfs.h				\
+	gcm-device-udev.c				\
+	gcm-device-udev.h				\
 	gcm-device-cups.c				\
 	gcm-device-cups.h				\
 	gcm-cie-widget.c				\
diff --git a/src/gcm-client.c b/src/gcm-client.c
index 9e8b96b..38c89f6 100644
--- a/src/gcm-client.c
+++ b/src/gcm-client.c
@@ -36,7 +36,7 @@
 
 #include "gcm-client.h"
 #include "gcm-device-xrandr.h"
-#include "gcm-device-sysfs.h"
+#include "gcm-device-udev.h"
 #include "gcm-device-cups.h"
 #include "gcm-screen.h"
 #include "gcm-utils.h"
@@ -183,79 +183,44 @@ out:
 }
 
 /**
- * gcm_client_gudev_add_type:
+ * gcm_client_gudev_add:
  **/
 static void
-gcm_client_gudev_add_type (GcmClient *client, GUdevDevice *udev_device, GcmDeviceTypeEnum type)
+gcm_client_gudev_add (GcmClient *client, GUdevDevice *udev_device)
 {
-	gchar *title;
 	GcmDevice *device = NULL;
-	gchar *id;
+	GcmDevice *device_tmp = NULL;
 	gboolean ret;
+	const gchar *value;
 	GError *error = NULL;
-	const gchar *sysfs_path;
 	GcmClientPrivate *priv = client->priv;
-	gchar *serial = NULL;
-	gchar *manufacturer = NULL;
-	gchar *model = NULL;
 
-	/* add new device */
-	id = gcm_client_get_id_for_udev_device (udev_device);
-	title = g_strdup_printf ("%s - %s",
-				g_udev_device_get_property (udev_device, "ID_VENDOR"),
-				g_udev_device_get_property (udev_device, "ID_MODEL"));
+	/* matches our udev rules, which we can change without recompiling */
+	value = g_udev_device_get_property (udev_device, "GCM_DEVICE");
+	if (value == NULL)
+		goto out;
 
-	/* turn space delimiters into spaces */
-	g_strdelimit (title, "_", ' ');
+	/* create new device */
+	device = gcm_device_udev_new ();
+	ret = gcm_device_udev_set_from_device (device, udev_device, &error);
+	if (!ret) {
+		egg_debug ("failed to set for device: %s", error->message);
+		g_error_free (error);
+		goto out;
+	}
 
 	/* we might have a previous saved device with this ID, in which case nuke it */
-	device = gcm_client_get_device_by_id (client, id);
-	if (device != NULL) {
+	device_tmp = gcm_client_get_device_by_id (client, gcm_device_get_id (device));
+	if (device_tmp != NULL) {
+
 		/* remove from the array */
-		g_ptr_array_remove (client->priv->array, device);
+		g_ptr_array_remove (client->priv->array, device_tmp);
 
 		/* emit a signal */
-		egg_debug ("emit removed: %s", id);
-		g_signal_emit (client, signals[SIGNAL_REMOVED], 0, device);
-
-		/* unref our copy */
-		g_object_unref (device);
-	}
-
-	/* get sysfs path */
-	sysfs_path = g_udev_device_get_sysfs_path (udev_device);
-
-	/* create device */
-	device = gcm_device_sysfs_new ();
-	manufacturer = g_strdup (g_udev_device_get_property (udev_device, "ID_VENDOR"));
-	model = g_strdup (g_udev_device_get_property (udev_device, "ID_MODEL"));
-	serial = g_strdup (g_udev_device_get_property (udev_device, "ID_SERIAL"));
-
-	/* get rid of underscores */
-	if (manufacturer != NULL) {
-		g_strdelimit (manufacturer, "_", ' ');
-		g_strchomp (manufacturer);
-	}
-	if (model != NULL) {
-		g_strdelimit (model, "_", ' ');
-		g_strchomp (model);
-	}
-	if (serial != NULL) {
-		g_strdelimit (serial, "_", ' ');
-		g_strchomp (serial);
+		egg_debug ("emit removed: %s", gcm_device_get_id (device));
+		g_signal_emit (client, signals[SIGNAL_REMOVED], 0, device_tmp);
 	}
 
-	g_object_set (device,
-		      "type", type,
-		      "id", id,
-		      "connected", TRUE,
-		      "serial", serial,
-		      "model", model,
-		      "manufacturer", manufacturer,
-		      "title", title,
-		      "native-device", sysfs_path,
-		      NULL);
-
 	/* load the device */
 	ret = gcm_device_load (device, &error);
 	if (!ret) {
@@ -268,33 +233,13 @@ gcm_client_gudev_add_type (GcmClient *client, GUdevDevice *udev_device, GcmDevic
 	g_ptr_array_add (priv->array, g_object_ref (device));
 
 	/* signal the addition */
-	egg_debug ("emit: added %s to device list", id);
+	egg_debug ("emit: added %s to device list", gcm_device_get_id (device));
 	g_signal_emit (client, signals[SIGNAL_ADDED], 0, device);
 out:
 	if (device != NULL)
 		g_object_unref (device);
-	g_free (serial);
-	g_free (manufacturer);
-	g_free (model);
-	g_free (id);
-	g_free (title);
-}
-
-/**
- * gcm_client_gudev_add:
- **/
-static void
-gcm_client_gudev_add (GcmClient *client, GUdevDevice *udev_device)
-{
-	const gchar *value;
-
-	/* matches our udev rules, which we can change without recompiling */
-	value = g_udev_device_get_property (udev_device, "GCM_DEVICE");
-	if (value != NULL) {
-		value = g_udev_device_get_property (udev_device, "GCM_TYPE");
-		egg_debug ("found %s device: %s", value, g_udev_device_get_sysfs_path (udev_device));
-		gcm_client_gudev_add_type (client, udev_device, gcm_device_type_enum_from_string (value));
-	}
+	if (device_tmp != NULL)
+		g_object_unref (device_tmp);
 }
 
 /**
@@ -510,9 +455,6 @@ gcm_client_xrandr_add (GcmClient *client, GnomeRROutput *output)
 		/* emit a signal */
 		egg_debug ("emit removed: %s (unconnected device that has just been connected)", gcm_device_get_id (device_tmp));
 		g_signal_emit (client, signals[SIGNAL_REMOVED], 0, device_tmp);
-
-		/* unref our copy */
-		g_object_unref (device_tmp);
 	}
 
 	/* load the device */
diff --git a/src/gcm-device-udev.c b/src/gcm-device-udev.c
new file mode 100644
index 0000000..e5e2daf
--- /dev/null
+++ b/src/gcm-device-udev.c
@@ -0,0 +1,239 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2009-2010 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include "gcm-device-udev.h"
+#include "gcm-utils.h"
+
+#include "egg-debug.h"
+
+static void     gcm_device_udev_finalize	(GObject     *object);
+
+#define GCM_DEVICE_UDEV_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GCM_TYPE_DEVICE_UDEV, GcmDeviceUdevPrivate))
+
+/**
+ * GcmDeviceUdevPrivate:
+ *
+ * Private #GcmDeviceUdev data
+ **/
+struct _GcmDeviceUdevPrivate
+{
+	gchar				*native_device;
+};
+
+enum {
+	PROP_0,
+	PROP_NATIVE_DEVICE,
+	PROP_LAST
+};
+
+G_DEFINE_TYPE (GcmDeviceUdev, gcm_device_udev, GCM_TYPE_DEVICE)
+
+/**
+ * gcm_device_udev_get_id_for_udev_device:
+ **/
+static gchar *
+gcm_device_udev_get_id_for_udev_device (GUdevDevice *udev_device)
+{
+	gchar *id;
+
+	/* get id */
+	id = g_strdup_printf ("sysfs_%s_%s",
+			      g_udev_device_get_property (udev_device, "ID_VENDOR"),
+			      g_udev_device_get_property (udev_device, "ID_MODEL"));
+
+	/* replace unsafe chars */
+	gcm_utils_alphanum_lcase (id);
+	return id;
+}
+
+/**
+ * gcm_device_udev_set_from_device:
+ **/
+gboolean
+gcm_device_udev_set_from_device (GcmDevice *device, GUdevDevice *udev_device, GError **error)
+{
+	gchar *id = NULL;
+	gchar *title;
+	gchar *serial = NULL;
+	gchar *manufacturer = NULL;
+	gchar *model = NULL;
+	GcmDeviceTypeEnum type;
+	const gchar *value;
+	const gchar *sysfs_path;
+
+	/* matches our udev rules, which we can change without recompiling */
+	value = g_udev_device_get_property (udev_device, "GCM_TYPE");
+	type = gcm_device_type_enum_from_string (value);
+
+	/* add new device */
+	id = gcm_device_udev_get_id_for_udev_device (udev_device);
+	title = g_strdup_printf ("%s - %s",
+				g_udev_device_get_property (udev_device, "ID_VENDOR"),
+				g_udev_device_get_property (udev_device, "ID_MODEL"));
+
+	/* turn space delimiters into spaces */
+	g_strdelimit (title, "_", ' ');
+
+	/* get sysfs path */
+	sysfs_path = g_udev_device_get_sysfs_path (udev_device);
+
+	/* create device */
+	manufacturer = g_strdup (g_udev_device_get_property (udev_device, "ID_VENDOR"));
+	model = g_strdup (g_udev_device_get_property (udev_device, "ID_MODEL"));
+	serial = g_strdup (g_udev_device_get_property (udev_device, "ID_SERIAL"));
+
+	/* get rid of underscores */
+	if (manufacturer != NULL) {
+		g_strdelimit (manufacturer, "_", ' ');
+		g_strchomp (manufacturer);
+	}
+	if (model != NULL) {
+		g_strdelimit (model, "_", ' ');
+		g_strchomp (model);
+	}
+	if (serial != NULL) {
+		g_strdelimit (serial, "_", ' ');
+		g_strchomp (serial);
+	}
+
+	g_object_set (device,
+		      "type", type,
+		      "id", id,
+		      "connected", TRUE,
+		      "serial", serial,
+		      "model", model,
+		      "manufacturer", manufacturer,
+		      "title", title,
+		      "native-device", sysfs_path,
+		      NULL);
+
+	g_free (serial);
+	g_free (manufacturer);
+	g_free (model);
+	g_free (id);
+	g_free (title);
+	return TRUE;
+}
+
+/**
+ * gcm_device_udev_get_property:
+ **/
+static void
+gcm_device_udev_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+{
+	GcmDeviceUdev *device_udev = GCM_DEVICE_UDEV (object);
+	GcmDeviceUdevPrivate *priv = device_udev->priv;
+
+	switch (prop_id) {
+	case PROP_NATIVE_DEVICE:
+		g_value_set_string (value, priv->native_device);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+/**
+ * gcm_device_udev_set_property:
+ **/
+static void
+gcm_device_udev_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+{
+	GcmDeviceUdev *device_udev = GCM_DEVICE_UDEV (object);
+	GcmDeviceUdevPrivate *priv = device_udev->priv;
+
+	switch (prop_id) {
+	case PROP_NATIVE_DEVICE:
+		g_free (priv->native_device);
+		priv->native_device = g_strdup (g_value_get_string (value));
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+/**
+ * gcm_device_udev_class_init:
+ **/
+static void
+gcm_device_udev_class_init (GcmDeviceUdevClass *klass)
+{
+	GParamSpec *pspec;
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	object_class->finalize = gcm_device_udev_finalize;
+	object_class->get_property = gcm_device_udev_get_property;
+	object_class->set_property = gcm_device_udev_set_property;
+
+
+	/**
+	 * GcmDeviceUdev:native-device:
+	 */
+	pspec = g_param_spec_string ("native-device", NULL, NULL,
+				     NULL,
+				     G_PARAM_READWRITE);
+	g_object_class_install_property (object_class, PROP_NATIVE_DEVICE, pspec);
+
+	g_type_class_add_private (klass, sizeof (GcmDeviceUdevPrivate));
+}
+
+/**
+ * gcm_device_udev_init:
+ **/
+static void
+gcm_device_udev_init (GcmDeviceUdev *device_udev)
+{
+	device_udev->priv = GCM_DEVICE_UDEV_GET_PRIVATE (device_udev);
+	device_udev->priv->native_device = NULL;
+}
+
+/**
+ * gcm_device_udev_finalize:
+ **/
+static void
+gcm_device_udev_finalize (GObject *object)
+{
+	GcmDeviceUdev *device_udev = GCM_DEVICE_UDEV (object);
+	GcmDeviceUdevPrivate *priv = device_udev->priv;
+
+	g_free (priv->native_device);
+
+	G_OBJECT_CLASS (gcm_device_udev_parent_class)->finalize (object);
+}
+
+/**
+ * gcm_device_udev_new:
+ *
+ * Return value: a new #GcmDevice object.
+ **/
+GcmDevice *
+gcm_device_udev_new (void)
+{
+	GcmDevice *device;
+	device = g_object_new (GCM_TYPE_DEVICE_UDEV, NULL);
+	return GCM_DEVICE (device);
+}
+
diff --git a/src/gcm-device-sysfs.h b/src/gcm-device-udev.h
similarity index 58%
rename from src/gcm-device-sysfs.h
rename to src/gcm-device-udev.h
index 1545ed7..e871887 100644
--- a/src/gcm-device-sysfs.h
+++ b/src/gcm-device-udev.h
@@ -19,41 +19,42 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifndef __GCM_DEVICE_SYSFS_H
-#define __GCM_DEVICE_SYSFS_H
+#ifndef __GCM_DEVICE_UDEV_H
+#define __GCM_DEVICE_UDEV_H
 
 #include <glib-object.h>
+#include <gudev/gudev.h>
 
 #include "gcm-device.h"
 
 G_BEGIN_DECLS
 
-#define GCM_TYPE_DEVICE_SYSFS		(gcm_device_sysfs_get_type ())
-#define GCM_DEVICE_SYSFS(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), GCM_TYPE_DEVICE_SYSFS, GcmDeviceSysfs))
-#define GCM_IS_DEVICE_SYSFS(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), GCM_TYPE_DEVICE_SYSFS))
+#define GCM_TYPE_DEVICE_UDEV		(gcm_device_udev_get_type ())
+#define GCM_DEVICE_UDEV(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), GCM_TYPE_DEVICE_UDEV, GcmDeviceUdev))
+#define GCM_IS_DEVICE_UDEV(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), GCM_TYPE_DEVICE_UDEV))
 
-typedef struct _GcmDeviceSysfsPrivate	GcmDeviceSysfsPrivate;
-typedef struct _GcmDeviceSysfs		GcmDeviceSysfs;
-typedef struct _GcmDeviceSysfsClass	GcmDeviceSysfsClass;
+typedef struct _GcmDeviceUdevPrivate	GcmDeviceUdevPrivate;
+typedef struct _GcmDeviceUdev		GcmDeviceUdev;
+typedef struct _GcmDeviceUdevClass	GcmDeviceUdevClass;
 
-struct _GcmDeviceSysfs
+struct _GcmDeviceUdev
 {
 	 GcmDevice			 parent;
-	 GcmDeviceSysfsPrivate		*priv;
+	 GcmDeviceUdevPrivate		*priv;
 };
 
-struct _GcmDeviceSysfsClass
+struct _GcmDeviceUdevClass
 {
 	GcmDeviceClass	parent_class;
 };
 
-GType		 gcm_device_sysfs_get_type		  	(void);
-GcmDevice	*gcm_device_sysfs_new				(void);
-gboolean	 gcm_device_sysfs_set_from_instance		(GcmDevice	*device,
-								 gpointer	 instance,
+GType		 gcm_device_udev_get_type		  	(void);
+GcmDevice	*gcm_device_udev_new				(void);
+gboolean	 gcm_device_udev_set_from_device		(GcmDevice	*device,
+								 GUdevDevice	*udev_device,
 								 GError		**error);
 
 G_END_DECLS
 
-#endif /* __GCM_DEVICE_SYSFS_H */
+#endif /* __GCM_DEVICE_UDEV_H */
 
diff --git a/src/gcm-device.c b/src/gcm-device.c
index 1bc876d..31731b7 100644
--- a/src/gcm-device.c
+++ b/src/gcm-device.c
@@ -77,7 +77,7 @@ enum {
 	PROP_CONTRAST,
 	PROP_PROFILE_FILENAME,
 	PROP_TITLE,
-	PROP_NATIVE_DEVICE_SYSFS,
+	PROP_NATIVE_DEVICE_UDEV,
 	PROP_LAST
 };
 
@@ -674,7 +674,7 @@ gcm_device_new (void)
 #ifdef EGG_TEST
 #include "egg-test.h"
 
-#include "gcm-device-sysfs.h"
+#include "gcm-device-udev.h"
 
 void
 gcm_device_test (EggTest *test)
@@ -693,7 +693,7 @@ gcm_device_test (EggTest *test)
 
 	/************************************************************/
 	egg_test_title (test, "get a device object");
-	device = gcm_device_sysfs_new ();
+	device = gcm_device_udev_new ();
 	egg_test_assert (test, device != NULL);
 
 	/************************************************************/



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