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)
- 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.
Idea is to write a ruby program that communicates with Rhythmbox and pidgin
- to get the current song from pidgin
- 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.tracknumComplete program is available for download heremp3.tag.title = "track title" mp3.tag.artist = "artist name" end
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 :)