hal: Branch 'master'

David Zeuthen david at kemper.freedesktop.org
Wed Jan 31 13:33:56 PST 2007


 hald/linux/device.c |   32 +++++++++++++----
 hald/util.c         |   96 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 hald/util.h         |    1 
 3 files changed, 121 insertions(+), 8 deletions(-)

New commits:
diff-tree f0266b7c5972f747d0730af7ccaea8e95c1c5816 (from f47230b49f1234f34c617e79963d18ba3a68cb26)
Author: Matthias Kretz <kretz at kde.org>
Date:   Wed Jan 31 16:33:35 2007 -0500

    better ALSA card name
    
    > So if I were to ask anything of the ALSA developers it would be to add
    > another file to easily get the card name... (because it's not easy to
    > extract out of /proc/asound/cards)
    
    Until that is available here's a new patch that gets the card_id property for
    all ALSA and OSS devices from /proc/asound/cards. (Patch against the git repo
    this time.)
    
    Without this patch on my computer:
    card 1: Intel
    card 2: io2
    card 3: default
    
    With this patch:
    card 1: HDA Intel
    card 2: Alesis io|2
    card 3: C-Media USB Headphone Set

diff --git a/hald/linux/device.c b/hald/linux/device.c
index 43f5d72..c22077e 100644
--- a/hald/linux/device.c
+++ b/hald/linux/device.c
@@ -559,6 +559,26 @@ dvb_compute_udi (HalDevice *d)
 	return TRUE;
 }
 
+static void
+asound_card_id_set (int cardnum, HalDevice *d, const char *propertyname)
+{
+	char aprocdir[256];
+	char linestart[5];
+	gchar *alsaname;
+
+	snprintf (aprocdir, sizeof (aprocdir), "%s/asound", get_hal_proc_path ());
+	snprintf (linestart, sizeof (linestart), "%2d [", cardnum);
+	alsaname = hal_util_grep_file_next_line (aprocdir, "cards", linestart, TRUE);
+	if (alsaname != NULL) {
+		gchar *end;
+		end = strstr (alsaname, " at ");
+		if (end != NULL) {
+			end[0] = '\0';
+		}
+		alsaname = g_strstrip (alsaname);
+		hal_device_property_set_string (d, propertyname, alsaname);
+	}
+}
 
 /*--------------------------------------------------------------------------------------------------------------*/
 
@@ -623,8 +643,7 @@ sound_add (const gchar *sysfs_path, cons
 			hal_device_property_set_int (d, "alsa.card", cardnum);
 			hal_device_property_set_string (d, "alsa.type", "control");
 	
-			snprintf (aprocdir, sizeof (aprocdir), "%s/asound/card%d", get_hal_proc_path (), cardnum);
-			hal_util_set_string_from_file (d, "alsa.card_id", aprocdir, "id");
+			asound_card_id_set (cardnum, d, "alsa.card_id");
 	
 			snprintf (buf, sizeof (buf), "%s ALSA Control Device", 
 				hal_device_property_get_string (d, "alsa.card_id"));
@@ -640,8 +659,7 @@ sound_add (const gchar *sysfs_path, cons
 			hal_device_property_set_int (d, "alsa.card", cardnum);
 			hal_device_property_set_int (d, "alsa.device", devicenum);
 	
-			snprintf (aprocdir, sizeof (aprocdir), "%s/asound/card%d", get_hal_proc_path (), cardnum);
-			hal_util_set_string_from_file (d, "alsa.card_id", aprocdir, "id");
+			asound_card_id_set (cardnum, d, "alsa.card_id");
 
 			if (!hal_util_set_string_from_file (d, "alsa.pcm_class", sysfs_path, "pcm_class"))
 				 hal_device_property_set_string (d, "alsa.pcm_class", "unknown");
@@ -685,8 +703,7 @@ sound_add (const gchar *sysfs_path, cons
 			hal_device_property_set_int (d, "alsa.card", cardnum);
 			hal_device_property_set_int (d, "alsa.device", devicenum);
 	
-			snprintf (aprocdir, sizeof (aprocdir), "%s/asound/card%d", get_hal_proc_path (), cardnum);
-			hal_util_set_string_from_file (d, "alsa.card_id", aprocdir, "id");
+			asound_card_id_set (cardnum, d, "alsa.card_id");
 
 			hal_device_property_set_string (d, "alsa.type", "hw_specific");
 			
@@ -730,8 +747,7 @@ sound_add (const gchar *sysfs_path, cons
 			hal_device_property_set_string (d, "oss.physical_device", hal_device_get_udi (parent_dev));
 			hal_device_property_set_int (d, "oss.card", cardnum);
 	
-			snprintf (aprocdir, sizeof (aprocdir), "%s/asound/card%d", get_hal_proc_path (), cardnum);
-			hal_util_set_string_from_file (d, "oss.card_id", aprocdir, "id");
+			asound_card_id_set (cardnum, d, "oss.card_id");
 	
 			snprintf (aprocdir, sizeof (aprocdir), "%s/asound/card%d/pcm0p", 
 				get_hal_proc_path (), cardnum);
diff --git a/hald/util.c b/hald/util.c
index 522e824..6ad4fcf 100644
--- a/hald/util.c
+++ b/hald/util.c
@@ -548,6 +548,102 @@ out:
 	return result;
 }
 
+/** Given a directory and filename, open the file and search for the
+ *  first line that starts with the given linestart string. Returns
+ *  the next line as a string if found.
+ *
+ *  @param  directory           Directory, e.g. "/proc/acpi/battery/BAT0"
+ *  @param  file                File, e.g. "info"
+ *  @param  linestart           Start of line, e.g. "serial number"
+ *  @param  reuse               Whether we should reuse the file contents
+ *                              if the file is the same; can be cleared
+ *                              with hal_util_grep_discard_existing_data()
+ *  @return                     NULL if not found, otherwise the next
+ *                              line.
+ *                              The string is only valid until the next
+ *                              invocation of this function.
+ */
+gchar *
+hal_util_grep_file_next_line (const gchar *directory, const gchar *file, const gchar *linestart, gboolean reuse)
+{
+	static gchar buf[2048];
+	static unsigned int bufsize;
+	static gchar filename[HAL_PATH_MAX];
+	static gchar oldfilename[HAL_PATH_MAX];
+	gchar *result;
+	gsize linestart_len;
+	gchar *p;
+
+	result = NULL;
+
+	/* TODO: use reuse and _grep_can_reuse parameters to avoid loading
+	 *       the file again and again
+	 */
+
+	if (file != NULL && strlen (file) > 0)
+		snprintf (filename, sizeof (filename), "%s/%s", directory, file);
+	else
+		strncpy (filename, directory, sizeof (filename));
+
+	if (_grep_can_reuse && reuse && strcmp (oldfilename, filename) == 0) {
+		/* just reuse old file; e.g. bufsize, buf */
+		/*HAL_INFO (("hal_util_grep_file: reusing buf for %s", filename));*/
+	} else {
+		FILE *f;
+
+		f = fopen (filename, "r");
+		if (f == NULL)
+			goto out;
+		bufsize = fread (buf, sizeof (char), sizeof (buf) - 1, f);
+		buf[bufsize] = '\0';
+		fclose (f);
+
+		/*HAL_INFO (("hal_util_grep_file: read %s of %d bytes", filename, bufsize));*/
+	}
+
+	/* book keeping */
+	_grep_can_reuse = TRUE;
+	strncpy (oldfilename, filename, sizeof(oldfilename));
+
+	linestart_len = strlen (linestart);
+
+	/* analyze buf */
+	p = buf;
+	do {
+		unsigned int linelen;
+		static char line[256];
+
+		for (linelen = 0; p[linelen] != '\n' && p[linelen] != '\0'; linelen++)
+			;
+
+		if (linelen < sizeof (line)) {
+
+			strncpy (line, p, linelen);
+			line[linelen] = '\0';
+
+			if (strncmp (line, linestart, linestart_len) == 0) {
+				/* go to next line */
+				p += linelen + 1;
+				if (p < buf + bufsize) { /* if there is one*/
+					for (linelen = 0; p[linelen] != '\n' && p[linelen] != '\0'; linelen++)
+						;
+					strncpy (line, p, linelen);
+					line[linelen] = '\0';
+
+					result = line;
+				}
+				goto out;
+			}
+		}
+
+		p += linelen + 1;
+
+	} while (p < buf + bufsize);
+
+out:
+	return result;
+}
+
 gchar *
 hal_util_grep_string_elem_from_file (const gchar *directory, const gchar *file, 
 				     const gchar *linestart, guint elem, gboolean reuse)
diff --git a/hald/util.h b/hald/util.h
index 85f0ab6..caf19e9 100644
--- a/hald/util.h
+++ b/hald/util.h
@@ -72,6 +72,7 @@ gboolean hal_util_path_ascend (gchar *pa
 void hal_util_grep_discard_existing_data (void);
 
 gchar *hal_util_grep_file (const gchar *directory, const gchar *file, const gchar *linestart, gboolean reuse_file);
+gchar *hal_util_grep_file_next_line (const gchar *directory, const gchar *file, const gchar *linestart, gboolean reuse);
 
 gint hal_util_grep_int_elem_from_file (const gchar *directory, const gchar *file, 
 				       const gchar *linestart, guint elem, guint base, gboolean reuse_file);


More information about the hal-commit mailing list