HAL hello world?

Damjan Jovanovic dj015 at yahoo.com
Tue Apr 4 23:58:28 PDT 2006


Hi

I'm trying to make a simple C app that can detect
newly connected hardware, and the following is not
working for me. Please help, and then put it somewhere
visible in the HAL documentation.

Basically, g-v-m works fine for me, but this code
segfaults in the "Getting property..." part, and if I
comment that part out, it picks up no hardware. If I
use the commented out libhal_ctx_init_direct()
instead, there is an error (HALD_DIRECT_ADDR or
whatever environment variable doesn't exist).

Also, how does HAL's "main loop integration work"? It
doesn't seem used anywhere in the code.

Thank you
Damjan

#include <stdio.h>
#include <libhal.h>
#include <unistd.h>

void DeviceAdded(LibHalContext *context, const char
*udi)
{
	printf("Device added: %s\n", udi);
}

int main()
{
	DBusError dbusError;
	DBusConnection *dbusConnection;
	LibHalContext *context = NULL;
	char *property;

	dbus_error_init(&dbusError);


	printf("Connecting to dbus...\n");
	dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM,
&dbusError);
	if (!dbusConnection || dbus_error_is_set(&dbusError))
		goto done;

	printf("Allocating context...\n");
	context = libhal_ctx_new();
	if (!context)
		goto done;

	printf("Setting connection...\n");
	if (!libhal_ctx_set_dbus_connection(context,
dbusConnection))
		goto done;

	printf("Initializing hal context...\n");
	if (!libhal_ctx_init(context, &dbusError)
		|| dbus_error_is_set(&dbusError))
	{
		goto done;
	}

/*
	context = libhal_ctx_init_direct(&dbusError);
	if (!context || dbus_error_is_set(&dbusError))
		goto done;
*/

	printf("Setting callback...\n");
	if (!libhal_ctx_set_device_added(context,
DeviceAdded))
		goto done;

	printf("Getting property...\n");
	dbus_error_init(&dbusError);
	property = libhal_device_get_property_string(context,

		"/org/freedesktop/Hal/devices/ide_1_0",
		"info.bus",
		&dbusError);
	if (strlen(property) == 0 ||
dbus_error_is_set(&dbusError))
		goto done;
	printf("It's %s\n", property);

	printf("Waiting for new devices...\n");
	for (;;)
	{
	
dbus_connection_dispatch(*(DBusConnection**)context);
		sleep(1);
	}

done:

	printf("Error");	
	if (dbus_error_is_set(&dbusError))
		printf(": %s", dbusError.message);
	printf("\n");

	if (context)
	{
		libhal_ctx_shutdown(context, &dbusError);
		libhal_ctx_free(context);
	}
}


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the hal mailing list