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: Screenshotting for Fun and Profit!||Oct 20, 2016|
|Nasdaq Selects Drupal 8||Oct 19, 2016|
|Canonical Ltd.'s Ubuntu Core||Oct 19, 2016|
|Build Your Own Raspberry Pi Camera||Oct 18, 2016|
|Netlist, Inc.'s HybriDIMM Storage Class Memory||Oct 17, 2016|
|Secure Desktops with Qubes: Compartmentalization||Oct 13, 2016|
- Non-Linux FOSS: Screenshotting for Fun and Profit!
- Nasdaq Selects Drupal 8
- Canonical Ltd.'s Ubuntu Core
- Build Your Own Raspberry Pi Camera
- Secure Desktops with Qubes: Compartmentalization
- Tech Tip: Really Simple HTTP Server with Python
- Netlist, Inc.'s HybriDIMM Storage Class Memory
- The Peculiar Case of Email in the Cloud
- Returning Values from Bash Functions
- SUSECON 2016: Where Technology Reigns Supreme