[gnome-color-manager] Add functionality to parse the pnp.ids file and show a vendor name for the EDID



commit a52ec01d9229d77bfa9ad3257e3d8fe6e7fad234
Author: Richard Hughes <richard hughsie com>
Date:   Thu Nov 5 16:26:09 2009 +0000

    Add functionality to parse the pnp.ids file and show a vendor name for the EDID

 src/Makefile.am     |    2 +
 src/gcm-calibrate.c |    6 +-
 src/gcm-client.c    |    2 +
 src/gcm-edid.c      |   25 ++++++-
 src/gcm-tables.c    |  223 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gcm-tables.h    |   66 +++++++++++++++
 6 files changed, 322 insertions(+), 2 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 69ce1c8..7af6248 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -30,6 +30,8 @@ libgcmshared_a_SOURCES =				\
 	gcm-clut.h					\
 	gcm-edid.c					\
 	gcm-edid.h					\
+	gcm-tables.c					\
+	gcm-tables.h					\
 	gcm-xserver.c					\
 	gcm-xserver.h					\
 	gcm-client.c					\
diff --git a/src/gcm-calibrate.c b/src/gcm-calibrate.c
index 3b804a1..f1b9c1c 100644
--- a/src/gcm-calibrate.c
+++ b/src/gcm-calibrate.c
@@ -344,6 +344,8 @@ gcm_calibrate_task_neutralise (GcmCalibrate *calibrate, GError **error)
 	if (priv->basename == NULL)
 		g_object_get (priv->edid, "ascii-string", &priv->basename, NULL);
 	if (priv->basename == NULL)
+		g_object_get (priv->edid, "vendor-name", &priv->basename, NULL);
+	if (priv->basename == NULL)
 		priv->basename = g_strdup ("custom");
 
 	/* make a suitable filename */
@@ -592,7 +594,9 @@ gcm_calibrate_task_generate_profile (GcmCalibrate *calibrate, GError **error)
 	description = g_strdup_printf ("%s, %s (%04i-%02i-%02i)", _("Custom"), description_tmp, date->year, date->month, date->day);
 
 	/* get manufacturer */
-	g_object_get (priv->edid, "ascii-string", &manufacturer, NULL);
+	g_object_get (priv->edid, "vendor-name", &manufacturer, NULL);
+	if (manufacturer == NULL)
+		g_object_get (priv->edid, "ascii-string", &manufacturer, NULL);
 	if (manufacturer == NULL)
 		manufacturer = g_strdup ("unknown manufacturer");
 
diff --git a/src/gcm-client.c b/src/gcm-client.c
index 42f9da4..520d40e 100644
--- a/src/gcm-client.c
+++ b/src/gcm-client.c
@@ -314,6 +314,8 @@ gcm_utils_get_output_name (GnomeRROutput *output)
 		g_object_get (edid, "ascii-string", &name, NULL);
 	if (name == NULL)
 		g_object_get (edid, "serial-number", &name, NULL);
+	if (name == NULL)
+		g_object_get (edid, "vendor-name", &name, NULL);
 
 out:
 	/* fallback to the output name */
diff --git a/src/gcm-edid.c b/src/gcm-edid.c
index ed31aec..91c6548 100644
--- a/src/gcm-edid.c
+++ b/src/gcm-edid.c
@@ -35,6 +35,7 @@
 #include <stdlib.h>
 
 #include "gcm-edid.h"
+#include "gcm-tables.h"
 
 #include "egg-debug.h"
 
@@ -50,15 +51,18 @@ static void     gcm_edid_finalize	(GObject     *object);
 struct _GcmEdidPrivate
 {
 	gchar				*monitor_name;
+	gchar				*vendor_name;
 	gchar				*serial_number;
 	gchar				*ascii_string;
 	gchar				*pnp_id;
 	gfloat				 gamma;
+	GcmTables			*tables;
 };
 
 enum {
 	PROP_0,
 	PROP_MONITOR_NAME,
+	PROP_VENDOR_NAME,
 	PROP_SERIAL_NUMBER,
 	PROP_ASCII_STRING,
 	PROP_GAMMA,
@@ -103,9 +107,11 @@ gcm_edid_parse (GcmEdid *edid, const guint8 *data, GError **error)
 
 	/* free old data */
 	g_free (priv->monitor_name);
+	g_free (priv->vendor_name);
 	g_free (priv->serial_number);
 	g_free (priv->ascii_string);
 	priv->monitor_name = NULL;
+	priv->vendor_name = NULL;
 	priv->serial_number = NULL;
 	priv->ascii_string = NULL;
 
@@ -183,6 +189,11 @@ gcm_edid_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec
 	case PROP_MONITOR_NAME:
 		g_value_set_string (value, priv->monitor_name);
 		break;
+	case PROP_VENDOR_NAME:
+		if (priv->vendor_name == NULL)
+			priv->vendor_name = gcm_tables_get_pnp_id (priv->tables, priv->pnp_id, NULL);
+		g_value_set_string (value, priv->vendor_name);
+		break;
 	case PROP_SERIAL_NUMBER:
 		g_value_set_string (value, priv->serial_number);
 		break;
@@ -235,6 +246,14 @@ gcm_edid_class_init (GcmEdidClass *klass)
 	g_object_class_install_property (object_class, PROP_MONITOR_NAME, pspec);
 
 	/**
+	 * GcmEdid:vendor-name:
+	 */
+	pspec = g_param_spec_string ("vendor-name", NULL, NULL,
+				     NULL,
+				     G_PARAM_READABLE);
+	g_object_class_install_property (object_class, PROP_VENDOR_NAME, pspec);
+
+	/**
 	 * GcmEdid:serial-number:
 	 */
 	pspec = g_param_spec_string ("serial-number", NULL, NULL,
@@ -277,8 +296,10 @@ gcm_edid_init (GcmEdid *edid)
 {
 	edid->priv = GCM_EDID_GET_PRIVATE (edid);
 	edid->priv->monitor_name = NULL;
+	edid->priv->vendor_name = NULL;
 	edid->priv->serial_number = NULL;
 	edid->priv->ascii_string = NULL;
+	edid->priv->tables = gcm_tables_new ();
 	edid->priv->pnp_id = g_new0 (gchar, 4);
 }
 
@@ -292,9 +313,11 @@ gcm_edid_finalize (GObject *object)
 	GcmEdidPrivate *priv = edid->priv;
 
 	g_free (priv->monitor_name);
+	g_free (priv->vendor_name);
 	g_free (priv->serial_number);
 	g_free (priv->ascii_string);
-	g_free (edid->priv->pnp_id);
+	g_free (priv->pnp_id);
+	g_object_unref (priv->tables);
 
 	G_OBJECT_CLASS (gcm_edid_parent_class)->finalize (object);
 }
diff --git a/src/gcm-tables.c b/src/gcm-tables.c
new file mode 100644
index 0000000..0e4e940
--- /dev/null
+++ b/src/gcm-tables.c
@@ -0,0 +1,223 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2009 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 tables.
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * SECTION:gcm-tables
+ * @short_description: An object to convert ID values into text
+ *
+ * This object parses the USB, PCI and PNP tables to return text for numbers.
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include "gcm-tables.h"
+
+#include "egg-debug.h"
+
+static void     gcm_tables_finalize	(GObject     *object);
+
+#define GCM_TABLES_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GCM_TYPE_TABLES, GcmTablesPrivate))
+
+/**
+ * GcmTablesPrivate:
+ *
+ * Private #GcmTables data
+ **/
+struct _GcmTablesPrivate
+{
+	gchar				*data_dir;
+	GHashTable			*pnp_table;
+};
+
+enum {
+	PROP_0,
+	PROP_DATA_DIR,
+	PROP_LAST
+};
+
+G_DEFINE_TYPE (GcmTables, gcm_tables, G_TYPE_OBJECT)
+
+/**
+ * gcm_tables_get_pnp_id:
+ **/
+gchar *
+gcm_tables_get_pnp_id (GcmTables *tables, const gchar *pnp_id, GError **error)
+{
+	GcmTablesPrivate *priv = tables->priv;
+	gchar *retval = NULL;
+	gpointer found;
+	guint size;
+	gchar *filename = NULL;
+	gboolean ret;
+	gchar *data = NULL;
+	gchar **split = NULL;
+	guint i;
+
+	g_return_val_if_fail (GCM_IS_TABLES (tables), NULL);
+	g_return_val_if_fail (pnp_id != NULL, NULL);
+
+	/* if table is empty, try to load it */
+	size = g_hash_table_size (priv->pnp_table);
+	if (size == 0) {
+
+		/* check it exists */
+		filename = g_build_filename (priv->data_dir, "pnp.ids", NULL);
+		ret = g_file_test (filename, G_FILE_TEST_EXISTS);
+		if (!ret) {
+			if (error != NULL)
+				*error = g_error_new (1, 0, "could not load %s", filename);
+			goto out;
+		}
+
+		/* load the contents */
+		egg_debug ("loading: %s", filename);
+		ret = g_file_get_contents (filename, &data, NULL, error);
+		if (!ret)
+			goto out;
+
+		/* parse into lines */
+		split = g_strsplit (data, "\n", -1);
+		for (i=0; split[i] != NULL; i++) {
+			split[i][3] = '\0';
+			g_hash_table_insert (priv->pnp_table, g_strdup (split[i]), g_strdup (&split[i][4]));
+		}
+	}
+
+	/* look this up in the table */
+	found = g_hash_table_lookup (priv->pnp_table, pnp_id);
+	if (found == NULL) {
+		if (error != NULL)
+			*error = g_error_new (1, 0, "could not find %s", pnp_id);
+		goto out;
+	}
+
+	/* return a copy */
+	retval = g_strdup (found);
+out:
+	g_free (data);
+	g_free (filename);
+	g_strfreev (split);
+	return retval;
+}
+
+/**
+ * gcm_tables_get_property:
+ **/
+static void
+gcm_tables_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+{
+	GcmTables *tables = GCM_TABLES (object);
+	GcmTablesPrivate *priv = tables->priv;
+
+	switch (prop_id) {
+	case PROP_DATA_DIR:
+		g_value_set_string (value, priv->data_dir);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+/**
+ * gcm_tables_set_property:
+ **/
+static void
+gcm_tables_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+{
+	GcmTables *tables = GCM_TABLES (object);
+	GcmTablesPrivate *priv = tables->priv;
+
+	switch (prop_id) {
+	case PROP_DATA_DIR:
+		g_free (priv->data_dir);
+		priv->data_dir = g_strdup (g_value_get_string (value));
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+/**
+ * gcm_tables_class_init:
+ **/
+static void
+gcm_tables_class_init (GcmTablesClass *klass)
+{
+	GParamSpec *pspec;
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	object_class->finalize = gcm_tables_finalize;
+	object_class->get_property = gcm_tables_get_property;
+	object_class->set_property = gcm_tables_set_property;
+
+	/**
+	 * GcmTables:data-dir:
+	 */
+	pspec = g_param_spec_string ("data-dir", NULL, NULL,
+				     NULL,
+				     G_PARAM_READABLE);
+	g_object_class_install_property (object_class, PROP_DATA_DIR, pspec);
+
+	g_type_class_add_private (klass, sizeof (GcmTablesPrivate));
+}
+
+/**
+ * gcm_tables_init:
+ **/
+static void
+gcm_tables_init (GcmTables *tables)
+{
+	tables->priv = GCM_TABLES_GET_PRIVATE (tables);
+	tables->priv->data_dir = g_strdup ("/usr/share/hwdata");
+	tables->priv->pnp_table = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free, (GDestroyNotify) g_free);
+}
+
+/**
+ * gcm_tables_finalize:
+ **/
+static void
+gcm_tables_finalize (GObject *object)
+{
+	GcmTables *tables = GCM_TABLES (object);
+	GcmTablesPrivate *priv = tables->priv;
+
+	g_free (priv->data_dir);
+	g_hash_table_unref (priv->pnp_table);
+
+	G_OBJECT_CLASS (gcm_tables_parent_class)->finalize (object);
+}
+
+/**
+ * gcm_tables_new:
+ *
+ * Return value: a new GcmTables object.
+ **/
+GcmTables *
+gcm_tables_new (void)
+{
+	GcmTables *tables;
+	tables = g_object_new (GCM_TYPE_TABLES, NULL);
+	return GCM_TABLES (tables);
+}
+
diff --git a/src/gcm-tables.h b/src/gcm-tables.h
new file mode 100644
index 0000000..4afaec2
--- /dev/null
+++ b/src/gcm-tables.h
@@ -0,0 +1,66 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2009 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 tables.
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GCM_TABLES_H
+#define __GCM_TABLES_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GCM_TYPE_TABLES			(gcm_tables_get_type ())
+#define GCM_TABLES(o)			(G_TYPE_CHECK_INSTANCE_CAST ((o), GCM_TYPE_TABLES, GcmTables))
+#define GCM_TABLES_CLASS(k)		(G_TYPE_CHECK_CLASS_CAST((k), GCM_TYPE_TABLES, GcmTablesClass))
+#define GCM_IS_TABLES(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), GCM_TYPE_TABLES))
+#define GCM_IS_TABLES_CLASS(k)		(G_TYPE_CHECK_CLASS_TYPE ((k), GCM_TYPE_TABLES))
+#define GCM_TABLES_GET_CLASS(o)		(G_TYPE_INSTANCE_GET_CLASS ((o), GCM_TYPE_TABLES, GcmTablesClass))
+
+typedef struct _GcmTablesPrivate	GcmTablesPrivate;
+typedef struct _GcmTables		GcmTables;
+typedef struct _GcmTablesClass		GcmTablesClass;
+
+struct _GcmTables
+{
+	 GObject			 parent;
+	 GcmTablesPrivate		*priv;
+};
+
+struct _GcmTablesClass
+{
+	GObjectClass	parent_class;
+	/* padding for future expansion */
+	void (*_gcm_reserved1) (void);
+	void (*_gcm_reserved2) (void);
+	void (*_gcm_reserved3) (void);
+	void (*_gcm_reserved4) (void);
+	void (*_gcm_reserved5) (void);
+};
+
+GType		 gcm_tables_get_type		  	(void);
+GcmTables	*gcm_tables_new				(void);
+gchar		*gcm_tables_get_pnp_id			(GcmTables		*tables,
+							 const gchar		*pnp_id,
+							 GError			**error);
+
+G_END_DECLS
+
+#endif /* __GCM_TABLES_H */
+



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