IPC with D-Bus in Linux
February 8th, 2008D-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.
session_bus = RBus.session_bus
rhythmbox = session_bus.get_object('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.
session_bus = RBus.session_bus
pidgin = session_bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
status = pidgin.PurpleSavedstatusNew(test,2)
pidgin.PurpleSavedstatusSetMessage(status, "hello world")
pidgin.PurpleSavedstatusActivate(status)
- Connects to Pidgin via D-bus
- Creates a new status message with name “test” and type 2 (2 stands for ‘available’)
- Saves it with status message as “Hello World”
- 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
- to get the current song from pidgin
2. get the song’s details from its mp3 tags (assuming its an mp3 file)
3. 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 ![]()
[...] using the Dbus interface of Rhythmbox music player (More details on this can be found in my article on IPC with Dbus. Once the songname was obtained, pushing it to the browsers required only the following code in the [...]
I wanted to learn more about DBus, and my first thoughts were “Wouldn’t it be cool to make a Ruby script that set my Pidgin status with Rhythmbox’s playing song? Probably has been done before…”
So of course, thank you!