Control Your Linux Desktop with D-Bus
In the rest of this article, I show some of the D-Bus functionality exposed by some popular applications and write some scripts to communicate with those applications and automate some tasks. Hopefully, this will give you some inspiration to communicate with your own favorite applications. I use different D-Bus tools and script languages to show the different ways you can use D-Bus.
I've already mentioned the first way to make use of D-Bus: by using the KDE programs qdbusviewer and qdbus. However, if you're not into KDE, you can use the command-line programs dbus-send and dbus-monitor to send and monitor D-Bus messages, respectively. For example, you can put the system into suspend mode with the following command:
$ dbus-send --dest=org.freedesktop.PowerManagement \ /org/freedesktop/PowerManagement \ org.freedesktop.PowerManagement.Suspend
As you can see, the dbus-send calls are almost identical to the ones with qdbus. The only difference is that you have to use the --dest parameter for the service name. But, let's look at something new. If you are watching a long YouTube video in your browser, the screensaver can kick in, because the Flash plugin doesn't communicate with the rest of your system. With D-Bus, you can stop this annoying behavior. The magic command is this:
$ dbus-send --print-reply \ --dest=org.gnome.ScreenSaver / \ org.gnome.ScreenSaver.Inhibit \ string:"YouTube" \ string:"Inhibit Screensaver"
With this command, you call the Inhibit method of the org.gnome.ScreenSaver interface with two arguments. The first one is the application's name. I use “YouTube” here, but it can be an arbitrary name. The second argument is the reason to inhibit the screensaver. dbus-send expects each argument to be preceded by its type, such as string, boolean, int16 and so on. The two arguments here are strings. I also use the argument --print-reply, because I need the reply of the command: the Inhibit method returns a uint32 number, which is a “cookie” identifying the inhibit request. If you want to uninhibit the screensaver, you have to send this cookie as the argument:
$ dbus-send --dest=org.gnome.ScreenSaver / \ org.gnome.ScreenSaver.UnInhibit \ uint32:1234567890
With these two commands, you can hack your own personal screensaver-inhibition shell script. Note: you need to save the cookie to a variable or a file when the first command runs and then substitute the actual cookie value in the command above.
If you are debugging D-Bus scripts or observing the methods and signals of other D-Bus applications, the command-line program dbus-monitor comes in very handy. Just fire it up in a terminal, and you will see all D-Bus activity scrolling by. dbus-monitor is useful for seeing all D-Bus activity in real time. So if something is happening on your system, for example, your network goes down, you can see in the output of dbus-monitor how this message is sent to the D-Bus bus. This way, you know which signals to listen for or which methods to call to tap in to the same events.
dbus-monitor also allows you to specify a set of expressions you want to watch—for example:
$ dbus-monitor --system "interface='org.freedesktop.NetworkManager'"
This will monitor all NetworkManager events. I use the --system argument because NetworkManager uses the system bus.
The Liferea feed reader has a small but interesting set of D-Bus methods. The most interesting method is Subscribe, which allows you to add a feed to Liferea from another application. One application that uses this is FeedBag, a Firefox extension that modifies the feed button in the browser's address bar: if you click on the button, it will add a subscription to Liferea instead of to the Live Bookmarks. Under the hood, this works because FeedBag calls the org.gnome.feed.Reader.Subscribe method. You can do the same from a terminal:
$ feed="http://feeds2.feedburner.com/linuxjournalcom?format=xml" $ dbus-send --dest=org.gnome.feed.Reader \ /org/gnome/feed/Reader \ org.gnome.feed.Reader.Subscribe \ string:"$feed"
Liferea provides a script, liferea-add-feed, which does exactly this, but with some added error handling.
Liferea also exposes some information via D-Bus, which is interesting if you have an alternative window manager that is not using Liferea's notification area. Then, you can brew your own notification system—just ask for the number of new and unread items in Liferea and show the output:
$ dbus-send --print-reply \ --dest=org.gnome.feed.Reader \ /org/gnome/feed/Reader \ org.gnome.feed.Reader.GetNewItems $ dbus-send --print-reply \ --dest=org.gnome.feed.Reader \ /org/gnome/feed/Reader \ org.gnome.feed.Reader.GetUnreadItems
|Non-Linux FOSS: libnotify, OS X Style||Jun 18, 2013|
|Containers—Not Virtual Machines—Are the Future Cloud||Jun 17, 2013|
|Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer||Jun 12, 2013|
|Weechat, Irssi's Little Brother||Jun 11, 2013|
|One Tail Just Isn't Enough||Jun 07, 2013|
|Introduction to MapReduce with Hadoop on Linux||Jun 05, 2013|
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Introduction to MapReduce with Hadoop on Linux
- RSS Feeds
- New Products
- Weechat, Irssi's Little Brother
- Tech Tip: Really Simple HTTP Server with Python
- Poul-Henning Kamp: welcome to
21 min 46 sec ago
- This has already been done
22 min 46 sec ago
- Reply to comment | Linux Journal
1 hour 8 min ago
- Welcome to 1998
1 hour 56 min ago
- notifier shortcomings
2 hours 20 min ago
3 hours 57 min ago
- Android User
3 hours 58 min ago
- Reply to comment | Linux Journal
5 hours 51 min ago
8 hours 41 min ago
- This is a good post. This
13 hours 54 min ago