How can i use DBusWatch struct

geaaru geaaru at
Wed Sep 19 08:50:43 PDT 2007

I see dbus_connection_setup_with_g_main implementation but i haven't
still clear idea.

My Target: manage events on dbus bus with an external loop (for example
with a select). So, for do it i need a file descriptor that can't be
recover (or it is advised from API documentation) with
dbus_connection_get_fd (conn). So, i'm tring to use DBusWatch...
Well, i set dbus_connection_set_watch_functions:
int easydbus_enable_watcher (...) {
dbus_connection_set_watch_functions (core->conn,
		easydbus_toggle_watch_callback, core, NULL);
return core->fd;
where callback functions are so implemented:

dbus_bool_t easydbus_add_watch_callback 
	(DBusWatch *watch, void *data) {
	struct EasyDbus_core *core = 
		(struct EasyDbus_core *) data;

	int flags = dbus_watch_get_flags (watch);
	int fd = dbus_watch_get_fd (watch);

	if (!dbus_watch_get_enabled (watch)) {
		EasyDbusDebug ("Watch not enabled");
		return TRUE;

	if (flags & DBUS_WATCH_READABLE) {
		EasyDbusDebug ("flag readable");
	if (flags & DBUS_WATCH_WRITABLE) {
		EasyDbusDebug ("flag writable");
	/// temporary solution for understand how DBusWatch
	// struct works.
	core->fd = fd;
	EasyDbusDebug ("i'm here %d %d", flags, core->fd);
	if (core->watch != NULL) {
		core->watch = watch;
		dbus_watch_set_data (watch, data, NULL);
	return TRUE;	

void easydbus_remove_watch_callback 
	(DBusWatch *watch, void *data) {
	struct EasyDbus_core *core = 
		(struct EasyDbus_core *) data;
	core->watch = NULL;
	EasyDbusDebug ("Remove Watch with fd = %d",

void easydbus_toggle_watch_callback 
	(DBusWatch *watch, void *data) {
	EasyDbusDebug ("do nothings for now.");

int easydbus_object_handle_message (data) {
	// first solution
	dbus_bus_connection_read_write_dispatch (core->conn, 0);

Then on my application do:
	// register object callback for manage method request
	int fd =  easydbus_enable_watcher (data);
	printf ("FD %d\n", fd);
	while (1) {
		FD_ZERO (&read);
		FD_SET (fd, &read);
		select (fd+1, &read, NULL, NULL, NULL);
		easydbus_object_handle_message (data);

So, with this solution first message (NameAcquired) isn't handled from
DBusWatch, while is managed if i send a method call. In this case only
first message is managed that is NameAcquired and not my method. Only on
second time is managed my method.
Hovewer, still on third time my method is still not managed.
I known (or i think) that dbus_bus_connection_read_write_dispatch()
isn't a good solution with DBusWatch, but how can i manage correctly
this scenario?
Still, with DBusWatch (and dbus_connection_set_watch_functions(...)) is
needed use dbus_connection_register_object_path function for set
callback or is advertise use directly a callback set with
dbus_connection_set_dispatch_status_function and
To remove dbus_bus_connection_read_write_dispatch i need use
dbus_connection_pop_message function for free queue and update DBusWatch
When is called toggle watch callback functions?



On Tue, 2007-09-18 at 13:53 -0400, Havoc Pennington wrote:
> Your best bet is to look at examples, such as
> dbus_connection_setup_with_g_main() in dbus-glib, or the equivalent in
> Qt.
> Havoc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 481 bytes
Desc: This is a digitally signed message part
Url : 

More information about the dbus mailing list