August 19, 2010

DBus Example

« Spring MVC and UTF-8 Encoding with CharacterEncodingFilter | Main | Track window and widget events with AT-SPI »

DBus represents an inter-process communication framework (IPC), which is especially used on Linux desktop environments. DBus is a freedesktop.org sub project. It was initiated for standardization of inter-process communication between different UI/window manager frameworks (KDE/Gnome) and Linux kernel. The DBus protocol has a binary format. A central DBus daemon receives messages of the clients and distributes them (broadcast/one-to-one).

For example if an USB stick will be inserted, the HAL DBus client (Hardware Abstraction Layer) informs all file browsers and desktops about this new hardware. You can use the libdbus library to implement C based DBus clients. The following basic example describes how to send a message to the bus.



#include 
#include 
 
static void send_dbus_message (DBusConnection *connection, const char *msg)
{
    DBusMessage *message;
    //initiliaze the message
    message = dbus_message_new_signal ("/org/developers/blog/tablet/event",
                                       "org.developers.blog.tablet.text.focus.event",
                                       msg);
    
    //send the message
    dbus_connection_send (connection, message, NULL);
    //deallocate the message
    dbus_message_unref (message);
}
 
int main (int argc, char **argv)
{
    DBusConnection *connection;
    DBusError error;
    
    //init error message
    dbus_error_init (&error);
    connection = dbus_bus_get (DBUS_BUS_SESSION, &error);
    if (!connection)
    {
        printf ("Connection to D-BUS daemon failed: %s", error.message);
    
        //deallocate error message
        dbus_error_free (&error);
        return 1;
    }
 
    send_dbus_message (connection, "TabletUITextFocusEvent");
    return 0;
}

On Ubuntu systems you have to build this source snippet with the following command.

gcc `pkg-config --cflags --libs dbus-1` -o dbus-example dbus-example.c

After execution of the example you can see your message on console display with the command: dbus-monitor

signal sender=:1.166 -> dest=(null destination) serial=2 path=/org/developers/blog/tablet/event; interface=org.developers.blog.tablet.text.focus; member=TabletUITextFocusEvent

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 7:48 AM in Linux

 

[Trackback URL for this entry]

Comment: Francisco at Mo, 29 Nov 5:18 PM

Thank you very much for your example

Comment: Antonio at Fr, 17 Dez 8:58 PM

Hi Rafael.

Thanks for the example. I hope you can help me to compile the example. I am trying to compile your example in Ubuntu 10.10 but it cannot find dbus package.

$ pkg-config --cflags --libs dbus
Package dbus was not found in the pkg-config search path.
Perhaps you should add the directory containing `dbus.pc'
to the PKG_CONFIG_PATH environment variable
No package 'dbus' found

$ ls /usr/lib/pkgconfig/dbus*
/usr/lib/pkgconfig/dbus-python.pc

I also cannot find a dubs-1-dev package available to apt-get.

Thank you.

Comment: Manikanta at Mo, 30 Mai 12:50 PM

Hi,I am a newbie to dbus i just tried this example ,
I think ur searching with wrong string for dbus,please use the following
1) sudo apt-get install libdbus-1-dev which will install you the dbus and
2)add the dbus.pc file to pkg-config using the following command
export PKG_CONFIG_PATH=/usr/lib/pkgconfig/dbus-python.pc :$PKG_CONFIG_PATH

3)Change the header file in the program

#include <dbus dbus.h=""> to #include <dbus/dbus.h>
now compile the program

i have done the same way no compilation errors were found and even no output was also shown

Thank you

Comment: 6 at Di, 19 Jul 7:14 AM

lbdbus-1-dev . That's the package.

Comment: 6 at Mi, 20 Jul 9:49 AM

If you can't find the package through sinaptics, just type in a terminal/terminator window sudo apt-get install libdbus-1-dev . That outta do it.

Good luck.

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
 
test