hal/hald/linux2/addons addon-acpi.c, 1.1, 1.2 addon-storage.c, 1.3, 1.4

David Zeuthen david at freedesktop.org
Wed Mar 2 11:19:15 PST 2005


Update of /cvs/hal/hal/hald/linux2/addons
In directory gabe:/tmp/cvs-serv21926/hald/linux2/addons

Modified Files:
	addon-acpi.c addon-storage.c 
Log Message:
2005-03-02  David Zeuthen  <davidz at redhat.com>

	* tools/device-manager/DeviceManager.py: Move some things around
	only to discover that D-BUS python bindings need fixing to actually
	push signals we subscribe to without giving the path...

	* tools/lshal.c (device_condition): Fix this function so it works
	after the DeviceCondition format changes earlier

	* libhal/libhal.h: Export prototype for libhal_device_emit_condition

	* libhal/libhal.c (libhal_device_emit_condition): New function

	* hald/linux2/addons/addon-storage.c (main): Fix that we should
	be verbose

	* hald/linux2/addons/addon-acpi.c (read_line): Rewrite this
	to not malloc all over the place
	(main): Try two places for ACPI events. Rework parsing of events
	and send out DeviceConditions where appropriate

	* hald/hald_dbus.c (device_emit_condition): New function
	(hald_dbus_filter_function): Handle EmitCondition

2005-03-01  Richard Hughes  <richard at hughsie.com>

	* hald/linux2/addons/addon-acpi.c (main): Call
	libhal_device_rescan for each ACPI event.



Index: addon-acpi.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux2/addons/addon-acpi.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- addon-acpi.c	8 Feb 2005 16:44:20 -0000	1.1
+++ addon-acpi.c	2 Mar 2005 19:19:13 -0000	1.2
@@ -38,32 +38,28 @@
 
 #include "libhal/libhal.h"
 
-#define MAX_BUFLEN	1024
+#include "../probing/shared.h"
+
 static char *
-read_line(int fd)
+read_line (int fd)
 {
-	static char *buf;
-	int buflen = 64;
-	int i = 0;
-	int r;
-	int searching = 1;
+	unsigned int i;
+	unsigned int r;
+	static char buf[256];
+	char *res;
+	dbus_bool_t searching;
 
-	while (searching) {
-		buf = realloc(buf, buflen);
-		if (!buf) {
-			fprintf(stderr, "ERR: malloc(%d): %s\n",
-				buflen, strerror(errno));
-			return NULL;
-		}
-		memset(buf+i, 0, buflen-i);
+	i = 0;
+	res = NULL;
+	searching = TRUE;
 
-		while (i < buflen) {
-			r = read(fd, buf+i, 1);
+	while (searching) {
+		while (i < sizeof (buf)) {
+			r = read(fd, buf + i, 1);
 			if (r < 0 && errno != EINTR) {
 				/* we should do something with the data */
-				fprintf(stderr, "ERR: read(): %s\n",
-					strerror(errno));
-				return NULL;
+				dbg ("ERR read(): %s\n", strerror(errno));
+				goto out;
 			} else if (r == 0) {
 				/* signal this in an almost standard way */
 				errno = EPIPE;
@@ -71,20 +67,24 @@
 			} else if (r == 1) {
 				/* scan for a newline */
 				if (buf[i] == '\n') {
-					searching = 0;
+					searching = FALSE;
 					buf[i] = '\0';
-					break;
+					res = buf;
+					goto out;
 				}
 				i++;
 			}
 		}
-		if (buflen >= MAX_BUFLEN) {
-			break;
+
+		if (i >= sizeof (buf)) {
+			dbg ("ERR: buffer size of %d is too small\n", sizeof (buf));
+			goto out;
 		}
-		buflen *= 2;
+
 	}
 
-	return buf;
+out:	
+	return res;
 }
 
 int
@@ -95,32 +95,44 @@
 	LibHalContext *ctx = NULL;
 	DBusError error;
 	DBusConnection *conn;
-	char buf[256];
-
+	char acpi_path[256];
+	char acpi_name[256];
+	unsigned int acpi_num1;
+	unsigned int acpi_num2;
+	
+	if ((getenv ("HALD_VERBOSE")) != NULL)
+		is_verbose = TRUE;
+	
 	dbus_error_init (&error);
 	if ((conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error)) == NULL)
 		goto out;
-
+	
 	if ((ctx = libhal_ctx_new ()) == NULL)
 		goto out;
 	if (!libhal_ctx_set_dbus_connection (ctx, conn))
 		goto out;
 	if (!libhal_ctx_init (ctx, &error))
 		goto out;
-
+	
 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (fd < 0) {
 		return fd;
 	}
-
+	
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_UNIX;
-	/*sprintf(addr.sun_path, "%s", "/proc/acpi/socket");*/
-	sprintf(addr.sun_path, "%s", "/var/run/acpid.socket");
+	/* TODO: get mountpoint of proc from... /proc/mounts.. :-) */
+	snprintf(addr.sun_path, sizeof (addr.sun_path), "%s", "/proc/acpi/event");
 	if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
-		goto out;
+		dbg ("Cannot open /proc/acpi/event: %s - trying trying /var/run/acpid.socket", strerror (errno));
+		/* TODO: make /var/run/acpid.socket a configure option */
+		snprintf (addr.sun_path, sizeof (addr.sun_path), "%s", "/var/run/acpid.socket");
+		if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
+			dbg ("Cannot open /var/run/acpid.socket - bailing out");
+			goto out;
+		}
 	}
-
+		
 	/* main loop */
 	while (1) {
 		char *event;
@@ -128,18 +140,43 @@
 		/* read and handle an event */
 		event = read_line (fd);
 		if (event) {
-			fprintf(stdout, "%s\n", event);
+			dbg ("ACPI event %s\n", event);
 		} else if (errno == EPIPE) {
-			fprintf(stderr, "connection closed\n");
+			dbg ("connection closed\n");
 			break;
 		}
 
-		/* TODO: handle event and do stuff */
+		if (sscanf (event, "%s %s %x %x", acpi_path, acpi_name, &acpi_num1, &acpi_num2) == 4) {
+			char udi[256];
 
-	}
+			snprintf (udi, sizeof (udi), "/org/freedesktop/Hal/devices/acpi_%s", acpi_name);
 
+			if (strncmp (acpi_path, "button/", sizeof ("button/") - 1) == 0) {
+				dbg ("button event");
 
-out:
+				/* TODO: only rescan if button got state */
+				dbus_error_init (&error);
+				libhal_device_rescan (ctx, udi, &error);
+
+				dbus_error_init (&error);
+				libhal_device_emit_condition (ctx, udi, "ButtonPressed", "", &error);
+
+			} else if (strncmp (acpi_path, "ac_adapter", sizeof ("ac_adapter/") - 1) == 0) {
+				dbg ("ac_adapter event");
+				dbus_error_init (&error);
+				libhal_device_rescan (ctx, udi, &error);
+			} else if (strncmp (acpi_path, "battery/", sizeof ("battery/") - 1) == 0) {
+				dbg ("battery event");
+			}
+
+		} else {
+			dbg ("cannot parse event");
+		}
+		
+	}
+	
+	
+	out:
 	if (fd >= 0)
 		close (fd);
 

Index: addon-storage.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux2/addons/addon-storage.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- addon-storage.c	16 Feb 2005 22:25:20 -0000	1.3
+++ addon-storage.c	2 Mar 2005 19:19:13 -0000	1.4
@@ -169,6 +169,10 @@
 		goto out;
 	if ((drive_type = getenv ("HAL_PROP_STORAGE_DRIVE_TYPE")) == NULL)
 		goto out;
+
+	if ((getenv ("HALD_VERBOSE")) != NULL)
+		is_verbose = TRUE;
+
 	storage_policy_should_mount_str = getenv ("HAL_PROP_STORAGE_POLICY_SHOULD_MOUNT");
 
 	support_media_changed_str = getenv ("HAL_PROP_STORAGE_CDROM_SUPPORT_MEDIA_CHANGED");
@@ -338,7 +342,7 @@
 			media_status = MEDIA_STATUS_NO_MEDIA;
 		
 		
-		dbg ("polling %s; got media=%d", device_file, got_media);
+		/*dbg ("polling %s; got media=%d", device_file, got_media);*/
 		
 	skip_check:
 




More information about the hal-commit mailing list