HAL hello world?

Damjan Jovanovic dj015 at yahoo.com
Wed Apr 5 23:44:55 PDT 2006


I read up some more on the dbus docs and found the
problem. Apparently dbus keeps 2 message queues for
each DBusConnection, and calling
dbus_connection_dispatch() only processes messages
already in the incoming queue, it doesn't actually
read from the socket to form new messages. I had to
use dbus_connection_read_write_dispatch() instead,
which both does socket I/O to form new messages, and
then processes them.

So this works. Now would you please put it into the
examples/ directory, so your project actually has a C
example, instead of just a Python one?

#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;

	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;
	}

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

	printf("Waiting for new devices...\n");
	for (;;)
	{
	dbus_connection_read_write_ dispatch(dbusConnection,
-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);
	}
}

--- Marko Anastasov <marko at marko.anastasov.name>
wrote:

> Cao,
> 
> Jesi gledao source g-v-m-a? Tamo mozes videti kako
> se radi
> mainloop integracija. while(1) u principu ne
> funkcionise.
> Onda jos u nekom init_hal() registrujes callback
> funkcije
> i to je to.
> 
> Pozdrav :)
> 
> Marko

mainloop integracija nije potrebna. g-v-m koristi
glib, ja ga necu. init_hal() je unutra "#if 0" u
verziji 0.5.5, sada je izgleda dosta samo
libhal_init_context().

Hvala
Damjan
 
> Translation:
> Hi,
>
> Did you look at the g-v-m source? You can see there
> how mainloop integration is done. while(1)
> doesn't work. And in something like init_hal() you
> register your callback functions and that's that.

Translation:
mainloop integration is unnecessary. g-v-m uses glib,
I don't wanna. init_hal() is inside an "#if 0" in
version 0.5.5, it looks like only
libhal_init_context() is needed now.

>
> У уто, 04. 04 2006. у 23:58 -0700, Damjan
> Jovanovic пише:
> > 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 
> > _______________________________________________
> > hal mailing list
> > hal at lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/hal
> > 
> > 
> 
> 


__________________________________________________
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