IPC with D-Bus in Linux

08 Feb 2008

D-bus is an interprocess communication mechanism using message buses. It is a way for running applications to communicate with one another.

Eg: We can write a program that communicates with Pidgin the IM program to get the buddy list.

Developers can enable D-bus support in their applications and expose a set of functionalities to be accessed via D-bus. Quite a few applications already have D-bus support.

Communicating to an D-Bus enabled Application

We can use a D-Bus API to communicate to a running application, that supports D-Bus. D-Bus APIs are available in many languages , starting from a very low level C API. Ruby D-Bus API is called Rbus.

Installing Rbus

sudo apt-get install dbus sudo apt-get install libdbus-1-3 sudo apt-get install libdbus-glib-1-dev sudo gem install rbus

Using Rbus to retrieve the 'now playing' songfrom Rhythmbox

Rhythmbox is a music player inspired by Apple ITunes, and is D-Bus enabled.

sudo apt-get install rhythmbox

The following code fragment establishes a D-bus connection with Rhythmbox and gets the currently playing song filename path from it.

sessionbus = RBus.sessionbus rhythmbox = sessionbus.getobject('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player') p rhythmbox.getPlayingUri()

Refer this for more examples on Rbus

Using Rbus to set status message in Pidgin

Pidgin is an opensource Instan messenger program. A detailed document on Pidgin's D-Bus support is available here.

sessionbus = RBus.sessionbus pidgin = sessionbus.getobject("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")

status = pidgin.PurpleSavedstatusNew(test,2) pidgin.PurpleSavedstatusSetMessage(status, "hello world") pidgin.PurpleSavedstatusActivate(status)

  1. Connects to Pidgin via D-bus
  2. Creates a new status message with name "test" and type 2 (2 stands for 'available')
  3. Saves it with status message as "Hello World"
  4. Activates it.
Making Rhythmbox's 'now playing' song the status message of Pidgin

Idea is to write a ruby program that communicates with Rhythmbox and pidgin

  1. to get the current song from pidgin
2. get the song's details from its mp3 tags (assuming its an mp3 file)

  1. set pidgin's status message with the extracted details, like song title, artist, album etc.

Extracting details from mp3 tags is extremely simple using the mp3info library

Mp3Info.open("myfile.mp3") do |mp3|
   puts mp3.tag.title
   puts mp3.tag.artist
   puts mp3.tag.album
   puts mp3.tag.tracknum

mp3.tag.title = "track title" mp3.tag.artist = "artist name" end

Complete program is available for download here

After starting pidgin and rhythmbox (play some mp3file with non-empty tags !), run the program as "ruby now_playing.rb". Pidgin's status message will now start showing the title of the playing song in Rhythm box :)